-
Notifications
You must be signed in to change notification settings - Fork 71
/
arguments.ts
706 lines (631 loc) · 18.4 KB
/
arguments.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
import { ISerializable } from './interfaces/ISerializable';
import * as ser from './serializers';
/**
* Typed Arguments facilitating the differentiation
* between different argument types due to Javascript's
* single number type.
*
* @remarks In AssemblyScript the latter are all native types
*/
export enum ArgTypes {
STRING,
BOOL,
U8,
U32,
U64,
I128,
U128,
U256,
I32,
I64,
F32,
F64,
ARRAY,
UINT8ARRAY,
SERIALIZABLE,
SERIALIZABLE_OBJECT_ARRAY,
}
export enum ArrayTypes {
STRING,
BOOL,
U8,
U32,
U64,
I128,
U128,
U256,
I32,
I64,
F32,
F64,
}
export interface IParam {
type: ArgTypes;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any;
}
/**
* Native types in AssemblyScript
*
* @remarks
* These are the types that can be used in AssemblyScript
*/
export type NativeType = string | boolean | number | bigint;
/**
* Storage and serialization class for remote function call arguments.
*
* @remarks
* This class can serialize typescript native types into bytes, in order to
* make smart-contract function call easier.
* It also can deserialize bytes.
*
*/
export class Args {
private offset = 0;
private serialized: Uint8Array;
private argsList: Array<IParam> = [];
/**
* Constructor to either serialize or deserialize data passed from/to DApps and remote Smart contracts.
*
* @param serialized - The optional serialized arguments to deserialize.
* @param offset - The optional offset to start deserializing from.
*/
constructor(serialized: Array<number> | Uint8Array = [], offset = 0) {
this.serialized = Uint8Array.from(serialized);
this.offset = offset;
}
public getArgsList(): Array<IParam> {
return this.argsList;
}
/**
* Returns the current deserialization offset of the serialized byte array.
*
* @returns the current offset
*/
public getOffset(): number {
return this.offset;
}
/**
* Returns the serialized byte array.
*
* @returns A byte array.
*/
public serialize(): Array<number> {
return Array.from(this.serialized);
}
// Getters
/**
* Returns the next string in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized string
*/
public nextString(): string {
const length = this.nextU32();
const end = this.offset + length;
const result = ser.bytesToStr(this.serialized.slice(this.offset, end));
this.offset = end;
return result;
}
/**
* Returns the next unsigned byte in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextU8(): bigint {
const value = ser.byteToU8(this.serialized, this.offset);
this.offset++;
return BigInt(value);
}
/**
* Returns the next unsigned integer in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number
*/
public nextU32(): number {
const value = ser.bytesToU32(this.serialized, this.offset);
this.offset += 4;
return value;
}
/**
* Returns the next long integer in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextU64(): bigint {
const value = ser.bytesToU64(this.serialized, this.offset);
this.offset += 8;
return value;
}
/**
* Returns the next int128 in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextI128(): bigint {
const value = ser.bytesToI128(this.serialized, this.offset);
this.offset += 16;
return value;
}
/**
* Returns the next uint128 in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextU128(): bigint {
const value = ser.bytesToU128(this.serialized, this.offset);
this.offset += 16;
return value;
}
/**
* Returns the next uint256 in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextU256(): bigint {
const value = ser.bytesToU256(this.serialized, this.offset);
this.offset += 32;
return value;
}
/**
* Returns the next boolean in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized boolean.
*/
nextBool(): boolean {
return !!this.serialized[this.offset++];
}
/**
* Returns the next signed integer in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextI32(): number {
const value = ser.bytesToI32(this.serialized, this.offset);
this.offset += 4;
return value;
}
/**
* Returns the next signed long integer in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextI64(): bigint {
const value = ser.bytesToI64(this.serialized, this.offset);
this.offset += 8;
return BigInt(value);
}
/**
* Returns the next floating number in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextF32(): number {
const value = ser.bytesToF32(this.serialized, this.offset);
this.offset += 4;
return value;
}
/**
* Returns the next long floating number in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized number.
*/
public nextF64(): number {
const value = ser.bytesToF64(this.serialized, this.offset);
this.offset += 8;
return value;
}
/**
* Returns the next sub byte array in the serialized byte array.
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @returns the deserialized byte array.
*/
public nextUint8Array(): Uint8Array {
const length = this.nextU32();
const byteArray = this.serialized.slice(this.offset, this.offset + length);
this.offset += length;
return byteArray;
}
/**
* Returns the next {@link ISerializable} object in the serialized byte array
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @param ctor - the class constructor prototype T.prototype
*
* @returns the deserialized object T
*/
public nextSerializable<T extends ISerializable<T>>(ctor: new () => T): T {
let deserializationResult = ser.deserializeObj(
this.serialized,
this.offset,
ctor,
);
this.offset = deserializationResult.offset;
return deserializationResult.instance;
}
/**
* Returns the next array of {@link ISerializable} objects in the serialized byte array
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @param ctor - the class constructor prototype T.prototype
*
* @returns the deserialized array of object that implement ISerializable
*/
public nextSerializableObjectArray<T extends ISerializable<T>>(
ctor: new () => T,
): T[] {
const length = this.nextU32();
if (!length) {
return [];
}
if (this.offset + length > this.serialized.length) {
throw new Error("can't deserialize length of array from given argument");
}
const buffer = this.getNextData(length);
const value = ser.bytesToSerializableObjectArray<T>(buffer, ctor);
this.offset += length;
return value;
}
/**
* Returns the next array of {@link ArgTypes} objects in the serialized byte array
*
* @remarks
* Increments to offset to point the data after the one that as been deserialized in the byte array.
*
* @param type - the type of the elements in the array.
*
* @returns the next array of object that are native type
*/
nextArray<T>(type: ArrayTypes): T[] {
const length = this.nextU32();
if (!length) {
return [];
}
if (this.offset + length > this.serialized.length) {
throw new Error("can't deserialize length of array from given argument");
}
const buffer = this.getNextData(length);
const value = ser.bytesToArray<T>(buffer, type);
this.offset += length;
return value;
}
// Setter
/**
* Adds a unsigned byte to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addU8(value: number): this {
this.serialized = Args.concatArrays(this.serialized, ser.u8toByte(value));
this.offset++;
this.argsList.push({ type: ArgTypes.U8, value: value });
return this;
}
/**
* Adds a boolean to the serialized arguments.
*
* @param value - the boolean to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addBool(value: boolean): this {
this.serialized = Args.concatArrays(
this.serialized,
ser.u8toByte(value ? 1 : 0),
);
this.offset++;
this.argsList.push({ type: ArgTypes.BOOL, value: value });
return this;
}
/**
* Adds an unsigned integer to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addU32(value: number): this {
this.serialized = Args.concatArrays(this.serialized, ser.u32ToBytes(value));
this.offset += 4;
this.argsList.push({ type: ArgTypes.U32, value: value });
return this;
}
/**
* Adds an unsigned long integer to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addU64(bigInt: bigint): this {
this.serialized = Args.concatArrays(
this.serialized,
ser.u64ToBytes(bigInt),
);
this.offset += 8;
this.argsList.push({ type: ArgTypes.U64, value: bigInt });
return this;
}
/**
* Adds a signed long integer to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addI128(bigInt: bigint): this {
this.serialized = Args.concatArrays(
this.serialized,
ser.i128ToBytes(bigInt),
);
this.offset += 16;
this.argsList.push({ type: ArgTypes.I128, value: bigInt });
return this;
}
/**
* Adds an unsigned long integer to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addU128(bigInt: bigint): this {
this.serialized = Args.concatArrays(
this.serialized,
ser.u128ToBytes(bigInt),
);
this.offset += 16;
this.argsList.push({ type: ArgTypes.U128, value: bigInt });
return this;
}
/**
* Adds an unsigned long integer to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addU256(bigInt: bigint): this {
this.serialized = Args.concatArrays(
this.serialized,
ser.u256ToBytes(bigInt),
);
this.offset += 32;
this.argsList.push({ type: ArgTypes.U256, value: bigInt });
return this;
}
/**
* Adds a signed integer to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addI32(value: number): this {
this.serialized = Args.concatArrays(this.serialized, ser.i32ToBytes(value));
this.offset += 4;
this.argsList.push({ type: ArgTypes.I32, value: value });
return this;
}
/**
* Adds a signed long integer to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addI64(bigInt: bigint): this {
this.serialized = Args.concatArrays(
this.serialized,
ser.i64ToBytes(bigInt),
);
this.offset += 8;
this.argsList.push({ type: ArgTypes.I64, value: bigInt });
return this;
}
/**
* Adds a floating number to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addF32(value: number): this {
this.serialized = Args.concatArrays(this.serialized, ser.f32ToBytes(value));
this.offset += 4;
this.argsList.push({ type: ArgTypes.F32, value: value });
return this;
}
/**
* Adds a long floating number to the serialized arguments.
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addF64(value: number): this {
this.serialized = Args.concatArrays(this.serialized, ser.f64ToBytes(value));
this.offset += 8;
this.argsList.push({ type: ArgTypes.F64, value: value });
return this;
}
/**
* Adds a byte array integer to the serialized arguments.
*
* @param array - the array to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addUint8Array(array: Uint8Array): this {
this.addU32(array.length);
this.serialized = Args.concatArrays(this.serialized, array);
this.offset += array.length;
// Remove the U32 from the argsList
this.argsList.pop();
this.argsList.push({ type: ArgTypes.UINT8ARRAY, value: array });
return this;
}
/**
* Adds a string to the serialized arguments.
*
* @remarks
* Works only if the argument is an instance of a handled type (String of 4294967295 characters maximum)
*
* @param value - the number to add.
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addString(value: string): this {
const maxSize = 4294967295;
const size = value.length;
if (size > maxSize) {
// eslint-disable-next-line no-console
console.warn('input string is too long, it will be truncated');
value = value.slice(0, maxSize);
}
const serialized = ser.strToBytes(value);
this.addU32(serialized.length);
this.serialized = Args.concatArrays(this.serialized, ser.strToBytes(value));
this.offset += ser.strToBytes(value).length;
// Remove the U32 from the argsList
this.argsList.pop();
this.argsList.push({ type: ArgTypes.STRING, value: value });
return this;
}
/**
* Adds a serializable object to the serialized arguments.
*
* @remarks
* The object must implement the {@link ISerializable} interface
*
* @see {@link ISerializable}
*
* @param value - the object to add
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addSerializable<T>(value: ISerializable<T>): this {
const serializedValue = value.serialize();
this.serialized = Args.concatArrays(this.serialized, serializedValue);
this.offset += serializedValue.length;
this.argsList.push({ type: ArgTypes.SERIALIZABLE, value: value });
return this;
}
/**
* Adds an array of serializable objects to the serialized arguments.
*
* @remarks
* Each object must implement the {@link ISerializable} interface.
* This will perform a deep copy of your objects thanks to the {@link ISerializable.serialize}
* method you define in your class.
*
* @see {@link ISerializable}
*
* @param arg - the argument to add
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
public addSerializableObjectArray<T extends ISerializable<T>>(
arg: T[],
): this {
const content: Uint8Array = ser.serializableObjectsArrayToBytes(arg);
this.addU32(content.length);
this.serialized = Args.concatArrays(this.serialized, content);
this.offset += content.length;
// Remove the U32 from the argsList
this.argsList.pop();
this.argsList.push({
type: ArgTypes.SERIALIZABLE_OBJECT_ARRAY,
value: arg,
});
return this;
}
/**
* Adds an array of objects to the serialized arguments.
*
* @remarks
* If the type of the values of the array is not native type, this will serialize the pointers, which is certainly
* not what you want! You can only serialize properly array of native types or array of `Serializable` object.
*
* @see {@link addSerializableObjectArray}
*
* @param arg - the argument to add
*
* @returns the serialized arguments to be able to chain `add` method calls.
*/
addArray(arg: NativeType[], type: ArrayTypes): this {
const content = ser.arrayToBytes(arg, type);
this.addU32(content.length);
this.serialized = Args.concatArrays(this.serialized, content);
this.offset += content.length;
// Remove the U32 from the argsList
this.argsList.pop();
this.argsList.push({ type: ArgTypes.ARRAY, value: arg });
return this;
}
// Utils
/**
* Internal function to concat to Uint8Array.
*
* @param a - first array to concat
* @param b - second array to concat
*
* @returns the concatenated array
*/
static concatArrays(a: Uint8Array, b: Uint8Array): Uint8Array {
return new Uint8Array([...a, ...b]);
}
/**
* Returns the data of requested size for current offset
*
* @param size - The data size
* @returns the slice of the serialized internal buffer
*/
private getNextData(size: number): Uint8Array {
return this.serialized.slice(this.offset, this.offset + size);
}
}