-
Notifications
You must be signed in to change notification settings - Fork 131
/
Transaction.js
1661 lines (1451 loc) · 53 KB
/
Transaction.js
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
/*-
*
* Hedera JavaScript SDK
*
* Copyright (C) 2020 - 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import Hbar from "../Hbar.js";
import TransactionResponse from "./TransactionResponse.js";
import TransactionId from "./TransactionId.js";
import TransactionHashMap from "./TransactionHashMap.js";
import SignatureMap from "./SignatureMap.js";
import Executable, { ExecutionState } from "../Executable.js";
import Status from "../Status.js";
import Long from "long";
import * as sha384 from "../cryptography/sha384.js";
import * as hex from "../encoding/hex.js";
import * as HashgraphProto from "@hashgraph/proto";
import PrecheckStatusError from "../PrecheckStatusError.js";
import AccountId from "../account/AccountId.js";
import PublicKey from "../PublicKey.js";
import List from "./List.js";
import Timestamp from "../Timestamp.js";
import * as util from "../util.js";
/**
* @typedef {import("bignumber.js").default} BigNumber
*/
/**
* @typedef {import("../schedule/ScheduleCreateTransaction.js").default} ScheduleCreateTransaction
* @typedef {import("../PrivateKey.js").default} PrivateKey
* @typedef {import("../channel/Channel.js").default} Channel
* @typedef {import("../client/Client.js").default<*, *>} Client
* @typedef {import("../Signer.js").Signer} Signer
*/
// 90 days (in seconds)
export const DEFAULT_AUTO_RENEW_PERIOD = Long.fromValue(7776000);
// maximum value of i64 (so there is never a record generated)
export const DEFAULT_RECORD_THRESHOLD = Hbar.fromTinybars(
Long.fromString("9223372036854775807")
);
// 120 seconds
const DEFAULT_TRANSACTION_VALID_DURATION = 120;
export const CHUNK_SIZE = 1024;
/**
* @type {Map<NonNullable<HashgraphProto.proto.TransactionBody["data"]>, (transactions: HashgraphProto.proto.ITransaction[], signedTransactions: HashgraphProto.proto.ISignedTransaction[], transactionIds: TransactionId[], nodeIds: AccountId[], bodies: HashgraphProto.proto.TransactionBody[]) => Transaction>}
*/
export const TRANSACTION_REGISTRY = new Map();
/**
* Base class for all transactions that may be submitted to Hedera.
*
* @abstract
* @augments {Executable<HashgraphProto.proto.ITransaction, HashgraphProto.proto.ITransactionResponse, TransactionResponse>}
*/
export default class Transaction extends Executable {
// A SDK transaction is composed of multiple, raw protobuf transactions.
// These should be functionally identical, with the exception of pointing to
// different nodes.
// When retrying a transaction after a network error or retry-able
// status response, we try a different transaction and thus a different node.
constructor() {
super();
/**
* List of proto transactions that have been built from this SDK
* transaction.
*
* This is a 2-D array built into one, meaning to
* get to the next row you'd index into this array `row * rowLength + column`
* where `rowLength` is `nodeAccountIds.length`
*
* @internal
* @type {List<HashgraphProto.proto.ITransaction | null>}
*/
this._transactions = new List();
/**
* List of proto transactions that have been built from this SDK
* transaction.
*
* This is a 2-D array built into one, meaning to
* get to the next row you'd index into this array `row * rowLength + column`
* where `rowLength` is `nodeAccountIds.length`
*
* @internal
* @type {List<HashgraphProto.proto.ISignedTransaction>}
*/
this._signedTransactions = new List();
/**
* Set of public keys (as string) who have signed this transaction so
* we do not allow them to sign it again.
*
* @internal
* @type {Set<string>}
*/
this._signerPublicKeys = new Set();
/**
* The transaction valid duration
*
* @private
* @type {number}
*/
this._transactionValidDuration = DEFAULT_TRANSACTION_VALID_DURATION;
/**
* The default max transaction fee for this particular transaction type.
* Most transactions use the default of 2 Hbars, but some requests such
* as `TokenCreateTransaction` need to use a different default value.
*
* @protected
* @type {Hbar}
*/
this._defaultMaxTransactionFee = new Hbar(2);
/**
* The max transaction fee on the request. This field is what users are able
* to set, not the `defaultMaxTransactionFee`. The purpose of this field is
* to allow us to determine if the user set the field explicitly, or if we're
* using the default max transation fee for the request.
*
* @private
* @type {Hbar | null}
*/
this._maxTransactionFee = null;
/**
* The transaction's memo
*
* @private
* @type {string}
*/
this._transactionMemo = "";
/**
* The list of transaction IDs. This list will almost always be of length 1.
* The only time this list will be a different length is for chunked transactions.
* The only two chunked transactions supported right now are `FileAppendTransaction`
* and `TopicMessageSubmitTransaction`
*
* @protected
* @type {List<TransactionId>}
*/
this._transactionIds = new List();
/**
* A list of public keys that will be added to the requests signatures
*
* @private
* @type {PublicKey[]}
*/
this._publicKeys = [];
/**
* The list of signing function 1-1 with `_publicKeys` which sign the request.
* The reason this list allows `null` is because if we go from bytes into
* a transaction, then we know the public key, but we don't have the signing function.
*
* @private
* @type {(((message: Uint8Array) => Promise<Uint8Array>) | null)[]}
*/
this._transactionSigners = [];
/**
* Determine if we should regenerate transaction IDs when we receive `TRANSACITON_EXPIRED`
*
* @private
* @type {?boolean}
*/
this._regenerateTransactionId = null;
}
/**
* Deserialize a transaction from bytes. The bytes can either be a `proto.Transaction` or
* `proto.TransactionList`.
*
* @param {Uint8Array} bytes
* @returns {Transaction}
*/
static fromBytes(bytes) {
const signedTransactions = [];
const transactionIds = [];
const nodeIds = [];
/** @type {string[]} */
const transactionIdStrings = [];
/** @type {string[]} */
const nodeIdStrings = [];
const bodies = [];
const list =
HashgraphProto.proto.TransactionList.decode(bytes).transactionList;
// If the list is of length 0, then teh bytes provided were not a
// `proto.TransactionList`
//
// FIXME: We should also check to make sure the bytes length is greater than
// 0 otherwise this check is wrong?
if (list.length === 0) {
const transaction = HashgraphProto.proto.Transaction.decode(bytes);
// We support `Transaction.signedTransactionBytes` and
// `Transaction.bodyBytes` + `Transaction.sigMap`. If the bytes represent the
// latter, convert them into `signedTransactionBytes`
if (transaction.signedTransactionBytes.length !== 0) {
list.push(transaction);
} else {
list.push({
signedTransactionBytes:
HashgraphProto.proto.SignedTransaction.encode({
bodyBytes: transaction.bodyBytes,
sigMap: transaction.sigMap,
}).finish(),
});
}
}
// This loop is responsible for fill out the `signedTransactions`, `transactionIds`,
// `nodeIds`, and `bodies` variables.
for (const transaction of list) {
// The `signedTransactionBytes` should not be null
if (transaction.signedTransactionBytes == null) {
throw new Error("Transaction.signedTransactionBytes are null");
}
// Decode a signed transaction
const signedTransaction =
HashgraphProto.proto.SignedTransaction.decode(
transaction.signedTransactionBytes
);
signedTransactions.push(signedTransaction);
// Decode a transaction body
const body = HashgraphProto.proto.TransactionBody.decode(
signedTransaction.bodyBytes
);
// Make sure the body is set
if (body.data == null) {
throw new Error("(BUG) body.data was not set in the protobuf");
}
bodies.push(body);
// Make sure the transaction ID within the body is set
if (body.transactionID != null) {
const transactionId = TransactionId._fromProtobuf(
/** @type {HashgraphProto.proto.ITransactionID} */ (
body.transactionID
)
);
// If we haven't already seen this transaction ID in the list, add it
if (!transactionIdStrings.includes(transactionId.toString())) {
transactionIds.push(transactionId);
transactionIdStrings.push(transactionId.toString());
}
}
// Make sure the node account ID within the body is set
if (body.nodeAccountID != null) {
const nodeAccountId = AccountId._fromProtobuf(
/** @type {HashgraphProto.proto.IAccountID} */ (
body.nodeAccountID
)
);
// If we haven't already seen this node account ID in the list, add it
if (!nodeIdStrings.includes(nodeAccountId.toString())) {
nodeIds.push(nodeAccountId);
nodeIdStrings.push(nodeAccountId.toString());
}
}
}
// FIXME: We should have a length check before we access `0` since that would error
const body = bodies[0];
// We should have at least more than one body
if (body == null || body.data == null) {
throw new Error(
"No transaction found in bytes or failed to decode TransactionBody"
);
}
// Use the registry to call the right transaction's `fromProtobuf` method based
// on the `body.data` string
const fromProtobuf = TRANSACTION_REGISTRY.get(body.data); //NOSONAR
// If we forgot to update the registry we should error
if (fromProtobuf == null) {
throw new Error(
`(BUG) Transaction.fromBytes() not implemented for type ${body.data}`
);
}
// That the specific transaction type from protobuf implementation and pass in all the
// information we've gathered.
return fromProtobuf(
list,
signedTransactions,
transactionIds,
nodeIds,
bodies
);
}
/**
* Convert this transaction a `ScheduleCreateTransaction`
*
* @returns {ScheduleCreateTransaction}
*/
schedule() {
this._requireNotFrozen();
if (SCHEDULE_CREATE_TRANSACTION.length != 1) {
throw new Error(
"ScheduleCreateTransaction has not been loaded yet"
);
}
return SCHEDULE_CREATE_TRANSACTION[0]()._setScheduledTransaction(this);
}
/**
* This method is called by each `*Transaction._fromProtobuf()` method. It does
* all the finalization before the user gets hold of a complete `Transaction`
*
* @template {Transaction} TransactionT
* @param {TransactionT} transaction
* @param {HashgraphProto.proto.ITransaction[]} transactions
* @param {HashgraphProto.proto.ISignedTransaction[]} signedTransactions
* @param {TransactionId[]} transactionIds
* @param {AccountId[]} nodeIds
* @param {HashgraphProto.proto.ITransactionBody[]} bodies
* @returns {TransactionT}
*/
static _fromProtobufTransactions(
transaction,
transactions,
signedTransactions,
transactionIds,
nodeIds,
bodies
) {
const body = bodies[0];
// "row" of the 2-D `bodies` array has all the same contents except for `nodeAccountID`
for (let i = 0; i < transactionIds.length; i++) {
for (let j = 0; j < nodeIds.length - 1; j++) {
if (
!util.compare(
bodies[i * nodeIds.length + j],
bodies[i * nodeIds.length + j + 1],
// eslint-disable-next-line ie11/no-collection-args
new Set(["nodeAccountID"])
)
) {
throw new Error("failed to validate transaction bodies");
}
}
}
// Remove node account IDs of 0
// _IIRC_ this was initial due to some funny behavior with `ScheduleCreateTransaction`
// We may be able to remove this.
const zero = new AccountId(0);
for (let i = 0; i < nodeIds.length; i++) {
if (nodeIds[i].equals(zero)) {
nodeIds.splice(i--, 1);
}
}
// Set the transactions accordingly, but don't lock the list because transactions can
// be regenerated if more signatures are added
transaction._transactions.setList(transactions);
// Set the signed transactions accordingly, and lock the list since signed transaction
// will not be regenerated. Although, they can be manipulated if for instance more
// signatures are added
transaction._signedTransactions.setList(signedTransactions).setLocked();
// Set the transaction IDs accordingly, and lock the list. Transaction IDs should not
// be regenerated if we're deserializing a request from bytes
transaction._transactionIds.setList(transactionIds).setLocked();
// Set the node account IDs accordingly, and lock the list. Node account IDs should
// never be changed if we're deserializing a request from bytes
transaction._nodeAccountIds.setList(nodeIds).setLocked();
// Make sure to update the rest of the fields
transaction._transactionValidDuration =
body.transactionValidDuration != null &&
body.transactionValidDuration.seconds != null
? Long.fromValue(body.transactionValidDuration.seconds).toInt()
: DEFAULT_TRANSACTION_VALID_DURATION;
transaction._maxTransactionFee =
body.transactionFee != null
? Hbar.fromTinybars(body.transactionFee)
: new Hbar(0);
transaction._transactionMemo = body.memo != null ? body.memo : "";
// Loop over a single row of `signedTransactions` and add all the public
// keys to the `signerPublicKeys` set, and `publicKeys` list with
// `null` in the `transactionSigners` at the same index.
for (let i = 0; i < nodeIds.length; i++) {
const signedTransaction = signedTransactions[i];
if (
signedTransaction.sigMap != null &&
signedTransaction.sigMap.sigPair != null
) {
for (const sigPair of signedTransaction.sigMap.sigPair) {
transaction._signerPublicKeys.add(
hex.encode(
/** @type {Uint8Array} */ (sigPair.pubKeyPrefix)
)
);
transaction._publicKeys.push(
PublicKey.fromBytes(
/** @type {Uint8Array} */ (sigPair.pubKeyPrefix)
)
);
transaction._transactionSigners.push(null);
}
}
}
return transaction;
}
/**
* Set the node account IDs
*
* @override
* @param {AccountId[]} nodeIds
* @returns {this}
*/
setNodeAccountIds(nodeIds) {
// The reason we overwrite this method is simply because we need to call `requireNotFrozen()`
// Now that I think of it, we could just add an abstract method `setterPrerequiest()` which
// by default does nothing, and `Executable` can call. Then we'd only need to overwrite that
// method once.
this._requireNotFrozen();
super.setNodeAccountIds(nodeIds);
return this;
}
/**
* Get the transaction valid duration
*
* @returns {number}
*/
get transactionValidDuration() {
return this._transactionValidDuration;
}
/**
* Sets the duration (in seconds) that this transaction is valid for.
*
* This is defaulted to 120 seconds (from the time its executed).
*
* @param {number} validDuration
* @returns {this}
*/
setTransactionValidDuration(validDuration) {
this._requireNotFrozen();
this._transactionValidDuration = validDuration;
return this;
}
/**
* Get the max transaction fee
*
* @returns {?Hbar}
*/
get maxTransactionFee() {
return this._maxTransactionFee;
}
/**
* Set the maximum transaction fee the operator (paying account)
* is willing to pay.
*
* @param {number | string | Long | BigNumber | Hbar} maxTransactionFee
* @returns {this}
*/
setMaxTransactionFee(maxTransactionFee) {
this._requireNotFrozen();
this._maxTransactionFee =
maxTransactionFee instanceof Hbar
? maxTransactionFee
: new Hbar(maxTransactionFee);
return this;
}
/**
* Is transaction ID regeneration enabled
*
* @returns {?boolean}
*/
get regenerateTransactionId() {
return this._regenerateTransactionId;
}
/**
* Set the maximum transaction fee the operator (paying account)
* is willing to pay.
*
* @param {boolean} regenerateTransactionId
* @returns {this}
*/
setRegenerateTransactionId(regenerateTransactionId) {
this._requireNotFrozen();
this._regenerateTransactionId = regenerateTransactionId;
return this;
}
/**
* Get the transaction memo
*
* @returns {string}
*/
get transactionMemo() {
return this._transactionMemo;
}
/**
* Set a note or description to be recorded in the transaction
* record (maximum length of 100 bytes).
*
* @param {string} transactionMemo
* @returns {this}
*/
setTransactionMemo(transactionMemo) {
this._requireNotFrozen();
this._transactionMemo = transactionMemo;
return this;
}
/**
* Get the curent transaction ID
*
* @returns {?TransactionId}
*/
get transactionId() {
if (this._transactionIds.isEmpty) {
return null;
}
// If a user calls `.transactionId` that means we need to use that transaction ID
// and **not** regenerate it. To do this, we simply lock the transaction ID list.
//
// This may be a little conffusing since a user can enable transaction ID regenration
// explicity, but if they call `.transactionId` then we will not regenerate transaction
// IDs.
this._transactionIds.setLocked();
return this._transactionIds.current;
}
/**
* Set the ID for this transaction.
*
* The transaction ID includes the operator's account ( the account paying the transaction
* fee). If two transactions have the same transaction ID, they won't both have an effect. One
* will complete normally and the other will fail with a duplicate transaction status.
*
* Normally, you should not use this method. Just before a transaction is executed, a
* transaction ID will be generated from the operator on the client.
*
* @param {TransactionId} transactionId
* @returns {this}
*/
setTransactionId(transactionId) {
this._requireNotFrozen();
this._transactionIds.setList([transactionId]).setLocked();
return this;
}
/**
* Sign the transaction with the private key
* **NOTE**: This is a thin wrapper around `.signWith()`
*
* @param {PrivateKey} privateKey
* @returns {Promise<this>}
*/
sign(privateKey) {
return this.signWith(privateKey.publicKey, (message) =>
Promise.resolve(privateKey.sign(message))
);
}
/**
* Sign the transaction with the public key and signer function
*
* If sign on demand is enabled no signing will be done immediately, instead
* the private key signing function and public key are saved to be used when
* a user calls an exit condition method (not sure what a better name for this is)
* such as `toBytes[Async]()`, `getTransactionHash[PerNode]()` or `execute()`.
*
* @param {PublicKey} publicKey
* @param {(message: Uint8Array) => Promise<Uint8Array>} transactionSigner
* @returns {Promise<this>}
*/
async signWith(publicKey, transactionSigner) {
// If signing on demand is disabled, we need to make sure
// the request is frozen
if (!this._signOnDemand) {
this._requireFrozen();
}
const publicKeyData = publicKey.toBytesRaw();
// note: this omits the DER prefix on purpose because Hedera doesn't
// support that in the protobuf. this means that we would fail
// to re-inflate [this._signerPublicKeys] during [fromBytes] if we used DER
// prefixes here
const publicKeyHex = hex.encode(publicKeyData);
if (this._signerPublicKeys.has(publicKeyHex)) {
// this public key has already signed this transaction
return this;
}
// If we add a new signer, then we need to re-create all transactions
this._transactions.clear();
// Save the current public key so we don't attempt to sign twice
this._signerPublicKeys.add(publicKeyHex);
// If signing on demand is enabled we will save the public key and signer and return
if (this._signOnDemand) {
this._publicKeys.push(publicKey);
this._transactionSigners.push(transactionSigner);
return this;
}
// If we get here, signing on demand is disabled, this means the transaction
// is frozen and we need to sign all the transactions immediately. If we're
// signing all the transactions immediately, we need to lock the node account IDs
// and transaction IDs.
// Now that I think of it, this code should likely exist in `freezeWith()`?
this._transactionIds.setLocked();
this._nodeAccountIds.setLocked();
// Sign each signed transatcion
for (const signedTransaction of this._signedTransactions.list) {
const bodyBytes = /** @type {Uint8Array} */ (
signedTransaction.bodyBytes
);
const signature = await transactionSigner(bodyBytes);
if (signedTransaction.sigMap == null) {
signedTransaction.sigMap = {};
}
if (signedTransaction.sigMap.sigPair == null) {
signedTransaction.sigMap.sigPair = [];
}
signedTransaction.sigMap.sigPair.push(
publicKey._toProtobufSignature(signature)
);
}
return this;
}
/**
* Sign the transaction with the client operator. This is a thin wrapper
* around `.signWith()`
*
* **NOTE**: If client does not have an operator set, this method will throw
*
* @param {import("../client/Client.js").default<Channel, *>} client
* @returns {Promise<this>}
*/
signWithOperator(client) {
const operator = client._operator;
if (operator == null) {
throw new Error(
"`client` must have an operator to sign with the operator"
);
}
if (!this._isFrozen()) {
this.freezeWith(client);
}
return this.signWith(operator.publicKey, operator.transactionSigner);
}
/**
* Add a signature explicitly
*
* This method requires the transaction to have exactly 1 node account ID set
* since different node account IDs have different byte representations and
* hence the same signature would not work for all transactions that are the same
* except for node account ID being different.
*
* @param {PublicKey} publicKey
* @param {Uint8Array} signature
* @returns {this}
*/
addSignature(publicKey, signature) {
// Require that only one node is set on this transaction
// FIXME: This doesn't consider if we have one node account ID set, but we're
// also a chunked transaction. We should also check transaction IDs is of length 1
this._requireOneNodeAccountId();
// If the transaction isn't frozen, freeze it.
if (!this.isFrozen()) {
this.freeze();
}
const publicKeyData = publicKey.toBytesRaw();
const publicKeyHex = hex.encode(publicKeyData);
if (this._signerPublicKeys.has(publicKeyHex)) {
// this public key has already signed this transaction
return this;
}
// Transactions will have to be regenerated
this._transactions.clear();
// Locking the transaction IDs and node account IDs is necessary for consistency
// between before and after execution
this._transactionIds.setLocked();
this._nodeAccountIds.setLocked();
this._signedTransactions.setLocked();
// Add the signature to the signed transaction list. This is a copy paste
// of `.signWith()`, but it really shouldn't be if `_signedTransactions.list`
// must be a length of one.
// FIXME: Remove unnecessary for loop.
for (const transaction of this._signedTransactions.list) {
if (transaction.sigMap == null) {
transaction.sigMap = {};
}
if (transaction.sigMap.sigPair == null) {
transaction.sigMap.sigPair = [];
}
transaction.sigMap.sigPair.push(
publicKey._toProtobufSignature(signature)
);
}
this._signerPublicKeys.add(publicKeyHex);
this._publicKeys.push(publicKey);
this._transactionSigners.push(null);
return this;
}
/**
* Get the current signatures on the request
*
* **NOTE**: Does NOT support sign on demand
*
* @returns {SignatureMap}
*/
getSignatures() {
// If a user is attempting to get signatures for a transaction, then the
// transaction must be frozen.
this._requireFrozen();
// Sign on demand must be disabled because this is the non-async version and
// signing requires awaiting callbacks.
this._requireNotSignOnDemand();
// Build all the transactions
this._buildAllTransactions();
// Lock transaction IDs, and node account IDs
this._transactionIds.setLocked();
this._nodeAccountIds.setLocked();
// Construct a signature map from this transaction
return SignatureMap._fromTransaction(this);
}
/**
* Get the current signatures on the request
*
* **NOTE**: Supports sign on demand
*
* @returns {Promise<SignatureMap>}
*/
async getSignaturesAsync() {
// If sign on demand is enabled, we don't need to care about being frozen
// since we can just regenerate and resign later if some field of the transaction
// changes.
// Locking the transaction IDs and node account IDs is necessary for consistency
// between before and after execution
this._transactionIds.setLocked();
this._nodeAccountIds.setLocked();
// Build all transactions, and sign them
await this._buildAllTransactionsAsync();
// Lock transaction IDs, and node account IDs
this._transactions.setLocked();
this._signedTransactions.setLocked();
// Construct a signature map from this transaction
return SignatureMap._fromTransaction(this);
}
/**
* Not sure why this is called `setTransactionId()` when it doesn't set anything...
* FIXME: Remove this?
*/
_setTransactionId() {
if (this._operatorAccountId == null && this._transactionIds.isEmpty) {
throw new Error(
"`transactionId` must be set or `client` must be provided with `freezeWith`"
);
}
}
/**
* Set the node account IDs using the client
*
* @param {?import("../client/Client.js").default<Channel, *>} client
*/
_setNodeAccountIds(client) {
if (!this._nodeAccountIds.isEmpty) {
return;
}
if (client == null) {
throw new Error(
"`nodeAccountId` must be set or `client` must be provided with `freezeWith`"
);
}
this._nodeAccountIds.setList(
client._network.getNodeAccountIdsForExecute()
);
}
/**
* Build all the signed transactions from the node account IDs
*
* @private
*/
_buildSignedTransactions() {
if (this._signedTransactions.locked) {
return;
}
this._signedTransactions.setList(
this._nodeAccountIds.list.map((nodeId) =>
this._makeSignedTransaction(nodeId)
)
);
}
/**
* Freeze this transaction from future modification to prepare for
* signing or serialization.
*
* @returns {this}
*/
freeze() {
return this.freezeWith(null);
}
/**
* @param {?AccountId} accountId
*/
_freezeWithAccountId(accountId) {
if (this._operatorAccountId == null) {
this._operatorAccountId = accountId;
}
}
/**
* Freeze this transaction from further modification to prepare for
* signing or serialization.
*
* Will use the `Client`, if available, to generate a default Transaction ID and select 1/3
* nodes to prepare this transaction for.
*
* @param {?import("../client/Client.js").default<Channel, *>} client
* @returns {this}
*/
freezeWith(client) {
// Set sign on demand based on client
this._signOnDemand = client != null ? client.signOnDemand : false;
// Save the operator
this._operator = client != null ? client._operator : null;
this._freezeWithAccountId(
client != null ? client.operatorAccountId : null
);
// Set max transaction fee to either `this._maxTransactionFee`,
// `client._defaultMaxTransactionFee`, or `this._defaultMaxTransactionFee`
// in that priority order depending on if `this._maxTransactionFee` has
// been set or if `client._defaultMaxTransactionFee` has been set.
this._maxTransactionFee =
this._maxTransactionFee == null
? client != null && client.defaultMaxTransactionFee != null
? client.defaultMaxTransactionFee
: this._defaultMaxTransactionFee
: this._maxTransactionFee;
// Determine if transaction ID generation should be enabled.
this._regenerateTransactionId =
client != null && this._regenerateTransactionId == null
? client.defaultRegenerateTransactionId
: this._regenerateTransactionId;
// Set the node account IDs via client
this._setNodeAccountIds(client);
// Make sure a transaction ID or operator is set.
this._setTransactionId();
// If a client was not provided, we need to make sure the transaction ID already set
// validates aginst the client.
if (client != null) {
for (const transactionId of this._transactionIds.list) {
if (transactionId.accountId != null) {
transactionId.accountId.validateChecksum(client);
}
}
}
// Build a list of transaction IDs so that if a user calls `.transactionId` they'll
// get a value, but if they dont' we'll just regenerate transaction IDs during execution
this._buildNewTransactionIdList();
// If sign on demand is disabled we need to build out all the signed transactions
if (!this._signOnDemand) {
this._buildSignedTransactions();
}
return this;
}
/**
* Sign the transaction using a signer
*
* This is part of the signature provider feature
*
* @param {Signer} signer
* @returns {Promise<this>}
*/
async signWithSigner(signer) {
await signer.signTransaction(this);
return this;
}
/**
* Freeze the transaction using a signer
*
* This is part of the signature provider feature.
*
* @param {Signer} signer
* @returns {Promise<this>}
*/