Utility¶
- getBytes(data, encoding='hex')¶
Converts input data to a Uint8Array.
- Arguments:
data (string | Uint8Array<ArrayBufferLike> | number[] | null | undefined) – Input data as a string, number array, Uint8Array, or null/undefined.
encoding (“hex” | “utf8” | “base64”) – Encoding to use if input is a string (‘hex’, ‘utf8’, ‘base64’). Default is ‘hex’.
- Returns:
Uint8Array – Uint8Array representation of the input data.
- toBuffer(input, encoding='utf8')¶
Converts input to a Uint8Array (buffer).
- Arguments:
input (string | ArrayBuffer | ArrayLike<number>) – Input as a string, ArrayBuffer, or Array-like number array.
encoding (“hex” | “utf8” | “base64”) – Encoding for string inputs (‘utf8’, ‘hex’, ‘base64’). Default is ‘utf8’.
- Returns:
Uint8Array – Uint8Array representation of input.
- hexToBytes(hex)¶
Converts a hex string to a Uint8Array.
- Arguments:
hex (string) – Hexadecimal string (optionally prefixed with ‘0x’).
- Returns:
Uint8Array – Uint8Array representing the hex string.
- bytesToHex(bytes, prefix=false)¶
Converts a Uint8Array to a hex string.
- Arguments:
bytes (Uint8Array) – Data to convert.
prefix (boolean) – Whether to add ‘0x’ prefix. Default is false.
- Returns:
string – Hexadecimal string.
- bytesToString(data)¶
Converts a string or Uint8Array to a hex string.
- Arguments:
data (string | Uint8Array<ArrayBufferLike> | null | undefined) – Input string or Uint8Array.
- Returns:
string – Hexadecimal string representation of input.
- randomBytes(len)¶
Generates cryptographically secure random bytes.
- Arguments:
len (number) – Number of random bytes to generate.
- Returns:
Uint8Array – Uint8Array of random bytes.
- bytesToInteger(bytes, littleEndian=false)¶
Converts a Uint8Array to a bigint.
- Arguments:
bytes (Uint8Array) – Byte array to convert.
littleEndian (boolean) – Whether to interpret bytes in little-endian order. Default is false.
- Returns:
bigint – bigint representation of bytes.
- ensureString(data)¶
Ensures the input is a string.
- Arguments:
data (string | Uint8Array<ArrayBufferLike>) – Input as a string or Uint8Array.
- Returns:
string – Input converted to string.
- stringToInteger(data)¶
Converts a string or Uint8Array to a bigint.
- Arguments:
data (string | Uint8Array<ArrayBufferLike>) – Input string (hex) or Uint8Array.
- Returns:
bigint – bigint representation of input.
- equalBytes(a, b)¶
Compares two Uint8Arrays for equality.
- Arguments:
a (Uint8Array) – First array.
b (Uint8Array) – Second array.
- Returns:
boolean – true if arrays are equal, false otherwise.
- integerToBytes(value, length, endianness='big')¶
Converts a bigint or number to a Uint8Array.
- Arguments:
value (number | bigint) – Value to convert.
length (number) – Optional fixed byte length for output.
endianness (“big” | “little”) – ‘big’ or ‘little’ endian. Default is ‘big’.
- Returns:
Uint8Array – Uint8Array representing the integer.
- concatBytes(...chunks)¶
Concatenates multiple Uint8Arrays.
- Arguments:
chunks (Uint8Array<ArrayBufferLike>[]) – Arrays to concatenate.
- Returns:
Uint8Array – Concatenated Uint8Array.
- bytesToBinaryString(data, zeroPadBits=0)¶
Converts bytes to a binary string.
- Arguments:
data (Uint8Array) – Input bytes.
zeroPadBits (number) – Optional zero-padding to reach a specific bit length.
- Returns:
string – Binary string representation of bytes.
- binaryStringToInteger(data)¶
Converts a binary string or bytes to a bigint.
- Arguments:
data (string | Uint8Array<ArrayBufferLike>) – Binary string or Uint8Array.
- Returns:
bigint – bigint representation.
- integerToBinaryString(data, zeroPadBits=0)¶
Converts an integer to a binary string.
- Arguments:
data (number | bigint) – Input number or bigint.
zeroPadBits (number) – Optional zero-padding to reach a specific bit length.
- Returns:
string – Binary string.
- binaryStringToBytes(data, zeroPadByteLen=0)¶
Converts a binary string or Uint8Array to bytes.
- Arguments:
data (string | Uint8Array<ArrayBufferLike>) – Input binary string or bytes.
zeroPadByteLen (number) – Optional zero-padding to reach a specific byte length.
- Returns:
Uint8Array – Uint8Array representation.
- isAllEqual(...inputs)¶
Checks if all inputs are equal.
- Arguments:
inputs ((string | number | boolean | ArrayBuffer | Uint8Array<ArrayBufferLike> | string[] | number[] | ArrayBufferView<ArrayBufferLike> | boolean[])[]) – Inputs of various types to compare.
- Returns:
boolean – true if all inputs are equal, false otherwise.
- generatePassphrase(length=32, chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')¶
Generates a random alphanumeric passphrase.
- Arguments:
length (number) – Length of the passphrase. Default is 32.
chars (string) – Characters to use. Default is alphanumeric.
- Returns:
string – Randomly generated passphrase string.
- getHmac(eccName)¶
Returns HMAC seed based on ECC name.
- Arguments:
eccName (string) – ECC curve name.
- Returns:
Uint8Array – Seed as Uint8Array.
- excludeKeys(nested, keys)¶
Excludes specific keys from nested objects recursively.
- Arguments:
nested (Record<string, any>) – Input object.
keys (string[]) – Keys to exclude.
- Returns:
Record<string, any> – New object excluding specified keys.
- pathToIndexes(path)¶
Converts derivation path string to an array of indexes.
- Arguments:
path (string) – Derivation path (e.g., “m/44’/0’/0’”).
- Returns:
number[] – Array of numeric indexes.
- indexesToPath(indexes)¶
Converts array of indexes to a derivation path string.
- Arguments:
indexes (number[]) – Array of numeric indexes.
- Returns:
string – Derivation path string.
- normalizeIndex(index, hardened=false)¶
Normalize a BIP32 derivation index to a tuple.
- Arguments:
index (IndexType) – Index as number, string (e.g., “0” or “0-3”), or tuple [from, to].
hardened (boolean) – Whether the index is hardened (default: false)
- Returns:
DerivationsType – Normalized derivation tuple.
- normalizeDerivation(path, indexes)¶
Normalize a derivation path string or numeric indexes.
- Arguments:
path (string) – Optional path string like “m/0’/1-3’”
indexes (number[]) – Optional array of numeric indexes
- Returns:
[string, number[], DerivationsType[]] – Tuple: [normalized path string, indexes array, derivation tuples]
- indexTupleToInteger(idx)¶
Convert a derivation tuple to integer representation
- Arguments:
idx (DerivationsType) – Derivation tuple [index, hardened] or [from, to, hardened]
- Returns:
number – Integer value with hardened bit applied if necessary
- indexTupleToString(idx)¶
Convert a derivation tuple to a string representation
- Arguments:
idx (DerivationsType) – Derivation tuple [index, hardened] or [from, to, hardened]
- Returns:
string – String like “0’” or “0-3’”
- indexStringToTuple(i)¶
Convert a single index string “0’” into tuple [0, true]
- Arguments:
i (string) – Index string
- Returns:
[number, boolean] – Tuple [index number, hardened boolean]
- xor(a, b)¶
XOR two Uint8Arrays of equal length
- Arguments:
a (Uint8Array) – First byte array
b (Uint8Array) – Second byte array
- Returns:
Uint8Array – New Uint8Array resulting from XOR
- addNoCarry(a, b)¶
Add two Uint8Arrays element-wise modulo 256
- Arguments:
a (Uint8Array) – First byte array
b (Uint8Array) – Second byte array
- Returns:
Uint8Array – New Uint8Array result
- multiplyScalarNoCarry(data, scalar)¶
Multiply each byte by a scalar modulo 256
- Arguments:
data (Uint8Array) – Byte array
scalar (number) – Integer scalar
- Returns:
Uint8Array – New Uint8Array result
- isBitsSet(value, bitNum)¶
Bit manipulation helpers
- Arguments:
value (number)
bitNum (number)
- Returns:
boolean
- areBitsSet(value, mask)¶
- Arguments:
value (number)
mask (number)
- Returns:
boolean
- setBit(value, bitNum)¶
- Arguments:
value (number)
bitNum (number)
- Returns:
number
- setBits(value, mask)¶
- Arguments:
value (number)
mask (number)
- Returns:
number
- resetBit(value, bitNum)¶
- Arguments:
value (number)
bitNum (number)
- Returns:
number
- resetBits(value, mask)¶
- Arguments:
value (number)
mask (number)
- Returns:
number
- bytesReverse(data)¶
Reverse a Uint8Array
- Arguments:
data (Uint8Array) – Byte array
- Returns:
Uint8Array – New Uint8Array reversed
- convertBits(data, fromBits, toBits)¶
Convert bits between different widths
- Arguments:
data (Uint8Array<ArrayBufferLike> | number[]) – Array of numbers or Uint8Array
fromBits (number) – Original bit width
toBits (number) – Target bit width
- Returns:
number[] | null – Array of converted bits or null if input invalid
- bytesChunkToWords(bytesChunk, wordsList, endianness)¶
Convert a byte chunk to mnemonic words
- Arguments:
bytesChunk (Uint8Array) – Uint8Array of bytes
wordsList (string[]) – Wordlist array
endianness (“big” | “little”) – “little” or “big”
- Returns:
[string, string, string] – Tuple of 3 mnemonic words
- wordsToBytesChunk(w1, w2, w3, wordsList, endianness)¶
Convert 3 mnemonic words to a byte chunk
- Arguments:
w1 (string) – Word 1
w2 (string) – Word 2
w3 (string) – Word 3
wordsList (string[]) – Wordlist array
endianness (“big” | “little”) – “little” or “big”
- Returns:
Uint8Array – Uint8Array of bytes
- toCamelCase(input)¶
Convert kebab-case string to camelCase
- Arguments:
input (string) – Input string
- Returns:
string – CamelCase string
- ensureTypeMatch(instanceOrClass, expectedType, options={})¶
Ensure a value matches the expected type(s)
- Arguments:
instanceOrClass (any) – Value or class instance
expectedType (any) – Expected type or constructor
options (EnsureTypeMatchOptionsInterface) – Optional object with otherTypes, strict, errorClass
- Returns:
any – Value or object with {value, isValid}