A class to make dealing with bytes easier. Allows to quickly convert between UTF-8, hexadecimal, and byte array representations of data.

Example

const input = Bytes.fromHexString("6574682d75736463");

const pairsString = input.toUtf8String();

Console.log(pairsString); // eth-usdc

Properties

value: Uint8Array

The underlying byte array. Can also be accessed through toByteArray.

Accessors

  • get buffer(): ArrayBuffer
  • The underlying ArrayBuffer in case you need to manipulate it directly. Most likely toUtf8String or toHexString is the better choice.

    Returns ArrayBuffer

    Example

    const input = Bytes.fromHexString("6574682d75736463");

    Console.log(String.UTF8.decode(input.buffer);); // eth-usdc
  • get length(): number
  • The length of the underlying byte array.

    Returns number

    Example

    const data = Bytes.fromHexString("6574682d75736463");

    Console.log(data.length); // 8

Methods

  • Create a new Bytes instance which is a combination of the first instance and the argument.

    Parameters

    • bytes: Bytes

      the bytes to add to this instance

    Returns Bytes

    Example

    const part1 = Bytes.fromUtf8String("Hello, ");
    const part2 = Bytes.fromUtf8String("world!");

    const combined = part1.concat(part2);

    Console.log(combined.toUtf8String()); // "Hello, world!"
  • Create a new Bytes instance from a slice of the original instance.

    Parameters

    • begin: number = 0

      Inclusive beginning of the slice to copy

    • end: number = i32.MAX_VALUE

      Exclusive end of the slice to copy

    Returns Bytes

    Example

    const data = Bytes.fromHexString("68656c6c6f2073656461");

    // Without begin and end it returns a new copy
    const copy = data.slice();
    Console.log(copy.toUtf8String()); // "hello seda"

    // Without end it starts the slice at begin and copies until the end
    const noEnd = data.slice(6);
    Console.log(noEnd.toUtf8String()); // "seda"

    // Without begin and end it returns a new copy
    const beginAndEnd = data.slice(1, 5);
    Console.log(beginAndEnd.toUtf8String()); // "ello"
  • The underlying byte array in case you need to use it directly. Can also be accessed through value.

    Returns Uint8Array

    Example

    const data = Bytes.fromHexString("6574682d75736463");

    const byteArray = data.toByteArray();
    Console.log(byteArray.length); // 8
  • Encode the bytes as a hexidecimal string without '0x' prefix.

    Returns string

    Example

    const data = Bytes.fromUtf8String("eth-usdc");

    const dataAsHex = data.toHexString();
    Console.log(dataAsHex); // "6574682d75736463"
  • Attempts to deserialize the Bytes into the '@json' annotated class <T>.

    Type Parameters

    • T

    Returns T

    an instance of '@json' annotated class

    Example

    @json
    class Data {
    id!: i64;
    value!: string;
    }

    const data = Bytes.fromString("{"id":1,"value":"test"}");

    const responseData = data.toJSON<Data>();

    Console.log(responseData.value); // "test"
  • Convert the Bytes instance to the number T supports the following number types: (i8, u8, i16, u16, i32, u32, i64 and u64) for u128 and u256 see Bytes.toU128 and Bytes.toU256

    Type Parameters

    • T

    Parameters

    • bigEndian: bool = false

      if the Bytes instance should be read as big-endian (defaults to little-endian)

    Returns T

    Example

    const littleEndianBytes = Bytes.fromHexString("0x0a000000");
    const num1 = littleEndianBytes.toNumber<u32>();

    Console.log(num1); // Outputs: 10

    const bigEndianBytes = Bytes.fromHexString("0x00000000000000f3");
    const number = bigEndianBytes.toNumber<u64>(true);

    Console.log(number); // Outputs: 243
  • Convert the Bytes instance to a u128 number for smaller number types see Bytes.toNumber for u256 see Bytes.toU256

    Parameters

    • bigEndian: bool = false

      if the Bytes instance should be read as big-endian (defaults to little-endian)

    Returns u128

    Example

    const littleEndianBytes = Bytes.fromHexString("0x0000000000000000000000003ade68b1");
    const num1 = littleEndianBytes.toU128();

    Console.log(num1); // Outputs: 235817861417383168075506718003194494976

    const bigEndianBytes = Bytes.fromHexString("0x0000000000000000000000003ade68b1");
    const number = bigEndianBytes.toU128(true);

    Console.log(number); // Outputs: 987654321
  • Convert the Bytes instance to a u256 number for smaller number types see Bytes.toNumber for u128 see Bytes.toU128

    Parameters

    • bigEndian: bool = false

      if the Bytes instance should be read as big-endian (defaults to little-endian)

    Returns u256

    Example

    const littleEndianBytes = Bytes.fromHexString("0x00000000000000000000000000000000000000000000000000000000075bcd15");
    const num1 = littleEndianBytes.toU256();

    Console.log(num1); // Outputs: 9861401716165347554763518477098801055286775394839307868237211366843748450304

    const bigEndianBytes = Bytes.fromHexString("0x00000000000000000000000000000000000000000000000000000000075bcd15");
    const number = bigEndianBytes.toU256(true);

    Console.log(number); // Outputs: 123456789
  • Decode this Bytes instance as a UTF-8 string.

    Returns string

    Example

    const input = Bytes.fromHexString("6574682d75736463");
    const pairsString = input.toUtf8String();

    const pairsList = pairsString.split("-");
    const a = pairsList[0];
    const b = pairsList[1];

    Console.log(a); // eth
    Console.log(b); // usdc
  • Type Parameters

    • T

    Parameters

    • sizeOfNumber: number

    Returns void

  • Wrap an existing byte array in Bytes.

    Parameters

    • payload: Uint8Array

    Returns Bytes

    Example

    import { decode } from "as-base64";

    const data = decode("aGVsbG8gc2VkYQ==");
    Console.log(data.toUtf8String()); // "hello seda"
  • Decode a hexidecimal string to Bytes. Ignores any '0x' prefix.

    Parameters

    • payload: string

    Returns Bytes

    Example

    const data = Bytes.fromHexString("6574682d75736463");
    // Equivalent to
    const dataWithPrefix = Bytes.fromHexString("0x6574682d75736463");

    Console.log(data.toUtf8String); // "eth-usdc"
    Console.log(dataWithPrefix.toUtf8String); // "eth-usdc"
  • Serialises a '@json' annotated class (<T>) as a string and returns the Bytes representation of that string.

    Type Parameters

    • T

    Parameters

    • data: T

      an instance of the '@json' annotated class

    Returns Bytes

    Example

    @json
    class Data {
    id!: i64;
    value!: string;
    }

    const data = new Data();
    data.id = 1;
    data.value = "test";

    Process.success(Bytes.fromJSON<Data>(data)); // Sets "{"id":1,"value":"test"}" in bytes as the result
  • Creates a new Bytes instance from a number supports any number type (i8, u8, i16, u16, i32, u32, i64, u64, u128, and u256)

    Type Parameters

    • T

    Parameters

    • number: T

      the number you want to convert to a Bytes instance

    • bigEndian: bool = false

      if you want to store the number as big-endian. Defaults to little-endian

    Returns Bytes

    Example

    const number = Bytes.fromNumber<u32>(10);
    Console.log(number.toHexString()); // Outputs: 0a000000

    const bigEndianNumber = Bytes.fromNumber<u32>(10, true);
    Console.log(number.toHexString()); // Outputs: 0000000a
  • Encode a given string as UTF-8 bytes.

    Parameters

    • payload: string

    Returns Bytes

    Example

    const output = Bytes.fromUtf8String("winner");

    // Exit the program with the UTF-8 encoded bytes as the output.
    Process.success(output);