@layerzerolabs/lz-serdes 3.0.15 → 3.0.16
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 +6 -0
- package/README.md +105 -0
- package/dist/index.cjs +84 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +214 -14
- package/dist/index.d.ts +214 -14
- package/dist/index.mjs +84 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @layerzerolabs/lz-serdes
|
|
2
|
+
|
|
3
|
+
The LayerZero SerDes package provides essential functions and interfaces for serialization and deserialization of data. It includes utilities for serializing and deserializing various data types.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Serialization**: Serialize various data types into bytes.
|
|
8
|
+
- **Deserialization**: Deserialize bytes into various data types.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
To install the LayerZero SerDes package, you can use npm or yarn:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install @layerzerolabs/lz-serdes
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
or
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
yarn add @layerzerolabs/lz-serdes
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Serialization
|
|
27
|
+
|
|
28
|
+
Class representing a serializer for various data types.
|
|
29
|
+
|
|
30
|
+
- serializeStr(value: string): void: Serializes a string.
|
|
31
|
+
- serializeBytes(value: Bytes): void: Serializes an array of bytes.
|
|
32
|
+
- serializeBool(value: boolean): void: Serializes a boolean value.
|
|
33
|
+
- serializeU8(value: Uint8): void: Serializes a uint8 number.
|
|
34
|
+
- serializeU16(value: Uint16): void: Serializes a uint16 number.
|
|
35
|
+
- serializeU32(value: Uint32): void: Serializes a uint32 number.
|
|
36
|
+
- serializeU64(value: AnyNumber): void: Serializes a uint64 number.
|
|
37
|
+
- serializeU128(value: AnyNumber): void: Serializes a uint128 number.
|
|
38
|
+
- serializeU256(value: AnyNumber): void: Serializes a uint256 number.
|
|
39
|
+
- getBytes(): Bytes: Returns the buffered bytes.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { Serializer } from "@layerzerolabs/lz-serdes";
|
|
43
|
+
|
|
44
|
+
const serializer = new Serializer();
|
|
45
|
+
serializer.serializeStr("Hello, world!");
|
|
46
|
+
const bytes = serializer.getBytes();
|
|
47
|
+
console.log(`Serialized bytes: ${bytes}`);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Deserialization
|
|
51
|
+
|
|
52
|
+
Class representing a deserializer for various data types.
|
|
53
|
+
|
|
54
|
+
- deserializeStr(): string: Deserializes a string.
|
|
55
|
+
- deserializeBytes(): Bytes: Deserializes an array of bytes.
|
|
56
|
+
- deserializeBool(): boolean: Deserializes a boolean value.
|
|
57
|
+
- deserializeU8(): Uint8: Deserializes a uint8 number.
|
|
58
|
+
- deserializeU16(): Uint16: Deserializes a uint16 number.
|
|
59
|
+
- deserializeU32(): Uint32: Deserializes a uint32 number.
|
|
60
|
+
- deserializeU64(): Uint64: Deserializes a uint64 number.
|
|
61
|
+
- deserializeU128(): Uint128: Deserializes a uint128 number.
|
|
62
|
+
- deserializeU256(): Uint256: Deserializes a uint256 number.
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { Deserializer } from "@layerzerolabs/lz-serdes";
|
|
66
|
+
|
|
67
|
+
const bytes = new Uint8Array([
|
|
68
|
+
12, 72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33,
|
|
69
|
+
]);
|
|
70
|
+
const deserializer = new Deserializer(bytes);
|
|
71
|
+
const str = deserializer.deserializeStr();
|
|
72
|
+
console.log(`Deserialized string: ${str}`);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### BCS Serialization
|
|
76
|
+
|
|
77
|
+
#### Serialize a uint64 Value
|
|
78
|
+
|
|
79
|
+
Serializes a uint64 value to bytes using BCS.
|
|
80
|
+
|
|
81
|
+
- value: The value to serialize.
|
|
82
|
+
- Returns: The serialized bytes.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { bcsSerializeUint64 } from "@layerzerolabs/lz-serdes";
|
|
86
|
+
|
|
87
|
+
const value = 123456789n;
|
|
88
|
+
const bytes = bcsSerializeUint64(value);
|
|
89
|
+
console.log(`Serialized uint64 bytes: ${bytes}`);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### Serialize a String
|
|
93
|
+
|
|
94
|
+
Serializes a string value to bytes using BCS.
|
|
95
|
+
|
|
96
|
+
- value: The value to serialize.
|
|
97
|
+
- Returns: The serialized bytes.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { bcsSerializeStr } from "@layerzerolabs/lz-serdes";
|
|
101
|
+
|
|
102
|
+
const value = "Hello, world!";
|
|
103
|
+
const bytes = bcsSerializeStr(value);
|
|
104
|
+
console.log(`Serialized string bytes: ${bytes}`);
|
|
105
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -22,11 +22,22 @@ var MAX_U256_BIG_INT = 115792089237316195423570985008687907853269984665640564039
|
|
|
22
22
|
|
|
23
23
|
// src/serializer.ts
|
|
24
24
|
var Serializer = class {
|
|
25
|
+
/**
|
|
26
|
+
* Creates an instance of Serializer.
|
|
27
|
+
*
|
|
28
|
+
* @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.
|
|
29
|
+
*/
|
|
25
30
|
constructor(littleEndian = true) {
|
|
26
31
|
this.littleEndian = littleEndian;
|
|
27
32
|
this.buffer = new ArrayBuffer(64);
|
|
28
33
|
this.offset = 0;
|
|
29
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Ensures that the buffer has enough space to handle the specified number of bytes.
|
|
37
|
+
* If the buffer is not large enough, it will be resized to accommodate the additional bytes.
|
|
38
|
+
*
|
|
39
|
+
* @param {number} bytes - The number of bytes to ensure space for.
|
|
40
|
+
*/
|
|
30
41
|
ensureBufferWillHandleSize(bytes) {
|
|
31
42
|
while (this.buffer.byteLength < this.offset + bytes) {
|
|
32
43
|
const newBuffer = new ArrayBuffer(this.buffer.byteLength * 2);
|
|
@@ -34,11 +45,23 @@ var Serializer = class {
|
|
|
34
45
|
this.buffer = newBuffer;
|
|
35
46
|
}
|
|
36
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Serializes the given byte values into the buffer.
|
|
50
|
+
*
|
|
51
|
+
* @param {Bytes} values - The byte values to serialize.
|
|
52
|
+
*/
|
|
37
53
|
serialize(values) {
|
|
38
54
|
this.ensureBufferWillHandleSize(values.length);
|
|
39
55
|
new Uint8Array(this.buffer, this.offset).set(values);
|
|
40
56
|
this.offset += values.length;
|
|
41
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Serializes a value using the specified DataView function.
|
|
60
|
+
*
|
|
61
|
+
* @param {(byteOffset: number, value: number, littleEndian?: boolean) => void} fn - The DataView function to use for serialization.
|
|
62
|
+
* @param {number} bytesLength - The length of the bytes to serialize.
|
|
63
|
+
* @param {number} value - The value to serialize.
|
|
64
|
+
*/
|
|
42
65
|
serializeWithFunction(fn, bytesLength, value) {
|
|
43
66
|
this.ensureBufferWillHandleSize(bytesLength);
|
|
44
67
|
const dv = new DataView(this.buffer, this.offset);
|
|
@@ -63,6 +86,7 @@ var Serializer = class {
|
|
|
63
86
|
serializeStr(value) {
|
|
64
87
|
const textEncoder = new TextEncoder();
|
|
65
88
|
this.serializeBytes(textEncoder.encode(value));
|
|
89
|
+
return this;
|
|
66
90
|
}
|
|
67
91
|
/**
|
|
68
92
|
* Serializes an array of bytes.
|
|
@@ -77,6 +101,7 @@ var Serializer = class {
|
|
|
77
101
|
this.serializeU32AsUbeb128(value.length);
|
|
78
102
|
}
|
|
79
103
|
this.serialize(value);
|
|
104
|
+
return this;
|
|
80
105
|
}
|
|
81
106
|
/**
|
|
82
107
|
* Serializes an array of bytes with known length. Therefore length doesn't need to be
|
|
@@ -85,6 +110,7 @@ var Serializer = class {
|
|
|
85
110
|
*/
|
|
86
111
|
serializeFixedBytes(value) {
|
|
87
112
|
this.serialize(value);
|
|
113
|
+
return this;
|
|
88
114
|
}
|
|
89
115
|
/**
|
|
90
116
|
* Serializes a boolean value.
|
|
@@ -97,15 +123,19 @@ var Serializer = class {
|
|
|
97
123
|
}
|
|
98
124
|
const byteValue = value ? 1 : 0;
|
|
99
125
|
this.serialize(new Uint8Array([byteValue]));
|
|
126
|
+
return this;
|
|
100
127
|
}
|
|
101
128
|
serializeU8(value) {
|
|
102
129
|
this.serialize(new Uint8Array([value]));
|
|
130
|
+
return this;
|
|
103
131
|
}
|
|
104
132
|
serializeU16(value) {
|
|
105
133
|
this.serializeWithFunction(DataView.prototype.setUint16, 2, value);
|
|
134
|
+
return this;
|
|
106
135
|
}
|
|
107
136
|
serializeU32(value) {
|
|
108
137
|
this.serializeWithFunction(DataView.prototype.setUint32, 4, value);
|
|
138
|
+
return this;
|
|
109
139
|
}
|
|
110
140
|
serializeU64(value) {
|
|
111
141
|
const low = BigInt(value.toString()) & BigInt(MAX_U32_NUMBER);
|
|
@@ -114,6 +144,7 @@ var Serializer = class {
|
|
|
114
144
|
array.forEach((x) => {
|
|
115
145
|
this.serializeU32(Number(x));
|
|
116
146
|
});
|
|
147
|
+
return this;
|
|
117
148
|
}
|
|
118
149
|
serializeU128(value) {
|
|
119
150
|
const low = BigInt(value.toString()) & MAX_U64_BIG_INT;
|
|
@@ -122,6 +153,7 @@ var Serializer = class {
|
|
|
122
153
|
array.forEach((x) => {
|
|
123
154
|
this.serializeU64(x);
|
|
124
155
|
});
|
|
156
|
+
return this;
|
|
125
157
|
}
|
|
126
158
|
serializeU256(value) {
|
|
127
159
|
const low = BigInt(value.toString()) & MAX_U128_BIG_INT;
|
|
@@ -130,6 +162,7 @@ var Serializer = class {
|
|
|
130
162
|
array.forEach((x) => {
|
|
131
163
|
this.serializeU128(x);
|
|
132
164
|
});
|
|
165
|
+
return this;
|
|
133
166
|
}
|
|
134
167
|
serializeU32AsUleb128(val) {
|
|
135
168
|
let value = val;
|
|
@@ -140,6 +173,7 @@ var Serializer = class {
|
|
|
140
173
|
}
|
|
141
174
|
valueArray.push(value);
|
|
142
175
|
this.serialize(new Uint8Array(valueArray));
|
|
176
|
+
return this;
|
|
143
177
|
}
|
|
144
178
|
serializeU32AsUbeb128(val) {
|
|
145
179
|
let value = val;
|
|
@@ -151,9 +185,12 @@ var Serializer = class {
|
|
|
151
185
|
valueArray.unshift(byte);
|
|
152
186
|
}
|
|
153
187
|
this.serialize(new Uint8Array(valueArray));
|
|
188
|
+
return this;
|
|
154
189
|
}
|
|
155
190
|
/**
|
|
156
191
|
* Returns the buffered bytes
|
|
192
|
+
*
|
|
193
|
+
* @returns {Bytes} The buffered bytes.
|
|
157
194
|
*/
|
|
158
195
|
getBytes() {
|
|
159
196
|
return new Uint8Array(this.buffer).slice(0, this.offset);
|
|
@@ -191,7 +228,7 @@ function checkNumberRange(minValue, maxValue, message) {
|
|
|
191
228
|
if (valueBigInt > BigInt(maxValue.toString()) || valueBigInt < BigInt(minValue.toString())) {
|
|
192
229
|
throw new Error(message ?? "Value is out of range");
|
|
193
230
|
}
|
|
194
|
-
childFunction.apply(this, [value]);
|
|
231
|
+
return childFunction.apply(this, [value]);
|
|
195
232
|
};
|
|
196
233
|
return descriptor;
|
|
197
234
|
};
|
|
@@ -199,12 +236,25 @@ function checkNumberRange(minValue, maxValue, message) {
|
|
|
199
236
|
|
|
200
237
|
// src/deserializer.ts
|
|
201
238
|
var Deserializer = class {
|
|
239
|
+
/**
|
|
240
|
+
* Creates an instance of Deserializer.
|
|
241
|
+
*
|
|
242
|
+
* @param {Bytes} data - The data to deserialize.
|
|
243
|
+
* @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.
|
|
244
|
+
*/
|
|
202
245
|
constructor(data, littleEndian = true) {
|
|
203
246
|
this.littleEndian = littleEndian;
|
|
204
247
|
this.buffer = new ArrayBuffer(data.length);
|
|
205
248
|
new Uint8Array(this.buffer).set(data, 0);
|
|
206
249
|
this.offset = 0;
|
|
207
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Reads a specified number of bytes from the buffer.
|
|
253
|
+
*
|
|
254
|
+
* @param {number} length - The number of bytes to read.
|
|
255
|
+
* @returns {ArrayBuffer} The read bytes.
|
|
256
|
+
* @throws {Error} If the end of the buffer is reached.
|
|
257
|
+
*/
|
|
208
258
|
read(length) {
|
|
209
259
|
if (this.offset + length > this.buffer.byteLength) {
|
|
210
260
|
throw new Error("Reached to the end of buffer");
|
|
@@ -213,9 +263,19 @@ var Deserializer = class {
|
|
|
213
263
|
this.offset += length;
|
|
214
264
|
return bytes;
|
|
215
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* Returns the number of remaining bytes in the buffer.
|
|
268
|
+
*
|
|
269
|
+
* @returns {number} The number of remaining bytes.
|
|
270
|
+
*/
|
|
216
271
|
remaining() {
|
|
217
272
|
return this.buffer.byteLength - this.offset;
|
|
218
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* Rewinds the buffer by a specified number of bytes.
|
|
276
|
+
*
|
|
277
|
+
* @param {number} length - The number of bytes to rewind.
|
|
278
|
+
*/
|
|
219
279
|
rewind(length) {
|
|
220
280
|
this.offset -= length;
|
|
221
281
|
}
|
|
@@ -232,6 +292,7 @@ var Deserializer = class {
|
|
|
232
292
|
* 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]), true);
|
|
233
293
|
* assert(deserializer.deserializeStr() === "çå∞≠¢õß∂ƒ∫");
|
|
234
294
|
* ```
|
|
295
|
+
* @returns {string} The deserialized string.
|
|
235
296
|
*/
|
|
236
297
|
deserializeStr() {
|
|
237
298
|
const value = this.deserializeBytes();
|
|
@@ -243,6 +304,8 @@ var Deserializer = class {
|
|
|
243
304
|
*
|
|
244
305
|
* layout for "bytes": bytes_length | bytes. bytes_length is the length of the bytes array that is
|
|
245
306
|
* uleb128/ubeb128 encoded. bytes_length is a u32 integer.
|
|
307
|
+
*
|
|
308
|
+
* @returns {Bytes} The deserialized bytes.
|
|
246
309
|
*/
|
|
247
310
|
deserializeBytes() {
|
|
248
311
|
const len = this.littleEndian ? this.deserializeUleb128AsU32() : this.deserializeUbeb128AsU32();
|
|
@@ -251,6 +314,8 @@ var Deserializer = class {
|
|
|
251
314
|
/**
|
|
252
315
|
* Deserializes an array of bytes. The number of bytes to read is already known.
|
|
253
316
|
*
|
|
317
|
+
* @param {number} len - The number of bytes to read.
|
|
318
|
+
* @returns {Bytes} The deserialized bytes.
|
|
254
319
|
*/
|
|
255
320
|
deserializeFixedBytes(len) {
|
|
256
321
|
return new Uint8Array(this.read(len));
|
|
@@ -259,6 +324,9 @@ var Deserializer = class {
|
|
|
259
324
|
* Deserializes a boolean value.
|
|
260
325
|
*
|
|
261
326
|
* layout for "boolean": One byte. "0x01" for True and "0x00" for False.
|
|
327
|
+
*
|
|
328
|
+
* @returns {boolean} The deserialized boolean value.
|
|
329
|
+
* @throws {Error} If the boolean value is invalid.
|
|
262
330
|
*/
|
|
263
331
|
deserializeBool() {
|
|
264
332
|
const bool = new Uint8Array(this.read(1))[0];
|
|
@@ -271,6 +339,8 @@ var Deserializer = class {
|
|
|
271
339
|
* Deserializes a uint8 number.
|
|
272
340
|
*
|
|
273
341
|
* layout for "uint8": One byte. Binary format in little-endian representation.
|
|
342
|
+
*
|
|
343
|
+
* @returns {Uint8} The deserialized uint8 number.
|
|
274
344
|
*/
|
|
275
345
|
deserializeU8() {
|
|
276
346
|
return new DataView(this.read(1)).getUint8(0);
|
|
@@ -286,6 +356,7 @@ var Deserializer = class {
|
|
|
286
356
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34]), false);
|
|
287
357
|
* assert(deserializer.deserializeU16() === 4660);
|
|
288
358
|
* ```
|
|
359
|
+
* @returns {Uint16} The deserialized uint16 number.
|
|
289
360
|
*/
|
|
290
361
|
deserializeU16() {
|
|
291
362
|
return new DataView(this.read(2)).getUint16(0, this.littleEndian);
|
|
@@ -301,6 +372,7 @@ var Deserializer = class {
|
|
|
301
372
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78]), false);
|
|
302
373
|
* assert(deserializer.deserializeU32() === 305419896);
|
|
303
374
|
* ```
|
|
375
|
+
* @returns {Uint32} The deserialized uint32 number.
|
|
304
376
|
*/
|
|
305
377
|
deserializeU32() {
|
|
306
378
|
return new DataView(this.read(4)).getUint32(0, this.littleEndian);
|
|
@@ -316,6 +388,7 @@ var Deserializer = class {
|
|
|
316
388
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]), false);
|
|
317
389
|
* assert(deserializer.deserializeU64() === 1311768467750121216);
|
|
318
390
|
* ```
|
|
391
|
+
* @returns {Uint64} The deserialized uint64 number.
|
|
319
392
|
*/
|
|
320
393
|
deserializeU64() {
|
|
321
394
|
const low = this.deserializeU32();
|
|
@@ -327,6 +400,8 @@ var Deserializer = class {
|
|
|
327
400
|
* Deserializes a uint128 number.
|
|
328
401
|
*
|
|
329
402
|
* layout for "uint128": Sixteen bytes. Binary format in little-endian representation.
|
|
403
|
+
*
|
|
404
|
+
* @returns {Uint128} The deserialized uint128 number.
|
|
330
405
|
*/
|
|
331
406
|
deserializeU128() {
|
|
332
407
|
const low = this.deserializeU64();
|
|
@@ -338,6 +413,8 @@ var Deserializer = class {
|
|
|
338
413
|
* Deserializes a uint256 number.
|
|
339
414
|
*
|
|
340
415
|
* layout for "uint256": Thirty-two bytes. Binary format in little-endian representation.
|
|
416
|
+
*
|
|
417
|
+
* @returns {Uint256} The deserialized uint256 number.
|
|
341
418
|
*/
|
|
342
419
|
deserializeU256() {
|
|
343
420
|
const low = this.deserializeU128();
|
|
@@ -349,6 +426,9 @@ var Deserializer = class {
|
|
|
349
426
|
* Deserializes a uleb128 encoded uint32 number.
|
|
350
427
|
*
|
|
351
428
|
* use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
|
|
429
|
+
*
|
|
430
|
+
* @returns {Uint32} The deserialized uleb128 encoded uint32 number.
|
|
431
|
+
* @throws {Error} If there is an overflow while parsing the value.
|
|
352
432
|
*/
|
|
353
433
|
deserializeUleb128AsU32() {
|
|
354
434
|
let value = BigInt(0);
|
|
@@ -370,6 +450,9 @@ var Deserializer = class {
|
|
|
370
450
|
* Deserializes a unsigned Variable-Length Big-Endian Encoding encoded uint32 number.
|
|
371
451
|
*
|
|
372
452
|
* use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
|
|
453
|
+
*
|
|
454
|
+
* @returns {Uint32} The deserialized ubeb128 encoded uint32 number.
|
|
455
|
+
* @throws {Error} If there is an overflow while parsing the value.
|
|
373
456
|
*/
|
|
374
457
|
deserializeUbeb128AsU32() {
|
|
375
458
|
let value = BigInt(0);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/consts.ts","../src/serializer.ts","../src/deserializer.ts","../src/helper.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAOO,IAAM,gBAAuB;AAC7B,IAAM,iBAAyB;AAC/B,IAAM,iBAAyB;AAC/B,IAAM,kBAA0B;AAChC,IAAM,mBAA4B;AAClC,IAAM,mBAA4B;;;ACGlC,IAAM,aAAN,MAAiB;AAAA,EAMpB,YAAY,eAAe,MAAM;AAC7B,SAAK,eAAe;AACpB,SAAK,SAAS,IAAI,YAAY,EAAE;AAChC,SAAK,SAAS;AAAA,EAClB;AAAA,EAEQ,2BAA2B,OAAqB;AACpD,WAAO,KAAK,OAAO,aAAa,KAAK,SAAS,OAAO;AACjD,YAAM,YAAY,IAAI,YAAY,KAAK,OAAO,aAAa,CAA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,WAAW;AAClC,aAAW,aAAa,KAAK;AAC7B,SAAO,WAAW,SAAS;AAC/B;AAEO,SAAS,iBAAiB,OAAyB;AACtD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,cAAc,KAAK;AAC9B,SAAO,WAAW,SAAS;AAC/B;AAEO,SAAS,iBAAiB,OAAyB;AACtD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,cAAc,KAAK;AAC9B,SAAO,WAAW,SAAS;AAC/B;AAEO,SAAS,iBAAiB,OAAuB;AACpD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,cAAc,KAAK;AAC9B,SAAO,WAAW,SAAS;AAC/B;AAEO,SAAS,gBAAgB,OAAsB;AAClD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,aAAa,KAAK;AAC7B,SAAO,WAAW,SAAS;AAC/B;AAEO,SAAS,kBAAkB,OAAqB;AACnD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,eAAe,KAAK;AAC/B,SAAO,WAAW,SAAS;AAC/B;AAEO,SAAS,uBAAuB,OAAqB;AACxD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,oBAAoB,KAAK;AACpC,SAAO,WAAW,SAAS;AAC/B","sourcesContent":["// Copyright © Aptos Foundation\n// Copyright © LayerZero Labs\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Uint128, Uint16, Uint256, Uint32, Uint64, Uint8 } from './types'\n\n// Upper bound values for uint8, uint16, uint64 and uint128\nexport const MAX_U8_NUMBER: Uint8 = 255\nexport const MAX_U16_NUMBER: Uint16 = 65535\nexport const MAX_U32_NUMBER: Uint32 = 4294967295\nexport const MAX_U64_BIG_INT: Uint64 = 18446744073709551615n\nexport const MAX_U128_BIG_INT: Uint128 = 340282366920938463463374607431768211455n\nexport const MAX_U256_BIG_INT: Uint256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935n\n","// Copyright © Aptos Foundation\n// Copyright © LayerZero Labs\n// SPDX-License-Identifier: Apache-2.0\n\n/* eslint-disable no-bitwise */\nimport {\n MAX_U128_BIG_INT,\n MAX_U16_NUMBER,\n MAX_U256_BIG_INT,\n MAX_U32_NUMBER,\n MAX_U64_BIG_INT,\n MAX_U8_NUMBER,\n} from './consts'\nimport { AnyNumber, Bytes, Uint16, Uint32, Uint8 } from './types'\n\nexport class Serializer {\n private buffer: ArrayBuffer\n\n private offset: number\n public readonly littleEndian: boolean\n\n constructor(littleEndian = true) {\n this.littleEndian = littleEndian\n this.buffer = new ArrayBuffer(64)\n this.offset = 0\n }\n\n private ensureBufferWillHandleSize(bytes: number): void {\n while (this.buffer.byteLength < this.offset + bytes) {\n const newBuffer = new ArrayBuffer(this.buffer.byteLength * 2)\n new Uint8Array(newBuffer).set(new Uint8Array(this.buffer))\n this.buffer = newBuffer\n }\n }\n\n protected serialize(values: Bytes): void {\n this.ensureBufferWillHandleSize(values.length)\n new Uint8Array(this.buffer, this.offset).set(values)\n this.offset += values.length\n }\n\n private serializeWithFunction(\n fn: (byteOffset: number, value: number, littleEndian?: boolean) => void,\n bytesLength: number,\n value: number\n ): void {\n this.ensureBufferWillHandleSize(bytesLength)\n const dv = new DataView(this.buffer, this.offset)\n fn.apply(dv, [0, value, this.littleEndian])\n this.offset += bytesLength\n }\n\n /**\n * Serializes a string. UTF8 string is supported. Serializes the string's bytes length \"l\" first,\n * and then serializes \"l\" bytes of the string content.\n *\n * layout for \"string\": string_length | string_content. string_length is the bytes length of\n * the string that is uleb128/ubeb128 encoded. string_length is a u32 integer.\n *\n * @example\n * ```ts\n * const serializer = new Serializer();\n * serializer.serializeStr(\"çå∞≠¢õß∂ƒ∫\");\n * assert(serializer.getBytes() === new Uint8Array([24, 0xc3, 0xa7, 0xc3, 0xa5, 0xe2, 0x88, 0x9e,\n * 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]));\n * ```\n */\n serializeStr(value: string): void {\n const textEncoder = new TextEncoder()\n this.serializeBytes(textEncoder.encode(value))\n }\n\n /**\n * Serializes an array of bytes.\n *\n * layout for \"bytes\": bytes_length | bytes. bytes_length is the length of the bytes array that is\n * uleb128/ubeb128 encoded. bytes_length is a u32 integer.\n */\n serializeBytes(value: Bytes): void {\n if (this.littleEndian) {\n this.serializeU32AsUleb128(value.length)\n } else {\n this.serializeU32AsUbeb128(value.length)\n }\n this.serialize(value)\n }\n\n /**\n * Serializes an array of bytes with known length. Therefore length doesn't need to be\n * serialized to help deserialization. When deserializing, the number of\n * bytes to deserialize needs to be passed in.\n */\n serializeFixedBytes(value: Bytes): void {\n this.serialize(value)\n }\n\n /**\n * Serializes a boolean value.\n *\n * layout for \"boolean\": One byte. \"0x01\" for True and \"0x00\" for False.\n */\n serializeBool(value: boolean): void {\n if (typeof value !== 'boolean') {\n throw new Error('Value needs to be a boolean')\n }\n const byteValue = value ? 1 : 0\n this.serialize(new Uint8Array([byteValue]))\n }\n\n /**\n * Serializes a uint8 number.\n *\n * layout for \"uint8\": One byte. Binary format in little-endian representation.\n */\n @checkNumberRange(0, MAX_U8_NUMBER)\n serializeU8(value: Uint8): void {\n this.serialize(new Uint8Array([value]))\n }\n\n /**\n * Serializes a uint16 number.\n *\n * layout for \"uint16\": Two bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const serializer = new Serializer(true);\n * serializer.serializeU16(4660);\n * assert(serializer.getBytes() === new Uint8Array([0x34, 0x12]));\n * const serializer = new Serializer(false);\n * serializer.serializeU16(4660);\n * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34]));\n * ```\n */\n @checkNumberRange(0, MAX_U16_NUMBER)\n serializeU16(value: Uint16): void {\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.serializeWithFunction(DataView.prototype.setUint16, 2, value)\n }\n\n /**\n * Serializes a uint32 number.\n *\n * layout for \"uint32\": Four bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const serializer = new Serializer(true);\n * serializer.serializeU32(305419896);\n * assert(serializer.getBytes() === new Uint8Array([0x78, 0x56, 0x34, 0x12]));\n * const serializer = new Serializer(false);\n * serializer.serializeU32(305419896);\n * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34, 0x56, 0x78]));\n * ```\n */\n @checkNumberRange(0, MAX_U32_NUMBER)\n serializeU32(value: Uint32): void {\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.serializeWithFunction(DataView.prototype.setUint32, 4, value)\n }\n\n /**\n * Serializes a uint64 number.\n *\n * layout for \"uint64\": Eight bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const serializer = new Serializer(true);\n * serializer.serializeU64(1311768467750121216);\n * assert(serializer.getBytes() === new Uint8Array([0x00, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12]));\n * const serializer = new Serializer(false);\n * serializer.serializeU64(1311768467750121216);\n * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]));\n * ```\n */\n @checkNumberRange(BigInt(0), MAX_U64_BIG_INT)\n serializeU64(value: AnyNumber): void {\n const low = BigInt(value.toString()) & BigInt(MAX_U32_NUMBER)\n const high = BigInt(value.toString()) >> BigInt(32)\n\n const array = this.littleEndian ? [low, high] : [high, low]\n array.forEach((x) => {\n this.serializeU32(Number(x))\n })\n }\n\n /**\n * Serializes a uint128 number.\n *\n * layout for \"uint128\": Sixteen bytes. Binary format in little-endian representation.\n */\n @checkNumberRange(BigInt(0), MAX_U128_BIG_INT)\n serializeU128(value: AnyNumber): void {\n const low = BigInt(value.toString()) & MAX_U64_BIG_INT\n const high = BigInt(value.toString()) >> BigInt(64)\n\n const array = this.littleEndian ? [low, high] : [high, low]\n array.forEach((x) => {\n this.serializeU64(x)\n })\n }\n\n /**\n * Serializes a uint256 number.\n *\n * layout for \"uint256\": Sixteen bytes. Binary format in little-endian representation.\n */\n @checkNumberRange(BigInt(0), MAX_U256_BIG_INT)\n serializeU256(value: AnyNumber): void {\n const low = BigInt(value.toString()) & MAX_U128_BIG_INT\n const high = BigInt(value.toString()) >> BigInt(128)\n\n const array = this.littleEndian ? [low, high] : [high, low]\n array.forEach((x) => {\n this.serializeU128(x)\n })\n }\n\n /**\n * Serializes a uint32 number with uleb128.\n *\n * use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values\n */\n @checkNumberRange(0, MAX_U32_NUMBER)\n serializeU32AsUleb128(val: Uint32): void {\n let value = val\n const valueArray = []\n while (value >>> 7 !== 0) {\n valueArray.push((value & 0x7f) | 0x80)\n value >>>= 7\n }\n valueArray.push(value)\n this.serialize(new Uint8Array(valueArray))\n }\n\n /**\n * Serializes a uint32 number with unsigned Variable-Length Big-Endian Encoding.\n *\n * use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values\n */\n @checkNumberRange(0, MAX_U32_NUMBER)\n serializeU32AsUbeb128(val: Uint32): void {\n let value = val\n const valueArray: Uint8[] = []\n\n valueArray.unshift(value & 0x7f)\n while (value >>> 7 !== 0) {\n value >>>= 7\n const byte = (value & 0x7f) | 0x80\n valueArray.unshift(byte)\n }\n\n this.serialize(new Uint8Array(valueArray))\n }\n\n /**\n * Returns the buffered bytes\n */\n getBytes(): Bytes {\n return new Uint8Array(this.buffer).slice(0, this.offset)\n }\n}\n\n/**\n * Creates a decorator to make sure the arg value of the decorated function is within a range.\n * @param minValue The arg value of decorated function must >= minValue\n * @param maxValue The arg value of decorated function must <= maxValue\n * @param message Error message\n */\nfunction checkNumberRange<T extends AnyNumber>(minValue: T, maxValue: T, message?: string) {\n return (target: unknown, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor => {\n /* eslint-disable @typescript-eslint/no-unsafe-assignment */\n const childFunction = descriptor.value as (value: AnyNumber) => void\n // eslint-disable-next-line no-param-reassign\n descriptor.value = function deco(value: AnyNumber): void {\n const valueBigInt = BigInt(value.toString())\n if (valueBigInt > BigInt(maxValue.toString()) || valueBigInt < BigInt(minValue.toString())) {\n throw new Error(message ?? 'Value is out of range')\n }\n childFunction.apply(this, [value])\n }\n return descriptor\n /* eslint-enable @typescript-eslint/no-unsafe-assignment */\n }\n}\n","// Copyright © Aptos Foundation\n// Copyright © LayerZero Labs\n// SPDX-License-Identifier: Apache-2.0\n\n/* eslint-disable no-bitwise */\nimport { MAX_U32_NUMBER } from './consts'\nimport { Bytes, Uint128, Uint16, Uint256, Uint32, Uint64, Uint8 } from './types'\n\nexport class Deserializer {\n private buffer: ArrayBuffer\n\n private offset: number\n public readonly littleEndian: boolean\n\n constructor(data: Bytes, littleEndian = true) {\n this.littleEndian = littleEndian\n // copies data to prevent outside mutation of buffer.\n this.buffer = new ArrayBuffer(data.length)\n new Uint8Array(this.buffer).set(data, 0)\n this.offset = 0\n }\n\n private read(length: number): ArrayBuffer {\n if (this.offset + length > this.buffer.byteLength) {\n throw new Error('Reached to the end of buffer')\n }\n\n const bytes = this.buffer.slice(this.offset, this.offset + length)\n this.offset += length\n return bytes\n }\n\n public remaining(): number {\n return this.buffer.byteLength - this.offset\n }\n\n public rewind(length: number): void {\n this.offset -= length\n }\n\n /**\n * Deserializes a string. UTF8 string is supported. Reads the string's bytes length \"l\" first,\n * and then reads \"l\" bytes of content. Decodes the byte array into a string.\n *\n * layout for \"string\": string_length | string_content. string_length is the bytes length of\n * the string that is uleb128/ubeb128 encoded. string_length is a u32 integer.\n *\n * @example\n * ```ts\n * const deserializer = new Deserializer(new Uint8Array([24, 0xc3, 0xa7, 0xc3, 0xa5, 0xe2, 0x88, 0x9e,\n * 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]), true);\n * assert(deserializer.deserializeStr() === \"çå∞≠¢õß∂ƒ∫\");\n * ```\n */\n deserializeStr(): string {\n const value = this.deserializeBytes()\n const textDecoder = new TextDecoder()\n return textDecoder.decode(value)\n }\n\n /**\n * Deserializes an array of bytes.\n *\n * layout for \"bytes\": bytes_length | bytes. bytes_length is the length of the bytes array that is\n * uleb128/ubeb128 encoded. bytes_length is a u32 integer.\n */\n deserializeBytes(): Bytes {\n const len = this.littleEndian ? this.deserializeUleb128AsU32() : this.deserializeUbeb128AsU32()\n\n return new Uint8Array(this.read(len))\n }\n\n /**\n * Deserializes an array of bytes. The number of bytes to read is already known.\n *\n */\n deserializeFixedBytes(len: number): Bytes {\n return new Uint8Array(this.read(len))\n }\n\n /**\n * Deserializes a boolean value.\n *\n * layout for \"boolean\": One byte. \"0x01\" for True and \"0x00\" for False.\n */\n deserializeBool(): boolean {\n const bool = new Uint8Array(this.read(1))[0]\n if (bool !== 1 && bool !== 0) {\n throw new Error('Invalid boolean value')\n }\n return bool === 1\n }\n\n /**\n * Deserializes a uint8 number.\n *\n * layout for \"uint8\": One byte. Binary format in little-endian representation.\n */\n deserializeU8(): Uint8 {\n return new DataView(this.read(1)).getUint8(0)\n }\n\n /**\n * Deserializes a uint16 number.\n *\n * layout for \"uint16\": Two bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const deserializer = new Deserializer(new Uint8Array([0x34, 0x12]), true);\n * assert(deserializer.deserializeU16() === 4660);\n * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34]), false);\n * assert(deserializer.deserializeU16() === 4660);\n * ```\n */\n deserializeU16(): Uint16 {\n return new DataView(this.read(2)).getUint16(0, this.littleEndian)\n }\n\n /**\n * Deserializes a uint32 number.\n *\n * layout for \"uint32\": Four bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const deserializer = new Deserializer(new Uint8Array([0x78, 0x56, 0x34, 0x12]), true);\n * assert(deserializer.deserializeU32() === 305419896);\n * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78]), false);\n * assert(deserializer.deserializeU32() === 305419896);\n * ```\n */\n deserializeU32(): Uint32 {\n return new DataView(this.read(4)).getUint32(0, this.littleEndian)\n }\n\n /**\n * Deserializes a uint64 number.\n *\n * layout for \"uint64\": Eight bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const deserializer = new Deserializer(new Uint8Array([0x00, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12]), true);\n * assert(deserializer.deserializeU64() === 1311768467750121216);\n * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]), false);\n * assert(deserializer.deserializeU64() === 1311768467750121216);\n * ```\n */\n deserializeU64(): Uint64 {\n const low = this.deserializeU32()\n const high = this.deserializeU32()\n\n const values = this.littleEndian ? [low, high] : [high, low]\n return BigInt((BigInt(values[1]) << BigInt(32)) | BigInt(values[0]))\n }\n\n /**\n * Deserializes a uint128 number.\n *\n * layout for \"uint128\": Sixteen bytes. Binary format in little-endian representation.\n */\n deserializeU128(): Uint128 {\n const low = this.deserializeU64()\n const high = this.deserializeU64()\n\n const values = this.littleEndian ? [low, high] : [high, low]\n return BigInt((values[1] << BigInt(64)) | values[0])\n }\n\n /**\n * Deserializes a uint256 number.\n *\n * layout for \"uint256\": Thirty-two bytes. Binary format in little-endian representation.\n */\n deserializeU256(): Uint256 {\n const low = this.deserializeU128()\n const high = this.deserializeU128()\n\n const values = this.littleEndian ? [low, high] : [high, low]\n return BigInt((values[1] << BigInt(128)) | values[0])\n }\n\n /**\n * Deserializes a uleb128 encoded uint32 number.\n *\n * use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values\n */\n deserializeUleb128AsU32(): Uint32 {\n let value = BigInt(0)\n let shift = 0\n\n while (value < MAX_U32_NUMBER) {\n const byte = this.deserializeU8()\n value |= BigInt(byte & 0x7f) << BigInt(shift)\n\n if ((byte & 0x80) === 0) {\n break\n }\n shift += 7\n }\n\n if (value > MAX_U32_NUMBER) {\n throw new Error('Overflow while parsing uleb128-encoded uint32 value')\n }\n\n return Number(value)\n }\n\n /**\n * Deserializes a unsigned Variable-Length Big-Endian Encoding encoded uint32 number.\n *\n * use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values\n */\n deserializeUbeb128AsU32(): Uint32 {\n let value = BigInt(0)\n\n while (value < MAX_U32_NUMBER) {\n const byte = this.deserializeU8()\n value |= BigInt(byte & 0x7f)\n\n if ((byte & 0x80) === 0) {\n break\n }\n value = value << BigInt(7)\n }\n\n if (value > MAX_U32_NUMBER) {\n throw new Error('Overflow while parsing ubeb128-encoded uint32 value')\n }\n\n return Number(value)\n }\n}\n","// Copyright © Aptos Foundation\n// Copyright © LayerZero Labs\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Deserializer } from './deserializer'\nimport { Serializer } from './serializer'\nimport { AnyNumber, Bytes, Seq, Uint16, Uint32, Uint8 } from './types'\n\ninterface Serializable {\n serialize(serializer: Serializer): void\n}\n\ninterface Deserializable<T> {\n deserialize(deserializer: Deserializer): T\n}\n\n// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style\ninterface ISerializer {\n [key: string]: (item: unknown) => void\n}\n\n/**\n * Serializes a vector values that are \"Serializable\".\n */\nexport function serializeVector<T extends Serializable>(value: Seq<T>, serializer: Serializer): void {\n serializer.serializeU32AsUleb128(value.length)\n value.forEach((item: T) => {\n item.serialize(serializer)\n })\n}\n\n/**\n * Serializes a vector with specified item serialization function.\n * Very dynamic function and bypasses static typechecking.\n */\nexport function serializeVectorWithFunc(value: unknown[], func: string): Bytes {\n const serializer = new Serializer()\n serializer.serializeU32AsUleb128(value.length)\n\n const f = (serializer as unknown as ISerializer)[func]\n if (typeof f !== 'function') {\n throw new Error(`The function '${func}' does not exist on the serializer.`)\n }\n\n value.forEach((item) => {\n f.call(serializer, item)\n })\n return serializer.getBytes()\n}\n\n/**\n * Deserializes a vector of values.\n */\nexport function deserializeVector<T>(deserializer: Deserializer, cls: Deserializable<T>): any[] {\n const length = deserializer.deserializeUleb128AsU32()\n const list: T[] = []\n for (let i = 0; i < length; i += 1) {\n list.push(cls.deserialize(deserializer))\n }\n return list\n}\n\nexport function bcsToBytes<T extends Serializable>(value: T): Bytes {\n const serializer = new Serializer()\n value.serialize(serializer)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeUint64(value: AnyNumber): Bytes {\n const serializer = new Serializer()\n serializer.serializeU64(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeU8(value: Uint8): Bytes {\n const serializer = new Serializer()\n serializer.serializeU8(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeU16(value: Uint16): Bytes {\n const serializer = new Serializer()\n serializer.serializeU16(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeU32(value: Uint32): Bytes {\n const serializer = new Serializer()\n serializer.serializeU32(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeU128(value: AnyNumber): Bytes {\n const serializer = new Serializer()\n serializer.serializeU128(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeU256(value: AnyNumber): Bytes {\n const serializer = new Serializer()\n serializer.serializeU256(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeBool(value: boolean): Bytes {\n const serializer = new Serializer()\n serializer.serializeBool(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeStr(value: string): Bytes {\n const serializer = new Serializer()\n serializer.serializeStr(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeBytes(value: Bytes): Bytes {\n const serializer = new Serializer()\n serializer.serializeBytes(value)\n return serializer.getBytes()\n}\n\nexport function bcsSerializeFixedBytes(value: Bytes): Bytes {\n const serializer = new Serializer()\n serializer.serializeFixedBytes(value)\n return serializer.getBytes()\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/consts.ts","../src/serializer.ts","../src/deserializer.ts","../src/helper.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAUO,IAAM,gBAAuB;AAM7B,IAAM,iBAAyB;AAM/B,IAAM,iBAAyB;AAM/B,IAAM,kBAA0B;AAMhC,IAAM,mBAA4B;AAMlC,IAAM,mBAA4B;;;ACtBlC,IAAM,aAAN,MAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;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WAAW;AAClC,aAAW,aAAa,KAAK;AAC7B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,kBAAkB,OAAqB;AACnD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,eAAe,KAAK;AAC/B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,uBAAuB,OAAqB;AACxD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,oBAAoB,KAAK;AACpC,SAAO,WAAW,SAAS;AAC/B","sourcesContent":["// Copyright © Aptos Foundation\n// Copyright © LayerZero Labs\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Uint128, Uint16, Uint256, Uint32, Uint64, Uint8 } from './types'\n\n/**\n * Upper bound value for uint8.\n * @type {Uint8}\n */\nexport const MAX_U8_NUMBER: Uint8 = 255\n\n/**\n * Upper bound value for uint16.\n * @type {Uint16}\n */\nexport const MAX_U16_NUMBER: Uint16 = 65535\n\n/**\n * Upper bound value for uint32.\n * @type {Uint32}\n */\nexport const MAX_U32_NUMBER: Uint32 = 4294967295\n\n/**\n * Upper bound value for uint64.\n * @type {Uint64}\n */\nexport const MAX_U64_BIG_INT: Uint64 = 18446744073709551615n\n\n/**\n * Upper bound value for uint128.\n * @type {Uint128}\n */\nexport const MAX_U128_BIG_INT: Uint128 = 340282366920938463463374607431768211455n\n\n/**\n * Upper bound value for uint256.\n * @type {Uint256}\n */\nexport const MAX_U256_BIG_INT: Uint256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935n\n","// Copyright © Aptos Foundation\n// Copyright © LayerZero Labs\n// SPDX-License-Identifier: Apache-2.0\n\n/* eslint-disable no-bitwise */\nimport {\n MAX_U128_BIG_INT,\n MAX_U16_NUMBER,\n MAX_U256_BIG_INT,\n MAX_U32_NUMBER,\n MAX_U64_BIG_INT,\n MAX_U8_NUMBER,\n} from './consts'\nimport { AnyNumber, Bytes, Uint16, Uint32, Uint8 } from './types'\n\n/**\n * Class representing a serializer for various data types.\n */\nexport class Serializer {\n private buffer: ArrayBuffer\n\n private offset: number\n public readonly littleEndian: boolean\n\n /**\n * Creates an instance of Serializer.\n *\n * @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.\n */\n constructor(littleEndian = true) {\n this.littleEndian = littleEndian\n this.buffer = new ArrayBuffer(64)\n this.offset = 0\n }\n\n /**\n * Ensures that the buffer has enough space to handle the specified number of bytes.\n * If the buffer is not large enough, it will be resized to accommodate the additional bytes.\n *\n * @param {number} bytes - The number of bytes to ensure space for.\n */\n private ensureBufferWillHandleSize(bytes: number): void {\n while (this.buffer.byteLength < this.offset + bytes) {\n const newBuffer = new ArrayBuffer(this.buffer.byteLength * 2)\n new Uint8Array(newBuffer).set(new Uint8Array(this.buffer))\n this.buffer = newBuffer\n }\n }\n\n /**\n * Serializes the given byte values into the buffer.\n *\n * @param {Bytes} values - The byte values to serialize.\n */\n protected serialize(values: Bytes): void {\n this.ensureBufferWillHandleSize(values.length)\n new Uint8Array(this.buffer, this.offset).set(values)\n this.offset += values.length\n }\n\n /**\n * Serializes a value using the specified DataView function.\n *\n * @param {(byteOffset: number, value: number, littleEndian?: boolean) => void} fn - The DataView function to use for serialization.\n * @param {number} bytesLength - The length of the bytes to serialize.\n * @param {number} value - The value to serialize.\n */\n private serializeWithFunction(\n fn: (byteOffset: number, value: number, littleEndian?: boolean) => void,\n bytesLength: number,\n value: number\n ): void {\n this.ensureBufferWillHandleSize(bytesLength)\n const dv = new DataView(this.buffer, this.offset)\n fn.apply(dv, [0, value, this.littleEndian])\n this.offset += bytesLength\n }\n\n /**\n * Serializes a string. UTF8 string is supported. Serializes the string's bytes length \"l\" first,\n * and then serializes \"l\" bytes of the string content.\n *\n * layout for \"string\": string_length | string_content. string_length is the bytes length of\n * the string that is uleb128/ubeb128 encoded. string_length is a u32 integer.\n *\n * @example\n * ```ts\n * const serializer = new Serializer();\n * serializer.serializeStr(\"çå∞≠¢õß∂ƒ∫\");\n * assert(serializer.getBytes() === new Uint8Array([24, 0xc3, 0xa7, 0xc3, 0xa5, 0xe2, 0x88, 0x9e,\n * 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]));\n * ```\n */\n serializeStr(value: string): this {\n const textEncoder = new TextEncoder()\n this.serializeBytes(textEncoder.encode(value))\n return this\n }\n\n /**\n * Serializes an array of bytes.\n *\n * layout for \"bytes\": bytes_length | bytes. bytes_length is the length of the bytes array that is\n * uleb128/ubeb128 encoded. bytes_length is a u32 integer.\n */\n serializeBytes(value: Bytes): this {\n if (this.littleEndian) {\n this.serializeU32AsUleb128(value.length)\n } else {\n this.serializeU32AsUbeb128(value.length)\n }\n this.serialize(value)\n return this\n }\n\n /**\n * Serializes an array of bytes with known length. Therefore length doesn't need to be\n * serialized to help deserialization. When deserializing, the number of\n * bytes to deserialize needs to be passed in.\n */\n serializeFixedBytes(value: Bytes): this {\n this.serialize(value)\n return this\n }\n\n /**\n * Serializes a boolean value.\n *\n * layout for \"boolean\": One byte. \"0x01\" for True and \"0x00\" for False.\n */\n serializeBool(value: boolean): this {\n if (typeof value !== 'boolean') {\n throw new Error('Value needs to be a boolean')\n }\n const byteValue = value ? 1 : 0\n this.serialize(new Uint8Array([byteValue]))\n return this\n }\n\n /**\n * Serializes a uint8 number.\n *\n * layout for \"uint8\": One byte. Binary format in little-endian representation.\n */\n @checkNumberRange(0, MAX_U8_NUMBER)\n serializeU8(value: Uint8): this {\n this.serialize(new Uint8Array([value]))\n return this\n }\n\n /**\n * Serializes a uint16 number.\n *\n * layout for \"uint16\": Two bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const serializer = new Serializer(true);\n * serializer.serializeU16(4660);\n * assert(serializer.getBytes() === new Uint8Array([0x34, 0x12]));\n * const serializer = new Serializer(false);\n * serializer.serializeU16(4660);\n * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34]));\n * ```\n */\n @checkNumberRange(0, MAX_U16_NUMBER)\n serializeU16(value: Uint16): this {\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.serializeWithFunction(DataView.prototype.setUint16, 2, value)\n return this\n }\n\n /**\n * Serializes a uint32 number.\n *\n * layout for \"uint32\": Four bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const serializer = new Serializer(true);\n * serializer.serializeU32(305419896);\n * assert(serializer.getBytes() === new Uint8Array([0x78, 0x56, 0x34, 0x12]));\n * const serializer = new Serializer(false);\n * serializer.serializeU32(305419896);\n * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34, 0x56, 0x78]));\n * ```\n */\n @checkNumberRange(0, MAX_U32_NUMBER)\n serializeU32(value: Uint32): this {\n // eslint-disable-next-line @typescript-eslint/unbound-method\n this.serializeWithFunction(DataView.prototype.setUint32, 4, value)\n return this\n }\n\n /**\n * Serializes a uint64 number.\n *\n * layout for \"uint64\": Eight bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const serializer = new Serializer(true);\n * serializer.serializeU64(1311768467750121216);\n * assert(serializer.getBytes() === new Uint8Array([0x00, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12]));\n * const serializer = new Serializer(false);\n * serializer.serializeU64(1311768467750121216);\n * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]));\n * ```\n */\n @checkNumberRange(BigInt(0), MAX_U64_BIG_INT)\n serializeU64(value: AnyNumber): this {\n const low = BigInt(value.toString()) & BigInt(MAX_U32_NUMBER)\n const high = BigInt(value.toString()) >> BigInt(32)\n\n const array = this.littleEndian ? [low, high] : [high, low]\n array.forEach((x) => {\n this.serializeU32(Number(x))\n })\n return this\n }\n\n /**\n * Serializes a uint128 number.\n *\n * layout for \"uint128\": Sixteen bytes. Binary format in little-endian representation.\n */\n @checkNumberRange(BigInt(0), MAX_U128_BIG_INT)\n serializeU128(value: AnyNumber): this {\n const low = BigInt(value.toString()) & MAX_U64_BIG_INT\n const high = BigInt(value.toString()) >> BigInt(64)\n\n const array = this.littleEndian ? [low, high] : [high, low]\n array.forEach((x) => {\n this.serializeU64(x)\n })\n return this\n }\n\n /**\n * Serializes a uint256 number.\n *\n * layout for \"uint256\": Sixteen bytes. Binary format in little-endian representation.\n */\n @checkNumberRange(BigInt(0), MAX_U256_BIG_INT)\n serializeU256(value: AnyNumber): this {\n const low = BigInt(value.toString()) & MAX_U128_BIG_INT\n const high = BigInt(value.toString()) >> BigInt(128)\n\n const array = this.littleEndian ? [low, high] : [high, low]\n array.forEach((x) => {\n this.serializeU128(x)\n })\n return this\n }\n\n /**\n * Serializes a uint32 number with uleb128.\n *\n * use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values\n */\n @checkNumberRange(0, MAX_U32_NUMBER)\n serializeU32AsUleb128(val: Uint32): this {\n let value = val\n const valueArray = []\n while (value >>> 7 !== 0) {\n valueArray.push((value & 0x7f) | 0x80)\n value >>>= 7\n }\n valueArray.push(value)\n this.serialize(new Uint8Array(valueArray))\n return this\n }\n\n /**\n * Serializes a uint32 number with unsigned Variable-Length Big-Endian Encoding.\n *\n * use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values\n */\n @checkNumberRange(0, MAX_U32_NUMBER)\n serializeU32AsUbeb128(val: Uint32): this {\n let value = val\n const valueArray: Uint8[] = []\n\n valueArray.unshift(value & 0x7f)\n while (value >>> 7 !== 0) {\n value >>>= 7\n const byte = (value & 0x7f) | 0x80\n valueArray.unshift(byte)\n }\n\n this.serialize(new Uint8Array(valueArray))\n return this\n }\n\n /**\n * Returns the buffered bytes\n *\n * @returns {Bytes} The buffered bytes.\n */\n getBytes(): Bytes {\n return new Uint8Array(this.buffer).slice(0, this.offset)\n }\n}\n\n/**\n * Creates a decorator to make sure the arg value of the decorated function is within a range.\n * @param minValue The arg value of decorated function must >= minValue\n * @param maxValue The arg value of decorated function must <= maxValue\n * @param message Error message\n */\nfunction checkNumberRange<T extends AnyNumber>(minValue: T, maxValue: T, message?: string) {\n return (target: unknown, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor => {\n /* eslint-disable @typescript-eslint/no-unsafe-assignment */\n const childFunction = descriptor.value as (value: AnyNumber) => any\n // eslint-disable-next-line no-param-reassign\n descriptor.value = function deco(value: AnyNumber): void {\n const valueBigInt = BigInt(value.toString())\n if (valueBigInt > BigInt(maxValue.toString()) || valueBigInt < BigInt(minValue.toString())) {\n throw new Error(message ?? 'Value is out of range')\n }\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n return childFunction.apply(this, [value])\n }\n return descriptor\n /* eslint-enable @typescript-eslint/no-unsafe-assignment */\n }\n}\n","// Copyright © Aptos Foundation\n// Copyright © LayerZero Labs\n// SPDX-License-Identifier: Apache-2.0\n\n/* eslint-disable no-bitwise */\nimport { MAX_U32_NUMBER } from './consts'\nimport { Bytes, Uint128, Uint16, Uint256, Uint32, Uint64, Uint8 } from './types'\n\n/**\n * Class representing a deserializer for various data types.\n */\nexport class Deserializer {\n private buffer: ArrayBuffer\n\n private offset: number\n public readonly littleEndian: boolean\n\n /**\n * Creates an instance of Deserializer.\n *\n * @param {Bytes} data - The data to deserialize.\n * @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.\n */\n constructor(data: Bytes, littleEndian = true) {\n this.littleEndian = littleEndian\n // copies data to prevent outside mutation of buffer.\n this.buffer = new ArrayBuffer(data.length)\n new Uint8Array(this.buffer).set(data, 0)\n this.offset = 0\n }\n\n /**\n * Reads a specified number of bytes from the buffer.\n *\n * @param {number} length - The number of bytes to read.\n * @returns {ArrayBuffer} The read bytes.\n * @throws {Error} If the end of the buffer is reached.\n */\n private read(length: number): ArrayBuffer {\n if (this.offset + length > this.buffer.byteLength) {\n throw new Error('Reached to the end of buffer')\n }\n\n const bytes = this.buffer.slice(this.offset, this.offset + length)\n this.offset += length\n return bytes\n }\n\n /**\n * Returns the number of remaining bytes in the buffer.\n *\n * @returns {number} The number of remaining bytes.\n */\n public remaining(): number {\n return this.buffer.byteLength - this.offset\n }\n\n /**\n * Rewinds the buffer by a specified number of bytes.\n *\n * @param {number} length - The number of bytes to rewind.\n */\n public rewind(length: number): void {\n this.offset -= length\n }\n\n /**\n * Deserializes a string. UTF8 string is supported. Reads the string's bytes length \"l\" first,\n * and then reads \"l\" bytes of content. Decodes the byte array into a string.\n *\n * layout for \"string\": string_length | string_content. string_length is the bytes length of\n * the string that is uleb128/ubeb128 encoded. string_length is a u32 integer.\n *\n * @example\n * ```ts\n * const deserializer = new Deserializer(new Uint8Array([24, 0xc3, 0xa7, 0xc3, 0xa5, 0xe2, 0x88, 0x9e,\n * 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]), true);\n * assert(deserializer.deserializeStr() === \"çå∞≠¢õß∂ƒ∫\");\n * ```\n * @returns {string} The deserialized string.\n */\n deserializeStr(): string {\n const value = this.deserializeBytes()\n const textDecoder = new TextDecoder()\n return textDecoder.decode(value)\n }\n\n /**\n * Deserializes an array of bytes.\n *\n * layout for \"bytes\": bytes_length | bytes. bytes_length is the length of the bytes array that is\n * uleb128/ubeb128 encoded. bytes_length is a u32 integer.\n *\n * @returns {Bytes} The deserialized bytes.\n */\n deserializeBytes(): Bytes {\n const len = this.littleEndian ? this.deserializeUleb128AsU32() : this.deserializeUbeb128AsU32()\n\n return new Uint8Array(this.read(len))\n }\n\n /**\n * Deserializes an array of bytes. The number of bytes to read is already known.\n *\n * @param {number} len - The number of bytes to read.\n * @returns {Bytes} The deserialized bytes.\n */\n deserializeFixedBytes(len: number): Bytes {\n return new Uint8Array(this.read(len))\n }\n\n /**\n * Deserializes a boolean value.\n *\n * layout for \"boolean\": One byte. \"0x01\" for True and \"0x00\" for False.\n *\n * @returns {boolean} The deserialized boolean value.\n * @throws {Error} If the boolean value is invalid.\n */\n deserializeBool(): boolean {\n const bool = new Uint8Array(this.read(1))[0]\n if (bool !== 1 && bool !== 0) {\n throw new Error('Invalid boolean value')\n }\n return bool === 1\n }\n\n /**\n * Deserializes a uint8 number.\n *\n * layout for \"uint8\": One byte. Binary format in little-endian representation.\n *\n * @returns {Uint8} The deserialized uint8 number.\n */\n deserializeU8(): Uint8 {\n return new DataView(this.read(1)).getUint8(0)\n }\n\n /**\n * Deserializes a uint16 number.\n *\n * layout for \"uint16\": Two bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const deserializer = new Deserializer(new Uint8Array([0x34, 0x12]), true);\n * assert(deserializer.deserializeU16() === 4660);\n * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34]), false);\n * assert(deserializer.deserializeU16() === 4660);\n * ```\n * @returns {Uint16} The deserialized uint16 number.\n */\n deserializeU16(): Uint16 {\n return new DataView(this.read(2)).getUint16(0, this.littleEndian)\n }\n\n /**\n * Deserializes a uint32 number.\n *\n * layout for \"uint32\": Four bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const deserializer = new Deserializer(new Uint8Array([0x78, 0x56, 0x34, 0x12]), true);\n * assert(deserializer.deserializeU32() === 305419896);\n * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78]), false);\n * assert(deserializer.deserializeU32() === 305419896);\n * ```\n * @returns {Uint32} The deserialized uint32 number.\n */\n deserializeU32(): Uint32 {\n return new DataView(this.read(4)).getUint32(0, this.littleEndian)\n }\n\n /**\n * Deserializes a uint64 number.\n *\n * layout for \"uint64\": Eight bytes. Binary format in little-endian representation.\n * @example\n * ```ts\n * const deserializer = new Deserializer(new Uint8Array([0x00, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12]), true);\n * assert(deserializer.deserializeU64() === 1311768467750121216);\n * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]), false);\n * assert(deserializer.deserializeU64() === 1311768467750121216);\n * ```\n * @returns {Uint64} The deserialized uint64 number.\n */\n deserializeU64(): Uint64 {\n const low = this.deserializeU32()\n const high = this.deserializeU32()\n\n const values = this.littleEndian ? [low, high] : [high, low]\n return BigInt((BigInt(values[1]) << BigInt(32)) | BigInt(values[0]))\n }\n\n /**\n * Deserializes a uint128 number.\n *\n * layout for \"uint128\": Sixteen bytes. Binary format in little-endian representation.\n *\n * @returns {Uint128} The deserialized uint128 number.\n */\n deserializeU128(): Uint128 {\n const low = this.deserializeU64()\n const high = this.deserializeU64()\n\n const values = this.littleEndian ? [low, high] : [high, low]\n return BigInt((values[1] << BigInt(64)) | values[0])\n }\n\n /**\n * Deserializes a uint256 number.\n *\n * layout for \"uint256\": Thirty-two bytes. Binary format in little-endian representation.\n *\n * @returns {Uint256} The deserialized uint256 number.\n */\n deserializeU256(): Uint256 {\n const low = this.deserializeU128()\n const high = this.deserializeU128()\n\n const values = this.littleEndian ? [low, high] : [high, low]\n return BigInt((values[1] << BigInt(128)) | values[0])\n }\n\n /**\n * Deserializes a uleb128 encoded uint32 number.\n *\n * use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values\n *\n * @returns {Uint32} The deserialized uleb128 encoded uint32 number.\n * @throws {Error} If there is an overflow while parsing the value.\n */\n deserializeUleb128AsU32(): Uint32 {\n let value = BigInt(0)\n let shift = 0\n\n while (value < MAX_U32_NUMBER) {\n const byte = this.deserializeU8()\n value |= BigInt(byte & 0x7f) << BigInt(shift)\n\n if ((byte & 0x80) === 0) {\n break\n }\n shift += 7\n }\n\n if (value > MAX_U32_NUMBER) {\n throw new Error('Overflow while parsing uleb128-encoded uint32 value')\n }\n\n return Number(value)\n }\n\n /**\n * Deserializes a unsigned Variable-Length Big-Endian Encoding encoded uint32 number.\n *\n * use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values\n *\n * @returns {Uint32} The deserialized ubeb128 encoded uint32 number.\n * @throws {Error} If there is an overflow while parsing the value.\n */\n deserializeUbeb128AsU32(): Uint32 {\n let value = BigInt(0)\n\n while (value < MAX_U32_NUMBER) {\n const byte = this.deserializeU8()\n value |= BigInt(byte & 0x7f)\n\n if ((byte & 0x80) === 0) {\n break\n }\n value = value << BigInt(7)\n }\n\n if (value > MAX_U32_NUMBER) {\n throw new Error('Overflow while parsing ubeb128-encoded uint32 value')\n }\n\n return Number(value)\n }\n}\n","// Copyright © Aptos Foundation\n// Copyright © LayerZero Labs\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Deserializer } from './deserializer'\nimport { Serializer } from './serializer'\nimport { AnyNumber, Bytes, Seq, Uint16, Uint32, Uint8 } from './types'\n\n/**\n * Interface representing a serializable object.\n */\ninterface Serializable {\n /**\n * Serializes the object using the provided serializer.\n *\n * @param {Serializer} serializer - The serializer to use.\n */\n serialize(serializer: Serializer): void\n}\n\n/**\n * Interface representing a deserializable object.\n */\ninterface Deserializable<T> {\n /**\n * Deserializes the object using the provided deserializer.\n *\n * @param {Deserializer} deserializer - The deserializer to use.\n * @returns {T} The deserialized object.\n */\n deserialize(deserializer: Deserializer): T\n}\n\n// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style\ninterface ISerializer {\n [key: string]: (item: unknown) => void\n}\n\n/**\n * Serializes a vector of values that are \"Serializable\".\n *\n * @param {Seq<T>} value - The vector of values to serialize.\n * @param {Serializer} serializer - The serializer to use.\n */\nexport function serializeVector<T extends Serializable>(value: Seq<T>, serializer: Serializer): void {\n serializer.serializeU32AsUleb128(value.length)\n value.forEach((item: T) => {\n item.serialize(serializer)\n })\n}\n\n/**\n * Serializes a vector with a specified item serialization function.\n * Very dynamic function and bypasses static typechecking.\n *\n * @param {unknown[]} value - The vector of values to serialize.\n * @param {string} func - The name of the serialization function to use.\n * @returns {Bytes} The serialized bytes.\n * @throws {Error} If the specified function does not exist on the serializer.\n */\nexport function serializeVectorWithFunc(value: unknown[], func: string): Bytes {\n const serializer = new Serializer()\n serializer.serializeU32AsUleb128(value.length)\n\n const f = (serializer as unknown as ISerializer)[func]\n if (typeof f !== 'function') {\n throw new Error(`The function '${func}' does not exist on the serializer.`)\n }\n\n value.forEach((item) => {\n f.call(serializer, item)\n })\n return serializer.getBytes()\n}\n\n/**\n * Deserializes a vector of values.\n *\n * @param {Deserializer} deserializer - The deserializer to use.\n * @param {Deserializable<T>} cls - The class to use for deserialization.\n * @returns {T[]} The deserialized vector of values.\n */\nexport function deserializeVector<T>(deserializer: Deserializer, cls: Deserializable<T>): any[] {\n const length = deserializer.deserializeUleb128AsU32()\n const list: T[] = []\n for (let i = 0; i < length; i += 1) {\n list.push(cls.deserialize(deserializer))\n }\n return list\n}\n\n/**\n * Serializes a value to bytes using BCS (Binary Canonical Serialization).\n *\n * @param {T} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsToBytes<T extends Serializable>(value: T): Bytes {\n const serializer = new Serializer()\n value.serialize(serializer)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a uint64 value to bytes using BCS.\n *\n * @param {AnyNumber} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeUint64(value: AnyNumber): Bytes {\n const serializer = new Serializer()\n serializer.serializeU64(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a uint8 value to bytes using BCS.\n *\n * @param {Uint8} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeU8(value: Uint8): Bytes {\n const serializer = new Serializer()\n serializer.serializeU8(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a uint16 value to bytes using BCS.\n *\n * @param {Uint16} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeU16(value: Uint16): Bytes {\n const serializer = new Serializer()\n serializer.serializeU16(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a uint32 value to bytes using BCS.\n *\n * @param {Uint32} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeU32(value: Uint32): Bytes {\n const serializer = new Serializer()\n serializer.serializeU32(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a uint128 value to bytes using BCS.\n *\n * @param {AnyNumber} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeU128(value: AnyNumber): Bytes {\n const serializer = new Serializer()\n serializer.serializeU128(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a uint256 value to bytes using BCS.\n *\n * @param {AnyNumber} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeU256(value: AnyNumber): Bytes {\n const serializer = new Serializer()\n serializer.serializeU256(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a boolean value to bytes using BCS.\n *\n * @param {boolean} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeBool(value: boolean): Bytes {\n const serializer = new Serializer()\n serializer.serializeBool(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a string value to bytes using BCS.\n *\n * @param {string} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeStr(value: string): Bytes {\n const serializer = new Serializer()\n serializer.serializeStr(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a byte array to bytes using BCS.\n *\n * @param {Bytes} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeBytes(value: Bytes): Bytes {\n const serializer = new Serializer()\n serializer.serializeBytes(value)\n return serializer.getBytes()\n}\n\n/**\n * Serializes a fixed-length byte array to bytes using BCS.\n *\n * @param {Bytes} value - The value to serialize.\n * @returns {Bytes} The serialized bytes.\n */\nexport function bcsSerializeFixedBytes(value: Bytes): Bytes {\n const serializer = new Serializer()\n serializer.serializeFixedBytes(value)\n return serializer.getBytes()\n}\n"]}
|