@haneullabs/bcs 0.1.0 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -9
- package/dist/bcs-type.d.mts +138 -0
- package/dist/bcs-type.d.mts.map +1 -0
- package/dist/bcs-type.mjs +269 -0
- package/dist/bcs-type.mjs.map +1 -0
- package/dist/bcs.d.mts +184 -0
- package/dist/bcs.d.mts.map +1 -0
- package/dist/bcs.mjs +250 -0
- package/dist/bcs.mjs.map +1 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +8 -0
- package/dist/reader.d.mts +96 -0
- package/dist/reader.d.mts.map +1 -0
- package/dist/reader.mjs +142 -0
- package/dist/reader.mjs.map +1 -0
- package/dist/types.d.mts +20 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +1 -0
- package/dist/uleb.d.mts +9 -0
- package/dist/uleb.d.mts.map +1 -0
- package/dist/uleb.mjs +36 -0
- package/dist/uleb.mjs.map +1 -0
- package/dist/utils.d.mts +23 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +56 -0
- package/dist/utils.mjs.map +1 -0
- package/dist/writer.d.mts +126 -0
- package/dist/writer.d.mts.map +1 -0
- package/dist/writer.mjs +179 -0
- package/dist/writer.mjs.map +1 -0
- package/docs/index.md +352 -0
- package/docs/llms-index.md +6 -0
- package/package.json +21 -25
- package/src/bcs.ts +32 -7
- package/src/index.ts +21 -34
- package/src/uleb.ts +0 -1
- package/dist/cjs/bcs-type.d.ts +0 -127
- package/dist/cjs/bcs-type.js +0 -386
- package/dist/cjs/bcs-type.js.map +0 -7
- package/dist/cjs/bcs.d.ts +0 -175
- package/dist/cjs/bcs.js +0 -406
- package/dist/cjs/bcs.js.map +0 -7
- package/dist/cjs/index.d.ts +0 -22
- package/dist/cjs/index.js +0 -59
- package/dist/cjs/index.js.map +0 -7
- package/dist/cjs/package.json +0 -5
- package/dist/cjs/reader.d.ts +0 -92
- package/dist/cjs/reader.js +0 -136
- package/dist/cjs/reader.js.map +0 -7
- package/dist/cjs/types.d.ts +0 -28
- package/dist/cjs/types.js +0 -17
- package/dist/cjs/types.js.map +0 -7
- package/dist/cjs/uleb.d.ts +0 -5
- package/dist/cjs/uleb.js +0 -66
- package/dist/cjs/uleb.js.map +0 -7
- package/dist/cjs/utils.d.ts +0 -18
- package/dist/cjs/utils.js +0 -74
- package/dist/cjs/utils.js.map +0 -7
- package/dist/cjs/writer.d.ts +0 -117
- package/dist/cjs/writer.js +0 -196
- package/dist/cjs/writer.js.map +0 -7
- package/dist/esm/bcs-type.d.ts +0 -127
- package/dist/esm/bcs-type.js +0 -366
- package/dist/esm/bcs-type.js.map +0 -7
- package/dist/esm/bcs.d.ts +0 -175
- package/dist/esm/bcs.js +0 -397
- package/dist/esm/bcs.js.map +0 -7
- package/dist/esm/index.d.ts +0 -22
- package/dist/esm/index.js +0 -46
- package/dist/esm/index.js.map +0 -7
- package/dist/esm/package.json +0 -5
- package/dist/esm/reader.d.ts +0 -92
- package/dist/esm/reader.js +0 -116
- package/dist/esm/reader.js.map +0 -7
- package/dist/esm/types.d.ts +0 -28
- package/dist/esm/types.js +0 -1
- package/dist/esm/types.js.map +0 -7
- package/dist/esm/uleb.d.ts +0 -5
- package/dist/esm/uleb.js +0 -46
- package/dist/esm/uleb.js.map +0 -7
- package/dist/esm/utils.d.ts +0 -18
- package/dist/esm/utils.js +0 -54
- package/dist/esm/utils.js.map +0 -7
- package/dist/esm/writer.d.ts +0 -117
- package/dist/esm/writer.js +0 -176
- package/dist/esm/writer.js.map +0 -7
- package/dist/tsconfig.esm.tsbuildinfo +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
package/dist/bcs.d.mts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { EnumInputShape, EnumOutputShape, InferBcsInput, InferBcsType, JoinString } from "./types.mjs";
|
|
2
|
+
import { BcsEnum, BcsStruct, BcsTuple, BcsType, BcsTypeOptions } from "./bcs-type.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/bcs.d.ts
|
|
5
|
+
declare function fixedArray<T extends BcsType<any>, Name extends string = string>(size: number, type: T, options?: BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
|
|
6
|
+
length: number;
|
|
7
|
+
}, Name>): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
|
|
8
|
+
length: number;
|
|
9
|
+
}, Name>;
|
|
10
|
+
declare function fixedArray<T, Input, Name extends string = string>(size: number, type: BcsType<T, Input>, options?: BcsTypeOptions<T[], Iterable<Input> & {
|
|
11
|
+
length: number;
|
|
12
|
+
}, Name>): BcsType<T[], Iterable<Input> & {
|
|
13
|
+
length: number;
|
|
14
|
+
}, Name>;
|
|
15
|
+
declare function option<T extends BcsType<any>>(type: T): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`>;
|
|
16
|
+
declare function option<T, Input, Name extends string = string>(type: BcsType<T, Input, Name>): BcsType<T | null, Input | null | undefined>;
|
|
17
|
+
declare function vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(type: T, options?: BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
|
|
18
|
+
length: number;
|
|
19
|
+
}, Name>): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
|
|
20
|
+
length: number;
|
|
21
|
+
}, Name>;
|
|
22
|
+
declare function vector<T, Input, Name extends string = string>(type: BcsType<T, Input, Name>, options?: BcsTypeOptions<T[], Iterable<Input> & {
|
|
23
|
+
length: number;
|
|
24
|
+
}, `vector<${Name}>`>): BcsType<T[], Iterable<Input> & {
|
|
25
|
+
length: number;
|
|
26
|
+
}, `vector<${Name}>`>;
|
|
27
|
+
/**
|
|
28
|
+
* Compares two byte arrays using lexicographic ordering.
|
|
29
|
+
* This matches Rust's Ord implementation for Vec<u8>/[u8] which is used for BTreeMap key ordering.
|
|
30
|
+
* Comparison is done byte-by-byte first, then by length if all compared bytes are equal.
|
|
31
|
+
*/
|
|
32
|
+
declare function compareBcsBytes(a: Uint8Array, b: Uint8Array): number;
|
|
33
|
+
declare function map<K$1 extends BcsType<any>, V extends BcsType<any>>(keyType: K$1, valueType: V): BcsType<Map<InferBcsType<K$1>, InferBcsType<V>>, Map<InferBcsInput<K$1>, InferBcsInput<V>>, `Map<${K$1['name']}, ${V['name']}>`>;
|
|
34
|
+
declare function map<K$1, V, InputK = K$1, InputV = V>(keyType: BcsType<K$1, InputK>, valueType: BcsType<V, InputV>): BcsType<Map<K$1, V>, Map<InputK, InputV>, `Map<${string}, ${string}>`>;
|
|
35
|
+
declare const bcs: {
|
|
36
|
+
/**
|
|
37
|
+
* Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
|
|
38
|
+
* @example
|
|
39
|
+
* bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
|
|
40
|
+
*/
|
|
41
|
+
u8(options?: BcsTypeOptions<number>): BcsType<number, number, "u8">;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
|
|
44
|
+
* @example
|
|
45
|
+
* bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
|
|
46
|
+
*/
|
|
47
|
+
u16(options?: BcsTypeOptions<number>): BcsType<number, number, "u16">;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
|
|
50
|
+
* @example
|
|
51
|
+
* bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
|
|
52
|
+
*/
|
|
53
|
+
u32(options?: BcsTypeOptions<number>): BcsType<number, number, "u32">;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
|
|
56
|
+
* @example
|
|
57
|
+
* bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
|
|
58
|
+
*/
|
|
59
|
+
u64(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u64">;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
|
|
62
|
+
* @example
|
|
63
|
+
* bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
|
|
64
|
+
*/
|
|
65
|
+
u128(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u128">;
|
|
66
|
+
/**
|
|
67
|
+
* Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
|
|
68
|
+
* @example
|
|
69
|
+
* bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
|
|
70
|
+
*/
|
|
71
|
+
u256(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u256">;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a BcsType that can be used to read and write boolean values.
|
|
74
|
+
* @example
|
|
75
|
+
* bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
|
|
76
|
+
*/
|
|
77
|
+
bool(options?: BcsTypeOptions<boolean>): BcsType<boolean, boolean, "bool">;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a BcsType that can be used to read and write unsigned LEB encoded integers
|
|
80
|
+
* @example
|
|
81
|
+
*
|
|
82
|
+
*/
|
|
83
|
+
uleb128(options?: BcsTypeOptions<number>): BcsType<number, number, string>;
|
|
84
|
+
/**
|
|
85
|
+
* Creates a BcsType representing a fixed length byte array
|
|
86
|
+
* @param size The number of bytes this types represents
|
|
87
|
+
* @example
|
|
88
|
+
* bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
|
|
89
|
+
*/
|
|
90
|
+
bytes<T extends number>(size: T, options?: BcsTypeOptions<Uint8Array, Iterable<number>>): BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, `bytes[${T}]`>;
|
|
91
|
+
/**
|
|
92
|
+
* Creates a BcsType representing a variable length byte array
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
|
|
96
|
+
*/
|
|
97
|
+
byteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>): BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
|
|
98
|
+
/**
|
|
99
|
+
* Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
|
|
100
|
+
* @example
|
|
101
|
+
* bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
|
|
102
|
+
*/
|
|
103
|
+
string(options?: BcsTypeOptions<string>): BcsType<string, string, "string">;
|
|
104
|
+
/**
|
|
105
|
+
* Creates a BcsType that represents a fixed length array of a given type
|
|
106
|
+
* @param size The number of elements in the array
|
|
107
|
+
* @param type The BcsType of each element in the array
|
|
108
|
+
* @example
|
|
109
|
+
* bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
|
|
110
|
+
*/
|
|
111
|
+
fixedArray: typeof fixedArray;
|
|
112
|
+
/**
|
|
113
|
+
* Creates a BcsType representing an optional value
|
|
114
|
+
* @param type The BcsType of the optional value
|
|
115
|
+
* @example
|
|
116
|
+
* bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
|
|
117
|
+
* bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
|
|
118
|
+
*/
|
|
119
|
+
option: typeof option;
|
|
120
|
+
/**
|
|
121
|
+
* Creates a BcsType representing a variable length vector of a given type
|
|
122
|
+
* @param type The BcsType of each element in the vector
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
|
|
126
|
+
*/
|
|
127
|
+
vector: typeof vector;
|
|
128
|
+
/**
|
|
129
|
+
* Creates a BcsType representing a tuple of a given set of types
|
|
130
|
+
* @param types The BcsTypes for each element in the tuple
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
|
|
134
|
+
* tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
|
|
135
|
+
*/
|
|
136
|
+
tuple<const T extends readonly BcsType<any, any>[], const Name extends string = `(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T_1 extends string> ? T_1 : never }, ", ">})`>(fields: T, options?: BcsTypeOptions<{ -readonly [K_1 in keyof T]: T[K_1] extends BcsType<infer T_1, any> ? T_1 : never }, { [K_1 in keyof T]: T[K_1] extends BcsType<any, infer T_1> ? T_1 : never }, Name>): BcsTuple<T, Name>;
|
|
137
|
+
/**
|
|
138
|
+
* Creates a BcsType representing a struct of a given set of fields
|
|
139
|
+
* @param name The name of the struct
|
|
140
|
+
* @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* const struct = bcs.struct('MyStruct', {
|
|
144
|
+
* a: bcs.u8(),
|
|
145
|
+
* b: bcs.string(),
|
|
146
|
+
* })
|
|
147
|
+
* struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
|
|
148
|
+
*/
|
|
149
|
+
struct<T extends Record<string, BcsType<any>>, const Name extends string = string>(name: Name, fields: T, options?: Omit<BcsTypeOptions<{ [K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never }, { [K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never }>, "name">): BcsStruct<T, string>;
|
|
150
|
+
/**
|
|
151
|
+
* Creates a BcsType representing an enum of a given set of options
|
|
152
|
+
* @param name The name of the enum
|
|
153
|
+
* @param values The values of the enum. The order of the values affects how data is serialized and deserialized.
|
|
154
|
+
* null can be used to represent a variant with no data.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* const enum = bcs.enum('MyEnum', {
|
|
158
|
+
* A: bcs.u8(),
|
|
159
|
+
* B: bcs.string(),
|
|
160
|
+
* C: null,
|
|
161
|
+
* })
|
|
162
|
+
* enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
|
|
163
|
+
* enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
|
|
164
|
+
* enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
|
|
165
|
+
*/
|
|
166
|
+
enum<T extends Record<string, BcsType<any> | null>, const Name extends string = string>(name: Name, fields: T, options?: Omit<BcsTypeOptions<EnumOutputShape<{ [K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true }>, EnumInputShape<{ [K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null }>, Name>, "name">): BcsEnum<T, Name>;
|
|
167
|
+
/**
|
|
168
|
+
* Creates a BcsType representing a map of a given key and value type
|
|
169
|
+
* @param keyType The BcsType of the key
|
|
170
|
+
* @param valueType The BcsType of the value
|
|
171
|
+
* @example
|
|
172
|
+
* const map = bcs.map(bcs.u8(), bcs.string())
|
|
173
|
+
* map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
|
|
174
|
+
*/
|
|
175
|
+
map: typeof map;
|
|
176
|
+
/**
|
|
177
|
+
* Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
|
|
178
|
+
* @param cb A callback that returns the BcsType
|
|
179
|
+
*/
|
|
180
|
+
lazy<T extends BcsType<any>>(cb: () => T): T;
|
|
181
|
+
};
|
|
182
|
+
//#endregion
|
|
183
|
+
export { bcs, compareBcsBytes };
|
|
184
|
+
//# sourceMappingURL=bcs.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bcs.d.mts","names":[],"sources":["../src/bcs.ts"],"mappings":";;;;iBAyBS,UAAA,WAAqB,OAAA,oCAAA,CAC7B,IAAA,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ulebEncode } from "./uleb.mjs";
|
|
2
|
+
import { BcsEnum, BcsStruct, BcsTuple, BcsType, bigUIntBcsType, dynamicSizeBcsType, fixedSizeBcsType, lazyBcsType, stringLikeBcsType, uIntBcsType } from "./bcs-type.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/bcs.ts
|
|
5
|
+
function fixedArray(size, type, options) {
|
|
6
|
+
return new BcsType({
|
|
7
|
+
read: (reader) => {
|
|
8
|
+
const result = new Array(size);
|
|
9
|
+
for (let i = 0; i < size; i++) result[i] = type.read(reader);
|
|
10
|
+
return result;
|
|
11
|
+
},
|
|
12
|
+
write: (value, writer) => {
|
|
13
|
+
for (const item of value) type.write(item, writer);
|
|
14
|
+
},
|
|
15
|
+
...options,
|
|
16
|
+
name: options?.name ?? `${type.name}[${size}]`,
|
|
17
|
+
validate: (value) => {
|
|
18
|
+
options?.validate?.(value);
|
|
19
|
+
if (!value || typeof value !== "object" || !("length" in value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
20
|
+
if (value.length !== size) throw new TypeError(`Expected array of length ${size}, found ${value.length}`);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function option(type) {
|
|
25
|
+
return bcs.enum(`Option<${type.name}>`, {
|
|
26
|
+
None: null,
|
|
27
|
+
Some: type
|
|
28
|
+
}).transform({
|
|
29
|
+
input: (value) => {
|
|
30
|
+
if (value == null) return { None: true };
|
|
31
|
+
return { Some: value };
|
|
32
|
+
},
|
|
33
|
+
output: (value) => {
|
|
34
|
+
if (value.$kind === "Some") return value.Some;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function vector(type, options) {
|
|
40
|
+
return new BcsType({
|
|
41
|
+
read: (reader) => {
|
|
42
|
+
const length = reader.readULEB();
|
|
43
|
+
const result = new Array(length);
|
|
44
|
+
for (let i = 0; i < length; i++) result[i] = type.read(reader);
|
|
45
|
+
return result;
|
|
46
|
+
},
|
|
47
|
+
write: (value, writer) => {
|
|
48
|
+
writer.writeULEB(value.length);
|
|
49
|
+
for (const item of value) type.write(item, writer);
|
|
50
|
+
},
|
|
51
|
+
...options,
|
|
52
|
+
name: options?.name ?? `vector<${type.name}>`,
|
|
53
|
+
validate: (value) => {
|
|
54
|
+
options?.validate?.(value);
|
|
55
|
+
if (!value || typeof value !== "object" || !("length" in value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Compares two byte arrays using lexicographic ordering.
|
|
61
|
+
* This matches Rust's Ord implementation for Vec<u8>/[u8] which is used for BTreeMap key ordering.
|
|
62
|
+
* Comparison is done byte-by-byte first, then by length if all compared bytes are equal.
|
|
63
|
+
*/
|
|
64
|
+
function compareBcsBytes(a, b) {
|
|
65
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++) if (a[i] !== b[i]) return a[i] - b[i];
|
|
66
|
+
return a.length - b.length;
|
|
67
|
+
}
|
|
68
|
+
function map(keyType, valueType) {
|
|
69
|
+
return new BcsType({
|
|
70
|
+
name: `Map<${keyType.name}, ${valueType.name}>`,
|
|
71
|
+
read: (reader) => {
|
|
72
|
+
const length = reader.readULEB();
|
|
73
|
+
const result = /* @__PURE__ */ new Map();
|
|
74
|
+
for (let i = 0; i < length; i++) result.set(keyType.read(reader), valueType.read(reader));
|
|
75
|
+
return result;
|
|
76
|
+
},
|
|
77
|
+
write: (value, writer) => {
|
|
78
|
+
const entries = [...value.entries()].map(([key, val]) => [keyType.serialize(key).toBytes(), val]);
|
|
79
|
+
entries.sort(([a], [b]) => compareBcsBytes(a, b));
|
|
80
|
+
writer.writeULEB(entries.length);
|
|
81
|
+
for (const [keyBytes, val] of entries) {
|
|
82
|
+
writer.writeBytes(keyBytes);
|
|
83
|
+
valueType.write(val, writer);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const bcs = {
|
|
89
|
+
u8(options) {
|
|
90
|
+
return uIntBcsType({
|
|
91
|
+
readMethod: "read8",
|
|
92
|
+
writeMethod: "write8",
|
|
93
|
+
size: 1,
|
|
94
|
+
maxValue: 2 ** 8 - 1,
|
|
95
|
+
...options,
|
|
96
|
+
name: options?.name ?? "u8"
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
u16(options) {
|
|
100
|
+
return uIntBcsType({
|
|
101
|
+
readMethod: "read16",
|
|
102
|
+
writeMethod: "write16",
|
|
103
|
+
size: 2,
|
|
104
|
+
maxValue: 2 ** 16 - 1,
|
|
105
|
+
...options,
|
|
106
|
+
name: options?.name ?? "u16"
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
u32(options) {
|
|
110
|
+
return uIntBcsType({
|
|
111
|
+
readMethod: "read32",
|
|
112
|
+
writeMethod: "write32",
|
|
113
|
+
size: 4,
|
|
114
|
+
maxValue: 2 ** 32 - 1,
|
|
115
|
+
...options,
|
|
116
|
+
name: options?.name ?? "u32"
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
u64(options) {
|
|
120
|
+
return bigUIntBcsType({
|
|
121
|
+
readMethod: "read64",
|
|
122
|
+
writeMethod: "write64",
|
|
123
|
+
size: 8,
|
|
124
|
+
maxValue: 2n ** 64n - 1n,
|
|
125
|
+
...options,
|
|
126
|
+
name: options?.name ?? "u64"
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
u128(options) {
|
|
130
|
+
return bigUIntBcsType({
|
|
131
|
+
readMethod: "read128",
|
|
132
|
+
writeMethod: "write128",
|
|
133
|
+
size: 16,
|
|
134
|
+
maxValue: 2n ** 128n - 1n,
|
|
135
|
+
...options,
|
|
136
|
+
name: options?.name ?? "u128"
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
u256(options) {
|
|
140
|
+
return bigUIntBcsType({
|
|
141
|
+
readMethod: "read256",
|
|
142
|
+
writeMethod: "write256",
|
|
143
|
+
size: 32,
|
|
144
|
+
maxValue: 2n ** 256n - 1n,
|
|
145
|
+
...options,
|
|
146
|
+
name: options?.name ?? "u256"
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
bool(options) {
|
|
150
|
+
return fixedSizeBcsType({
|
|
151
|
+
size: 1,
|
|
152
|
+
read: (reader) => reader.read8() === 1,
|
|
153
|
+
write: (value, writer) => writer.write8(value ? 1 : 0),
|
|
154
|
+
...options,
|
|
155
|
+
name: options?.name ?? "bool",
|
|
156
|
+
validate: (value) => {
|
|
157
|
+
options?.validate?.(value);
|
|
158
|
+
if (typeof value !== "boolean") throw new TypeError(`Expected boolean, found ${typeof value}`);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
},
|
|
162
|
+
uleb128(options) {
|
|
163
|
+
return dynamicSizeBcsType({
|
|
164
|
+
read: (reader) => reader.readULEB(),
|
|
165
|
+
serialize: (value) => {
|
|
166
|
+
return Uint8Array.from(ulebEncode(value));
|
|
167
|
+
},
|
|
168
|
+
...options,
|
|
169
|
+
name: options?.name ?? "uleb128"
|
|
170
|
+
});
|
|
171
|
+
},
|
|
172
|
+
bytes(size, options) {
|
|
173
|
+
return fixedSizeBcsType({
|
|
174
|
+
size,
|
|
175
|
+
read: (reader) => reader.readBytes(size),
|
|
176
|
+
write: (value, writer) => {
|
|
177
|
+
writer.writeBytes(new Uint8Array(value));
|
|
178
|
+
},
|
|
179
|
+
...options,
|
|
180
|
+
name: options?.name ?? `bytes[${size}]`,
|
|
181
|
+
validate: (value) => {
|
|
182
|
+
options?.validate?.(value);
|
|
183
|
+
if (!value || typeof value !== "object" || !("length" in value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
184
|
+
if (value.length !== size) throw new TypeError(`Expected array of length ${size}, found ${value.length}`);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
byteVector(options) {
|
|
189
|
+
return new BcsType({
|
|
190
|
+
read: (reader) => {
|
|
191
|
+
const length = reader.readULEB();
|
|
192
|
+
return reader.readBytes(length);
|
|
193
|
+
},
|
|
194
|
+
write: (value, writer) => {
|
|
195
|
+
const array = new Uint8Array(value);
|
|
196
|
+
writer.writeULEB(array.length);
|
|
197
|
+
writer.writeBytes(array);
|
|
198
|
+
},
|
|
199
|
+
...options,
|
|
200
|
+
name: options?.name ?? "vector<u8>",
|
|
201
|
+
serializedSize: (value) => {
|
|
202
|
+
const length = "length" in value ? value.length : null;
|
|
203
|
+
return length == null ? null : ulebEncode(length).length + length;
|
|
204
|
+
},
|
|
205
|
+
validate: (value) => {
|
|
206
|
+
options?.validate?.(value);
|
|
207
|
+
if (!value || typeof value !== "object" || !("length" in value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
string(options) {
|
|
212
|
+
return stringLikeBcsType({
|
|
213
|
+
toBytes: (value) => new TextEncoder().encode(value),
|
|
214
|
+
fromBytes: (bytes) => new TextDecoder().decode(bytes),
|
|
215
|
+
...options,
|
|
216
|
+
name: options?.name ?? "string"
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
fixedArray,
|
|
220
|
+
option,
|
|
221
|
+
vector,
|
|
222
|
+
tuple(fields, options) {
|
|
223
|
+
return new BcsTuple({
|
|
224
|
+
fields,
|
|
225
|
+
...options
|
|
226
|
+
});
|
|
227
|
+
},
|
|
228
|
+
struct(name, fields, options) {
|
|
229
|
+
return new BcsStruct({
|
|
230
|
+
name,
|
|
231
|
+
fields,
|
|
232
|
+
...options
|
|
233
|
+
});
|
|
234
|
+
},
|
|
235
|
+
enum(name, fields, options) {
|
|
236
|
+
return new BcsEnum({
|
|
237
|
+
name,
|
|
238
|
+
fields,
|
|
239
|
+
...options
|
|
240
|
+
});
|
|
241
|
+
},
|
|
242
|
+
map,
|
|
243
|
+
lazy(cb) {
|
|
244
|
+
return lazyBcsType(cb);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
//#endregion
|
|
249
|
+
export { bcs, compareBcsBytes };
|
|
250
|
+
//# sourceMappingURL=bcs.mjs.map
|
package/dist/bcs.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bcs.mjs","names":[],"sources":["../src/bcs.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { BcsTypeOptions } from './bcs-type.js';\nimport {\n\tBcsEnum,\n\tBcsStruct,\n\tBcsTuple,\n\tBcsType,\n\tbigUIntBcsType,\n\tdynamicSizeBcsType,\n\tfixedSizeBcsType,\n\tlazyBcsType,\n\tstringLikeBcsType,\n\tuIntBcsType,\n} from './bcs-type.js';\nimport type {\n\tEnumInputShape,\n\tEnumOutputShape,\n\tInferBcsInput,\n\tInferBcsType,\n\tJoinString,\n} from './types.js';\nimport { ulebEncode } from './uleb.js';\n\nfunction fixedArray<T extends BcsType<any>, Name extends string = string>(\n\tsize: number,\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>;\nfunction fixedArray<T, Input, Name extends string = string>(\n\tsize: number,\n\ttype: BcsType<T, Input>,\n\toptions?: BcsTypeOptions<T[], Iterable<Input> & { length: number }, Name>,\n): BcsType<T[], Iterable<Input> & { length: number }, Name>;\nfunction fixedArray<T extends BcsType<any>, Name extends string = `${T['name']}[${number}]`>(\n\tsize: number,\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name> {\n\treturn new BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>({\n\t\tread: (reader) => {\n\t\t\tconst result: InferBcsType<T>[] = new Array(size);\n\t\t\tfor (let i = 0; i < size; i++) {\n\t\t\t\tresult[i] = type.read(reader);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\tfor (const item of value) {\n\t\t\t\ttype.write(item, writer);\n\t\t\t}\n\t\t},\n\t\t...options,\n\t\tname: (options?.name ?? `${type.name}[${size}]`) as Name,\n\t\tvalidate: (value) => {\n\t\t\toptions?.validate?.(value);\n\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t}\n\t\t\tif (value.length !== size) {\n\t\t\t\tthrow new TypeError(`Expected array of length ${size}, found ${value.length}`);\n\t\t\t}\n\t\t},\n\t});\n}\n\nfunction option<T extends BcsType<any>>(\n\ttype: T,\n): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`>;\nfunction option<T, Input, Name extends string = string>(\n\ttype: BcsType<T, Input, Name>,\n): BcsType<T | null, Input | null | undefined>;\nfunction option<T extends BcsType<any>>(\n\ttype: T,\n): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`> {\n\treturn bcs\n\t\t.enum(`Option<${type.name}>`, {\n\t\t\tNone: null,\n\t\t\tSome: type,\n\t\t})\n\t\t.transform({\n\t\t\tinput: (value: InferBcsInput<T> | null | undefined) => {\n\t\t\t\tif (value == null) {\n\t\t\t\t\treturn { None: true };\n\t\t\t\t}\n\n\t\t\t\treturn { Some: value };\n\t\t\t},\n\t\t\toutput: (value) => {\n\t\t\t\tif (value.$kind === 'Some') {\n\t\t\t\t\treturn value.Some as InferBcsType<T>;\n\t\t\t\t}\n\n\t\t\t\treturn null;\n\t\t\t},\n\t\t});\n}\n\nfunction vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>;\nfunction vector<T, Input, Name extends string = string>(\n\ttype: BcsType<T, Input, Name>,\n\toptions?: BcsTypeOptions<T[], Iterable<Input> & { length: number }, `vector<${Name}>`>,\n): BcsType<T[], Iterable<Input> & { length: number }, `vector<${Name}>`>;\nfunction vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name> {\n\treturn new BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>({\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst result: InferBcsType<T>[] = new Array(length);\n\t\t\tfor (let i = 0; i < length; i++) {\n\t\t\t\tresult[i] = type.read(reader);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\twriter.writeULEB(value.length);\n\t\t\tfor (const item of value) {\n\t\t\t\ttype.write(item, writer);\n\t\t\t}\n\t\t},\n\t\t...options,\n\t\tname: (options?.name ?? `vector<${type.name}>`) as Name,\n\t\tvalidate: (value) => {\n\t\t\toptions?.validate?.(value);\n\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t}\n\t\t},\n\t});\n}\n\n/**\n * Compares two byte arrays using lexicographic ordering.\n * This matches Rust's Ord implementation for Vec<u8>/[u8] which is used for BTreeMap key ordering.\n * Comparison is done byte-by-byte first, then by length if all compared bytes are equal.\n */\nexport function compareBcsBytes(a: Uint8Array, b: Uint8Array): number {\n\tfor (let i = 0; i < Math.min(a.length, b.length); i++) {\n\t\tif (a[i] !== b[i]) {\n\t\t\treturn a[i] - b[i];\n\t\t}\n\t}\n\n\treturn a.length - b.length;\n}\n\nfunction map<K extends BcsType<any>, V extends BcsType<any>>(\n\tkeyType: K,\n\tvalueType: V,\n): BcsType<\n\tMap<InferBcsType<K>, InferBcsType<V>>,\n\tMap<InferBcsInput<K>, InferBcsInput<V>>,\n\t`Map<${K['name']}, ${V['name']}>`\n>;\nfunction map<K, V, InputK = K, InputV = V>(\n\tkeyType: BcsType<K, InputK>,\n\tvalueType: BcsType<V, InputV>,\n): BcsType<Map<K, V>, Map<InputK, InputV>, `Map<${string}, ${string}>`>;\nfunction map<K extends BcsType<any>, V extends BcsType<any>>(\n\tkeyType: K,\n\tvalueType: V,\n): BcsType<\n\tMap<InferBcsType<K>, InferBcsType<V>>,\n\tMap<InferBcsInput<K>, InferBcsInput<V>>,\n\t`Map<${K['name']}, ${V['name']}>`\n> {\n\treturn new BcsType({\n\t\tname: `Map<${keyType.name}, ${valueType.name}>`,\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst result = new Map<InferBcsType<K>, InferBcsType<V>>();\n\t\t\tfor (let i = 0; i < length; i++) {\n\t\t\t\tresult.set(keyType.read(reader), valueType.read(reader));\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\tconst entries = [...value.entries()].map(\n\t\t\t\t([key, val]) => [keyType.serialize(key).toBytes(), val] as const,\n\t\t\t);\n\t\t\tentries.sort(([a], [b]) => compareBcsBytes(a, b));\n\n\t\t\twriter.writeULEB(entries.length);\n\t\t\tfor (const [keyBytes, val] of entries) {\n\t\t\t\twriter.writeBytes(keyBytes);\n\t\t\t\tvalueType.write(val, writer);\n\t\t\t}\n\t\t},\n\t});\n}\n\nexport const bcs = {\n\t/**\n\t * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.\n\t * @example\n\t * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]\n\t */\n\tu8(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read8',\n\t\t\twriteMethod: 'write8',\n\t\t\tsize: 1,\n\t\t\tmaxValue: 2 ** 8 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u8') as 'u8',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.\n\t * @example\n\t * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]\n\t */\n\tu16(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read16',\n\t\t\twriteMethod: 'write16',\n\t\t\tsize: 2,\n\t\t\tmaxValue: 2 ** 16 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u16') as 'u16',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.\n\t * @example\n\t * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]\n\t */\n\tu32(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read32',\n\t\t\twriteMethod: 'write32',\n\t\t\tsize: 4,\n\t\t\tmaxValue: 2 ** 32 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u32') as 'u32',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.\n\t * @example\n\t * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]\n\t */\n\tu64(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read64',\n\t\t\twriteMethod: 'write64',\n\t\t\tsize: 8,\n\t\t\tmaxValue: 2n ** 64n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u64') as 'u64',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.\n\t * @example\n\t * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]\n\t */\n\tu128(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read128',\n\t\t\twriteMethod: 'write128',\n\t\t\tsize: 16,\n\t\t\tmaxValue: 2n ** 128n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u128') as 'u128',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.\n\t * @example\n\t * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]\n\t */\n\tu256(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read256',\n\t\t\twriteMethod: 'write256',\n\t\t\tsize: 32,\n\t\t\tmaxValue: 2n ** 256n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u256') as 'u256',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write boolean values.\n\t * @example\n\t * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]\n\t */\n\tbool(options?: BcsTypeOptions<boolean>) {\n\t\treturn fixedSizeBcsType({\n\t\t\tsize: 1,\n\t\t\tread: (reader) => reader.read8() === 1,\n\t\t\twrite: (value, writer) => writer.write8(value ? 1 : 0),\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'bool') as 'bool',\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'boolean') {\n\t\t\t\t\tthrow new TypeError(`Expected boolean, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write unsigned LEB encoded integers\n\t * @example\n\t *\n\t */\n\tuleb128(options?: BcsTypeOptions<number>) {\n\t\treturn dynamicSizeBcsType({\n\t\t\tread: (reader) => reader.readULEB(),\n\t\t\tserialize: (value) => {\n\t\t\t\treturn Uint8Array.from(ulebEncode(value));\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'uleb128') as 'uleb128',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a fixed length byte array\n\t * @param size The number of bytes this types represents\n\t * @example\n\t * bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]\n\t */\n\tbytes<T extends number>(size: T, options?: BcsTypeOptions<Uint8Array, Iterable<number>>) {\n\t\treturn fixedSizeBcsType<Uint8Array, Iterable<number>, `bytes[${T}]`>({\n\t\t\tsize,\n\t\t\tread: (reader) => reader.readBytes(size),\n\t\t\twrite: (value, writer) => {\n\t\t\t\twriter.writeBytes(new Uint8Array(value));\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? `bytes[${size}]`) as `bytes[${T}]`,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t\tif (value.length !== size) {\n\t\t\t\t\tthrow new TypeError(`Expected array of length ${size}, found ${value.length}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a variable length byte array\n\t *\n\t * @example\n\t * bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]\n\t */\n\tbyteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>) {\n\t\treturn new BcsType<Uint8Array, Iterable<number>, 'vector<u8>'>({\n\t\t\tread: (reader) => {\n\t\t\t\tconst length = reader.readULEB();\n\n\t\t\t\treturn reader.readBytes(length);\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tconst array = new Uint8Array(value);\n\t\t\t\twriter.writeULEB(array.length);\n\t\t\t\twriter.writeBytes(array);\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'vector<u8>') as 'vector<u8>',\n\t\t\tserializedSize: (value) => {\n\t\t\t\tconst length = 'length' in value ? (value.length as number) : null;\n\t\t\t\treturn length == null ? null : ulebEncode(length).length + length;\n\t\t\t},\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded\n\t * @example\n\t * bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]\n\t */\n\tstring(options?: BcsTypeOptions<string>) {\n\t\treturn stringLikeBcsType({\n\t\t\ttoBytes: (value) => new TextEncoder().encode(value),\n\t\t\tfromBytes: (bytes) => new TextDecoder().decode(bytes),\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'string') as 'string',\n\t\t});\n\t},\n\t/**\n\t * Creates a BcsType that represents a fixed length array of a given type\n\t * @param size The number of elements in the array\n\t * @param type The BcsType of each element in the array\n\t * @example\n\t * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]\n\t */\n\tfixedArray,\n\n\t/**\n\t * Creates a BcsType representing an optional value\n\t * @param type The BcsType of the optional value\n\t * @example\n\t * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]\n\t * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]\n\t */\n\toption,\n\n\t/**\n\t * Creates a BcsType representing a variable length vector of a given type\n\t * @param type The BcsType of each element in the vector\n\t *\n\t * @example\n\t * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]\n\t */\n\tvector,\n\n\t/**\n\t * Creates a BcsType representing a tuple of a given set of types\n\t * @param types The BcsTypes for each element in the tuple\n\t *\n\t * @example\n\t * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])\n\t * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]\n\t */\n\ttuple<\n\t\tconst T extends readonly BcsType<any, any>[],\n\t\tconst Name extends string =\n\t\t\t`(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`,\n\t>(\n\t\tfields: T,\n\t\toptions?: BcsTypeOptions<\n\t\t\t{\n\t\t\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t\t\t},\n\t\t\t{\n\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t\t\t},\n\t\t\tName\n\t\t>,\n\t) {\n\t\treturn new BcsTuple<T, Name>({\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a struct of a given set of fields\n\t * @param name The name of the struct\n\t * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized\n\t *\n\t * @example\n\t * const struct = bcs.struct('MyStruct', {\n\t * a: bcs.u8(),\n\t * b: bcs.string(),\n\t * })\n\t * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]\n\t */\n\tstruct<T extends Record<string, BcsType<any>>, const Name extends string = string>(\n\t\tname: Name,\n\t\tfields: T,\n\t\toptions?: Omit<\n\t\t\tBcsTypeOptions<\n\t\t\t\t{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t\t\t\t}\n\t\t\t>,\n\t\t\t'name'\n\t\t>,\n\t) {\n\t\treturn new BcsStruct<T>({\n\t\t\tname,\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing an enum of a given set of options\n\t * @param name The name of the enum\n\t * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.\n\t * null can be used to represent a variant with no data.\n\t *\n\t * @example\n\t * const enum = bcs.enum('MyEnum', {\n\t * A: bcs.u8(),\n\t * B: bcs.string(),\n\t * C: null,\n\t * })\n\t * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]\n\t * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]\n\t * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]\n\t */\n\tenum<T extends Record<string, BcsType<any> | null>, const Name extends string = string>(\n\t\tname: Name,\n\t\tfields: T,\n\t\toptions?: Omit<\n\t\t\tBcsTypeOptions<\n\t\t\t\tEnumOutputShape<{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;\n\t\t\t\t}>,\n\t\t\t\tEnumInputShape<{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t\t\t\t}>,\n\t\t\t\tName\n\t\t\t>,\n\t\t\t'name'\n\t\t>,\n\t) {\n\t\treturn new BcsEnum<T, Name>({\n\t\t\tname,\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a map of a given key and value type\n\t * @param keyType The BcsType of the key\n\t * @param valueType The BcsType of the value\n\t * @example\n\t * const map = bcs.map(bcs.u8(), bcs.string())\n\t * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]\n\t */\n\tmap,\n\n\t/**\n\t * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.\n\t * @param cb A callback that returns the BcsType\n\t */\n\tlazy<T extends BcsType<any>>(cb: () => T): T {\n\t\treturn lazyBcsType(cb) as T;\n\t},\n};\n"],"mappings":";;;;AAuCA,SAAS,WACR,MACA,MACA,SAKoF;AACpF,QAAO,IAAI,QAAkF;EAC5F,OAAO,WAAW;GACjB,MAAM,SAA4B,IAAI,MAAM,KAAK;A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SAWC;AACD,SAAO,IAAI,UAAa;GACvB;GACA;GACA,GAAG;GACH,CAAC;;CAmBH,KACC,MACA,QACA,SAYC;AACD,SAAO,IAAI,QAAiB;GAC3B;GACA;GACA,GAAG;GACH,CAAC;;CAWH;CAMA,KAA6B,IAAgB;AAC5C,SAAO,YAAY,GAAG;;CAEvB"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BcsReader } from "./reader.mjs";
|
|
2
|
+
import { Encoding, EnumInputShape, EnumOutputShape, EnumOutputShapeWithKeys, InferBcsInput, InferBcsType, JoinString } from "./types.mjs";
|
|
3
|
+
import { BcsWriter, BcsWriterOptions } from "./writer.mjs";
|
|
4
|
+
import { BcsEnum, BcsStruct, BcsTuple, BcsType, BcsTypeOptions, SerializedBcs, isSerializedBcs } from "./bcs-type.mjs";
|
|
5
|
+
import { bcs, compareBcsBytes } from "./bcs.mjs";
|
|
6
|
+
import { decodeStr, encodeStr, splitGenericParameters } from "./utils.mjs";
|
|
7
|
+
import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@haneullabs/utils";
|
|
8
|
+
export { BcsEnum, BcsReader, BcsStruct, BcsTuple, BcsType, type BcsTypeOptions, BcsWriter, type BcsWriterOptions, type Encoding, type EnumInputShape, type EnumOutputShape, type EnumOutputShapeWithKeys, type InferBcsInput, type InferBcsType, type JoinString, SerializedBcs, bcs, compareBcsBytes, decodeStr, encodeStr, fromBase58, fromBase64, fromHex, isSerializedBcs, splitGenericParameters, toBase58, toBase64, toHex };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BcsReader } from "./reader.mjs";
|
|
2
|
+
import { decodeStr, encodeStr, splitGenericParameters } from "./utils.mjs";
|
|
3
|
+
import { BcsWriter } from "./writer.mjs";
|
|
4
|
+
import { BcsEnum, BcsStruct, BcsTuple, BcsType, SerializedBcs, isSerializedBcs } from "./bcs-type.mjs";
|
|
5
|
+
import { bcs, compareBcsBytes } from "./bcs.mjs";
|
|
6
|
+
import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@haneullabs/utils";
|
|
7
|
+
|
|
8
|
+
export { BcsEnum, BcsReader, BcsStruct, BcsTuple, BcsType, BcsWriter, SerializedBcs, bcs, compareBcsBytes, decodeStr, encodeStr, fromBase58, fromBase64, fromHex, isSerializedBcs, splitGenericParameters, toBase58, toBase64, toHex };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//#region src/reader.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Class used for reading BCS data chunk by chunk. Meant to be used
|
|
4
|
+
* by some wrapper, which will make sure that data is valid and is
|
|
5
|
+
* matching the desired format.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // data for this example is:
|
|
9
|
+
* // { a: u8, b: u32, c: bool, d: u64 }
|
|
10
|
+
*
|
|
11
|
+
* let reader = new BcsReader("647f1a060001ffffe7890423c78a050102030405");
|
|
12
|
+
* let field1 = reader.read8();
|
|
13
|
+
* let field2 = reader.read32();
|
|
14
|
+
* let field3 = reader.read8() === '1'; // bool
|
|
15
|
+
* let field4 = reader.read64();
|
|
16
|
+
* // ....
|
|
17
|
+
*
|
|
18
|
+
* Reading vectors is another deal in bcs. To read a vector, you first need to read
|
|
19
|
+
* its length using {@link readULEB}. Here's an example:
|
|
20
|
+
* @example
|
|
21
|
+
* // data encoded: { field: [1, 2, 3, 4, 5] }
|
|
22
|
+
* let reader = new BcsReader("050102030405");
|
|
23
|
+
* let vec_length = reader.readULEB();
|
|
24
|
+
* let elements = [];
|
|
25
|
+
* for (let i = 0; i < vec_length; i++) {
|
|
26
|
+
* elements.push(reader.read8());
|
|
27
|
+
* }
|
|
28
|
+
* console.log(elements); // [1,2,3,4,5]
|
|
29
|
+
*
|
|
30
|
+
* @param {String} data HEX-encoded data (serialized BCS)
|
|
31
|
+
*/
|
|
32
|
+
declare class BcsReader {
|
|
33
|
+
private dataView;
|
|
34
|
+
private bytePosition;
|
|
35
|
+
/**
|
|
36
|
+
* @param {Uint8Array} data Data to use as a buffer.
|
|
37
|
+
*/
|
|
38
|
+
constructor(data: Uint8Array);
|
|
39
|
+
/**
|
|
40
|
+
* Shift current cursor position by `bytes`.
|
|
41
|
+
*
|
|
42
|
+
* @param {Number} bytes Number of bytes to
|
|
43
|
+
* @returns {this} Self for possible chaining.
|
|
44
|
+
*/
|
|
45
|
+
shift(bytes: number): this;
|
|
46
|
+
/**
|
|
47
|
+
* Read U8 value from the buffer and shift cursor by 1.
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
read8(): number;
|
|
51
|
+
/**
|
|
52
|
+
* Read U16 value from the buffer and shift cursor by 2.
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
read16(): number;
|
|
56
|
+
/**
|
|
57
|
+
* Read U32 value from the buffer and shift cursor by 4.
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
read32(): number;
|
|
61
|
+
/**
|
|
62
|
+
* Read U64 value from the buffer and shift cursor by 8.
|
|
63
|
+
* @returns
|
|
64
|
+
*/
|
|
65
|
+
read64(): string;
|
|
66
|
+
/**
|
|
67
|
+
* Read U128 value from the buffer and shift cursor by 16.
|
|
68
|
+
*/
|
|
69
|
+
read128(): string;
|
|
70
|
+
/**
|
|
71
|
+
* Read U128 value from the buffer and shift cursor by 32.
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
read256(): string;
|
|
75
|
+
/**
|
|
76
|
+
* Read `num` number of bytes from the buffer and shift cursor by `num`.
|
|
77
|
+
* @param num Number of bytes to read.
|
|
78
|
+
*/
|
|
79
|
+
readBytes(num: number): Uint8Array;
|
|
80
|
+
/**
|
|
81
|
+
* Read ULEB value - an integer of varying size. Used for enum indexes and
|
|
82
|
+
* vector lengths.
|
|
83
|
+
* @returns {Number} The ULEB value.
|
|
84
|
+
*/
|
|
85
|
+
readULEB(): number;
|
|
86
|
+
/**
|
|
87
|
+
* Read a BCS vector: read a length and then apply function `cb` X times
|
|
88
|
+
* where X is the length of the vector, defined as ULEB in BCS bytes.
|
|
89
|
+
* @param cb Callback to process elements of vector.
|
|
90
|
+
* @returns {Array<Any>} Array of the resulting values, returned by callback.
|
|
91
|
+
*/
|
|
92
|
+
readVec(cb: (reader: BcsReader, i: number, length: number) => any): any[];
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
export { BcsReader };
|
|
96
|
+
//# sourceMappingURL=reader.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reader.d.mts","names":[],"sources":["../src/reader.ts"],"mappings":";;AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,SAAA;EAAA,QACJ,QAAA;EAAA,QACA,YAAA;EA8GyD;;;cAzGrD,IAAA,EAAM,UAAA;;;;;;;EASlB,KAAA,CAAM,KAAA;;;;;EAQN,KAAA,CAAA;;;;;EASA,MAAA,CAAA;;;;;EASA,MAAA,CAAA;;;;;EASA,MAAA,CAAA;;;;EAWA,OAAA,CAAA;;;;;EAWA,OAAA,CAAA;;;;;EAWA,SAAA,CAAU,GAAA,WAAc,UAAA;;;;;;EAaxB,QAAA,CAAA;;;;;;;EAeA,OAAA,CAAQ,EAAA,GAAK,MAAA,EAAQ,SAAA,EAAW,CAAA,UAAW,MAAA;AAAA"}
|