@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/dist/index.d.mts CHANGED
@@ -1,20 +1,74 @@
1
+ /**
2
+ * Type representing a sequence of elements.
3
+ * @template T - The type of elements in the sequence.
4
+ */
1
5
  type Seq<T> = T[];
6
+ /**
7
+ * Type representing an 8-bit unsigned integer.
8
+ */
2
9
  type Uint8 = number;
10
+ /**
11
+ * Type representing a 16-bit unsigned integer.
12
+ */
3
13
  type Uint16 = number;
14
+ /**
15
+ * Type representing a 32-bit unsigned integer.
16
+ */
4
17
  type Uint32 = number;
18
+ /**
19
+ * Type representing a 64-bit unsigned integer.
20
+ */
5
21
  type Uint64 = bigint;
22
+ /**
23
+ * Type representing a 128-bit unsigned integer.
24
+ */
6
25
  type Uint128 = bigint;
26
+ /**
27
+ * Type representing a 256-bit unsigned integer.
28
+ */
7
29
  type Uint256 = bigint;
30
+ /**
31
+ * Type representing a number that can be either a bigint or a number.
32
+ */
8
33
  type AnyNumber = bigint | number;
34
+ /**
35
+ * Type representing a byte array.
36
+ */
9
37
  type Bytes = Uint8Array;
10
38
 
39
+ /**
40
+ * Class representing a serializer for various data types.
41
+ */
11
42
  declare class Serializer {
12
43
  private buffer;
13
44
  private offset;
14
45
  readonly littleEndian: boolean;
46
+ /**
47
+ * Creates an instance of Serializer.
48
+ *
49
+ * @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.
50
+ */
15
51
  constructor(littleEndian?: boolean);
52
+ /**
53
+ * Ensures that the buffer has enough space to handle the specified number of bytes.
54
+ * If the buffer is not large enough, it will be resized to accommodate the additional bytes.
55
+ *
56
+ * @param {number} bytes - The number of bytes to ensure space for.
57
+ */
16
58
  private ensureBufferWillHandleSize;
59
+ /**
60
+ * Serializes the given byte values into the buffer.
61
+ *
62
+ * @param {Bytes} values - The byte values to serialize.
63
+ */
17
64
  protected serialize(values: Bytes): void;
65
+ /**
66
+ * Serializes a value using the specified DataView function.
67
+ *
68
+ * @param {(byteOffset: number, value: number, littleEndian?: boolean) => void} fn - The DataView function to use for serialization.
69
+ * @param {number} bytesLength - The length of the bytes to serialize.
70
+ * @param {number} value - The value to serialize.
71
+ */
18
72
  private serializeWithFunction;
19
73
  /**
20
74
  * Serializes a string. UTF8 string is supported. Serializes the string's bytes length "l" first,
@@ -31,32 +85,32 @@ declare class Serializer {
31
85
  * 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]));
32
86
  * ```
33
87
  */
34
- serializeStr(value: string): void;
88
+ serializeStr(value: string): this;
35
89
  /**
36
90
  * Serializes an array of bytes.
37
91
  *
38
92
  * layout for "bytes": bytes_length | bytes. bytes_length is the length of the bytes array that is
39
93
  * uleb128/ubeb128 encoded. bytes_length is a u32 integer.
40
94
  */
41
- serializeBytes(value: Bytes): void;
95
+ serializeBytes(value: Bytes): this;
42
96
  /**
43
97
  * Serializes an array of bytes with known length. Therefore length doesn't need to be
44
98
  * serialized to help deserialization. When deserializing, the number of
45
99
  * bytes to deserialize needs to be passed in.
46
100
  */
47
- serializeFixedBytes(value: Bytes): void;
101
+ serializeFixedBytes(value: Bytes): this;
48
102
  /**
49
103
  * Serializes a boolean value.
50
104
  *
51
105
  * layout for "boolean": One byte. "0x01" for True and "0x00" for False.
52
106
  */
53
- serializeBool(value: boolean): void;
107
+ serializeBool(value: boolean): this;
54
108
  /**
55
109
  * Serializes a uint8 number.
56
110
  *
57
111
  * layout for "uint8": One byte. Binary format in little-endian representation.
58
112
  */
59
- serializeU8(value: Uint8): void;
113
+ serializeU8(value: Uint8): this;
60
114
  /**
61
115
  * Serializes a uint16 number.
62
116
  *
@@ -71,7 +125,7 @@ declare class Serializer {
71
125
  * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34]));
72
126
  * ```
73
127
  */
74
- serializeU16(value: Uint16): void;
128
+ serializeU16(value: Uint16): this;
75
129
  /**
76
130
  * Serializes a uint32 number.
77
131
  *
@@ -86,7 +140,7 @@ declare class Serializer {
86
140
  * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34, 0x56, 0x78]));
87
141
  * ```
88
142
  */
89
- serializeU32(value: Uint32): void;
143
+ serializeU32(value: Uint32): this;
90
144
  /**
91
145
  * Serializes a uint64 number.
92
146
  *
@@ -101,44 +155,72 @@ declare class Serializer {
101
155
  * assert(serializer.getBytes() === new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]));
102
156
  * ```
103
157
  */
104
- serializeU64(value: AnyNumber): void;
158
+ serializeU64(value: AnyNumber): this;
105
159
  /**
106
160
  * Serializes a uint128 number.
107
161
  *
108
162
  * layout for "uint128": Sixteen bytes. Binary format in little-endian representation.
109
163
  */
110
- serializeU128(value: AnyNumber): void;
164
+ serializeU128(value: AnyNumber): this;
111
165
  /**
112
166
  * Serializes a uint256 number.
113
167
  *
114
168
  * layout for "uint256": Sixteen bytes. Binary format in little-endian representation.
115
169
  */
116
- serializeU256(value: AnyNumber): void;
170
+ serializeU256(value: AnyNumber): this;
117
171
  /**
118
172
  * Serializes a uint32 number with uleb128.
119
173
  *
120
174
  * use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
121
175
  */
122
- serializeU32AsUleb128(val: Uint32): void;
176
+ serializeU32AsUleb128(val: Uint32): this;
123
177
  /**
124
178
  * Serializes a uint32 number with unsigned Variable-Length Big-Endian Encoding.
125
179
  *
126
180
  * use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
127
181
  */
128
- serializeU32AsUbeb128(val: Uint32): void;
182
+ serializeU32AsUbeb128(val: Uint32): this;
129
183
  /**
130
184
  * Returns the buffered bytes
185
+ *
186
+ * @returns {Bytes} The buffered bytes.
131
187
  */
132
188
  getBytes(): Bytes;
133
189
  }
134
190
 
191
+ /**
192
+ * Class representing a deserializer for various data types.
193
+ */
135
194
  declare class Deserializer {
136
195
  private buffer;
137
196
  private offset;
138
197
  readonly littleEndian: boolean;
198
+ /**
199
+ * Creates an instance of Deserializer.
200
+ *
201
+ * @param {Bytes} data - The data to deserialize.
202
+ * @param {boolean} [littleEndian=true] - Whether to use little-endian byte order.
203
+ */
139
204
  constructor(data: Bytes, littleEndian?: boolean);
205
+ /**
206
+ * Reads a specified number of bytes from the buffer.
207
+ *
208
+ * @param {number} length - The number of bytes to read.
209
+ * @returns {ArrayBuffer} The read bytes.
210
+ * @throws {Error} If the end of the buffer is reached.
211
+ */
140
212
  private read;
213
+ /**
214
+ * Returns the number of remaining bytes in the buffer.
215
+ *
216
+ * @returns {number} The number of remaining bytes.
217
+ */
141
218
  remaining(): number;
219
+ /**
220
+ * Rewinds the buffer by a specified number of bytes.
221
+ *
222
+ * @param {number} length - The number of bytes to rewind.
223
+ */
142
224
  rewind(length: number): void;
143
225
  /**
144
226
  * Deserializes a string. UTF8 string is supported. Reads the string's bytes length "l" first,
@@ -153,6 +235,7 @@ declare class Deserializer {
153
235
  * 0xe2, 0x89, 0xa0, 0xc2, 0xa2, 0xc3, 0xb5, 0xc3, 0x9f, 0xe2, 0x88, 0x82, 0xc6, 0x92, 0xe2, 0x88, 0xab]), true);
154
236
  * assert(deserializer.deserializeStr() === "çå∞≠¢õß∂ƒ∫");
155
237
  * ```
238
+ * @returns {string} The deserialized string.
156
239
  */
157
240
  deserializeStr(): string;
158
241
  /**
@@ -160,23 +243,32 @@ declare class Deserializer {
160
243
  *
161
244
  * layout for "bytes": bytes_length | bytes. bytes_length is the length of the bytes array that is
162
245
  * uleb128/ubeb128 encoded. bytes_length is a u32 integer.
246
+ *
247
+ * @returns {Bytes} The deserialized bytes.
163
248
  */
164
249
  deserializeBytes(): Bytes;
165
250
  /**
166
251
  * Deserializes an array of bytes. The number of bytes to read is already known.
167
252
  *
253
+ * @param {number} len - The number of bytes to read.
254
+ * @returns {Bytes} The deserialized bytes.
168
255
  */
169
256
  deserializeFixedBytes(len: number): Bytes;
170
257
  /**
171
258
  * Deserializes a boolean value.
172
259
  *
173
260
  * layout for "boolean": One byte. "0x01" for True and "0x00" for False.
261
+ *
262
+ * @returns {boolean} The deserialized boolean value.
263
+ * @throws {Error} If the boolean value is invalid.
174
264
  */
175
265
  deserializeBool(): boolean;
176
266
  /**
177
267
  * Deserializes a uint8 number.
178
268
  *
179
269
  * layout for "uint8": One byte. Binary format in little-endian representation.
270
+ *
271
+ * @returns {Uint8} The deserialized uint8 number.
180
272
  */
181
273
  deserializeU8(): Uint8;
182
274
  /**
@@ -190,6 +282,7 @@ declare class Deserializer {
190
282
  * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34]), false);
191
283
  * assert(deserializer.deserializeU16() === 4660);
192
284
  * ```
285
+ * @returns {Uint16} The deserialized uint16 number.
193
286
  */
194
287
  deserializeU16(): Uint16;
195
288
  /**
@@ -203,6 +296,7 @@ declare class Deserializer {
203
296
  * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78]), false);
204
297
  * assert(deserializer.deserializeU32() === 305419896);
205
298
  * ```
299
+ * @returns {Uint32} The deserialized uint32 number.
206
300
  */
207
301
  deserializeU32(): Uint32;
208
302
  /**
@@ -216,63 +310,169 @@ declare class Deserializer {
216
310
  * const deserializer = new Deserializer(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0xAB, 0xCD, 0xEF, 0x00]), false);
217
311
  * assert(deserializer.deserializeU64() === 1311768467750121216);
218
312
  * ```
313
+ * @returns {Uint64} The deserialized uint64 number.
219
314
  */
220
315
  deserializeU64(): Uint64;
221
316
  /**
222
317
  * Deserializes a uint128 number.
223
318
  *
224
319
  * layout for "uint128": Sixteen bytes. Binary format in little-endian representation.
320
+ *
321
+ * @returns {Uint128} The deserialized uint128 number.
225
322
  */
226
323
  deserializeU128(): Uint128;
227
324
  /**
228
325
  * Deserializes a uint256 number.
229
326
  *
230
327
  * layout for "uint256": Thirty-two bytes. Binary format in little-endian representation.
328
+ *
329
+ * @returns {Uint256} The deserialized uint256 number.
231
330
  */
232
331
  deserializeU256(): Uint256;
233
332
  /**
234
333
  * Deserializes a uleb128 encoded uint32 number.
235
334
  *
236
335
  * use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
336
+ *
337
+ * @returns {Uint32} The deserialized uleb128 encoded uint32 number.
338
+ * @throws {Error} If there is an overflow while parsing the value.
237
339
  */
238
340
  deserializeUleb128AsU32(): Uint32;
239
341
  /**
240
342
  * Deserializes a unsigned Variable-Length Big-Endian Encoding encoded uint32 number.
241
343
  *
242
344
  * use ubeb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
345
+ *
346
+ * @returns {Uint32} The deserialized ubeb128 encoded uint32 number.
347
+ * @throws {Error} If there is an overflow while parsing the value.
243
348
  */
244
349
  deserializeUbeb128AsU32(): Uint32;
245
350
  }
246
351
 
352
+ /**
353
+ * Interface representing a serializable object.
354
+ */
247
355
  interface Serializable {
356
+ /**
357
+ * Serializes the object using the provided serializer.
358
+ *
359
+ * @param {Serializer} serializer - The serializer to use.
360
+ */
248
361
  serialize(serializer: Serializer): void;
249
362
  }
363
+ /**
364
+ * Interface representing a deserializable object.
365
+ */
250
366
  interface Deserializable<T> {
367
+ /**
368
+ * Deserializes the object using the provided deserializer.
369
+ *
370
+ * @param {Deserializer} deserializer - The deserializer to use.
371
+ * @returns {T} The deserialized object.
372
+ */
251
373
  deserialize(deserializer: Deserializer): T;
252
374
  }
253
375
  /**
254
- * Serializes a vector values that are "Serializable".
376
+ * Serializes a vector of values that are "Serializable".
377
+ *
378
+ * @param {Seq<T>} value - The vector of values to serialize.
379
+ * @param {Serializer} serializer - The serializer to use.
255
380
  */
256
381
  declare function serializeVector<T extends Serializable>(value: Seq<T>, serializer: Serializer): void;
257
382
  /**
258
- * Serializes a vector with specified item serialization function.
383
+ * Serializes a vector with a specified item serialization function.
259
384
  * Very dynamic function and bypasses static typechecking.
385
+ *
386
+ * @param {unknown[]} value - The vector of values to serialize.
387
+ * @param {string} func - The name of the serialization function to use.
388
+ * @returns {Bytes} The serialized bytes.
389
+ * @throws {Error} If the specified function does not exist on the serializer.
260
390
  */
261
391
  declare function serializeVectorWithFunc(value: unknown[], func: string): Bytes;
262
392
  /**
263
393
  * Deserializes a vector of values.
394
+ *
395
+ * @param {Deserializer} deserializer - The deserializer to use.
396
+ * @param {Deserializable<T>} cls - The class to use for deserialization.
397
+ * @returns {T[]} The deserialized vector of values.
264
398
  */
265
399
  declare function deserializeVector<T>(deserializer: Deserializer, cls: Deserializable<T>): any[];
400
+ /**
401
+ * Serializes a value to bytes using BCS (Binary Canonical Serialization).
402
+ *
403
+ * @param {T} value - The value to serialize.
404
+ * @returns {Bytes} The serialized bytes.
405
+ */
266
406
  declare function bcsToBytes<T extends Serializable>(value: T): Bytes;
407
+ /**
408
+ * Serializes a uint64 value to bytes using BCS.
409
+ *
410
+ * @param {AnyNumber} value - The value to serialize.
411
+ * @returns {Bytes} The serialized bytes.
412
+ */
267
413
  declare function bcsSerializeUint64(value: AnyNumber): Bytes;
414
+ /**
415
+ * Serializes a uint8 value to bytes using BCS.
416
+ *
417
+ * @param {Uint8} value - The value to serialize.
418
+ * @returns {Bytes} The serialized bytes.
419
+ */
268
420
  declare function bcsSerializeU8(value: Uint8): Bytes;
421
+ /**
422
+ * Serializes a uint16 value to bytes using BCS.
423
+ *
424
+ * @param {Uint16} value - The value to serialize.
425
+ * @returns {Bytes} The serialized bytes.
426
+ */
269
427
  declare function bcsSerializeU16(value: Uint16): Bytes;
428
+ /**
429
+ * Serializes a uint32 value to bytes using BCS.
430
+ *
431
+ * @param {Uint32} value - The value to serialize.
432
+ * @returns {Bytes} The serialized bytes.
433
+ */
270
434
  declare function bcsSerializeU32(value: Uint32): Bytes;
435
+ /**
436
+ * Serializes a uint128 value to bytes using BCS.
437
+ *
438
+ * @param {AnyNumber} value - The value to serialize.
439
+ * @returns {Bytes} The serialized bytes.
440
+ */
271
441
  declare function bcsSerializeU128(value: AnyNumber): Bytes;
442
+ /**
443
+ * Serializes a uint256 value to bytes using BCS.
444
+ *
445
+ * @param {AnyNumber} value - The value to serialize.
446
+ * @returns {Bytes} The serialized bytes.
447
+ */
272
448
  declare function bcsSerializeU256(value: AnyNumber): Bytes;
449
+ /**
450
+ * Serializes a boolean value to bytes using BCS.
451
+ *
452
+ * @param {boolean} value - The value to serialize.
453
+ * @returns {Bytes} The serialized bytes.
454
+ */
273
455
  declare function bcsSerializeBool(value: boolean): Bytes;
456
+ /**
457
+ * Serializes a string value to bytes using BCS.
458
+ *
459
+ * @param {string} value - The value to serialize.
460
+ * @returns {Bytes} The serialized bytes.
461
+ */
274
462
  declare function bcsSerializeStr(value: string): Bytes;
463
+ /**
464
+ * Serializes a byte array to bytes using BCS.
465
+ *
466
+ * @param {Bytes} value - The value to serialize.
467
+ * @returns {Bytes} The serialized bytes.
468
+ */
275
469
  declare function bcsSerializeBytes(value: Bytes): Bytes;
470
+ /**
471
+ * Serializes a fixed-length byte array to bytes using BCS.
472
+ *
473
+ * @param {Bytes} value - The value to serialize.
474
+ * @returns {Bytes} The serialized bytes.
475
+ */
276
476
  declare function bcsSerializeFixedBytes(value: Bytes): Bytes;
277
477
 
278
478
  export { type AnyNumber, type Bytes, Deserializer, type Seq, Serializer, type Uint128, type Uint16, type Uint256, type Uint32, type Uint64, type Uint8, bcsSerializeBool, bcsSerializeBytes, bcsSerializeFixedBytes, bcsSerializeStr, bcsSerializeU128, bcsSerializeU16, bcsSerializeU256, bcsSerializeU32, bcsSerializeU8, bcsSerializeUint64, bcsToBytes, deserializeVector, serializeVector, serializeVectorWithFunc };