@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,196 @@
|
|
|
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 writer_exports = {};
|
|
20
|
+
__export(writer_exports, {
|
|
21
|
+
BcsWriter: () => BcsWriter
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(writer_exports);
|
|
24
|
+
var import_uleb = require("./uleb.js");
|
|
25
|
+
var import_utils = require("./utils.js");
|
|
26
|
+
class BcsWriter {
|
|
27
|
+
constructor({
|
|
28
|
+
initialSize = 1024,
|
|
29
|
+
maxSize = Infinity,
|
|
30
|
+
allocateSize = 1024
|
|
31
|
+
} = {}) {
|
|
32
|
+
this.bytePosition = 0;
|
|
33
|
+
this.size = initialSize;
|
|
34
|
+
this.maxSize = maxSize;
|
|
35
|
+
this.allocateSize = allocateSize;
|
|
36
|
+
this.dataView = new DataView(new ArrayBuffer(initialSize));
|
|
37
|
+
}
|
|
38
|
+
ensureSizeOrGrow(bytes) {
|
|
39
|
+
const requiredSize = this.bytePosition + bytes;
|
|
40
|
+
if (requiredSize > this.size) {
|
|
41
|
+
const nextSize = Math.min(
|
|
42
|
+
this.maxSize,
|
|
43
|
+
Math.max(this.size + requiredSize, this.size + this.allocateSize)
|
|
44
|
+
);
|
|
45
|
+
if (requiredSize > nextSize) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
`Attempting to serialize to BCS, but buffer does not have enough size. Allocated size: ${this.size}, Max size: ${this.maxSize}, Required size: ${requiredSize}`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
this.size = nextSize;
|
|
51
|
+
const nextBuffer = new ArrayBuffer(this.size);
|
|
52
|
+
new Uint8Array(nextBuffer).set(new Uint8Array(this.dataView.buffer));
|
|
53
|
+
this.dataView = new DataView(nextBuffer);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Shift current cursor position by `bytes`.
|
|
58
|
+
*
|
|
59
|
+
* @param {Number} bytes Number of bytes to
|
|
60
|
+
* @returns {this} Self for possible chaining.
|
|
61
|
+
*/
|
|
62
|
+
shift(bytes) {
|
|
63
|
+
this.bytePosition += bytes;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Write a U8 value into a buffer and shift cursor position by 1.
|
|
68
|
+
* @param {Number} value Value to write.
|
|
69
|
+
* @returns {this}
|
|
70
|
+
*/
|
|
71
|
+
write8(value) {
|
|
72
|
+
this.ensureSizeOrGrow(1);
|
|
73
|
+
this.dataView.setUint8(this.bytePosition, Number(value));
|
|
74
|
+
return this.shift(1);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Write a U8 value into a buffer and shift cursor position by 1.
|
|
78
|
+
* @param {Number} value Value to write.
|
|
79
|
+
* @returns {this}
|
|
80
|
+
*/
|
|
81
|
+
writeBytes(bytes) {
|
|
82
|
+
this.ensureSizeOrGrow(bytes.length);
|
|
83
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
84
|
+
this.dataView.setUint8(this.bytePosition + i, bytes[i]);
|
|
85
|
+
}
|
|
86
|
+
return this.shift(bytes.length);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Write a U16 value into a buffer and shift cursor position by 2.
|
|
90
|
+
* @param {Number} value Value to write.
|
|
91
|
+
* @returns {this}
|
|
92
|
+
*/
|
|
93
|
+
write16(value) {
|
|
94
|
+
this.ensureSizeOrGrow(2);
|
|
95
|
+
this.dataView.setUint16(this.bytePosition, Number(value), true);
|
|
96
|
+
return this.shift(2);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Write a U32 value into a buffer and shift cursor position by 4.
|
|
100
|
+
* @param {Number} value Value to write.
|
|
101
|
+
* @returns {this}
|
|
102
|
+
*/
|
|
103
|
+
write32(value) {
|
|
104
|
+
this.ensureSizeOrGrow(4);
|
|
105
|
+
this.dataView.setUint32(this.bytePosition, Number(value), true);
|
|
106
|
+
return this.shift(4);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Write a U64 value into a buffer and shift cursor position by 8.
|
|
110
|
+
* @param {bigint} value Value to write.
|
|
111
|
+
* @returns {this}
|
|
112
|
+
*/
|
|
113
|
+
write64(value) {
|
|
114
|
+
toLittleEndian(BigInt(value), 8).forEach((el) => this.write8(el));
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Write a U128 value into a buffer and shift cursor position by 16.
|
|
119
|
+
*
|
|
120
|
+
* @param {bigint} value Value to write.
|
|
121
|
+
* @returns {this}
|
|
122
|
+
*/
|
|
123
|
+
write128(value) {
|
|
124
|
+
toLittleEndian(BigInt(value), 16).forEach((el) => this.write8(el));
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Write a U256 value into a buffer and shift cursor position by 16.
|
|
129
|
+
*
|
|
130
|
+
* @param {bigint} value Value to write.
|
|
131
|
+
* @returns {this}
|
|
132
|
+
*/
|
|
133
|
+
write256(value) {
|
|
134
|
+
toLittleEndian(BigInt(value), 32).forEach((el) => this.write8(el));
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Write a ULEB value into a buffer and shift cursor position by number of bytes
|
|
139
|
+
* written.
|
|
140
|
+
* @param {Number} value Value to write.
|
|
141
|
+
* @returns {this}
|
|
142
|
+
*/
|
|
143
|
+
writeULEB(value) {
|
|
144
|
+
(0, import_uleb.ulebEncode)(value).forEach((el) => this.write8(el));
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Write a vector into a buffer by first writing the vector length and then calling
|
|
149
|
+
* a callback on each passed value.
|
|
150
|
+
*
|
|
151
|
+
* @param {Array<Any>} vector Array of elements to write.
|
|
152
|
+
* @param {WriteVecCb} cb Callback to call on each element of the vector.
|
|
153
|
+
* @returns {this}
|
|
154
|
+
*/
|
|
155
|
+
writeVec(vector, cb) {
|
|
156
|
+
this.writeULEB(vector.length);
|
|
157
|
+
Array.from(vector).forEach((el, i) => cb(this, el, i, vector.length));
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Adds support for iterations over the object.
|
|
162
|
+
* @returns {Uint8Array}
|
|
163
|
+
*/
|
|
164
|
+
// oxlint-disable-next-line require-yields
|
|
165
|
+
*[Symbol.iterator]() {
|
|
166
|
+
for (let i = 0; i < this.bytePosition; i++) {
|
|
167
|
+
yield this.dataView.getUint8(i);
|
|
168
|
+
}
|
|
169
|
+
return this.toBytes();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Get underlying buffer taking only value bytes (in case initial buffer size was bigger).
|
|
173
|
+
* @returns {Uint8Array} Resulting bcs.
|
|
174
|
+
*/
|
|
175
|
+
toBytes() {
|
|
176
|
+
return new Uint8Array(this.dataView.buffer.slice(0, this.bytePosition));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Represent data as 'hex' or 'base64'
|
|
180
|
+
* @param encoding Encoding to use: 'base64' or 'hex'
|
|
181
|
+
*/
|
|
182
|
+
toString(encoding) {
|
|
183
|
+
return (0, import_utils.encodeStr)(this.toBytes(), encoding);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function toLittleEndian(bigint, size) {
|
|
187
|
+
const result = new Uint8Array(size);
|
|
188
|
+
let i = 0;
|
|
189
|
+
while (bigint > 0) {
|
|
190
|
+
result[i] = Number(bigint % BigInt(256));
|
|
191
|
+
bigint = bigint / BigInt(256);
|
|
192
|
+
i += 1;
|
|
193
|
+
}
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
//# 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 %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gCAAW,KAAK,EAAE,QAAQ,CAAC,OAAO,KAAK,OAAO,EAAE,CAAC;AACjD,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,SAAS,QAAe,IAAwE;AAC/F,SAAK,UAAU,OAAO,MAAM;AAC5B,UAAM,KAAK,MAAM,EAAE,QAAQ,CAAC,IAAI,MAAM,GAAG,MAAM,IAAI,GAAG,OAAO,MAAM,CAAC;AACpE,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,EAAE,OAAO,QAAQ,IAAwC;AACxD,aAAS,IAAI,GAAG,IAAI,KAAK,cAAc,KAAK;AAC3C,YAAM,KAAK,SAAS,SAAS,CAAC;AAAA,IAC/B;AACA,WAAO,KAAK,QAAQ;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAmC;AAClC,WAAO,IAAI,WAAW,KAAK,SAAS,OAAO,MAAM,GAAG,KAAK,YAAY,CAAC;AAAA,EACvE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAS,UAA4B;AACpC,eAAO,wBAAU,KAAK,QAAQ,GAAG,QAAQ;AAAA,EAC1C;AACD;AAEA,SAAS,eAAe,QAAgB,MAAc;AACrD,QAAM,SAAS,IAAI,WAAW,IAAI;AAClC,MAAI,IAAI;AACR,SAAO,SAAS,GAAG;AAClB,WAAO,CAAC,IAAI,OAAO,SAAS,OAAO,GAAG,CAAC;AACvC,aAAS,SAAS,OAAO,GAAG;AAC5B,SAAK;AAAA,EACN;AACA,SAAO;AACR;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { BcsReader } from './reader.js';
|
|
2
|
+
import type { BcsWriterOptions } from './writer.js';
|
|
3
|
+
import { BcsWriter } from './writer.js';
|
|
4
|
+
import type { EnumInputShape, EnumOutputShape, JoinString } from './types.js';
|
|
5
|
+
export interface BcsTypeOptions<T, Input = T, Name extends string = string> {
|
|
6
|
+
name?: Name;
|
|
7
|
+
validate?: (value: Input) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare class BcsType<T, Input = T, const Name extends string = string> {
|
|
10
|
+
#private;
|
|
11
|
+
$inferType: T;
|
|
12
|
+
$inferInput: Input;
|
|
13
|
+
name: Name;
|
|
14
|
+
read: (reader: BcsReader) => T;
|
|
15
|
+
serializedSize: (value: Input, options?: BcsWriterOptions) => number | null;
|
|
16
|
+
validate: (value: Input) => void;
|
|
17
|
+
constructor(options: {
|
|
18
|
+
name: Name;
|
|
19
|
+
read: (reader: BcsReader) => T;
|
|
20
|
+
write: (value: Input, writer: BcsWriter) => void;
|
|
21
|
+
serialize?: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;
|
|
22
|
+
serializedSize?: (value: Input) => number | null;
|
|
23
|
+
validate?: (value: Input) => void;
|
|
24
|
+
} & BcsTypeOptions<T, Input, Name>);
|
|
25
|
+
write(value: Input, writer: BcsWriter): void;
|
|
26
|
+
serialize(value: Input, options?: BcsWriterOptions): SerializedBcs<T, Input>;
|
|
27
|
+
parse(bytes: Uint8Array): T;
|
|
28
|
+
fromHex(hex: string): T;
|
|
29
|
+
fromBase58(b64: string): T;
|
|
30
|
+
fromBase64(b64: string): T;
|
|
31
|
+
transform<T2 = T, Input2 = Input, NewName extends string = Name>({ name, input, output, validate, }: {
|
|
32
|
+
input?: (val: Input2) => Input;
|
|
33
|
+
output?: (value: T) => T2;
|
|
34
|
+
} & BcsTypeOptions<T2, Input2, NewName>): BcsType<T2, Input2, NewName>;
|
|
35
|
+
}
|
|
36
|
+
declare const SERIALIZED_BCS_BRAND: never;
|
|
37
|
+
export declare function isSerializedBcs(obj: unknown): obj is SerializedBcs<unknown>;
|
|
38
|
+
export declare class SerializedBcs<T, Input = T> {
|
|
39
|
+
#private;
|
|
40
|
+
[SERIALIZED_BCS_BRAND]: boolean;
|
|
41
|
+
constructor(schema: BcsType<T, Input>, bytes: Uint8Array<ArrayBuffer>);
|
|
42
|
+
toBytes(): Uint8Array<ArrayBuffer>;
|
|
43
|
+
toHex(): string;
|
|
44
|
+
toBase64(): string;
|
|
45
|
+
toBase58(): string;
|
|
46
|
+
parse(): T;
|
|
47
|
+
}
|
|
48
|
+
export declare function fixedSizeBcsType<T, Input = T, const Name extends string = string>({ size, ...options }: {
|
|
49
|
+
name: Name;
|
|
50
|
+
size: number;
|
|
51
|
+
read: (reader: BcsReader) => T;
|
|
52
|
+
write: (value: Input, writer: BcsWriter) => void;
|
|
53
|
+
} & BcsTypeOptions<T, Input, Name>): BcsType<T, Input, Name>;
|
|
54
|
+
export declare function uIntBcsType<const Name extends string = string>({ readMethod, writeMethod, ...options }: {
|
|
55
|
+
name: Name;
|
|
56
|
+
size: number;
|
|
57
|
+
readMethod: `read${8 | 16 | 32}`;
|
|
58
|
+
writeMethod: `write${8 | 16 | 32}`;
|
|
59
|
+
maxValue: number;
|
|
60
|
+
} & BcsTypeOptions<number, number, Name>): BcsType<number, number, Name>;
|
|
61
|
+
export declare function bigUIntBcsType<const Name extends string = string>({ readMethod, writeMethod, ...options }: {
|
|
62
|
+
name: Name;
|
|
63
|
+
size: number;
|
|
64
|
+
readMethod: `read${64 | 128 | 256}`;
|
|
65
|
+
writeMethod: `write${64 | 128 | 256}`;
|
|
66
|
+
maxValue: bigint;
|
|
67
|
+
} & BcsTypeOptions<string, string | number | bigint>): BcsType<string, string | number | bigint, Name>;
|
|
68
|
+
export declare function dynamicSizeBcsType<T, Input = T, const Name extends string = string>({ serialize, ...options }: {
|
|
69
|
+
name: Name;
|
|
70
|
+
read: (reader: BcsReader) => T;
|
|
71
|
+
serialize: (value: Input, options?: BcsWriterOptions) => Uint8Array<ArrayBuffer>;
|
|
72
|
+
} & BcsTypeOptions<T, Input>): BcsType<T, Input, string>;
|
|
73
|
+
export declare function stringLikeBcsType<const Name extends string = string>({ toBytes, fromBytes, ...options }: {
|
|
74
|
+
name: Name;
|
|
75
|
+
toBytes: (value: string) => Uint8Array;
|
|
76
|
+
fromBytes: (bytes: Uint8Array) => string;
|
|
77
|
+
serializedSize?: (value: string) => number | null;
|
|
78
|
+
} & BcsTypeOptions<string, string, Name>): BcsType<string, string, Name>;
|
|
79
|
+
export declare function lazyBcsType<T, Input>(cb: () => BcsType<T, Input>): BcsType<T, Input, string>;
|
|
80
|
+
export interface BcsStructOptions<T extends Record<string, BcsType<any>>, Name extends string = string> extends Omit<BcsTypeOptions<{
|
|
81
|
+
[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;
|
|
82
|
+
}, {
|
|
83
|
+
[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;
|
|
84
|
+
}, Name>, 'name'> {
|
|
85
|
+
name: Name;
|
|
86
|
+
fields: T;
|
|
87
|
+
}
|
|
88
|
+
export declare class BcsStruct<T extends Record<string, BcsType<any>>, const Name extends string = string> extends BcsType<{
|
|
89
|
+
[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : never;
|
|
90
|
+
}, {
|
|
91
|
+
[K in keyof T]: T[K] extends BcsType<any, infer U> ? U : never;
|
|
92
|
+
}, Name> {
|
|
93
|
+
constructor({ name, fields, ...options }: BcsStructOptions<T, Name>);
|
|
94
|
+
}
|
|
95
|
+
export interface BcsEnumOptions<T extends Record<string, BcsType<any> | null>, Name extends string = string> extends Omit<BcsTypeOptions<EnumOutputShape<{
|
|
96
|
+
[K in keyof T]: T[K] extends BcsType<infer U, any, any> ? U : true;
|
|
97
|
+
}>, EnumInputShape<{
|
|
98
|
+
[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;
|
|
99
|
+
}>, Name>, 'name'> {
|
|
100
|
+
name: Name;
|
|
101
|
+
fields: T;
|
|
102
|
+
}
|
|
103
|
+
export declare class BcsEnum<T extends Record<string, BcsType<any> | null>, const Name extends string = string> extends BcsType<EnumOutputShape<{
|
|
104
|
+
[K in keyof T]: T[K] extends BcsType<infer U, any> ? U : true;
|
|
105
|
+
}>, EnumInputShape<{
|
|
106
|
+
[K in keyof T]: T[K] extends BcsType<any, infer U, any> ? U : boolean | object | null;
|
|
107
|
+
}>, Name> {
|
|
108
|
+
constructor({ fields, ...options }: BcsEnumOptions<T, Name>);
|
|
109
|
+
}
|
|
110
|
+
export interface BcsTupleOptions<T extends readonly BcsType<any>[], Name extends string> extends Omit<BcsTypeOptions<{
|
|
111
|
+
-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;
|
|
112
|
+
}, {
|
|
113
|
+
[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;
|
|
114
|
+
}, Name>, 'name'> {
|
|
115
|
+
name?: Name;
|
|
116
|
+
fields: T;
|
|
117
|
+
}
|
|
118
|
+
export declare class BcsTuple<const T extends readonly BcsType<any>[], const Name extends string = `(${JoinString<{
|
|
119
|
+
[K in keyof T]: T[K] extends BcsType<any, any, infer T> ? T : never;
|
|
120
|
+
}, ', '>})`> extends BcsType<{
|
|
121
|
+
-readonly [K in keyof T]: T[K] extends BcsType<infer T, any> ? T : never;
|
|
122
|
+
}, {
|
|
123
|
+
[K in keyof T]: T[K] extends BcsType<any, infer T> ? T : never;
|
|
124
|
+
}, Name> {
|
|
125
|
+
constructor({ fields, name, ...options }: BcsTupleOptions<T, Name>);
|
|
126
|
+
}
|
|
127
|
+
export {};
|