@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,386 @@
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 __typeError = (msg) => {
7
+ throw TypeError(msg);
8
+ };
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
23
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
24
+ 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);
25
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
26
+ var bcs_type_exports = {};
27
+ __export(bcs_type_exports, {
28
+ BcsEnum: () => BcsEnum,
29
+ BcsStruct: () => BcsStruct,
30
+ BcsTuple: () => BcsTuple,
31
+ BcsType: () => BcsType,
32
+ SerializedBcs: () => SerializedBcs,
33
+ bigUIntBcsType: () => bigUIntBcsType,
34
+ dynamicSizeBcsType: () => dynamicSizeBcsType,
35
+ fixedSizeBcsType: () => fixedSizeBcsType,
36
+ isSerializedBcs: () => isSerializedBcs,
37
+ lazyBcsType: () => lazyBcsType,
38
+ stringLikeBcsType: () => stringLikeBcsType,
39
+ uIntBcsType: () => uIntBcsType
40
+ });
41
+ module.exports = __toCommonJS(bcs_type_exports);
42
+ var import_utils = require("@haneullabs/utils");
43
+ var import_reader = require("./reader.js");
44
+ var import_uleb = require("./uleb.js");
45
+ var import_writer = require("./writer.js");
46
+ var _write, _serialize, _schema, _bytes;
47
+ const _BcsType = class _BcsType {
48
+ constructor(options) {
49
+ __privateAdd(this, _write);
50
+ __privateAdd(this, _serialize);
51
+ this.name = options.name;
52
+ this.read = options.read;
53
+ this.serializedSize = options.serializedSize ?? (() => null);
54
+ __privateSet(this, _write, options.write);
55
+ __privateSet(this, _serialize, options.serialize ?? ((value, options2) => {
56
+ const writer = new import_writer.BcsWriter({
57
+ initialSize: this.serializedSize(value) ?? void 0,
58
+ ...options2
59
+ });
60
+ __privateGet(this, _write).call(this, value, writer);
61
+ return writer.toBytes();
62
+ }));
63
+ this.validate = options.validate ?? (() => {
64
+ });
65
+ }
66
+ write(value, writer) {
67
+ this.validate(value);
68
+ __privateGet(this, _write).call(this, value, writer);
69
+ }
70
+ serialize(value, options) {
71
+ this.validate(value);
72
+ return new SerializedBcs(this, __privateGet(this, _serialize).call(this, value, options));
73
+ }
74
+ parse(bytes) {
75
+ const reader = new import_reader.BcsReader(bytes);
76
+ return this.read(reader);
77
+ }
78
+ fromHex(hex) {
79
+ return this.parse((0, import_utils.fromHex)(hex));
80
+ }
81
+ fromBase58(b64) {
82
+ return this.parse((0, import_utils.fromBase58)(b64));
83
+ }
84
+ fromBase64(b64) {
85
+ return this.parse((0, import_utils.fromBase64)(b64));
86
+ }
87
+ transform({
88
+ name,
89
+ input,
90
+ output,
91
+ validate
92
+ }) {
93
+ return new _BcsType({
94
+ name: name ?? this.name,
95
+ read: (reader) => output ? output(this.read(reader)) : this.read(reader),
96
+ write: (value, writer) => __privateGet(this, _write).call(this, input ? input(value) : value, writer),
97
+ serializedSize: (value) => this.serializedSize(input ? input(value) : value),
98
+ serialize: (value, options) => __privateGet(this, _serialize).call(this, input ? input(value) : value, options),
99
+ validate: (value) => {
100
+ validate?.(value);
101
+ this.validate(input ? input(value) : value);
102
+ }
103
+ });
104
+ }
105
+ };
106
+ _write = new WeakMap();
107
+ _serialize = new WeakMap();
108
+ let BcsType = _BcsType;
109
+ const SERIALIZED_BCS_BRAND = Symbol.for("@haneullabs/serialized-bcs");
110
+ function isSerializedBcs(obj) {
111
+ return !!obj && typeof obj === "object" && obj[SERIALIZED_BCS_BRAND] === true;
112
+ }
113
+ class SerializedBcs {
114
+ constructor(schema, bytes) {
115
+ __privateAdd(this, _schema);
116
+ __privateAdd(this, _bytes);
117
+ __privateSet(this, _schema, schema);
118
+ __privateSet(this, _bytes, bytes);
119
+ }
120
+ // Used to brand SerializedBcs so that they can be identified, even between multiple copies
121
+ // of the @haneullabs/bcs package are installed
122
+ get [SERIALIZED_BCS_BRAND]() {
123
+ return true;
124
+ }
125
+ toBytes() {
126
+ return __privateGet(this, _bytes);
127
+ }
128
+ toHex() {
129
+ return (0, import_utils.toHex)(__privateGet(this, _bytes));
130
+ }
131
+ toBase64() {
132
+ return (0, import_utils.toBase64)(__privateGet(this, _bytes));
133
+ }
134
+ toBase58() {
135
+ return (0, import_utils.toBase58)(__privateGet(this, _bytes));
136
+ }
137
+ parse() {
138
+ return __privateGet(this, _schema).parse(__privateGet(this, _bytes));
139
+ }
140
+ }
141
+ _schema = new WeakMap();
142
+ _bytes = new WeakMap();
143
+ function fixedSizeBcsType({
144
+ size,
145
+ ...options
146
+ }) {
147
+ return new BcsType({
148
+ ...options,
149
+ serializedSize: () => size
150
+ });
151
+ }
152
+ function uIntBcsType({
153
+ readMethod,
154
+ writeMethod,
155
+ ...options
156
+ }) {
157
+ return fixedSizeBcsType({
158
+ ...options,
159
+ read: (reader) => reader[readMethod](),
160
+ write: (value, writer) => writer[writeMethod](value),
161
+ validate: (value) => {
162
+ if (value < 0 || value > options.maxValue) {
163
+ throw new TypeError(
164
+ `Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`
165
+ );
166
+ }
167
+ options.validate?.(value);
168
+ }
169
+ });
170
+ }
171
+ function bigUIntBcsType({
172
+ readMethod,
173
+ writeMethod,
174
+ ...options
175
+ }) {
176
+ return fixedSizeBcsType({
177
+ ...options,
178
+ read: (reader) => reader[readMethod](),
179
+ write: (value, writer) => writer[writeMethod](BigInt(value)),
180
+ validate: (val) => {
181
+ const value = BigInt(val);
182
+ if (value < 0 || value > options.maxValue) {
183
+ throw new TypeError(
184
+ `Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`
185
+ );
186
+ }
187
+ options.validate?.(value);
188
+ }
189
+ });
190
+ }
191
+ function dynamicSizeBcsType({
192
+ serialize,
193
+ ...options
194
+ }) {
195
+ const type = new BcsType({
196
+ ...options,
197
+ serialize,
198
+ write: (value, writer) => {
199
+ for (const byte of type.serialize(value).toBytes()) {
200
+ writer.write8(byte);
201
+ }
202
+ }
203
+ });
204
+ return type;
205
+ }
206
+ function stringLikeBcsType({
207
+ toBytes,
208
+ fromBytes,
209
+ ...options
210
+ }) {
211
+ return new BcsType({
212
+ ...options,
213
+ read: (reader) => {
214
+ const length = reader.readULEB();
215
+ const bytes = reader.readBytes(length);
216
+ return fromBytes(bytes);
217
+ },
218
+ write: (hex, writer) => {
219
+ const bytes = toBytes(hex);
220
+ writer.writeULEB(bytes.length);
221
+ for (let i = 0; i < bytes.length; i++) {
222
+ writer.write8(bytes[i]);
223
+ }
224
+ },
225
+ serialize: (value) => {
226
+ const bytes = toBytes(value);
227
+ const size = (0, import_uleb.ulebEncode)(bytes.length);
228
+ const result = new Uint8Array(size.length + bytes.length);
229
+ result.set(size, 0);
230
+ result.set(bytes, size.length);
231
+ return result;
232
+ },
233
+ validate: (value) => {
234
+ if (typeof value !== "string") {
235
+ throw new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);
236
+ }
237
+ options.validate?.(value);
238
+ }
239
+ });
240
+ }
241
+ function lazyBcsType(cb) {
242
+ let lazyType = null;
243
+ function getType() {
244
+ if (!lazyType) {
245
+ lazyType = cb();
246
+ }
247
+ return lazyType;
248
+ }
249
+ return new BcsType({
250
+ name: "lazy",
251
+ read: (data) => getType().read(data),
252
+ serializedSize: (value) => getType().serializedSize(value),
253
+ write: (value, writer) => getType().write(value, writer),
254
+ serialize: (value, options) => getType().serialize(value, options).toBytes()
255
+ });
256
+ }
257
+ class BcsStruct extends BcsType {
258
+ constructor({ name, fields, ...options }) {
259
+ const canonicalOrder = Object.entries(fields);
260
+ super({
261
+ name,
262
+ serializedSize: (values) => {
263
+ let total = 0;
264
+ for (const [field, type] of canonicalOrder) {
265
+ const size = type.serializedSize(values[field]);
266
+ if (size == null) {
267
+ return null;
268
+ }
269
+ total += size;
270
+ }
271
+ return total;
272
+ },
273
+ read: (reader) => {
274
+ const result = {};
275
+ for (const [field, type] of canonicalOrder) {
276
+ result[field] = type.read(reader);
277
+ }
278
+ return result;
279
+ },
280
+ write: (value, writer) => {
281
+ for (const [field, type] of canonicalOrder) {
282
+ type.write(value[field], writer);
283
+ }
284
+ },
285
+ ...options,
286
+ validate: (value) => {
287
+ options?.validate?.(value);
288
+ if (typeof value !== "object" || value == null) {
289
+ throw new TypeError(`Expected object, found ${typeof value}`);
290
+ }
291
+ }
292
+ });
293
+ }
294
+ }
295
+ class BcsEnum extends BcsType {
296
+ constructor({ fields, ...options }) {
297
+ const canonicalOrder = Object.entries(fields);
298
+ super({
299
+ read: (reader) => {
300
+ const index = reader.readULEB();
301
+ const enumEntry = canonicalOrder[index];
302
+ if (!enumEntry) {
303
+ throw new TypeError(`Unknown value ${index} for enum ${options.name}`);
304
+ }
305
+ const [kind, type] = enumEntry;
306
+ return {
307
+ [kind]: type?.read(reader) ?? true,
308
+ $kind: kind
309
+ };
310
+ },
311
+ write: (value, writer) => {
312
+ const [name, val] = Object.entries(value).filter(
313
+ ([name2]) => Object.hasOwn(fields, name2)
314
+ )[0];
315
+ for (let i = 0; i < canonicalOrder.length; i++) {
316
+ const [optionName, optionType] = canonicalOrder[i];
317
+ if (optionName === name) {
318
+ writer.writeULEB(i);
319
+ optionType?.write(val, writer);
320
+ return;
321
+ }
322
+ }
323
+ },
324
+ ...options,
325
+ validate: (value) => {
326
+ options?.validate?.(value);
327
+ if (typeof value !== "object" || value == null) {
328
+ throw new TypeError(`Expected object, found ${typeof value}`);
329
+ }
330
+ const keys = Object.keys(value).filter(
331
+ (k) => value[k] !== void 0 && Object.hasOwn(fields, k)
332
+ );
333
+ if (keys.length !== 1) {
334
+ throw new TypeError(
335
+ `Expected object with one key, but found ${keys.length} for type ${options.name}}`
336
+ );
337
+ }
338
+ const [variant] = keys;
339
+ if (!Object.hasOwn(fields, variant)) {
340
+ throw new TypeError(`Invalid enum variant ${variant}`);
341
+ }
342
+ }
343
+ });
344
+ }
345
+ }
346
+ class BcsTuple extends BcsType {
347
+ constructor({ fields, name, ...options }) {
348
+ super({
349
+ name: name ?? `(${fields.map((t) => t.name).join(", ")})`,
350
+ serializedSize: (values) => {
351
+ let total = 0;
352
+ for (let i = 0; i < fields.length; i++) {
353
+ const size = fields[i].serializedSize(values[i]);
354
+ if (size == null) {
355
+ return null;
356
+ }
357
+ total += size;
358
+ }
359
+ return total;
360
+ },
361
+ read: (reader) => {
362
+ const result = [];
363
+ for (const field of fields) {
364
+ result.push(field.read(reader));
365
+ }
366
+ return result;
367
+ },
368
+ write: (value, writer) => {
369
+ for (let i = 0; i < fields.length; i++) {
370
+ fields[i].write(value[i], writer);
371
+ }
372
+ },
373
+ ...options,
374
+ validate: (value) => {
375
+ options?.validate?.(value);
376
+ if (!Array.isArray(value)) {
377
+ throw new TypeError(`Expected array, found ${typeof value}`);
378
+ }
379
+ if (value.length !== fields.length) {
380
+ throw new TypeError(`Expected array of length ${fields.length}, found ${value.length}`);
381
+ }
382
+ }
383
+ });
384
+ }
385
+ }
386
+ //# 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 of length ${fields.length}, found ${value.length}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t}\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAA2E;AAC3E,oBAA0B;AAC1B,kBAA2B;AAE3B,oBAA0B;AAP1B;AAeO,MAAM,WAAN,MAAM,SAA0D;AAAA,EAUtE,YACC,SAQC;AAZF;AACA;AAYC,SAAK,OAAO,QAAQ;AACpB,SAAK,OAAO,QAAQ;AACpB,SAAK,iBAAiB,QAAQ,mBAAmB,MAAM;AACvD,uBAAK,QAAS,QAAQ;AACtB,uB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 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 {};