@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,116 @@
1
+ import { ulebDecode } from "./uleb.js";
2
+ class BcsReader {
3
+ /**
4
+ * @param {Uint8Array} data Data to use as a buffer.
5
+ */
6
+ constructor(data) {
7
+ this.bytePosition = 0;
8
+ this.dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
9
+ }
10
+ /**
11
+ * Shift current cursor position by `bytes`.
12
+ *
13
+ * @param {Number} bytes Number of bytes to
14
+ * @returns {this} Self for possible chaining.
15
+ */
16
+ shift(bytes) {
17
+ this.bytePosition += bytes;
18
+ return this;
19
+ }
20
+ /**
21
+ * Read U8 value from the buffer and shift cursor by 1.
22
+ * @returns
23
+ */
24
+ read8() {
25
+ const value = this.dataView.getUint8(this.bytePosition);
26
+ this.shift(1);
27
+ return value;
28
+ }
29
+ /**
30
+ * Read U16 value from the buffer and shift cursor by 2.
31
+ * @returns
32
+ */
33
+ read16() {
34
+ const value = this.dataView.getUint16(this.bytePosition, true);
35
+ this.shift(2);
36
+ return value;
37
+ }
38
+ /**
39
+ * Read U32 value from the buffer and shift cursor by 4.
40
+ * @returns
41
+ */
42
+ read32() {
43
+ const value = this.dataView.getUint32(this.bytePosition, true);
44
+ this.shift(4);
45
+ return value;
46
+ }
47
+ /**
48
+ * Read U64 value from the buffer and shift cursor by 8.
49
+ * @returns
50
+ */
51
+ read64() {
52
+ const value1 = this.read32();
53
+ const value2 = this.read32();
54
+ const result = value2.toString(16) + value1.toString(16).padStart(8, "0");
55
+ return BigInt("0x" + result).toString(10);
56
+ }
57
+ /**
58
+ * Read U128 value from the buffer and shift cursor by 16.
59
+ */
60
+ read128() {
61
+ const value1 = BigInt(this.read64());
62
+ const value2 = BigInt(this.read64());
63
+ const result = value2.toString(16) + value1.toString(16).padStart(16, "0");
64
+ return BigInt("0x" + result).toString(10);
65
+ }
66
+ /**
67
+ * Read U128 value from the buffer and shift cursor by 32.
68
+ * @returns
69
+ */
70
+ read256() {
71
+ const value1 = BigInt(this.read128());
72
+ const value2 = BigInt(this.read128());
73
+ const result = value2.toString(16) + value1.toString(16).padStart(32, "0");
74
+ return BigInt("0x" + result).toString(10);
75
+ }
76
+ /**
77
+ * Read `num` number of bytes from the buffer and shift cursor by `num`.
78
+ * @param num Number of bytes to read.
79
+ */
80
+ readBytes(num) {
81
+ const start = this.bytePosition + this.dataView.byteOffset;
82
+ const value = new Uint8Array(this.dataView.buffer, start, num);
83
+ this.shift(num);
84
+ return value;
85
+ }
86
+ /**
87
+ * Read ULEB value - an integer of varying size. Used for enum indexes and
88
+ * vector lengths.
89
+ * @returns {Number} The ULEB value.
90
+ */
91
+ readULEB() {
92
+ const start = this.bytePosition + this.dataView.byteOffset;
93
+ const buffer = new Uint8Array(this.dataView.buffer, start);
94
+ const { value, length } = ulebDecode(buffer);
95
+ this.shift(length);
96
+ return value;
97
+ }
98
+ /**
99
+ * Read a BCS vector: read a length and then apply function `cb` X times
100
+ * where X is the length of the vector, defined as ULEB in BCS bytes.
101
+ * @param cb Callback to process elements of vector.
102
+ * @returns {Array<Any>} Array of the resulting values, returned by callback.
103
+ */
104
+ readVec(cb) {
105
+ const length = this.readULEB();
106
+ const result = [];
107
+ for (let i = 0; i < length; i++) {
108
+ result.push(cb(this, i, length));
109
+ }
110
+ return result;
111
+ }
112
+ }
113
+ export {
114
+ BcsReader
115
+ };
116
+ //# sourceMappingURL=reader.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/reader.ts"],
4
+ "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"],
5
+ "mappings": "AAGA,SAAS,kBAAkB;AAgCpB,MAAM,UAAU;AAAA;AAAA;AAAA;AAAA,EAOtB,YAAY,MAAkB;AAL9B,SAAQ,eAAuB;AAM9B,SAAK,WAAW,IAAI,SAAS,KAAK,QAAQ,KAAK,YAAY,KAAK,UAAU;AAAA,EAC3E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OAAe;AACpB,SAAK,gBAAgB;AACrB,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,QAAgB;AACf,UAAM,QAAQ,KAAK,SAAS,SAAS,KAAK,YAAY;AACtD,SAAK,MAAM,CAAC;AACZ,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,SAAiB;AAChB,UAAM,QAAQ,KAAK,SAAS,UAAU,KAAK,cAAc,IAAI;AAC7D,SAAK,MAAM,CAAC;AACZ,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,SAAiB;AAChB,UAAM,QAAQ,KAAK,SAAS,UAAU,KAAK,cAAc,IAAI;AAC7D,SAAK,MAAM,CAAC;A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,MAAM;AAEjB,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,QAAQ,IAAkE;AACzE,UAAM,SAAS,KAAK,SAAS;AAC7B,UAAM,SAAS,CAAC;AAChB,aAAS,IAAI,GAAG,IAAI,QAAQ,KAAK;AAChC,aAAO,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;AAAA,IAChC;AACA,WAAO;AAAA,EACR;AACD;",
6
+ "names": []
7
+ }
@@ -0,0 +1,28 @@
1
+ import type { Simplify } from '@haneullabs/utils';
2
+ import type { BcsType } from './bcs-type.js';
3
+ /**
4
+ * Supported encodings.
5
+ * Used in `Reader.toString()` as well as in `decodeStr` and `encodeStr` functions.
6
+ */
7
+ export type Encoding = 'base58' | 'base64' | 'hex';
8
+ export type InferBcsType<T extends BcsType<any>> = T extends BcsType<infer U, any> ? U : never;
9
+ export type InferBcsInput<T extends BcsType<any, any>> = T extends BcsType<any, infer U> ? U : never;
10
+ export 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>;
11
+ export type EnumOutputShapeWithKeys<T extends Record<string, unknown>, Keys extends string> = {
12
+ [K in keyof T]: Exclude<Keys, K> extends infer Empty extends string ? Simplify<{
13
+ [K2 in K]: T[K];
14
+ } & {
15
+ [K in Empty]?: never;
16
+ } & {
17
+ $kind: K;
18
+ }> : never;
19
+ }[keyof T];
20
+ export type EnumInputShape<T extends Record<string, unknown>> = {
21
+ [K in keyof T]: {
22
+ [K2 in K]: T[K];
23
+ };
24
+ }[keyof T];
25
+ export type JoinString<T, Sep extends string> = T extends readonly [
26
+ infer F extends string,
27
+ ...infer R extends string[]
28
+ ] ? [] extends R ? F : `${F}${Sep}${JoinString<R, Sep>}` : '';
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,5 @@
1
+ export declare function ulebEncode(num: number | bigint): number[];
2
+ export declare function ulebDecode(arr: number[] | Uint8Array): {
3
+ value: number;
4
+ length: number;
5
+ };
@@ -0,0 +1,46 @@
1
+ function ulebEncode(num) {
2
+ let bigNum = BigInt(num);
3
+ const arr = [];
4
+ let len = 0;
5
+ if (bigNum === 0n) {
6
+ return [0];
7
+ }
8
+ while (bigNum > 0) {
9
+ arr[len] = Number(bigNum & 0x7fn);
10
+ bigNum >>= 7n;
11
+ if (bigNum > 0n) {
12
+ arr[len] |= 128;
13
+ }
14
+ len += 1;
15
+ }
16
+ return arr;
17
+ }
18
+ function ulebDecode(arr) {
19
+ let total = 0n;
20
+ let shift = 0n;
21
+ let len = 0;
22
+ while (true) {
23
+ if (len >= arr.length) {
24
+ throw new Error("ULEB decode error: buffer overflow");
25
+ }
26
+ const byte = arr[len];
27
+ len += 1;
28
+ total += BigInt(byte & 127) << shift;
29
+ if ((byte & 128) === 0) {
30
+ break;
31
+ }
32
+ shift += 7n;
33
+ }
34
+ if (total > BigInt(Number.MAX_SAFE_INTEGER)) {
35
+ throw new Error("ULEB decode error: value exceeds MAX_SAFE_INTEGER");
36
+ }
37
+ return {
38
+ value: Number(total),
39
+ length: len
40
+ };
41
+ }
42
+ export {
43
+ ulebDecode,
44
+ ulebEncode
45
+ };
46
+ //# sourceMappingURL=uleb.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/uleb.ts"],
4
+ "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\t// eslint-disable-next-line no-constant-condition\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"],
5
+ "mappings": "AAKO,SAAS,WAAW,KAAgC;AAC1D,MAAI,SAAS,OAAO,GAAG;AACvB,QAAM,MAAgB,CAAC;AACvB,MAAI,MAAM;AAEV,MAAI,WAAW,IAAI;AAClB,WAAO,CAAC,CAAC;AAAA,EACV;AAEA,SAAO,SAAS,GAAG;AAClB,QAAI,GAAG,IAAI,OAAO,SAAS,KAAK;AAChC,eAAW;AACX,QAAI,SAAS,IAAI;AAChB,UAAI,GAAG,KAAK;AAAA,IACb;AACA,WAAO;AAAA,EACR;AAEA,SAAO;AACR;AAIO,SAAS,WAAW,KAGzB;AACD,MAAI,QAAQ;AACZ,MAAI,QAAQ;AACZ,MAAI,MAAM;AAGV,SAAO,MAAM;AACZ,QAAI,OAAO,IAAI,QAAQ;AACtB,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACrD;AAEA,UAAM,OAAO,IAAI,GAAG;AACpB,WAAO;AACP,aAAS,OAAO,OAAO,GAAI,KAAK;AAChC,SAAK,OAAO,SAAU,GAAG;AACxB;AAAA,IACD;AACA,aAAS;AAAA,EACV;AAGA,MAAI,QAAQ,OAAO,OAAO,gBAAgB,GAAG;AAC5C,UAAM,IAAI,MAAM,mDAAmD;AAAA,EACpE;AAEA,SAAO;AAAA,IACN,OAAO,OAAO,KAAK;AAAA,IACnB,QAAQ;AAAA,EACT;AACD;",
6
+ "names": []
7
+ }
@@ -0,0 +1,18 @@
1
+ import type { Encoding } from './types.js';
2
+ /**
3
+ * Encode data with either `hex` or `base64`.
4
+ *
5
+ * @param {Uint8Array} data Data to encode.
6
+ * @param {String} encoding Encoding to use: base64 or hex
7
+ * @returns {String} Encoded value.
8
+ */
9
+ export declare function encodeStr(data: Uint8Array, encoding: Encoding): string;
10
+ /**
11
+ * Decode either `base64` or `hex` data.
12
+ *
13
+ * @param {String} data Data to encode.
14
+ * @param {String} encoding Encoding to use: base64 or hex
15
+ * @returns {Uint8Array} Encoded value.
16
+ */
17
+ export declare function decodeStr(data: string, encoding: Encoding): Uint8Array;
18
+ export declare function splitGenericParameters(str: string, genericSeparators?: [string, string]): string[];
@@ -0,0 +1,54 @@
1
+ import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from "@haneullabs/utils";
2
+ function encodeStr(data, encoding) {
3
+ switch (encoding) {
4
+ case "base58":
5
+ return toBase58(data);
6
+ case "base64":
7
+ return toBase64(data);
8
+ case "hex":
9
+ return toHex(data);
10
+ default:
11
+ throw new Error("Unsupported encoding, supported values are: base64, hex");
12
+ }
13
+ }
14
+ function decodeStr(data, encoding) {
15
+ switch (encoding) {
16
+ case "base58":
17
+ return fromBase58(data);
18
+ case "base64":
19
+ return fromBase64(data);
20
+ case "hex":
21
+ return fromHex(data);
22
+ default:
23
+ throw new Error("Unsupported encoding, supported values are: base64, hex");
24
+ }
25
+ }
26
+ function splitGenericParameters(str, genericSeparators = ["<", ">"]) {
27
+ const [left, right] = genericSeparators;
28
+ const tok = [];
29
+ let word = "";
30
+ let nestedAngleBrackets = 0;
31
+ for (let i = 0; i < str.length; i++) {
32
+ const char = str[i];
33
+ if (char === left) {
34
+ nestedAngleBrackets++;
35
+ }
36
+ if (char === right) {
37
+ nestedAngleBrackets--;
38
+ }
39
+ if (nestedAngleBrackets === 0 && char === ",") {
40
+ tok.push(word.trim());
41
+ word = "";
42
+ continue;
43
+ }
44
+ word += char;
45
+ }
46
+ tok.push(word.trim());
47
+ return tok;
48
+ }
49
+ export {
50
+ decodeStr,
51
+ encodeStr,
52
+ splitGenericParameters
53
+ };
54
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/utils.ts"],
4
+ "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"],
5
+ "mappings": "AAGA,SAAS,YAAY,YAAY,SAAS,UAAU,UAAU,aAAa;AAUpE,SAAS,UAAU,MAAkB,UAA4B;AACvE,UAAQ,UAAU;AAAA,IACjB,KAAK;AACJ,aAAO,SAAS,IAAI;AAAA,IACrB,KAAK;AACJ,aAAO,SAAS,IAAI;AAAA,IACrB,KAAK;AACJ,aAAO,MAAM,IAAI;AAAA,IAClB;AACC,YAAM,IAAI,MAAM,yDAAyD;AAAA,EAC3E;AACD;AASO,SAAS,UAAU,MAAc,UAAgC;AACvE,UAAQ,UAAU;AAAA,IACjB,KAAK;AACJ,aAAO,WAAW,IAAI;AAAA,IACvB,KAAK;AACJ,aAAO,WAAW,IAAI;AAAA,IACvB,KAAK;AACJ,aAAO,QAAQ,IAAI;AAAA,IACpB;AACC,YAAM,IAAI,MAAM,yDAAyD;AAAA,EAC3E;AACD;AAEO,SAAS,uBACf,KACA,oBAAsC,CAAC,KAAK,GAAG,GAC9C;AACD,QAAM,CAAC,MAAM,KAAK,IAAI;AACtB,QAAM,MAAM,CAAC;AACb,MAAI,OAAO;AACX,MAAI,sBAAsB;AAE1B,WAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACpC,UAAM,OAAO,IAAI,CAAC;AAClB,QAAI,SAAS,MAAM;AAClB;AAAA,IACD;AACA,QAAI,SAAS,OAAO;AACnB;AAAA,IACD;AACA,QAAI,wBAAwB,KAAK,SAAS,KAAK;AAC9C,UAAI,KAAK,KAAK,KAAK,CAAC;AACpB,aAAO;AACP;AAAA,IACD;AACA,YAAQ;AAAA,EACT;AAEA,MAAI,KAAK,KAAK,KAAK,CAAC;AAEpB,SAAO;AACR;",
6
+ "names": []
7
+ }
@@ -0,0 +1,117 @@
1
+ import type { Encoding } from './types.js';
2
+ export interface BcsWriterOptions {
3
+ /** The initial size (in bytes) of the buffer tht will be allocated */
4
+ initialSize?: number;
5
+ /** The maximum size (in bytes) that the buffer is allowed to grow to */
6
+ maxSize?: number;
7
+ /** The amount of bytes that will be allocated whenever additional memory is required */
8
+ allocateSize?: number;
9
+ }
10
+ /**
11
+ * Class used to write BCS data into a buffer. Initializer requires
12
+ * some size of a buffer to init; default value for this buffer is 1KB.
13
+ *
14
+ * Most methods are chainable, so it is possible to write them in one go.
15
+ *
16
+ * @example
17
+ * let serialized = new BcsWriter()
18
+ * .write8(10)
19
+ * .write32(1000000)
20
+ * .write64(10000001000000)
21
+ * .hex();
22
+ */
23
+ /**
24
+ * Set of methods that allows data encoding/decoding as standalone
25
+ * BCS value or a part of a composed structure/vector.
26
+ */
27
+ export declare class BcsWriter {
28
+ private dataView;
29
+ private bytePosition;
30
+ private size;
31
+ private maxSize;
32
+ private allocateSize;
33
+ constructor({ initialSize, maxSize, allocateSize, }?: BcsWriterOptions);
34
+ private ensureSizeOrGrow;
35
+ /**
36
+ * Shift current cursor position by `bytes`.
37
+ *
38
+ * @param {Number} bytes Number of bytes to
39
+ * @returns {this} Self for possible chaining.
40
+ */
41
+ shift(bytes: number): this;
42
+ /**
43
+ * Write a U8 value into a buffer and shift cursor position by 1.
44
+ * @param {Number} value Value to write.
45
+ * @returns {this}
46
+ */
47
+ write8(value: number | bigint): this;
48
+ /**
49
+ * Write a U8 value into a buffer and shift cursor position by 1.
50
+ * @param {Number} value Value to write.
51
+ * @returns {this}
52
+ */
53
+ writeBytes(bytes: Uint8Array): this;
54
+ /**
55
+ * Write a U16 value into a buffer and shift cursor position by 2.
56
+ * @param {Number} value Value to write.
57
+ * @returns {this}
58
+ */
59
+ write16(value: number | bigint): this;
60
+ /**
61
+ * Write a U32 value into a buffer and shift cursor position by 4.
62
+ * @param {Number} value Value to write.
63
+ * @returns {this}
64
+ */
65
+ write32(value: number | bigint): this;
66
+ /**
67
+ * Write a U64 value into a buffer and shift cursor position by 8.
68
+ * @param {bigint} value Value to write.
69
+ * @returns {this}
70
+ */
71
+ write64(value: number | bigint): this;
72
+ /**
73
+ * Write a U128 value into a buffer and shift cursor position by 16.
74
+ *
75
+ * @param {bigint} value Value to write.
76
+ * @returns {this}
77
+ */
78
+ write128(value: number | bigint): this;
79
+ /**
80
+ * Write a U256 value into a buffer and shift cursor position by 16.
81
+ *
82
+ * @param {bigint} value Value to write.
83
+ * @returns {this}
84
+ */
85
+ write256(value: number | bigint): this;
86
+ /**
87
+ * Write a ULEB value into a buffer and shift cursor position by number of bytes
88
+ * written.
89
+ * @param {Number} value Value to write.
90
+ * @returns {this}
91
+ */
92
+ writeULEB(value: number): this;
93
+ /**
94
+ * Write a vector into a buffer by first writing the vector length and then calling
95
+ * a callback on each passed value.
96
+ *
97
+ * @param {Array<Any>} vector Array of elements to write.
98
+ * @param {WriteVecCb} cb Callback to call on each element of the vector.
99
+ * @returns {this}
100
+ */
101
+ writeVec(vector: any[], cb: (writer: BcsWriter, el: any, i: number, len: number) => void): this;
102
+ /**
103
+ * Adds support for iterations over the object.
104
+ * @returns {Uint8Array}
105
+ */
106
+ [Symbol.iterator](): Iterator<number, Iterable<number>>;
107
+ /**
108
+ * Get underlying buffer taking only value bytes (in case initial buffer size was bigger).
109
+ * @returns {Uint8Array} Resulting bcs.
110
+ */
111
+ toBytes(): Uint8Array<ArrayBuffer>;
112
+ /**
113
+ * Represent data as 'hex' or 'base64'
114
+ * @param encoding Encoding to use: 'base64' or 'hex'
115
+ */
116
+ toString(encoding: Encoding): string;
117
+ }
@@ -0,0 +1,176 @@
1
+ import { ulebEncode } from "./uleb.js";
2
+ import { encodeStr } from "./utils.js";
3
+ class BcsWriter {
4
+ constructor({
5
+ initialSize = 1024,
6
+ maxSize = Infinity,
7
+ allocateSize = 1024
8
+ } = {}) {
9
+ this.bytePosition = 0;
10
+ this.size = initialSize;
11
+ this.maxSize = maxSize;
12
+ this.allocateSize = allocateSize;
13
+ this.dataView = new DataView(new ArrayBuffer(initialSize));
14
+ }
15
+ ensureSizeOrGrow(bytes) {
16
+ const requiredSize = this.bytePosition + bytes;
17
+ if (requiredSize > this.size) {
18
+ const nextSize = Math.min(
19
+ this.maxSize,
20
+ Math.max(this.size + requiredSize, this.size + this.allocateSize)
21
+ );
22
+ if (requiredSize > nextSize) {
23
+ throw new Error(
24
+ `Attempting to serialize to BCS, but buffer does not have enough size. Allocated size: ${this.size}, Max size: ${this.maxSize}, Required size: ${requiredSize}`
25
+ );
26
+ }
27
+ this.size = nextSize;
28
+ const nextBuffer = new ArrayBuffer(this.size);
29
+ new Uint8Array(nextBuffer).set(new Uint8Array(this.dataView.buffer));
30
+ this.dataView = new DataView(nextBuffer);
31
+ }
32
+ }
33
+ /**
34
+ * Shift current cursor position by `bytes`.
35
+ *
36
+ * @param {Number} bytes Number of bytes to
37
+ * @returns {this} Self for possible chaining.
38
+ */
39
+ shift(bytes) {
40
+ this.bytePosition += bytes;
41
+ return this;
42
+ }
43
+ /**
44
+ * Write a U8 value into a buffer and shift cursor position by 1.
45
+ * @param {Number} value Value to write.
46
+ * @returns {this}
47
+ */
48
+ write8(value) {
49
+ this.ensureSizeOrGrow(1);
50
+ this.dataView.setUint8(this.bytePosition, Number(value));
51
+ return this.shift(1);
52
+ }
53
+ /**
54
+ * Write a U8 value into a buffer and shift cursor position by 1.
55
+ * @param {Number} value Value to write.
56
+ * @returns {this}
57
+ */
58
+ writeBytes(bytes) {
59
+ this.ensureSizeOrGrow(bytes.length);
60
+ for (let i = 0; i < bytes.length; i++) {
61
+ this.dataView.setUint8(this.bytePosition + i, bytes[i]);
62
+ }
63
+ return this.shift(bytes.length);
64
+ }
65
+ /**
66
+ * Write a U16 value into a buffer and shift cursor position by 2.
67
+ * @param {Number} value Value to write.
68
+ * @returns {this}
69
+ */
70
+ write16(value) {
71
+ this.ensureSizeOrGrow(2);
72
+ this.dataView.setUint16(this.bytePosition, Number(value), true);
73
+ return this.shift(2);
74
+ }
75
+ /**
76
+ * Write a U32 value into a buffer and shift cursor position by 4.
77
+ * @param {Number} value Value to write.
78
+ * @returns {this}
79
+ */
80
+ write32(value) {
81
+ this.ensureSizeOrGrow(4);
82
+ this.dataView.setUint32(this.bytePosition, Number(value), true);
83
+ return this.shift(4);
84
+ }
85
+ /**
86
+ * Write a U64 value into a buffer and shift cursor position by 8.
87
+ * @param {bigint} value Value to write.
88
+ * @returns {this}
89
+ */
90
+ write64(value) {
91
+ toLittleEndian(BigInt(value), 8).forEach((el) => this.write8(el));
92
+ return this;
93
+ }
94
+ /**
95
+ * Write a U128 value into a buffer and shift cursor position by 16.
96
+ *
97
+ * @param {bigint} value Value to write.
98
+ * @returns {this}
99
+ */
100
+ write128(value) {
101
+ toLittleEndian(BigInt(value), 16).forEach((el) => this.write8(el));
102
+ return this;
103
+ }
104
+ /**
105
+ * Write a U256 value into a buffer and shift cursor position by 16.
106
+ *
107
+ * @param {bigint} value Value to write.
108
+ * @returns {this}
109
+ */
110
+ write256(value) {
111
+ toLittleEndian(BigInt(value), 32).forEach((el) => this.write8(el));
112
+ return this;
113
+ }
114
+ /**
115
+ * Write a ULEB value into a buffer and shift cursor position by number of bytes
116
+ * written.
117
+ * @param {Number} value Value to write.
118
+ * @returns {this}
119
+ */
120
+ writeULEB(value) {
121
+ ulebEncode(value).forEach((el) => this.write8(el));
122
+ return this;
123
+ }
124
+ /**
125
+ * Write a vector into a buffer by first writing the vector length and then calling
126
+ * a callback on each passed value.
127
+ *
128
+ * @param {Array<Any>} vector Array of elements to write.
129
+ * @param {WriteVecCb} cb Callback to call on each element of the vector.
130
+ * @returns {this}
131
+ */
132
+ writeVec(vector, cb) {
133
+ this.writeULEB(vector.length);
134
+ Array.from(vector).forEach((el, i) => cb(this, el, i, vector.length));
135
+ return this;
136
+ }
137
+ /**
138
+ * Adds support for iterations over the object.
139
+ * @returns {Uint8Array}
140
+ */
141
+ // oxlint-disable-next-line require-yields
142
+ *[Symbol.iterator]() {
143
+ for (let i = 0; i < this.bytePosition; i++) {
144
+ yield this.dataView.getUint8(i);
145
+ }
146
+ return this.toBytes();
147
+ }
148
+ /**
149
+ * Get underlying buffer taking only value bytes (in case initial buffer size was bigger).
150
+ * @returns {Uint8Array} Resulting bcs.
151
+ */
152
+ toBytes() {
153
+ return new Uint8Array(this.dataView.buffer.slice(0, this.bytePosition));
154
+ }
155
+ /**
156
+ * Represent data as 'hex' or 'base64'
157
+ * @param encoding Encoding to use: 'base64' or 'hex'
158
+ */
159
+ toString(encoding) {
160
+ return encodeStr(this.toBytes(), encoding);
161
+ }
162
+ }
163
+ function toLittleEndian(bigint, size) {
164
+ const result = new Uint8Array(size);
165
+ let i = 0;
166
+ while (bigint > 0) {
167
+ result[i] = Number(bigint % BigInt(256));
168
+ bigint = bigint / BigInt(256);
169
+ i += 1;
170
+ }
171
+ return result;
172
+ }
173
+ export {
174
+ BcsWriter
175
+ };
176
+ //# sourceMappingURL=writer.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/writer.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Encoding } from './types.js';\nimport { ulebEncode } from './uleb.js';\nimport { encodeStr } from './utils.js';\n\nexport interface BcsWriterOptions {\n\t/** The initial size (in bytes) of the buffer tht will be allocated */\n\tinitialSize?: number;\n\t/** The maximum size (in bytes) that the buffer is allowed to grow to */\n\tmaxSize?: number;\n\t/** The amount of bytes that will be allocated whenever additional memory is required */\n\tallocateSize?: number;\n}\n\n/**\n * Class used to write BCS data into a buffer. Initializer requires\n * some size of a buffer to init; default value for this buffer is 1KB.\n *\n * Most methods are chainable, so it is possible to write them in one go.\n *\n * @example\n * let serialized = new BcsWriter()\n * .write8(10)\n * .write32(1000000)\n * .write64(10000001000000)\n * .hex();\n */\n\n/**\n * Set of methods that allows data encoding/decoding as standalone\n * BCS value or a part of a composed structure/vector.\n */\nexport class BcsWriter {\n\tprivate dataView: DataView<ArrayBuffer>;\n\tprivate bytePosition: number = 0;\n\tprivate size: number;\n\tprivate maxSize: number;\n\tprivate allocateSize: number;\n\n\tconstructor({\n\t\tinitialSize = 1024,\n\t\tmaxSize = Infinity,\n\t\tallocateSize = 1024,\n\t}: BcsWriterOptions = {}) {\n\t\tthis.size = initialSize;\n\t\tthis.maxSize = maxSize;\n\t\tthis.allocateSize = allocateSize;\n\t\tthis.dataView = new DataView(new ArrayBuffer(initialSize));\n\t}\n\n\tprivate ensureSizeOrGrow(bytes: number) {\n\t\tconst requiredSize = this.bytePosition + bytes;\n\t\tif (requiredSize > this.size) {\n\t\t\tconst nextSize = Math.min(\n\t\t\t\tthis.maxSize,\n\t\t\t\tMath.max(this.size + requiredSize, this.size + this.allocateSize),\n\t\t\t);\n\t\t\tif (requiredSize > nextSize) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Attempting to serialize to BCS, but buffer does not have enough size. Allocated size: ${this.size}, Max size: ${this.maxSize}, Required size: ${requiredSize}`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tthis.size = nextSize;\n\t\t\tconst nextBuffer = new ArrayBuffer(this.size);\n\t\t\tnew Uint8Array(nextBuffer).set(new Uint8Array(this.dataView.buffer));\n\t\t\tthis.dataView = new DataView(nextBuffer);\n\t\t}\n\t}\n\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): this {\n\t\tthis.bytePosition += bytes;\n\t\treturn this;\n\t}\n\t/**\n\t * Write a U8 value into a buffer and shift cursor position by 1.\n\t * @param {Number} value Value to write.\n\t * @returns {this}\n\t */\n\twrite8(value: number | bigint): this {\n\t\tthis.ensureSizeOrGrow(1);\n\t\tthis.dataView.setUint8(this.bytePosition, Number(value));\n\t\treturn this.shift(1);\n\t}\n\n\t/**\n\t * Write a U8 value into a buffer and shift cursor position by 1.\n\t * @param {Number} value Value to write.\n\t * @returns {this}\n\t */\n\twriteBytes(bytes: Uint8Array): this {\n\t\tthis.ensureSizeOrGrow(bytes.length);\n\n\t\tfor (let i = 0; i < bytes.length; i++) {\n\t\t\tthis.dataView.setUint8(this.bytePosition + i, bytes[i]);\n\t\t}\n\n\t\treturn this.shift(bytes.length);\n\t}\n\t/**\n\t * Write a U16 value into a buffer and shift cursor position by 2.\n\t * @param {Number} value Value to write.\n\t * @returns {this}\n\t */\n\twrite16(value: number | bigint): this {\n\t\tthis.ensureSizeOrGrow(2);\n\t\tthis.dataView.setUint16(this.bytePosition, Number(value), true);\n\t\treturn this.shift(2);\n\t}\n\t/**\n\t * Write a U32 value into a buffer and shift cursor position by 4.\n\t * @param {Number} value Value to write.\n\t * @returns {this}\n\t */\n\twrite32(value: number | bigint): this {\n\t\tthis.ensureSizeOrGrow(4);\n\t\tthis.dataView.setUint32(this.bytePosition, Number(value), true);\n\t\treturn this.shift(4);\n\t}\n\t/**\n\t * Write a U64 value into a buffer and shift cursor position by 8.\n\t * @param {bigint} value Value to write.\n\t * @returns {this}\n\t */\n\twrite64(value: number | bigint): this {\n\t\ttoLittleEndian(BigInt(value), 8).forEach((el) => this.write8(el));\n\n\t\treturn this;\n\t}\n\t/**\n\t * Write a U128 value into a buffer and shift cursor position by 16.\n\t *\n\t * @param {bigint} value Value to write.\n\t * @returns {this}\n\t */\n\twrite128(value: number | bigint): this {\n\t\ttoLittleEndian(BigInt(value), 16).forEach((el) => this.write8(el));\n\n\t\treturn this;\n\t}\n\t/**\n\t * Write a U256 value into a buffer and shift cursor position by 16.\n\t *\n\t * @param {bigint} value Value to write.\n\t * @returns {this}\n\t */\n\twrite256(value: number | bigint): this {\n\t\ttoLittleEndian(BigInt(value), 32).forEach((el) => this.write8(el));\n\n\t\treturn this;\n\t}\n\t/**\n\t * Write a ULEB value into a buffer and shift cursor position by number of bytes\n\t * written.\n\t * @param {Number} value Value to write.\n\t * @returns {this}\n\t */\n\twriteULEB(value: number): this {\n\t\tulebEncode(value).forEach((el) => this.write8(el));\n\t\treturn this;\n\t}\n\t/**\n\t * Write a vector into a buffer by first writing the vector length and then calling\n\t * a callback on each passed value.\n\t *\n\t * @param {Array<Any>} vector Array of elements to write.\n\t * @param {WriteVecCb} cb Callback to call on each element of the vector.\n\t * @returns {this}\n\t */\n\twriteVec(vector: any[], cb: (writer: BcsWriter, el: any, i: number, len: number) => void): this {\n\t\tthis.writeULEB(vector.length);\n\t\tArray.from(vector).forEach((el, i) => cb(this, el, i, vector.length));\n\t\treturn this;\n\t}\n\n\t/**\n\t * Adds support for iterations over the object.\n\t * @returns {Uint8Array}\n\t */\n\t// oxlint-disable-next-line require-yields\n\t*[Symbol.iterator](): Iterator<number, Iterable<number>> {\n\t\tfor (let i = 0; i < this.bytePosition; i++) {\n\t\t\tyield this.dataView.getUint8(i);\n\t\t}\n\t\treturn this.toBytes();\n\t}\n\n\t/**\n\t * Get underlying buffer taking only value bytes (in case initial buffer size was bigger).\n\t * @returns {Uint8Array} Resulting bcs.\n\t */\n\ttoBytes(): Uint8Array<ArrayBuffer> {\n\t\treturn new Uint8Array(this.dataView.buffer.slice(0, this.bytePosition));\n\t}\n\n\t/**\n\t * Represent data as 'hex' or 'base64'\n\t * @param encoding Encoding to use: 'base64' or 'hex'\n\t */\n\ttoString(encoding: Encoding): string {\n\t\treturn encodeStr(this.toBytes(), encoding);\n\t}\n}\n\nfunction toLittleEndian(bigint: bigint, size: number) {\n\tconst result = new Uint8Array(size);\n\tlet i = 0;\n\twhile (bigint > 0) {\n\t\tresult[i] = Number(bigint % BigInt(256));\n\t\tbigint = bigint / BigInt(256);\n\t\ti += 1;\n\t}\n\treturn result;\n}\n"],
5
+ "mappings": "AAIA,SAAS,kBAAkB;AAC3B,SAAS,iBAAiB;AA6BnB,MAAM,UAAU;AAAA,EAOtB,YAAY;AAAA,IACX,cAAc;AAAA,IACd,UAAU;AAAA,IACV,eAAe;AAAA,EAChB,IAAsB,CAAC,GAAG;AAT1B,SAAQ,eAAuB;AAU9B,SAAK,OAAO;AACZ,SAAK,UAAU;AACf,SAAK,eAAe;AACpB,SAAK,WAAW,IAAI,SAAS,IAAI,YAAY,WAAW,CAAC;AAAA,EAC1D;AAAA,EAEQ,iBAAiB,OAAe;AACvC,UAAM,eAAe,KAAK,eAAe;AACzC,QAAI,eAAe,KAAK,MAAM;AAC7B,YAAM,WAAW,KAAK;AAAA,QACrB,KAAK;AAAA,QACL,KAAK,IAAI,KAAK,OAAO,cAAc,KAAK,OAAO,KAAK,YAAY;AAAA,MACjE;AACA,UAAI,eAAe,UAAU;AAC5B,cAAM,IAAI;AAAA,UACT,yFAAyF,KAAK,IAAI,eAAe,KAAK,OAAO,oBAAoB,YAAY;AAAA,QAC9J;AAAA,MACD;AAEA,WAAK,OAAO;AACZ,YAAM,aAAa,IAAI,YAAY,KAAK,IAAI;AAC5C,UAAI,WAAW,UAAU,EAAE,IAAI,IAAI,WAAW,KAAK,SAAS,MAAM,CAAC;AACnE,WAAK,WAAW,IAAI,SAAS,UAAU;AAAA,IACxC;AAAA,EACD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