or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-operations.mddata-conversion.mdhex-operations.mdindex.mdsignature-operations.mdtype-checking.md
tile.json

index.mddocs/

@ethersproject/bytes

@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.

Package Information

  • Package Name: @ethersproject/bytes
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @ethersproject/bytes

Core Imports

import { 
  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");

Basic Usage

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");
}

Architecture

The package is organized around several key functional areas:

  • Type Checking: Validation functions to identify valid byte-like data and formats
  • Data Conversion: Core conversion between bytes, hex strings, numbers, and other formats
  • Array Operations: Functions for manipulating byte arrays (concatenation, padding, stripping)
  • Hex Operations: Advanced hex string manipulation and formatting utilities
  • Signature Handling: Cryptographic signature parsing and reconstruction for Ethereum

Capabilities

Type Checking and Validation

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;

Type Checking and Validation

Data Conversion

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;

Data Conversion

Array Operations

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;

Array Operations

Hex String Operations

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;

Hex String Operations

Signature Operations

Functions for parsing and reconstructing cryptographic signatures used in Ethereum transactions.

function splitSignature(signature: SignatureLike): Signature;
function joinSignature(signature: SignatureLike): string;

Signature Operations

Core Types

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;
}