@layerzerolabs/lz-serdes 3.0.15 → 3.0.17
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 +13 -0
- package/README.md +105 -0
- package/dist/index.cjs +97 -15
- 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 +97 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
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
|
@@ -3,12 +3,11 @@
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
5
|
var __decorateClass = (decorators, target, key, kind) => {
|
|
6
|
-
var result =
|
|
6
|
+
var result = __getOwnPropDesc(target, key) ;
|
|
7
7
|
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
8
8
|
if (decorator = decorators[i])
|
|
9
|
-
result = (
|
|
10
|
-
if (
|
|
11
|
-
__defProp(target, key, result);
|
|
9
|
+
result = (decorator(target, key, result) ) || result;
|
|
10
|
+
if (result) __defProp(target, key, result);
|
|
12
11
|
return result;
|
|
13
12
|
};
|
|
14
13
|
|
|
@@ -22,11 +21,22 @@ var MAX_U256_BIG_INT = 115792089237316195423570985008687907853269984665640564039
|
|
|
22
21
|
|
|
23
22
|
// src/serializer.ts
|
|
24
23
|
var Serializer = class {
|
|
24
|
+
/**
|
|
25
|
+
* Creates an instance of Serializer.
|
|
26
|
+
*
|
|
27
|
+
* @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.
|
|
28
|
+
*/
|
|
25
29
|
constructor(littleEndian = true) {
|
|
26
30
|
this.littleEndian = littleEndian;
|
|
27
31
|
this.buffer = new ArrayBuffer(64);
|
|
28
32
|
this.offset = 0;
|
|
29
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Ensures that the buffer has enough space to handle the specified number of bytes.
|
|
36
|
+
* If the buffer is not large enough, it will be resized to accommodate the additional bytes.
|
|
37
|
+
*
|
|
38
|
+
* @param {number} bytes - The number of bytes to ensure space for.
|
|
39
|
+
*/
|
|
30
40
|
ensureBufferWillHandleSize(bytes) {
|
|
31
41
|
while (this.buffer.byteLength < this.offset + bytes) {
|
|
32
42
|
const newBuffer = new ArrayBuffer(this.buffer.byteLength * 2);
|
|
@@ -34,11 +44,23 @@ var Serializer = class {
|
|
|
34
44
|
this.buffer = newBuffer;
|
|
35
45
|
}
|
|
36
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Serializes the given byte values into the buffer.
|
|
49
|
+
*
|
|
50
|
+
* @param {Bytes} values - The byte values to serialize.
|
|
51
|
+
*/
|
|
37
52
|
serialize(values) {
|
|
38
53
|
this.ensureBufferWillHandleSize(values.length);
|
|
39
54
|
new Uint8Array(this.buffer, this.offset).set(values);
|
|
40
55
|
this.offset += values.length;
|
|
41
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Serializes a value using the specified DataView function.
|
|
59
|
+
*
|
|
60
|
+
* @param {(byteOffset: number, value: number, littleEndian?: boolean) => void} fn - The DataView function to use for serialization.
|
|
61
|
+
* @param {number} bytesLength - The length of the bytes to serialize.
|
|
62
|
+
* @param {number} value - The value to serialize.
|
|
63
|
+
*/
|
|
42
64
|
serializeWithFunction(fn, bytesLength, value) {
|
|
43
65
|
this.ensureBufferWillHandleSize(bytesLength);
|
|
44
66
|
const dv = new DataView(this.buffer, this.offset);
|
|
@@ -63,6 +85,7 @@ var Serializer = class {
|
|
|
63
85
|
serializeStr(value) {
|
|
64
86
|
const textEncoder = new TextEncoder();
|
|
65
87
|
this.serializeBytes(textEncoder.encode(value));
|
|
88
|
+
return this;
|
|
66
89
|
}
|
|
67
90
|
/**
|
|
68
91
|
* Serializes an array of bytes.
|
|
@@ -77,6 +100,7 @@ var Serializer = class {
|
|
|
77
100
|
this.serializeU32AsUbeb128(value.length);
|
|
78
101
|
}
|
|
79
102
|
this.serialize(value);
|
|
103
|
+
return this;
|
|
80
104
|
}
|
|
81
105
|
/**
|
|
82
106
|
* Serializes an array of bytes with known length. Therefore length doesn't need to be
|
|
@@ -85,6 +109,7 @@ var Serializer = class {
|
|
|
85
109
|
*/
|
|
86
110
|
serializeFixedBytes(value) {
|
|
87
111
|
this.serialize(value);
|
|
112
|
+
return this;
|
|
88
113
|
}
|
|
89
114
|
/**
|
|
90
115
|
* Serializes a boolean value.
|
|
@@ -97,15 +122,19 @@ var Serializer = class {
|
|
|
97
122
|
}
|
|
98
123
|
const byteValue = value ? 1 : 0;
|
|
99
124
|
this.serialize(new Uint8Array([byteValue]));
|
|
125
|
+
return this;
|
|
100
126
|
}
|
|
101
127
|
serializeU8(value) {
|
|
102
128
|
this.serialize(new Uint8Array([value]));
|
|
129
|
+
return this;
|
|
103
130
|
}
|
|
104
131
|
serializeU16(value) {
|
|
105
132
|
this.serializeWithFunction(DataView.prototype.setUint16, 2, value);
|
|
133
|
+
return this;
|
|
106
134
|
}
|
|
107
135
|
serializeU32(value) {
|
|
108
136
|
this.serializeWithFunction(DataView.prototype.setUint32, 4, value);
|
|
137
|
+
return this;
|
|
109
138
|
}
|
|
110
139
|
serializeU64(value) {
|
|
111
140
|
const low = BigInt(value.toString()) & BigInt(MAX_U32_NUMBER);
|
|
@@ -114,6 +143,7 @@ var Serializer = class {
|
|
|
114
143
|
array.forEach((x) => {
|
|
115
144
|
this.serializeU32(Number(x));
|
|
116
145
|
});
|
|
146
|
+
return this;
|
|
117
147
|
}
|
|
118
148
|
serializeU128(value) {
|
|
119
149
|
const low = BigInt(value.toString()) & MAX_U64_BIG_INT;
|
|
@@ -122,6 +152,7 @@ var Serializer = class {
|
|
|
122
152
|
array.forEach((x) => {
|
|
123
153
|
this.serializeU64(x);
|
|
124
154
|
});
|
|
155
|
+
return this;
|
|
125
156
|
}
|
|
126
157
|
serializeU256(value) {
|
|
127
158
|
const low = BigInt(value.toString()) & MAX_U128_BIG_INT;
|
|
@@ -130,6 +161,7 @@ var Serializer = class {
|
|
|
130
161
|
array.forEach((x) => {
|
|
131
162
|
this.serializeU128(x);
|
|
132
163
|
});
|
|
164
|
+
return this;
|
|
133
165
|
}
|
|
134
166
|
serializeU32AsUleb128(val) {
|
|
135
167
|
let value = val;
|
|
@@ -140,6 +172,7 @@ var Serializer = class {
|
|
|
140
172
|
}
|
|
141
173
|
valueArray.push(value);
|
|
142
174
|
this.serialize(new Uint8Array(valueArray));
|
|
175
|
+
return this;
|
|
143
176
|
}
|
|
144
177
|
serializeU32AsUbeb128(val) {
|
|
145
178
|
let value = val;
|
|
@@ -151,9 +184,12 @@ var Serializer = class {
|
|
|
151
184
|
valueArray.unshift(byte);
|
|
152
185
|
}
|
|
153
186
|
this.serialize(new Uint8Array(valueArray));
|
|
187
|
+
return this;
|
|
154
188
|
}
|
|
155
189
|
/**
|
|
156
190
|
* Returns the buffered bytes
|
|
191
|
+
*
|
|
192
|
+
* @returns {Bytes} The buffered bytes.
|
|
157
193
|
*/
|
|
158
194
|
getBytes() {
|
|
159
195
|
return new Uint8Array(this.buffer).slice(0, this.offset);
|
|
@@ -161,37 +197,37 @@ var Serializer = class {
|
|
|
161
197
|
};
|
|
162
198
|
__decorateClass([
|
|
163
199
|
checkNumberRange(0, MAX_U8_NUMBER)
|
|
164
|
-
], Serializer.prototype, "serializeU8"
|
|
200
|
+
], Serializer.prototype, "serializeU8");
|
|
165
201
|
__decorateClass([
|
|
166
202
|
checkNumberRange(0, MAX_U16_NUMBER)
|
|
167
|
-
], Serializer.prototype, "serializeU16"
|
|
203
|
+
], Serializer.prototype, "serializeU16");
|
|
168
204
|
__decorateClass([
|
|
169
205
|
checkNumberRange(0, MAX_U32_NUMBER)
|
|
170
|
-
], Serializer.prototype, "serializeU32"
|
|
206
|
+
], Serializer.prototype, "serializeU32");
|
|
171
207
|
__decorateClass([
|
|
172
208
|
checkNumberRange(BigInt(0), MAX_U64_BIG_INT)
|
|
173
|
-
], Serializer.prototype, "serializeU64"
|
|
209
|
+
], Serializer.prototype, "serializeU64");
|
|
174
210
|
__decorateClass([
|
|
175
211
|
checkNumberRange(BigInt(0), MAX_U128_BIG_INT)
|
|
176
|
-
], Serializer.prototype, "serializeU128"
|
|
212
|
+
], Serializer.prototype, "serializeU128");
|
|
177
213
|
__decorateClass([
|
|
178
214
|
checkNumberRange(BigInt(0), MAX_U256_BIG_INT)
|
|
179
|
-
], Serializer.prototype, "serializeU256"
|
|
215
|
+
], Serializer.prototype, "serializeU256");
|
|
180
216
|
__decorateClass([
|
|
181
217
|
checkNumberRange(0, MAX_U32_NUMBER)
|
|
182
|
-
], Serializer.prototype, "serializeU32AsUleb128"
|
|
218
|
+
], Serializer.prototype, "serializeU32AsUleb128");
|
|
183
219
|
__decorateClass([
|
|
184
220
|
checkNumberRange(0, MAX_U32_NUMBER)
|
|
185
|
-
], Serializer.prototype, "serializeU32AsUbeb128"
|
|
221
|
+
], Serializer.prototype, "serializeU32AsUbeb128");
|
|
186
222
|
function checkNumberRange(minValue, maxValue, message) {
|
|
187
223
|
return (target, propertyKey, descriptor) => {
|
|
188
224
|
const childFunction = descriptor.value;
|
|
189
225
|
descriptor.value = function deco(value) {
|
|
190
226
|
const valueBigInt = BigInt(value.toString());
|
|
191
227
|
if (valueBigInt > BigInt(maxValue.toString()) || valueBigInt < BigInt(minValue.toString())) {
|
|
192
|
-
throw new Error(
|
|
228
|
+
throw new Error("Value is out of range");
|
|
193
229
|
}
|
|
194
|
-
childFunction.apply(this, [value]);
|
|
230
|
+
return childFunction.apply(this, [value]);
|
|
195
231
|
};
|
|
196
232
|
return descriptor;
|
|
197
233
|
};
|
|
@@ -199,12 +235,25 @@ function checkNumberRange(minValue, maxValue, message) {
|
|
|
199
235
|
|
|
200
236
|
// src/deserializer.ts
|
|
201
237
|
var Deserializer = class {
|
|
238
|
+
/**
|
|
239
|
+
* Creates an instance of Deserializer.
|
|
240
|
+
*
|
|
241
|
+
* @param {Bytes} data - The data to deserialize.
|
|
242
|
+
* @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.
|
|
243
|
+
*/
|
|
202
244
|
constructor(data, littleEndian = true) {
|
|
203
245
|
this.littleEndian = littleEndian;
|
|
204
246
|
this.buffer = new ArrayBuffer(data.length);
|
|
205
247
|
new Uint8Array(this.buffer).set(data, 0);
|
|
206
248
|
this.offset = 0;
|
|
207
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* Reads a specified number of bytes from the buffer.
|
|
252
|
+
*
|
|
253
|
+
* @param {number} length - The number of bytes to read.
|
|
254
|
+
* @returns {ArrayBuffer} The read bytes.
|
|
255
|
+
* @throws {Error} If the end of the buffer is reached.
|
|
256
|
+
*/
|
|
208
257
|
read(length) {
|
|
209
258
|
if (this.offset + length > this.buffer.byteLength) {
|
|
210
259
|
throw new Error("Reached to the end of buffer");
|
|
@@ -213,9 +262,19 @@ var Deserializer = class {
|
|
|
213
262
|
this.offset += length;
|
|
214
263
|
return bytes;
|
|
215
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* Returns the number of remaining bytes in the buffer.
|
|
267
|
+
*
|
|
268
|
+
* @returns {number} The number of remaining bytes.
|
|
269
|
+
*/
|
|
216
270
|
remaining() {
|
|
217
271
|
return this.buffer.byteLength - this.offset;
|
|
218
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Rewinds the buffer by a specified number of bytes.
|
|
275
|
+
*
|
|
276
|
+
* @param {number} length - The number of bytes to rewind.
|
|
277
|
+
*/
|
|
219
278
|
rewind(length) {
|
|
220
279
|
this.offset -= length;
|
|
221
280
|
}
|
|
@@ -232,6 +291,7 @@ var Deserializer = class {
|
|
|
232
291
|
* 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]), true);
|
|
233
292
|
* assert(deserializer.deserializeStr() === "çå∞≠¢õß∂ƒ∫");
|
|
234
293
|
* ```
|
|
294
|
+
* @returns {string} The deserialized string.
|
|
235
295
|
*/
|
|
236
296
|
deserializeStr() {
|
|
237
297
|
const value = this.deserializeBytes();
|
|
@@ -243,6 +303,8 @@ var Deserializer = class {
|
|
|
243
303
|
*
|
|
244
304
|
* layout for "bytes": bytes_length | bytes. bytes_length is the length of the bytes array that is
|
|
245
305
|
* uleb128/ubeb128 encoded. bytes_length is a u32 integer.
|
|
306
|
+
*
|
|
307
|
+
* @returns {Bytes} The deserialized bytes.
|
|
246
308
|
*/
|
|
247
309
|
deserializeBytes() {
|
|
248
310
|
const len = this.littleEndian ? this.deserializeUleb128AsU32() : this.deserializeUbeb128AsU32();
|
|
@@ -251,6 +313,8 @@ var Deserializer = class {
|
|
|
251
313
|
/**
|
|
252
314
|
* Deserializes an array of bytes. The number of bytes to read is already known.
|
|
253
315
|
*
|
|
316
|
+
* @param {number} len - The number of bytes to read.
|
|
317
|
+
* @returns {Bytes} The deserialized bytes.
|
|
254
318
|
*/
|
|
255
319
|
deserializeFixedBytes(len) {
|
|
256
320
|
return new Uint8Array(this.read(len));
|
|
@@ -259,6 +323,9 @@ var Deserializer = class {
|
|
|
259
323
|
* Deserializes a boolean value.
|
|
260
324
|
*
|
|
261
325
|
* layout for "boolean": One byte. "0x01" for True and "0x00" for False.
|
|
326
|
+
*
|
|
327
|
+
* @returns {boolean} The deserialized boolean value.
|
|
328
|
+
* @throws {Error} If the boolean value is invalid.
|
|
262
329
|
*/
|
|
263
330
|
deserializeBool() {
|
|
264
331
|
const bool = new Uint8Array(this.read(1))[0];
|
|
@@ -271,6 +338,8 @@ var Deserializer = class {
|
|
|
271
338
|
* Deserializes a uint8 number.
|
|
272
339
|
*
|
|
273
340
|
* layout for "uint8": One byte. Binary format in little-endian representation.
|
|
341
|
+
*
|
|
342
|
+
* @returns {Uint8} The deserialized uint8 number.
|
|
274
343
|
*/
|
|
275
344
|
deserializeU8() {
|
|
276
345
|
return new DataView(this.read(1)).getUint8(0);
|
|
@@ -286,6 +355,7 @@ var Deserializer = class {
|
|
|
286
355
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34]), false);
|
|
287
356
|
* assert(deserializer.deserializeU16() === 4660);
|
|
288
357
|
* ```
|
|
358
|
+
* @returns {Uint16} The deserialized uint16 number.
|
|
289
359
|
*/
|
|
290
360
|
deserializeU16() {
|
|
291
361
|
return new DataView(this.read(2)).getUint16(0, this.littleEndian);
|
|
@@ -301,6 +371,7 @@ var Deserializer = class {
|
|
|
301
371
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78]), false);
|
|
302
372
|
* assert(deserializer.deserializeU32() === 305419896);
|
|
303
373
|
* ```
|
|
374
|
+
* @returns {Uint32} The deserialized uint32 number.
|
|
304
375
|
*/
|
|
305
376
|
deserializeU32() {
|
|
306
377
|
return new DataView(this.read(4)).getUint32(0, this.littleEndian);
|
|
@@ -316,6 +387,7 @@ var Deserializer = class {
|
|
|
316
387
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]), false);
|
|
317
388
|
* assert(deserializer.deserializeU64() === 1311768467750121216);
|
|
318
389
|
* ```
|
|
390
|
+
* @returns {Uint64} The deserialized uint64 number.
|
|
319
391
|
*/
|
|
320
392
|
deserializeU64() {
|
|
321
393
|
const low = this.deserializeU32();
|
|
@@ -327,6 +399,8 @@ var Deserializer = class {
|
|
|
327
399
|
* Deserializes a uint128 number.
|
|
328
400
|
*
|
|
329
401
|
* layout for "uint128": Sixteen bytes. Binary format in little-endian representation.
|
|
402
|
+
*
|
|
403
|
+
* @returns {Uint128} The deserialized uint128 number.
|
|
330
404
|
*/
|
|
331
405
|
deserializeU128() {
|
|
332
406
|
const low = this.deserializeU64();
|
|
@@ -338,6 +412,8 @@ var Deserializer = class {
|
|
|
338
412
|
* Deserializes a uint256 number.
|
|
339
413
|
*
|
|
340
414
|
* layout for "uint256": Thirty-two bytes. Binary format in little-endian representation.
|
|
415
|
+
*
|
|
416
|
+
* @returns {Uint256} The deserialized uint256 number.
|
|
341
417
|
*/
|
|
342
418
|
deserializeU256() {
|
|
343
419
|
const low = this.deserializeU128();
|
|
@@ -349,6 +425,9 @@ var Deserializer = class {
|
|
|
349
425
|
* Deserializes a uleb128 encoded uint32 number.
|
|
350
426
|
*
|
|
351
427
|
* use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
|
|
428
|
+
*
|
|
429
|
+
* @returns {Uint32} The deserialized uleb128 encoded uint32 number.
|
|
430
|
+
* @throws {Error} If there is an overflow while parsing the value.
|
|
352
431
|
*/
|
|
353
432
|
deserializeUleb128AsU32() {
|
|
354
433
|
let value = BigInt(0);
|
|
@@ -370,6 +449,9 @@ var Deserializer = class {
|
|
|
370
449
|
* Deserializes a unsigned Variable-Length Big-Endian Encoding encoded uint32 number.
|
|
371
450
|
*
|
|
372
451
|
* use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
|
|
452
|
+
*
|
|
453
|
+
* @returns {Uint32} The deserialized ubeb128 encoded uint32 number.
|
|
454
|
+
* @throws {Error} If there is an overflow while parsing the value.
|
|
373
455
|
*/
|
|
374
456
|
deserializeUbeb128AsU32() {
|
|
375
457
|
let value = BigInt(0);
|
|
@@ -487,5 +569,5 @@ exports.bcsToBytes = bcsToBytes;
|
|
|
487
569
|
exports.deserializeVector = deserializeVector;
|
|
488
570
|
exports.serializeVector = serializeVector;
|
|
489
571
|
exports.serializeVectorWithFunc = serializeVectorWithFunc;
|
|
490
|
-
//# sourceMappingURL=
|
|
572
|
+
//# sourceMappingURL=index.cjs.map
|
|
491
573
|
//# sourceMappingURL=index.cjs.map
|
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,CAAC;AAC5D,UAAI,WAAW,SAAS,EAAE,IAAI,IAAI,WAAW,KAAK,MAAM,CAAC;AACzD,WAAK,SAAS;AAAA,IAClB;AAAA,EACJ;AAAA,EAEU,UAAU,QAAqB;AACrC,SAAK,2BAA2B,OAAO,MAAM;AAC7C,QAAI,WAAW,KAAK,QAAQ,KAAK,MAAM,EAAE,IAAI,MAAM;AACnD,SAAK,UAAU,OAAO;AAAA,EAC1B;AAAA,EAEQ,sBACJ,IACA,aACA,OACI;AACJ,SAAK,2BAA2B,WAAW;AAC3C,UAAM,KAAK,IAAI,SAAS,KAAK,QAAQ,KAAK,MAAM;AAChD,OAAG,MAAM,IAAI,CAAC,GAAG,OAAO,KAAK,YAAY,CAAC;AAC1C,SAAK,UAAU;AAAA,EACnB;AAAA;AAAA;AA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B,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,aAAuB,GAAA,GAAA;AAM7B,IAAM,cAAyB,GAAA,KAAA;AAM/B,IAAM,cAAyB,GAAA,UAAA;AAM/B,IAAM,eAA0B,GAAA,qBAAA;AAMhC,IAAM,gBAA4B,GAAA,wCAAA;AAMlC,IAAM,gBAA4B,GAAA,+EAAA;;;ACtBlC,IAAM,aAAN,MAAiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWpB,WAAA,CAAY,eAAe,IAAM,EAAA;AAC7B,IAAA,IAAA,CAAK,YAAe,GAAA,YAAA;AACpB,IAAK,IAAA,CAAA,MAAA,GAAS,IAAI,WAAA,CAAY,EAAE,CAAA;AAChC,IAAA,IAAA,CAAK,MAAS,GAAA,CAAA;AAAA;AAClB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,2BAA2B,KAAqB,EAAA;AACpD,IAAA,OAAO,IAAK,CAAA,MAAA,CAAO,UAAa,GAAA,IAAA,CAAK,SAAS,KAAO,EAAA;AACjD,MAAA,MAAM,YAAY,IAAI,WAAA,CAAY,IAAK,CAAA,MAAA,CAAO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M,MAAA,UAAA,GAAa,IAAI,UAAW,EAAA;AAClC,EAAA,UAAA,CAAW,cAAc,KAAK,CAAA;AAC9B,EAAA,OAAO,WAAW,QAAS,EAAA;AAC/B;AAQO,SAAS,iBAAiB,KAAuB,EAAA;AACpD,EAAM,MAAA,UAAA,GAAa,IAAI,UAAW,EAAA;AAClC,EAAA,UAAA,CAAW,cAAc,KAAK,CAAA;AAC9B,EAAA,OAAO,WAAW,QAAS,EAAA;AAC/B;AAQO,SAAS,gBAAgB,KAAsB,EAAA;AAClD,EAAM,MAAA,UAAA,GAAa,IAAI,UAAW,EAAA;AAClC,EAAA,UAAA,CAAW,aAAa,KAAK,CAAA;AAC7B,EAAA,OAAO,WAAW,QAAS,EAAA;AAC/B;AAQO,SAAS,kBAAkB,KAAqB,EAAA;AACnD,EAAM,MAAA,UAAA,GAAa,IAAI,UAAW,EAAA;AAClC,EAAA,UAAA,CAAW,eAAe,KAAK,CAAA;AAC/B,EAAA,OAAO,WAAW,QAAS,EAAA;AAC/B;AAQO,SAAS,uBAAuB,KAAqB,EAAA;AACxD,EAAM,MAAA,UAAA,GAAa,IAAI,UAAW,EAAA;AAClC,EAAA,UAAA,CAAW,oBAAoB,KAAK,CAAA;AACpC,EAAA,OAAO,WAAW,QAAS,EAAA;AAC/B","file":"index.cjs","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"]}
|