Skip to main content
@tevm/voltaire
@tevm/voltaire / index / BrandedBytes

BrandedBytes

Classes

InvalidBytesFormatError

Defined in: src/primitives/Bytes/errors.js:13

Extends

  • Error

Constructors

Constructor
new InvalidBytesFormatError(message, details?): InvalidBytesFormatError
Defined in: src/primitives/Bytes/errors.js:18
Parameters
message
string
details?
any
Returns
InvalidBytesFormatError
Overrides
Error.constructor

Properties

details
details: any
Defined in: src/primitives/Bytes/errors.js:21
name
name: string
Defined in: src/primitives/Bytes/errors.js:20
Inherited from
Error.name

InvalidBytesLengthError

Defined in: src/primitives/Bytes/errors.js:1

Extends

  • Error

Constructors

Constructor
new InvalidBytesLengthError(message, details?): InvalidBytesLengthError
Defined in: src/primitives/Bytes/errors.js:6
Parameters
message
string
details?
any
Returns
InvalidBytesLengthError
Overrides
Error.constructor

Properties

details
details: any
Defined in: src/primitives/Bytes/errors.js:9
name
name: string
Defined in: src/primitives/Bytes/errors.js:8
Inherited from
Error.name

InvalidValueError

Defined in: src/primitives/Bytes/errors.js:25

Extends

  • Error

Constructors

Constructor
new InvalidValueError(message, details?): InvalidValueError
Defined in: src/primitives/Bytes/errors.js:30
Parameters
message
string
details?
any
Returns
InvalidValueError
Overrides
Error.constructor

Properties

details
details: any
Defined in: src/primitives/Bytes/errors.js:33
name
name: string
Defined in: src/primitives/Bytes/errors.js:32
Inherited from
Error.name

Variables

BytesType

const BytesType: object
Defined in: src/primitives/Bytes/Bytes.index.ts:59

Type Declaration

assert()
assert: (value) => BytesType
Assert that value is valid Bytes, throw if not
Parameters
value
unknown Value to check
Returns
BytesType The validated bytes
Throws
If value is not valid Bytes
Example
import * as Bytes from './primitives/Bytes/index.js';
const bytes = Bytes.assert(new Uint8Array([1, 2, 3])); // returns bytes
Bytes.assert("not bytes"); // throws Error
clone()
clone: (bytes) => BytesType
Clone Bytes
Parameters
bytes
BytesType Bytes to clone
Returns
BytesType Cloned Bytes
Example
const copy = Bytes.clone(bytes);
compare()
compare: (a, b) => number
Compare two Bytes (lexicographic)
Parameters
a
BytesType First Bytes
b
BytesType Second Bytes
Returns
number -1 if a < b, 0 if equal, 1 if a > b
Example
const cmp = Bytes.compare(bytes1, bytes2);
concat()
concat: (…arrays) => BytesType
Concatenate multiple Bytes
Parameters
arrays
BytesType[] Bytes to concatenate
Returns
BytesType Concatenated Bytes
Example
const result = Bytes.concat(bytes1, bytes2, bytes3);
equals()
equals: (a, b) => boolean
Check if two Bytes are equal
Parameters
a
BytesType First Bytes
b
BytesType Second Bytes
Returns
boolean True if equal
Example
const equal = Bytes.equals(bytes1, bytes2);
from()
from: (value) => BytesType
Create Bytes from various input types (universal constructor)
Parameters
value
Uint8Array, hex string, UTF-8 string, or number array string | Uint8Array<ArrayBufferLike> | number[]
Returns
BytesType Bytes
Throws
If value type is unsupported or invalid
Example
const b1 = Bytes.from(new Uint8Array([0x01, 0x02]));
const b2 = Bytes.from("0x1234");
const b3 = Bytes.from("hello");
const b4 = Bytes.from([0x01, 0x02, 0x03]);
fromBigInt()
fromBigInt: (value, size?) => BytesType
Convert bigint to Bytes
Parameters
value
bigint BigInt to convert (must be non-negative)
size?
number Optional byte size (pads or throws if too small)
Returns
BytesType Bytes
Throws
If value is negative or doesn’t fit in size
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.fromBigInt(255n);      // Uint8Array([0xff])
Bytes.fromBigInt(255n, 2);   // Uint8Array([0x00, 0xff])
Bytes.fromBigInt(0x1234n);   // Uint8Array([0x12, 0x34])
fromHex()
fromHex: (hex) => BytesType
Create Bytes from hex string
Parameters
hex
string Hex string with 0x prefix
Returns
BytesType Bytes
Throws
If hex string is invalid
Example
const bytes = Bytes.fromHex("0x1234");
// Uint8Array([0x12, 0x34])
fromNumber()
fromNumber: (value, size?) => BytesType
Convert number to Bytes
Parameters
value
number Number to convert (must be safe integer, non-negative)
size?
number Optional byte size (pads or throws if too small)
Returns
BytesType Bytes
Throws
If value is negative, not an integer, or exceeds MAX_SAFE_INTEGER
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.fromNumber(255);     // Uint8Array([0xff])
Bytes.fromNumber(255, 2);  // Uint8Array([0x00, 0xff])
Bytes.fromNumber(0x1234);  // Uint8Array([0x12, 0x34])
fromString()
fromString: (str) => BytesType
Create Bytes from UTF-8 string
Parameters
str
string UTF-8 string
Returns
BytesType Bytes
Example
const bytes = Bytes.fromString("hello");
// Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])
isBytes()
isBytes: (value) => value is BytesType
Check if value is a valid Bytes (Uint8Array)
Parameters
value
unknown Value to check
Returns
value is BytesType True if value is Uint8Array
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.isBytes(new Uint8Array([1, 2, 3])); // true
Bytes.isBytes([1, 2, 3]);                  // false
Bytes.isBytes("0x1234");                   // false
isEmpty()
isEmpty: (bytes) => boolean
Check if Bytes is empty
Parameters
bytes
BytesType Bytes to check
Returns
boolean True if empty
Example
const empty = Bytes.isEmpty(bytes);
padLeft()
padLeft: (bytes, targetSize) => BytesType
Pad Bytes on the left (start) with zeros to target size
Parameters
bytes
BytesType Bytes to pad
targetSize
number Target size in bytes
Returns
BytesType Padded bytes
Throws
If bytes exceeds target size
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.padLeft(new Uint8Array([0x12, 0x34]), 4); // Uint8Array([0x00, 0x00, 0x12, 0x34])
padRight()
padRight: (bytes, targetSize) => BytesType
Pad Bytes on the right (end) with zeros to target size
Parameters
bytes
BytesType Bytes to pad
targetSize
number Target size in bytes
Returns
BytesType Padded bytes
Throws
If bytes exceeds target size
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.padRight(new Uint8Array([0x12, 0x34]), 4); // Uint8Array([0x12, 0x34, 0x00, 0x00])
random()
random: (size) => BytesType
Generate random Bytes of specified size
Parameters
size
number Number of random bytes to generate
Returns
BytesType Random bytes
Example
import * as Bytes from './primitives/Bytes/index.js';
const random32 = Bytes.random(32); // 32 random bytes
const random16 = Bytes.random(16); // 16 random bytes
size()
size: (bytes) => number
Get size of Bytes
Parameters
bytes
BytesType Bytes
Returns
number Size in bytes
Example
const size = Bytes.size(bytes);
slice()
slice: (bytes, start, end?) => BytesType
Slice Bytes
Parameters
bytes
BytesType Bytes to slice
start
number Start index
end?
number End index (optional)
Returns
BytesType Sliced Bytes
Example
const slice = Bytes.slice(bytes, 0, 4);
toBigInt()
toBigInt: (bytes) => bigint
Convert Bytes to bigint
Parameters
bytes
BytesType Bytes to convert
Returns
bigint BigInt value
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.toBigInt(new Uint8Array([0xff]));       // 255n
Bytes.toBigInt(new Uint8Array([0x12, 0x34])); // 4660n
toHex()
toHex: (bytes) => HexType
Convert Bytes to hex string
Parameters
bytes
BytesType Bytes to convert
Returns
HexType Hex string with 0x prefix
Example
const hex = Bytes.toHex(bytes);
// "0x1234"
toNumber()
toNumber: (bytes) => number
Convert Bytes to number
Parameters
bytes
BytesType Bytes to convert
Returns
number Number value
Throws
If value exceeds MAX_SAFE_INTEGER
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.toNumber(new Uint8Array([0xff]));       // 255
Bytes.toNumber(new Uint8Array([0x12, 0x34])); // 4660
toString()
toString: (bytes) => string
Convert Bytes to UTF-8 string
Parameters
bytes
BytesType Bytes to convert
Returns
string UTF-8 string
Example
const str = Bytes.toString(bytes);
// "hello"
trimLeft()
trimLeft: (bytes) => BytesType
Trim leading zeros from Bytes
Parameters
bytes
BytesType Bytes to trim
Returns
BytesType Trimmed bytes
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.trimLeft(new Uint8Array([0x00, 0x00, 0x12, 0x34])); // Uint8Array([0x12, 0x34])
Bytes.trimLeft(new Uint8Array([0x00, 0x00, 0x00]));       // Uint8Array([])
trimRight()
trimRight: (bytes) => BytesType
Trim trailing zeros from Bytes
Parameters
bytes
BytesType Bytes to trim
Returns
BytesType Trimmed bytes
Example
import * as Bytes from './primitives/Bytes/index.js';
Bytes.trimRight(new Uint8Array([0x12, 0x34, 0x00, 0x00])); // Uint8Array([0x12, 0x34])
Bytes.trimRight(new Uint8Array([0x00, 0x00, 0x00]));       // Uint8Array([])
zero()
zero: (size) => BytesType
Create zero Bytes of specified size
Parameters
size
number Size in bytes
Returns
BytesType Zero Bytes
Example
const zeros = Bytes.zero(32);

Functions

assert()

assert(value): BytesType
Defined in: src/primitives/Bytes/assert.js:17 Assert that value is valid Bytes, throw if not

Parameters

value
unknown Value to check

Returns

BytesType The validated bytes

Throws

If value is not valid Bytes

Example

import * as Bytes from './primitives/Bytes/index.js';
const bytes = Bytes.assert(new Uint8Array([1, 2, 3])); // returns bytes
Bytes.assert("not bytes"); // throws Error

clone()

clone(bytes): BytesType
Defined in: src/primitives/Bytes/clone.js:12 Clone Bytes

Parameters

bytes
BytesType Bytes to clone

Returns

BytesType Cloned Bytes

Example

const copy = Bytes.clone(bytes);

compare()

compare(a, b): number
Defined in: src/primitives/Bytes/compare.js:13 Compare two Bytes (lexicographic)

Parameters

a
BytesType First Bytes
b
BytesType Second Bytes

Returns

number -1 if a < b, 0 if equal, 1 if a > b

Example

const cmp = Bytes.compare(bytes1, bytes2);

concat()

concat(…arrays): BytesType
Defined in: src/primitives/Bytes/concat.js:12 Concatenate multiple Bytes

Parameters

arrays
BytesType[] Bytes to concatenate

Returns

BytesType Concatenated Bytes

Example

const result = Bytes.concat(bytes1, bytes2, bytes3);

equals()

equals(a, b): boolean
Defined in: src/primitives/Bytes/equals.js:13 Check if two Bytes are equal

Parameters

a
BytesType First Bytes
b
BytesType Second Bytes

Returns

boolean True if equal

Example

const equal = Bytes.equals(bytes1, bytes2);

from()

from(value): BytesType
Defined in: src/primitives/Bytes/from.js:20 Create Bytes from various input types (universal constructor)

Parameters

value
Uint8Array, hex string, UTF-8 string, or number array string | Uint8Array<ArrayBufferLike> | number[]

Returns

BytesType Bytes

Throws

If value type is unsupported or invalid

Example

const b1 = Bytes.from(new Uint8Array([0x01, 0x02]));
const b2 = Bytes.from("0x1234");
const b3 = Bytes.from("hello");
const b4 = Bytes.from([0x01, 0x02, 0x03]);

fromBigInt()

fromBigInt(value, size?): BytesType
Defined in: src/primitives/Bytes/fromBigInt.js:17 Convert bigint to Bytes

Parameters

value
bigint BigInt to convert (must be non-negative)
size?
number Optional byte size (pads or throws if too small)

Returns

BytesType Bytes

Throws

If value is negative or doesn’t fit in size

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.fromBigInt(255n);      // Uint8Array([0xff])
Bytes.fromBigInt(255n, 2);   // Uint8Array([0x00, 0xff])
Bytes.fromBigInt(0x1234n);   // Uint8Array([0x12, 0x34])

fromHex()

fromHex(hex): BytesType
Defined in: src/primitives/Bytes/fromHex.js:16 Create Bytes from hex string

Parameters

hex
string Hex string with 0x prefix

Returns

BytesType Bytes

Throws

If hex string is invalid

Example

const bytes = Bytes.fromHex("0x1234");
// Uint8Array([0x12, 0x34])

fromNumber()

fromNumber(value, size?): BytesType
Defined in: src/primitives/Bytes/fromNumber.js:17 Convert number to Bytes

Parameters

value
number Number to convert (must be safe integer, non-negative)
size?
number Optional byte size (pads or throws if too small)

Returns

BytesType Bytes

Throws

If value is negative, not an integer, or exceeds MAX_SAFE_INTEGER

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.fromNumber(255);     // Uint8Array([0xff])
Bytes.fromNumber(255, 2);  // Uint8Array([0x00, 0xff])
Bytes.fromNumber(0x1234);  // Uint8Array([0x12, 0x34])

fromString()

fromString(str): BytesType
Defined in: src/primitives/Bytes/fromString.js:13 Create Bytes from UTF-8 string

Parameters

str
string UTF-8 string

Returns

BytesType Bytes

Example

const bytes = Bytes.fromString("hello");
// Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])

isBytes()

isBytes(value): value is BytesType
Defined in: src/primitives/Bytes/isBytes.js:15 Check if value is a valid Bytes (Uint8Array)

Parameters

value
unknown Value to check

Returns

value is BytesType True if value is Uint8Array

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.isBytes(new Uint8Array([1, 2, 3])); // true
Bytes.isBytes([1, 2, 3]);                  // false
Bytes.isBytes("0x1234");                   // false

isEmpty()

isEmpty(bytes): boolean
Defined in: src/primitives/Bytes/isEmpty.js:12 Check if Bytes is empty

Parameters

bytes
BytesType Bytes to check

Returns

boolean True if empty

Example

const empty = Bytes.isEmpty(bytes);

padLeft()

padLeft(bytes, targetSize): BytesType
Defined in: src/primitives/Bytes/padLeft.js:15 Pad Bytes on the left (start) with zeros to target size

Parameters

bytes
BytesType Bytes to pad
targetSize
number Target size in bytes

Returns

BytesType Padded bytes

Throws

If bytes exceeds target size

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.padLeft(new Uint8Array([0x12, 0x34]), 4); // Uint8Array([0x00, 0x00, 0x12, 0x34])

padRight()

padRight(bytes, targetSize): BytesType
Defined in: src/primitives/Bytes/padRight.js:15 Pad Bytes on the right (end) with zeros to target size

Parameters

bytes
BytesType Bytes to pad
targetSize
number Target size in bytes

Returns

BytesType Padded bytes

Throws

If bytes exceeds target size

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.padRight(new Uint8Array([0x12, 0x34]), 4); // Uint8Array([0x12, 0x34, 0x00, 0x00])

random()

random(size): BytesType
Defined in: src/primitives/Bytes/random.js:14 Generate random Bytes of specified size

Parameters

size
number Number of random bytes to generate

Returns

BytesType Random bytes

Example

import * as Bytes from './primitives/Bytes/index.js';
const random32 = Bytes.random(32); // 32 random bytes
const random16 = Bytes.random(16); // 16 random bytes

size()

size(bytes): number
Defined in: src/primitives/Bytes/size.js:12 Get size of Bytes

Parameters

bytes
BytesType Bytes

Returns

number Size in bytes

Example

const size = Bytes.size(bytes);

slice()

slice(bytes, start, end?): BytesType
Defined in: src/primitives/Bytes/slice.js:14 Slice Bytes

Parameters

bytes
BytesType Bytes to slice
start
number Start index
end?
number End index (optional)

Returns

BytesType Sliced Bytes

Example

const slice = Bytes.slice(bytes, 0, 4);

toBigInt()

toBigInt(bytes): bigint
Defined in: src/primitives/Bytes/toBigInt.js:14 Convert Bytes to bigint

Parameters

bytes
BytesType Bytes to convert

Returns

bigint BigInt value

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.toBigInt(new Uint8Array([0xff]));       // 255n
Bytes.toBigInt(new Uint8Array([0x12, 0x34])); // 4660n

toHex()

toHex(bytes): HexType
Defined in: src/primitives/Bytes/toHex.js:13 Convert Bytes to hex string

Parameters

bytes
BytesType Bytes to convert

Returns

HexType Hex string with 0x prefix

Example

const hex = Bytes.toHex(bytes);
// "0x1234"

toNumber()

toNumber(bytes): number
Defined in: src/primitives/Bytes/toNumber.js:15 Convert Bytes to number

Parameters

bytes
BytesType Bytes to convert

Returns

number Number value

Throws

If value exceeds MAX_SAFE_INTEGER

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.toNumber(new Uint8Array([0xff]));       // 255
Bytes.toNumber(new Uint8Array([0x12, 0x34])); // 4660

toString()

toString(bytes): string
Defined in: src/primitives/Bytes/toString.js:14 Convert Bytes to UTF-8 string

Parameters

bytes
BytesType Bytes to convert

Returns

string UTF-8 string

Example

const str = Bytes.toString(bytes);
// "hello"

trimLeft()

trimLeft(bytes): BytesType
Defined in: src/primitives/Bytes/trimLeft.js:14 Trim leading zeros from Bytes

Parameters

bytes
BytesType Bytes to trim

Returns

BytesType Trimmed bytes

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.trimLeft(new Uint8Array([0x00, 0x00, 0x12, 0x34])); // Uint8Array([0x12, 0x34])
Bytes.trimLeft(new Uint8Array([0x00, 0x00, 0x00]));       // Uint8Array([])

trimRight()

trimRight(bytes): BytesType
Defined in: src/primitives/Bytes/trimRight.js:14 Trim trailing zeros from Bytes

Parameters

bytes
BytesType Bytes to trim

Returns

BytesType Trimmed bytes

Example

import * as Bytes from './primitives/Bytes/index.js';
Bytes.trimRight(new Uint8Array([0x12, 0x34, 0x00, 0x00])); // Uint8Array([0x12, 0x34])
Bytes.trimRight(new Uint8Array([0x00, 0x00, 0x00]));       // Uint8Array([])

zero()

zero(size): BytesType
Defined in: src/primitives/Bytes/zero.js:12 Create zero Bytes of specified size

Parameters

size
number Size in bytes

Returns

BytesType Zero Bytes

Example

const zeros = Bytes.zero(32);