bcs
@iota/bcs / bcs
Variable: bcs
constbcs:object
Defined in: bcs.ts:190
Type Declaration
fixedArray
fixedArray: {<
T,Name>(size,type,options?):BcsType<InferBcsType<T>[],Iterable<InferBcsInput<T>,any,any> &object,Name>; <T,Input,Name>(size,type,options?):BcsType<T[],Iterable<Input,any,any> &object,Name>; }
Creates a BcsType that represents a fixed length array of a given type
Call Signature
<
T,Name>(size,type,options?):BcsType<InferBcsType<T>[],Iterable<InferBcsInput<T>,any,any> &object,Name>
Type Parameters
T
T extends BcsType<any, any, string>
Name
Name extends string = string
Parameters
size
number
type
T
options?
BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>, any, any> & object, Name>
Returns
BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>, any, any> & object, Name>
Call Signature
<
T,Input,Name>(size,type,options?):BcsType<T[],Iterable<Input,any,any> &object,Name>
Type Parameters
T
T
Input
Input
Name
Name extends string = string
Parameters
size
number
type
BcsType<T, Input>
options?
BcsTypeOptions<T[], Iterable<Input, any, any> & object, Name>
Returns
BcsType<T[], Iterable<Input, any, any> & object, Name>
Param
The number of elements in the array
Param
The BcsType of each element in the array
Example
bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
map
map: {<
K,V>(keyType,valueType):BcsType<Map<InferBcsType<K>,InferBcsType<V>>,Map<InferBcsInput<K>,InferBcsInput<V>>,`Map<${K["name"]}, ${V["name"]}>`>; <K,V,InputK,InputV>(keyType,valueType):BcsType<Map<K,V>,Map<InputK,InputV>,`Map<${string}, ${string}>`>; }
Creates a BcsType representing a map of a given key and value type
Call Signature
<
K,V>(keyType,valueType):BcsType<Map<InferBcsType<K>,InferBcsType<V>>,Map<InferBcsInput<K>,InferBcsInput<V>>,`Map<${K["name"]}, ${V["name"]}>`>
Type Parameters
K
K extends BcsType<any, any, string>
V
V extends BcsType<any, any, string>
Parameters
keyType
K
valueType
V
Returns
BcsType<Map<InferBcsType<K>, InferBcsType<V>>, Map<InferBcsInput<K>, InferBcsInput<V>>, `Map<${K["name"]}, ${V["name"]}>`>
Call Signature
<
K,V,InputK,InputV>(keyType,valueType):BcsType<Map<K,V>,Map<InputK,InputV>,`Map<${string}, ${string}>`>
Type Parameters
K
K
V
V
InputK
InputK = K
InputV
InputV = V
Parameters
keyType
BcsType<K, InputK>
valueType
BcsType<V, InputV>
Returns
BcsType<Map<K, V>, Map<InputK, InputV>, `Map<${string}, ${string}>`>
Param
The BcsType of the key
Param
The BcsType of the value
Example
const map = bcs.map(bcs.u8(), bcs.string())
map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
option
option: {<
T>(type):BcsType<InferBcsType<T> |null,InferBcsInput<T> |null|undefined,`Option<${T["name"]}>`>; <T,Input,Name>(type):BcsType<T|null,Input|null|undefined>; }
Creates a BcsType representing an optional value
Call Signature
<
T>(type):BcsType<InferBcsType<T> |null,InferBcsInput<T> |null|undefined,`Option<${T["name"]}>`>
Type Parameters
T
T extends BcsType<any, any, string>
Parameters
type
T
Returns
BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T["name"]}>`>
Call Signature
<
T,Input,Name>(type):BcsType<T|null,Input|null|undefined>
Type Parameters
T
T
Input
Input
Name
Name extends string = string
Parameters
type
BcsType<T, Input, Name>
Returns
BcsType<T | null, Input | null | undefined>
Param
The BcsType of the optional value
Example
bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
vector
vector: {<
T,Name>(type,options?):BcsType<InferBcsType<T>[],Iterable<InferBcsInput<T>,any,any> &object,Name>; <T,Input,Name>(type,options?):BcsType<T[],Iterable<Input,any,any> &object,`vector<${Name}>`>; }
Creates a BcsType representing a variable length vector of a given type
Call Signature
<
T,Name>(type,options?):BcsType<InferBcsType<T>[],Iterable<InferBcsInput<T>,any,any> &object,Name>
Type Parameters
T
T extends BcsType<any, any, string>
Name
Name extends string = `vector<${T["name"]}>`
Parameters
type
T
options?
BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>, any, any> & object, Name>
Returns
BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>, any, any> & object, Name>
Call Signature
<
T,Input,Name>(type,options?):BcsType<T[],Iterable<Input,any,any> &object,`vector<${Name}>`>
Type Parameters
T
T
Input
Input
Name
Name extends string = string
Parameters
type
BcsType<T, Input, Name>
options?
BcsTypeOptions<T[], Iterable<Input, any, any> & object, `vector<${Name}>`>
Returns
BcsType<T[], Iterable<Input, any, any> & object, `vector<${Name}>`>
Param
The BcsType of each element in the vector
Example
bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
bool()
bool(
options?):BcsType<boolean,boolean,"bool">
Creates a BcsType that can be used to read and write boolean values.
Parameters
options?
BcsTypeOptions<boolean, boolean, string>
Returns
BcsType<boolean, boolean, "bool">
Example
bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
bytes()
bytes<
T>(size,options?):BcsType<Uint8Array<ArrayBufferLike>,Iterable<number,any,any>,`bytes[${T}]`>
Creates a BcsType representing a fixed length byte array
Type Parameters
T
T extends number
Parameters
size
T
The number of bytes this types represents
options?
BcsTypeOptions<Uint8Array<ArrayBufferLike>, Iterable<number, any, any>, string>
Returns
BcsType<Uint8Array<ArrayBufferLike>, Iterable<number, any, any>, `bytes[${T}]`>
Example
bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
byteVector()
byteVector(
options?):BcsType<Uint8Array<ArrayBufferLike>,Iterable<number,any,any>,"vector<u8>">
Creates a BcsType representing a variable length byte array
Parameters
options?
BcsTypeOptions<Uint8Array<ArrayBufferLike>, Iterable<number, any, any>, string>
Returns
BcsType<Uint8Array<ArrayBufferLike>, Iterable<number, any, any>, "vector<u8>">
Example
bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
enum()
enum<
T,Name>(name,fields,options?):BcsEnum<T,Name>
Creates a BcsType representing an enum of a given set of options
Type Parameters
T
T extends Record<string, BcsType<any, any, string> | null>
Name
Name extends string = string
Parameters
name
Name
The name of the enum
fields
T
options?
Omit<BcsTypeOptions<EnumOutputShape<{ [K in string | number | symbol]: T[K] extends BcsType<U, any, any> ? U : true }>, EnumInputShape<{ [K in string | number | symbol]: T[K] extends BcsType<any, U, any> ? U : boolean | object | null }>, Name>, "name">
Returns
BcsEnum<T, Name>
Example
const enum = bcs.enum('MyEnum', {
A: bcs.u8(),
B: bcs.string(),
C: null,
})
enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
lazy()
lazy<
T>(cb):T
Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
Type Parameters
T
T extends BcsType<any, any, string>
Parameters
cb
() => T
A callback that returns the BcsType
Returns
T
string()
string(
options?):BcsType<string,string,string>
Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
Parameters
options?
BcsTypeOptions<string, string, string>
Returns
BcsType<string, string, string>
Example
bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
struct()
struct<
T,Name>(name,fields,options?):BcsStruct<T,string>
Creates a BcsType representing a struct of a given set of fields
Type Parameters
T
T extends Record<string, BcsType<any, any, string>>
Name
Name extends string = string
Parameters
name
Name
The name of the struct
fields
T
The fields of the struct. The order of the fields affects how data is serialized and deserialized
options?
Omit<BcsTypeOptions<{ [K in string | number | symbol]: T[K] extends BcsType<U, any, string> ? U : never }, { [K in string | number | symbol]: T[K] extends BcsType<any, U, string> ? U : never }, string>, "name">
Returns
BcsStruct<T, string>
Example
const struct = bcs.struct('MyStruct', {
a: bcs.u8(),
b: bcs.string(),
})
struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
tuple()
tuple<
T,Name>(fields,options?):BcsTuple<T,Name>
Creates a BcsType representing a tuple of a given set of types
Type Parameters
T
T extends readonly BcsType<any, any, string>[]
Name
Name extends string = `(${JoinString<{ [K in string | number | symbol]: T[K] extends BcsType<any, any, T> ? T : never }, ", ">})`
Parameters
fields
T
options?
BcsTypeOptions<{ -readonly [K in string | number | symbol]: T[K] extends BcsType<T, any, string> ? T : never }, { [K in string | number | symbol]: T[K] extends BcsType<any, T, string> ? T : never }, Name>
Returns
BcsTuple<T, Name>
Example
const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
u128()
u128(
options?):BcsType<string,string|number|bigint,"u128">
Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
Parameters
options?
BcsTypeOptions<string, string | number | bigint, string>
Returns
BcsType<string, string | number | bigint, "u128">
Example
bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
u16()
u16(
options?):BcsType<number,number,"u16">
Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
Parameters
options?
BcsTypeOptions<number, number, string>
Returns
BcsType<number, number, "u16">
Example
bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
u256()
u256(
options?):BcsType<string,string|number|bigint,"u256">
Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
Parameters
options?
BcsTypeOptions<string, string | number | bigint, string>
Returns
BcsType<string, string | number | bigint, "u256">
Example
bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
u32()
u32(
options?):BcsType<number,number,"u32">
Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
Parameters
options?
BcsTypeOptions<number, number, string>
Returns
BcsType<number, number, "u32">
Example
bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
u64()
u64(
options?):BcsType<string,string|number|bigint,"u64">
Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
Parameters
options?
BcsTypeOptions<string, string | number | bigint, string>
Returns
BcsType<string, string | number | bigint, "u64">
Example
bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
u8()
u8(
options?):BcsType<number,number,"u8">
Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
Parameters
options?
BcsTypeOptions<number, number, string>
Returns
BcsType<number, number, "u8">
Example
bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
uleb128()
uleb128(
options?):BcsType<number,number,"uleb128">
Creates a BcsType that can be used to read and write unsigned LEB encoded integers
Parameters
options?
BcsTypeOptions<number, number, string>
Returns
BcsType<number, number, "uleb128">
Example