Class used for reading BCS data chunk by chunk. Meant to be used by some wrapper, which will make sure that data is valid and is matching the desired format.

Example

// data for this example is:
// { a: u8, b: u32, c: bool, d: u64 }

let reader = new BcsReader("647f1a060001ffffe7890423c78a050102030405");
let field1 = reader.read8();
let field2 = reader.read32();
let field3 = reader.read8() === '1'; // bool
let field4 = reader.read64();
// ....

Reading vectors is another deal in bcs. To read a vector, you first need to read
its length using {@link readULEB}. Here's an example:

Example

// data encoded: { field: [1, 2, 3, 4, 5] }
let reader = new BcsReader("050102030405");
let vec_length = reader.readULEB();
let elements = [];
for (let i = 0; i < vec_length; i++) {
elements.push(reader.read8());
}
console.log(elements); // [1,2,3,4,5]

Param: data

HEX-encoded data (serialized BCS)

Constructors

  • Parameters

    • data: Uint8Array

      Data to use as a buffer.

    Returns BcsReader

Methods

  • Read U128 value from the buffer and shift cursor by 16.

    Returns string

  • Read U16 value from the buffer and shift cursor by 2.

    Returns number

  • Read U128 value from the buffer and shift cursor by 32.

    Returns string

  • Read U32 value from the buffer and shift cursor by 4.

    Returns number

  • Read U64 value from the buffer and shift cursor by 8.

    Returns string

  • Read U8 value from the buffer and shift cursor by 1.

    Returns number

  • Read num number of bytes from the buffer and shift cursor by num.

    Parameters

    • num: number

      Number of bytes to read.

    Returns Uint8Array

  • Read ULEB value - an integer of varying size. Used for enum indexes and vector lengths.

    Returns number

    The ULEB value.

  • Read a BCS vector: read a length and then apply function cb X times where X is the length of the vector, defined as ULEB in BCS bytes.

    Parameters

    • cb: ((reader, i, length) => any)

      Callback to process elements of vector.

        • (reader, i, length): any
        • Parameters

          Returns any

    Returns any[]

    Array of the resulting values, returned by callback.

  • Shift current cursor position by bytes.

    Parameters

    • bytes: number

      Number of bytes to

    Returns BcsReader

    Self for possible chaining.