Options
All
  • Public
  • Public/Protected
  • All
Menu

Package codec

@ckb-lumos/codec

This module provides a set of functions to pack(encode) and unpack(decode) data.

graph TD;
    Uint8Array-->Codec;
    Codec-->|unpack|JSObject;
    JSObject-->Codec;
    Codec-->|pack|Uint8Array

Quick Start

import { struct, Uint8, Uint128 } from "@ckb-lumos/codec";

// udt-info.mol
// struct UDTInfo {
//  total_supply: Uint128,
//  decimals: Uint8,
// }
// array Uint8 [byte; 1];
// array Uint128 [byte; 16];

// 1. create molecule binding
const UDTInfo /*: Codec */ = struct(
  {
    totalSupply: Uint128,
    decimals: Uint8,
  },
  ["totalSupply", "decimals"]
);

// 2. usage
// 2.1 pack
const buf /*: Uint8Array*/ = UDTInfo.pack({
  totalSupply: BI.from(21000000 * 10 ** 8),
  decimals: 8,
});
// 2.2 unpack
const udtInfo = UDTInfo.unpack(buf); // { totalSupply: BI(21000000 * 10 ** 8), decimals: 8 }

Molecule

Molecule is a lightweight serialization system that focuses only on the layout of byte(s) and not on specific data types. This library will help developers to create TypeScript-friendly molecule bindings in an easy way.

layout is a set of Codec that helps to bind molecule to JavaScript plain object/array.

  • array: Array<T> <=> Uint8Array
  • vector: Array<T> <=> Uint8Array
  • struct: { [key: string]: T } <=> Uint8Array
  • table: { [key: string]: T } <=> Uint8Array
  • option: T | undefined <=> Uint8Array
  • union: { type: string, value: T } <=> Uint8Array

Example

RGB Color

Suppose we want to describe an RGB color, then we can use a tuple3 of uint8 to describe the color

# color-by-tuple3.mol

array RGB [Uint8; 3];
const RGB = array(Uint8, 3);

const [r, g, b] = RGB.unpack(buffer);
// const unpacked = RGB.unpack(buffer)
// const r = unpacked[0];
// const g = unpacked[1];
// const b = unpacked[2];

Of course, we could also use a struct to more directly describe rgb separately

# color-by-struct.mol

struct RGB {
  r: Uint8,
  g: Uint8,
  b: Uint8,
}
const RGB = struct(
  { r: Uint8, g: Uint8, b: Uint8 },
  ["r", "g", "b"] // order of the keys needs to be consistent with the schema
);

const { r, g, b } = RGB.unpack(buffer);
// const unpacked = RGB.unpack(buffer);
// const r = unpacked.r;
// const g = unpacked.g;
// const b = unpacked.b;

Number

number is a set of Codec that helps to encode/decode number to/from Uint8Array. Because of ckb-vm is a RISCV machine, the number is encoded in little-endian by default.

  • Uint8(BE|LE): number <=> Uint8
  • Uint16(BE|LE): number <=> Uint16
  • Uint32(BE|LE): number <=> Uint32
  • Uint64(BE|LE): BI <=> Uint64
  • Uint128(BE|LE): BI <=> Uint128
  • Uint256(BE|LE): BI <=> Uint256
  • Uint512(BE|LE): BI <=> Uint512
import { Uint32, Uint128 } from "@ckb-lumos/codec";

const packedU32 = Uint32.pack(100); // == Uint8Array([100, 0, 0, 0]) little-endian
// const packedU32 = Uint32LE.pack(100); // == Uint8Array([100, 0, 0, 0]) little-endian
const packedU32BE = Uint32BE.pack(100); // == Uint8Array([0, 0, 0, 100]) big-endian

// unpack sUDT amount to a BI(BigInteger)
const sudtAmount = Uint128.unapck("0x0000e45d76a1f90e0c00000000000000"); // == BI.from('222440000000000000000')
// Uint8Array or Uint8Array are also supported
// Uint128.unpack(
//   Uint8Array.from([
//     0x00, 0x00, 0xe4, 0x5d,
//     0x76, 0xa1, 0xf9, 0x0e,
//     0x0c, 0x00, 0x00, 0x00,
//     0x00, 0x00, 0x00, 0x00,
//   ])
// );

Custom Codec

When we encounter molecule layouts like byte | array SomeBytes [byte; n] | vector SomeBytes <byte>, and the common codec is not sufficient, we can customize the codec to help us interpret these byte(s)

Let's see an example of how to implement a UTF8String codec. If we want to store a UTF8String of indefinite length, then the corresponding molecule structure should be a vector UTF8String <byte>

import { byteVecOf } from "@ckb-lumos/codec";
import { Buffer } from "buffer"; // https://github.com/feross/buffer

const UTF8String = byteVecOf<string>({
  pack: (str) => {
    return Uint8Array.from(Buffer.from(str, "utf8")).buffer;
  },
  unpack: (buf) => {
    return Buffer.from(buf).toString("utf8");
  },
});

Why I Need This Module

molecule is flexible in that it is a serialization scheme that focuses only on byte(s) layout. When developers encounter byte | array FixedBytes [byte; n] | vector DynBytes <byte>, these byte(s) need to be translated into understandable data types, such as array Uint32 [byte; 4] is generally translated as number.

This module can help us convert bytes to common data types in a simple way. If you have some experience with CKB, you will have encountered more complex scripts like OmniLock, where it is easy to get confused about how to handle bytes when we want to sign it, if we can combine WitnessArgs.lock(BytesOpt) with OmniLockWitnessLock.signature(BytesOpt), then it will be easier to do the signing, we can check the real world OmniLock witness case to see how it works

table WitnessArgs {
    lock:                   BytesOpt,          // Lock args
    inputType:             BytesOpt,          // Type args for input
    outputType:            BytesOpt,          // Type args for output
}

table OmniLockWitnessLock {
    signature: BytesOpt,
    rc_identity: RcIdentityOpt,
    preimage: BytesOpt,
}

Works with TypeScript

Get Type Definition from Value

import { molecule } from "@ckb-lumos/codec";
import type { UnpackResult } from "@ckb-lumos/codec";

const { struct } = molecule;

const RGB = struct(
  { r: Uint8, g: Uint8, b: Uint8 },
  ["r", "g", "b"] // order of the keys needs to be consistent with the schema
);

// We don't need to repeat the definition like this
// type UnpackedRGB = { r: number; g: number; b: number };
type UnpackedRGB = UnpackResult<typeof RGB>;

Index

References

Classes

Interfaces

Type aliases

Variables

Functions

References

AnyCodec

Re-exports AnyCodec

AnyCodec

Re-exports AnyCodec

ArrayCodec

Re-exports ArrayCodec

ArrayCodec

Re-exports ArrayCodec

ArrayCodec

Re-exports ArrayCodec

ArrayCodec

Re-exports ArrayCodec

BytesLike

Re-exports BytesLike

BytesLike

Re-exports BytesLike

NullableCodec

Re-exports NullableCodec

NullableCodec

Re-exports NullableCodec

NullableCodec

Re-exports NullableCodec

NullableCodec

Re-exports NullableCodec

ObjectCodec

Re-exports ObjectCodec

ObjectCodec

Re-exports ObjectCodec

ObjectCodec

Re-exports ObjectCodec

ObjectCodec

Re-exports ObjectCodec

PackParam

Re-exports PackParam

PackParam

Re-exports PackParam

PackResult

Re-exports PackResult

PackResult

Re-exports PackResult

Uint128

Re-exports Uint128

Uint128

Re-exports Uint128

Uint128BE

Re-exports Uint128BE

Uint128BE

Re-exports Uint128BE

Uint128LE

Re-exports Uint128LE

Uint128LE

Re-exports Uint128LE

Uint16

Re-exports Uint16

Uint16

Re-exports Uint16

Uint16BE

Re-exports Uint16BE

Uint16BE

Re-exports Uint16BE

Uint16LE

Re-exports Uint16LE

Uint16LE

Re-exports Uint16LE

Uint256

Re-exports Uint256

Uint256

Re-exports Uint256

Uint256BE

Re-exports Uint256BE

Uint256BE

Re-exports Uint256BE

Uint256LE

Re-exports Uint256LE

Uint256LE

Re-exports Uint256LE

Uint32

Re-exports Uint32

Uint32

Re-exports Uint32

Uint32BE

Re-exports Uint32BE

Uint32BE

Re-exports Uint32BE

Uint32LE

Re-exports Uint32LE

Uint32LE

Re-exports Uint32LE

Uint512

Re-exports Uint512

Uint512

Re-exports Uint512

Uint512BE

Re-exports Uint512BE

Uint512BE

Re-exports Uint512BE

Uint512LE

Re-exports Uint512LE

Uint512LE

Re-exports Uint512LE

Uint64

Re-exports Uint64

Uint64

Re-exports Uint64

Uint64BE

Re-exports Uint64BE

Uint64BE

Re-exports Uint64BE

Uint64LE

Re-exports Uint64LE

Uint64LE

Re-exports Uint64LE

Uint8

Re-exports Uint8

Uint8

Re-exports Uint8

UnpackParam

Re-exports UnpackParam

UnpackParam

Re-exports UnpackParam

UnpackResult

Re-exports UnpackResult

UnpackResult

Re-exports UnpackResult

array

Re-exports array

array

Re-exports array

byteArrayOf

Re-exports byteArrayOf

byteArrayOf

Re-exports byteArrayOf

byteOf

Re-exports byteOf

byteOf

Re-exports byteOf

byteVecOf

Re-exports byteVecOf

byteVecOf

Re-exports byteVecOf

bytes

Re-exports bytes

bytes

Re-exports bytes

createArrayCodec

Re-exports createArrayCodec

createArrayCodec

Re-exports createArrayCodec

createArrayCodec

Re-exports createArrayCodec

createArrayCodec

Re-exports createArrayCodec

createBytesCodec

Re-exports createBytesCodec

createBytesCodec

Re-exports createBytesCodec

createFixedBytesCodec

Re-exports createFixedBytesCodec

createFixedBytesCodec

Re-exports createFixedBytesCodec

createNullableCodec

Re-exports createNullableCodec

createNullableCodec

Re-exports createNullableCodec

createNullableCodec

Re-exports createNullableCodec

createNullableCodec

Re-exports createNullableCodec

createObjectCodec

Re-exports createObjectCodec

createObjectCodec

Re-exports createObjectCodec

createObjectCodec

Re-exports createObjectCodec

createObjectCodec

Re-exports createObjectCodec

enhancePack

Re-exports enhancePack

enhancePack

Re-exports enhancePack

enhancePack

Re-exports enhancePack

enhancePack

Re-exports enhancePack

isFixedCodec

Re-exports isFixedCodec

isFixedCodec

Re-exports isFixedCodec

molecule

Re-exports molecule

molecule

Re-exports molecule

number

Re-exports number

number

Re-exports number

option

Re-exports option

option

Re-exports option

struct

Re-exports struct

struct

Re-exports struct

table

Re-exports table

table

Re-exports table

union

Re-exports union

union

Re-exports union

vector

Re-exports vector

vector

Re-exports vector

Type aliases

AnyCodec

AnyCodec: Codec<any, any>

AnyCodec

AnyCodec: Codec<any, any>

ArrayCodec

ArrayCodec<C>: Codec<PackResult<C>[], UnpackResult<C>[], PackParam<C>[], UnpackParam<C>[]>

Type parameters

ArrayCodec

ArrayCodec<T>: BytesCodec<Array<UnpackResult<T>>, Array<PackParam<T>>>

Type parameters

ArrayCodec

ArrayCodec<C>: Codec<PackResult<C>[], UnpackResult<C>[], PackParam<C>[], UnpackParam<C>[]>

Type parameters

ArrayCodec

ArrayCodec<T>: BytesCodec<Array<UnpackResult<T>>, Array<PackParam<T>>>

Type parameters

BytesCodec

BytesCodec<Unpacked, Packable>: Codec<Uint8Array, Unpacked, Packable, BytesLike>

Type parameters

  • Unpacked

  • Packable

BytesCodec

BytesCodec<Unpacked, Packable>: Codec<Uint8Array, Unpacked, Packable, BytesLike>

Type parameters

  • Unpacked

  • Packable

BytesLike

BytesLike: ArrayLike<number> | ArrayBuffer | string

BytesLike

BytesLike: ArrayLike<number> | ArrayBuffer | string

CodecOptionalPath

CodecOptionalPath: typeof CODEC_OPTIONAL_PATH

CodecOptionalPath

CodecOptionalPath: typeof CODEC_OPTIONAL_PATH

Fixed

Fixed: { __isFixedCodec__: true; byteLength: number }

Type declaration

  • Readonly __isFixedCodec__: true
  • Readonly byteLength: number

Fixed

Fixed: { __isFixedCodec__: true; byteLength: number }

Type declaration

  • Readonly __isFixedCodec__: true
  • Readonly byteLength: number

FixedBytesCodec

FixedBytesCodec<Unpacked, Packable>: BytesCodec<Unpacked, Packable> & Fixed

Type parameters

  • Unpacked

  • Packable

FixedBytesCodec

FixedBytesCodec<Unpacked, Packable>: BytesCodec<Unpacked, Packable> & Fixed

Type parameters

  • Unpacked

  • Packable

NonNullableKeys

NonNullableKeys<O>: {}[keyof O]

Type parameters

  • O: Record<string, unknown>

NonNullableKeys

NonNullableKeys<O>: {}[keyof O]

Type parameters

  • O: Record<string, unknown>

NullableKeys

NullableKeys<O>: {}[keyof O]

Type parameters

  • O: Record<string, unknown>

NullableKeys

NullableKeys<O>: {}[keyof O]

Type parameters

  • O: Record<string, unknown>

ObjectCodec

ObjectCodec<Shape>: Codec<{}, {}, {}, {}>

Type parameters

ObjectCodec

ObjectCodec<T>: BytesCodec<PartialNullable<{}>, PartialNullable<{}>>

Type parameters

ObjectCodec

ObjectCodec<Shape>: Codec<{}, {}, {}, {}>

Type parameters

ObjectCodec

ObjectCodec<T>: BytesCodec<PartialNullable<{}>, PartialNullable<{}>>

Type parameters

ObjectCodecShape

ObjectCodecShape: Record<string, AnyCodec>

ObjectCodecShape

ObjectCodecShape: Record<string, AnyCodec>

PackParam

PackParam<T>: T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packable : never

Type parameters

PackParam

PackParam<T>: T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packable : never

Type parameters

PackResult

PackResult<T>: T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packed : never

Type parameters

PackResult

PackResult<T>: T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packed : never

Type parameters

PartialNullable

PartialNullable<O>: Partial<Pick<O, NullableKeys<O>>> & Pick<O, NonNullableKeys<O>>

Type parameters

  • O: Record<string, unknown>

PartialNullable

PartialNullable<O>: Partial<Pick<O, NullableKeys<O>>> & Pick<O, NonNullableKeys<O>>

Type parameters

  • O: Record<string, unknown>

Uint8ArrayCodec

Uint8ArrayCodec<Unpacked, Packable>: Codec<Uint8Array, Unpacked, Packable>

Type parameters

  • Unpacked

  • Packable

Uint8ArrayCodec

Uint8ArrayCodec<Unpacked, Packable>: Codec<Uint8Array, Unpacked, Packable>

Type parameters

  • Unpacked

  • Packable

UnionCodec

UnionCodec<T>: BytesCodec<{}[keyof T], {}[keyof T]>

Type parameters

UnionCodec

UnionCodec<T>: BytesCodec<{}[keyof T], {}[keyof T]>

Type parameters

UnpackParam

UnpackParam<T>: T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpackable : never

Type parameters

UnpackParam

UnpackParam<T>: T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpackable : never

Type parameters

UnpackResult

UnpackResult<T>: T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpacked : never

Type parameters

UnpackResult

UnpackResult<T>: T extends Codec<infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpacked : never

Type parameters

Variables

Const Byte32

Byte32: FixedBytesCodec<string, BytesLike>

Const Byte32

Byte32: Codec<Uint8Array, string, string | ArrayLike<number> | ArrayBuffer, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createFixedHexBytesCodec(32)

Const Byte32Vec

Byte32Vec: BytesCodec<string[], BytesLike[]>

Const Byte32Vec

Byte32Vec: Codec<Uint8Array, string[], (string | ArrayLike<number> | ArrayBuffer)[], string | ArrayLike<number> | ArrayBuffer> = vector(Byte32)

Const Bytes

Bytes: BytesCodec<string, BytesLike>

placeholder codec, generally used as a placeholder

// for example, when some BytesOpt is not used, it will be filled with this codec
// option BytesOpt (Bytes);
const UnusedBytesOpt = UnknownOpt

Const Bytes

Bytes: Codec<Uint8Array, string, string | ArrayLike<number> | ArrayBuffer, string | ArrayLike<number> | ArrayBuffer> = byteVecOf({ pack: bytify, unpack: hexify })

placeholder codec, generally used as a placeholder

// for example, when some BytesOpt is not used, it will be filled with this codec
// option BytesOpt (Bytes);
const UnusedBytesOpt = UnknownOpt

Const BytesOpt

BytesOpt: OptionCodec<BytesCodec<string, BytesLike>>

Const BytesOpt

BytesOpt: OptionCodec<Codec<Uint8Array, string, string | ArrayLike<number> | ArrayBuffer, string | ArrayLike<number> | ArrayBuffer>> = option(Bytes)

Const BytesVec

BytesVec: BytesCodec<string[], BytesLike[]>

Const BytesVec

BytesVec: Codec<Uint8Array, string[], (string | ArrayLike<number> | ArrayBuffer)[], string | ArrayLike<number> | ArrayBuffer> = vector(Bytes)

Const CODEC_EXECUTE_ERROR_NAME

CODEC_EXECUTE_ERROR_NAME: "CodecExecuteError" = "CodecExecuteError"

Const CODEC_OPTIONAL_PATH

CODEC_OPTIONAL_PATH: "__lc_option__" = "__lc_option__"

Const CODEC_OPTIONAL_PATH

CODEC_OPTIONAL_PATH: "__lc_option__" = "__lc_option__"

Const HEX_DECIMAL_REGEX

HEX_DECIMAL_REGEX: RegExp = /^0x([0-9a-fA-F])+$/

Const HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP

HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP: Map<number, RegExp> = new Map<number, RegExp>()

Const HEX_STRING_REGEX

HEX_STRING_REGEX: RegExp = /^0x([0-9a-fA-F][0-9a-fA-F])*$/

Const HEX_STRING_WITH_BYTELENGTH_REGEX_MAP

HEX_STRING_WITH_BYTELENGTH_REGEX_MAP: Map<number, RegExp> = new Map<number, RegExp>()

Const HexifyCodec

HexifyCodec: Codec<Uint8Array, string, string | ArrayLike<number> | ArrayBuffer, string | ArrayLike<number> | ArrayBuffer> = createBytesCodec<string, BytesLike>({pack: bytify,unpack: hexify,})

Const Uint128

alias

Uint128LE

Const Uint128

Uint128: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = Uint128LE
alias

Uint128LE

Const Uint128BE

Uint128BE: FixedBytesCodec<BI, BIish>

Const Uint128BE

Uint128BE: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintBICodec(16)

Const Uint128LE

Uint128LE: FixedBytesCodec<BI, BIish>

Const Uint128LE

Uint128LE: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintBICodec(16, true)

Const Uint16

Uint16: FixedBytesCodec<number, BIish>
alias

Uint16LE

Const Uint16

Uint16: Codec<Uint8Array, number, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = Uint16LE
alias

Uint16LE

Const Uint16BE

Uint16BE: FixedBytesCodec<number, BIish>

Const Uint16BE

Uint16BE: Codec<Uint8Array, number, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintNumberCodec(2)

Const Uint16LE

Uint16LE: FixedBytesCodec<number, BIish>

Const Uint16LE

Uint16LE: Codec<Uint8Array, number, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintNumberCodec(2, true)

Const Uint256

alias

Uint256LE

Const Uint256

Uint256: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = Uint256LE
alias

Uint256LE

Const Uint256BE

Uint256BE: FixedBytesCodec<BI, BIish>

Const Uint256BE

Uint256BE: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintBICodec(32)

Const Uint256LE

Uint256LE: FixedBytesCodec<BI, BIish>

Const Uint256LE

Uint256LE: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintBICodec(32, true)

Const Uint32

Uint32: FixedBytesCodec<number, BIish>
alias

Uint32LE

Const Uint32

Uint32: Codec<Uint8Array, number, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = Uint32LE
alias

Uint32LE

Const Uint32BE

Uint32BE: FixedBytesCodec<number, BIish>

Const Uint32BE

Uint32BE: Codec<Uint8Array, number, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintNumberCodec(4)

Const Uint32LE

Uint32LE: FixedBytesCodec<number, BIish>

Const Uint32LE

Uint32LE: Codec<Uint8Array, number, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintNumberCodec(4, true)

Const Uint512

alias

Uint512LE

Const Uint512

Uint512: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = Uint512LE
alias

Uint512LE

Const Uint512BE

Uint512BE: FixedBytesCodec<BI, BIish>

Const Uint512BE

Uint512BE: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintBICodec(64)

Const Uint512LE

Uint512LE: FixedBytesCodec<BI, BIish>

Const Uint512LE

Uint512LE: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintBICodec(64, true)

Const Uint64

alias

Uint64LE

Const Uint64

Uint64: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = Uint64LE
alias

Uint64LE

Const Uint64BE

Const Uint64BE

Uint64BE: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintBICodec(8)

Const Uint64LE

Const Uint64LE

Uint64LE: Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintBICodec(8, true)

Const Uint8

Uint8: FixedBytesCodec<number, BIish>

Const Uint8

Uint8: Codec<Uint8Array, number, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number } = createUintNumberCodec(1)

Const WitnessArgs

WitnessArgs: BytesCodec<{ input_type?: string | undefined; lock?: string | undefined; output_type?: string | undefined }, { input_type?: string | ArrayBuffer | ArrayLike<number> | undefined; lock?: string | ArrayBuffer | ArrayLike<number> | undefined; output_type?: string | ArrayBuffer | ArrayLike<number> | undefined }>
example
// secp256k1 lock witness
WitnessArgs.pack({ lock: '0x' + '00'.repeat(65) })

Const WitnessArgs

WitnessArgs: Codec<Uint8Array, { input_type?: UnpackResult<InputTypeCodec>; lock?: UnpackResult<LockCodec>; output_type?: UnpackResult<OutputTypeCodec> }, { input_type?: PackParam<InputTypeCodec>; lock?: PackParam<LockCodec>; output_type?: PackParam<OutputTypeCodec> }, string | ArrayLike<number> | ArrayBuffer> = WitnessArgsOf({lock: HexifyCodec,input_type: HexifyCodec,output_type: HexifyCodec,})
example
// secp256k1 lock witness
WitnessArgs.pack({ lock: '0x' + '00'.repeat(65) })

Functions

WitnessArgsOf

  • WitnessArgsOf<LockCodec, InputTypeCodec, OutputTypeCodec>(payload: { input_type: InputTypeCodec; lock: LockCodec; output_type: OutputTypeCodec }): BytesCodec<{ input_type?: UnpackResult<InputTypeCodec>; lock?: UnpackResult<LockCodec>; output_type?: UnpackResult<OutputTypeCodec> }, { input_type?: PackParam<InputTypeCodec>; lock?: PackParam<LockCodec>; output_type?: PackParam<OutputTypeCodec> }>
  • Type parameters

    Parameters

    • payload: { input_type: InputTypeCodec; lock: LockCodec; output_type: OutputTypeCodec }
      • input_type: InputTypeCodec
      • lock: LockCodec
      • output_type: OutputTypeCodec

    Returns BytesCodec<{ input_type?: UnpackResult<InputTypeCodec>; lock?: UnpackResult<LockCodec>; output_type?: UnpackResult<OutputTypeCodec> }, { input_type?: PackParam<InputTypeCodec>; lock?: PackParam<LockCodec>; output_type?: PackParam<OutputTypeCodec> }>

WitnessArgsOf

  • WitnessArgsOf<LockCodec, InputTypeCodec, OutputTypeCodec>(payload: { input_type: InputTypeCodec; lock: LockCodec; output_type: OutputTypeCodec }): BytesCodec<{ input_type?: UnpackResult<InputTypeCodec>; lock?: UnpackResult<LockCodec>; output_type?: UnpackResult<OutputTypeCodec> }, { input_type?: PackParam<InputTypeCodec>; lock?: PackParam<LockCodec>; output_type?: PackParam<OutputTypeCodec> }>
  • Type parameters

    Parameters

    • payload: { input_type: InputTypeCodec; lock: LockCodec; output_type: OutputTypeCodec }
      • input_type: InputTypeCodec
      • lock: LockCodec
      • output_type: OutputTypeCodec

    Returns BytesCodec<{ input_type?: UnpackResult<InputTypeCodec>; lock?: UnpackResult<LockCodec>; output_type?: UnpackResult<OutputTypeCodec> }, { input_type?: PackParam<InputTypeCodec>; lock?: PackParam<LockCodec>; output_type?: PackParam<OutputTypeCodec> }>

array

  • The array is a fixed-size type: it has a fixed-size inner type and a fixed length. The size of an array is the size of inner type times the length.

    Type parameters

    Parameters

    • itemCodec: T

      the fixed-size array item codec

    • itemCount: number

    Returns ArrayCodec<T> & Fixed

array

  • The array is a fixed-size type: it has a fixed-size inner type and a fixed length. The size of an array is the size of inner type times the length.

    Type parameters

    Parameters

    • itemCodec: T

      the fixed-size array item codec

    • itemCount: number

    Returns ArrayCodec<T> & Fixed

assertBufferLength

  • assertBufferLength(buf: { byteLength: number }, length: number): void
  • Parameters

    • buf: { byteLength: number }
      • byteLength: number
    • length: number

    Returns void

assertBufferLength

  • assertBufferLength(buf: { byteLength: number }, length: number): void
  • Parameters

    • buf: { byteLength: number }
      • byteLength: number
    • length: number

    Returns void

assertHexDecimal

  • assertHexDecimal(str: string, byteLength?: undefined | number): void
  • Parameters

    • str: string
    • Optional byteLength: undefined | number

    Returns void

assertHexDecimal

  • assertHexDecimal(str: string, byteLength?: undefined | number): void
  • Parameters

    • str: string
    • Optional byteLength: undefined | number

    Returns void

assertHexString

  • assertHexString(str: string, byteLength?: undefined | number): void
  • Parameters

    • str: string
    • Optional byteLength: undefined | number

    Returns void

assertHexString

  • assertHexString(str: string, byteLength?: undefined | number): void
  • Parameters

    • str: string
    • Optional byteLength: undefined | number

    Returns void

assertMinBufferLength

  • assertMinBufferLength(buf: { byteLength: number }, length: number): void
  • Parameters

    • buf: { byteLength: number }
      • byteLength: number
    • length: number

    Returns void

assertMinBufferLength

  • assertMinBufferLength(buf: { byteLength: number }, length: number): void
  • Parameters

    • buf: { byteLength: number }
      • byteLength: number
    • length: number

    Returns void

assertNumberRange

  • assertNumberRange(value: BIish, min: BIish, max: BIish, typeName: string): void
  • Parameters

    Returns void

assertUtf8String

  • assertUtf8String(str: string): void
  • Parameters

    • str: string

    Returns void

assertUtf8String

  • assertUtf8String(str: string): void
  • Parameters

    • str: string

    Returns void

byteArrayOf

  • byteArrayOf<Packed, Packable>(codec: BytesCodec<Packed, Packable> & { byteLength: number }): FixedBytesCodec<Packed, Packable>
  • a helper function to create custom codec of array SomeType [byte; n]

    Type parameters

    • Packed

    • Packable

    Parameters

    • codec: BytesCodec<Packed, Packable> & { byteLength: number }

    Returns FixedBytesCodec<Packed, Packable>

byteArrayOf

  • byteArrayOf<Packed, Packable>(codec: BytesCodec<Packed, Packable> & { byteLength: number }): FixedBytesCodec<Packed, Packable>
  • a helper function to create custom codec of array SomeType [byte; n]

    Type parameters

    • Packed

    • Packable

    Parameters

    • codec: BytesCodec<Packed, Packable> & { byteLength: number }

    Returns FixedBytesCodec<Packed, Packable>

byteOf

  • a helper function to create custom codec of byte

    Type parameters

    • Packed

    • Packable

    Parameters

    Returns FixedBytesCodec<Packed, Packable>

byteOf

  • a helper function to create custom codec of byte

    Type parameters

    • Packed

    • Packable

    Parameters

    Returns FixedBytesCodec<Packed, Packable>

byteVecOf

  • a helper function to create custom codec of vector Bytes <byte>

    Type parameters

    • Packed

    • Packable

    Parameters

    Returns BytesCodec<Packed, Packable>

byteVecOf

  • a helper function to create custom codec of vector Bytes <byte>

    Type parameters

    • Packed

    • Packable

    Parameters

    Returns BytesCodec<Packed, Packable>

bytify

  • convert a BytesLike to an Uint8Array

    Parameters

    Returns Uint8Array

bytify

  • convert a BytesLike to an Uint8Array

    Parameters

    Returns Uint8Array

bytifyArrayLike

  • bytifyArrayLike(xs: ArrayLike<number>): Uint8Array
  • Parameters

    • xs: ArrayLike<number>

    Returns Uint8Array

bytifyHex

  • bytifyHex(hex: string): Uint8Array
  • Parameters

    • hex: string

    Returns Uint8Array

bytifyRawString

  • bytifyRawString(rawString: string): Uint8Array
  • Parameters

    • rawString: string

    Returns Uint8Array

bytifyRawString

  • bytifyRawString(rawString: string): Uint8Array
  • Parameters

    • rawString: string

    Returns Uint8Array

checkShape

  • checkShape<T>(shape: T, fields: keyof T[]): void
  • Type parameters

    • T

    Parameters

    • shape: T
    • fields: keyof T[]

    Returns void

concat

  • concat(...bytesLikes: BytesLike[]): Uint8Array
  • Parameters

    Returns Uint8Array

concat

  • concat(...bytesLikes: BytesLike[]): Uint8Array
  • Parameters

    Returns Uint8Array

createArrayCodec

  • Type parameters

    Parameters

    • codec: C

    Returns ArrayCodec<C>

createArrayCodec

  • Type parameters

    Parameters

    • codec: C

    Returns ArrayCodec<C>

createBytesCodec

  • This function helps to create a codec that can

    Type parameters

    • Unpacked

    • Packable

    Parameters

    Returns BytesCodec<Unpacked, Packable>

createBytesCodec

  • This function helps to create a codec that can

    Type parameters

    • Unpacked

    • Packable

    Parameters

    Returns BytesCodec<Unpacked, Packable>

createFixedBytesCodec

  • Type parameters

    • Unpacked

    • Packable

    Parameters

    Returns FixedBytesCodec<Unpacked, Packable>

createFixedBytesCodec

  • Type parameters

    • Unpacked

    • Packable

    Parameters

    Returns FixedBytesCodec<Unpacked, Packable>

createFixedHexBytesCodec

  • Parameters

    • byteLength: number

    Returns FixedBytesCodec<string, BytesLike>

createFixedHexBytesCodec

  • Parameters

    • byteLength: number

    Returns FixedBytesCodec<string, BytesLike>

createNullableCodec

  • Type parameters

    Parameters

    • codec: C

    Returns NullableCodec<C>

createNullableCodec

  • Type parameters

    Parameters

    • codec: C

    Returns NullableCodec<C>

createObjectCodec

  • createObjectCodec<Shape>(codecShape: Shape): ObjectCodec<Shape>
  • a high-order codec that helps to organize multiple codecs together into a single object

    example
    const codec = createObjectCodec({
      r: Uint8,
      g: Uint8,
      b: Uint8,
    });
    
    // { r: ArrayBuffer([0xff]), g: ArrayBuffer([0x00]), b: ArrayBuffer([0x00]) }
    codec.pack({ r: 255, g: 0, b: 0 });

    Type parameters

    Parameters

    • codecShape: Shape

    Returns ObjectCodec<Shape>

createObjectCodec

  • createObjectCodec<Shape>(codecShape: Shape): ObjectCodec<Shape>
  • a high-order codec that helps to organize multiple codecs together into a single object

    example
    const codec = createObjectCodec({
      r: Uint8,
      g: Uint8,
      b: Uint8,
    });
    
    // { r: ArrayBuffer([0xff]), g: ArrayBuffer([0x00]), b: ArrayBuffer([0x00]) }
    codec.pack({ r: 255, g: 0, b: 0 });

    Type parameters

    Parameters

    • codecShape: Shape

    Returns ObjectCodec<Shape>

Const createUintBICodec

  • createUintBICodec(byteLength: number, littleEndian?: boolean): Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number }
  • Parameters

    • byteLength: number
    • Default value littleEndian: boolean = false

    Returns Codec<Uint8Array, BI, string | number | bigint | BI, string | ArrayLike<number> | ArrayBuffer> & { __isFixedCodec__: true; byteLength: number }

createUintNumberCodec

  • Parameters

    • byteLength: number
    • Default value littleEndian: boolean = false

    Returns FixedBytesCodec<number, BIish>

diff

  • diff(x1: unknown[], x2: unknown[]): unknown[]
  • Parameters

    • x1: unknown[]
    • x2: unknown[]

    Returns unknown[]

dynvec

  • Vector with dynamic size item codec

    Type parameters

    Parameters

    • itemCodec: T

      the vector item codec. It can be fixed-size or dynamic-size. For example, you can create a recursive vector with this.

    Returns ArrayCodec<T>

dynvec

  • Vector with dynamic size item codec

    Type parameters

    Parameters

    • itemCodec: T

      the vector item codec. It can be fixed-size or dynamic-size. For example, you can create a recursive vector with this.

    Returns ArrayCodec<T>

enhancePack

enhancePack

equal

  • Parameters

    Returns boolean

equal

  • Parameters

    Returns boolean

equalUint8Array

  • equalUint8Array(a: Uint8Array, b: Uint8Array): boolean
  • Parameters

    • a: Uint8Array
    • b: Uint8Array

    Returns boolean

fixvec

  • Vector with fixed size item codec

    Type parameters

    Parameters

    • itemCodec: T

      fixed-size vector item codec

    Returns ArrayCodec<T>

fixvec

  • Vector with fixed size item codec

    Type parameters

    Parameters

    • itemCodec: T

      fixed-size vector item codec

    Returns ArrayCodec<T>

hexify

  • convert a BytesLike to an even length hex string prefixed with "0x"

    example

    hexify([0,1,2,3]) // "0x010203" hexify(Buffer.from([1, 2, 3])) // "0x010203"

    Parameters

    Returns string

hexify

  • convert a BytesLike to an even length hex string prefixed with "0x"

    example

    hexify([0,1,2,3]) // "0x010203" hexify(Buffer.from([1, 2, 3])) // "0x010203"

    Parameters

    Returns string

isCodecExecuteError

  • isCodecExecuteError(error: unknown): error is CodecExecuteError
  • Parameters

    • error: unknown

    Returns error is CodecExecuteError

isCodecExecuteError

  • isCodecExecuteError(error: unknown): error is CodecExecuteError
  • Parameters

    • error: unknown

    Returns error is CodecExecuteError

isFixedCodec

  • isFixedCodec<T>(codec: BytesCodec<T>): codec is FixedBytesCodec<T>
  • Type parameters

    • T

    Parameters

    Returns codec is FixedBytesCodec<T>

isFixedCodec

  • isFixedCodec<T>(codec: BytesCodec<T>): codec is FixedBytesCodec<T>
  • Type parameters

    • T

    Parameters

    Returns codec is FixedBytesCodec<T>

isObjectLike

  • isObjectLike(x: unknown): x is Record<string, unknown>
  • Parameters

    • x: unknown

    Returns x is Record<string, unknown>

isObjectLike

  • isObjectLike(x: unknown): x is Record<string, unknown>
  • Parameters

    • x: unknown

    Returns x is Record<string, unknown>

option

  • Option is a dynamic-size type. Serializing an option depends on whether it is empty or not:

    • if it's empty, there is zero bytes (the size is 0).
    • if it's not empty, just serialize the inner item (the size is same as the inner item's size).

    Type parameters

    Parameters

    • itemCodec: T

    Returns OptionCodec<T>

option

  • Option is a dynamic-size type. Serializing an option depends on whether it is empty or not:

    • if it's empty, there is zero bytes (the size is 0).
    • if it's not empty, just serialize the inner item (the size is same as the inner item's size).

    Type parameters

    Parameters

    • itemCodec: T

    Returns OptionCodec<T>

struct

  • Struct is a fixed-size type: all fields in struct are fixed-size and it has a fixed quantity of fields. The size of a struct is the sum of all fields' size.

    Type parameters

    Parameters

    • shape: T

      a object contains all fields' codec

    • fields: keyof T[]

      the shape's keys. It provide an order for serialization/deserialization.

    Returns ObjectCodec<T> & Fixed

struct

  • Struct is a fixed-size type: all fields in struct are fixed-size and it has a fixed quantity of fields. The size of a struct is the sum of all fields' size.

    Type parameters

    Parameters

    • shape: T

      a object contains all fields' codec

    • fields: keyof T[]

      the shape's keys. It provide an order for serialization/deserialization.

    Returns ObjectCodec<T> & Fixed

table

  • table<T>(shape: T, fields: keyof T[]): ObjectCodec<T>
  • Table is a dynamic-size type. It can be considered as a dynvec but the length is fixed.

    Type parameters

    Parameters

    • shape: T

      The table shape, item codec can be dynamic size

    • fields: keyof T[]

      the shape's keys. Also provide an order for pack/unpack.

    Returns ObjectCodec<T>

table

  • table<T>(shape: T, fields: keyof T[]): ObjectCodec<T>
  • Table is a dynamic-size type. It can be considered as a dynvec but the length is fixed.

    Type parameters

    Parameters

    • shape: T

      The table shape, item codec can be dynamic size

    • fields: keyof T[]

      the shape's keys. Also provide an order for pack/unpack.

    Returns ObjectCodec<T>

trackCodeExecuteError

  • trackCodeExecuteError<T>(path: string | number | symbol, fn: () => T): T
  • Type parameters

    • T

    Parameters

    • path: string | number | symbol
    • fn: () => T
        • (): T
        • Returns T

    Returns T

trackCodeExecuteError

  • trackCodeExecuteError<T>(path: string | number | symbol, fn: () => T): T
  • Type parameters

    • T

    Parameters

    • path: string | number | symbol
    • fn: () => T
        • (): T
        • Returns T

    Returns T

union

  • union<T>(itemCodec: T, fields: keyof T[]): UnionCodec<T>
  • Union is a dynamic-size type. Serializing a union has two steps:

    • Serialize a item type id in bytes as a 32 bit unsigned integer in little-endian. The item type id is the index of the inner items, and it's starting at 0.
    • Serialize the inner item.

    Type parameters

    Parameters

    • itemCodec: T

      the union item record

    • fields: keyof T[]

      the list of itemCodec's keys. It's also provide an order for pack/unpack.

    Returns UnionCodec<T>

union

  • union<T>(itemCodec: T, fields: keyof T[]): UnionCodec<T>
  • Union is a dynamic-size type. Serializing a union has two steps:

    • Serialize a item type id in bytes as a 32 bit unsigned integer in little-endian. The item type id is the index of the inner items, and it's starting at 0.
    • Serialize the inner item.

    Type parameters

    Parameters

    • itemCodec: T

      the union item record

    • fields: keyof T[]

      the list of itemCodec's keys. It's also provide an order for pack/unpack.

    Returns UnionCodec<T>

vector

  • General vector codec, if itemCodec is fixed size type, it will create a fixvec codec, otherwise a dynvec codec will be created.

    Type parameters

    Parameters

    • itemCodec: T

    Returns ArrayCodec<T>

vector

  • General vector codec, if itemCodec is fixed size type, it will create a fixvec codec, otherwise a dynvec codec will be created.

    Type parameters

    Parameters

    • itemCodec: T

    Returns ArrayCodec<T>

Generated using TypeDoc