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
Name | Description |
---|---|
Bytes.assert | Asserts if the given value is Bytes.Bytes . |
Bytes.concat | Concatenates two or more Bytes.Bytes . |
Bytes.from | Instantiates a Bytes.Bytes value from a Uint8Array , a hex string, or an array of unsigned 8-bit integers. |
Bytes.fromArray | Converts an array of unsigned 8-bit integers into Bytes.Bytes . |
Bytes.fromBoolean | Encodes a boolean value into Bytes.Bytes . |
Bytes.fromHex | Encodes a Hex.Hex value into Bytes.Bytes . |
Bytes.fromNumber | Encodes a number value into Bytes.Bytes . |
Bytes.fromString | Encodes a string into Bytes.Bytes . |
Bytes.isEqual | Checks if two Bytes.Bytes values are equal. |
Bytes.padLeft | Pads a Bytes.Bytes value to the left with zero bytes until it reaches the given size (default: 32 bytes). |
Bytes.padRight | Pads a Bytes.Bytes value to the right with zero bytes until it reaches the given size (default: 32 bytes). |
Bytes.random | Generates random Bytes.Bytes of the specified length. |
Bytes.size | Retrieves the size of a Bytes.Bytes value. |
Bytes.slice | Returns a section of a Bytes.Bytes value given a start/end bytes offset. |
Bytes.toBigInt | Decodes a Bytes.Bytes into a bigint. |
Bytes.toBoolean | Decodes a Bytes.Bytes into a boolean. |
Bytes.toHex | Encodes a Bytes.Bytes value into a Hex.Hex value. |
Bytes.toNumber | Decodes a Bytes.Bytes into a number. |
Bytes.toString | Decodes a Bytes.Bytes into a string. |
Bytes.trimLeft | Trims leading zeros from a Bytes.Bytes value. |
Bytes.trimRight | Trims trailing zeros from a Bytes.Bytes value. |
Bytes.validate | Checks if the given value is Bytes.Bytes . |
Errors
Name | Description |
---|---|
Bytes.InvalidBytesBooleanError | Thrown when the bytes value cannot be represented as a boolean. |
Bytes.InvalidBytesTypeError | Thrown when a value cannot be converted to bytes. |
Bytes.SizeExceedsPaddingSizeError | Thrown when a the padding size exceeds the maximum allowed size. |
Bytes.SizeOverflowError | Thrown when a size exceeds the maximum allowed size. |
Bytes.SliceOffsetOutOfBoundsError | Thrown when a slice offset is out-of-bounds. |
Types
Name | Description |
---|---|
Bytes.Bytes | Root type for a Bytes array. |