@ethersproject/bytes provides comprehensive byte manipulation utilities for the ethers.js ecosystem, designed specifically for handling binary data in Ethereum and blockchain applications. It offers essential functions for converting between different data formats, validating data types, performing byte operations, and working with cryptographic signatures.
npm install @ethersproject/bytesimport {
arrayify, hexlify, concat, isBytes, isBytesLike, isHexString,
stripZeros, zeroPad, splitSignature, joinSignature,
type Bytes, type BytesLike, type Signature
} from "@ethersproject/bytes";For CommonJS:
const {
arrayify, hexlify, concat, isBytes, isBytesLike, isHexString,
stripZeros, zeroPad, splitSignature, joinSignature
} = require("@ethersproject/bytes");import { arrayify, hexlify, concat, isBytesLike } from "@ethersproject/bytes";
// Convert hex string to bytes
const bytes = arrayify("0x1234abcd");
// Result: Uint8Array([0x12, 0x34, 0xab, 0xcd])
// Convert bytes back to hex
const hex = hexlify(bytes);
// Result: "0x1234abcd"
// Concatenate multiple byte arrays
const combined = concat([bytes, arrayify("0xfefe")]);
// Result: Uint8Array([0x12, 0x34, 0xab, 0xcd, 0xfe, 0xfe])
// Validate data types
if (isBytesLike("0x1234")) {
console.log("Valid bytes-like data");
}The package is organized around several key functional areas:
Functions for validating and type-checking binary data formats commonly used in Ethereum applications.
function isBytesLike(value: any): value is BytesLike;
function isBytes(value: any): value is Bytes;
function isHexString(value: any, length?: number): boolean;Core conversion functions for transforming between different binary data representations.
function arrayify(value: BytesLike | Hexable | number, options?: DataOptions): Uint8Array;
function hexlify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): string;Functions for manipulating byte arrays including concatenation, padding, and zero-stripping.
function concat(items: ReadonlyArray<BytesLike>): Uint8Array;
function stripZeros(value: BytesLike): Uint8Array;
function zeroPad(value: BytesLike, length: number): Uint8Array;Advanced utilities for working with hexadecimal string representations of binary data.
function hexDataLength(data: BytesLike): number;
function hexDataSlice(data: BytesLike, offset: number, endOffset?: number): string;
function hexConcat(items: ReadonlyArray<BytesLike>): string;
function hexValue(value: BytesLike | Hexable | number | bigint): string;
function hexStripZeros(value: BytesLike): string;
function hexZeroPad(value: BytesLike, length: number): string;Functions for parsing and reconstructing cryptographic signatures used in Ethereum transactions.
function splitSignature(signature: SignatureLike): Signature;
function joinSignature(signature: SignatureLike): string;type Bytes = ArrayLike<number>;
type BytesLike = Bytes | string;
interface DataOptions {
allowMissingPrefix?: boolean;
hexPad?: "left" | "right" | null;
}
interface Hexable {
toHexString(): string;
}
type SignatureLike = {
r: string;
s?: string;
_vs?: string;
recoveryParam?: number;
v?: number;
} | BytesLike;
interface Signature {
r: string;
s: string;
_vs: string;
recoveryParam: number;
v: number;
yParityAndS: string;
compact: string;
}