@haneullabs/bcs 2.0.3 → 2.1.2

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/dist/bcs.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  import { ulebEncode } from "./uleb.mjs";
2
2
  import { BcsEnum, BcsStruct, BcsTuple, BcsType, bigUIntBcsType, dynamicSizeBcsType, fixedSizeBcsType, lazyBcsType, stringLikeBcsType, uIntBcsType } from "./bcs-type.mjs";
3
-
4
3
  //#region src/bcs.ts
5
4
  function fixedArray(size, type, options) {
6
5
  return new BcsType({
@@ -86,16 +85,26 @@ function map(keyType, valueType) {
86
85
  });
87
86
  }
88
87
  const bcs = {
88
+ /**
89
+ * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
90
+ * @example
91
+ * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
92
+ */
89
93
  u8(options) {
90
94
  return uIntBcsType({
91
95
  readMethod: "read8",
92
96
  writeMethod: "write8",
93
97
  size: 1,
94
- maxValue: 2 ** 8 - 1,
98
+ maxValue: 255,
95
99
  ...options,
96
100
  name: options?.name ?? "u8"
97
101
  });
98
102
  },
103
+ /**
104
+ * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
105
+ * @example
106
+ * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
107
+ */
99
108
  u16(options) {
100
109
  return uIntBcsType({
101
110
  readMethod: "read16",
@@ -106,6 +115,11 @@ const bcs = {
106
115
  name: options?.name ?? "u16"
107
116
  });
108
117
  },
118
+ /**
119
+ * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
120
+ * @example
121
+ * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
122
+ */
109
123
  u32(options) {
110
124
  return uIntBcsType({
111
125
  readMethod: "read32",
@@ -116,6 +130,11 @@ const bcs = {
116
130
  name: options?.name ?? "u32"
117
131
  });
118
132
  },
133
+ /**
134
+ * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
135
+ * @example
136
+ * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
137
+ */
119
138
  u64(options) {
120
139
  return bigUIntBcsType({
121
140
  readMethod: "read64",
@@ -126,6 +145,11 @@ const bcs = {
126
145
  name: options?.name ?? "u64"
127
146
  });
128
147
  },
148
+ /**
149
+ * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
150
+ * @example
151
+ * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
152
+ */
129
153
  u128(options) {
130
154
  return bigUIntBcsType({
131
155
  readMethod: "read128",
@@ -136,6 +160,11 @@ const bcs = {
136
160
  name: options?.name ?? "u128"
137
161
  });
138
162
  },
163
+ /**
164
+ * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
165
+ * @example
166
+ * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
167
+ */
139
168
  u256(options) {
140
169
  return bigUIntBcsType({
141
170
  readMethod: "read256",
@@ -146,10 +175,19 @@ const bcs = {
146
175
  name: options?.name ?? "u256"
147
176
  });
148
177
  },
178
+ /**
179
+ * Creates a BcsType that can be used to read and write boolean values.
180
+ * @example
181
+ * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
182
+ */
149
183
  bool(options) {
150
184
  return fixedSizeBcsType({
151
185
  size: 1,
152
- read: (reader) => reader.read8() === 1,
186
+ read: (reader) => {
187
+ const value = reader.read8();
188
+ if (value > 1) throw new TypeError(`Invalid bool value: expected 0 or 1, found ${value}`);
189
+ return value === 1;
190
+ },
153
191
  write: (value, writer) => writer.write8(value ? 1 : 0),
154
192
  ...options,
155
193
  name: options?.name ?? "bool",
@@ -159,6 +197,11 @@ const bcs = {
159
197
  }
160
198
  });
161
199
  },
200
+ /**
201
+ * Creates a BcsType that can be used to read and write unsigned LEB encoded integers
202
+ * @example
203
+ *
204
+ */
162
205
  uleb128(options) {
163
206
  return dynamicSizeBcsType({
164
207
  read: (reader) => reader.readULEB(),
@@ -169,6 +212,12 @@ const bcs = {
169
212
  name: options?.name ?? "uleb128"
170
213
  });
171
214
  },
215
+ /**
216
+ * Creates a BcsType representing a fixed length byte array
217
+ * @param size The number of bytes this types represents
218
+ * @example
219
+ * bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
220
+ */
172
221
  bytes(size, options) {
173
222
  return fixedSizeBcsType({
174
223
  size,
@@ -185,6 +234,12 @@ const bcs = {
185
234
  }
186
235
  });
187
236
  },
237
+ /**
238
+ * Creates a BcsType representing a variable length byte array
239
+ *
240
+ * @example
241
+ * bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
242
+ */
188
243
  byteVector(options) {
189
244
  return new BcsType({
190
245
  read: (reader) => {
@@ -208,23 +263,72 @@ const bcs = {
208
263
  }
209
264
  });
210
265
  },
266
+ /**
267
+ * Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
268
+ * @example
269
+ * bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
270
+ */
211
271
  string(options) {
212
272
  return stringLikeBcsType({
213
273
  toBytes: (value) => new TextEncoder().encode(value),
214
- fromBytes: (bytes) => new TextDecoder().decode(bytes),
274
+ fromBytes: (bytes) => new TextDecoder("utf-8", {
275
+ fatal: true,
276
+ ignoreBOM: true
277
+ }).decode(bytes),
215
278
  ...options,
216
279
  name: options?.name ?? "string"
217
280
  });
218
281
  },
282
+ /**
283
+ * Creates a BcsType that represents a fixed length array of a given type
284
+ * @param size The number of elements in the array
285
+ * @param type The BcsType of each element in the array
286
+ * @example
287
+ * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
288
+ */
219
289
  fixedArray,
290
+ /**
291
+ * Creates a BcsType representing an optional value
292
+ * @param type The BcsType of the optional value
293
+ * @example
294
+ * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
295
+ * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
296
+ */
220
297
  option,
298
+ /**
299
+ * Creates a BcsType representing a variable length vector of a given type
300
+ * @param type The BcsType of each element in the vector
301
+ *
302
+ * @example
303
+ * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
304
+ */
221
305
  vector,
306
+ /**
307
+ * Creates a BcsType representing a tuple of a given set of types
308
+ * @param types The BcsTypes for each element in the tuple
309
+ *
310
+ * @example
311
+ * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
312
+ * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
313
+ */
222
314
  tuple(fields, options) {
223
315
  return new BcsTuple({
224
316
  fields,
225
317
  ...options
226
318
  });
227
319
  },
320
+ /**
321
+ * Creates a BcsType representing a struct of a given set of fields
322
+ * @param name The name of the struct
323
+ * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized
324
+ *
325
+ * @example
326
+ * const struct = bcs.struct('MyStruct', {
327
+ * a: bcs.u8(),
328
+ * b: bcs.string(),
329
+ * })
330
+ * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
331
+ */
228
332
  struct(name, fields, options) {
229
333
  return new BcsStruct({
230
334
  name,
@@ -232,6 +336,22 @@ const bcs = {
232
336
  ...options
233
337
  });
234
338
  },
339
+ /**
340
+ * Creates a BcsType representing an enum of a given set of options
341
+ * @param name The name of the enum
342
+ * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.
343
+ * null can be used to represent a variant with no data.
344
+ *
345
+ * @example
346
+ * const enum = bcs.enum('MyEnum', {
347
+ * A: bcs.u8(),
348
+ * B: bcs.string(),
349
+ * C: null,
350
+ * })
351
+ * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
352
+ * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
353
+ * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
354
+ */
235
355
  enum(name, fields, options) {
236
356
  return new BcsEnum({
237
357
  name,
@@ -239,12 +359,24 @@ const bcs = {
239
359
  ...options
240
360
  });
241
361
  },
362
+ /**
363
+ * Creates a BcsType representing a map of a given key and value type
364
+ * @param keyType The BcsType of the key
365
+ * @param valueType The BcsType of the value
366
+ * @example
367
+ * const map = bcs.map(bcs.u8(), bcs.string())
368
+ * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
369
+ */
242
370
  map,
371
+ /**
372
+ * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
373
+ * @param cb A callback that returns the BcsType
374
+ */
243
375
  lazy(cb) {
244
376
  return lazyBcsType(cb);
245
377
  }
246
378
  };
247
-
248
379
  //#endregion
249
380
  export { bcs, compareBcsBytes };
381
+
250
382
  //# sourceMappingURL=bcs.mjs.map
package/dist/bcs.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"bcs.mjs","names":[],"sources":["../src/bcs.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { BcsTypeOptions } from './bcs-type.js';\nimport {\n\tBcsEnum,\n\tBcsStruct,\n\tBcsTuple,\n\tBcsType,\n\tbigUIntBcsType,\n\tdynamicSizeBcsType,\n\tfixedSizeBcsType,\n\tlazyBcsType,\n\tstringLikeBcsType,\n\tuIntBcsType,\n} from './bcs-type.js';\nimport type {\n\tEnumInputShape,\n\tEnumOutputShape,\n\tInferBcsInput,\n\tInferBcsType,\n\tJoinString,\n} from './types.js';\nimport { ulebEncode } from './uleb.js';\n\nfunction fixedArray<T extends BcsType<any>, Name extends string = string>(\n\tsize: number,\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>;\nfunction fixedArray<T, Input, Name extends string = string>(\n\tsize: number,\n\ttype: BcsType<T, Input>,\n\toptions?: BcsTypeOptions<T[], Iterable<Input> & { length: number }, Name>,\n): BcsType<T[], Iterable<Input> & { length: number }, Name>;\nfunction fixedArray<T extends BcsType<any>, Name extends string = `${T['name']}[${number}]`>(\n\tsize: number,\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name> {\n\treturn new BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>({\n\t\tread: (reader) => {\n\t\t\tconst result: InferBcsType<T>[] = new Array(size);\n\t\t\tfor (let i = 0; i < size; i++) {\n\t\t\t\tresult[i] = type.read(reader);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\tfor (const item of value) {\n\t\t\t\ttype.write(item, writer);\n\t\t\t}\n\t\t},\n\t\t...options,\n\t\tname: (options?.name ?? `${type.name}[${size}]`) as Name,\n\t\tvalidate: (value) => {\n\t\t\toptions?.validate?.(value);\n\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t}\n\t\t\tif (value.length !== size) {\n\t\t\t\tthrow new TypeError(`Expected array of length ${size}, found ${value.length}`);\n\t\t\t}\n\t\t},\n\t});\n}\n\nfunction option<T extends BcsType<any>>(\n\ttype: T,\n): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`>;\nfunction option<T, Input, Name extends string = string>(\n\ttype: BcsType<T, Input, Name>,\n): BcsType<T | null, Input | null | undefined>;\nfunction option<T extends BcsType<any>>(\n\ttype: T,\n): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`> {\n\treturn bcs\n\t\t.enum(`Option<${type.name}>`, {\n\t\t\tNone: null,\n\t\t\tSome: type,\n\t\t})\n\t\t.transform({\n\t\t\tinput: (value: InferBcsInput<T> | null | undefined) => {\n\t\t\t\tif (value == null) {\n\t\t\t\t\treturn { None: true };\n\t\t\t\t}\n\n\t\t\t\treturn { Some: value };\n\t\t\t},\n\t\t\toutput: (value) => {\n\t\t\t\tif (value.$kind === 'Some') {\n\t\t\t\t\treturn value.Some as InferBcsType<T>;\n\t\t\t\t}\n\n\t\t\t\treturn null;\n\t\t\t},\n\t\t});\n}\n\nfunction vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>;\nfunction vector<T, Input, Name extends string = string>(\n\ttype: BcsType<T, Input, Name>,\n\toptions?: BcsTypeOptions<T[], Iterable<Input> & { length: number }, `vector<${Name}>`>,\n): BcsType<T[], Iterable<Input> & { length: number }, `vector<${Name}>`>;\nfunction vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name> {\n\treturn new BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>({\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst result: InferBcsType<T>[] = new Array(length);\n\t\t\tfor (let i = 0; i < length; i++) {\n\t\t\t\tresult[i] = type.read(reader);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\twriter.writeULEB(value.length);\n\t\t\tfor (const item of value) {\n\t\t\t\ttype.write(item, writer);\n\t\t\t}\n\t\t},\n\t\t...options,\n\t\tname: (options?.name ?? `vector<${type.name}>`) as Name,\n\t\tvalidate: (value) => {\n\t\t\toptions?.validate?.(value);\n\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t}\n\t\t},\n\t});\n}\n\n/**\n * Compares two byte arrays using lexicographic ordering.\n * This matches Rust's Ord implementation for Vec<u8>/[u8] which is used for BTreeMap key ordering.\n * Comparison is done byte-by-byte first, then by length if all compared bytes are equal.\n */\nexport function compareBcsBytes(a: Uint8Array, b: Uint8Array): number {\n\tfor (let i = 0; i < Math.min(a.length, b.length); i++) {\n\t\tif (a[i] !== b[i]) {\n\t\t\treturn a[i] - b[i];\n\t\t}\n\t}\n\n\treturn a.length - b.length;\n}\n\nfunction map<K extends BcsType<any>, V extends BcsType<any>>(\n\tkeyType: K,\n\tvalueType: V,\n): BcsType<\n\tMap<InferBcsType<K>, InferBcsType<V>>,\n\tMap<InferBcsInput<K>, InferBcsInput<V>>,\n\t`Map<${K['name']}, ${V['name']}>`\n>;\nfunction map<K, V, InputK = K, InputV = V>(\n\tkeyType: BcsType<K, InputK>,\n\tvalueType: BcsType<V, InputV>,\n): BcsType<Map<K, V>, Map<InputK, InputV>, `Map<${string}, ${string}>`>;\nfunction map<K extends BcsType<any>, V extends BcsType<any>>(\n\tkeyType: K,\n\tvalueType: V,\n): BcsType<\n\tMap<InferBcsType<K>, InferBcsType<V>>,\n\tMap<InferBcsInput<K>, InferBcsInput<V>>,\n\t`Map<${K['name']}, ${V['name']}>`\n> {\n\treturn new BcsType({\n\t\tname: `Map<${keyType.name}, ${valueType.name}>`,\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst result = new Map<InferBcsType<K>, InferBcsType<V>>();\n\t\t\tfor (let i = 0; i < length; i++) {\n\t\t\t\tresult.set(keyType.read(reader), valueType.read(reader));\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\tconst entries = [...value.entries()].map(\n\t\t\t\t([key, val]) => [keyType.serialize(key).toBytes(), val] as const,\n\t\t\t);\n\t\t\tentries.sort(([a], [b]) => compareBcsBytes(a, b));\n\n\t\t\twriter.writeULEB(entries.length);\n\t\t\tfor (const [keyBytes, val] of entries) {\n\t\t\t\twriter.writeBytes(keyBytes);\n\t\t\t\tvalueType.write(val, writer);\n\t\t\t}\n\t\t},\n\t});\n}\n\nexport const bcs = {\n\t/**\n\t * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.\n\t * @example\n\t * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]\n\t */\n\tu8(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read8',\n\t\t\twriteMethod: 'write8',\n\t\t\tsize: 1,\n\t\t\tmaxValue: 2 ** 8 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u8') as 'u8',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.\n\t * @example\n\t * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]\n\t */\n\tu16(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read16',\n\t\t\twriteMethod: 'write16',\n\t\t\tsize: 2,\n\t\t\tmaxValue: 2 ** 16 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u16') as 'u16',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.\n\t * @example\n\t * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]\n\t */\n\tu32(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read32',\n\t\t\twriteMethod: 'write32',\n\t\t\tsize: 4,\n\t\t\tmaxValue: 2 ** 32 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u32') as 'u32',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.\n\t * @example\n\t * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]\n\t */\n\tu64(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read64',\n\t\t\twriteMethod: 'write64',\n\t\t\tsize: 8,\n\t\t\tmaxValue: 2n ** 64n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u64') as 'u64',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.\n\t * @example\n\t * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]\n\t */\n\tu128(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read128',\n\t\t\twriteMethod: 'write128',\n\t\t\tsize: 16,\n\t\t\tmaxValue: 2n ** 128n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u128') as 'u128',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.\n\t * @example\n\t * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]\n\t */\n\tu256(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read256',\n\t\t\twriteMethod: 'write256',\n\t\t\tsize: 32,\n\t\t\tmaxValue: 2n ** 256n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u256') as 'u256',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write boolean values.\n\t * @example\n\t * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]\n\t */\n\tbool(options?: BcsTypeOptions<boolean>) {\n\t\treturn fixedSizeBcsType({\n\t\t\tsize: 1,\n\t\t\tread: (reader) => reader.read8() === 1,\n\t\t\twrite: (value, writer) => writer.write8(value ? 1 : 0),\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'bool') as 'bool',\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (typeof value !== 'boolean') {\n\t\t\t\t\tthrow new TypeError(`Expected boolean, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write unsigned LEB encoded integers\n\t * @example\n\t *\n\t */\n\tuleb128(options?: BcsTypeOptions<number>) {\n\t\treturn dynamicSizeBcsType({\n\t\t\tread: (reader) => reader.readULEB(),\n\t\t\tserialize: (value) => {\n\t\t\t\treturn Uint8Array.from(ulebEncode(value));\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'uleb128') as 'uleb128',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a fixed length byte array\n\t * @param size The number of bytes this types represents\n\t * @example\n\t * bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]\n\t */\n\tbytes<T extends number>(size: T, options?: BcsTypeOptions<Uint8Array, Iterable<number>>) {\n\t\treturn fixedSizeBcsType<Uint8Array, Iterable<number>, `bytes[${T}]`>({\n\t\t\tsize,\n\t\t\tread: (reader) => reader.readBytes(size),\n\t\t\twrite: (value, writer) => {\n\t\t\t\twriter.writeBytes(new Uint8Array(value));\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? `bytes[${size}]`) as `bytes[${T}]`,\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t\tif (value.length !== size) {\n\t\t\t\t\tthrow new TypeError(`Expected array of length ${size}, found ${value.length}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a variable length byte array\n\t *\n\t * @example\n\t * bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]\n\t */\n\tbyteVector(options?: BcsTypeOptions<Uint8Array, Iterable<number>>) {\n\t\treturn new BcsType<Uint8Array, Iterable<number>, 'vector<u8>'>({\n\t\t\tread: (reader) => {\n\t\t\t\tconst length = reader.readULEB();\n\n\t\t\t\treturn reader.readBytes(length);\n\t\t\t},\n\t\t\twrite: (value, writer) => {\n\t\t\t\tconst array = new Uint8Array(value);\n\t\t\t\twriter.writeULEB(array.length);\n\t\t\t\twriter.writeBytes(array);\n\t\t\t},\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'vector<u8>') as 'vector<u8>',\n\t\t\tserializedSize: (value) => {\n\t\t\t\tconst length = 'length' in value ? (value.length as number) : null;\n\t\t\t\treturn length == null ? null : ulebEncode(length).length + length;\n\t\t\t},\n\t\t\tvalidate: (value) => {\n\t\t\t\toptions?.validate?.(value);\n\t\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded\n\t * @example\n\t * bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]\n\t */\n\tstring(options?: BcsTypeOptions<string>) {\n\t\treturn stringLikeBcsType({\n\t\t\ttoBytes: (value) => new TextEncoder().encode(value),\n\t\t\tfromBytes: (bytes) => new TextDecoder().decode(bytes),\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'string') as 'string',\n\t\t});\n\t},\n\t/**\n\t * Creates a BcsType that represents a fixed length array of a given type\n\t * @param size The number of elements in the array\n\t * @param type The BcsType of each element in the array\n\t * @example\n\t * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]\n\t */\n\tfixedArray,\n\n\t/**\n\t * Creates a BcsType representing an optional value\n\t * @param type The BcsType of the optional value\n\t * @example\n\t * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]\n\t * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]\n\t */\n\toption,\n\n\t/**\n\t * Creates a BcsType representing a variable length vector of a given type\n\t * @param type The BcsType of each element in the vector\n\t *\n\t * @example\n\t * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]\n\t */\n\tvector,\n\n\t/**\n\t * Creates a BcsType representing a tuple of a given set of types\n\t * @param types The BcsTypes for each element in the tuple\n\t *\n\t * @example\n\t * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])\n\t * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]\n\t */\n\ttuple<\n\t\tconst T extends readonly BcsType<any, any>[],\n\t\tconst Name extends string =\n\t\t\t`(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`,\n\t>(\n\t\tfields: T,\n\t\toptions?: BcsTypeOptions<\n\t\t\t{\n\t\t\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t\t\t},\n\t\t\t{\n\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t\t\t},\n\t\t\tName\n\t\t>,\n\t) {\n\t\treturn new BcsTuple<T, Name>({\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a struct of a given set of fields\n\t * @param name The name of the struct\n\t * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized\n\t *\n\t * @example\n\t * const struct = bcs.struct('MyStruct', {\n\t * a: bcs.u8(),\n\t * b: bcs.string(),\n\t * })\n\t * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]\n\t */\n\tstruct<T extends Record<string, BcsType<any>>, const Name extends string = string>(\n\t\tname: Name,\n\t\tfields: T,\n\t\toptions?: Omit<\n\t\t\tBcsTypeOptions<\n\t\t\t\t{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t\t\t\t}\n\t\t\t>,\n\t\t\t'name'\n\t\t>,\n\t) {\n\t\treturn new BcsStruct<T>({\n\t\t\tname,\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing an enum of a given set of options\n\t * @param name The name of the enum\n\t * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.\n\t * null can be used to represent a variant with no data.\n\t *\n\t * @example\n\t * const enum = bcs.enum('MyEnum', {\n\t * A: bcs.u8(),\n\t * B: bcs.string(),\n\t * C: null,\n\t * })\n\t * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]\n\t * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]\n\t * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]\n\t */\n\tenum<T extends Record<string, BcsType<any> | null>, const Name extends string = string>(\n\t\tname: Name,\n\t\tfields: T,\n\t\toptions?: Omit<\n\t\t\tBcsTypeOptions<\n\t\t\t\tEnumOutputShape<{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;\n\t\t\t\t}>,\n\t\t\t\tEnumInputShape<{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t\t\t\t}>,\n\t\t\t\tName\n\t\t\t>,\n\t\t\t'name'\n\t\t>,\n\t) {\n\t\treturn new BcsEnum<T, Name>({\n\t\t\tname,\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a map of a given key and value type\n\t * @param keyType The BcsType of the key\n\t * @param valueType The BcsType of the value\n\t * @example\n\t * const map = bcs.map(bcs.u8(), bcs.string())\n\t * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]\n\t */\n\tmap,\n\n\t/**\n\t * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.\n\t * @param cb A callback that returns the BcsType\n\t */\n\tlazy<T extends BcsType<any>>(cb: () => T): T {\n\t\treturn lazyBcsType(cb) as T;\n\t},\n};\n"],"mappings":";;;;AAuCA,SAAS,WACR,MACA,MACA,SAKoF;AACpF,QAAO,IAAI,QAAkF;EAC5F,OAAO,WAAW;GACjB,MAAM,SAA4B,IAAI,MAAM,KAAK;AACjD,QAAK,IAAI,IAAI,GAAG,IAAI,MAAM,IACzB,QAAO,KAAK,KAAK,KAAK,OAAO;AAE9B,UAAO;;EAER,QAAQ,OAAO,WAAW;AACzB,QAAK,MAAM,QAAQ,MAClB,MAAK,MAAM,MAAM,OAAO;;EAG1B,GAAG;EACH,MAAO,SAAS,QAAQ,GAAG,KAAK,KAAK,GAAG,KAAK;EAC7C,WAAW,UAAU;AACpB,YAAS,WAAW,MAAM;AAC1B,OAAI,CAAC,SAAS,OAAO,UAAU,YAAY,EAAE,YAAY,OACxD,OAAM,IAAI,UAAU,yBAAyB,OAAO,QAAQ;AAE7D,OAAI,MAAM,WAAW,KACpB,OAAM,IAAI,UAAU,4BAA4B,KAAK,UAAU,MAAM,SAAS;;EAGhF,CAAC;;AASH,SAAS,OACR,MAC+F;AAC/F,QAAO,IACL,KAAK,UAAU,KAAK,KAAK,IAAI;EAC7B,MAAM;EACN,MAAM;EACN,CAAC,CACD,UAAU;EACV,QAAQ,UAA+C;AACtD,OAAI,SAAS,KACZ,QAAO,EAAE,MAAM,MAAM;AAGtB,UAAO,EAAE,MAAM,OAAO;;EAEvB,SAAS,UAAU;A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;AAC9D,WAAO,UAAU,OAAO,OAAO,WAAW,OAAO,CAAC,SAAS;;GAE5D,WAAW,UAAU;AACpB,aAAS,WAAW,MAAM;AAC1B,QAAI,CAAC,SAAS,OAAO,UAAU,YAAY,EAAE,YAAY,OACxD,OAAM,IAAI,UAAU,yBAAyB,OAAO,QAAQ;;GAG9D,CAAC;;CAQH,OAAO,SAAkC;AACxC,SAAO,kBAAkB;GACxB,UAAU,UAAU,IAAI,aAAa,CAAC,OAAO,MAAM;GACnD,YAAY,UAAU,IAAI,aAAa,CAAC,OAAO,MAAM;GACrD,GAAG;GACH,MAAO,SAAS,QAAQ;GACxB,CAAC;;CASH;CASA;CASA;CAUA,MAKC,QACA,SASC;AACD,SAAO,IAAI,SAAkB;GAC5B;GACA,GAAG;GACH,CAAC;;CAeH,OACC,MACA,QACA,SAWC;AACD,SAAO,IAAI,UAAa;GACvB;GACA;GACA,GAAG;GACH,CAAC;;CAmBH,KACC,MACA,QACA,SAYC;AACD,SAAO,IAAI,QAAiB;GAC3B;GACA;GACA,GAAG;GACH,CAAC;;CAWH;CAMA,KAA6B,IAAgB;AAC5C,SAAO,YAAY,GAAG;;CAEvB"}
1
+ {"version":3,"file":"bcs.mjs","names":[],"sources":["../src/bcs.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { BcsTypeOptions } from './bcs-type.js';\nimport {\n\tBcsEnum,\n\tBcsStruct,\n\tBcsTuple,\n\tBcsType,\n\tbigUIntBcsType,\n\tdynamicSizeBcsType,\n\tfixedSizeBcsType,\n\tlazyBcsType,\n\tstringLikeBcsType,\n\tuIntBcsType,\n} from './bcs-type.js';\nimport type {\n\tEnumInputShape,\n\tEnumOutputShape,\n\tInferBcsInput,\n\tInferBcsType,\n\tJoinString,\n} from './types.js';\nimport { ulebEncode } from './uleb.js';\n\nfunction fixedArray<T extends BcsType<any>, Name extends string = string>(\n\tsize: number,\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>;\nfunction fixedArray<T, Input, Name extends string = string>(\n\tsize: number,\n\ttype: BcsType<T, Input>,\n\toptions?: BcsTypeOptions<T[], Iterable<Input> & { length: number }, Name>,\n): BcsType<T[], Iterable<Input> & { length: number }, Name>;\nfunction fixedArray<T extends BcsType<any>, Name extends string = `${T['name']}[${number}]`>(\n\tsize: number,\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name> {\n\treturn new BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>({\n\t\tread: (reader) => {\n\t\t\tconst result: InferBcsType<T>[] = new Array(size);\n\t\t\tfor (let i = 0; i < size; i++) {\n\t\t\t\tresult[i] = type.read(reader);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\tfor (const item of value) {\n\t\t\t\ttype.write(item, writer);\n\t\t\t}\n\t\t},\n\t\t...options,\n\t\tname: (options?.name ?? `${type.name}[${size}]`) as Name,\n\t\tvalidate: (value) => {\n\t\t\toptions?.validate?.(value);\n\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t}\n\t\t\tif (value.length !== size) {\n\t\t\t\tthrow new TypeError(`Expected array of length ${size}, found ${value.length}`);\n\t\t\t}\n\t\t},\n\t});\n}\n\nfunction option<T extends BcsType<any>>(\n\ttype: T,\n): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`>;\nfunction option<T, Input, Name extends string = string>(\n\ttype: BcsType<T, Input, Name>,\n): BcsType<T | null, Input | null | undefined>;\nfunction option<T extends BcsType<any>>(\n\ttype: T,\n): BcsType<InferBcsType<T> | null, InferBcsInput<T> | null | undefined, `Option<${T['name']}>`> {\n\treturn bcs\n\t\t.enum(`Option<${type.name}>`, {\n\t\t\tNone: null,\n\t\t\tSome: type,\n\t\t})\n\t\t.transform({\n\t\t\tinput: (value: InferBcsInput<T> | null | undefined) => {\n\t\t\t\tif (value == null) {\n\t\t\t\t\treturn { None: true };\n\t\t\t\t}\n\n\t\t\t\treturn { Some: value };\n\t\t\t},\n\t\t\toutput: (value) => {\n\t\t\t\tif (value.$kind === 'Some') {\n\t\t\t\t\treturn value.Some as InferBcsType<T>;\n\t\t\t\t}\n\n\t\t\t\treturn null;\n\t\t\t},\n\t\t});\n}\n\nfunction vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>;\nfunction vector<T, Input, Name extends string = string>(\n\ttype: BcsType<T, Input, Name>,\n\toptions?: BcsTypeOptions<T[], Iterable<Input> & { length: number }, `vector<${Name}>`>,\n): BcsType<T[], Iterable<Input> & { length: number }, `vector<${Name}>`>;\nfunction vector<T extends BcsType<any>, Name extends string = `vector<${T['name']}>`>(\n\ttype: T,\n\toptions?: BcsTypeOptions<\n\t\tInferBcsType<T>[],\n\t\tIterable<InferBcsInput<T>> & { length: number },\n\t\tName\n\t>,\n): BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name> {\n\treturn new BcsType<InferBcsType<T>[], Iterable<InferBcsInput<T>> & { length: number }, Name>({\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst result: InferBcsType<T>[] = new Array(length);\n\t\t\tfor (let i = 0; i < length; i++) {\n\t\t\t\tresult[i] = type.read(reader);\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\twriter.writeULEB(value.length);\n\t\t\tfor (const item of value) {\n\t\t\t\ttype.write(item, writer);\n\t\t\t}\n\t\t},\n\t\t...options,\n\t\tname: (options?.name ?? `vector<${type.name}>`) as Name,\n\t\tvalidate: (value) => {\n\t\t\toptions?.validate?.(value);\n\t\t\tif (!value || typeof value !== 'object' || !('length' in value)) {\n\t\t\t\tthrow new TypeError(`Expected array, found ${typeof value}`);\n\t\t\t}\n\t\t},\n\t});\n}\n\n/**\n * Compares two byte arrays using lexicographic ordering.\n * This matches Rust's Ord implementation for Vec<u8>/[u8] which is used for BTreeMap key ordering.\n * Comparison is done byte-by-byte first, then by length if all compared bytes are equal.\n */\nexport function compareBcsBytes(a: Uint8Array, b: Uint8Array): number {\n\tfor (let i = 0; i < Math.min(a.length, b.length); i++) {\n\t\tif (a[i] !== b[i]) {\n\t\t\treturn a[i] - b[i];\n\t\t}\n\t}\n\n\treturn a.length - b.length;\n}\n\nfunction map<K extends BcsType<any>, V extends BcsType<any>>(\n\tkeyType: K,\n\tvalueType: V,\n): BcsType<\n\tMap<InferBcsType<K>, InferBcsType<V>>,\n\tMap<InferBcsInput<K>, InferBcsInput<V>>,\n\t`Map<${K['name']}, ${V['name']}>`\n>;\nfunction map<K, V, InputK = K, InputV = V>(\n\tkeyType: BcsType<K, InputK>,\n\tvalueType: BcsType<V, InputV>,\n): BcsType<Map<K, V>, Map<InputK, InputV>, `Map<${string}, ${string}>`>;\nfunction map<K extends BcsType<any>, V extends BcsType<any>>(\n\tkeyType: K,\n\tvalueType: V,\n): BcsType<\n\tMap<InferBcsType<K>, InferBcsType<V>>,\n\tMap<InferBcsInput<K>, InferBcsInput<V>>,\n\t`Map<${K['name']}, ${V['name']}>`\n> {\n\treturn new BcsType({\n\t\tname: `Map<${keyType.name}, ${valueType.name}>`,\n\t\tread: (reader) => {\n\t\t\tconst length = reader.readULEB();\n\t\t\tconst result = new Map<InferBcsType<K>, InferBcsType<V>>();\n\t\t\tfor (let i = 0; i < length; i++) {\n\t\t\t\tresult.set(keyType.read(reader), valueType.read(reader));\n\t\t\t}\n\t\t\treturn result;\n\t\t},\n\t\twrite: (value, writer) => {\n\t\t\tconst entries = [...value.entries()].map(\n\t\t\t\t([key, val]) => [keyType.serialize(key).toBytes(), val] as const,\n\t\t\t);\n\t\t\tentries.sort(([a], [b]) => compareBcsBytes(a, b));\n\n\t\t\twriter.writeULEB(entries.length);\n\t\t\tfor (const [keyBytes, val] of entries) {\n\t\t\t\twriter.writeBytes(keyBytes);\n\t\t\t\tvalueType.write(val, writer);\n\t\t\t}\n\t\t},\n\t});\n}\n\nexport const bcs = {\n\t/**\n\t * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.\n\t * @example\n\t * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]\n\t */\n\tu8(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read8',\n\t\t\twriteMethod: 'write8',\n\t\t\tsize: 1,\n\t\t\tmaxValue: 2 ** 8 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u8') as 'u8',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.\n\t * @example\n\t * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]\n\t */\n\tu16(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read16',\n\t\t\twriteMethod: 'write16',\n\t\t\tsize: 2,\n\t\t\tmaxValue: 2 ** 16 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u16') as 'u16',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.\n\t * @example\n\t * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]\n\t */\n\tu32(options?: BcsTypeOptions<number>) {\n\t\treturn uIntBcsType({\n\t\t\treadMethod: 'read32',\n\t\t\twriteMethod: 'write32',\n\t\t\tsize: 4,\n\t\t\tmaxValue: 2 ** 32 - 1,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u32') as 'u32',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.\n\t * @example\n\t * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]\n\t */\n\tu64(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read64',\n\t\t\twriteMethod: 'write64',\n\t\t\tsize: 8,\n\t\t\tmaxValue: 2n ** 64n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u64') as 'u64',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.\n\t * @example\n\t * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]\n\t */\n\tu128(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read128',\n\t\t\twriteMethod: 'write128',\n\t\t\tsize: 16,\n\t\t\tmaxValue: 2n ** 128n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u128') as 'u128',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.\n\t * @example\n\t * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]\n\t */\n\tu256(options?: BcsTypeOptions<string, number | bigint | string>) {\n\t\treturn bigUIntBcsType({\n\t\t\treadMethod: 'read256',\n\t\t\twriteMethod: 'write256',\n\t\t\tsize: 32,\n\t\t\tmaxValue: 2n ** 256n - 1n,\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'u256') as 'u256',\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType that can be used to read and write boolean values.\n\t * @example\n\t * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]\n\t */\n\tbool(options?: BcsTypeOptions<boolean>) {\n\t\treturn fixedSizeBcsType({\n\t\t\tsize: 1,\n\t\t\tread: (reader) => {\n\t\t\t\tconst value = reader.read8();\n\t\t\t\tif (value > 1) {\n\t\t\t\t\tthrow new TypeError(`Invalid bool value: expected 0 or 1, found ${value}`);\n\t\t\t\t}\n\t\t\t\treturn value === 1;\n\t\t\t},\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) =>\n\t\t\t\tnew TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes),\n\t\t\t...options,\n\t\t\tname: (options?.name ?? 'string') as 'string',\n\t\t});\n\t},\n\t/**\n\t * Creates a BcsType that represents a fixed length array of a given type\n\t * @param size The number of elements in the array\n\t * @param type The BcsType of each element in the array\n\t * @example\n\t * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]\n\t */\n\tfixedArray,\n\n\t/**\n\t * Creates a BcsType representing an optional value\n\t * @param type The BcsType of the optional value\n\t * @example\n\t * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]\n\t * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]\n\t */\n\toption,\n\n\t/**\n\t * Creates a BcsType representing a variable length vector of a given type\n\t * @param type The BcsType of each element in the vector\n\t *\n\t * @example\n\t * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]\n\t */\n\tvector,\n\n\t/**\n\t * Creates a BcsType representing a tuple of a given set of types\n\t * @param types The BcsTypes for each element in the tuple\n\t *\n\t * @example\n\t * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])\n\t * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]\n\t */\n\ttuple<\n\t\tconst T extends readonly BcsType<any, any>[],\n\t\tconst Name extends string =\n\t\t\t`(${JoinString<{ [K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never }, ', '>})`,\n\t>(\n\t\tfields: T,\n\t\toptions?: BcsTypeOptions<\n\t\t\t{\n\t\t\t\t-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;\n\t\t\t},\n\t\t\t{\n\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;\n\t\t\t},\n\t\t\tName\n\t\t>,\n\t) {\n\t\treturn new BcsTuple<T, Name>({\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a struct of a given set of fields\n\t * @param name The name of the struct\n\t * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized\n\t *\n\t * @example\n\t * const struct = bcs.struct('MyStruct', {\n\t * a: bcs.u8(),\n\t * b: bcs.string(),\n\t * })\n\t * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]\n\t */\n\tstruct<T extends Record<string, BcsType<any>>, const Name extends string = string>(\n\t\tname: Name,\n\t\tfields: T,\n\t\toptions?: Omit<\n\t\t\tBcsTypeOptions<\n\t\t\t\t{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;\n\t\t\t\t}\n\t\t\t>,\n\t\t\t'name'\n\t\t>,\n\t) {\n\t\treturn new BcsStruct<T>({\n\t\t\tname,\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing an enum of a given set of options\n\t * @param name The name of the enum\n\t * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.\n\t * null can be used to represent a variant with no data.\n\t *\n\t * @example\n\t * const enum = bcs.enum('MyEnum', {\n\t * A: bcs.u8(),\n\t * B: bcs.string(),\n\t * C: null,\n\t * })\n\t * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]\n\t * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]\n\t * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]\n\t */\n\tenum<T extends Record<string, BcsType<any> | null>, const Name extends string = string>(\n\t\tname: Name,\n\t\tfields: T,\n\t\toptions?: Omit<\n\t\t\tBcsTypeOptions<\n\t\t\t\tEnumOutputShape<{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;\n\t\t\t\t}>,\n\t\t\t\tEnumInputShape<{\n\t\t\t\t\t[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;\n\t\t\t\t}>,\n\t\t\t\tName\n\t\t\t>,\n\t\t\t'name'\n\t\t>,\n\t) {\n\t\treturn new BcsEnum<T, Name>({\n\t\t\tname,\n\t\t\tfields,\n\t\t\t...options,\n\t\t});\n\t},\n\n\t/**\n\t * Creates a BcsType representing a map of a given key and value type\n\t * @param keyType The BcsType of the key\n\t * @param valueType The BcsType of the value\n\t * @example\n\t * const map = bcs.map(bcs.u8(), bcs.string())\n\t * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]\n\t */\n\tmap,\n\n\t/**\n\t * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.\n\t * @param cb A callback that returns the BcsType\n\t */\n\tlazy<T extends BcsType<any>>(cb: () => T): T {\n\t\treturn lazyBcsType(cb) as T;\n\t},\n};\n"],"mappings":";;;AAuCA,SAAS,WACR,MACA,MACA,SAKoF;CACpF,OAAO,IAAI,QAAkF;EAC5F,OAAO,WAAW;GACjB,MAAM,SAA4B,IAAI,MAAM,IAAI;GAChD,KAAK,IAAI,IAAI,GAAG,IAAI,MAAM,KACzB,OAAO,KAAK,KAAK,KAAK,MAAM;GAE7B,OAAO;EACR;EACA,QAAQ,OAAO,WAAW;GACzB,KAAK,MAAM,QAAQ,OAClB,KAAK,MAAM,MAAM,MAAM;EAEzB;EACA,GAAG;EACH,MAAO,SAAS,QAAQ,GAAG,KAAK,KAAK,GAAG,KAAK;EAC7C,WAAW,UAAU;GACpB,SAAS,WAAW,KAAK;GACzB,IAAI,CAAC,SAAS,OAAO,UAAU,YAAY,EAAE,YAAY,QACxD,MAAM,IAAI,UAAU,yBAAyB,OAAO,OAAO;GAE5D,IAAI,MAAM,WAAW,MACpB,MAAM,IAAI,UAAU,4BAA4B,KAAK,UAAU,MAAM,Q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CACF;;;;;;;;;;;;;;;;;CAkBA,KACC,MACA,QACA,SAYC;EACD,OAAO,IAAI,QAAiB;GAC3B;GACA;GACA,GAAG;EACJ,CAAC;CACF;;;;;;;;;CAUA;;;;;CAMA,KAA6B,IAAgB;EAC5C,OAAO,YAAY,EAAE;CACtB;AACD"}
package/dist/index.mjs CHANGED
@@ -4,5 +4,4 @@ import { BcsWriter } from "./writer.mjs";
4
4
  import { BcsEnum, BcsStruct, BcsTuple, BcsType, SerializedBcs, isSerializedBcs } from "./bcs-type.mjs";
5
5
  import { bcs, compareBcsBytes } from "./bcs.mjs";
6
6
  import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@haneullabs/utils";
7
-
8
- export { BcsEnum, BcsReader, BcsStruct, BcsTuple, BcsType, BcsWriter, SerializedBcs, bcs, compareBcsBytes, decodeStr, encodeStr, fromBase58, fromBase64, fromHex, isSerializedBcs, splitGenericParameters, toBase58, toBase64, toHex };
7
+ export { BcsEnum, BcsReader, BcsStruct, BcsTuple, BcsType, BcsWriter, SerializedBcs, bcs, compareBcsBytes, decodeStr, encodeStr, fromBase58, fromBase64, fromHex, isSerializedBcs, splitGenericParameters, toBase58, toBase64, toHex };
@@ -1 +1 @@
1
- {"version":3,"file":"reader.d.mts","names":[],"sources":["../src/reader.ts"],"mappings":";;AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAAa,SAAA;EAAA,QACJ,QAAA;EAAA,QACA,YAAA;EA8GyD;;;cAzGrD,IAAA,EAAM,UAAA;;;;;;;EASlB,KAAA,CAAM,KAAA;;;;;EAQN,KAAA,CAAA;;;;;EASA,MAAA,CAAA;;;;;EASA,MAAA,CAAA;;;;;EASA,MAAA,CAAA;;;;EAWA,OAAA,CAAA;;;;;EAWA,OAAA,CAAA;;;;;EAWA,SAAA,CAAU,GAAA,WAAc,UAAA;;;;;;EAaxB,QAAA,CAAA;;;;;;;EAeA,OAAA,CAAQ,EAAA,GAAK,MAAA,EAAQ,SAAA,EAAW,CAAA,UAAW,MAAA;AAAA"}
1
+ {"version":3,"file":"reader.d.mts","names":[],"sources":["../src/reader.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAmCa;UACJ;UACA;;;;EAKR,YAAY,MAAM;;;;;;;EASlB,MAAM;;;;;EAQN;;;;;EASA;;;;;EASA;;;;;EASA;;;;EAWA;;;;;EAWA;;;;;EAWA,UAAU,cAAc;;;;;;EAaxB;;;;;;;EAeA,QAAQ,KAAK,QAAQ,WAAW,WAAW"}
package/dist/reader.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  import { ulebDecode } from "./uleb.mjs";
2
-
3
2
  //#region src/reader.ts
4
3
  /**
5
4
  * Class used for reading BCS data chunk by chunk. Meant to be used
@@ -119,7 +118,8 @@ var BcsReader = class {
119
118
  */
120
119
  readULEB() {
121
120
  const start = this.bytePosition + this.dataView.byteOffset;
122
- const { value, length } = ulebDecode(new Uint8Array(this.dataView.buffer, start));
121
+ const buffer = new Uint8Array(this.dataView.buffer, start);
122
+ const { value, length } = ulebDecode(buffer);
123
123
  this.shift(length);
124
124
  return value;
125
125
  }
@@ -136,7 +136,7 @@ var BcsReader = class {
136
136
  return result;
137
137
  }
138
138
  };
139
-
140
139
  //#endregion
141
140
  export { BcsReader };
141
+
142
142
  //# sourceMappingURL=reader.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"reader.mjs","names":[],"sources":["../src/reader.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { ulebDecode } from './uleb.js';\n\n/**\n * Class used for reading BCS data chunk by chunk. Meant to be used\n * by some wrapper, which will make sure that data is valid and is\n * matching the desired format.\n *\n * @example\n * // data for this example is:\n * // { a: u8, b: u32, c: bool, d: u64 }\n *\n * let reader = new BcsReader(\"647f1a060001ffffe7890423c78a050102030405\");\n * let field1 = reader.read8();\n * let field2 = reader.read32();\n * let field3 = reader.read8() === '1'; // bool\n * let field4 = reader.read64();\n * // ....\n *\n * Reading vectors is another deal in bcs. To read a vector, you first need to read\n * its length using {@link readULEB}. Here's an example:\n * @example\n * // data encoded: { field: [1, 2, 3, 4, 5] }\n * let reader = new BcsReader(\"050102030405\");\n * let vec_length = reader.readULEB();\n * let elements = [];\n * for (let i = 0; i < vec_length; i++) {\n * elements.push(reader.read8());\n * }\n * console.log(elements); // [1,2,3,4,5]\n *\n * @param {String} data HEX-encoded data (serialized BCS)\n */\nexport class BcsReader {\n\tprivate dataView: DataView;\n\tprivate bytePosition: number = 0;\n\n\t/**\n\t * @param {Uint8Array} data Data to use as a buffer.\n\t */\n\tconstructor(data: Uint8Array) {\n\t\tthis.dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);\n\t}\n\t/**\n\t * Shift current cursor position by `bytes`.\n\t *\n\t * @param {Number} bytes Number of bytes to\n\t * @returns {this} Self for possible chaining.\n\t */\n\tshift(bytes: number) {\n\t\tthis.bytePosition += bytes;\n\t\treturn this;\n\t}\n\t/**\n\t * Read U8 value from the buffer and shift cursor by 1.\n\t * @returns\n\t */\n\tread8(): number {\n\t\tconst value = this.dataView.getUint8(this.bytePosition);\n\t\tthis.shift(1);\n\t\treturn value;\n\t}\n\t/**\n\t * Read U16 value from the buffer and shift cursor by 2.\n\t * @returns\n\t */\n\tread16(): number {\n\t\tconst value = this.dataView.getUint16(this.bytePosition, true);\n\t\tthis.shift(2);\n\t\treturn value;\n\t}\n\t/**\n\t * Read U32 value from the buffer and shift cursor by 4.\n\t * @returns\n\t */\n\tread32(): number {\n\t\tconst value = this.dataView.getUint32(this.bytePosition, true);\n\t\tthis.shift(4);\n\t\treturn value;\n\t}\n\t/**\n\t * Read U64 value from the buffer and shift cursor by 8.\n\t * @returns\n\t */\n\tread64(): string {\n\t\tconst value1 = this.read32();\n\t\tconst value2 = this.read32();\n\n\t\tconst result = value2.toString(16) + value1.toString(16).padStart(8, '0');\n\n\t\treturn BigInt('0x' + result).toString(10);\n\t}\n\t/**\n\t * Read U128 value from the buffer and shift cursor by 16.\n\t */\n\tread128(): string {\n\t\tconst value1 = BigInt(this.read64());\n\t\tconst value2 = BigInt(this.read64());\n\t\tconst result = value2.toString(16) + value1.toString(16).padStart(16, '0');\n\n\t\treturn BigInt('0x' + result).toString(10);\n\t}\n\t/**\n\t * Read U128 value from the buffer and shift cursor by 32.\n\t * @returns\n\t */\n\tread256(): string {\n\t\tconst value1 = BigInt(this.read128());\n\t\tconst value2 = BigInt(this.read128());\n\t\tconst result = value2.toString(16) + value1.toString(16).padStart(32, '0');\n\n\t\treturn BigInt('0x' + result).toString(10);\n\t}\n\t/**\n\t * Read `num` number of bytes from the buffer and shift cursor by `num`.\n\t * @param num Number of bytes to read.\n\t */\n\treadBytes(num: number): Uint8Array {\n\t\tconst start = this.bytePosition + this.dataView.byteOffset;\n\t\tconst value = new Uint8Array(this.dataView.buffer, start, num);\n\n\t\tthis.shift(num);\n\n\t\treturn value;\n\t}\n\t/**\n\t * Read ULEB value - an integer of varying size. Used for enum indexes and\n\t * vector lengths.\n\t * @returns {Number} The ULEB value.\n\t */\n\treadULEB(): number {\n\t\tconst start = this.bytePosition + this.dataView.byteOffset;\n\t\tconst buffer = new Uint8Array(this.dataView.buffer, start);\n\t\tconst { value, length } = ulebDecode(buffer);\n\n\t\tthis.shift(length);\n\n\t\treturn value;\n\t}\n\t/**\n\t * Read a BCS vector: read a length and then apply function `cb` X times\n\t * where X is the length of the vector, defined as ULEB in BCS bytes.\n\t * @param cb Callback to process elements of vector.\n\t * @returns {Array<Any>} Array of the resulting values, returned by callback.\n\t */\n\treadVec(cb: (reader: BcsReader, i: number, length: number) => any): any[] {\n\t\tconst length = this.readULEB();\n\t\tconst result = [];\n\t\tfor (let i = 0; i < length; i++) {\n\t\t\tresult.push(cb(this, i, length));\n\t\t}\n\t\treturn result;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,IAAa,YAAb,MAAuB;;;;CAOtB,YAAY,MAAkB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E9D,OAAK,MAAM,IAAI;AAEf,SAAO;;;;;;;CAOR,WAAmB;EAClB,MAAM,QAAQ,KAAK,eAAe,KAAK,SAAS;EAEhD,MAAM,EAAE,OAAO,WAAW,WADX,IAAI,WAAW,KAAK,SAAS,QAAQ,MAAM,CACd;AAE5C,OAAK,MAAM,OAAO;AAElB,SAAO;;;;;;;;CAQR,QAAQ,IAAkE;EACzE,MAAM,SAAS,KAAK,UAAU;EAC9B,MAAM,SAAS,EAAE;AACjB,OAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,IAC3B,QAAO,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC;AAEjC,SAAO"}
1
+ {"version":3,"file":"reader.mjs","names":[],"sources":["../src/reader.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { ulebDecode } from './uleb.js';\n\n/**\n * Class used for reading BCS data chunk by chunk. Meant to be used\n * by some wrapper, which will make sure that data is valid and is\n * matching the desired format.\n *\n * @example\n * // data for this example is:\n * // { a: u8, b: u32, c: bool, d: u64 }\n *\n * let reader = new BcsReader(\"647f1a060001ffffe7890423c78a050102030405\");\n * let field1 = reader.read8();\n * let field2 = reader.read32();\n * let field3 = reader.read8() === '1'; // bool\n * let field4 = reader.read64();\n * // ....\n *\n * Reading vectors is another deal in bcs. To read a vector, you first need to read\n * its length using {@link readULEB}. Here's an example:\n * @example\n * // data encoded: { field: [1, 2, 3, 4, 5] }\n * let reader = new BcsReader(\"050102030405\");\n * let vec_length = reader.readULEB();\n * let elements = [];\n * for (let i = 0; i < vec_length; i++) {\n * elements.push(reader.read8());\n * }\n * console.log(elements); // [1,2,3,4,5]\n *\n * @param {String} data HEX-encoded data (serialized BCS)\n */\nexport class BcsReader {\n\tprivate dataView: DataView;\n\tprivate bytePosition: number = 0;\n\n\t/**\n\t * @param {Uint8Array} data Data to use as a buffer.\n\t */\n\tconstructor(data: Uint8Array) {\n\t\tthis.dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);\n\t}\n\t/**\n\t * Shift current cursor position by `bytes`.\n\t *\n\t * @param {Number} bytes Number of bytes to\n\t * @returns {this} Self for possible chaining.\n\t */\n\tshift(bytes: number) {\n\t\tthis.bytePosition += bytes;\n\t\treturn this;\n\t}\n\t/**\n\t * Read U8 value from the buffer and shift cursor by 1.\n\t * @returns\n\t */\n\tread8(): number {\n\t\tconst value = this.dataView.getUint8(this.bytePosition);\n\t\tthis.shift(1);\n\t\treturn value;\n\t}\n\t/**\n\t * Read U16 value from the buffer and shift cursor by 2.\n\t * @returns\n\t */\n\tread16(): number {\n\t\tconst value = this.dataView.getUint16(this.bytePosition, true);\n\t\tthis.shift(2);\n\t\treturn value;\n\t}\n\t/**\n\t * Read U32 value from the buffer and shift cursor by 4.\n\t * @returns\n\t */\n\tread32(): number {\n\t\tconst value = this.dataView.getUint32(this.bytePosition, true);\n\t\tthis.shift(4);\n\t\treturn value;\n\t}\n\t/**\n\t * Read U64 value from the buffer and shift cursor by 8.\n\t * @returns\n\t */\n\tread64(): string {\n\t\tconst value1 = this.read32();\n\t\tconst value2 = this.read32();\n\n\t\tconst result = value2.toString(16) + value1.toString(16).padStart(8, '0');\n\n\t\treturn BigInt('0x' + result).toString(10);\n\t}\n\t/**\n\t * Read U128 value from the buffer and shift cursor by 16.\n\t */\n\tread128(): string {\n\t\tconst value1 = BigInt(this.read64());\n\t\tconst value2 = BigInt(this.read64());\n\t\tconst result = value2.toString(16) + value1.toString(16).padStart(16, '0');\n\n\t\treturn BigInt('0x' + result).toString(10);\n\t}\n\t/**\n\t * Read U128 value from the buffer and shift cursor by 32.\n\t * @returns\n\t */\n\tread256(): string {\n\t\tconst value1 = BigInt(this.read128());\n\t\tconst value2 = BigInt(this.read128());\n\t\tconst result = value2.toString(16) + value1.toString(16).padStart(32, '0');\n\n\t\treturn BigInt('0x' + result).toString(10);\n\t}\n\t/**\n\t * Read `num` number of bytes from the buffer and shift cursor by `num`.\n\t * @param num Number of bytes to read.\n\t */\n\treadBytes(num: number): Uint8Array {\n\t\tconst start = this.bytePosition + this.dataView.byteOffset;\n\t\tconst value = new Uint8Array(this.dataView.buffer, start, num);\n\n\t\tthis.shift(num);\n\n\t\treturn value;\n\t}\n\t/**\n\t * Read ULEB value - an integer of varying size. Used for enum indexes and\n\t * vector lengths.\n\t * @returns {Number} The ULEB value.\n\t */\n\treadULEB(): number {\n\t\tconst start = this.bytePosition + this.dataView.byteOffset;\n\t\tconst buffer = new Uint8Array(this.dataView.buffer, start);\n\t\tconst { value, length } = ulebDecode(buffer);\n\n\t\tthis.shift(length);\n\n\t\treturn value;\n\t}\n\t/**\n\t * Read a BCS vector: read a length and then apply function `cb` X times\n\t * where X is the length of the vector, defined as ULEB in BCS bytes.\n\t * @param cb Callback to process elements of vector.\n\t * @returns {Array<Any>} Array of the resulting values, returned by callback.\n\t */\n\treadVec(cb: (reader: BcsReader, i: number, length: number) => any): any[] {\n\t\tconst length = this.readULEB();\n\t\tconst result = [];\n\t\tfor (let i = 0; i < length; i++) {\n\t\t\tresult.push(cb(this, i, length));\n\t\t}\n\t\treturn result;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,IAAa,YAAb,MAAuB;;;;CAOtB,YAAY,MAAkB;EALC,KAAA,eAAA;EAM9B,KAAK,WAAW,IAAI,SAAS,KAAK,QAAQ,KAAK,YAAY,KAAK,UAAU;CAC3E;;;;;;;CAOA,MAAM,OAAe;EACpB,KAAK,gBAAgB;EACrB,OAAO;CACR;;;;;CAKA,QAAgB;EACf,MAAM,QAAQ,KAAK,SAAS,SAAS,KAAK,YAAY;EACtD,KAAK,MAAM,CAAC;EACZ,OAAO;CACR;;;;;CAKA,SAAiB;EAChB,MAAM,QAAQ,KAAK,SAAS,UAAU,KAAK,cAAc,IAAI;EAC7D,KAAK,MAAM,CAAC;EACZ,OAAO;CACR;;;;;CAKA,SAAiB;EAChB,MAAM,QAAQ,KAAK,SAAS,UAAU,KAA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,IAAI,GAAG,IAAI,QAAQ,KAC3B,OAAO,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;EAEhC,OAAO;CACR;AACD"}
package/dist/types.d.mts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { BcsType } from "./bcs-type.mjs";
2
2
  import { Simplify } from "@haneullabs/utils";
3
-
4
3
  //#region src/types.d.ts
5
4
  /**
6
5
  * Supported encodings.
@@ -9,11 +8,11 @@ import { Simplify } from "@haneullabs/utils";
9
8
  type Encoding = 'base58' | 'base64' | 'hex';
10
9
  type InferBcsType<T extends BcsType<any>> = T extends BcsType<infer U, any> ? U : never;
11
10
  type InferBcsInput<T extends BcsType<any, any>> = T extends BcsType<any, infer U> ? U : never;
12
- type EnumOutputShape<T extends Record<string, unknown>, Keys extends string = Extract<keyof T, string>, Values = (T[keyof T] extends infer Type ? (Type extends BcsType<infer U> ? U : never) : never)> = 0 extends Values ? EnumOutputShapeWithKeys<T, never> : 0n extends Values ? EnumOutputShapeWithKeys<T, never> : '' extends Values ? EnumOutputShapeWithKeys<T, never> : false extends Values ? EnumOutputShapeWithKeys<T, never> : EnumOutputShapeWithKeys<T, Keys>;
13
- type EnumOutputShapeWithKeys<T extends Record<string, unknown>, Keys extends string> = { [K in keyof T]: Exclude<Keys, K> extends infer Empty extends string ? Simplify<{ [K2 in K]: T[K] } & { [K in Empty]?: never } & {
11
+ type EnumOutputShape<T extends Record<string, unknown>, Keys extends string = Extract<keyof T, string>, Values = T[keyof T] extends (infer Type) ? (Type extends BcsType<infer U> ? U : never) : never> = 0 extends Values ? EnumOutputShapeWithKeys<T, never> : 0n extends Values ? EnumOutputShapeWithKeys<T, never> : '' extends Values ? EnumOutputShapeWithKeys<T, never> : false extends Values ? EnumOutputShapeWithKeys<T, never> : EnumOutputShapeWithKeys<T, Keys>;
12
+ type EnumOutputShapeWithKeys<T extends Record<string, unknown>, Keys extends string> = { [K in keyof T]: Exclude<Keys, K> extends (infer Empty extends string) ? Simplify<{ [K2 in K]: T[K]; } & { [K in Empty]?: never; } & {
14
13
  $kind: K;
15
- }> : never }[keyof T];
16
- type EnumInputShape<T extends Record<string, unknown>> = { [K in keyof T]: { [K2 in K]: T[K] } }[keyof T];
14
+ }> : never; }[keyof T];
15
+ type EnumInputShape<T extends Record<string, unknown>> = { [K in keyof T]: { [K2 in K]: T[K]; }; }[keyof T];
17
16
  type JoinString<T, Sep extends string> = T extends readonly [infer F extends string, ...infer R extends string[]] ? [] extends R ? F : `${F}${Sep}${JoinString<R, Sep>}` : '';
18
17
  //#endregion
19
18
  export { Encoding, EnumInputShape, EnumOutputShape, EnumOutputShapeWithKeys, InferBcsInput, InferBcsType, JoinString };
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;AAUA;;KAAY,QAAA;AAAA,KAEA,YAAA,WAAuB,OAAA,SAAgB,CAAA,SAAU,OAAA,iBAAwB,CAAA;AAAA,KACzE,aAAA,WAAwB,OAAA,cACnC,CAAA,SAAU,OAAA,iBAAwB,CAAA;AAAA,KAEvB,eAAA,WACD,MAAA,yCACY,OAAA,OAAc,CAAA,qBAC3B,CAAA,OAAQ,CAAA,wBAAyB,IAAA,SAAa,OAAA,YAAmB,CAAA,gCAC7D,MAAA,GACX,uBAAA,CAAwB,CAAA,sBACb,MAAA,GACV,uBAAA,CAAwB,CAAA,sBACb,MAAA,GACV,uBAAA,CAAwB,CAAA,yBACV,MAAA,GACb,uBAAA,CAAwB,CAAA,WACxB,uBAAA,CAAwB,CAAA,EAAG,IAAA;AAAA,KAErB,uBAAA,WAAkC,MAAA,wDACjC,CAAA,GAAI,OAAA,CAAQ,IAAA,EAAM,CAAA,uCAC3B,QAAA,UACS,CAAA,GAAI,CAAA,CAAE,CAAA,cAAe,KAAA;EAC7B,KAAA,EAAO,CAAA;AAAA,mBAIJ,CAAA;AAAA,KAEI,cAAA,WAAyB,MAAA,mCACxB,CAAA,YAAa,CAAA,GAAI,CAAA,CAAE,CAAA,YACxB,CAAA;AAAA,KAEI,UAAA,0BAAoC,CAAA,qFAIlC,CAAA,GACV,CAAA,MACG,CAAA,GAAI,GAAA,GAAM,UAAA,CAAW,CAAA,EAAG,GAAA"}
1
+ {"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;KAUY;KAEA,aAAa,UAAU,gBAAgB,UAAU,cAAc,UAAU;KACzE,cAAc,UAAU,qBACnC,UAAU,mBAAmB,KAAK;KAEvB,gBACX,UAAU,yBACV,sBAAsB,cAAc,YACpC,SAAS,QAAQ,kBAAiB,SAAQ,aAAa,cAAc,KAAK,gCAC7D,SACX,wBAAwB,uBACb,SACV,wBAAwB,uBACb,SACV,wBAAwB,0BACV,SACb,wBAAwB,YACxB,wBAAwB,GAAG;KAErB,wBAAwB,UAAU,yBAAyB,0BACrE,WAAW,IAAI,QAAQ,MAAM,kBAAiB,wBAC5C,YACG,MAAM,IAAI,EAAE,WAAU,KAAK;EAC7B,OAAO;oBAIJ;KAEI,eAAe,UAAU,8BACnC,WAAW,OAAO,MAAM,IAAI,EAAE,eACxB;KAEI,WAAW,GAAG,sBAAsB,0BACzC,2BACG,iCAEI,IACV,OACG,IAAI,MAAM,WAAW,GAAG"}
package/dist/types.mjs CHANGED
@@ -1 +1 @@
1
- export { };
1
+ export {};
@@ -1 +1 @@
1
- {"version":3,"file":"uleb.d.mts","names":[],"sources":["../src/uleb.ts"],"mappings":";iBAKgB,UAAA,CAAW,GAAA;AAAA,iBAuBX,UAAA,CAAW,GAAA,aAAgB,UAAA;EAC1C,KAAA;EACA,MAAA;AAAA"}
1
+ {"version":3,"file":"uleb.d.mts","names":[],"sources":["../src/uleb.ts"],"mappings":";iBAKgB,WAAW;iBAuBX,WAAW,gBAAgB;EAC1C;EACA"}
package/dist/uleb.mjs CHANGED
@@ -21,7 +21,10 @@ function ulebDecode(arr) {
21
21
  const byte = arr[len];
22
22
  len += 1;
23
23
  total += BigInt(byte & 127) << shift;
24
- if ((byte & 128) === 0) break;
24
+ if ((byte & 128) === 0) {
25
+ if (byte === 0 && len > 1) throw new Error("ULEB decode error: non-canonical encoding");
26
+ break;
27
+ }
25
28
  shift += 7n;
26
29
  }
27
30
  if (total > BigInt(Number.MAX_SAFE_INTEGER)) throw new Error("ULEB decode error: value exceeds MAX_SAFE_INTEGER");
@@ -30,7 +33,7 @@ function ulebDecode(arr) {
30
33
  length: len
31
34
  };
32
35
  }
33
-
34
36
  //#endregion
35
37
  export { ulebDecode, ulebEncode };
38
+
36
39
  //# sourceMappingURL=uleb.mjs.map
package/dist/uleb.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"uleb.mjs","names":[],"sources":["../src/uleb.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// Helper utility: write number as an ULEB array.\n// Original code is taken from: https://www.npmjs.com/package/uleb128 (no longer exists)\nexport function ulebEncode(num: number | bigint): number[] {\n\tlet bigNum = BigInt(num);\n\tconst arr: number[] = [];\n\tlet len = 0;\n\n\tif (bigNum === 0n) {\n\t\treturn [0];\n\t}\n\n\twhile (bigNum > 0) {\n\t\tarr[len] = Number(bigNum & 0x7fn);\n\t\tbigNum >>= 7n;\n\t\tif (bigNum > 0n) {\n\t\t\tarr[len] |= 0x80;\n\t\t}\n\t\tlen += 1;\n\t}\n\n\treturn arr;\n}\n\n// Helper utility: decode ULEB as an array of numbers.\n// Original code is taken from: https://www.npmjs.com/package/uleb128 (no longer exists)\nexport function ulebDecode(arr: number[] | Uint8Array): {\n\tvalue: number;\n\tlength: number;\n} {\n\tlet total = 0n;\n\tlet shift = 0n;\n\tlet len = 0;\n\n\twhile (true) {\n\t\tif (len >= arr.length) {\n\t\t\tthrow new Error('ULEB decode error: buffer overflow');\n\t\t}\n\n\t\tconst byte = arr[len];\n\t\tlen += 1;\n\t\ttotal += BigInt(byte & 0x7f) << shift;\n\t\tif ((byte & 0x80) === 0) {\n\t\t\tbreak;\n\t\t}\n\t\tshift += 7n;\n\t}\n\n\t// TODO: return bigint in next major version\n\tif (total > BigInt(Number.MAX_SAFE_INTEGER)) {\n\t\tthrow new Error('ULEB decode error: value exceeds MAX_SAFE_INTEGER');\n\t}\n\n\treturn {\n\t\tvalue: Number(total),\n\t\tlength: len,\n\t};\n}\n"],"mappings":";AAKA,SAAgB,WAAW,KAAgC;CAC1D,IAAI,SAAS,OAAO,IAAI;CACxB,MAAM,MAAgB,EAAE;CACxB,IAAI,MAAM;AAEV,KAAI,WAAW,GACd,QAAO,CAAC,EAAE;AAGX,QAAO,SAAS,GAAG;AAClB,MAAI,OAAO,OAAO,SAAS,KAAM;AACjC,aAAW;AACX,MAAI,SAAS,GACZ,KAAI,QAAQ;AAEb,SAAO;;AAGR,QAAO;;AAKR,SAAgB,WAAW,KAGzB;CACD,IAAI,QAAQ;CACZ,IAAI,QAAQ;CACZ,IAAI,MAAM;AAEV,QAAO,MAAM;AACZ,MAAI,OAAO,IAAI,OACd,OAAM,IAAI,MAAM,qCAAqC;EAGtD,MAAM,OAAO,IAAI;AACjB,SAAO;AACP,WAAS,OAAO,OAAO,IAAK,IAAI;AAChC,OAAK,OAAO,SAAU,EACrB;AAED,WAAS;;AAIV,KAAI,QAAQ,OAAO,OAAO,iBAAiB,CAC1C,OAAM,IAAI,MAAM,oDAAoD;AAGrE,QAAO;EACN,OAAO,OAAO,MAAM;EACpB,QAAQ;EACR"}
1
+ {"version":3,"file":"uleb.mjs","names":[],"sources":["../src/uleb.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n// Helper utility: write number as an ULEB array.\n// Original code is taken from: https://www.npmjs.com/package/uleb128 (no longer exists)\nexport function ulebEncode(num: number | bigint): number[] {\n\tlet bigNum = BigInt(num);\n\tconst arr: number[] = [];\n\tlet len = 0;\n\n\tif (bigNum === 0n) {\n\t\treturn [0];\n\t}\n\n\twhile (bigNum > 0) {\n\t\tarr[len] = Number(bigNum & 0x7fn);\n\t\tbigNum >>= 7n;\n\t\tif (bigNum > 0n) {\n\t\t\tarr[len] |= 0x80;\n\t\t}\n\t\tlen += 1;\n\t}\n\n\treturn arr;\n}\n\n// Helper utility: decode ULEB as an array of numbers.\n// Original code is taken from: https://www.npmjs.com/package/uleb128 (no longer exists)\nexport function ulebDecode(arr: number[] | Uint8Array): {\n\tvalue: number;\n\tlength: number;\n} {\n\tlet total = 0n;\n\tlet shift = 0n;\n\tlet len = 0;\n\n\twhile (true) {\n\t\tif (len >= arr.length) {\n\t\t\tthrow new Error('ULEB decode error: buffer overflow');\n\t\t}\n\n\t\tconst byte = arr[len];\n\t\tlen += 1;\n\t\ttotal += BigInt(byte & 0x7f) << shift;\n\t\tif ((byte & 0x80) === 0) {\n\t\t\t// A trailing zero group adds no value, so the encoding is not minimal\n\t\t\tif (byte === 0 && len > 1) {\n\t\t\t\tthrow new Error('ULEB decode error: non-canonical encoding');\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tshift += 7n;\n\t}\n\n\t// TODO: return bigint in next major version\n\tif (total > BigInt(Number.MAX_SAFE_INTEGER)) {\n\t\tthrow new Error('ULEB decode error: value exceeds MAX_SAFE_INTEGER');\n\t}\n\n\treturn {\n\t\tvalue: Number(total),\n\t\tlength: len,\n\t};\n}\n"],"mappings":";AAKA,SAAgB,WAAW,KAAgC;CAC1D,IAAI,SAAS,OAAO,GAAG;CACvB,MAAM,MAAgB,CAAC;CACvB,IAAI,MAAM;CAEV,IAAI,WAAW,IACd,OAAO,CAAC,CAAC;CAGV,OAAO,SAAS,GAAG;EAClB,IAAI,OAAO,OAAO,SAAS,IAAK;EAChC,WAAW;EACX,IAAI,SAAS,IACZ,IAAI,QAAQ;EAEb,OAAO;CACR;CAEA,OAAO;AACR;AAIA,SAAgB,WAAW,KAGzB;CACD,IAAI,QAAQ;CACZ,IAAI,QAAQ;CACZ,IAAI,MAAM;CAEV,OAAO,MAAM;EACZ,IAAI,OAAO,IAAI,QACd,MAAM,IAAI,MAAM,oCAAoC;EAGrD,MAAM,OAAO,IAAI;EACjB,OAAO;EACP,SAAS,OAAO,OAAO,GAAI,KAAK;EAChC,KAAK,OAAO,SAAU,GAAG;GAExB,IAAI,SAAS,KAAK,MAAM,GACvB,MAAM,IAAI,MAAM,2CAA2C;GAE5D;EACD;EACA,SAAS;CACV;CAGA,IAAI,QAAQ,OAAO,OAAO,gBAAgB,GACzC,MAAM,IAAI,MAAM,mDAAmD;CAGpE,OAAO;EACN,OAAO,OAAO,KAAK;EACnB,QAAQ;CACT;AACD"}
package/dist/utils.d.mts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Encoding } from "./types.mjs";
2
-
3
2
  //#region src/utils.d.ts
4
3
  /**
5
4
  * Encode data with either `hex` or `base64`.
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.mts","names":[],"sources":["../src/utils.ts"],"mappings":";;;;;AAaA;;;;;iBAAgB,SAAA,CAAU,IAAA,EAAM,UAAA,EAAY,QAAA,EAAU,QAAA;;;;;AAoBtD;;;iBAAgB,SAAA,CAAU,IAAA,UAAc,QAAA,EAAU,QAAA,GAAW,UAAA;AAAA,iBAa7C,sBAAA,CACf,GAAA,UACA,iBAAA"}
1
+ {"version":3,"file":"utils.d.mts","names":[],"sources":["../src/utils.ts"],"mappings":";;;;;;;;;iBAagB,UAAU,MAAM,YAAY,UAAU;;;;;;;;iBAoBtC,UAAU,cAAc,UAAU,WAAW;iBAa7C,uBACf,aACA"}
package/dist/utils.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@haneullabs/utils";
2
-
3
2
  //#region src/utils.ts
4
3
  /**
5
4
  * Encode data with either `hex` or `base64`.
@@ -50,7 +49,7 @@ function splitGenericParameters(str, genericSeparators = ["<", ">"]) {
50
49
  tok.push(word.trim());
51
50
  return tok;
52
51
  }
53
-
54
52
  //#endregion
55
53
  export { decodeStr, encodeStr, splitGenericParameters };
54
+
56
55
  //# sourceMappingURL=utils.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from '@haneullabs/utils';\nimport type { Encoding } from './types.js';\n\n/**\n * Encode data with either `hex` or `base64`.\n *\n * @param {Uint8Array} data Data to encode.\n * @param {String} encoding Encoding to use: base64 or hex\n * @returns {String} Encoded value.\n */\nexport function encodeStr(data: Uint8Array, encoding: Encoding): string {\n\tswitch (encoding) {\n\t\tcase 'base58':\n\t\t\treturn toBase58(data);\n\t\tcase 'base64':\n\t\t\treturn toBase64(data);\n\t\tcase 'hex':\n\t\t\treturn toHex(data);\n\t\tdefault:\n\t\t\tthrow new Error('Unsupported encoding, supported values are: base64, hex');\n\t}\n}\n\n/**\n * Decode either `base64` or `hex` data.\n *\n * @param {String} data Data to encode.\n * @param {String} encoding Encoding to use: base64 or hex\n * @returns {Uint8Array} Encoded value.\n */\nexport function decodeStr(data: string, encoding: Encoding): Uint8Array {\n\tswitch (encoding) {\n\t\tcase 'base58':\n\t\t\treturn fromBase58(data);\n\t\tcase 'base64':\n\t\t\treturn fromBase64(data);\n\t\tcase 'hex':\n\t\t\treturn fromHex(data);\n\t\tdefault:\n\t\t\tthrow new Error('Unsupported encoding, supported values are: base64, hex');\n\t}\n}\n\nexport function splitGenericParameters(\n\tstr: string,\n\tgenericSeparators: [string, string] = ['<', '>'],\n) {\n\tconst [left, right] = genericSeparators;\n\tconst tok = [];\n\tlet word = '';\n\tlet nestedAngleBrackets = 0;\n\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str[i];\n\t\tif (char === left) {\n\t\t\tnestedAngleBrackets++;\n\t\t}\n\t\tif (char === right) {\n\t\t\tnestedAngleBrackets--;\n\t\t}\n\t\tif (nestedAngleBrackets === 0 && char === ',') {\n\t\t\ttok.push(word.trim());\n\t\t\tword = '';\n\t\t\tcontinue;\n\t\t}\n\t\tword += char;\n\t}\n\n\ttok.push(word.trim());\n\n\treturn tok;\n}\n"],"mappings":";;;;;;;;;;AAaA,SAAgB,UAAU,MAAkB,UAA4B;AACvE,SAAQ,UAAR;EACC,KAAK,SACJ,QAAO,SAAS,KAAK;EACtB,KAAK,SACJ,QAAO,SAAS,KAAK;EACtB,KAAK,MACJ,QAAO,MAAM,KAAK;EACnB,QACC,OAAM,IAAI,MAAM,0DAA0D;;;;;;;;;;AAW7E,SAAgB,UAAU,MAAc,UAAgC;AACvE,SAAQ,UAAR;EACC,KAAK,SACJ,QAAO,WAAW,KAAK;EACxB,KAAK,SACJ,QAAO,WAAW,KAAK;EACxB,KAAK,MACJ,QAAO,QAAQ,KAAK;EACrB,QACC,OAAM,IAAI,MAAM,0DAA0D;;;AAI7E,SAAgB,uBACf,KACA,oBAAsC,CAAC,KAAK,IAAI,EAC/C;CACD,MAAM,CAAC,MAAM,SAAS;CACtB,MAAM,MAAM,EAAE;CACd,IAAI,OAAO;CACX,IAAI,sBAAsB;AAE1B,MAAK,IAAI,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;EACpC,MAAM,OAAO,IAAI;AACjB,MAAI,SAAS,KACZ;AAED,MAAI,SAAS,MACZ;AAED,MAAI,wBAAwB,KAAK,SAAS,KAAK;AAC9C,OAAI,KAAK,KAAK,MAAM,CAAC;AACrB,UAAO;AACP;;AAED,UAAQ;;AAGT,KAAI,KAAK,KAAK,MAAM,CAAC;AAErB,QAAO"}
1
+ {"version":3,"file":"utils.mjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from '@haneullabs/utils';\nimport type { Encoding } from './types.js';\n\n/**\n * Encode data with either `hex` or `base64`.\n *\n * @param {Uint8Array} data Data to encode.\n * @param {String} encoding Encoding to use: base64 or hex\n * @returns {String} Encoded value.\n */\nexport function encodeStr(data: Uint8Array, encoding: Encoding): string {\n\tswitch (encoding) {\n\t\tcase 'base58':\n\t\t\treturn toBase58(data);\n\t\tcase 'base64':\n\t\t\treturn toBase64(data);\n\t\tcase 'hex':\n\t\t\treturn toHex(data);\n\t\tdefault:\n\t\t\tthrow new Error('Unsupported encoding, supported values are: base64, hex');\n\t}\n}\n\n/**\n * Decode either `base64` or `hex` data.\n *\n * @param {String} data Data to encode.\n * @param {String} encoding Encoding to use: base64 or hex\n * @returns {Uint8Array} Encoded value.\n */\nexport function decodeStr(data: string, encoding: Encoding): Uint8Array {\n\tswitch (encoding) {\n\t\tcase 'base58':\n\t\t\treturn fromBase58(data);\n\t\tcase 'base64':\n\t\t\treturn fromBase64(data);\n\t\tcase 'hex':\n\t\t\treturn fromHex(data);\n\t\tdefault:\n\t\t\tthrow new Error('Unsupported encoding, supported values are: base64, hex');\n\t}\n}\n\nexport function splitGenericParameters(\n\tstr: string,\n\tgenericSeparators: [string, string] = ['<', '>'],\n) {\n\tconst [left, right] = genericSeparators;\n\tconst tok = [];\n\tlet word = '';\n\tlet nestedAngleBrackets = 0;\n\n\tfor (let i = 0; i < str.length; i++) {\n\t\tconst char = str[i];\n\t\tif (char === left) {\n\t\t\tnestedAngleBrackets++;\n\t\t}\n\t\tif (char === right) {\n\t\t\tnestedAngleBrackets--;\n\t\t}\n\t\tif (nestedAngleBrackets === 0 && char === ',') {\n\t\t\ttok.push(word.trim());\n\t\t\tword = '';\n\t\t\tcontinue;\n\t\t}\n\t\tword += char;\n\t}\n\n\ttok.push(word.trim());\n\n\treturn tok;\n}\n"],"mappings":";;;;;;;;;AAaA,SAAgB,UAAU,MAAkB,UAA4B;CACvE,QAAQ,UAAR;EACC,KAAK,UACJ,OAAO,SAAS,IAAI;EACrB,KAAK,UACJ,OAAO,SAAS,IAAI;EACrB,KAAK,OACJ,OAAO,MAAM,IAAI;EAClB,SACC,MAAM,IAAI,MAAM,yDAAyD;CAC3E;AACD;;;;;;;;AASA,SAAgB,UAAU,MAAc,UAAgC;CACvE,QAAQ,UAAR;EACC,KAAK,UACJ,OAAO,WAAW,IAAI;EACvB,KAAK,UACJ,OAAO,WAAW,IAAI;EACvB,KAAK,OACJ,OAAO,QAAQ,IAAI;EACpB,SACC,MAAM,IAAI,MAAM,yDAAyD;CAC3E;AACD;AAEA,SAAgB,uBACf,KACA,oBAAsC,CAAC,KAAK,GAAG,GAC9C;CACD,MAAM,CAAC,MAAM,SAAS;CACtB,MAAM,MAAM,CAAC;CACb,IAAI,OAAO;CACX,IAAI,sBAAsB;CAE1B,KAAK,IAAI,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;EACpC,MAAM,OAAO,IAAI;EACjB,IAAI,SAAS,MACZ;EAED,IAAI,SAAS,OACZ;EAED,IAAI,wBAAwB,KAAK,SAAS,KAAK;GAC9C,IAAI,KAAK,KAAK,KAAK,CAAC;GACpB,OAAO;GACP;EACD;EACA,QAAQ;CACT;CAEA,IAAI,KAAK,KAAK,KAAK,CAAC;CAEpB,OAAO;AACR"}
package/dist/writer.d.mts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Encoding } from "./types.mjs";
2
-
3
2
  //#region src/writer.d.ts
4
3
  interface BcsWriterOptions {
5
4
  /** The initial size (in bytes) of the buffer tht will be allocated */
@@ -32,11 +31,7 @@ declare class BcsWriter {
32
31
  private size;
33
32
  private maxSize;
34
33
  private allocateSize;
35
- constructor({
36
- initialSize,
37
- maxSize,
38
- allocateSize
39
- }?: BcsWriterOptions);
34
+ constructor({ initialSize, maxSize, allocateSize }?: BcsWriterOptions);
40
35
  private ensureSizeOrGrow;
41
36
  /**
42
37
  * Shift current cursor position by `bytes`.
@@ -1 +1 @@
1
- {"version":3,"file":"writer.d.mts","names":[],"sources":["../src/writer.ts"],"mappings":";;;UAOiB,gBAAA;;EAEhB,WAAA;EAFgC;EAIhC,OAAA;EAJgC;EAMhC,YAAA;AAAA;;;;AAqBD;;;;;;;;;;;;;;c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;;;EAQtB,SAAS,UAAU"}
package/dist/writer.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  import { ulebEncode } from "./uleb.mjs";
2
2
  import { encodeStr } from "./utils.mjs";
3
-
4
3
  //#region src/writer.ts
5
4
  /**
6
5
  * Class used to write BCS data into a buffer. Initializer requires
@@ -173,7 +172,7 @@ function toLittleEndian(bigint, size) {
173
172
  }
174
173
  return result;
175
174
  }
176
-
177
175
  //#endregion
178
176
  export { BcsWriter };
177
+
179
178
  //# sourceMappingURL=writer.mjs.map