viem
Version:
134 lines • 4.75 kB
TypeScript
import type { ErrorType } from '../../errors/utils.js';
import type { ByteArray, Hex } from '../../types/misc.js';
import { type IsHexErrorType } from '../data/isHex.js';
import { type PadErrorType } from '../data/pad.js';
import { type AssertSizeErrorType } from './fromHex.js';
import { type NumberToHexErrorType, type NumberToHexOpts } from './toHex.js';
export type ToBytesParameters = {
/** Size of the output bytes. */
size?: number | undefined;
};
export type ToBytesErrorType = NumberToBytesErrorType | BoolToBytesErrorType | HexToBytesErrorType | StringToBytesErrorType | IsHexErrorType | ErrorType;
/**
* Encodes a UTF-8 string, hex value, bigint, number or boolean to a byte array.
*
* - Docs: https://viem.sh/docs/utilities/toBytes
* - Example: https://viem.sh/docs/utilities/toBytes#usage
*
* @param value Value to encode.
* @param opts Options.
* @returns Byte array value.
*
* @example
* import { toBytes } from 'viem'
* const data = toBytes('Hello world')
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
*
* @example
* import { toBytes } from 'viem'
* const data = toBytes(420)
* // Uint8Array([1, 164])
*
* @example
* import { toBytes } from 'viem'
* const data = toBytes(420, { size: 4 })
* // Uint8Array([0, 0, 1, 164])
*/
export declare function toBytes(value: string | bigint | number | boolean | Hex, opts?: ToBytesParameters): ByteArray;
export type BoolToBytesOpts = {
/** Size of the output bytes. */
size?: number | undefined;
};
export type BoolToBytesErrorType = AssertSizeErrorType | PadErrorType | ErrorType;
/**
* Encodes a boolean into a byte array.
*
* - Docs: https://viem.sh/docs/utilities/toBytes#booltobytes
*
* @param value Boolean value to encode.
* @param opts Options.
* @returns Byte array value.
*
* @example
* import { boolToBytes } from 'viem'
* const data = boolToBytes(true)
* // Uint8Array([1])
*
* @example
* import { boolToBytes } from 'viem'
* const data = boolToBytes(true, { size: 32 })
* // Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
*/
export declare function boolToBytes(value: boolean, opts?: BoolToBytesOpts): ByteArray;
export type HexToBytesOpts = {
/** Size of the output bytes. */
size?: number | undefined;
};
export type HexToBytesErrorType = AssertSizeErrorType | PadErrorType | ErrorType;
/**
* Encodes a hex string into a byte array.
*
* - Docs: https://viem.sh/docs/utilities/toBytes#hextobytes
*
* @param hex Hex string to encode.
* @param opts Options.
* @returns Byte array value.
*
* @example
* import { hexToBytes } from 'viem'
* const data = hexToBytes('0x48656c6c6f20776f726c6421')
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
*
* @example
* import { hexToBytes } from 'viem'
* const data = hexToBytes('0x48656c6c6f20776f726c6421', { size: 32 })
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
*/
export declare function hexToBytes(hex_: Hex, opts?: HexToBytesOpts): ByteArray;
export type NumberToBytesErrorType = NumberToHexErrorType | HexToBytesErrorType | ErrorType;
/**
* Encodes a number into a byte array.
*
* - Docs: https://viem.sh/docs/utilities/toBytes#numbertobytes
*
* @param value Number to encode.
* @param opts Options.
* @returns Byte array value.
*
* @example
* import { numberToBytes } from 'viem'
* const data = numberToBytes(420)
* // Uint8Array([1, 164])
*
* @example
* import { numberToBytes } from 'viem'
* const data = numberToBytes(420, { size: 4 })
* // Uint8Array([0, 0, 1, 164])
*/
export declare function numberToBytes(value: bigint | number, opts?: NumberToHexOpts | undefined): ByteArray;
export type StringToBytesOpts = {
/** Size of the output bytes. */
size?: number | undefined;
};
export type StringToBytesErrorType = AssertSizeErrorType | PadErrorType | ErrorType;
/**
* Encodes a UTF-8 string into a byte array.
*
* - Docs: https://viem.sh/docs/utilities/toBytes#stringtobytes
*
* @param value String to encode.
* @param opts Options.
* @returns Byte array value.
*
* @example
* import { stringToBytes } from 'viem'
* const data = stringToBytes('Hello world!')
* // Uint8Array([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33])
*
* @example
* import { stringToBytes } from 'viem'
* const data = stringToBytes('Hello world!', { size: 32 })
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
*/
export declare function stringToBytes(value: string, opts?: StringToBytesOpts): ByteArray;
//# sourceMappingURL=toBytes.d.ts.map