@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/dist/index.mjs
CHANGED
|
@@ -20,11 +20,22 @@ var MAX_U256_BIG_INT = 115792089237316195423570985008687907853269984665640564039
|
|
|
20
20
|
|
|
21
21
|
// src/serializer.ts
|
|
22
22
|
var Serializer = class {
|
|
23
|
+
/**
|
|
24
|
+
* Creates an instance of Serializer.
|
|
25
|
+
*
|
|
26
|
+
* @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.
|
|
27
|
+
*/
|
|
23
28
|
constructor(littleEndian = true) {
|
|
24
29
|
this.littleEndian = littleEndian;
|
|
25
30
|
this.buffer = new ArrayBuffer(64);
|
|
26
31
|
this.offset = 0;
|
|
27
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Ensures that the buffer has enough space to handle the specified number of bytes.
|
|
35
|
+
* If the buffer is not large enough, it will be resized to accommodate the additional bytes.
|
|
36
|
+
*
|
|
37
|
+
* @param {number} bytes - The number of bytes to ensure space for.
|
|
38
|
+
*/
|
|
28
39
|
ensureBufferWillHandleSize(bytes) {
|
|
29
40
|
while (this.buffer.byteLength < this.offset + bytes) {
|
|
30
41
|
const newBuffer = new ArrayBuffer(this.buffer.byteLength * 2);
|
|
@@ -32,11 +43,23 @@ var Serializer = class {
|
|
|
32
43
|
this.buffer = newBuffer;
|
|
33
44
|
}
|
|
34
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Serializes the given byte values into the buffer.
|
|
48
|
+
*
|
|
49
|
+
* @param {Bytes} values - The byte values to serialize.
|
|
50
|
+
*/
|
|
35
51
|
serialize(values) {
|
|
36
52
|
this.ensureBufferWillHandleSize(values.length);
|
|
37
53
|
new Uint8Array(this.buffer, this.offset).set(values);
|
|
38
54
|
this.offset += values.length;
|
|
39
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Serializes a value using the specified DataView function.
|
|
58
|
+
*
|
|
59
|
+
* @param {(byteOffset: number, value: number, littleEndian?: boolean) => void} fn - The DataView function to use for serialization.
|
|
60
|
+
* @param {number} bytesLength - The length of the bytes to serialize.
|
|
61
|
+
* @param {number} value - The value to serialize.
|
|
62
|
+
*/
|
|
40
63
|
serializeWithFunction(fn, bytesLength, value) {
|
|
41
64
|
this.ensureBufferWillHandleSize(bytesLength);
|
|
42
65
|
const dv = new DataView(this.buffer, this.offset);
|
|
@@ -61,6 +84,7 @@ var Serializer = class {
|
|
|
61
84
|
serializeStr(value) {
|
|
62
85
|
const textEncoder = new TextEncoder();
|
|
63
86
|
this.serializeBytes(textEncoder.encode(value));
|
|
87
|
+
return this;
|
|
64
88
|
}
|
|
65
89
|
/**
|
|
66
90
|
* Serializes an array of bytes.
|
|
@@ -75,6 +99,7 @@ var Serializer = class {
|
|
|
75
99
|
this.serializeU32AsUbeb128(value.length);
|
|
76
100
|
}
|
|
77
101
|
this.serialize(value);
|
|
102
|
+
return this;
|
|
78
103
|
}
|
|
79
104
|
/**
|
|
80
105
|
* Serializes an array of bytes with known length. Therefore length doesn't need to be
|
|
@@ -83,6 +108,7 @@ var Serializer = class {
|
|
|
83
108
|
*/
|
|
84
109
|
serializeFixedBytes(value) {
|
|
85
110
|
this.serialize(value);
|
|
111
|
+
return this;
|
|
86
112
|
}
|
|
87
113
|
/**
|
|
88
114
|
* Serializes a boolean value.
|
|
@@ -95,15 +121,19 @@ var Serializer = class {
|
|
|
95
121
|
}
|
|
96
122
|
const byteValue = value ? 1 : 0;
|
|
97
123
|
this.serialize(new Uint8Array([byteValue]));
|
|
124
|
+
return this;
|
|
98
125
|
}
|
|
99
126
|
serializeU8(value) {
|
|
100
127
|
this.serialize(new Uint8Array([value]));
|
|
128
|
+
return this;
|
|
101
129
|
}
|
|
102
130
|
serializeU16(value) {
|
|
103
131
|
this.serializeWithFunction(DataView.prototype.setUint16, 2, value);
|
|
132
|
+
return this;
|
|
104
133
|
}
|
|
105
134
|
serializeU32(value) {
|
|
106
135
|
this.serializeWithFunction(DataView.prototype.setUint32, 4, value);
|
|
136
|
+
return this;
|
|
107
137
|
}
|
|
108
138
|
serializeU64(value) {
|
|
109
139
|
const low = BigInt(value.toString()) & BigInt(MAX_U32_NUMBER);
|
|
@@ -112,6 +142,7 @@ var Serializer = class {
|
|
|
112
142
|
array.forEach((x) => {
|
|
113
143
|
this.serializeU32(Number(x));
|
|
114
144
|
});
|
|
145
|
+
return this;
|
|
115
146
|
}
|
|
116
147
|
serializeU128(value) {
|
|
117
148
|
const low = BigInt(value.toString()) & MAX_U64_BIG_INT;
|
|
@@ -120,6 +151,7 @@ var Serializer = class {
|
|
|
120
151
|
array.forEach((x) => {
|
|
121
152
|
this.serializeU64(x);
|
|
122
153
|
});
|
|
154
|
+
return this;
|
|
123
155
|
}
|
|
124
156
|
serializeU256(value) {
|
|
125
157
|
const low = BigInt(value.toString()) & MAX_U128_BIG_INT;
|
|
@@ -128,6 +160,7 @@ var Serializer = class {
|
|
|
128
160
|
array.forEach((x) => {
|
|
129
161
|
this.serializeU128(x);
|
|
130
162
|
});
|
|
163
|
+
return this;
|
|
131
164
|
}
|
|
132
165
|
serializeU32AsUleb128(val) {
|
|
133
166
|
let value = val;
|
|
@@ -138,6 +171,7 @@ var Serializer = class {
|
|
|
138
171
|
}
|
|
139
172
|
valueArray.push(value);
|
|
140
173
|
this.serialize(new Uint8Array(valueArray));
|
|
174
|
+
return this;
|
|
141
175
|
}
|
|
142
176
|
serializeU32AsUbeb128(val) {
|
|
143
177
|
let value = val;
|
|
@@ -149,9 +183,12 @@ var Serializer = class {
|
|
|
149
183
|
valueArray.unshift(byte);
|
|
150
184
|
}
|
|
151
185
|
this.serialize(new Uint8Array(valueArray));
|
|
186
|
+
return this;
|
|
152
187
|
}
|
|
153
188
|
/**
|
|
154
189
|
* Returns the buffered bytes
|
|
190
|
+
*
|
|
191
|
+
* @returns {Bytes} The buffered bytes.
|
|
155
192
|
*/
|
|
156
193
|
getBytes() {
|
|
157
194
|
return new Uint8Array(this.buffer).slice(0, this.offset);
|
|
@@ -189,7 +226,7 @@ function checkNumberRange(minValue, maxValue, message) {
|
|
|
189
226
|
if (valueBigInt > BigInt(maxValue.toString()) || valueBigInt < BigInt(minValue.toString())) {
|
|
190
227
|
throw new Error(message ?? "Value is out of range");
|
|
191
228
|
}
|
|
192
|
-
childFunction.apply(this, [value]);
|
|
229
|
+
return childFunction.apply(this, [value]);
|
|
193
230
|
};
|
|
194
231
|
return descriptor;
|
|
195
232
|
};
|
|
@@ -197,12 +234,25 @@ function checkNumberRange(minValue, maxValue, message) {
|
|
|
197
234
|
|
|
198
235
|
// src/deserializer.ts
|
|
199
236
|
var Deserializer = class {
|
|
237
|
+
/**
|
|
238
|
+
* Creates an instance of Deserializer.
|
|
239
|
+
*
|
|
240
|
+
* @param {Bytes} data - The data to deserialize.
|
|
241
|
+
* @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.
|
|
242
|
+
*/
|
|
200
243
|
constructor(data, littleEndian = true) {
|
|
201
244
|
this.littleEndian = littleEndian;
|
|
202
245
|
this.buffer = new ArrayBuffer(data.length);
|
|
203
246
|
new Uint8Array(this.buffer).set(data, 0);
|
|
204
247
|
this.offset = 0;
|
|
205
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Reads a specified number of bytes from the buffer.
|
|
251
|
+
*
|
|
252
|
+
* @param {number} length - The number of bytes to read.
|
|
253
|
+
* @returns {ArrayBuffer} The read bytes.
|
|
254
|
+
* @throws {Error} If the end of the buffer is reached.
|
|
255
|
+
*/
|
|
206
256
|
read(length) {
|
|
207
257
|
if (this.offset + length > this.buffer.byteLength) {
|
|
208
258
|
throw new Error("Reached to the end of buffer");
|
|
@@ -211,9 +261,19 @@ var Deserializer = class {
|
|
|
211
261
|
this.offset += length;
|
|
212
262
|
return bytes;
|
|
213
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Returns the number of remaining bytes in the buffer.
|
|
266
|
+
*
|
|
267
|
+
* @returns {number} The number of remaining bytes.
|
|
268
|
+
*/
|
|
214
269
|
remaining() {
|
|
215
270
|
return this.buffer.byteLength - this.offset;
|
|
216
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* Rewinds the buffer by a specified number of bytes.
|
|
274
|
+
*
|
|
275
|
+
* @param {number} length - The number of bytes to rewind.
|
|
276
|
+
*/
|
|
217
277
|
rewind(length) {
|
|
218
278
|
this.offset -= length;
|
|
219
279
|
}
|
|
@@ -230,6 +290,7 @@ var Deserializer = class {
|
|
|
230
290
|
* 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]), true);
|
|
231
291
|
* assert(deserializer.deserializeStr() === "çå∞≠¢õß∂ƒ∫");
|
|
232
292
|
* ```
|
|
293
|
+
* @returns {string} The deserialized string.
|
|
233
294
|
*/
|
|
234
295
|
deserializeStr() {
|
|
235
296
|
const value = this.deserializeBytes();
|
|
@@ -241,6 +302,8 @@ var Deserializer = class {
|
|
|
241
302
|
*
|
|
242
303
|
* layout for "bytes": bytes_length | bytes. bytes_length is the length of the bytes array that is
|
|
243
304
|
* uleb128/ubeb128 encoded. bytes_length is a u32 integer.
|
|
305
|
+
*
|
|
306
|
+
* @returns {Bytes} The deserialized bytes.
|
|
244
307
|
*/
|
|
245
308
|
deserializeBytes() {
|
|
246
309
|
const len = this.littleEndian ? this.deserializeUleb128AsU32() : this.deserializeUbeb128AsU32();
|
|
@@ -249,6 +312,8 @@ var Deserializer = class {
|
|
|
249
312
|
/**
|
|
250
313
|
* Deserializes an array of bytes. The number of bytes to read is already known.
|
|
251
314
|
*
|
|
315
|
+
* @param {number} len - The number of bytes to read.
|
|
316
|
+
* @returns {Bytes} The deserialized bytes.
|
|
252
317
|
*/
|
|
253
318
|
deserializeFixedBytes(len) {
|
|
254
319
|
return new Uint8Array(this.read(len));
|
|
@@ -257,6 +322,9 @@ var Deserializer = class {
|
|
|
257
322
|
* Deserializes a boolean value.
|
|
258
323
|
*
|
|
259
324
|
* layout for "boolean": One byte. "0x01" for True and "0x00" for False.
|
|
325
|
+
*
|
|
326
|
+
* @returns {boolean} The deserialized boolean value.
|
|
327
|
+
* @throws {Error} If the boolean value is invalid.
|
|
260
328
|
*/
|
|
261
329
|
deserializeBool() {
|
|
262
330
|
const bool = new Uint8Array(this.read(1))[0];
|
|
@@ -269,6 +337,8 @@ var Deserializer = class {
|
|
|
269
337
|
* Deserializes a uint8 number.
|
|
270
338
|
*
|
|
271
339
|
* layout for "uint8": One byte. Binary format in little-endian representation.
|
|
340
|
+
*
|
|
341
|
+
* @returns {Uint8} The deserialized uint8 number.
|
|
272
342
|
*/
|
|
273
343
|
deserializeU8() {
|
|
274
344
|
return new DataView(this.read(1)).getUint8(0);
|
|
@@ -284,6 +354,7 @@ var Deserializer = class {
|
|
|
284
354
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34]), false);
|
|
285
355
|
* assert(deserializer.deserializeU16() === 4660);
|
|
286
356
|
* ```
|
|
357
|
+
* @returns {Uint16} The deserialized uint16 number.
|
|
287
358
|
*/
|
|
288
359
|
deserializeU16() {
|
|
289
360
|
return new DataView(this.read(2)).getUint16(0, this.littleEndian);
|
|
@@ -299,6 +370,7 @@ var Deserializer = class {
|
|
|
299
370
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78]), false);
|
|
300
371
|
* assert(deserializer.deserializeU32() === 305419896);
|
|
301
372
|
* ```
|
|
373
|
+
* @returns {Uint32} The deserialized uint32 number.
|
|
302
374
|
*/
|
|
303
375
|
deserializeU32() {
|
|
304
376
|
return new DataView(this.read(4)).getUint32(0, this.littleEndian);
|
|
@@ -314,6 +386,7 @@ var Deserializer = class {
|
|
|
314
386
|
* const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]), false);
|
|
315
387
|
* assert(deserializer.deserializeU64() === 1311768467750121216);
|
|
316
388
|
* ```
|
|
389
|
+
* @returns {Uint64} The deserialized uint64 number.
|
|
317
390
|
*/
|
|
318
391
|
deserializeU64() {
|
|
319
392
|
const low = this.deserializeU32();
|
|
@@ -325,6 +398,8 @@ var Deserializer = class {
|
|
|
325
398
|
* Deserializes a uint128 number.
|
|
326
399
|
*
|
|
327
400
|
* layout for "uint128": Sixteen bytes. Binary format in little-endian representation.
|
|
401
|
+
*
|
|
402
|
+
* @returns {Uint128} The deserialized uint128 number.
|
|
328
403
|
*/
|
|
329
404
|
deserializeU128() {
|
|
330
405
|
const low = this.deserializeU64();
|
|
@@ -336,6 +411,8 @@ var Deserializer = class {
|
|
|
336
411
|
* Deserializes a uint256 number.
|
|
337
412
|
*
|
|
338
413
|
* layout for "uint256": Thirty-two bytes. Binary format in little-endian representation.
|
|
414
|
+
*
|
|
415
|
+
* @returns {Uint256} The deserialized uint256 number.
|
|
339
416
|
*/
|
|
340
417
|
deserializeU256() {
|
|
341
418
|
const low = this.deserializeU128();
|
|
@@ -347,6 +424,9 @@ var Deserializer = class {
|
|
|
347
424
|
* Deserializes a uleb128 encoded uint32 number.
|
|
348
425
|
*
|
|
349
426
|
* use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
|
|
427
|
+
*
|
|
428
|
+
* @returns {Uint32} The deserialized uleb128 encoded uint32 number.
|
|
429
|
+
* @throws {Error} If there is an overflow while parsing the value.
|
|
350
430
|
*/
|
|
351
431
|
deserializeUleb128AsU32() {
|
|
352
432
|
let value = BigInt(0);
|
|
@@ -368,6 +448,9 @@ var Deserializer = class {
|
|
|
368
448
|
* Deserializes a unsigned Variable-Length Big-Endian Encoding encoded uint32 number.
|
|
369
449
|
*
|
|
370
450
|
* use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
|
|
451
|
+
*
|
|
452
|
+
* @returns {Uint32} The deserialized ubeb128 encoded uint32 number.
|
|
453
|
+
* @throws {Error} If there is an overflow while parsing the value.
|
|
371
454
|
*/
|
|
372
455
|
deserializeUbeb128AsU32() {
|
|
373
456
|
let value = BigInt(0);
|
package/dist/index.mjs.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,Y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ClD,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;AAAA,EAWpB,YAAY,eAAe,MAAM;AAC7B,SAAK,eAAe;AACpB,SAAK,SAAS,IAAI,YAAY,EAAE;AAChC,SAAK,SAAS;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,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;AAAA;AAAA;AAAA;AAAA;AAAA,EAOU,U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AW;AAClC,aAAW,aAAa,KAAK;AAC7B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,eAAe,OAAqB;AAChD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,YAAY,KAAK;AAC5B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,gBAAgB,OAAsB;AAClD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,aAAa,KAAK;AAC7B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,gBAAgB,OAAsB;AAClD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,aAAa,KAAK;AAC7B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,iBAAiB,OAAyB;AACtD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,cAAc,KAAK;AAC9B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,iBAAiB,OAAyB;AACtD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,cAAc,KAAK;AAC9B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,iBAAiB,OAAuB;AACpD,QAAM,aAAa,IAAI,WAAW;AAClC,aAAW,cAAc,KAAK;AAC9B,SAAO,WAAW,SAAS;AAC/B;AAQO,SAAS,gBAAgB,OAAsB;AAClD,QAAM,aAAa,IAAI,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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@layerzerolabs/lz-serdes",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.16",
|
|
4
4
|
"description": "LayerZero Core Library",
|
|
5
5
|
"license": "BUSL-1.1",
|
|
6
6
|
"exports": {
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@jest/globals": "^29.7.0",
|
|
28
|
-
"@layerzerolabs/tsup-config-next": "^3.0.
|
|
29
|
-
"@layerzerolabs/typescript-config-next": "^3.0.
|
|
28
|
+
"@layerzerolabs/tsup-config-next": "^3.0.16",
|
|
29
|
+
"@layerzerolabs/typescript-config-next": "^3.0.16",
|
|
30
30
|
"@types/jest": "^29.5.10",
|
|
31
31
|
"jest": "^29.7.0",
|
|
32
32
|
"jest-extended": "^4.0.2",
|