A collection of useful utilities for @polkadot ecosystem with type checking, data conversion, and performance optimization functions
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Bi-directional conversion utilities for transforming data between different formats commonly used in blockchain applications, including hex strings, Uint8Arrays, numbers, and various text encodings.
Convert between hex strings and other data types with proper validation and formatting.
/**
* Converts hex string to Uint8Array
* @param value - Hex string (with or without 0x prefix)
* @param bitLength - Expected bit length for validation
*/
function hexToU8a(value?: string | null, bitLength?: number): Uint8Array;
/**
* Converts hex string to BigInt
* @param value - Hex string
*/
function hexToBigInt(value: string): bigint;
/**
* Converts hex string to BN (big number)
* @param value - Hex string
*/
function hexToBn(value: string): BN;
/**
* Converts hex string to regular number
* @param value - Hex string
*/
function hexToNumber(value: string): number;
/**
* Converts hex string to UTF-8 string
* @param value - Hex string representing UTF-8 encoded text
*/
function hexToString(value: string): string;Convert Uint8Arrays to various data formats and back.
/**
* Converts Uint8Array to hex string
* @param value - Uint8Array to convert
* @param bitLength - Expected bit length (-1 for no padding)
* @param isPrefixed - Whether to include 0x prefix
*/
function u8aToHex(value?: Uint8Array | null, bitLength?: number, isPrefixed?: boolean): HexString;
/**
* Converts Uint8Array to BigInt
* @param value - Uint8Array in little-endian format
*/
function u8aToBigInt(value: Uint8Array): bigint;
/**
* Converts Uint8Array to BN
* @param value - Uint8Array
* @param options - Conversion options (isLe, isNegative)
*/
function u8aToBn(value: Uint8Array, options?: ToBnOptions): BN;
/**
* Converts Uint8Array to regular number
* @param value - Uint8Array
* @param options - Conversion options
*/
function u8aToNumber(value: Uint8Array, options?: NumberOptions): number;
/**
* Converts Uint8Array to UTF-8 string
* @param value - Uint8Array containing UTF-8 encoded text
*/
function u8aToString(value: Uint8Array): string;
/**
* Converts Uint8Array to Buffer (Node.js)
* @param value - Uint8Array to convert
*/
function u8aToBuffer(value: Uint8Array): Buffer;
/**
* Converts Uint8Array to floating point number
* @param value - Uint8Array (4 or 8 bytes)
* @param options - Conversion options
*/
function u8aToFloat(value: Uint8Array, options?: NumberOptions): number;Convert strings to various binary formats and back.
/**
* Converts UTF-8 string to Uint8Array
* @param value - String to convert
*/
function stringToU8a(value: string): Uint8Array;
/**
* Converts UTF-8 string to hex string
* @param value - String to convert
*/
function stringToHex(value: string): HexString;Convert numbers to binary formats.
/**
* Converts number to hex string
* @param value - Number to convert
* @param bitLength - Expected bit length for padding
*/
function numberToHex(value: number, bitLength?: number): HexString;
/**
* Converts number to Uint8Array
* @param value - Number to convert
* @param bitLength - Expected bit length in bits
*/
function numberToU8a(value: number, bitLength?: number): Uint8Array;
/**
* Converts floating point number to Uint8Array
* @param value - Float number to convert
* @param bitLength - 32 for float32, 64 for float64
*/
function floatToU8a(value: number, bitLength?: 32 | 64): Uint8Array;Convert BigInt values to various formats.
/**
* Converts BigInt to hex string
* @param value - BigInt to convert
* @param bitLength - Expected bit length for padding
*/
function nToHex(value: bigint, bitLength?: number): HexString;
/**
* Converts BigInt to Uint8Array
* @param value - BigInt to convert
* @param bitLength - Expected bit length in bits
*/
function nToU8a(value: bigint, bitLength?: number): Uint8Array;
/**
* Converts number to BigInt
* @param value - Number to convert
*/
function nToBigInt(value: number | bigint | string): bigint;Convert BN (big number) values to various formats.
/**
* Converts BN to hex string
* @param value - BN to convert
* @param bitLength - Expected bit length for padding
*/
function bnToHex(value: BN, bitLength?: number): HexString;
/**
* Converts BN to Uint8Array
* @param value - BN to convert
* @param options - Conversion options
*/
function bnToU8a(value: BN, options?: ToBnOptions): Uint8Array;
/**
* Converts value to BN
* @param value - Value to convert (number, string, bigint, Uint8Array, BN)
*/
function bnToBn(value: BN | number | string | bigint | Uint8Array): BN;
/**
* Creates BN from hex string
* @param value - Hex string
*/
function bnFromHex(value: string): BN;Convert Buffer objects to Uint8Array (Node.js compatibility).
/**
* Converts Buffer to Uint8Array
* @param buffer - Buffer to convert
*/
function bufferToU8a(buffer: Buffer): Uint8Array;Ensure values are in the correct format.
/**
* Ensures value is a Uint8Array, converting if necessary
* @param value - Value to convert (string, number array, or Uint8Array)
*/
function u8aToU8a(value?: U8aLike | null): Uint8Array;Basic Hex Conversion:
import { hexToU8a, u8aToHex } from "@polkadot/util";
// Convert hex string to bytes
const bytes = hexToU8a("0x48656c6c6f"); // [72, 101, 108, 108, 111]
// Convert bytes back to hex
const hex = u8aToHex(bytes); // "0x48656c6c6f"
// Convert without 0x prefix
const hexUnprefixed = u8aToHex(bytes, -1, false); // "48656c6c6f"String Encoding:
import { stringToU8a, u8aToString, stringToHex } from "@polkadot/util";
const message = "Hello, World!";
// String to bytes
const bytes = stringToU8a(message);
// Bytes back to string
const decoded = u8aToString(bytes); // "Hello, World!"
// String directly to hex
const hex = stringToHex(message); // "0x48656c6c6f2c20576f726c6421"Number Conversion:
import { numberToHex, numberToU8a, u8aToNumber } from "@polkadot/util";
const num = 255;
// Number to hex with padding
const hex = numberToHex(num, 16); // "0x00ff"
// Number to bytes
const bytes = numberToU8a(num, 16); // [255, 0] (little-endian)
// Bytes back to number
const decoded = u8aToNumber(bytes); // 255BigInt and BN Conversion:
import {
nToBigInt, nToHex, nToU8a,
bnToBn, bnToHex, bnToU8a
} from "@polkadot/util";
// BigInt conversion
const bigInt = nToBigInt("12345678901234567890");
const hex = nToHex(bigInt); // "0xab54a98ceb1f0ad2"
const bytes = nToU8a(bigInt);
// BN conversion
const bn = bnToBn("12345678901234567890");
const bnHex = bnToHex(bn); // "0xab54a98ceb1f0ad2"
const bnBytes = bnToU8a(bn);type HexString = `0x${string}`;
type U8aLike = number[] | Uint8Array | string;
interface ToBnOptions {
isLe?: boolean;
isNegative?: boolean;
}
interface NumberOptions extends ToBnOptions {
bitLength?: number;
}Install with Tessl CLI
npx tessl i tessl/npm-polkadot--util