@haneullabs/bcs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +388 -0
- package/README.md +358 -0
- package/dist/cjs/bcs-type.d.ts +127 -0
- package/dist/cjs/bcs-type.js +386 -0
- package/dist/cjs/bcs-type.js.map +7 -0
- package/dist/cjs/bcs.d.ts +175 -0
- package/dist/cjs/bcs.js +406 -0
- package/dist/cjs/bcs.js.map +7 -0
- package/dist/cjs/index.d.ts +22 -0
- package/dist/cjs/index.js +59 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/reader.d.ts +92 -0
- package/dist/cjs/reader.js +136 -0
- package/dist/cjs/reader.js.map +7 -0
- package/dist/cjs/types.d.ts +28 -0
- package/dist/cjs/types.js +17 -0
- package/dist/cjs/types.js.map +7 -0
- package/dist/cjs/uleb.d.ts +5 -0
- package/dist/cjs/uleb.js +66 -0
- package/dist/cjs/uleb.js.map +7 -0
- package/dist/cjs/utils.d.ts +18 -0
- package/dist/cjs/utils.js +74 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/cjs/writer.d.ts +117 -0
- package/dist/cjs/writer.js +196 -0
- package/dist/cjs/writer.js.map +7 -0
- package/dist/esm/bcs-type.d.ts +127 -0
- package/dist/esm/bcs-type.js +366 -0
- package/dist/esm/bcs-type.js.map +7 -0
- package/dist/esm/bcs.d.ts +175 -0
- package/dist/esm/bcs.js +397 -0
- package/dist/esm/bcs.js.map +7 -0
- package/dist/esm/index.d.ts +22 -0
- package/dist/esm/index.js +46 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/package.json +5 -0
- package/dist/esm/reader.d.ts +92 -0
- package/dist/esm/reader.js +116 -0
- package/dist/esm/reader.js.map +7 -0
- package/dist/esm/types.d.ts +28 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +7 -0
- package/dist/esm/uleb.d.ts +5 -0
- package/dist/esm/uleb.js +46 -0
- package/dist/esm/uleb.js.map +7 -0
- package/dist/esm/utils.d.ts +18 -0
- package/dist/esm/utils.js +54 -0
- package/dist/esm/utils.js.map +7 -0
- package/dist/esm/writer.d.ts +117 -0
- package/dist/esm/writer.js +176 -0
- package/dist/esm/writer.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +73 -0
- package/src/bcs-type.ts +531 -0
- package/src/bcs.ts +543 -0
- package/src/index.ts +82 -0
- package/src/reader.ts +156 -0
- package/src/types.ts +52 -0
- package/src/uleb.ts +61 -0
- package/src/utils.ts +75 -0
- package/src/writer.ts +222 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var reader_exports = {};
|
|
20
|
+
__export(reader_exports, {
|
|
21
|
+
BcsReader: () => BcsReader
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(reader_exports);
|
|
24
|
+
var import_uleb = require("./uleb.js");
|
|
25
|
+
class BcsReader {
|
|
26
|
+
/**
|
|
27
|
+
* @param {Uint8Array} data Data to use as a buffer.
|
|
28
|
+
*/
|
|
29
|
+
constructor(data) {
|
|
30
|
+
this.bytePosition = 0;
|
|
31
|
+
this.dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
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
|
+
* Read U8 value from the buffer and shift cursor by 1.
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
read8() {
|
|
48
|
+
const value = this.dataView.getUint8(this.bytePosition);
|
|
49
|
+
this.shift(1);
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Read U16 value from the buffer and shift cursor by 2.
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
read16() {
|
|
57
|
+
const value = this.dataView.getUint16(this.bytePosition, true);
|
|
58
|
+
this.shift(2);
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Read U32 value from the buffer and shift cursor by 4.
|
|
63
|
+
* @returns
|
|
64
|
+
*/
|
|
65
|
+
read32() {
|
|
66
|
+
const value = this.dataView.getUint32(this.bytePosition, true);
|
|
67
|
+
this.shift(4);
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Read U64 value from the buffer and shift cursor by 8.
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
read64() {
|
|
75
|
+
const value1 = this.read32();
|
|
76
|
+
const value2 = this.read32();
|
|
77
|
+
const result = value2.toString(16) + value1.toString(16).padStart(8, "0");
|
|
78
|
+
return BigInt("0x" + result).toString(10);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Read U128 value from the buffer and shift cursor by 16.
|
|
82
|
+
*/
|
|
83
|
+
read128() {
|
|
84
|
+
const value1 = BigInt(this.read64());
|
|
85
|
+
const value2 = BigInt(this.read64());
|
|
86
|
+
const result = value2.toString(16) + value1.toString(16).padStart(16, "0");
|
|
87
|
+
return BigInt("0x" + result).toString(10);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Read U128 value from the buffer and shift cursor by 32.
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
read256() {
|
|
94
|
+
const value1 = BigInt(this.read128());
|
|
95
|
+
const value2 = BigInt(this.read128());
|
|
96
|
+
const result = value2.toString(16) + value1.toString(16).padStart(32, "0");
|
|
97
|
+
return BigInt("0x" + result).toString(10);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Read `num` number of bytes from the buffer and shift cursor by `num`.
|
|
101
|
+
* @param num Number of bytes to read.
|
|
102
|
+
*/
|
|
103
|
+
readBytes(num) {
|
|
104
|
+
const start = this.bytePosition + this.dataView.byteOffset;
|
|
105
|
+
const value = new Uint8Array(this.dataView.buffer, start, num);
|
|
106
|
+
this.shift(num);
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Read ULEB value - an integer of varying size. Used for enum indexes and
|
|
111
|
+
* vector lengths.
|
|
112
|
+
* @returns {Number} The ULEB value.
|
|
113
|
+
*/
|
|
114
|
+
readULEB() {
|
|
115
|
+
const start = this.bytePosition + this.dataView.byteOffset;
|
|
116
|
+
const buffer = new Uint8Array(this.dataView.buffer, start);
|
|
117
|
+
const { value, length } = (0, import_uleb.ulebDecode)(buffer);
|
|
118
|
+
this.shift(length);
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Read a BCS vector: read a length and then apply function `cb` X times
|
|
123
|
+
* where X is the length of the vector, defined as ULEB in BCS bytes.
|
|
124
|
+
* @param cb Callback to process elements of vector.
|
|
125
|
+
* @returns {Array<Any>} Array of the resulting values, returned by callback.
|
|
126
|
+
*/
|
|
127
|
+
readVec(cb) {
|
|
128
|
+
const length = this.readULEB();
|
|
129
|
+
const result = [];
|
|
130
|
+
for (let i = 0; i < length; i++) {
|
|
131
|
+
result.push(cb(this, i, length));
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# 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": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,kBAA2B;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;AACZ,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,SAAiB;AAChB,UAAM,SAAS,KAAK,OAAO;AAC3B,UAAM,SAAS,KAAK,OAAO;AAE3B,UAAM,SAAS,OAAO,SAAS,EAAE,IAAI,OAAO,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AAExE,WAAO,OAAO,OAAO,MAAM,EAAE,SAAS,EAAE;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA,EAIA,UAAkB;AACjB,UAAM,SAAS,OAAO,KAAK,OAAO,CAAC;AACnC,UAAM,SAAS,OAAO,KAAK,OAAO,CAAC;AACnC,UAAM,SAAS,OAAO,SAAS,EAAE,IAAI,OAAO,SAAS,EAAE,EAAE,SAAS,IAAI,GAAG;AAEzE,WAAO,OAAO,OAAO,MAAM,EAAE,SAAS,EAAE;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,UAAkB;AACjB,UAAM,SAAS,OAAO,KAAK,QAAQ,CAAC;AACpC,UAAM,SAAS,OAAO,KAAK,QAAQ,CAAC;AACpC,UAAM,SAAS,OAAO,SAAS,EAAE,IAAI,OAAO,SAAS,EAAE,EAAE,SAAS,IAAI,GAAG;AAEzE,WAAO,OAAO,OAAO,MAAM,EAAE,SAAS,EAAE;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA,EAKA,UAAU,KAAyB;AAClC,UAAM,QAAQ,KAAK,eAAe,KAAK,SAAS;AAChD,UAAM,QAAQ,IAAI,WAAW,KAAK,SAAS,QAAQ,OAAO,GAAG;AAE7D,SAAK,MAAM,GAAG;AAEd,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,WAAmB;AAClB,UAAM,QAAQ,KAAK,eAAe,KAAK,SAAS;AAChD,UAAM,SAAS,IAAI,WAAW,KAAK,SAAS,QAAQ,KAAK;AACzD,UAAM,EAAE,OAAO,OAAO,QAAI,wBAAW,MAAM;AAE3C,SAAK,MAAM,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,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var types_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(types_exports);
|
|
17
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/types.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Simplify } from '@haneullabs/utils';\nimport type { BcsType } from './bcs-type.js';\n\n/**\n * Supported encodings.\n * Used in `Reader.toString()` as well as in `decodeStr` and `encodeStr` functions.\n */\nexport type Encoding = 'base58' | 'base64' | 'hex';\n\nexport type InferBcsType<T extends BcsType<any>> = T extends BcsType<infer U, any> ? U : never;\nexport type InferBcsInput<T extends BcsType<any, any>> =\n\tT extends BcsType<any, infer U> ? U : never;\n\nexport type EnumOutputShape<\n\tT extends Record<string, unknown>,\n\tKeys extends string = Extract<keyof T, string>,\n\tValues = T[keyof T] extends infer Type ? (Type extends BcsType<infer U> ? U : never) : never,\n> = 0 extends Values\n\t? EnumOutputShapeWithKeys<T, never>\n\t: 0n extends Values\n\t\t? EnumOutputShapeWithKeys<T, never>\n\t\t: '' extends Values\n\t\t\t? EnumOutputShapeWithKeys<T, never>\n\t\t\t: false extends Values\n\t\t\t\t? EnumOutputShapeWithKeys<T, never>\n\t\t\t\t: EnumOutputShapeWithKeys<T, Keys>;\n\nexport type EnumOutputShapeWithKeys<T extends Record<string, unknown>, Keys extends string> = {\n\t[K in keyof T]: Exclude<Keys, K> extends infer Empty extends string\n\t\t? Simplify<\n\t\t\t\t{ [K2 in K]: T[K] } & { [K in Empty]?: never } & {\n\t\t\t\t\t$kind: K;\n\t\t\t\t}\n\t\t\t>\n\t\t: never;\n}[keyof T];\n\nexport type EnumInputShape<T extends Record<string, unknown>> = {\n\t[K in keyof T]: { [K2 in K]: T[K] };\n}[keyof T];\n\nexport type JoinString<T, Sep extends string> = T extends readonly [\n\tinfer F extends string,\n\t...infer R extends string[],\n]\n\t? [] extends R\n\t\t? F\n\t\t: `${F}${Sep}${JoinString<R, Sep>}`\n\t: '';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/cjs/uleb.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var uleb_exports = {};
|
|
20
|
+
__export(uleb_exports, {
|
|
21
|
+
ulebDecode: () => ulebDecode,
|
|
22
|
+
ulebEncode: () => ulebEncode
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(uleb_exports);
|
|
25
|
+
function ulebEncode(num) {
|
|
26
|
+
let bigNum = BigInt(num);
|
|
27
|
+
const arr = [];
|
|
28
|
+
let len = 0;
|
|
29
|
+
if (bigNum === 0n) {
|
|
30
|
+
return [0];
|
|
31
|
+
}
|
|
32
|
+
while (bigNum > 0) {
|
|
33
|
+
arr[len] = Number(bigNum & 0x7fn);
|
|
34
|
+
bigNum >>= 7n;
|
|
35
|
+
if (bigNum > 0n) {
|
|
36
|
+
arr[len] |= 128;
|
|
37
|
+
}
|
|
38
|
+
len += 1;
|
|
39
|
+
}
|
|
40
|
+
return arr;
|
|
41
|
+
}
|
|
42
|
+
function ulebDecode(arr) {
|
|
43
|
+
let total = 0n;
|
|
44
|
+
let shift = 0n;
|
|
45
|
+
let len = 0;
|
|
46
|
+
while (true) {
|
|
47
|
+
if (len >= arr.length) {
|
|
48
|
+
throw new Error("ULEB decode error: buffer overflow");
|
|
49
|
+
}
|
|
50
|
+
const byte = arr[len];
|
|
51
|
+
len += 1;
|
|
52
|
+
total += BigInt(byte & 127) << shift;
|
|
53
|
+
if ((byte & 128) === 0) {
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
shift += 7n;
|
|
57
|
+
}
|
|
58
|
+
if (total > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
59
|
+
throw new Error("ULEB decode error: value exceeds MAX_SAFE_INTEGER");
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
value: Number(total),
|
|
63
|
+
length: len
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
//# 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": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var utils_exports = {};
|
|
20
|
+
__export(utils_exports, {
|
|
21
|
+
decodeStr: () => decodeStr,
|
|
22
|
+
encodeStr: () => encodeStr,
|
|
23
|
+
splitGenericParameters: () => splitGenericParameters
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(utils_exports);
|
|
26
|
+
var import_utils = require("@haneullabs/utils");
|
|
27
|
+
function encodeStr(data, encoding) {
|
|
28
|
+
switch (encoding) {
|
|
29
|
+
case "base58":
|
|
30
|
+
return (0, import_utils.toBase58)(data);
|
|
31
|
+
case "base64":
|
|
32
|
+
return (0, import_utils.toBase64)(data);
|
|
33
|
+
case "hex":
|
|
34
|
+
return (0, import_utils.toHex)(data);
|
|
35
|
+
default:
|
|
36
|
+
throw new Error("Unsupported encoding, supported values are: base64, hex");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function decodeStr(data, encoding) {
|
|
40
|
+
switch (encoding) {
|
|
41
|
+
case "base58":
|
|
42
|
+
return (0, import_utils.fromBase58)(data);
|
|
43
|
+
case "base64":
|
|
44
|
+
return (0, import_utils.fromBase64)(data);
|
|
45
|
+
case "hex":
|
|
46
|
+
return (0, import_utils.fromHex)(data);
|
|
47
|
+
default:
|
|
48
|
+
throw new Error("Unsupported encoding, supported values are: base64, hex");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function splitGenericParameters(str, genericSeparators = ["<", ">"]) {
|
|
52
|
+
const [left, right] = genericSeparators;
|
|
53
|
+
const tok = [];
|
|
54
|
+
let word = "";
|
|
55
|
+
let nestedAngleBrackets = 0;
|
|
56
|
+
for (let i = 0; i < str.length; i++) {
|
|
57
|
+
const char = str[i];
|
|
58
|
+
if (char === left) {
|
|
59
|
+
nestedAngleBrackets++;
|
|
60
|
+
}
|
|
61
|
+
if (char === right) {
|
|
62
|
+
nestedAngleBrackets--;
|
|
63
|
+
}
|
|
64
|
+
if (nestedAngleBrackets === 0 && char === ",") {
|
|
65
|
+
tok.push(word.trim());
|
|
66
|
+
word = "";
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
word += char;
|
|
70
|
+
}
|
|
71
|
+
tok.push(word.trim());
|
|
72
|
+
return tok;
|
|
73
|
+
}
|
|
74
|
+
//# 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": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,mBAA2E;AAUpE,SAAS,UAAU,MAAkB,UAA4B;AACvE,UAAQ,UAAU;AAAA,IACjB,KAAK;AACJ,iBAAO,uBAAS,IAAI;AAAA,IACrB,KAAK;AACJ,iBAAO,uBAAS,IAAI;AAAA,IACrB,KAAK;AACJ,iBAAO,oBAAM,IAAI;AAAA,IAClB;AACC,YAAM,IAAI,MAAM,yDAAyD;AAAA,EAC3E;AACD;AASO,SAAS,UAAU,MAAc,UAAgC;AACvE,UAAQ,UAAU;AAAA,IACjB,KAAK;AACJ,iBAAO,yBAAW,IAAI;AAAA,IACvB,KAAK;AACJ,iBAAO,yBAAW,IAAI;AAAA,IACvB,KAAK;AACJ,iBAAO,sBAAQ,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
|
+
}
|