-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathtransaction.ts
1381 lines (1323 loc) · 45.7 KB
/
transaction.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 { Buffer } from 'buffer';
import base32 from 'hi-base32';
import * as address from './encoding/address';
import * as encoding from './encoding/encoding';
import * as nacl from './nacl/naclWrappers';
import * as utils from './utils/utils';
import { translateBoxReferences } from './boxStorage';
import {
OnApplicationComplete,
TransactionParams,
TransactionType,
isTransactionType,
BoxReference,
} from './types/transactions/base';
import AnyTransaction, {
MustHaveSuggestedParams,
MustHaveSuggestedParamsInline,
EncodedTransaction,
EncodedSignedTransaction,
EncodedMultisig,
EncodedLogicSig,
} from './types/transactions';
import { Address } from './types/address';
const ALGORAND_TRANSACTION_LENGTH = 52;
export const ALGORAND_MIN_TX_FEE = 1000; // version v5
const ALGORAND_TRANSACTION_LEASE_LENGTH = 32;
const ALGORAND_MAX_ASSET_DECIMALS = 19;
const NUM_ADDL_BYTES_AFTER_SIGNING = 75; // NUM_ADDL_BYTES_AFTER_SIGNING is the number of bytes added to a txn after signing it
const ALGORAND_TRANSACTION_LEASE_LABEL_LENGTH = 5;
const ALGORAND_TRANSACTION_ADDRESS_LENGTH = 32;
const ALGORAND_TRANSACTION_REKEY_LABEL_LENGTH = 5;
const ASSET_METADATA_HASH_LENGTH = 32;
const KEYREG_VOTE_KEY_LENGTH = 32;
const KEYREG_SELECTION_KEY_LENGTH = 32;
const KEYREG_STATE_PROOF_KEY_LENGTH = 64;
type AnyTransactionWithParams = MustHaveSuggestedParams<AnyTransaction>;
type AnyTransactionWithParamsInline = MustHaveSuggestedParamsInline<AnyTransaction>;
/**
* A modified version of the transaction params. Represents the internal structure that the Transaction class uses
* to store inputted transaction objects.
*/
// Omit allows overwriting properties
interface TransactionStorageStructure
extends Omit<
TransactionParams,
| 'from'
| 'to'
| 'genesisHash'
| 'closeRemainderTo'
| 'voteKey'
| 'selectionKey'
| 'stateProofKey'
| 'assetManager'
| 'assetReserve'
| 'assetFreeze'
| 'assetClawback'
| 'assetRevocationTarget'
| 'freezeAccount'
| 'appAccounts'
| 'suggestedParams'
| 'reKeyTo'
> {
from: string | Address;
to: string | Address;
fee: number;
amount: number | bigint;
firstRound: number;
lastRound: number;
note?: Uint8Array;
genesisID: string;
genesisHash: string | Buffer;
lease?: Uint8Array;
closeRemainderTo?: string | Address;
voteKey: string | Buffer;
selectionKey: string | Buffer;
stateProofKey: string | Buffer;
voteFirst: number;
voteLast: number;
voteKeyDilution: number;
assetIndex: number;
assetTotal: number | bigint;
assetDecimals: number;
assetDefaultFrozen: boolean;
assetManager: string | Address;
assetReserve: string | Address;
assetFreeze: string | Address;
assetClawback: string | Address;
assetUnitName: string;
assetName: string;
assetURL: string;
assetMetadataHash?: string | Uint8Array;
freezeAccount: string | Address;
freezeState: boolean;
assetRevocationTarget?: string | Address;
appIndex: number;
appOnComplete: OnApplicationComplete;
appLocalInts: number;
appLocalByteSlices: number;
appGlobalInts: number;
appGlobalByteSlices: number;
appApprovalProgram: Uint8Array;
appClearProgram: Uint8Array;
appArgs?: Uint8Array[];
appAccounts?: string[] | Address[];
appForeignApps?: number[];
appForeignAssets?: number[];
type?: TransactionType;
flatFee: boolean;
reKeyTo?: string | Address;
nonParticipation?: boolean;
group?: Buffer;
extraPages?: number;
boxes?: BoxReference[];
stateProofType?: number | bigint;
stateProof?: Uint8Array;
stateProofMessage?: Uint8Array;
}
function getKeyregKey(
input: undefined | string | Uint8Array | Buffer,
inputName: string,
length: number
): Buffer | undefined {
if (input == null) {
return undefined;
}
let inputAsBuffer: Buffer | undefined;
if (typeof input === 'string') {
inputAsBuffer = Buffer.from(input, 'base64');
} else if (input.constructor === Uint8Array) {
inputAsBuffer = Buffer.from(input);
} else if (Buffer.isBuffer(input)) {
inputAsBuffer = input;
}
if (inputAsBuffer == null || inputAsBuffer.byteLength !== length) {
throw Error(
`${inputName} must be a ${length} byte Uint8Array or Buffer or base64 string.`
);
}
return inputAsBuffer;
}
/**
* Transaction enables construction of Algorand transactions
* */
export class Transaction implements TransactionStorageStructure {
name = 'Transaction';
tag = Buffer.from('TX');
// Implement transaction params
from: Address;
to: Address;
fee: number;
amount: number | bigint;
firstRound: number;
lastRound: number;
note?: Uint8Array;
genesisID: string;
genesisHash: Buffer;
lease?: Uint8Array;
closeRemainderTo?: Address;
voteKey: Buffer;
selectionKey: Buffer;
stateProofKey: Buffer;
voteFirst: number;
voteLast: number;
voteKeyDilution: number;
assetIndex: number;
assetTotal: number | bigint;
assetDecimals: number;
assetDefaultFrozen: boolean;
assetManager: Address;
assetReserve: Address;
assetFreeze: Address;
assetClawback: Address;
assetUnitName: string;
assetName: string;
assetURL: string;
assetMetadataHash?: Uint8Array;
freezeAccount: Address;
freezeState: boolean;
assetRevocationTarget?: Address;
appIndex: number;
appOnComplete: OnApplicationComplete;
appLocalInts: number;
appLocalByteSlices: number;
appGlobalInts: number;
appGlobalByteSlices: number;
appApprovalProgram: Uint8Array;
appClearProgram: Uint8Array;
appArgs?: Uint8Array[];
appAccounts?: Address[];
appForeignApps?: number[];
appForeignAssets?: number[];
boxes?: BoxReference[];
type?: TransactionType;
flatFee: boolean;
reKeyTo?: Address;
nonParticipation?: boolean;
group?: Buffer;
extraPages?: number;
stateProofType?: number | bigint;
stateProof?: Uint8Array;
stateProofMessage?: Uint8Array;
constructor({ ...transaction }: AnyTransaction) {
// Populate defaults
/* eslint-disable no-param-reassign */
const defaults: Partial<TransactionParams> = {
type: TransactionType.pay,
flatFee: false,
nonParticipation: false,
};
// Default type
if (typeof transaction.type === 'undefined') {
transaction.type = defaults.type;
}
// Default flatFee
if (
typeof (transaction as AnyTransactionWithParamsInline).flatFee ===
'undefined'
) {
(transaction as AnyTransactionWithParamsInline).flatFee =
defaults.flatFee;
}
// Default nonParticipation
if (
transaction.type === TransactionType.keyreg &&
typeof transaction.voteKey !== 'undefined' &&
typeof transaction.nonParticipation === 'undefined'
) {
transaction.nonParticipation = defaults.nonParticipation;
}
/* eslint-enable no-param-reassign */
// Move suggested parameters from its object to inline
if (
(transaction as AnyTransactionWithParams).suggestedParams !== undefined
) {
// Create a temporary reference to the transaction object that has params inline and also as a suggested params object
// - Helpful for moving params from named object to inline
const reference = transaction as AnyTransactionWithParams &
AnyTransactionWithParamsInline;
reference.genesisHash = reference.suggestedParams.genesisHash;
reference.fee = reference.suggestedParams.fee;
if (reference.suggestedParams.flatFee !== undefined)
reference.flatFee = reference.suggestedParams.flatFee;
reference.firstRound = reference.suggestedParams.firstRound;
reference.lastRound = reference.suggestedParams.lastRound;
reference.genesisID = reference.suggestedParams.genesisID;
}
// At this point all suggestedParams have been moved to be inline, so we can reassign the transaction object type
// to one which is more useful as we prepare properties for storing
const txn = transaction as TransactionStorageStructure;
txn.from = address.decodeAddress(txn.from as string);
if (txn.to !== undefined) txn.to = address.decodeAddress(txn.to as string);
if (txn.closeRemainderTo !== undefined)
txn.closeRemainderTo = address.decodeAddress(
txn.closeRemainderTo as string
);
if (txn.assetManager !== undefined)
txn.assetManager = address.decodeAddress(txn.assetManager as string);
if (txn.assetReserve !== undefined)
txn.assetReserve = address.decodeAddress(txn.assetReserve as string);
if (txn.assetFreeze !== undefined)
txn.assetFreeze = address.decodeAddress(txn.assetFreeze as string);
if (txn.assetClawback !== undefined)
txn.assetClawback = address.decodeAddress(txn.assetClawback as string);
if (txn.assetRevocationTarget !== undefined)
txn.assetRevocationTarget = address.decodeAddress(
txn.assetRevocationTarget as string
);
if (txn.freezeAccount !== undefined)
txn.freezeAccount = address.decodeAddress(txn.freezeAccount as string);
if (txn.reKeyTo !== undefined)
txn.reKeyTo = address.decodeAddress(txn.reKeyTo as string);
if (txn.genesisHash === undefined)
throw Error('genesis hash must be specified and in a base64 string.');
txn.genesisHash = Buffer.from(txn.genesisHash as string, 'base64');
if (
txn.amount !== undefined &&
(!(
Number.isSafeInteger(txn.amount) ||
(typeof txn.amount === 'bigint' &&
txn.amount <= BigInt('0xffffffffffffffff'))
) ||
txn.amount < 0)
)
throw Error(
'Amount must be a positive number and smaller than 2^64-1. If the number is larger than 2^53-1, use bigint.'
);
if (!Number.isSafeInteger(txn.fee) || txn.fee < 0)
throw Error('fee must be a positive number and smaller than 2^53-1');
if (!Number.isSafeInteger(txn.firstRound) || txn.firstRound < 0)
throw Error('firstRound must be a positive number');
if (!Number.isSafeInteger(txn.lastRound) || txn.lastRound < 0)
throw Error('lastRound must be a positive number');
if (
txn.extraPages !== undefined &&
(!Number.isInteger(txn.extraPages) ||
txn.extraPages < 0 ||
txn.extraPages > 3)
)
throw Error('extraPages must be an Integer between and including 0 to 3');
if (
txn.assetTotal !== undefined &&
(!(
Number.isSafeInteger(txn.assetTotal) ||
(typeof txn.assetTotal === 'bigint' &&
txn.assetTotal <= BigInt('0xffffffffffffffff'))
) ||
txn.assetTotal < 0)
)
throw Error(
'Total asset issuance must be a positive number and smaller than 2^64-1. If the number is larger than 2^53-1, use bigint.'
);
if (
txn.assetDecimals !== undefined &&
(!Number.isSafeInteger(txn.assetDecimals) ||
txn.assetDecimals < 0 ||
txn.assetDecimals > ALGORAND_MAX_ASSET_DECIMALS)
)
throw Error(
`assetDecimals must be a positive number and smaller than ${ALGORAND_MAX_ASSET_DECIMALS.toString()}`
);
if (
txn.assetIndex !== undefined &&
(!Number.isSafeInteger(txn.assetIndex) || txn.assetIndex < 0)
)
throw Error(
'Asset index must be a positive number and smaller than 2^53-1'
);
if (
txn.appIndex !== undefined &&
(!Number.isSafeInteger(txn.appIndex) || txn.appIndex < 0)
)
throw Error(
'Application index must be a positive number and smaller than 2^53-1'
);
if (
txn.appLocalInts !== undefined &&
(!Number.isSafeInteger(txn.appLocalInts) || txn.appLocalInts < 0)
)
throw Error(
'Application local ints count must be a positive number and smaller than 2^53-1'
);
if (
txn.appLocalByteSlices !== undefined &&
(!Number.isSafeInteger(txn.appLocalByteSlices) ||
txn.appLocalByteSlices < 0)
)
throw Error(
'Application local byte slices count must be a positive number and smaller than 2^53-1'
);
if (
txn.appGlobalInts !== undefined &&
(!Number.isSafeInteger(txn.appGlobalInts) || txn.appGlobalInts < 0)
)
throw Error(
'Application global ints count must be a positive number and smaller than 2^53-1'
);
if (
txn.appGlobalByteSlices !== undefined &&
(!Number.isSafeInteger(txn.appGlobalByteSlices) ||
txn.appGlobalByteSlices < 0)
)
throw Error(
'Application global byte slices count must be a positive number and smaller than 2^53-1'
);
if (txn.appApprovalProgram !== undefined) {
if (txn.appApprovalProgram.constructor !== Uint8Array)
throw Error('appApprovalProgram must be a Uint8Array.');
}
if (txn.appClearProgram !== undefined) {
if (txn.appClearProgram.constructor !== Uint8Array)
throw Error('appClearProgram must be a Uint8Array.');
}
if (txn.appArgs !== undefined) {
if (!Array.isArray(txn.appArgs))
throw Error('appArgs must be an Array of Uint8Array.');
txn.appArgs = txn.appArgs.slice();
txn.appArgs.forEach((arg) => {
if (arg.constructor !== Uint8Array)
throw Error('each element of AppArgs must be a Uint8Array.');
});
} else {
txn.appArgs = [];
}
if (txn.appAccounts !== undefined) {
if (!Array.isArray(txn.appAccounts))
throw Error('appAccounts must be an Array of addresses.');
txn.appAccounts = txn.appAccounts.map((addressAsString) =>
address.decodeAddress(addressAsString)
);
}
if (txn.appForeignApps !== undefined) {
if (!Array.isArray(txn.appForeignApps))
throw Error('appForeignApps must be an Array of integers.');
txn.appForeignApps = txn.appForeignApps.slice();
txn.appForeignApps.forEach((foreignAppIndex) => {
if (!Number.isSafeInteger(foreignAppIndex) || foreignAppIndex < 0)
throw Error(
'each foreign application index must be a positive number and smaller than 2^53-1'
);
});
}
if (txn.appForeignAssets !== undefined) {
if (!Array.isArray(txn.appForeignAssets))
throw Error('appForeignAssets must be an Array of integers.');
txn.appForeignAssets = txn.appForeignAssets.slice();
txn.appForeignAssets.forEach((foreignAssetIndex) => {
if (!Number.isSafeInteger(foreignAssetIndex) || foreignAssetIndex < 0)
throw Error(
'each foreign asset index must be a positive number and smaller than 2^53-1'
);
});
}
if (txn.boxes !== undefined) {
if (!Array.isArray(txn.boxes))
throw Error('boxes must be an Array of BoxReference.');
txn.boxes = txn.boxes.slice();
txn.boxes.forEach((box) => {
if (
!Number.isSafeInteger(box.appIndex) ||
box.name.constructor !== Uint8Array
)
throw Error(
'box app index must be a number and name must be an Uint8Array.'
);
});
}
if (
txn.assetMetadataHash !== undefined &&
txn.assetMetadataHash.length !== 0
) {
if (typeof txn.assetMetadataHash === 'string') {
txn.assetMetadataHash = new Uint8Array(
Buffer.from(txn.assetMetadataHash)
);
}
if (
txn.assetMetadataHash.constructor !== Uint8Array ||
txn.assetMetadataHash.byteLength !== ASSET_METADATA_HASH_LENGTH
) {
throw Error(
`assetMetadataHash must be a ${ASSET_METADATA_HASH_LENGTH} byte Uint8Array or string.`
);
}
if (txn.assetMetadataHash.every((value) => value === 0)) {
// if hash contains all 0s, omit it
txn.assetMetadataHash = undefined;
}
} else {
txn.assetMetadataHash = undefined;
}
if (txn.note !== undefined) {
if (txn.note.constructor !== Uint8Array)
throw Error('note must be a Uint8Array.');
} else {
txn.note = new Uint8Array(0);
}
if (txn.lease !== undefined) {
if (txn.lease.constructor !== Uint8Array)
throw Error('lease must be a Uint8Array.');
if (txn.lease.length !== ALGORAND_TRANSACTION_LEASE_LENGTH)
throw Error(
`lease must be of length ${ALGORAND_TRANSACTION_LEASE_LENGTH.toString()}.`
);
if (txn.lease.every((value) => value === 0)) {
// if lease contains all 0s, omit it
txn.lease = new Uint8Array(0);
}
} else {
txn.lease = new Uint8Array(0);
}
txn.voteKey = getKeyregKey(txn.voteKey, 'voteKey', KEYREG_VOTE_KEY_LENGTH);
txn.selectionKey = getKeyregKey(
txn.selectionKey,
'selectionKey',
KEYREG_SELECTION_KEY_LENGTH
);
txn.stateProofKey = getKeyregKey(
txn.stateProofKey,
'stateProofKey',
KEYREG_STATE_PROOF_KEY_LENGTH
);
// Checking non-participation key registration
if (
txn.nonParticipation &&
(txn.voteKey ||
txn.selectionKey ||
txn.voteFirst ||
txn.stateProofKey ||
txn.voteLast ||
txn.voteKeyDilution)
) {
throw new Error(
'nonParticipation is true but participation params are present.'
);
}
// Checking online key registration
if (
!txn.nonParticipation &&
(txn.voteKey ||
txn.selectionKey ||
txn.stateProofKey ||
txn.voteFirst ||
txn.voteLast ||
txn.voteKeyDilution) &&
!(
txn.voteKey &&
txn.selectionKey &&
txn.voteFirst &&
txn.voteLast &&
txn.voteKeyDilution
)
// stateProofKey not included here for backwards compatibility
) {
throw new Error(
'online key registration missing at least one of the following fields: ' +
'voteKey, selectionKey, voteFirst, voteLast, voteKeyDilution'
);
}
// The last option is an offline key registration where all the fields
// nonParticipation, voteKey, selectionKey, voteFirst, voteLast, voteKeyDilution
// are all undefined/false
// Remove unwanted properties and store transaction on instance
delete ((txn as unknown) as AnyTransactionWithParams).suggestedParams;
Object.assign(this, utils.removeUndefinedProperties(txn));
// Modify Fee
if (!txn.flatFee) {
this.fee *= this.estimateSize();
// If suggested fee too small and will be rejected, set to min tx fee
if (this.fee < ALGORAND_MIN_TX_FEE) {
this.fee = ALGORAND_MIN_TX_FEE;
}
}
// say we are aware of groups
this.group = undefined;
// stpf fields
if (
txn.stateProofType !== undefined &&
(!Number.isSafeInteger(txn.stateProofType) || txn.stateProofType < 0)
)
throw Error(
'State Proof type must be a positive number and smaller than 2^53-1'
);
if (txn.stateProofMessage !== undefined) {
if (txn.stateProofMessage.constructor !== Uint8Array)
throw Error('stateProofMessage must be a Uint8Array.');
} else {
txn.stateProofMessage = new Uint8Array(0);
}
if (txn.stateProof !== undefined) {
if (txn.stateProof.constructor !== Uint8Array)
throw Error('stateProof must be a Uint8Array.');
} else {
txn.stateProof = new Uint8Array(0);
}
}
// eslint-disable-next-line camelcase
get_obj_for_encoding() {
if (this.type === 'pay') {
const txn: EncodedTransaction = {
amt: this.amount,
fee: this.fee,
fv: this.firstRound,
lv: this.lastRound,
note: Buffer.from(this.note),
snd: Buffer.from(this.from.publicKey),
type: 'pay',
gen: this.genesisID,
gh: this.genesisHash,
lx: Buffer.from(this.lease),
grp: this.group,
};
// parse close address
if (
this.closeRemainderTo !== undefined &&
address.encodeAddress(this.closeRemainderTo.publicKey) !==
address.ALGORAND_ZERO_ADDRESS_STRING
) {
txn.close = Buffer.from(this.closeRemainderTo.publicKey);
}
if (this.reKeyTo !== undefined) {
txn.rekey = Buffer.from(this.reKeyTo.publicKey);
}
// allowed zero values
if (this.to !== undefined) txn.rcv = Buffer.from(this.to.publicKey);
if (!txn.note.length) delete txn.note;
if (!txn.amt) delete txn.amt;
if (!txn.fee) delete txn.fee;
if (!txn.fv) delete txn.fv;
if (!txn.gen) delete txn.gen;
if (txn.grp === undefined) delete txn.grp;
if (!txn.lx.length) delete txn.lx;
if (!txn.rekey) delete txn.rekey;
return txn;
}
if (this.type === 'keyreg') {
const txn: EncodedTransaction = {
fee: this.fee,
fv: this.firstRound,
lv: this.lastRound,
note: Buffer.from(this.note),
snd: Buffer.from(this.from.publicKey),
type: this.type,
gen: this.genesisID,
gh: this.genesisHash,
lx: Buffer.from(this.lease),
grp: this.group,
votekey: this.voteKey,
selkey: this.selectionKey,
sprfkey: this.stateProofKey,
votefst: this.voteFirst,
votelst: this.voteLast,
votekd: this.voteKeyDilution,
};
// allowed zero values
if (!txn.note.length) delete txn.note;
if (!txn.lx.length) delete txn.lx;
if (!txn.fee) delete txn.fee;
if (!txn.fv) delete txn.fv;
if (!txn.gen) delete txn.gen;
if (txn.grp === undefined) delete txn.grp;
if (this.reKeyTo !== undefined) {
txn.rekey = Buffer.from(this.reKeyTo.publicKey);
}
if (this.nonParticipation) {
txn.nonpart = true;
}
if (!txn.selkey) delete txn.selkey;
if (!txn.votekey) delete txn.votekey;
if (!txn.sprfkey) delete txn.sprfkey;
if (!txn.votefst) delete txn.votefst;
if (!txn.votelst) delete txn.votelst;
if (!txn.votekd) delete txn.votekd;
return txn;
}
if (this.type === 'acfg') {
// asset creation, or asset reconfigure, or asset destruction
const txn: EncodedTransaction = {
fee: this.fee,
fv: this.firstRound,
lv: this.lastRound,
note: Buffer.from(this.note),
snd: Buffer.from(this.from.publicKey),
type: this.type,
gen: this.genesisID,
gh: this.genesisHash,
lx: Buffer.from(this.lease),
grp: this.group,
caid: this.assetIndex,
apar: {
t: this.assetTotal,
df: this.assetDefaultFrozen,
dc: this.assetDecimals,
},
};
if (this.assetManager !== undefined)
txn.apar.m = Buffer.from(this.assetManager.publicKey);
if (this.assetReserve !== undefined)
txn.apar.r = Buffer.from(this.assetReserve.publicKey);
if (this.assetFreeze !== undefined)
txn.apar.f = Buffer.from(this.assetFreeze.publicKey);
if (this.assetClawback !== undefined)
txn.apar.c = Buffer.from(this.assetClawback.publicKey);
if (this.assetName !== undefined) txn.apar.an = this.assetName;
if (this.assetUnitName !== undefined) txn.apar.un = this.assetUnitName;
if (this.assetURL !== undefined) txn.apar.au = this.assetURL;
if (this.assetMetadataHash !== undefined)
txn.apar.am = Buffer.from(this.assetMetadataHash);
// allowed zero values
if (!txn.note.length) delete txn.note;
if (!txn.lx.length) delete txn.lx;
if (!txn.amt) delete txn.amt;
if (!txn.fee) delete txn.fee;
if (!txn.fv) delete txn.fv;
if (!txn.gen) delete txn.gen;
if (this.reKeyTo !== undefined) {
txn.rekey = Buffer.from(this.reKeyTo.publicKey);
}
if (!txn.caid) delete txn.caid;
if (
!txn.apar.t &&
!txn.apar.un &&
!txn.apar.an &&
!txn.apar.df &&
!txn.apar.m &&
!txn.apar.r &&
!txn.apar.f &&
!txn.apar.c &&
!txn.apar.au &&
!txn.apar.am &&
!txn.apar.dc
) {
delete txn.apar;
} else {
if (!txn.apar.t) delete txn.apar.t;
if (!txn.apar.dc) delete txn.apar.dc;
if (!txn.apar.un) delete txn.apar.un;
if (!txn.apar.an) delete txn.apar.an;
if (!txn.apar.df) delete txn.apar.df;
if (!txn.apar.m) delete txn.apar.m;
if (!txn.apar.r) delete txn.apar.r;
if (!txn.apar.f) delete txn.apar.f;
if (!txn.apar.c) delete txn.apar.c;
if (!txn.apar.au) delete txn.apar.au;
if (!txn.apar.am) delete txn.apar.am;
}
if (txn.grp === undefined) delete txn.grp;
return txn;
}
if (this.type === 'axfer') {
// asset transfer, acceptance, revocation, mint, or burn
const txn: EncodedTransaction = {
aamt: this.amount,
fee: this.fee,
fv: this.firstRound,
lv: this.lastRound,
note: Buffer.from(this.note),
snd: Buffer.from(this.from.publicKey),
arcv: Buffer.from(this.to.publicKey),
type: this.type,
gen: this.genesisID,
gh: this.genesisHash,
lx: Buffer.from(this.lease),
grp: this.group,
xaid: this.assetIndex,
};
if (this.closeRemainderTo !== undefined)
txn.aclose = Buffer.from(this.closeRemainderTo.publicKey);
if (this.assetRevocationTarget !== undefined)
txn.asnd = Buffer.from(this.assetRevocationTarget.publicKey);
// allowed zero values
if (!txn.note.length) delete txn.note;
if (!txn.lx.length) delete txn.lx;
if (!txn.aamt) delete txn.aamt;
if (!txn.amt) delete txn.amt;
if (!txn.fee) delete txn.fee;
if (!txn.fv) delete txn.fv;
if (!txn.gen) delete txn.gen;
if (txn.grp === undefined) delete txn.grp;
if (!txn.aclose) delete txn.aclose;
if (!txn.asnd) delete txn.asnd;
if (!txn.rekey) delete txn.rekey;
if (this.reKeyTo !== undefined) {
txn.rekey = Buffer.from(this.reKeyTo.publicKey);
}
return txn;
}
if (this.type === 'afrz') {
// asset freeze or unfreeze
const txn: EncodedTransaction = {
fee: this.fee,
fv: this.firstRound,
lv: this.lastRound,
note: Buffer.from(this.note),
snd: Buffer.from(this.from.publicKey),
type: this.type,
gen: this.genesisID,
gh: this.genesisHash,
lx: Buffer.from(this.lease),
grp: this.group,
faid: this.assetIndex,
afrz: this.freezeState,
};
if (this.freezeAccount !== undefined)
txn.fadd = Buffer.from(this.freezeAccount.publicKey);
// allowed zero values
if (!txn.note.length) delete txn.note;
if (!txn.lx.length) delete txn.lx;
if (!txn.amt) delete txn.amt;
if (!txn.fee) delete txn.fee;
if (!txn.fv) delete txn.fv;
if (!txn.gen) delete txn.gen;
if (!txn.afrz) delete txn.afrz;
if (txn.grp === undefined) delete txn.grp;
if (this.reKeyTo !== undefined) {
txn.rekey = Buffer.from(this.reKeyTo.publicKey);
}
return txn;
}
if (this.type === 'appl') {
// application call of some kind
const txn: EncodedTransaction = {
fee: this.fee,
fv: this.firstRound,
lv: this.lastRound,
note: Buffer.from(this.note),
snd: Buffer.from(this.from.publicKey),
type: this.type,
gen: this.genesisID,
gh: this.genesisHash,
lx: Buffer.from(this.lease),
grp: this.group,
apid: this.appIndex,
apan: this.appOnComplete,
apls: {
nui: this.appLocalInts,
nbs: this.appLocalByteSlices,
},
apgs: {
nui: this.appGlobalInts,
nbs: this.appGlobalByteSlices,
},
apfa: this.appForeignApps,
apas: this.appForeignAssets,
apep: this.extraPages,
apbx: translateBoxReferences(
this.boxes,
this.appForeignApps,
this.appIndex
),
};
if (this.reKeyTo !== undefined) {
txn.rekey = Buffer.from(this.reKeyTo.publicKey);
}
if (this.appApprovalProgram !== undefined) {
txn.apap = Buffer.from(this.appApprovalProgram);
}
if (this.appClearProgram !== undefined) {
txn.apsu = Buffer.from(this.appClearProgram);
}
if (this.appArgs !== undefined) {
txn.apaa = this.appArgs.map((arg) => Buffer.from(arg));
}
if (this.appAccounts !== undefined) {
txn.apat = this.appAccounts.map((decodedAddress) =>
Buffer.from(decodedAddress.publicKey)
);
}
// allowed zero values
if (!txn.note.length) delete txn.note;
if (!txn.lx.length) delete txn.lx;
if (!txn.amt) delete txn.amt;
if (!txn.fee) delete txn.fee;
if (!txn.fv) delete txn.fv;
if (!txn.gen) delete txn.gen;
if (!txn.apid) delete txn.apid;
if (!txn.apls.nui) delete txn.apls.nui;
if (!txn.apls.nbs) delete txn.apls.nbs;
if (!txn.apls.nui && !txn.apls.nbs) delete txn.apls;
if (!txn.apgs.nui) delete txn.apgs.nui;
if (!txn.apgs.nbs) delete txn.apgs.nbs;
if (!txn.apaa || !txn.apaa.length) delete txn.apaa;
if (!txn.apgs.nui && !txn.apgs.nbs) delete txn.apgs;
if (!txn.apap) delete txn.apap;
if (!txn.apsu) delete txn.apsu;
if (!txn.apan) delete txn.apan;
if (!txn.apfa || !txn.apfa.length) delete txn.apfa;
if (!txn.apas || !txn.apas.length) delete txn.apas;
for (const box of txn.apbx) {
if (!box.i) delete box.i;
if (!box.n || !box.n.length) delete box.n;
}
if (!txn.apbx || !txn.apbx.length) delete txn.apbx;
if (!txn.apat || !txn.apat.length) delete txn.apat;
if (!txn.apep) delete txn.apep;
if (txn.grp === undefined) delete txn.grp;
return txn;
}
if (this.type === 'stpf') {
// state proof txn
const txn: EncodedTransaction = {
fee: this.fee,
fv: this.firstRound,
lv: this.lastRound,
note: Buffer.from(this.note),
snd: Buffer.from(this.from.publicKey),
type: this.type,
gen: this.genesisID,
gh: this.genesisHash,
lx: Buffer.from(this.lease),
sptype: this.stateProofType,
spmsg: Buffer.from(this.stateProofMessage),
sp: Buffer.from(this.stateProof),
};
// allowed zero values
if (!txn.sptype) delete txn.sptype;
if (!txn.note.length) delete txn.note;
if (!txn.lx.length) delete txn.lx;
if (!txn.amt) delete txn.amt;
if (!txn.fee) delete txn.fee;
if (!txn.fv) delete txn.fv;
if (!txn.gen) delete txn.gen;
if (!txn.apid) delete txn.apid;
if (!txn.apaa || !txn.apaa.length) delete txn.apaa;
if (!txn.apap) delete txn.apap;
if (!txn.apsu) delete txn.apsu;
if (!txn.apan) delete txn.apan;
if (!txn.apfa || !txn.apfa.length) delete txn.apfa;
if (!txn.apas || !txn.apas.length) delete txn.apas;
if (!txn.apat || !txn.apat.length) delete txn.apat;
if (!txn.apep) delete txn.apep;
if (txn.grp === undefined) delete txn.grp;
return txn;
}
return undefined;
}
// eslint-disable-next-line camelcase
static from_obj_for_encoding(txnForEnc: EncodedTransaction): Transaction {
const txn = Object.create(this.prototype) as Transaction;
txn.name = 'Transaction';
txn.tag = Buffer.from('TX');
txn.genesisID = txnForEnc.gen;
txn.genesisHash = Buffer.from(txnForEnc.gh);
if (!isTransactionType(txnForEnc.type)) {
throw new Error(`Unrecognized transaction type: ${txnForEnc.type}`);
}
txn.type = txnForEnc.type;
txn.fee = txnForEnc.fee;
txn.firstRound = txnForEnc.fv;
txn.lastRound = txnForEnc.lv;
txn.note = new Uint8Array(txnForEnc.note);
txn.lease = new Uint8Array(txnForEnc.lx);
txn.from = address.decodeAddress(
address.encodeAddress(new Uint8Array(txnForEnc.snd))
);
if (txnForEnc.grp !== undefined) txn.group = Buffer.from(txnForEnc.grp);
if (txnForEnc.rekey !== undefined)
txn.reKeyTo = address.decodeAddress(
address.encodeAddress(new Uint8Array(txnForEnc.rekey))
);
if (txnForEnc.type === 'pay') {
txn.amount = txnForEnc.amt;
txn.to = address.decodeAddress(
address.encodeAddress(new Uint8Array(txnForEnc.rcv))
);
if (txnForEnc.close !== undefined)
txn.closeRemainderTo = address.decodeAddress(
address.encodeAddress(txnForEnc.close)
);
} else if (txnForEnc.type === 'keyreg') {
if (txnForEnc.votekey !== undefined) {
txn.voteKey = Buffer.from(txnForEnc.votekey);
}
if (txnForEnc.selkey !== undefined) {
txn.selectionKey = Buffer.from(txnForEnc.selkey);
}
if (txnForEnc.sprfkey !== undefined) {
txn.stateProofKey = Buffer.from(txnForEnc.sprfkey);
}
if (txnForEnc.votekd !== undefined) {
txn.voteKeyDilution = txnForEnc.votekd;
}
if (txnForEnc.votefst !== undefined) {
txn.voteFirst = txnForEnc.votefst;
}
if (txnForEnc.votelst !== undefined) {
txn.voteLast = txnForEnc.votelst;
}
if (txnForEnc.nonpart !== undefined) {
txn.nonParticipation = txnForEnc.nonpart;
}
} else if (txnForEnc.type === 'acfg') {
// asset creation, or asset reconfigure, or asset destruction
if (txnForEnc.caid !== undefined) {
txn.assetIndex = txnForEnc.caid;
}
if (txnForEnc.apar !== undefined) {
txn.assetTotal = txnForEnc.apar.t;
txn.assetDefaultFrozen = txnForEnc.apar.df;
if (txnForEnc.apar.dc !== undefined)
txn.assetDecimals = txnForEnc.apar.dc;
if (txnForEnc.apar.m !== undefined)
txn.assetManager = address.decodeAddress(
address.encodeAddress(new Uint8Array(txnForEnc.apar.m))
);
if (txnForEnc.apar.r !== undefined)
txn.assetReserve = address.decodeAddress(
address.encodeAddress(new Uint8Array(txnForEnc.apar.r))
);
if (txnForEnc.apar.f !== undefined)