@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
package/src/reader.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { ulebDecode } from './uleb.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Class used for reading BCS data chunk by chunk. Meant to be used
|
|
8
|
+
* by some wrapper, which will make sure that data is valid and is
|
|
9
|
+
* matching the desired format.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // data for this example is:
|
|
13
|
+
* // { a: u8, b: u32, c: bool, d: u64 }
|
|
14
|
+
*
|
|
15
|
+
* let reader = new BcsReader("647f1a060001ffffe7890423c78a050102030405");
|
|
16
|
+
* let field1 = reader.read8();
|
|
17
|
+
* let field2 = reader.read32();
|
|
18
|
+
* let field3 = reader.read8() === '1'; // bool
|
|
19
|
+
* let field4 = reader.read64();
|
|
20
|
+
* // ....
|
|
21
|
+
*
|
|
22
|
+
* Reading vectors is another deal in bcs. To read a vector, you first need to read
|
|
23
|
+
* its length using {@link readULEB}. Here's an example:
|
|
24
|
+
* @example
|
|
25
|
+
* // data encoded: { field: [1, 2, 3, 4, 5] }
|
|
26
|
+
* let reader = new BcsReader("050102030405");
|
|
27
|
+
* let vec_length = reader.readULEB();
|
|
28
|
+
* let elements = [];
|
|
29
|
+
* for (let i = 0; i < vec_length; i++) {
|
|
30
|
+
* elements.push(reader.read8());
|
|
31
|
+
* }
|
|
32
|
+
* console.log(elements); // [1,2,3,4,5]
|
|
33
|
+
*
|
|
34
|
+
* @param {String} data HEX-encoded data (serialized BCS)
|
|
35
|
+
*/
|
|
36
|
+
export class BcsReader {
|
|
37
|
+
private dataView: DataView;
|
|
38
|
+
private bytePosition: number = 0;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {Uint8Array} data Data to use as a buffer.
|
|
42
|
+
*/
|
|
43
|
+
constructor(data: Uint8Array) {
|
|
44
|
+
this.dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Shift current cursor position by `bytes`.
|
|
48
|
+
*
|
|
49
|
+
* @param {Number} bytes Number of bytes to
|
|
50
|
+
* @returns {this} Self for possible chaining.
|
|
51
|
+
*/
|
|
52
|
+
shift(bytes: number) {
|
|
53
|
+
this.bytePosition += bytes;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Read U8 value from the buffer and shift cursor by 1.
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
read8(): number {
|
|
61
|
+
const value = this.dataView.getUint8(this.bytePosition);
|
|
62
|
+
this.shift(1);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Read U16 value from the buffer and shift cursor by 2.
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
read16(): number {
|
|
70
|
+
const value = this.dataView.getUint16(this.bytePosition, true);
|
|
71
|
+
this.shift(2);
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Read U32 value from the buffer and shift cursor by 4.
|
|
76
|
+
* @returns
|
|
77
|
+
*/
|
|
78
|
+
read32(): number {
|
|
79
|
+
const value = this.dataView.getUint32(this.bytePosition, true);
|
|
80
|
+
this.shift(4);
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Read U64 value from the buffer and shift cursor by 8.
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
read64(): string {
|
|
88
|
+
const value1 = this.read32();
|
|
89
|
+
const value2 = this.read32();
|
|
90
|
+
|
|
91
|
+
const result = value2.toString(16) + value1.toString(16).padStart(8, '0');
|
|
92
|
+
|
|
93
|
+
return BigInt('0x' + result).toString(10);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Read U128 value from the buffer and shift cursor by 16.
|
|
97
|
+
*/
|
|
98
|
+
read128(): string {
|
|
99
|
+
const value1 = BigInt(this.read64());
|
|
100
|
+
const value2 = BigInt(this.read64());
|
|
101
|
+
const result = value2.toString(16) + value1.toString(16).padStart(16, '0');
|
|
102
|
+
|
|
103
|
+
return BigInt('0x' + result).toString(10);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Read U128 value from the buffer and shift cursor by 32.
|
|
107
|
+
* @returns
|
|
108
|
+
*/
|
|
109
|
+
read256(): string {
|
|
110
|
+
const value1 = BigInt(this.read128());
|
|
111
|
+
const value2 = BigInt(this.read128());
|
|
112
|
+
const result = value2.toString(16) + value1.toString(16).padStart(32, '0');
|
|
113
|
+
|
|
114
|
+
return BigInt('0x' + result).toString(10);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Read `num` number of bytes from the buffer and shift cursor by `num`.
|
|
118
|
+
* @param num Number of bytes to read.
|
|
119
|
+
*/
|
|
120
|
+
readBytes(num: number): Uint8Array {
|
|
121
|
+
const start = this.bytePosition + this.dataView.byteOffset;
|
|
122
|
+
const value = new Uint8Array(this.dataView.buffer, start, num);
|
|
123
|
+
|
|
124
|
+
this.shift(num);
|
|
125
|
+
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Read ULEB value - an integer of varying size. Used for enum indexes and
|
|
130
|
+
* vector lengths.
|
|
131
|
+
* @returns {Number} The ULEB value.
|
|
132
|
+
*/
|
|
133
|
+
readULEB(): number {
|
|
134
|
+
const start = this.bytePosition + this.dataView.byteOffset;
|
|
135
|
+
const buffer = new Uint8Array(this.dataView.buffer, start);
|
|
136
|
+
const { value, length } = ulebDecode(buffer);
|
|
137
|
+
|
|
138
|
+
this.shift(length);
|
|
139
|
+
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Read a BCS vector: read a length and then apply function `cb` X times
|
|
144
|
+
* where X is the length of the vector, defined as ULEB in BCS bytes.
|
|
145
|
+
* @param cb Callback to process elements of vector.
|
|
146
|
+
* @returns {Array<Any>} Array of the resulting values, returned by callback.
|
|
147
|
+
*/
|
|
148
|
+
readVec(cb: (reader: BcsReader, i: number, length: number) => any): any[] {
|
|
149
|
+
const length = this.readULEB();
|
|
150
|
+
const result = [];
|
|
151
|
+
for (let i = 0; i < length; i++) {
|
|
152
|
+
result.push(cb(this, i, length));
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { Simplify } from '@haneullabs/utils';
|
|
5
|
+
import type { BcsType } from './bcs-type.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Supported encodings.
|
|
9
|
+
* Used in `Reader.toString()` as well as in `decodeStr` and `encodeStr` functions.
|
|
10
|
+
*/
|
|
11
|
+
export type Encoding = 'base58' | 'base64' | 'hex';
|
|
12
|
+
|
|
13
|
+
export type InferBcsType<T extends BcsType<any>> = T extends BcsType<infer U, any> ? U : never;
|
|
14
|
+
export type InferBcsInput<T extends BcsType<any, any>> =
|
|
15
|
+
T extends BcsType<any, infer U> ? U : never;
|
|
16
|
+
|
|
17
|
+
export type EnumOutputShape<
|
|
18
|
+
T extends Record<string, unknown>,
|
|
19
|
+
Keys extends string = Extract<keyof T, string>,
|
|
20
|
+
Values = T[keyof T] extends infer Type ? (Type extends BcsType<infer U> ? U : never) : never,
|
|
21
|
+
> = 0 extends Values
|
|
22
|
+
? EnumOutputShapeWithKeys<T, never>
|
|
23
|
+
: 0n extends Values
|
|
24
|
+
? EnumOutputShapeWithKeys<T, never>
|
|
25
|
+
: '' extends Values
|
|
26
|
+
? EnumOutputShapeWithKeys<T, never>
|
|
27
|
+
: false extends Values
|
|
28
|
+
? EnumOutputShapeWithKeys<T, never>
|
|
29
|
+
: EnumOutputShapeWithKeys<T, Keys>;
|
|
30
|
+
|
|
31
|
+
export type EnumOutputShapeWithKeys<T extends Record<string, unknown>, Keys extends string> = {
|
|
32
|
+
[K in keyof T]: Exclude<Keys, K> extends infer Empty extends string
|
|
33
|
+
? Simplify<
|
|
34
|
+
{ [K2 in K]: T[K] } & { [K in Empty]?: never } & {
|
|
35
|
+
$kind: K;
|
|
36
|
+
}
|
|
37
|
+
>
|
|
38
|
+
: never;
|
|
39
|
+
}[keyof T];
|
|
40
|
+
|
|
41
|
+
export type EnumInputShape<T extends Record<string, unknown>> = {
|
|
42
|
+
[K in keyof T]: { [K2 in K]: T[K] };
|
|
43
|
+
}[keyof T];
|
|
44
|
+
|
|
45
|
+
export type JoinString<T, Sep extends string> = T extends readonly [
|
|
46
|
+
infer F extends string,
|
|
47
|
+
...infer R extends string[],
|
|
48
|
+
]
|
|
49
|
+
? [] extends R
|
|
50
|
+
? F
|
|
51
|
+
: `${F}${Sep}${JoinString<R, Sep>}`
|
|
52
|
+
: '';
|
package/src/uleb.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Helper utility: write number as an ULEB array.
|
|
5
|
+
// Original code is taken from: https://www.npmjs.com/package/uleb128 (no longer exists)
|
|
6
|
+
export function ulebEncode(num: number | bigint): number[] {
|
|
7
|
+
let bigNum = BigInt(num);
|
|
8
|
+
const arr: number[] = [];
|
|
9
|
+
let len = 0;
|
|
10
|
+
|
|
11
|
+
if (bigNum === 0n) {
|
|
12
|
+
return [0];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
while (bigNum > 0) {
|
|
16
|
+
arr[len] = Number(bigNum & 0x7fn);
|
|
17
|
+
bigNum >>= 7n;
|
|
18
|
+
if (bigNum > 0n) {
|
|
19
|
+
arr[len] |= 0x80;
|
|
20
|
+
}
|
|
21
|
+
len += 1;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return arr;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Helper utility: decode ULEB as an array of numbers.
|
|
28
|
+
// Original code is taken from: https://www.npmjs.com/package/uleb128 (no longer exists)
|
|
29
|
+
export function ulebDecode(arr: number[] | Uint8Array): {
|
|
30
|
+
value: number;
|
|
31
|
+
length: number;
|
|
32
|
+
} {
|
|
33
|
+
let total = 0n;
|
|
34
|
+
let shift = 0n;
|
|
35
|
+
let len = 0;
|
|
36
|
+
|
|
37
|
+
// eslint-disable-next-line no-constant-condition
|
|
38
|
+
while (true) {
|
|
39
|
+
if (len >= arr.length) {
|
|
40
|
+
throw new Error('ULEB decode error: buffer overflow');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const byte = arr[len];
|
|
44
|
+
len += 1;
|
|
45
|
+
total += BigInt(byte & 0x7f) << shift;
|
|
46
|
+
if ((byte & 0x80) === 0) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
shift += 7n;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// TODO: return bigint in next major version
|
|
53
|
+
if (total > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
54
|
+
throw new Error('ULEB decode error: value exceeds MAX_SAFE_INTEGER');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
value: Number(total),
|
|
59
|
+
length: len,
|
|
60
|
+
};
|
|
61
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { fromBase58, fromBase64, fromHex, toBase58, toBase64, toHex } from '@haneullabs/utils';
|
|
5
|
+
import type { Encoding } from './types.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Encode data with either `hex` or `base64`.
|
|
9
|
+
*
|
|
10
|
+
* @param {Uint8Array} data Data to encode.
|
|
11
|
+
* @param {String} encoding Encoding to use: base64 or hex
|
|
12
|
+
* @returns {String} Encoded value.
|
|
13
|
+
*/
|
|
14
|
+
export function encodeStr(data: Uint8Array, encoding: Encoding): string {
|
|
15
|
+
switch (encoding) {
|
|
16
|
+
case 'base58':
|
|
17
|
+
return toBase58(data);
|
|
18
|
+
case 'base64':
|
|
19
|
+
return toBase64(data);
|
|
20
|
+
case 'hex':
|
|
21
|
+
return toHex(data);
|
|
22
|
+
default:
|
|
23
|
+
throw new Error('Unsupported encoding, supported values are: base64, hex');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Decode either `base64` or `hex` data.
|
|
29
|
+
*
|
|
30
|
+
* @param {String} data Data to encode.
|
|
31
|
+
* @param {String} encoding Encoding to use: base64 or hex
|
|
32
|
+
* @returns {Uint8Array} Encoded value.
|
|
33
|
+
*/
|
|
34
|
+
export function decodeStr(data: string, encoding: Encoding): Uint8Array {
|
|
35
|
+
switch (encoding) {
|
|
36
|
+
case 'base58':
|
|
37
|
+
return fromBase58(data);
|
|
38
|
+
case 'base64':
|
|
39
|
+
return fromBase64(data);
|
|
40
|
+
case 'hex':
|
|
41
|
+
return fromHex(data);
|
|
42
|
+
default:
|
|
43
|
+
throw new Error('Unsupported encoding, supported values are: base64, hex');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function splitGenericParameters(
|
|
48
|
+
str: string,
|
|
49
|
+
genericSeparators: [string, string] = ['<', '>'],
|
|
50
|
+
) {
|
|
51
|
+
const [left, right] = genericSeparators;
|
|
52
|
+
const tok = [];
|
|
53
|
+
let word = '';
|
|
54
|
+
let nestedAngleBrackets = 0;
|
|
55
|
+
|
|
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
|
+
|
|
72
|
+
tok.push(word.trim());
|
|
73
|
+
|
|
74
|
+
return tok;
|
|
75
|
+
}
|
package/src/writer.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { Encoding } from './types.js';
|
|
5
|
+
import { ulebEncode } from './uleb.js';
|
|
6
|
+
import { encodeStr } from './utils.js';
|
|
7
|
+
|
|
8
|
+
export interface BcsWriterOptions {
|
|
9
|
+
/** The initial size (in bytes) of the buffer tht will be allocated */
|
|
10
|
+
initialSize?: number;
|
|
11
|
+
/** The maximum size (in bytes) that the buffer is allowed to grow to */
|
|
12
|
+
maxSize?: number;
|
|
13
|
+
/** The amount of bytes that will be allocated whenever additional memory is required */
|
|
14
|
+
allocateSize?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Class used to write BCS data into a buffer. Initializer requires
|
|
19
|
+
* some size of a buffer to init; default value for this buffer is 1KB.
|
|
20
|
+
*
|
|
21
|
+
* Most methods are chainable, so it is possible to write them in one go.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* let serialized = new BcsWriter()
|
|
25
|
+
* .write8(10)
|
|
26
|
+
* .write32(1000000)
|
|
27
|
+
* .write64(10000001000000)
|
|
28
|
+
* .hex();
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Set of methods that allows data encoding/decoding as standalone
|
|
33
|
+
* BCS value or a part of a composed structure/vector.
|
|
34
|
+
*/
|
|
35
|
+
export class BcsWriter {
|
|
36
|
+
private dataView: DataView<ArrayBuffer>;
|
|
37
|
+
private bytePosition: number = 0;
|
|
38
|
+
private size: number;
|
|
39
|
+
private maxSize: number;
|
|
40
|
+
private allocateSize: number;
|
|
41
|
+
|
|
42
|
+
constructor({
|
|
43
|
+
initialSize = 1024,
|
|
44
|
+
maxSize = Infinity,
|
|
45
|
+
allocateSize = 1024,
|
|
46
|
+
}: BcsWriterOptions = {}) {
|
|
47
|
+
this.size = initialSize;
|
|
48
|
+
this.maxSize = maxSize;
|
|
49
|
+
this.allocateSize = allocateSize;
|
|
50
|
+
this.dataView = new DataView(new ArrayBuffer(initialSize));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private ensureSizeOrGrow(bytes: number) {
|
|
54
|
+
const requiredSize = this.bytePosition + bytes;
|
|
55
|
+
if (requiredSize > this.size) {
|
|
56
|
+
const nextSize = Math.min(
|
|
57
|
+
this.maxSize,
|
|
58
|
+
Math.max(this.size + requiredSize, this.size + this.allocateSize),
|
|
59
|
+
);
|
|
60
|
+
if (requiredSize > nextSize) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
`Attempting to serialize to BCS, but buffer does not have enough size. Allocated size: ${this.size}, Max size: ${this.maxSize}, Required size: ${requiredSize}`,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
this.size = nextSize;
|
|
67
|
+
const nextBuffer = new ArrayBuffer(this.size);
|
|
68
|
+
new Uint8Array(nextBuffer).set(new Uint8Array(this.dataView.buffer));
|
|
69
|
+
this.dataView = new DataView(nextBuffer);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Shift current cursor position by `bytes`.
|
|
75
|
+
*
|
|
76
|
+
* @param {Number} bytes Number of bytes to
|
|
77
|
+
* @returns {this} Self for possible chaining.
|
|
78
|
+
*/
|
|
79
|
+
shift(bytes: number): this {
|
|
80
|
+
this.bytePosition += bytes;
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Write a U8 value into a buffer and shift cursor position by 1.
|
|
85
|
+
* @param {Number} value Value to write.
|
|
86
|
+
* @returns {this}
|
|
87
|
+
*/
|
|
88
|
+
write8(value: number | bigint): this {
|
|
89
|
+
this.ensureSizeOrGrow(1);
|
|
90
|
+
this.dataView.setUint8(this.bytePosition, Number(value));
|
|
91
|
+
return this.shift(1);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Write a U8 value into a buffer and shift cursor position by 1.
|
|
96
|
+
* @param {Number} value Value to write.
|
|
97
|
+
* @returns {this}
|
|
98
|
+
*/
|
|
99
|
+
writeBytes(bytes: Uint8Array): this {
|
|
100
|
+
this.ensureSizeOrGrow(bytes.length);
|
|
101
|
+
|
|
102
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
103
|
+
this.dataView.setUint8(this.bytePosition + i, bytes[i]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return this.shift(bytes.length);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Write a U16 value into a buffer and shift cursor position by 2.
|
|
110
|
+
* @param {Number} value Value to write.
|
|
111
|
+
* @returns {this}
|
|
112
|
+
*/
|
|
113
|
+
write16(value: number | bigint): this {
|
|
114
|
+
this.ensureSizeOrGrow(2);
|
|
115
|
+
this.dataView.setUint16(this.bytePosition, Number(value), true);
|
|
116
|
+
return this.shift(2);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Write a U32 value into a buffer and shift cursor position by 4.
|
|
120
|
+
* @param {Number} value Value to write.
|
|
121
|
+
* @returns {this}
|
|
122
|
+
*/
|
|
123
|
+
write32(value: number | bigint): this {
|
|
124
|
+
this.ensureSizeOrGrow(4);
|
|
125
|
+
this.dataView.setUint32(this.bytePosition, Number(value), true);
|
|
126
|
+
return this.shift(4);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Write a U64 value into a buffer and shift cursor position by 8.
|
|
130
|
+
* @param {bigint} value Value to write.
|
|
131
|
+
* @returns {this}
|
|
132
|
+
*/
|
|
133
|
+
write64(value: number | bigint): this {
|
|
134
|
+
toLittleEndian(BigInt(value), 8).forEach((el) => this.write8(el));
|
|
135
|
+
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Write a U128 value into a buffer and shift cursor position by 16.
|
|
140
|
+
*
|
|
141
|
+
* @param {bigint} value Value to write.
|
|
142
|
+
* @returns {this}
|
|
143
|
+
*/
|
|
144
|
+
write128(value: number | bigint): this {
|
|
145
|
+
toLittleEndian(BigInt(value), 16).forEach((el) => this.write8(el));
|
|
146
|
+
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Write a U256 value into a buffer and shift cursor position by 16.
|
|
151
|
+
*
|
|
152
|
+
* @param {bigint} value Value to write.
|
|
153
|
+
* @returns {this}
|
|
154
|
+
*/
|
|
155
|
+
write256(value: number | bigint): this {
|
|
156
|
+
toLittleEndian(BigInt(value), 32).forEach((el) => this.write8(el));
|
|
157
|
+
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Write a ULEB value into a buffer and shift cursor position by number of bytes
|
|
162
|
+
* written.
|
|
163
|
+
* @param {Number} value Value to write.
|
|
164
|
+
* @returns {this}
|
|
165
|
+
*/
|
|
166
|
+
writeULEB(value: number): this {
|
|
167
|
+
ulebEncode(value).forEach((el) => this.write8(el));
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Write a vector into a buffer by first writing the vector length and then calling
|
|
172
|
+
* a callback on each passed value.
|
|
173
|
+
*
|
|
174
|
+
* @param {Array<Any>} vector Array of elements to write.
|
|
175
|
+
* @param {WriteVecCb} cb Callback to call on each element of the vector.
|
|
176
|
+
* @returns {this}
|
|
177
|
+
*/
|
|
178
|
+
writeVec(vector: any[], cb: (writer: BcsWriter, el: any, i: number, len: number) => void): this {
|
|
179
|
+
this.writeULEB(vector.length);
|
|
180
|
+
Array.from(vector).forEach((el, i) => cb(this, el, i, vector.length));
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Adds support for iterations over the object.
|
|
186
|
+
* @returns {Uint8Array}
|
|
187
|
+
*/
|
|
188
|
+
// oxlint-disable-next-line require-yields
|
|
189
|
+
*[Symbol.iterator](): Iterator<number, Iterable<number>> {
|
|
190
|
+
for (let i = 0; i < this.bytePosition; i++) {
|
|
191
|
+
yield this.dataView.getUint8(i);
|
|
192
|
+
}
|
|
193
|
+
return this.toBytes();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Get underlying buffer taking only value bytes (in case initial buffer size was bigger).
|
|
198
|
+
* @returns {Uint8Array} Resulting bcs.
|
|
199
|
+
*/
|
|
200
|
+
toBytes(): Uint8Array<ArrayBuffer> {
|
|
201
|
+
return new Uint8Array(this.dataView.buffer.slice(0, this.bytePosition));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Represent data as 'hex' or 'base64'
|
|
206
|
+
* @param encoding Encoding to use: 'base64' or 'hex'
|
|
207
|
+
*/
|
|
208
|
+
toString(encoding: Encoding): string {
|
|
209
|
+
return encodeStr(this.toBytes(), encoding);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function toLittleEndian(bigint: bigint, size: number) {
|
|
214
|
+
const result = new Uint8Array(size);
|
|
215
|
+
let i = 0;
|
|
216
|
+
while (bigint > 0) {
|
|
217
|
+
result[i] = Number(bigint % BigInt(256));
|
|
218
|
+
bigint = bigint / BigInt(256);
|
|
219
|
+
i += 1;
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|