@haneullabs/bcs 0.1.0
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 +388 -0
- package/README.md +358 -0
- package/dist/cjs/bcs-type.d.ts +127 -0
- package/dist/cjs/bcs-type.js +386 -0
- package/dist/cjs/bcs-type.js.map +7 -0
- package/dist/cjs/bcs.d.ts +175 -0
- package/dist/cjs/bcs.js +406 -0
- package/dist/cjs/bcs.js.map +7 -0
- package/dist/cjs/index.d.ts +22 -0
- package/dist/cjs/index.js +59 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/reader.d.ts +92 -0
- package/dist/cjs/reader.js +136 -0
- package/dist/cjs/reader.js.map +7 -0
- package/dist/cjs/types.d.ts +28 -0
- package/dist/cjs/types.js +17 -0
- package/dist/cjs/types.js.map +7 -0
- package/dist/cjs/uleb.d.ts +5 -0
- package/dist/cjs/uleb.js +66 -0
- package/dist/cjs/uleb.js.map +7 -0
- package/dist/cjs/utils.d.ts +18 -0
- package/dist/cjs/utils.js +74 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/cjs/writer.d.ts +117 -0
- package/dist/cjs/writer.js +196 -0
- package/dist/cjs/writer.js.map +7 -0
- package/dist/esm/bcs-type.d.ts +127 -0
- package/dist/esm/bcs-type.js +366 -0
- package/dist/esm/bcs-type.js.map +7 -0
- package/dist/esm/bcs.d.ts +175 -0
- package/dist/esm/bcs.js +397 -0
- package/dist/esm/bcs.js.map +7 -0
- package/dist/esm/index.d.ts +22 -0
- package/dist/esm/index.js +46 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/package.json +5 -0
- package/dist/esm/reader.d.ts +92 -0
- package/dist/esm/reader.js +116 -0
- package/dist/esm/reader.js.map +7 -0
- package/dist/esm/types.d.ts +28 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +7 -0
- package/dist/esm/uleb.d.ts +5 -0
- package/dist/esm/uleb.js +46 -0
- package/dist/esm/uleb.js.map +7 -0
- package/dist/esm/utils.d.ts +18 -0
- package/dist/esm/utils.js +54 -0
- package/dist/esm/utils.js.map +7 -0
- package/dist/esm/writer.d.ts +117 -0
- package/dist/esm/writer.js +176 -0
- package/dist/esm/writer.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +73 -0
- package/src/bcs-type.ts +531 -0
- package/src/bcs.ts +543 -0
- package/src/index.ts +82 -0
- package/src/reader.ts +156 -0
- package/src/types.ts +52 -0
- package/src/uleb.ts +61 -0
- package/src/utils.ts +75 -0
- package/src/writer.ts +222 -0
package/dist/cjs/bcs.js
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var bcs_exports = {};
|
|
20
|
+
__export(bcs_exports, {
|
|
21
|
+
bcs: () => bcs
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(bcs_exports);
|
|
24
|
+
var import_bcs_type = require("./bcs-type.js");
|
|
25
|
+
var import_uleb = require("./uleb.js");
|
|
26
|
+
function fixedArray(size, type, options) {
|
|
27
|
+
return new import_bcs_type.BcsType({
|
|
28
|
+
read: (reader) => {
|
|
29
|
+
const result = new Array(size);
|
|
30
|
+
for (let i = 0; i < size; i++) {
|
|
31
|
+
result[i] = type.read(reader);
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
},
|
|
35
|
+
write: (value, writer) => {
|
|
36
|
+
for (const item of value) {
|
|
37
|
+
type.write(item, writer);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
...options,
|
|
41
|
+
name: options?.name ?? `${type.name}[${size}]`,
|
|
42
|
+
validate: (value) => {
|
|
43
|
+
options?.validate?.(value);
|
|
44
|
+
if (!value || typeof value !== "object" || !("length" in value)) {
|
|
45
|
+
throw new TypeError(`Expected array, found ${typeof value}`);
|
|
46
|
+
}
|
|
47
|
+
if (value.length !== size) {
|
|
48
|
+
throw new TypeError(`Expected array of length ${size}, found ${value.length}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function option(type) {
|
|
54
|
+
return bcs.enum(`Option<${type.name}>`, {
|
|
55
|
+
None: null,
|
|
56
|
+
Some: type
|
|
57
|
+
}).transform({
|
|
58
|
+
input: (value) => {
|
|
59
|
+
if (value == null) {
|
|
60
|
+
return { None: true };
|
|
61
|
+
}
|
|
62
|
+
return { Some: value };
|
|
63
|
+
},
|
|
64
|
+
output: (value) => {
|
|
65
|
+
if (value.$kind === "Some") {
|
|
66
|
+
return value.Some;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function vector(type, options) {
|
|
73
|
+
return new import_bcs_type.BcsType({
|
|
74
|
+
read: (reader) => {
|
|
75
|
+
const length = reader.readULEB();
|
|
76
|
+
const result = new Array(length);
|
|
77
|
+
for (let i = 0; i < length; i++) {
|
|
78
|
+
result[i] = type.read(reader);
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
},
|
|
82
|
+
write: (value, writer) => {
|
|
83
|
+
writer.writeULEB(value.length);
|
|
84
|
+
for (const item of value) {
|
|
85
|
+
type.write(item, writer);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
...options,
|
|
89
|
+
name: options?.name ?? `vector<${type.name}>`,
|
|
90
|
+
validate: (value) => {
|
|
91
|
+
options?.validate?.(value);
|
|
92
|
+
if (!value || typeof value !== "object" || !("length" in value)) {
|
|
93
|
+
throw new TypeError(`Expected array, found ${typeof value}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
function map(keyType, valueType) {
|
|
99
|
+
return bcs.vector(bcs.tuple([keyType, valueType])).transform({
|
|
100
|
+
name: `Map<${keyType.name}, ${valueType.name}>`,
|
|
101
|
+
input: (value) => {
|
|
102
|
+
return [...value.entries()];
|
|
103
|
+
},
|
|
104
|
+
output: (value) => {
|
|
105
|
+
const result = /* @__PURE__ */ new Map();
|
|
106
|
+
for (const [key, val] of value) {
|
|
107
|
+
result.set(key, val);
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
const bcs = {
|
|
114
|
+
/**
|
|
115
|
+
* Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
|
|
116
|
+
* @example
|
|
117
|
+
* bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
|
|
118
|
+
*/
|
|
119
|
+
u8(options) {
|
|
120
|
+
return (0, import_bcs_type.uIntBcsType)({
|
|
121
|
+
readMethod: "read8",
|
|
122
|
+
writeMethod: "write8",
|
|
123
|
+
size: 1,
|
|
124
|
+
maxValue: 2 ** 8 - 1,
|
|
125
|
+
...options,
|
|
126
|
+
name: options?.name ?? "u8"
|
|
127
|
+
});
|
|
128
|
+
},
|
|
129
|
+
/**
|
|
130
|
+
* Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
|
|
131
|
+
* @example
|
|
132
|
+
* bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
|
|
133
|
+
*/
|
|
134
|
+
u16(options) {
|
|
135
|
+
return (0, import_bcs_type.uIntBcsType)({
|
|
136
|
+
readMethod: "read16",
|
|
137
|
+
writeMethod: "write16",
|
|
138
|
+
size: 2,
|
|
139
|
+
maxValue: 2 ** 16 - 1,
|
|
140
|
+
...options,
|
|
141
|
+
name: options?.name ?? "u16"
|
|
142
|
+
});
|
|
143
|
+
},
|
|
144
|
+
/**
|
|
145
|
+
* Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
|
|
146
|
+
* @example
|
|
147
|
+
* bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
|
|
148
|
+
*/
|
|
149
|
+
u32(options) {
|
|
150
|
+
return (0, import_bcs_type.uIntBcsType)({
|
|
151
|
+
readMethod: "read32",
|
|
152
|
+
writeMethod: "write32",
|
|
153
|
+
size: 4,
|
|
154
|
+
maxValue: 2 ** 32 - 1,
|
|
155
|
+
...options,
|
|
156
|
+
name: options?.name ?? "u32"
|
|
157
|
+
});
|
|
158
|
+
},
|
|
159
|
+
/**
|
|
160
|
+
* Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
|
|
161
|
+
* @example
|
|
162
|
+
* bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
|
|
163
|
+
*/
|
|
164
|
+
u64(options) {
|
|
165
|
+
return (0, import_bcs_type.bigUIntBcsType)({
|
|
166
|
+
readMethod: "read64",
|
|
167
|
+
writeMethod: "write64",
|
|
168
|
+
size: 8,
|
|
169
|
+
maxValue: 2n ** 64n - 1n,
|
|
170
|
+
...options,
|
|
171
|
+
name: options?.name ?? "u64"
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
/**
|
|
175
|
+
* Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
|
|
176
|
+
* @example
|
|
177
|
+
* bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
|
|
178
|
+
*/
|
|
179
|
+
u128(options) {
|
|
180
|
+
return (0, import_bcs_type.bigUIntBcsType)({
|
|
181
|
+
readMethod: "read128",
|
|
182
|
+
writeMethod: "write128",
|
|
183
|
+
size: 16,
|
|
184
|
+
maxValue: 2n ** 128n - 1n,
|
|
185
|
+
...options,
|
|
186
|
+
name: options?.name ?? "u128"
|
|
187
|
+
});
|
|
188
|
+
},
|
|
189
|
+
/**
|
|
190
|
+
* Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
|
|
191
|
+
* @example
|
|
192
|
+
* bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
|
|
193
|
+
*/
|
|
194
|
+
u256(options) {
|
|
195
|
+
return (0, import_bcs_type.bigUIntBcsType)({
|
|
196
|
+
readMethod: "read256",
|
|
197
|
+
writeMethod: "write256",
|
|
198
|
+
size: 32,
|
|
199
|
+
maxValue: 2n ** 256n - 1n,
|
|
200
|
+
...options,
|
|
201
|
+
name: options?.name ?? "u256"
|
|
202
|
+
});
|
|
203
|
+
},
|
|
204
|
+
/**
|
|
205
|
+
* Creates a BcsType that can be used to read and write boolean values.
|
|
206
|
+
* @example
|
|
207
|
+
* bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
|
|
208
|
+
*/
|
|
209
|
+
bool(options) {
|
|
210
|
+
return (0, import_bcs_type.fixedSizeBcsType)({
|
|
211
|
+
size: 1,
|
|
212
|
+
read: (reader) => reader.read8() === 1,
|
|
213
|
+
write: (value, writer) => writer.write8(value ? 1 : 0),
|
|
214
|
+
...options,
|
|
215
|
+
name: options?.name ?? "bool",
|
|
216
|
+
validate: (value) => {
|
|
217
|
+
options?.validate?.(value);
|
|
218
|
+
if (typeof value !== "boolean") {
|
|
219
|
+
throw new TypeError(`Expected boolean, found ${typeof value}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
/**
|
|
225
|
+
* Creates a BcsType that can be used to read and write unsigned LEB encoded integers
|
|
226
|
+
* @example
|
|
227
|
+
*
|
|
228
|
+
*/
|
|
229
|
+
uleb128(options) {
|
|
230
|
+
return (0, import_bcs_type.dynamicSizeBcsType)({
|
|
231
|
+
read: (reader) => reader.readULEB(),
|
|
232
|
+
serialize: (value) => {
|
|
233
|
+
return Uint8Array.from((0, import_uleb.ulebEncode)(value));
|
|
234
|
+
},
|
|
235
|
+
...options,
|
|
236
|
+
name: options?.name ?? "uleb128"
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
/**
|
|
240
|
+
* Creates a BcsType representing a fixed length byte array
|
|
241
|
+
* @param size The number of bytes this types represents
|
|
242
|
+
* @example
|
|
243
|
+
* bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
|
|
244
|
+
*/
|
|
245
|
+
bytes(size, options) {
|
|
246
|
+
return (0, import_bcs_type.fixedSizeBcsType)({
|
|
247
|
+
size,
|
|
248
|
+
read: (reader) => reader.readBytes(size),
|
|
249
|
+
write: (value, writer) => {
|
|
250
|
+
writer.writeBytes(new Uint8Array(value));
|
|
251
|
+
},
|
|
252
|
+
...options,
|
|
253
|
+
name: options?.name ?? `bytes[${size}]`,
|
|
254
|
+
validate: (value) => {
|
|
255
|
+
options?.validate?.(value);
|
|
256
|
+
if (!value || typeof value !== "object" || !("length" in value)) {
|
|
257
|
+
throw new TypeError(`Expected array, found ${typeof value}`);
|
|
258
|
+
}
|
|
259
|
+
if (value.length !== size) {
|
|
260
|
+
throw new TypeError(`Expected array of length ${size}, found ${value.length}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
},
|
|
265
|
+
/**
|
|
266
|
+
* Creates a BcsType representing a variable length byte array
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
|
|
270
|
+
*/
|
|
271
|
+
byteVector(options) {
|
|
272
|
+
return new import_bcs_type.BcsType({
|
|
273
|
+
read: (reader) => {
|
|
274
|
+
const length = reader.readULEB();
|
|
275
|
+
return reader.readBytes(length);
|
|
276
|
+
},
|
|
277
|
+
write: (value, writer) => {
|
|
278
|
+
const array = new Uint8Array(value);
|
|
279
|
+
writer.writeULEB(array.length);
|
|
280
|
+
writer.writeBytes(array);
|
|
281
|
+
},
|
|
282
|
+
...options,
|
|
283
|
+
name: options?.name ?? "vector<u8>",
|
|
284
|
+
serializedSize: (value) => {
|
|
285
|
+
const length = "length" in value ? value.length : null;
|
|
286
|
+
return length == null ? null : (0, import_uleb.ulebEncode)(length).length + length;
|
|
287
|
+
},
|
|
288
|
+
validate: (value) => {
|
|
289
|
+
options?.validate?.(value);
|
|
290
|
+
if (!value || typeof value !== "object" || !("length" in value)) {
|
|
291
|
+
throw new TypeError(`Expected array, found ${typeof value}`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
},
|
|
296
|
+
/**
|
|
297
|
+
* Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
|
|
298
|
+
* @example
|
|
299
|
+
* bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
|
|
300
|
+
*/
|
|
301
|
+
string(options) {
|
|
302
|
+
return (0, import_bcs_type.stringLikeBcsType)({
|
|
303
|
+
toBytes: (value) => new TextEncoder().encode(value),
|
|
304
|
+
fromBytes: (bytes) => new TextDecoder().decode(bytes),
|
|
305
|
+
...options,
|
|
306
|
+
name: options?.name ?? "string"
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
/**
|
|
310
|
+
* Creates a BcsType that represents a fixed length array of a given type
|
|
311
|
+
* @param size The number of elements in the array
|
|
312
|
+
* @param type The BcsType of each element in the array
|
|
313
|
+
* @example
|
|
314
|
+
* bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
|
|
315
|
+
*/
|
|
316
|
+
fixedArray,
|
|
317
|
+
/**
|
|
318
|
+
* Creates a BcsType representing an optional value
|
|
319
|
+
* @param type The BcsType of the optional value
|
|
320
|
+
* @example
|
|
321
|
+
* bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
|
|
322
|
+
* bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
|
|
323
|
+
*/
|
|
324
|
+
option,
|
|
325
|
+
/**
|
|
326
|
+
* Creates a BcsType representing a variable length vector of a given type
|
|
327
|
+
* @param type The BcsType of each element in the vector
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
|
|
331
|
+
*/
|
|
332
|
+
vector,
|
|
333
|
+
/**
|
|
334
|
+
* Creates a BcsType representing a tuple of a given set of types
|
|
335
|
+
* @param types The BcsTypes for each element in the tuple
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
|
|
339
|
+
* tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
|
|
340
|
+
*/
|
|
341
|
+
tuple(fields, options) {
|
|
342
|
+
return new import_bcs_type.BcsTuple({
|
|
343
|
+
fields,
|
|
344
|
+
...options
|
|
345
|
+
});
|
|
346
|
+
},
|
|
347
|
+
/**
|
|
348
|
+
* Creates a BcsType representing a struct of a given set of fields
|
|
349
|
+
* @param name The name of the struct
|
|
350
|
+
* @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized
|
|
351
|
+
*
|
|
352
|
+
* @example
|
|
353
|
+
* const struct = bcs.struct('MyStruct', {
|
|
354
|
+
* a: bcs.u8(),
|
|
355
|
+
* b: bcs.string(),
|
|
356
|
+
* })
|
|
357
|
+
* struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
|
|
358
|
+
*/
|
|
359
|
+
struct(name, fields, options) {
|
|
360
|
+
return new import_bcs_type.BcsStruct({
|
|
361
|
+
name,
|
|
362
|
+
fields,
|
|
363
|
+
...options
|
|
364
|
+
});
|
|
365
|
+
},
|
|
366
|
+
/**
|
|
367
|
+
* Creates a BcsType representing an enum of a given set of options
|
|
368
|
+
* @param name The name of the enum
|
|
369
|
+
* @param values The values of the enum. The order of the values affects how data is serialized and deserialized.
|
|
370
|
+
* null can be used to represent a variant with no data.
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* const enum = bcs.enum('MyEnum', {
|
|
374
|
+
* A: bcs.u8(),
|
|
375
|
+
* B: bcs.string(),
|
|
376
|
+
* C: null,
|
|
377
|
+
* })
|
|
378
|
+
* enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
|
|
379
|
+
* enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
|
|
380
|
+
* enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
|
|
381
|
+
*/
|
|
382
|
+
enum(name, fields, options) {
|
|
383
|
+
return new import_bcs_type.BcsEnum({
|
|
384
|
+
name,
|
|
385
|
+
fields,
|
|
386
|
+
...options
|
|
387
|
+
});
|
|
388
|
+
},
|
|
389
|
+
/**
|
|
390
|
+
* Creates a BcsType representing a map of a given key and value type
|
|
391
|
+
* @param keyType The BcsType of the key
|
|
392
|
+
* @param valueType The BcsType of the value
|
|
393
|
+
* @example
|
|
394
|
+
* const map = bcs.map(bcs.u8(), bcs.string())
|
|
395
|
+
* map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
|
|
396
|
+
*/
|
|
397
|
+
map,
|
|
398
|
+
/**
|
|
399
|
+
* Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
|
|
400
|
+
* @param cb A callback that returns the BcsType
|
|
401
|
+
*/
|
|
402
|
+
lazy(cb) {
|
|
403
|
+
return (0, import_bcs_type.lazyBcsType)(cb);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
//# sourceMappingURL=bcs.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/bcs.ts"],
|
|
4
|
+
"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\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 bcs.vector(bcs.tuple([keyType, valueType])).transform({\n\t\tname: `Map<${keyType.name}, ${valueType.name}>`,\n\t\tinput: (value: Map<InferBcsInput<K>, InferBcsInput<V>>) => {\n\t\t\treturn [...value.entries()];\n\t\t},\n\t\toutput: (value) => {\n\t\t\tconst result = new Map<InferBcsType<K>, InferBcsType<V>>();\n\t\t\tfor (const [key, val] of value) {\n\t\t\t\tresult.set(key, val);\n\t\t\t}\n\t\t\treturn result;\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"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,sBAWO;AAQP,kBAA2B;AAgB3B,SAAS,WACR,MACA,MACA,SAKoF;AACpF,SAAO,IAAI,wBAAkF;AAAA,IAC5F,MAAM,CAAC,WAAW;AACjB,YAAM,SAA4B,IAAI,MAAM,IAAI;AAChD,eAAS,IAAI,GAAG,IAAI,MAAM,KAAK;AAC9B,eAAO,CAAC,IAAI,KAAK,KAAK,MAAM;AAAA,MAC7B;AACA,aAAO;AAAA,IACR;AAAA,IACA,OAAO,CAAC,OAAO,WAAW;AACzB,iBAAW,QAAQ,OAAO;AACzB,aAAK,MAAM,MAAM,MAAM;AAAA,MACxB;AAAA,IACD;AAAA,IACA,GAAG;AAAA,IACH,MAAO,SAAS,QAAQ,GAAG,KAAK,IAAI,IAAI,IAAI;A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,CAAC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBA,KACC,MACA,QACA,SAYC;AACD,WAAO,IAAI,wBAAiB;AAAA,MAC3B;AAAA,MACA;AAAA,MACA,GAAG;AAAA,IACJ,CAAC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KAA6B,IAAgB;AAC5C,eAAO,6BAAY,EAAE;AAAA,EACtB;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { toBase58, fromBase58, toBase64, fromBase64, toHex, fromHex } from '@haneullabs/utils';
|
|
2
|
+
import type { BcsTypeOptions } from './bcs-type.js';
|
|
3
|
+
import { BcsType, BcsStruct, BcsEnum, BcsTuple, isSerializedBcs, SerializedBcs } from './bcs-type.js';
|
|
4
|
+
import { bcs } from './bcs.js';
|
|
5
|
+
import { BcsReader } from './reader.js';
|
|
6
|
+
import type { EnumInputShape, EnumOutputShape, EnumOutputShapeWithKeys, InferBcsInput, InferBcsType } from './types.js';
|
|
7
|
+
import { decodeStr, encodeStr, splitGenericParameters } from './utils.js';
|
|
8
|
+
import type { BcsWriterOptions } from './writer.js';
|
|
9
|
+
import { BcsWriter } from './writer.js';
|
|
10
|
+
export { bcs, BcsType, BcsStruct, BcsEnum, BcsTuple, type BcsTypeOptions, SerializedBcs, isSerializedBcs, toBase58, fromBase58, toBase64, fromBase64, toHex, fromHex, encodeStr, decodeStr, splitGenericParameters, BcsReader, BcsWriter, type BcsWriterOptions, type InferBcsInput, type InferBcsType, type EnumOutputShape, type EnumInputShape, type EnumOutputShapeWithKeys, };
|
|
11
|
+
/** @deprecated use toBase58 instead */
|
|
12
|
+
export declare const toB58: (buffer: Uint8Array) => string;
|
|
13
|
+
/** @deprecated use fromBase58 instead */
|
|
14
|
+
export declare const fromB58: (str: string) => Uint8Array<ArrayBuffer>;
|
|
15
|
+
/** @deprecated use toBase64 instead */
|
|
16
|
+
export declare const toB64: typeof toBase64;
|
|
17
|
+
/** @deprecated use fromBase64 instead */
|
|
18
|
+
export declare const fromB64: typeof fromBase64;
|
|
19
|
+
/** @deprecated use toHex instead */
|
|
20
|
+
export declare const toHEX: typeof toHex;
|
|
21
|
+
/** @deprecated use fromHex instead */
|
|
22
|
+
export declare const fromHEX: typeof fromHex;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var index_exports = {};
|
|
20
|
+
__export(index_exports, {
|
|
21
|
+
BcsEnum: () => import_bcs_type.BcsEnum,
|
|
22
|
+
BcsReader: () => import_reader.BcsReader,
|
|
23
|
+
BcsStruct: () => import_bcs_type.BcsStruct,
|
|
24
|
+
BcsTuple: () => import_bcs_type.BcsTuple,
|
|
25
|
+
BcsType: () => import_bcs_type.BcsType,
|
|
26
|
+
BcsWriter: () => import_writer.BcsWriter,
|
|
27
|
+
SerializedBcs: () => import_bcs_type.SerializedBcs,
|
|
28
|
+
bcs: () => import_bcs.bcs,
|
|
29
|
+
decodeStr: () => import_utils2.decodeStr,
|
|
30
|
+
encodeStr: () => import_utils2.encodeStr,
|
|
31
|
+
fromB58: () => fromB58,
|
|
32
|
+
fromB64: () => fromB64,
|
|
33
|
+
fromBase58: () => import_utils.fromBase58,
|
|
34
|
+
fromBase64: () => import_utils.fromBase64,
|
|
35
|
+
fromHEX: () => fromHEX,
|
|
36
|
+
fromHex: () => import_utils.fromHex,
|
|
37
|
+
isSerializedBcs: () => import_bcs_type.isSerializedBcs,
|
|
38
|
+
splitGenericParameters: () => import_utils2.splitGenericParameters,
|
|
39
|
+
toB58: () => toB58,
|
|
40
|
+
toB64: () => toB64,
|
|
41
|
+
toBase58: () => import_utils.toBase58,
|
|
42
|
+
toBase64: () => import_utils.toBase64,
|
|
43
|
+
toHEX: () => toHEX,
|
|
44
|
+
toHex: () => import_utils.toHex
|
|
45
|
+
});
|
|
46
|
+
module.exports = __toCommonJS(index_exports);
|
|
47
|
+
var import_utils = require("@haneullabs/utils");
|
|
48
|
+
var import_bcs_type = require("./bcs-type.js");
|
|
49
|
+
var import_bcs = require("./bcs.js");
|
|
50
|
+
var import_reader = require("./reader.js");
|
|
51
|
+
var import_utils2 = require("./utils.js");
|
|
52
|
+
var import_writer = require("./writer.js");
|
|
53
|
+
const toB58 = import_utils.toBase58;
|
|
54
|
+
const fromB58 = import_utils.fromBase58;
|
|
55
|
+
const toB64 = import_utils.toBase64;
|
|
56
|
+
const fromB64 = import_utils.fromBase64;
|
|
57
|
+
const toHEX = import_utils.toHex;
|
|
58
|
+
const fromHEX = import_utils.fromHex;
|
|
59
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n/*\n * BCS implementation {@see https://github.com/diem/bcs } for JavaScript.\n * Intended to be used for Move applications; supports both NodeJS and browser.\n *\n * For more details and examples {@see README.md }.\n *\n * @module bcs\n * @property {BcsReader}\n */\n\nimport { toBase58, fromBase58, toBase64, fromBase64, toHex, fromHex } from '@haneullabs/utils';\nimport type { BcsTypeOptions } from './bcs-type.js';\nimport {\n\tBcsType,\n\tBcsStruct,\n\tBcsEnum,\n\tBcsTuple,\n\tisSerializedBcs,\n\tSerializedBcs,\n} from './bcs-type.js';\nimport { bcs } from './bcs.js';\nimport { BcsReader } from './reader.js';\nimport type {\n\tEnumInputShape,\n\tEnumOutputShape,\n\tEnumOutputShapeWithKeys,\n\tInferBcsInput,\n\tInferBcsType,\n} from './types.js';\nimport { decodeStr, encodeStr, splitGenericParameters } from './utils.js';\nimport type { BcsWriterOptions } from './writer.js';\nimport { BcsWriter } from './writer.js';\n\n// Re-export all encoding dependencies.\nexport {\n\tbcs,\n\tBcsType,\n\tBcsStruct,\n\tBcsEnum,\n\tBcsTuple,\n\ttype BcsTypeOptions,\n\tSerializedBcs,\n\tisSerializedBcs,\n\ttoBase58,\n\tfromBase58,\n\ttoBase64,\n\tfromBase64,\n\ttoHex,\n\tfromHex,\n\tencodeStr,\n\tdecodeStr,\n\tsplitGenericParameters,\n\tBcsReader,\n\tBcsWriter,\n\ttype BcsWriterOptions,\n\ttype InferBcsInput,\n\ttype InferBcsType,\n\ttype EnumOutputShape,\n\ttype EnumInputShape,\n\ttype EnumOutputShapeWithKeys,\n};\n\n/** @deprecated use toBase58 instead */\nexport const toB58 = toBase58;\n\n/** @deprecated use fromBase58 instead */\nexport const fromB58 = fromBase58;\n\n/** @deprecated use toBase64 instead */\nexport const toB64 = toBase64;\n\n/** @deprecated use fromBase64 instead */\nexport const fromB64 = fromBase64;\n\n/** @deprecated use toHex instead */\nexport const toHEX = toHex;\n\n/** @deprecated use fromHex instead */\nexport const fromHEX = fromHex;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,mBAA2E;AAE3E,sBAOO;AACP,iBAAoB;AACpB,oBAA0B;AAQ1B,IAAAA,gBAA6D;AAE7D,oBAA0B;AAgCnB,MAAM,QAAQ;AAGd,MAAM,UAAU;AAGhB,MAAM,QAAQ;AAGd,MAAM,UAAU;AAGhB,MAAM,QAAQ;AAGd,MAAM,UAAU;",
|
|
6
|
+
"names": ["import_utils"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class used for reading BCS data chunk by chunk. Meant to be used
|
|
3
|
+
* by some wrapper, which will make sure that data is valid and is
|
|
4
|
+
* matching the desired format.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* // data for this example is:
|
|
8
|
+
* // { a: u8, b: u32, c: bool, d: u64 }
|
|
9
|
+
*
|
|
10
|
+
* let reader = new BcsReader("647f1a060001ffffe7890423c78a050102030405");
|
|
11
|
+
* let field1 = reader.read8();
|
|
12
|
+
* let field2 = reader.read32();
|
|
13
|
+
* let field3 = reader.read8() === '1'; // bool
|
|
14
|
+
* let field4 = reader.read64();
|
|
15
|
+
* // ....
|
|
16
|
+
*
|
|
17
|
+
* Reading vectors is another deal in bcs. To read a vector, you first need to read
|
|
18
|
+
* its length using {@link readULEB}. Here's an example:
|
|
19
|
+
* @example
|
|
20
|
+
* // data encoded: { field: [1, 2, 3, 4, 5] }
|
|
21
|
+
* let reader = new BcsReader("050102030405");
|
|
22
|
+
* let vec_length = reader.readULEB();
|
|
23
|
+
* let elements = [];
|
|
24
|
+
* for (let i = 0; i < vec_length; i++) {
|
|
25
|
+
* elements.push(reader.read8());
|
|
26
|
+
* }
|
|
27
|
+
* console.log(elements); // [1,2,3,4,5]
|
|
28
|
+
*
|
|
29
|
+
* @param {String} data HEX-encoded data (serialized BCS)
|
|
30
|
+
*/
|
|
31
|
+
export declare class BcsReader {
|
|
32
|
+
private dataView;
|
|
33
|
+
private bytePosition;
|
|
34
|
+
/**
|
|
35
|
+
* @param {Uint8Array} data Data to use as a buffer.
|
|
36
|
+
*/
|
|
37
|
+
constructor(data: Uint8Array);
|
|
38
|
+
/**
|
|
39
|
+
* Shift current cursor position by `bytes`.
|
|
40
|
+
*
|
|
41
|
+
* @param {Number} bytes Number of bytes to
|
|
42
|
+
* @returns {this} Self for possible chaining.
|
|
43
|
+
*/
|
|
44
|
+
shift(bytes: number): this;
|
|
45
|
+
/**
|
|
46
|
+
* Read U8 value from the buffer and shift cursor by 1.
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
read8(): number;
|
|
50
|
+
/**
|
|
51
|
+
* Read U16 value from the buffer and shift cursor by 2.
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
read16(): number;
|
|
55
|
+
/**
|
|
56
|
+
* Read U32 value from the buffer and shift cursor by 4.
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
read32(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Read U64 value from the buffer and shift cursor by 8.
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
read64(): string;
|
|
65
|
+
/**
|
|
66
|
+
* Read U128 value from the buffer and shift cursor by 16.
|
|
67
|
+
*/
|
|
68
|
+
read128(): string;
|
|
69
|
+
/**
|
|
70
|
+
* Read U128 value from the buffer and shift cursor by 32.
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
read256(): string;
|
|
74
|
+
/**
|
|
75
|
+
* Read `num` number of bytes from the buffer and shift cursor by `num`.
|
|
76
|
+
* @param num Number of bytes to read.
|
|
77
|
+
*/
|
|
78
|
+
readBytes(num: number): Uint8Array;
|
|
79
|
+
/**
|
|
80
|
+
* Read ULEB value - an integer of varying size. Used for enum indexes and
|
|
81
|
+
* vector lengths.
|
|
82
|
+
* @returns {Number} The ULEB value.
|
|
83
|
+
*/
|
|
84
|
+
readULEB(): number;
|
|
85
|
+
/**
|
|
86
|
+
* Read a BCS vector: read a length and then apply function `cb` X times
|
|
87
|
+
* where X is the length of the vector, defined as ULEB in BCS bytes.
|
|
88
|
+
* @param cb Callback to process elements of vector.
|
|
89
|
+
* @returns {Array<Any>} Array of the resulting values, returned by callback.
|
|
90
|
+
*/
|
|
91
|
+
readVec(cb: (reader: BcsReader, i: number, length: number) => any): any[];
|
|
92
|
+
}
|