Skip to content

Bytes

A set of Ethereum-related utility functions for working with Uint8Array instances.

Examples

Below are some examples demonstrating common usages of the Bytes module:

Instantiating Bytes

Values can be instantiated as Bytes.Bytes using:

import { Bytes } from 'ox'
 
const value_array = Bytes.from([1, 2, 3, 4, 5])
Uint8Array [1, 2, 3, 4, 5]
const value_boolean = Bytes.fromBoolean(true)
Uint8Array [1]
const value_hex = Bytes.fromHex('0x1234567890abcdef')
Uint8Array [18, 52, 86, 120, 144, 175, 207, 15]
const value_number = Bytes.fromNumber(1234567890)
Uint8Array [4, 160, 216]
const value_string = Bytes.fromString('Hello World!')
Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]

Converting from Bytes

Values can be converted from Bytes.Bytes using:

import { Bytes } from 'ox'
 
const value_bigint = Bytes.toBigInt(Bytes.from([4, 160, 216]))
1234567890n
const value_boolean = Bytes.toBoolean(Bytes.from([1]))
true
const value_hex = Bytes.toHex(Bytes.from([222, 173, 190, 239]))
'0xdeadbeef'
const value_number = Bytes.toNumber(Bytes.from([4, 160, 216]))
1234567890
const value_string = Bytes.toString(Bytes.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
'Hello World!'

Concatenating Bytes

Values can be concatenated using Bytes.concat:

import { Bytes } from 'ox'
 
const a = Bytes.from([1, 2, 3])
const b = Bytes.from([4, 5, 6])
const c = Bytes.concat(a, b)
Uint8Array [1, 2, 3, 4, 5, 6]

Slicing Bytes

Values can be sliced using Bytes.slice:

import { Bytes } from 'ox'
 
const value = Bytes.slice(Bytes.from([1, 2, 3, 4, 5, 6]), 2, 4)
Uint8Array [3, 4]

Padding Bytes

Values can be padded with zeroes using Bytes.padLeft or Bytes.padRight:

import { Bytes } from 'ox'
 
const value_1 = Bytes.padLeft(Bytes.from([1, 2, 3]), 5)
Uint8Array [0, 0, 1, 2, 3]
const value_2 = Bytes.padRight(Bytes.from([1, 2, 3]), 5)
Uint8Array [1, 2, 3, 0, 0]

Trimming Bytes

Zeroes in values can be trimmed using Bytes.trimLeft or Bytes.trimRight:

import { Bytes } from 'ox'
 
const value = Bytes.trimLeft(Bytes.from([0, 0, 1, 2, 3]))
Uint8Array [1, 2, 3]

Functions

NameDescription
Bytes.assertAsserts if the given value is Bytes.Bytes.
Bytes.concatConcatenates two or more Bytes.Bytes.
Bytes.fromInstantiates a Bytes.Bytes value from a Uint8Array, a hex string, or an array of unsigned 8-bit integers.
Bytes.fromArrayConverts an array of unsigned 8-bit integers into Bytes.Bytes.
Bytes.fromBooleanEncodes a boolean value into Bytes.Bytes.
Bytes.fromHexEncodes a Hex.Hex value into Bytes.Bytes.
Bytes.fromNumberEncodes a number value into Bytes.Bytes.
Bytes.fromStringEncodes a string into Bytes.Bytes.
Bytes.isEqualChecks if two Bytes.Bytes values are equal.
Bytes.padLeftPads a Bytes.Bytes value to the left with zero bytes until it reaches the given size (default: 32 bytes).
Bytes.padRightPads a Bytes.Bytes value to the right with zero bytes until it reaches the given size (default: 32 bytes).
Bytes.randomGenerates random Bytes.Bytes of the specified length.
Bytes.sizeRetrieves the size of a Bytes.Bytes value.
Bytes.sliceReturns a section of a Bytes.Bytes value given a start/end bytes offset.
Bytes.toBigIntDecodes a Bytes.Bytes into a bigint.
Bytes.toBooleanDecodes a Bytes.Bytes into a boolean.
Bytes.toHexEncodes a Bytes.Bytes value into a Hex.Hex value.
Bytes.toNumberDecodes a Bytes.Bytes into a number.
Bytes.toStringDecodes a Bytes.Bytes into a string.
Bytes.trimLeftTrims leading zeros from a Bytes.Bytes value.
Bytes.trimRightTrims trailing zeros from a Bytes.Bytes value.
Bytes.validateChecks if the given value is Bytes.Bytes.

Errors

NameDescription
Bytes.InvalidBytesBooleanErrorThrown when the bytes value cannot be represented as a boolean.
Bytes.InvalidBytesTypeErrorThrown when a value cannot be converted to bytes.
Bytes.SizeExceedsPaddingSizeErrorThrown when a the padding size exceeds the maximum allowed size.
Bytes.SizeOverflowErrorThrown when a size exceeds the maximum allowed size.
Bytes.SliceOffsetOutOfBoundsErrorThrown when a slice offset is out-of-bounds.

Types

NameDescription
Bytes.BytesRoot type for a Bytes array.