Type aliases
AnyCodec
Any
Codec: Codec < any , any >
Defined in packages/codec/lib/base.d.ts:5
AnyCodec
Any
Codec: Codec < any , any >
Defined in packages/codec/src/base.ts:15
ArrayCodec
Defined in packages/codec/lib/high-order/nested.d.ts:33
Type parameters
ArrayCodec
Defined in packages/codec/lib/molecule/layout.d.ts:28
Type parameters
ArrayCodec
Defined in packages/codec/src/high-order/nested.ts:90
Type parameters
ArrayCodec
Defined in packages/codec/src/molecule/layout.ts:54
Type parameters
BytesCodec
Bytes
Codec<Unpacked, Packable>: Codec < Uint8Array , Unpacked , Packable , BytesLike >
Defined in packages/codec/lib/base.d.ts:12
Type parameters
BytesCodec
Bytes
Codec<Unpacked, Packable>: Codec < Uint8Array , Unpacked , Packable , BytesLike >
Defined in packages/codec/src/base.ts:60
Type parameters
BytesLike
BytesLike: ArrayLike < number > | ArrayBuffer | string
Defined in packages/codec/lib/base.d.ts:11
BytesLike
BytesLike: ArrayLike < number > | ArrayBuffer | string
Defined in packages/codec/src/base.ts:58
CodecOptionalPath
Defined in packages/codec/lib/error.d.ts:2
CodecOptionalPath
Defined in packages/codec/src/error.ts:3
Fixed
Fixed: { __isFixedCodec__: true ; byteLength: number }
Defined in packages/codec/lib/base.d.ts:18
Type declaration
Readonly __isFixedCodec__: true
Readonly byteLength: number
Fixed
Fixed: { __isFixedCodec__: true ; byteLength: number }
Defined in packages/codec/src/base.ts:80
Type declaration
Readonly __isFixedCodec__: true
Readonly byteLength: number
FixedBytesCodec
Fixed
BytesCodec<Unpacked, Packable>: BytesCodec < Unpacked , Packable > & Fixed
Defined in packages/codec/lib/base.d.ts:22
Type parameters
FixedBytesCodec
Fixed
BytesCodec<Unpacked, Packable>: BytesCodec < Unpacked , Packable > & Fixed
Defined in packages/codec/src/base.ts:85
Type parameters
NonNullableKeys
NonNullableKeys<O>: {} [ keyof O ]
Defined in packages/codec/lib/molecule/layout.d.ts:16
Type parameters
O: Record < string , unknown >
NonNullableKeys
NonNullableKeys<O>: {} [ keyof O ]
Defined in packages/codec/src/molecule/layout.ts:35
Type parameters
O: Record < string , unknown >
NullableKeys
NullableKeys<O>: {} [ keyof O ]
Defined in packages/codec/lib/molecule/layout.d.ts:13
Type parameters
O: Record < string , unknown >
NullableKeys
NullableKeys<O>: {} [ keyof O ]
Defined in packages/codec/src/molecule/layout.ts:32
Type parameters
O: Record < string , unknown >
ObjectCodec
Object
Codec<Shape>: Codec < {} , {} , {} , {} >
Defined in packages/codec/lib/high-order/nested.d.ts:8
Type parameters
ObjectCodec
Defined in packages/codec/lib/molecule/layout.d.ts:20
Type parameters
ObjectCodec
Object
Codec<Shape>: Codec < {} , {} , {} , {} >
Defined in packages/codec/src/high-order/nested.ts:36
Type parameters
ObjectCodec
Defined in packages/codec/src/molecule/layout.ts:44
Type parameters
ObjectCodecShape
Object
CodecShape: Record < string , AnyCodec >
Defined in packages/codec/lib/high-order/nested.d.ts:7
ObjectCodecShape
Object
CodecShape: Record < string , AnyCodec >
Defined in packages/codec/src/high-order/nested.ts:35
PackParam
PackParam<T>: T extends Codec < infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packable : never
Defined in packages/codec/lib/base.d.ts:7
Type parameters
PackParam
PackParam<T>: T extends Codec < infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packable : never
Defined in packages/codec/src/base.ts:25
Type parameters
PackResult
PackResult<T>: T extends Codec < infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packed : never
Defined in packages/codec/lib/base.d.ts:6
Type parameters
PackResult
PackResult<T>: T extends Codec < infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Packed : never
Defined in packages/codec/src/base.ts:17
Type parameters
PartialNullable
Defined in packages/codec/lib/molecule/layout.d.ts:19
Type parameters
O: Record < string , unknown >
PartialNullable
Defined in packages/codec/src/molecule/layout.ts:40
Type parameters
O: Record < string , unknown >
Uint8ArrayCodec
Uint8
ArrayCodec<Unpacked, Packable>: Codec < Uint8Array , Unpacked , Packable >
Defined in packages/codec/lib/base.d.ts:10
Type parameters
Uint8ArrayCodec
Uint8
ArrayCodec<Unpacked, Packable>: Codec < Uint8Array , Unpacked , Packable >
Defined in packages/codec/src/base.ts:52
Type parameters
UnionCodec
Union
Codec<T>: BytesCodec < {} [ keyof T ] , {} [ keyof T ] >
Defined in packages/codec/lib/molecule/layout.d.ts:29
Type parameters
UnionCodec
Union
Codec<T>: BytesCodec < {} [ keyof T ] , {} [ keyof T ] >
Defined in packages/codec/src/molecule/layout.ts:59
Type parameters
UnpackParam
UnpackParam<T>: T extends Codec < infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpackable : never
Defined in packages/codec/lib/base.d.ts:9
Type parameters
UnpackParam
UnpackParam<T>: T extends Codec < infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpackable : never
Defined in packages/codec/src/base.ts:43
Type parameters
UnpackResult
UnpackResult<T>: T extends Codec < infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpacked : never
Defined in packages/codec/lib/base.d.ts:8
Type parameters
UnpackResult
UnpackResult<T>: T extends Codec < infer Packed, infer Unpacked, infer Packable, infer Unpackable> ? Unpacked : never
Defined in packages/codec/src/base.ts:34
Type parameters
Variables
Const Byte32
Defined in packages/codec/lib/blockchain.d.ts:14
Const Byte32
Byte32
: Codec < Uint8Array , string , string | ArrayLike < number > | ArrayBuffer , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createFixedHexBytesCodec(32)
Defined in packages/codec/src/blockchain.ts:39
Const Byte32Vec
Defined in packages/codec/lib/blockchain.d.ts:15
Const Byte32Vec
Byte32
Vec: Codec < Uint8Array , string [] , ( string | ArrayLike < number > | ArrayBuffer ) [] , string | ArrayLike < number > | ArrayBuffer > = vector(Byte32)
Defined in packages/codec/src/blockchain.ts:40
Const Bytes
Defined in packages/codec/lib/blockchain.d.ts:11
Const Bytes
Bytes
: Codec < Uint8Array , string , string | ArrayLike < number > | ArrayBuffer , string | ArrayLike < number > | ArrayBuffer > = byteVecOf({ pack: bytify, unpack: hexify })
Defined in packages/codec/src/blockchain.ts:35
Const BytesOpt
Defined in packages/codec/lib/blockchain.d.ts:12
Const BytesOpt
Bytes
Opt: OptionCodec < Codec < Uint8Array , string , string | ArrayLike < number > | ArrayBuffer , string | ArrayLike < number > | ArrayBuffer > > = option(Bytes)
Defined in packages/codec/src/blockchain.ts:37
Const BytesVec
Defined in packages/codec/lib/blockchain.d.ts:13
Const BytesVec
Bytes
Vec: Codec < Uint8Array , string [] , ( string | ArrayLike < number > | ArrayBuffer ) [] , string | ArrayLike < number > | ArrayBuffer > = vector(Bytes)
Defined in packages/codec/src/blockchain.ts:38
Const CODEC_EXECUTE_ERROR_NAME
CODEC_EXECUTE_ERROR_NAME: "CodecExecuteError" = "CodecExecuteError"
Defined in packages/codec/src/error.ts:10
Const CODEC_OPTIONAL_PATH
CODEC_OPTIONAL_PATH: "__lc_option__" = "__lc_option__"
Defined in packages/codec/lib/error.d.ts:1
Const CODEC_OPTIONAL_PATH
CODEC_OPTIONAL_PATH: "__lc_option__" = "__lc_option__"
Defined in packages/codec/src/error.ts:2
Const HEX_DECIMAL_REGEX
HEX_DECIMAL_REGEX: RegExp = /^0x([0-9a-fA-F])+$/
Defined in packages/codec/src/utils.ts:7
Const HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP
HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP: Map < number , RegExp > = new Map<number, RegExp>()
Defined in packages/codec/src/utils.ts:8
Const HEX_STRING_REGEX
HEX_STRING_REGEX: RegExp = /^0x([0-9a-fA-F][0-9a-fA-F])*$/
Defined in packages/codec/src/utils.ts:28
Const HEX_STRING_WITH_BYTELENGTH_REGEX_MAP
HEX_STRING_WITH_BYTELENGTH_REGEX_MAP: Map < number , RegExp > = new Map<number, RegExp>()
Defined in packages/codec/src/utils.ts:29
Const HexifyCodec
Hexify
Codec: Codec < Uint8Array , string , string | ArrayLike < number > | ArrayBuffer , string | ArrayLike < number > | ArrayBuffer > = createBytesCodec<string, BytesLike>({pack: bytify,unpack: hexify,})
Defined in packages/codec/src/blockchain.ts:72
Const Uint128
Defined in packages/codec/lib/number/uint.d.ts:27
Const Uint128
Uint128
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = Uint128LE
Defined in packages/codec/src/number/uint.ts:116
Const Uint128BE
Defined in packages/codec/lib/number/uint.d.ts:23
Const Uint128BE
Uint128BE
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintBICodec(16)
Defined in packages/codec/src/number/uint.ts:112
Const Uint128LE
Defined in packages/codec/lib/number/uint.d.ts:22
Const Uint128LE
Uint128LE
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintBICodec(16, true)
Defined in packages/codec/src/number/uint.ts:111
Const Uint16
Defined in packages/codec/lib/number/uint.d.ts:9
Const Uint16
Uint16
: Codec < Uint8Array , number , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = Uint16LE
Defined in packages/codec/src/number/uint.ts:95
Const Uint16BE
Defined in packages/codec/lib/number/uint.d.ts:5
Const Uint16BE
Uint16BE
: Codec < Uint8Array , number , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintNumberCodec(2)
Defined in packages/codec/src/number/uint.ts:91
Const Uint16LE
Defined in packages/codec/lib/number/uint.d.ts:4
Const Uint16LE
Uint16LE
: Codec < Uint8Array , number , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintNumberCodec(2, true)
Defined in packages/codec/src/number/uint.ts:90
Const Uint256
Defined in packages/codec/lib/number/uint.d.ts:33
Const Uint256
Uint256
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = Uint256LE
Defined in packages/codec/src/number/uint.ts:123
Const Uint256BE
Defined in packages/codec/lib/number/uint.d.ts:29
Const Uint256BE
Uint256BE
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintBICodec(32)
Defined in packages/codec/src/number/uint.ts:119
Const Uint256LE
Defined in packages/codec/lib/number/uint.d.ts:28
Const Uint256LE
Uint256LE
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintBICodec(32, true)
Defined in packages/codec/src/number/uint.ts:118
Const Uint32
Defined in packages/codec/lib/number/uint.d.ts:15
Const Uint32
Uint32
: Codec < Uint8Array , number , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = Uint32LE
Defined in packages/codec/src/number/uint.ts:102
Const Uint32BE
Defined in packages/codec/lib/number/uint.d.ts:11
Const Uint32BE
Uint32BE
: Codec < Uint8Array , number , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintNumberCodec(4)
Defined in packages/codec/src/number/uint.ts:98
Const Uint32LE
Defined in packages/codec/lib/number/uint.d.ts:10
Const Uint32LE
Uint32LE
: Codec < Uint8Array , number , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintNumberCodec(4, true)
Defined in packages/codec/src/number/uint.ts:97
Const Uint512
Defined in packages/codec/lib/number/uint.d.ts:39
Const Uint512
Uint512
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = Uint512LE
Defined in packages/codec/src/number/uint.ts:130
Const Uint512BE
Defined in packages/codec/lib/number/uint.d.ts:35
Const Uint512BE
Uint512BE
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintBICodec(64)
Defined in packages/codec/src/number/uint.ts:126
Const Uint512LE
Defined in packages/codec/lib/number/uint.d.ts:34
Const Uint512LE
Uint512LE
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintBICodec(64, true)
Defined in packages/codec/src/number/uint.ts:125
Const Uint64
Defined in packages/codec/lib/number/uint.d.ts:21
Const Uint64
Uint64
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = Uint64LE
Defined in packages/codec/src/number/uint.ts:109
Const Uint64BE
Defined in packages/codec/lib/number/uint.d.ts:17
Const Uint64BE
Uint64BE
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintBICodec(8)
Defined in packages/codec/src/number/uint.ts:105
Const Uint64LE
Defined in packages/codec/lib/number/uint.d.ts:16
Const Uint64LE
Uint64LE
: Codec < Uint8Array , BI , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintBICodec(8, true)
Defined in packages/codec/src/number/uint.ts:104
Const Uint8
Defined in packages/codec/lib/number/uint.d.ts:3
Const Uint8
Uint8
: Codec < Uint8Array , number , string | number | bigint | BI , string | ArrayLike < number > | ArrayBuffer > & { __isFixedCodec__
: true ; byteLength
: number } = createUintNumberCodec(1)
Defined in packages/codec/src/number/uint.ts:88
Const WitnessArgs
Witness
Args: 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 } >
Defined in packages/codec/lib/blockchain.d.ts:37
Const WitnessArgs
Witness
Args: 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,})
Defined in packages/codec/src/blockchain.ts:85
@ckb-lumos/codec
This module provides a set of functions to pack(encode) and unpack(decode) data.
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 ofCodec
that helps to bind molecule to JavaScript plain object/array.Array<T>
<=>Uint8Array
Array<T>
<=>Uint8Array
{ [key: string]: T }
<=>Uint8Array
{ [key: string]: T }
<=>Uint8Array
T | undefined
<=>Uint8Array
{ 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
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
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 ofCodec
that helps to encode/decode number to/fromUint8Array
. 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 avector UTF8String <byte>
import { byteVecOf, bytes } 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(bytes.bytify(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 asarray Uint32 [byte; 4]
is generally translated asnumber
.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)
withOmniLockWitnessLock.signature(BytesOpt)
, then it will be easier to do the signing, we can check the real world OmniLock witness case to see how it worksWorks 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>;