@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.
Files changed (63) hide show
  1. package/CHANGELOG.md +388 -0
  2. package/README.md +358 -0
  3. package/dist/cjs/bcs-type.d.ts +127 -0
  4. package/dist/cjs/bcs-type.js +386 -0
  5. package/dist/cjs/bcs-type.js.map +7 -0
  6. package/dist/cjs/bcs.d.ts +175 -0
  7. package/dist/cjs/bcs.js +406 -0
  8. package/dist/cjs/bcs.js.map +7 -0
  9. package/dist/cjs/index.d.ts +22 -0
  10. package/dist/cjs/index.js +59 -0
  11. package/dist/cjs/index.js.map +7 -0
  12. package/dist/cjs/package.json +5 -0
  13. package/dist/cjs/reader.d.ts +92 -0
  14. package/dist/cjs/reader.js +136 -0
  15. package/dist/cjs/reader.js.map +7 -0
  16. package/dist/cjs/types.d.ts +28 -0
  17. package/dist/cjs/types.js +17 -0
  18. package/dist/cjs/types.js.map +7 -0
  19. package/dist/cjs/uleb.d.ts +5 -0
  20. package/dist/cjs/uleb.js +66 -0
  21. package/dist/cjs/uleb.js.map +7 -0
  22. package/dist/cjs/utils.d.ts +18 -0
  23. package/dist/cjs/utils.js +74 -0
  24. package/dist/cjs/utils.js.map +7 -0
  25. package/dist/cjs/writer.d.ts +117 -0
  26. package/dist/cjs/writer.js +196 -0
  27. package/dist/cjs/writer.js.map +7 -0
  28. package/dist/esm/bcs-type.d.ts +127 -0
  29. package/dist/esm/bcs-type.js +366 -0
  30. package/dist/esm/bcs-type.js.map +7 -0
  31. package/dist/esm/bcs.d.ts +175 -0
  32. package/dist/esm/bcs.js +397 -0
  33. package/dist/esm/bcs.js.map +7 -0
  34. package/dist/esm/index.d.ts +22 -0
  35. package/dist/esm/index.js +46 -0
  36. package/dist/esm/index.js.map +7 -0
  37. package/dist/esm/package.json +5 -0
  38. package/dist/esm/reader.d.ts +92 -0
  39. package/dist/esm/reader.js +116 -0
  40. package/dist/esm/reader.js.map +7 -0
  41. package/dist/esm/types.d.ts +28 -0
  42. package/dist/esm/types.js +1 -0
  43. package/dist/esm/types.js.map +7 -0
  44. package/dist/esm/uleb.d.ts +5 -0
  45. package/dist/esm/uleb.js +46 -0
  46. package/dist/esm/uleb.js.map +7 -0
  47. package/dist/esm/utils.d.ts +18 -0
  48. package/dist/esm/utils.js +54 -0
  49. package/dist/esm/utils.js.map +7 -0
  50. package/dist/esm/writer.d.ts +117 -0
  51. package/dist/esm/writer.js +176 -0
  52. package/dist/esm/writer.js.map +7 -0
  53. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  54. package/dist/tsconfig.tsbuildinfo +1 -0
  55. package/package.json +73 -0
  56. package/src/bcs-type.ts +531 -0
  57. package/src/bcs.ts +543 -0
  58. package/src/index.ts +82 -0
  59. package/src/reader.ts +156 -0
  60. package/src/types.ts +52 -0
  61. package/src/uleb.ts +61 -0
  62. package/src/utils.ts +75 -0
  63. package/src/writer.ts +222 -0
@@ -0,0 +1,366 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var _write, _serialize, _schema, _bytes;
9
+ import { fromBase58, fromBase64, toBase58, toBase64, fromHex, toHex } from "@haneullabs/utils";
10
+ import { BcsReader } from "./reader.js";
11
+ import { ulebEncode } from "./uleb.js";
12
+ import { BcsWriter } from "./writer.js";
13
+ const _BcsType = class _BcsType {
14
+ constructor(options) {
15
+ __privateAdd(this, _write);
16
+ __privateAdd(this, _serialize);
17
+ this.name = options.name;
18
+ this.read = options.read;
19
+ this.serializedSize = options.serializedSize ?? (() => null);
20
+ __privateSet(this, _write, options.write);
21
+ __privateSet(this, _serialize, options.serialize ?? ((value, options2) => {
22
+ const writer = new BcsWriter({
23
+ initialSize: this.serializedSize(value) ?? void 0,
24
+ ...options2
25
+ });
26
+ __privateGet(this, _write).call(this, value, writer);
27
+ return writer.toBytes();
28
+ }));
29
+ this.validate = options.validate ?? (() => {
30
+ });
31
+ }
32
+ write(value, writer) {
33
+ this.validate(value);
34
+ __privateGet(this, _write).call(this, value, writer);
35
+ }
36
+ serialize(value, options) {
37
+ this.validate(value);
38
+ return new SerializedBcs(this, __privateGet(this, _serialize).call(this, value, options));
39
+ }
40
+ parse(bytes) {
41
+ const reader = new BcsReader(bytes);
42
+ return this.read(reader);
43
+ }
44
+ fromHex(hex) {
45
+ return this.parse(fromHex(hex));
46
+ }
47
+ fromBase58(b64) {
48
+ return this.parse(fromBase58(b64));
49
+ }
50
+ fromBase64(b64) {
51
+ return this.parse(fromBase64(b64));
52
+ }
53
+ transform({
54
+ name,
55
+ input,
56
+ output,
57
+ validate
58
+ }) {
59
+ return new _BcsType({
60
+ name: name ?? this.name,
61
+ read: (reader) => output ? output(this.read(reader)) : this.read(reader),
62
+ write: (value, writer) => __privateGet(this, _write).call(this, input ? input(value) : value, writer),
63
+ serializedSize: (value) => this.serializedSize(input ? input(value) : value),
64
+ serialize: (value, options) => __privateGet(this, _serialize).call(this, input ? input(value) : value, options),
65
+ validate: (value) => {
66
+ validate?.(value);
67
+ this.validate(input ? input(value) : value);
68
+ }
69
+ });
70
+ }
71
+ };
72
+ _write = new WeakMap();
73
+ _serialize = new WeakMap();
74
+ let BcsType = _BcsType;
75
+ const SERIALIZED_BCS_BRAND = Symbol.for("@haneullabs/serialized-bcs");
76
+ function isSerializedBcs(obj) {
77
+ return !!obj && typeof obj === "object" && obj[SERIALIZED_BCS_BRAND] === true;
78
+ }
79
+ class SerializedBcs {
80
+ constructor(schema, bytes) {
81
+ __privateAdd(this, _schema);
82
+ __privateAdd(this, _bytes);
83
+ __privateSet(this, _schema, schema);
84
+ __privateSet(this, _bytes, bytes);
85
+ }
86
+ // Used to brand SerializedBcs so that they can be identified, even between multiple copies
87
+ // of the @haneullabs/bcs package are installed
88
+ get [SERIALIZED_BCS_BRAND]() {
89
+ return true;
90
+ }
91
+ toBytes() {
92
+ return __privateGet(this, _bytes);
93
+ }
94
+ toHex() {
95
+ return toHex(__privateGet(this, _bytes));
96
+ }
97
+ toBase64() {
98
+ return toBase64(__privateGet(this, _bytes));
99
+ }
100
+ toBase58() {
101
+ return toBase58(__privateGet(this, _bytes));
102
+ }
103
+ parse() {
104
+ return __privateGet(this, _schema).parse(__privateGet(this, _bytes));
105
+ }
106
+ }
107
+ _schema = new WeakMap();
108
+ _bytes = new WeakMap();
109
+ function fixedSizeBcsType({
110
+ size,
111
+ ...options
112
+ }) {
113
+ return new BcsType({
114
+ ...options,
115
+ serializedSize: () => size
116
+ });
117
+ }
118
+ function uIntBcsType({
119
+ readMethod,
120
+ writeMethod,
121
+ ...options
122
+ }) {
123
+ return fixedSizeBcsType({
124
+ ...options,
125
+ read: (reader) => reader[readMethod](),
126
+ write: (value, writer) => writer[writeMethod](value),
127
+ validate: (value) => {
128
+ if (value < 0 || value > options.maxValue) {
129
+ throw new TypeError(
130
+ `Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`
131
+ );
132
+ }
133
+ options.validate?.(value);
134
+ }
135
+ });
136
+ }
137
+ function bigUIntBcsType({
138
+ readMethod,
139
+ writeMethod,
140
+ ...options
141
+ }) {
142
+ return fixedSizeBcsType({
143
+ ...options,
144
+ read: (reader) => reader[readMethod](),
145
+ write: (value, writer) => writer[writeMethod](BigInt(value)),
146
+ validate: (val) => {
147
+ const value = BigInt(val);
148
+ if (value < 0 || value > options.maxValue) {
149
+ throw new TypeError(
150
+ `Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`
151
+ );
152
+ }
153
+ options.validate?.(value);
154
+ }
155
+ });
156
+ }
157
+ function dynamicSizeBcsType({
158
+ serialize,
159
+ ...options
160
+ }) {
161
+ const type = new BcsType({
162
+ ...options,
163
+ serialize,
164
+ write: (value, writer) => {
165
+ for (const byte of type.serialize(value).toBytes()) {
166
+ writer.write8(byte);
167
+ }
168
+ }
169
+ });
170
+ return type;
171
+ }
172
+ function stringLikeBcsType({
173
+ toBytes,
174
+ fromBytes,
175
+ ...options
176
+ }) {
177
+ return new BcsType({
178
+ ...options,
179
+ read: (reader) => {
180
+ const length = reader.readULEB();
181
+ const bytes = reader.readBytes(length);
182
+ return fromBytes(bytes);
183
+ },
184
+ write: (hex, writer) => {
185
+ const bytes = toBytes(hex);
186
+ writer.writeULEB(bytes.length);
187
+ for (let i = 0; i < bytes.length; i++) {
188
+ writer.write8(bytes[i]);
189
+ }
190
+ },
191
+ serialize: (value) => {
192
+ const bytes = toBytes(value);
193
+ const size = ulebEncode(bytes.length);
194
+ const result = new Uint8Array(size.length + bytes.length);
195
+ result.set(size, 0);
196
+ result.set(bytes, size.length);
197
+ return result;
198
+ },
199
+ validate: (value) => {
200
+ if (typeof value !== "string") {
201
+ throw new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);
202
+ }
203
+ options.validate?.(value);
204
+ }
205
+ });
206
+ }
207
+ function lazyBcsType(cb) {
208
+ let lazyType = null;
209
+ function getType() {
210
+ if (!lazyType) {
211
+ lazyType = cb();
212
+ }
213
+ return lazyType;
214
+ }
215
+ return new BcsType({
216
+ name: "lazy",
217
+ read: (data) => getType().read(data),
218
+ serializedSize: (value) => getType().serializedSize(value),
219
+ write: (value, writer) => getType().write(value, writer),
220
+ serialize: (value, options) => getType().serialize(value, options).toBytes()
221
+ });
222
+ }
223
+ class BcsStruct extends BcsType {
224
+ constructor({ name, fields, ...options }) {
225
+ const canonicalOrder = Object.entries(fields);
226
+ super({
227
+ name,
228
+ serializedSize: (values) => {
229
+ let total = 0;
230
+ for (const [field, type] of canonicalOrder) {
231
+ const size = type.serializedSize(values[field]);
232
+ if (size == null) {
233
+ return null;
234
+ }
235
+ total += size;
236
+ }
237
+ return total;
238
+ },
239
+ read: (reader) => {
240
+ const result = {};
241
+ for (const [field, type] of canonicalOrder) {
242
+ result[field] = type.read(reader);
243
+ }
244
+ return result;
245
+ },
246
+ write: (value, writer) => {
247
+ for (const [field, type] of canonicalOrder) {
248
+ type.write(value[field], writer);
249
+ }
250
+ },
251
+ ...options,
252
+ validate: (value) => {
253
+ options?.validate?.(value);
254
+ if (typeof value !== "object" || value == null) {
255
+ throw new TypeError(`Expected object, found ${typeof value}`);
256
+ }
257
+ }
258
+ });
259
+ }
260
+ }
261
+ class BcsEnum extends BcsType {
262
+ constructor({ fields, ...options }) {
263
+ const canonicalOrder = Object.entries(fields);
264
+ super({
265
+ read: (reader) => {
266
+ const index = reader.readULEB();
267
+ const enumEntry = canonicalOrder[index];
268
+ if (!enumEntry) {
269
+ throw new TypeError(`Unknown value ${index} for enum ${options.name}`);
270
+ }
271
+ const [kind, type] = enumEntry;
272
+ return {
273
+ [kind]: type?.read(reader) ?? true,
274
+ $kind: kind
275
+ };
276
+ },
277
+ write: (value, writer) => {
278
+ const [name, val] = Object.entries(value).filter(
279
+ ([name2]) => Object.hasOwn(fields, name2)
280
+ )[0];
281
+ for (let i = 0; i < canonicalOrder.length; i++) {
282
+ const [optionName, optionType] = canonicalOrder[i];
283
+ if (optionName === name) {
284
+ writer.writeULEB(i);
285
+ optionType?.write(val, writer);
286
+ return;
287
+ }
288
+ }
289
+ },
290
+ ...options,
291
+ validate: (value) => {
292
+ options?.validate?.(value);
293
+ if (typeof value !== "object" || value == null) {
294
+ throw new TypeError(`Expected object, found ${typeof value}`);
295
+ }
296
+ const keys = Object.keys(value).filter(
297
+ (k) => value[k] !== void 0 && Object.hasOwn(fields, k)
298
+ );
299
+ if (keys.length !== 1) {
300
+ throw new TypeError(
301
+ `Expected object with one key, but found ${keys.length} for type ${options.name}}`
302
+ );
303
+ }
304
+ const [variant] = keys;
305
+ if (!Object.hasOwn(fields, variant)) {
306
+ throw new TypeError(`Invalid enum variant ${variant}`);
307
+ }
308
+ }
309
+ });
310
+ }
311
+ }
312
+ class BcsTuple extends BcsType {
313
+ constructor({ fields, name, ...options }) {
314
+ super({
315
+ name: name ?? `(${fields.map((t) => t.name).join(", ")})`,
316
+ serializedSize: (values) => {
317
+ let total = 0;
318
+ for (let i = 0; i < fields.length; i++) {
319
+ const size = fields[i].serializedSize(values[i]);
320
+ if (size == null) {
321
+ return null;
322
+ }
323
+ total += size;
324
+ }
325
+ return total;
326
+ },
327
+ read: (reader) => {
328
+ const result = [];
329
+ for (const field of fields) {
330
+ result.push(field.read(reader));
331
+ }
332
+ return result;
333
+ },
334
+ write: (value, writer) => {
335
+ for (let i = 0; i < fields.length; i++) {
336
+ fields[i].write(value[i], writer);
337
+ }
338
+ },
339
+ ...options,
340
+ validate: (value) => {
341
+ options?.validate?.(value);
342
+ if (!Array.isArray(value)) {
343
+ throw new TypeError(`Expected array, found ${typeof value}`);
344
+ }
345
+ if (value.length !== fields.length) {
346
+ throw new TypeError(`Expected array of length ${fields.length}, found ${value.length}`);
347
+ }
348
+ }
349
+ });
350
+ }
351
+ }
352
+ export {
353
+ BcsEnum,
354
+ BcsStruct,
355
+ BcsTuple,
356
+ BcsType,
357
+ SerializedBcs,
358
+ bigUIntBcsType,
359
+ dynamicSizeBcsType,
360
+ fixedSizeBcsType,
361
+ isSerializedBcs,
362
+ lazyBcsType,
363
+ stringLikeBcsType,
364
+ uIntBcsType
365
+ };
366
+ //# sourceMappingURL=bcs-type.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/bcs-type.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase58, fromBase64, toBase58, toBase64, fromHex, toHex } from '@haneullabs/utils';\nimport { BcsReader } from './reader.js';\nimport { ulebEncode } from './uleb.js';\nimport type { BcsWriterOptions } from './writer.js';\nimport { BcsWriter } from './writer.js';\nimport type { EnumInputShape, EnumOutputShape, JoinString } from './types.js';\n\nexport interface BcsTypeOptions<T, Input = T, Name extends string = string> {\n\tname?: Name;\n\tvalidate?: (value: Input) => void;\n}\n\nexport class BcsType<T, Input = T, const Name extends string = string> {\n\t$inferType!: T;\n\t$inferInput!: Input;\n\tname: Name;\n\tread: (reader: BcsReader) => T;\n\tserializedSize: (value: Input, options?: BcsWriterOptions) => number | null;\n\tvalidate: (value: Input) => void;\n\t#write: (value: Input, writer: BcsWriter) => void;\n\t#serialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n\n\tconstructor(\n\t\toptions: {\n\t\t\tname: Name;\n\t\t\tread: (reader: BcsReader) => T;\n\t\t\twrite: (value: Input, writer: BcsWriter) => void;\n\t\t\tserialize?: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n\t\t\tserializedSize?: (value: Input) => number | null;\n\t\t\tvalidate?: (value: Input) => void;\n\t\t} & BcsTypeOptions<T, Input, Name>,\n\t) {\n\t\tthis.name = options.name;\n\t\tthis.read = options.read;\n\t\tthis.serializedSize = options.serializedSize ?? (() => null);\n\t\tthis.#write = options.write;\n\t\tthis.#serialize =\n\t\t\toptions.serialize ??\n\t\t\t((value, options) => {\n\t\t\t\tconst writer = new BcsWriter({\n\t\t\t\t\tinitialSize: this.serializedSize(value) ?? undefined,\n\t\t\t\t\t...options,\n\t\t\t\t});\n\t\t\t\tthis.#write(value, writer);\n\t\t\t\treturn writer.toBytes();\n\t\t\t});\n\n\t\tthis.validate = options.validate ?? (() => {});\n\t}\n\n\twrite(value: Input, writer: BcsWriter) {\n\t\tthis.validate(value);\n\t\tthis.#write(value, writer);\n\t}\n\n\tserialize(value: Input, options?: BcsWriterOptions) {\n\t\tthis.validate(value);\n\t\treturn new SerializedBcs(this, this.#serialize(value, options));\n\t}\n\n\tparse(bytes: Uint8Array): T {\n\t\tconst reader = new BcsReader(bytes);\n\t\treturn this.read(reader);\n\t}\n\n\tfromHex(hex: string) {\n\t\treturn this.parse(fromHex(hex));\n\t}\n\n\tfromBase58(b64: string) {\n\t\treturn this.parse(fromBase58(b64));\n\t}\n\n\tfromBase64(b64: string) {\n\t\treturn this.parse(fromBase64(b64));\n\t}\n\n\ttransform<T2 = T, Input2 = Input, NewName extends string = Name>({\n\t\tname,\n\t\tinput,\n\t\toutput,\n\t\tvalidate,\n\t}: {\n\t\tinput?: (val: Input2) => Input;\n\t\toutput?: (value: T) => T2;\n\t} & BcsTypeOptions<T2, Input2, NewName>) {\n\t\treturn new BcsType<T2, Input2, NewName>({\n\t\t\tname: (name ?? this.name) as NewName,\n\t\t\tread: (reader) => (output ? output(this.read(reader)) : (this.read(reader) as never)),\n\t\t\twrite: (value, writer) => this.#write(input ? input(value) : (value as never), writer),\n\t\t\tserializedSize: (value) => this.serializedSize(input ? input(value) : (value as never)),\n\t\t\tserialize: (value, options) =>\n\t\t\t\tthis.#serialize(input ? input(value) : (value as never), options),\n\t\t\tvalidate: (value) => {\n\t\t\t\tvalidate?.(value);\n\t\t\t\tthis.validate(input ? input(value) : (value as never));\n\t\t\t},\n\t\t});\n\t}\n}\n\nconst SERIALIZED_BCS_BRAND = Symbol.for('@haneullabs/serialized-bcs') as never;\nexport function isSerializedBcs(obj: unknown): obj is SerializedBcs<unknown> {\n\treturn !!obj && typeof obj === 'object' && (obj as any)[SERIALIZED_BCS_BRAND] === true;\n}\n\nexport class SerializedBcs<T, Input = T> {\n\t#schema: BcsType<T, Input>;\n\t#bytes: Uint8Array<ArrayBuffer>;\n\n\t// Used to brand SerializedBcs so that they can be identified, even between multiple copies\n\t// of the @haneullabs/bcs package are installed\n\tget [SERIALIZED_BCS_BRAND]() {\n\t\treturn true;\n\t}\n\n\tconstructor(schema: BcsType<T, Input>, bytes: Uint8Array<ArrayBuffer>) {\n\t\tthis.#schema = schema;\n\t\tthis.#bytes = bytes;\n\t}\n\n\ttoBytes() {\n\t\treturn this.#bytes;\n\t}\n\n\ttoHex() {\n\t\treturn toHex(this.#bytes);\n\t}\n\n\ttoBase64() {\n\t\treturn toBase64(this.#bytes);\n\t}\n\n\ttoBase58() {\n\t\treturn toBase58(this.#bytes);\n\t}\n\n\tparse() {\n\t\treturn this.#schema.parse(this.#bytes);\n\t}\n}\n\nexport function fixedSizeBcsType<T, Input = T, const Name extends string = string>({\n\tsize,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\tread: (reader: BcsReader) => T;\n\twrite: (value: Input, writer: BcsWriter) => void;\n} & BcsTypeOptions<T, Input, Name>) {\n\treturn new BcsType<T, Input, Name>({\n\t\t...options,\n\t\tserializedSize: () => size,\n\t});\n}\n\nexport function uIntBcsType<const Name extends string = string>({\n\treadMethod,\n\twriteMethod,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\treadMethod: `read${8 | 16 | 32}`;\n\twriteMethod: `write${8 | 16 | 32}`;\n\tmaxValue: number;\n} & BcsTypeOptions<number, number, Name>) {\n\treturn fixedSizeBcsType<number, number, Name>({\n\t\t...options,\n\t\tread: (reader) => reader[readMethod](),\n\t\twrite: (value, writer) => writer[writeMethod](value),\n\t\tvalidate: (value) => {\n\t\t\tif (value < 0 || value > options.maxValue) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function bigUIntBcsType<const Name extends string = string>({\n\treadMethod,\n\twriteMethod,\n\t...options\n}: {\n\tname: Name;\n\tsize: number;\n\treadMethod: `read${64 | 128 | 256}`;\n\twriteMethod: `write${64 | 128 | 256}`;\n\tmaxValue: bigint;\n} & BcsTypeOptions<string, string | number | bigint>) {\n\treturn fixedSizeBcsType<string, string | number | bigint, Name>({\n\t\t...options,\n\t\tread: (reader) => reader[readMethod](),\n\t\twrite: (value, writer) => writer[writeMethod](BigInt(value)),\n\t\tvalidate: (val) => {\n\t\t\tconst value = BigInt(val);\n\t\t\tif (value < 0 || value > options.maxValue) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({\n\tserialize,\n\t...options\n}: {\n\tname: Name;\n\tread: (reader: BcsReader) => T;\n\tserialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;\n} & BcsTypeOptions<T, Input>) {\n\tconst type = new BcsType<T, Input>({\n\t\t...options,\n\t\tserialize,\n\t\twrite: (value, writer) => {\n\t\t\tfor (const byte of type.serialize(value).toBytes()) {\n\t\t\t\twriter.write8(byte);\n\t\t\t}\n\t\t},\n\t});\n\n\treturn type;\n}\n\nexport function stringLikeBcsType<const Name extends string = string>({\n\ttoBytes,\n\tfromBytes,\n\t...options\n}: {\n\tname: Name;\n\ttoBytes: (value: string) => Uint8Array;\n\tfromBytes: (bytes: Uint8Array) => string;\n\tserializedSize?: (value: string) => number | null;\n} & BcsTypeOptions<string, string, Name>) {\n\treturn new BcsType<string, string, Name>({\n\t\t...options,\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst bytes = reader.readBytes(length);\n\n\t\t\treturn fromBytes(bytes);\n\t\t},\n\t\twrite: (hex, writer) => {\n\t\t\tconst bytes = toBytes(hex);\n\t\t\twriter.writeULEB(bytes.length);\n\t\t\tfor (let i = 0; i < bytes.length; i++) {\n\t\t\t\twriter.write8(bytes[i]);\n\t\t\t}\n\t\t},\n\t\tserialize: (value) => {\n\t\t\tconst bytes = toBytes(value);\n\t\t\tconst size = ulebEncode(bytes.length);\n\t\t\tconst result = new Uint8Array(size.length + bytes.length);\n\t\t\tresult.set(size, 0);\n\t\t\tresult.set(bytes, size.length);\n\n\t\t\treturn result;\n\t\t},\n\t\tvalidate: (value) => {\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tthrow new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);\n\t\t\t}\n\t\t\toptions.validate?.(value);\n\t\t},\n\t});\n}\n\nexport function lazyBcsType<T, Input>(cb: () => BcsType<T, Input>) {\n\tlet lazyType: BcsType<T, Input> | null = null;\n\tfunction getType() {\n\t\tif (!lazyType) {\n\t\t\tlazyType = cb();\n\t\t}\n\t\treturn lazyType;\n\t}\n\n\treturn new BcsType<T, Input>({\n\t\tname: 'lazy' as never,\n\t\tread: (data) => getType().read(data),\n\t\tserializedSize: (value) => getType().serializedSize(value),\n\t\twrite: (value, writer) => getType().write(value, writer),\n\t\tserialize: (value, options) => getType().serialize(value, options).toBytes(),\n\t});\n}\n\nexport interface BcsStructOptions<\n\tT extends Record<string, BcsType<any>>,\n\tName extends string = string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t\t},\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t\t},\n\t\tName\n\t>,\n\t'name'\n> {\n\tname: Name;\n\tfields: T;\n}\n\nexport class BcsStruct<\n\tT extends Record<string, BcsType<any>>,\n\tconst Name extends string = string,\n> extends BcsType<\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t},\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t},\n\tName\n> {\n\tconstructor({ name, fields, ...options }: BcsStructOptions<T, Name>) {\n\t\tconst canonicalOrder = Object.entries(fields);\n\n\t\tsuper({\n\t\t\tname,\n\t\t\tserializedSize: (values) => {\n\t\t\t\tlet total = 0;\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\tconst size = type.serializedSize(values[field]);\n\t\t\t\t\tif (size == null) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\ttotal += size;\n\t\t\t\t}\n\n\t\t\t\treturn total;\n\t\t\t},\n\t\t\tread: (reader) => {\n\t\t\t\tconst result: Record<string, unknown> = {};\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\tresult[field] = type.read(reader);\n\t\t\t\t}\n\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tfor (const [field, type] of canonicalOrder) {\n\t\t\t\t\ttype.write(value[field], writer);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'object' || value == null) {\n\t\t\t\t\tthrow new TypeError(`Expected object, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport interface BcsEnumOptions<\n\tT extends Record<string, BcsType<any> | null>,\n\tName extends string = string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\tEnumOutputShape<{\n\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;\n\t\t}>,\n\t\tEnumInputShape<{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t\t}>,\n\t\tName\n\t>,\n\t'name'\n> {\n\tname: Name;\n\tfields: T;\n}\n\nexport class BcsEnum<\n\tT extends Record<string, BcsType<any> | null>,\n\tconst Name extends string = string,\n> extends BcsType<\n\tEnumOutputShape<{\n\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true;\n\t}>,\n\tEnumInputShape<{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t}>,\n\tName\n> {\n\tconstructor({ fields, ...options }: BcsEnumOptions<T, Name>) {\n\t\tconst canonicalOrder = Object.entries(fields as object);\n\t\tsuper({\n\t\t\tread: (reader) => {\n\t\t\t\tconst index = reader.readULEB();\n\n\t\t\t\tconst enumEntry = canonicalOrder[index];\n\t\t\t\tif (!enumEntry) {\n\t\t\t\t\tthrow new TypeError(`Unknown value ${index} for enum ${options.name}`);\n\t\t\t\t}\n\n\t\t\t\tconst [kind, type] = enumEntry;\n\n\t\t\t\treturn {\n\t\t\t\t\t[kind]: type?.read(reader) ?? true,\n\t\t\t\t\t$kind: kind,\n\t\t\t\t} as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tconst [name, val] = Object.entries(value).filter(([name]) =>\n\t\t\t\t\tObject.hasOwn(fields, name),\n\t\t\t\t)[0];\n\n\t\t\t\tfor (let i = 0; i < canonicalOrder.length; i++) {\n\t\t\t\t\tconst [optionName, optionType] = canonicalOrder[i];\n\t\t\t\t\tif (optionName === name) {\n\t\t\t\t\t\twriter.writeULEB(i);\n\t\t\t\t\t\toptionType?.write(val, writer);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'object' || value == null) {\n\t\t\t\t\tthrow new TypeError(`Expected object, found ${typeof value}`);\n\t\t\t\t}\n\n\t\t\t\tconst keys = Object.keys(value).filter(\n\t\t\t\t\t(k) => value[k] !== undefined && Object.hasOwn(fields, k),\n\t\t\t\t);\n\n\t\t\t\tif (keys.length !== 1) {\n\t\t\t\t\tthrow new TypeError(\n\t\t\t\t\t\t`Expected object with one key, but found ${keys.length} for type ${options.name}}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tconst [variant] = keys;\n\n\t\t\t\tif (!Object.hasOwn(fields, variant)) {\n\t\t\t\t\tthrow new TypeError(`Invalid enum variant ${variant}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n\nexport interface BcsTupleOptions<\n\tT extends readonly BcsType<any>[],\n\tName extends string,\n> extends Omit<\n\tBcsTypeOptions<\n\t\t{\n\t\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t\t},\n\t\t{\n\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t\t},\n\t\tName\n\t>,\n\t'name'\n> {\n\tname?: Name;\n\tfields: T;\n}\n\nexport class BcsTuple<\n\tconst T extends readonly BcsType<any>[],\n\tconst Name extends string =\n\t\t`(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`,\n> extends BcsType<\n\t{\n\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t},\n\t{\n\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t},\n\tName\n> {\n\tconstructor({ fields, name, ...options }: BcsTupleOptions<T, Name>) {\n\t\tsuper({\n\t\t\tname: name ?? (`(${fields.map((t) => t.name).join(', ')})` as never),\n\t\t\tserializedSize: (values) => {\n\t\t\t\tlet total = 0;\n\t\t\t\tfor (let i = 0; i < fields.length; i++) {\n\t\t\t\t\tconst size = fields[i].serializedSize(values[i]);\n\t\t\t\t\tif (size == null) {\n\t\t\t\t\t\treturn null;\n\t\t\t\t\t}\n\n\t\t\t\t\ttotal += size;\n\t\t\t\t}\n\n\t\t\t\treturn total;\n\t\t\t},\n\t\t\tread: (reader) => {\n\t\t\t\tconst result: unknown[] = [];\n\t\t\t\tfor (const field of fields) {\n\t\t\t\t\tresult.push(field.read(reader));\n\t\t\t\t}\n\t\t\t\treturn result as never;\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tfor (let i = 0; i < fields.length; i++) {\n\t\t\t\t\tfields[i].write(value[i], writer);\n\t\t\t\t}\n\t\t\t},\n\t\t\t...options,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!Array.isArray(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 !== fields.length) {\n\t\t\t\t\tthrow new TypeError(`Expected array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AAC,MAAM,QAAQ,KAAK,GAAG;AAC1B,gBAAM,IAAI,UAAU,yBAAyB,OAAO,KAAK,EAAE;AAAA,QAC5D;AACA,YAAI,MAAM,WAAW,OAAO,QAAQ;AACnC,gBAAM,IAAI,UAAU,4BAA4B,OAAO,MAAM,WAAW,MAAM,MAAM,EAAE;AAAA,QACvF;AAAA,MACD;AAAA,IACD,CAAC;AAAA,EACF;AACD;",
6
+ "names": ["options", "name"]
7
+ }
@@ -0,0 +1,175 @@
1
+ import type { BcsTypeOptions } from './bcs-type.js';
2
+ import { BcsEnum, BcsStruct, BcsTuple, BcsType } from './bcs-type.js';
3
+ import type { EnumInputShape, EnumOutputShape, InferBcsInput, InferBcsType, JoinString } from './types.js';
4
+ declare function fixedArray<T extends BcsType<any>, Name extends string = string>(size: number, type: T, options?: BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
5
+ length: number;
6
+ }, Name>): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
7
+ length: number;
8
+ }, Name>;
9
+ declare function fixedArray<T, Input, Name extends string = string>(size: number, type: BcsType<T, Input>, options?: BcsTypeOptions<T[], Iterable<Input> & {
10
+ length: number;
11
+ }, Name>): BcsType<T[], Iterable<Input> & {
12
+ length: number;
13
+ }, Name>;
14
+ declare function option<T extends BcsType<any>>(type: T): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`>;
15
+ declare function option<T, Input, Name extends string = string>(type: BcsType<T, Input, Name>): BcsType<T | null, Input | null | undefined>;
16
+ declare function vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(type: T, options?: BcsTypeOptions<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
17
+ length: number;
18
+ }, Name>): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & {
19
+ length: number;
20
+ }, Name>;
21
+ declare function vector<T, Input, Name extends string = string>(type: BcsType<T, Input, Name>, options?: BcsTypeOptions<T[], Iterable<Input> & {
22
+ length: number;
23
+ }, `vector<${Name}>`>): BcsType<T[], Iterable<Input> & {
24
+ length: number;
25
+ }, `vector<${Name}>`>;
26
+ declare function map<K extends BcsType<any>, V extends BcsType<any>>(keyType: K, valueType: V): BcsType<Map<InferBcsType<K>, InferBcsType<V>>, Map<InferBcsInput<K>, InferBcsInput<V>>, `Map<${K['name']}, ${V['name']}>`>;
27
+ declare function map<K, V, InputK = K, InputV = V>(keyType: BcsType<K, InputK>, valueType: BcsType<V, InputV>): BcsType<Map<K, V>, Map<InputK, InputV>, `Map<${string}, ${string}>`>;
28
+ export declare const bcs: {
29
+ /**
30
+ * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
31
+ * @example
32
+ * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
33
+ */
34
+ u8(options?: BcsTypeOptions<number>): BcsType<number, number, "u8">;
35
+ /**
36
+ * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
37
+ * @example
38
+ * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
39
+ */
40
+ u16(options?: BcsTypeOptions<number>): BcsType<number, number, "u16">;
41
+ /**
42
+ * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
43
+ * @example
44
+ * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
45
+ */
46
+ u32(options?: BcsTypeOptions<number>): BcsType<number, number, "u32">;
47
+ /**
48
+ * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
49
+ * @example
50
+ * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
51
+ */
52
+ u64(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u64">;
53
+ /**
54
+ * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
55
+ * @example
56
+ * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
57
+ */
58
+ u128(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u128">;
59
+ /**
60
+ * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
61
+ * @example
62
+ * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
63
+ */
64
+ u256(options?: BcsTypeOptions<string, number | bigint | string>): BcsType<string, string | number | bigint, "u256">;
65
+ /**
66
+ * Creates a BcsType that can be used to read and write boolean values.
67
+ * @example
68
+ * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
69
+ */
70
+ bool(options?: BcsTypeOptions<boolean>): BcsType<boolean, boolean, "bool">;
71
+ /**
72
+ * Creates a BcsType that can be used to read and write unsigned LEB encoded integers
73
+ * @example
74
+ *
75
+ */
76
+ uleb128(options?: BcsTypeOptions<number>): BcsType<number, number, string>;
77
+ /**
78
+ * Creates a BcsType representing a fixed length byte array
79
+ * @param size The number of bytes this types represents
80
+ * @example
81
+ * bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
82
+ */
83
+ bytes<T extends number>(size: T, options?: BcsTypeOptions<Uint8Array, Iterable<number>>): BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, `bytes[${T}]`>;
84
+ /**
85
+ * Creates a BcsType representing a variable length byte array
86
+ *
87
+ * @example
88
+ * bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
89
+ */
90
+ byteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>): BcsType<Uint8Array<ArrayBufferLike>, Iterable<number>, "vector<u8>">;
91
+ /**
92
+ * Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
93
+ * @example
94
+ * bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
95
+ */
96
+ string(options?: BcsTypeOptions<string>): BcsType<string, string, "string">;
97
+ /**
98
+ * Creates a BcsType that represents a fixed length array of a given type
99
+ * @param size The number of elements in the array
100
+ * @param type The BcsType of each element in the array
101
+ * @example
102
+ * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
103
+ */
104
+ fixedArray: typeof fixedArray;
105
+ /**
106
+ * Creates a BcsType representing an optional value
107
+ * @param type The BcsType of the optional value
108
+ * @example
109
+ * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
110
+ * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
111
+ */
112
+ option: typeof option;
113
+ /**
114
+ * Creates a BcsType representing a variable length vector of a given type
115
+ * @param type The BcsType of each element in the vector
116
+ *
117
+ * @example
118
+ * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
119
+ */
120
+ vector: typeof vector;
121
+ /**
122
+ * Creates a BcsType representing a tuple of a given set of types
123
+ * @param types The BcsTypes for each element in the tuple
124
+ *
125
+ * @example
126
+ * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
127
+ * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
128
+ */
129
+ 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>;
130
+ /**
131
+ * Creates a BcsType representing a struct of a given set of fields
132
+ * @param name The name of the struct
133
+ * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized
134
+ *
135
+ * @example
136
+ * const struct = bcs.struct('MyStruct', {
137
+ * a: bcs.u8(),
138
+ * b: bcs.string(),
139
+ * })
140
+ * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
141
+ */
142
+ 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>;
143
+ /**
144
+ * Creates a BcsType representing an enum of a given set of options
145
+ * @param name The name of the enum
146
+ * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.
147
+ * null can be used to represent a variant with no data.
148
+ *
149
+ * @example
150
+ * const enum = bcs.enum('MyEnum', {
151
+ * A: bcs.u8(),
152
+ * B: bcs.string(),
153
+ * C: null,
154
+ * })
155
+ * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
156
+ * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
157
+ * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
158
+ */
159
+ 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>;
160
+ /**
161
+ * Creates a BcsType representing a map of a given key and value type
162
+ * @param keyType The BcsType of the key
163
+ * @param valueType The BcsType of the value
164
+ * @example
165
+ * const map = bcs.map(bcs.u8(), bcs.string())
166
+ * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
167
+ */
168
+ map: typeof map;
169
+ /**
170
+ * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
171
+ * @param cb A callback that returns the BcsType
172
+ */
173
+ lazy<T extends BcsType<any>>(cb: () => T): T;
174
+ };
175
+ export {};