/
index.ts
1779 lines (1414 loc) · 54.4 KB
/
index.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { bech32 } from 'bech32';
import { concat, arrayify, hexlify } from '@ethersproject/bytes';
import { BcsDeserializer, BcsSerializer } from '../bcs';
import { Serializer } from '../serde/serializer';
import { Deserializer } from '../serde/deserializer';
import { Optional, Seq, Tuple, ListTuple, unit, bool, int8, int16, int32, int64, int128, uint8, uint16, uint32, uint64, uint128, float32, float64, char, str, bytes } from '../serde/types';
import { dec2bin, bin2dec, setBit, isSetBit, dec2uint8array, uint8array2dec } from "../../../utils/helper";
const CryptoMaterialError = {
SerializationError: 'Struct to be signed does not serialize correctly',
DeserializationError: 'Key or signature material does not deserialize correctly',
ValidationError: 'Key or signature material deserializes, but is otherwise not valid',
WrongLengthError: 'Key, threshold or signature material does not have the expected size',
CanonicalRepresentationError: 'Part of the signature or key is not canonical resulting to malleability issues',
SmallSubgroupError: 'A curve point (i.e., a public key) lies on a small group',
PointNotOnCurveError: 'A curve point (i.e., a public key) does not satisfy the curve equation',
BitVecError: 'BitVec errors in accountable multi-sig schemes',
}
const MAX_NUM_OF_KEYS = 32
const BITMAP_NUM_OF_BYTES = 4
// The length of the Ed25519PrivateKey
const ED25519_PRIVATE_KEY_LENGTH = 32;
// The length of the Ed25519PublicKey
const ED25519_PUBLIC_KEY_LENGTH = 32;
// The length of the Ed25519Signature
const ED25519_SIGNATURE_LENGTH = 32;
export class AccessPath {
constructor(public field0: AccountAddress, public field1: DataPath) {
}
public serialize(serializer: Serializer): void {
this.field0.serialize(serializer);
this.field1.serialize(serializer);
}
static deserialize(deserializer: Deserializer): AccessPath {
const field0 = AccountAddress.deserialize(deserializer);
const field1 = DataPath.deserialize(deserializer);
return new AccessPath(field0, field1);
}
}
export class AccountAddress {
static LENGTH: uint8 = 16;
constructor(public value: ListTuple<[uint8]>) {
}
public serialize(serializer: Serializer): void {
Helpers.serializeArray16U8Array(this.value, serializer);
}
static deserialize(deserializer: Deserializer): AccountAddress {
const value = Helpers.deserializeArray16U8Array(deserializer);
return new AccountAddress(value);
}
}
export class AccountResource {
constructor(public authentication_key: Seq<uint8>, public withdrawal_capability: Optional<WithdrawCapabilityResource>, public key_rotation_capability: Optional<KeyRotationCapabilityResource>, public withdraw_events: EventHandle, public deposit_events: EventHandle, public accept_token_events: EventHandle, public sequence_number: uint64) {
}
public serialize(serializer: Serializer): void {
Helpers.serializeVectorU8(this.authentication_key, serializer);
Helpers.serializeOptionWithdrawCapabilityResource(this.withdrawal_capability, serializer);
Helpers.serializeOptionKeyRotationCapabilityResource(this.key_rotation_capability, serializer);
this.withdraw_events.serialize(serializer);
this.deposit_events.serialize(serializer);
this.accept_token_events.serialize(serializer);
serializer.serializeU64(this.sequence_number);
}
static deserialize(deserializer: Deserializer): AccountResource {
const authentication_key = Helpers.deserializeVectorU8(deserializer);
const withdrawal_capability = Helpers.deserializeOptionWithdrawCapabilityResource(deserializer);
const key_rotation_capability = Helpers.deserializeOptionKeyRotationCapabilityResource(deserializer);
const withdraw_events = EventHandle.deserialize(deserializer);
const deposit_events = EventHandle.deserialize(deserializer);
const accept_token_events = EventHandle.deserialize(deserializer);
const sequence_number = deserializer.deserializeU64();
return new AccountResource(authentication_key, withdrawal_capability, key_rotation_capability, withdraw_events, deposit_events, accept_token_events, sequence_number);
}
}
export class ArgumentABI {
constructor(public name: str, public type_tag: TypeTag) {
}
public serialize(serializer: Serializer): void {
serializer.serializeStr(this.name);
this.type_tag.serialize(serializer);
}
static deserialize(deserializer: Deserializer): ArgumentABI {
const name = deserializer.deserializeStr();
const type_tag = TypeTag.deserialize(deserializer);
return new ArgumentABI(name, type_tag);
}
}
export class AuthenticationKey {
constructor(public value: bytes) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value);
}
static deserialize(deserializer: Deserializer): AuthenticationKey {
const value = deserializer.deserializeBytes();
return new AuthenticationKey(value);
}
}
export class BlockMetadata {
constructor(public parent_hash: HashValue, public timestamp: uint64, public author: AccountAddress, public author_auth_key: Optional<AuthenticationKey>, public uncles: uint64, public number: uint64, public chain_id: ChainId, public parent_gas_used: uint64) {
}
public serialize(serializer: Serializer): void {
this.parent_hash.serialize(serializer);
serializer.serializeU64(this.timestamp);
this.author.serialize(serializer);
Helpers.serializeOptionAuthenticationKey(this.author_auth_key, serializer);
serializer.serializeU64(this.uncles);
serializer.serializeU64(this.number);
this.chain_id.serialize(serializer);
serializer.serializeU64(this.parent_gas_used);
}
static deserialize(deserializer: Deserializer): BlockMetadata {
const parent_hash = HashValue.deserialize(deserializer);
const timestamp = deserializer.deserializeU64();
const author = AccountAddress.deserialize(deserializer);
const author_auth_key = Helpers.deserializeOptionAuthenticationKey(deserializer);
const uncles = deserializer.deserializeU64();
const number = deserializer.deserializeU64();
const chain_id = ChainId.deserialize(deserializer);
const parent_gas_used = deserializer.deserializeU64();
return new BlockMetadata(parent_hash, timestamp, author, author_auth_key, uncles, number, chain_id, parent_gas_used);
}
}
export class ChainId {
constructor(public id: uint8) {
}
public serialize(serializer: Serializer): void {
serializer.serializeU8(this.id);
}
static deserialize(deserializer: Deserializer): ChainId {
const id = deserializer.deserializeU8();
return new ChainId(id);
}
}
export abstract class ContractEvent {
abstract serialize(serializer: Serializer): void;
static deserialize(deserializer: Deserializer): ContractEvent {
const index = deserializer.deserializeVariantIndex();
switch (index) {
case 0: return ContractEventVariantV0.load(deserializer);
default: throw new Error("Unknown variant index for ContractEvent: " + index);
}
}
}
export class ContractEventVariantV0 extends ContractEvent {
constructor(public value: ContractEventV0) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(0);
this.value.serialize(serializer);
}
static load(deserializer: Deserializer): ContractEventVariantV0 {
const value = ContractEventV0.deserialize(deserializer);
return new ContractEventVariantV0(value);
}
}
export class ContractEventV0 {
constructor(public key: EventKey, public sequence_number: uint64, public type_tag: TypeTag, public event_data: bytes) {
}
public serialize(serializer: Serializer): void {
this.key.serialize(serializer);
serializer.serializeU64(this.sequence_number);
this.type_tag.serialize(serializer);
serializer.serializeBytes(this.event_data);
}
static deserialize(deserializer: Deserializer): ContractEventV0 {
const key = EventKey.deserialize(deserializer);
const sequence_number = deserializer.deserializeU64();
const type_tag = TypeTag.deserialize(deserializer);
const event_data = deserializer.deserializeBytes();
return new ContractEventV0(key, sequence_number, type_tag, event_data);
}
}
export abstract class DataPath {
abstract serialize(serializer: Serializer): void;
static deserialize(deserializer: Deserializer): DataPath {
const index = deserializer.deserializeVariantIndex();
switch (index) {
case 0: return DataPathVariantCode.load(deserializer);
case 1: return DataPathVariantResource.load(deserializer);
default: throw new Error("Unknown variant index for DataPath: " + index);
}
}
}
export class DataPathVariantCode extends DataPath {
constructor(public value: Identifier) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(0);
this.value.serialize(serializer);
}
static load(deserializer: Deserializer): DataPathVariantCode {
const value = Identifier.deserialize(deserializer);
return new DataPathVariantCode(value);
}
}
export class DataPathVariantResource extends DataPath {
constructor(public value: StructTag) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(1);
this.value.serialize(serializer);
}
static load(deserializer: Deserializer): DataPathVariantResource {
const value = StructTag.deserialize(deserializer);
return new DataPathVariantResource(value);
}
}
export abstract class DataType {
abstract serialize(serializer: Serializer): void;
static deserialize(deserializer: Deserializer): DataType {
const index = deserializer.deserializeVariantIndex();
switch (index) {
case 0: return DataTypeVariantCODE.load(deserializer);
case 1: return DataTypeVariantRESOURCE.load(deserializer);
default: throw new Error("Unknown variant index for DataType: " + index);
}
}
}
export class DataTypeVariantCODE extends DataType {
constructor() {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(0);
}
static load(deserializer: Deserializer): DataTypeVariantCODE {
return new DataTypeVariantCODE();
}
}
export class DataTypeVariantRESOURCE extends DataType {
constructor() {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(1);
}
static load(deserializer: Deserializer): DataTypeVariantRESOURCE {
return new DataTypeVariantRESOURCE();
}
}
export class Ed25519PrivateKey {
constructor(public value: bytes) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value);
}
static deserialize(deserializer: Deserializer): Ed25519PrivateKey {
const value = deserializer.deserializeBytes();
return new Ed25519PrivateKey(value);
}
}
export class Ed25519PublicKey {
constructor(public value: bytes) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value);
}
static deserialize(deserializer: Deserializer): Ed25519PublicKey {
const value = deserializer.deserializeBytes();
return new Ed25519PublicKey(value);
}
}
export class Ed25519Signature {
constructor(public value: bytes) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value);
}
static deserialize(deserializer: Deserializer): Ed25519Signature {
const value = deserializer.deserializeBytes();
return new Ed25519Signature(value);
}
}
export class EventHandle {
constructor(public count: uint64, public key: EventKey) {
}
public serialize(serializer: Serializer): void {
serializer.serializeU64(this.count);
this.key.serialize(serializer);
}
static deserialize(deserializer: Deserializer): EventHandle {
const count = deserializer.deserializeU64();
const key = EventKey.deserialize(deserializer);
return new EventHandle(count, key);
}
}
export class EventKey {
constructor(public value: bytes) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value);
}
static deserialize(deserializer: Deserializer): EventKey {
const value = deserializer.deserializeBytes();
return new EventKey(value);
}
}
export class HashValue {
constructor(public value: bytes) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value);
}
static deserialize(deserializer: Deserializer): HashValue {
const value = deserializer.deserializeBytes();
return new HashValue(value);
}
}
export class Identifier {
constructor(public value: str) {
}
public serialize(serializer: Serializer): void {
serializer.serializeStr(this.value);
}
static deserialize(deserializer: Deserializer): Identifier {
const value = deserializer.deserializeStr();
return new Identifier(value);
}
}
export class KeyRotationCapabilityResource {
constructor(public account_address: AccountAddress) {
}
public serialize(serializer: Serializer): void {
this.account_address.serialize(serializer);
}
static deserialize(deserializer: Deserializer): KeyRotationCapabilityResource {
const account_address = AccountAddress.deserialize(deserializer);
return new KeyRotationCapabilityResource(account_address);
}
}
export class Module {
constructor(public code: bytes) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.code);
}
static deserialize(deserializer: Deserializer): Module {
const code = deserializer.deserializeBytes();
return new Module(code);
}
}
export class ModuleId {
constructor(public address: AccountAddress, public name: Identifier) {
}
public serialize(serializer: Serializer): void {
this.address.serialize(serializer);
this.name.serialize(serializer);
}
static deserialize(deserializer: Deserializer): ModuleId {
const address = AccountAddress.deserialize(deserializer);
const name = Identifier.deserialize(deserializer);
return new ModuleId(address, name);
}
}
export class MultiEd25519PrivateKey {
constructor(public value: bytes) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value);
}
static deserialize(deserializer: Deserializer): MultiEd25519PrivateKey {
const value = deserializer.deserializeBytes();
return new MultiEd25519PrivateKey(value);
}
}
export class MultiEd25519PublicKey {
constructor(public public_keys: Seq<Ed25519PublicKey>, public threshold: uint8) {
const num_of_public_keys = public_keys.length;
if (threshold === 0 || num_of_public_keys < threshold) {
throw new Error(CryptoMaterialError.ValidationError)
} else if (num_of_public_keys > MAX_NUM_OF_KEYS) {
throw new Error(CryptoMaterialError.WrongLengthError)
}
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value());
}
static deserialize(deserializer: Deserializer): MultiEd25519PublicKey {
const bytes = deserializer.deserializeBytes()
const public_keys: Seq<Ed25519PublicKey> = [];
const count = (bytes.length - 1) / 32
for (let i = 0; i < count; i++) {
const start = i * 32;
const end = start + 32;
public_keys.push(new Ed25519PublicKey(bytes.slice(start, end)));
}
const threshold = new DataView(bytes.slice(-1).buffer, 0).getUint8(0);;
return new MultiEd25519PublicKey(public_keys, threshold);
}
public value(): Uint8Array {
const arrPub = []
this.public_keys.forEach((pub) => {
arrPub.push(pub.value)
})
const arrThreshold = new Uint8Array(1);
arrThreshold[0] = this.threshold
const bytes = concat([...arrPub, ...arrThreshold])
return bytes;
}
}
export class MultiEd25519Signature {
// 0b00010000000000000000000000000001(268435457), the 3rd and 31st positions are set.
constructor(public signatures: Seq<Ed25519Signature>, public bitmap: uint8) {
}
static build(origin_signatures: [Ed25519Signature, uint8][]): MultiEd25519Signature {
const num_of_sigs = origin_signatures.length;
if (num_of_sigs === 0 || num_of_sigs > MAX_NUM_OF_KEYS) {
throw new Error(CryptoMaterialError.ValidationError)
}
const sorted_signatures = origin_signatures.sort((a, b) => {
return a[1] > b[1] ? 1 : -1
})
const sigs = []
let bitmap = 0b00000000000000000000000000000000
sorted_signatures.forEach((k, v) => {
console.log(k, v)
if (k[1] >= MAX_NUM_OF_KEYS) {
throw new Error(`${ CryptoMaterialError.BitVecError }: Signature index is out of range`)
} else if (isSetBit(bitmap, k[1])) {
throw new Error(`${ CryptoMaterialError.BitVecError }: Duplicate signature index`)
} else {
sigs.push(k[0])
bitmap = setBit(bitmap, k[1])
}
})
return new MultiEd25519Signature(sigs, bitmap);
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.value());
}
static deserialize(deserializer: Deserializer): MultiEd25519Signature {
const bytes = deserializer.deserializeBytes()
const signatures: Seq<Ed25519Signature> = [];
const count = (bytes.length - 4) / 64
for (let i = 0; i < count; i++) {
const start = i * 64;
const end = start + 64;
signatures.push(new Ed25519Signature(bytes.slice(start, end)));
}
const bitmap = uint8array2dec(bytes.slice(-4));
return new MultiEd25519Signature(signatures, bitmap);
}
public value(): Uint8Array {
const arrSignatures = []
this.signatures.forEach((signature) => {
arrSignatures.push(signature.value)
})
const arrBitmap = dec2uint8array(this.bitmap);
const bytes = concat([...arrSignatures, ...arrBitmap])
return bytes;
}
}
export class MultiEd25519SignatureShard {
constructor(public signature: MultiEd25519Signature, public threshold: uint8) {
}
public signatures(): [Ed25519Signature, uint8][] {
const signatures = this.signature.signatures;
const bitmap = this.signature.bitmap;
const result: [Ed25519Signature, uint8][] = []
let bitmap_index = 0
signatures.forEach((v, idx) => {
while (!isSetBit(bitmap, bitmap_index)) {
bitmap_index += 1;
}
result.push([v, bitmap_index])
bitmap_index += 1
})
return result
}
static merge(shards: Seq<MultiEd25519SignatureShard>): MultiEd25519SignatureShard {
if (shards.length === 0) {
throw new Error('MultiEd25519SignatureShard shards is empty')
}
const threshold = shards[0].threshold
const signatures: [Ed25519Signature, uint8][] = []
shards.forEach((shard) => {
if (shard.threshold !== threshold) {
throw new Error('MultiEd25519SignatureShard shards threshold not same')
}
signatures.push(...shard.signatures())
})
return new MultiEd25519SignatureShard(MultiEd25519Signature.build(signatures), threshold)
}
public is_enough(): boolean {
return this.signature.signatures.length >= this.threshold
}
}
// Part of private keys in the multi-key Ed25519 structure along with the threshold.
// note: the private keys must be a sequential part of the MultiEd25519PrivateKey
export class MultiEd25519KeyShard {
constructor(public public_keys: Seq<Ed25519PublicKey>, public threshold: uint8, public private_keys: Record<uint8, Ed25519PrivateKey>) {
const num_of_public_keys = public_keys.length;
const num_of_private_keys = Object.keys(private_keys).length;
if (threshold === 0 || num_of_private_keys === 0 || num_of_public_keys < threshold) {
throw new Error(CryptoMaterialError.ValidationError)
} else if (num_of_private_keys > MAX_NUM_OF_KEYS || num_of_public_keys > MAX_NUM_OF_KEYS) {
throw new Error(CryptoMaterialError.WrongLengthError)
}
}
public serialize(serializer: Serializer): void {
serializer.serializeU8(this.public_keys.length)
serializer.serializeU8(this.threshold)
serializer.serializeU8(this.len())
this.public_keys.forEach((pub) => {
pub.serialize(serializer)
})
Object.keys(this.private_keys).forEach((pos) => {
serializer.serializeU8(Number.parseInt(pos, 10))
this.private_keys[pos].serialize(serializer)
})
}
static deserialize(deserializer: Deserializer): MultiEd25519KeyShard {
const publicKeysLen = deserializer.deserializeU8();
const threshold = deserializer.deserializeU8();
const privateKeysLen = deserializer.deserializeU8();
const public_keys: Seq<Ed25519PublicKey> = [];
for (let i = 0; i < publicKeysLen; i++) {
public_keys.push(Ed25519PublicKey.deserialize(deserializer));
}
const private_keys: Record<uint8, Ed25519PrivateKey> = [];
for (let i = 0; i < privateKeysLen; i++) {
const pos = deserializer.deserializeU8()
const privateKey = Ed25519PrivateKey.deserialize(deserializer)
public_keys[pos] = privateKey
}
return new MultiEd25519KeyShard(public_keys, threshold, private_keys);
}
public publicKey(): MultiEd25519PublicKey {
return new MultiEd25519PublicKey(this.public_keys, this.threshold);
}
// should be different for each account, since the private_keys are not the same
public privateKeys(): Ed25519PrivateKey[] {
return Object.values(this.private_keys);
}
// should be different for each account, since the private_keys are not the same
public privateKey(): Uint8Array {
const arrHead = new Uint8Array(3);
arrHead[0] = this.public_keys.length
arrHead[1] = this.threshold
arrHead[2] = this.len()
const arrPub = []
this.public_keys.forEach((pub) => {
arrPub.push(pub.value)
})
const arrPriv = []
Object.values(this.private_keys).forEach((priv) => {
arrPriv.push(priv.value)
})
const bytes = concat([arrHead, ...arrPub, ...arrPriv])
return bytes;
}
public len(): uint8 {
return Object.values(this.private_keys).length;
}
public isEmpty(): boolean {
return this.len() === 0;
}
}
export class Package {
constructor(public package_address: AccountAddress, public modules: Seq<Module>, public init_script: Optional<ScriptFunction>) {
}
public serialize(serializer: Serializer): void {
this.package_address.serialize(serializer);
Helpers.serializeVectorModule(this.modules, serializer);
Helpers.serializeOptionScriptFunction(this.init_script, serializer);
}
static deserialize(deserializer: Deserializer): Package {
const package_address = AccountAddress.deserialize(deserializer);
const modules = Helpers.deserializeVectorModule(deserializer);
const init_script = Helpers.deserializeOptionScriptFunction(deserializer);
return new Package(package_address, modules, init_script);
}
}
export class RawUserTransaction {
constructor(public sender: AccountAddress, public sequence_number: uint64, public payload: TransactionPayload, public max_gas_amount: uint64, public gas_unit_price: uint64, public gas_token_code: str, public expiration_timestamp_secs: uint64, public chain_id: ChainId) {
}
public serialize(serializer: Serializer): void {
this.sender.serialize(serializer);
serializer.serializeU64(this.sequence_number);
this.payload.serialize(serializer);
serializer.serializeU64(this.max_gas_amount);
serializer.serializeU64(this.gas_unit_price);
serializer.serializeStr(this.gas_token_code);
serializer.serializeU64(this.expiration_timestamp_secs);
this.chain_id.serialize(serializer);
}
static deserialize(deserializer: Deserializer): RawUserTransaction {
const sender = AccountAddress.deserialize(deserializer);
const sequence_number = deserializer.deserializeU64();
const payload = TransactionPayload.deserialize(deserializer);
const max_gas_amount = deserializer.deserializeU64();
const gas_unit_price = deserializer.deserializeU64();
const gas_token_code = deserializer.deserializeStr();
const expiration_timestamp_secs = deserializer.deserializeU64();
const chain_id = ChainId.deserialize(deserializer);
return new RawUserTransaction(sender, sequence_number, payload, max_gas_amount, gas_unit_price, gas_token_code, expiration_timestamp_secs, chain_id);
}
}
export class Script {
constructor(public code: bytes, public ty_args: Seq<TypeTag>, public args: Seq<bytes>) {
}
public serialize(serializer: Serializer): void {
serializer.serializeBytes(this.code);
Helpers.serializeVectorTypeTag(this.ty_args, serializer);
Helpers.serializeVectorBytes(this.args, serializer);
}
static deserialize(deserializer: Deserializer): Script {
const code = deserializer.deserializeBytes();
const ty_args = Helpers.deserializeVectorTypeTag(deserializer);
const args = Helpers.deserializeVectorBytes(deserializer);
return new Script(code, ty_args, args);
}
}
export abstract class ScriptABI {
abstract serialize(serializer: Serializer): void;
static deserialize(deserializer: Deserializer): ScriptABI {
const index = deserializer.deserializeVariantIndex();
switch (index) {
case 0: return ScriptABIVariantTransactionScript.load(deserializer);
case 1: return ScriptABIVariantScriptFunction.load(deserializer);
default: throw new Error("Unknown variant index for ScriptABI: " + index);
}
}
}
export class ScriptABIVariantTransactionScript extends ScriptABI {
constructor(public value: TransactionScriptABI) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(0);
this.value.serialize(serializer);
}
static load(deserializer: Deserializer): ScriptABIVariantTransactionScript {
const value = TransactionScriptABI.deserialize(deserializer);
return new ScriptABIVariantTransactionScript(value);
}
}
export class ScriptABIVariantScriptFunction extends ScriptABI {
constructor(public value: ScriptFunctionABI) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(1);
this.value.serialize(serializer);
}
static load(deserializer: Deserializer): ScriptABIVariantScriptFunction {
const value = ScriptFunctionABI.deserialize(deserializer);
return new ScriptABIVariantScriptFunction(value);
}
}
export class ScriptFunction {
// need to rename `function` to `func` as `function` is a keyword in JS.
constructor(public module: ModuleId, public func: Identifier, public ty_args: Seq<TypeTag>, public args: Seq<bytes>) {
}
public serialize(serializer: Serializer): void {
this.module.serialize(serializer);
this.func.serialize(serializer);
Helpers.serializeVectorTypeTag(this.ty_args, serializer);
Helpers.serializeVectorBytes(this.args, serializer);
}
static deserialize(deserializer: Deserializer): ScriptFunction {
const module = ModuleId.deserialize(deserializer);
const func = Identifier.deserialize(deserializer);
const ty_args = Helpers.deserializeVectorTypeTag(deserializer);
const args = Helpers.deserializeVectorBytes(deserializer);
return new ScriptFunction(module, func, ty_args, args);
}
}
export class ScriptFunctionABI {
constructor(public name: str, public module_name: ModuleId, public doc: str, public ty_args: Seq<TypeArgumentABI>, public args: Seq<ArgumentABI>) {
}
public serialize(serializer: Serializer): void {
serializer.serializeStr(this.name);
this.module_name.serialize(serializer);
serializer.serializeStr(this.doc);
Helpers.serializeVectorTypeArgumentAbi(this.ty_args, serializer);
Helpers.serializeVectorArgumentAbi(this.args, serializer);
}
static deserialize(deserializer: Deserializer): ScriptFunctionABI {
const name = deserializer.deserializeStr();
const module_name = ModuleId.deserialize(deserializer);
const doc = deserializer.deserializeStr();
const ty_args = Helpers.deserializeVectorTypeArgumentAbi(deserializer);
const args = Helpers.deserializeVectorArgumentAbi(deserializer);
return new ScriptFunctionABI(name, module_name, doc, ty_args, args);
}
}
export class SignedUserTransaction {
constructor(public raw_txn: RawUserTransaction, public authenticator: TransactionAuthenticator) {
}
public serialize(serializer: Serializer): void {
this.raw_txn.serialize(serializer);
this.authenticator.serialize(serializer);
}
static deserialize(deserializer: Deserializer): SignedUserTransaction {
const raw_txn = RawUserTransaction.deserialize(deserializer);
const authenticator = TransactionAuthenticator.deserialize(deserializer);
return new SignedUserTransaction(raw_txn, authenticator);
}
static ed25519(raw_txn: RawUserTransaction, public_key: Ed25519PublicKey, signature: Ed25519Signature): SignedUserTransaction {
const authenticator = new TransactionAuthenticatorVariantEd25519(public_key, signature);
return new SignedUserTransaction(raw_txn, authenticator);
}
static multi_ed25519(raw_txn: RawUserTransaction, public_key: MultiEd25519PublicKey, signature: MultiEd25519Signature): SignedUserTransaction {
const authenticator = new TransactionAuthenticatorVariantMultiEd25519(public_key, signature);
return new SignedUserTransaction(raw_txn, authenticator);
}
}
export class StructTag {
constructor(public address: AccountAddress, public module: Identifier, public name: Identifier, public type_params: Seq<TypeTag>) {
}
public serialize(serializer: Serializer): void {
this.address.serialize(serializer);
this.module.serialize(serializer);
this.name.serialize(serializer);
Helpers.serializeVectorTypeTag(this.type_params, serializer);
}
static deserialize(deserializer: Deserializer): StructTag {
const address = AccountAddress.deserialize(deserializer);
const module = Identifier.deserialize(deserializer);
const name = Identifier.deserialize(deserializer);
const type_params = Helpers.deserializeVectorTypeTag(deserializer);
return new StructTag(address, module, name, type_params);
}
}
export abstract class Transaction {
abstract serialize(serializer: Serializer): void;
static deserialize(deserializer: Deserializer): Transaction {
const index = deserializer.deserializeVariantIndex();
switch (index) {
case 0: return TransactionVariantUserTransaction.load(deserializer);
case 1: return TransactionVariantBlockMetadata.load(deserializer);
default: throw new Error("Unknown variant index for Transaction: " + index);
}
}
}
export class TransactionVariantUserTransaction extends Transaction {
constructor(public value: SignedUserTransaction) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(0);
this.value.serialize(serializer);
}
static load(deserializer: Deserializer): TransactionVariantUserTransaction {
const value = SignedUserTransaction.deserialize(deserializer);
return new TransactionVariantUserTransaction(value);
}
}
export class TransactionVariantBlockMetadata extends Transaction {
constructor(public value: BlockMetadata) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(1);
this.value.serialize(serializer);
}
static load(deserializer: Deserializer): TransactionVariantBlockMetadata {
const value = BlockMetadata.deserialize(deserializer);
return new TransactionVariantBlockMetadata(value);
}
}
export abstract class TransactionArgument {
abstract serialize(serializer: Serializer): void;
static deserialize(deserializer: Deserializer): TransactionArgument {
const index = deserializer.deserializeVariantIndex();
switch (index) {
case 0: return TransactionArgumentVariantU8.load(deserializer);
case 1: return TransactionArgumentVariantU64.load(deserializer);
case 2: return TransactionArgumentVariantU128.load(deserializer);
case 3: return TransactionArgumentVariantAddress.load(deserializer);
case 4: return TransactionArgumentVariantU8Vector.load(deserializer);
case 5: return TransactionArgumentVariantBool.load(deserializer);
default: throw new Error("Unknown variant index for TransactionArgument: " + index);
}
}
}
export class TransactionArgumentVariantU8 extends TransactionArgument {
constructor(public value: uint8) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(0);
serializer.serializeU8(this.value);
}
static load(deserializer: Deserializer): TransactionArgumentVariantU8 {
const value = deserializer.deserializeU8();
return new TransactionArgumentVariantU8(value);
}
}
export class TransactionArgumentVariantU64 extends TransactionArgument {
constructor(public value: uint64) {
super();
}
public serialize(serializer: Serializer): void {
serializer.serializeVariantIndex(1);
serializer.serializeU64(this.value);
}
static load(deserializer: Deserializer): TransactionArgumentVariantU64 {