forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclerk.go
1251 lines (1127 loc) · 39.6 KB
/
clerk.go
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
// Copyright (C) 2019-2022 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
generatedV2 "github.com/algorand/go-algorand/daemon/algod/api/server/v2/generated"
v1 "github.com/algorand/go-algorand/daemon/algod/api/spec/v1"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/algorand/go-algorand/data/transactions/verify"
"github.com/algorand/go-algorand/libgoal"
"github.com/algorand/go-algorand/protocol"
"github.com/spf13/cobra"
)
var (
toAddress string
account string
amount uint64
txFilename string
rejectsFilename string
closeToAddress string
noProgramOutput bool
writeSourceMap bool
signProgram bool
programSource string
argB64Strings []string
disassemble bool
verbose bool
progByteFile string
msigParams string
logicSigFile string
timeStamp int64
protoVersion string
rekeyToAddress string
signerAddress string
rawOutput bool
)
func init() {
clerkCmd.AddCommand(sendCmd)
clerkCmd.AddCommand(rawsendCmd)
clerkCmd.AddCommand(inspectCmd)
clerkCmd.AddCommand(signCmd)
clerkCmd.AddCommand(groupCmd)
clerkCmd.AddCommand(splitCmd)
clerkCmd.AddCommand(compileCmd)
clerkCmd.AddCommand(dryrunCmd)
clerkCmd.AddCommand(dryrunRemoteCmd)
// Wallet to be used for the clerk operation
clerkCmd.PersistentFlags().StringVarP(&walletName, "wallet", "w", "", "Set the wallet to be used for the selected operation")
// send flags
sendCmd.Flags().StringVarP(&account, "from", "f", "", "Account address to send the money from (If not specified, uses default account)")
sendCmd.Flags().StringVarP(&toAddress, "to", "t", "", "Address to send to money to (required)")
sendCmd.Flags().Uint64VarP(&amount, "amount", "a", 0, "The amount to be transferred (required), in microAlgos")
sendCmd.Flags().StringVarP(&closeToAddress, "close-to", "c", "", "Close account and send remainder to this address")
sendCmd.Flags().StringVar(&rekeyToAddress, "rekey-to", "", "Rekey account to the given spending key/address. (Future transactions from this account will need to be signed with the new key.)")
sendCmd.Flags().StringVarP(&programSource, "from-program", "F", "", "Program source to use as account logic")
sendCmd.Flags().StringVarP(&progByteFile, "from-program-bytes", "P", "", "Program binary to use as account logic")
sendCmd.Flags().StringSliceVar(&argB64Strings, "argb64", nil, "base64 encoded args to pass to transaction logic")
sendCmd.Flags().StringVarP(&logicSigFile, "logic-sig", "L", "", "LogicSig to apply to transaction")
sendCmd.Flags().StringVar(&msigParams, "msig-params", "", "Multisig preimage parameters - [threshold] [Address 1] [Address 2] ...\nUsed to add the necessary fields in case the account was rekeyed to a multisig account")
sendCmd.MarkFlagRequired("to")
sendCmd.MarkFlagRequired("amount")
// Add common transaction flags
addTxnFlags(sendCmd)
// rawsend flags
rawsendCmd.Flags().StringVarP(&txFilename, "filename", "f", "", "Filename of file containing raw transactions")
rawsendCmd.Flags().StringVarP(&rejectsFilename, "rejects", "r", "", "Filename for writing rejects to (default is txFilename.rej)")
rawsendCmd.Flags().BoolVarP(&noWaitAfterSend, "no-wait", "N", false, "Don't wait for transactions to commit")
rawsendCmd.MarkFlagRequired("filename")
signCmd.Flags().StringVarP(&txFilename, "infile", "i", "", "Partially-signed transaction file to add signature to")
signCmd.Flags().StringVarP(&outFilename, "outfile", "o", "", "Filename for writing the signed transaction")
signCmd.Flags().StringVarP(&signerAddress, "signer", "S", "", "Address of key to sign with, if different from transaction \"from\" address due to rekeying")
signCmd.Flags().StringVarP(&programSource, "program", "p", "", "Program source to use as account logic")
signCmd.Flags().StringVarP(&logicSigFile, "logic-sig", "L", "", "LogicSig to apply to transaction")
signCmd.Flags().StringSliceVar(&argB64Strings, "argb64", nil, "base64 encoded args to pass to transaction logic")
signCmd.Flags().StringVarP(&protoVersion, "proto", "P", "", "consensus protocol version id string")
signCmd.MarkFlagRequired("infile")
signCmd.MarkFlagRequired("outfile")
groupCmd.Flags().StringVarP(&txFilename, "infile", "i", "", "File storing transactions to be grouped")
groupCmd.Flags().StringVarP(&outFilename, "outfile", "o", "", "Filename for writing the grouped transactions")
groupCmd.MarkFlagRequired("infile")
groupCmd.MarkFlagRequired("outfile")
splitCmd.Flags().StringVarP(&txFilename, "infile", "i", "", "File storing transactions to be split")
splitCmd.Flags().StringVarP(&outFilename, "outfile", "o", "", "Base filename for writing the individual transactions; each transaction will be written to filename-N.ext")
splitCmd.MarkFlagRequired("infile")
splitCmd.MarkFlagRequired("outfile")
compileCmd.Flags().BoolVarP(&disassemble, "disassemble", "D", false, "disassemble a compiled program")
compileCmd.Flags().BoolVarP(&noProgramOutput, "no-out", "n", false, "don't write contract program binary")
compileCmd.Flags().BoolVarP(&writeSourceMap, "map", "m", false, "write out source map")
compileCmd.Flags().BoolVarP(&signProgram, "sign", "s", false, "sign program, output is a binary signed LogicSig record")
compileCmd.Flags().StringVarP(&outFilename, "outfile", "o", "", "Filename to write program bytes or signed LogicSig to")
compileCmd.Flags().StringVarP(&account, "account", "a", "", "Account address to sign the program (If not specified, uses default account)")
dryrunCmd.Flags().StringVarP(&txFilename, "txfile", "t", "", "transaction or transaction-group to test")
dryrunCmd.Flags().StringVarP(&protoVersion, "proto", "P", "", "consensus protocol version id string")
dryrunCmd.Flags().BoolVar(&dumpForDryrun, "dryrun-dump", false, "Dump in dryrun format acceptable by dryrun REST api instead of running")
dryrunCmd.Flags().Var(&dumpForDryrunFormat, "dryrun-dump-format", "Dryrun dump format: "+dumpForDryrunFormat.AllowedString())
dryrunCmd.Flags().StringSliceVar(&dumpForDryrunAccts, "dryrun-accounts", nil, "additional accounts to include into dryrun request obj")
dryrunCmd.Flags().StringVarP(&outFilename, "outfile", "o", "", "Filename for writing dryrun state object")
dryrunCmd.MarkFlagRequired("txfile")
dryrunRemoteCmd.Flags().StringVarP(&txFilename, "dryrun-state", "D", "", "dryrun request object to run")
dryrunRemoteCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print more info")
dryrunRemoteCmd.Flags().BoolVarP(&rawOutput, "raw", "r", false, "output raw response from algod")
dryrunRemoteCmd.MarkFlagRequired("dryrun-state")
}
var clerkCmd = &cobra.Command{
Use: "clerk",
Short: "Provides the tools to control transactions ",
Long: `Collection of commands to support the management of transaction information.`,
Args: validateNoPosArgsFn,
Run: func(cmd *cobra.Command, args []string) {
//If no arguments passed, we should fallback to help
cmd.HelpFunc()(cmd, args)
},
}
func waitForCommit(client libgoal.Client, txid string, transactionLastValidRound uint64) (txn v1.Transaction, err error) {
// Get current round information
stat, err := client.Status()
if err != nil {
return v1.Transaction{}, fmt.Errorf(errorRequestFail, err)
}
for {
// Check if we know about the transaction yet
txn, err = client.PendingTransactionInformation(txid)
if err != nil {
return v1.Transaction{}, fmt.Errorf(errorRequestFail, err)
}
if txn.ConfirmedRound > 0 {
reportInfof(infoTxCommitted, txid, txn.ConfirmedRound)
break
}
if txn.PoolError != "" {
return v1.Transaction{}, fmt.Errorf(txPoolError, txid, txn.PoolError)
}
// check if we've already committed to the block number equals to the transaction's last valid round.
// if this is the case, the transaction would not be included in the blockchain, and we can exit right
// here.
if transactionLastValidRound > 0 && stat.LastRound >= transactionLastValidRound {
return v1.Transaction{}, fmt.Errorf(errorTransactionExpired, txid)
}
reportInfof(infoTxPending, txid, stat.LastRound)
// WaitForRound waits until round "stat.LastRound+1" is committed
stat, err = client.WaitForRound(stat.LastRound)
if err != nil {
return v1.Transaction{}, fmt.Errorf(errorRequestFail, err)
}
}
return
}
func createSignedTransaction(client libgoal.Client, signTx bool, dataDir string, walletName string, tx transactions.Transaction, signer basics.Address) (stxn transactions.SignedTxn, err error) {
if signTx {
// Sign the transaction
wh, pw := ensureWalletHandleMaybePassword(dataDir, walletName, true)
if signer.IsZero() {
stxn, err = client.SignTransactionWithWallet(wh, pw, tx)
} else {
stxn, err = client.SignTransactionWithWalletAndSigner(wh, pw, signer.String(), tx)
}
return
}
// Wrap in a transactions.SignedTxn with an empty sig.
// This way protocol.Encode will encode the transaction type
stxn, err = transactions.AssembleSignedTxn(tx, crypto.Signature{}, crypto.MultisigSig{})
if err != nil {
return
}
stxn = populateBlankMultisig(client, dataDir, walletName, stxn)
return
}
func writeSignedTxnsToFile(stxns []transactions.SignedTxn, filename string) error {
var outData []byte
for _, stxn := range stxns {
outData = append(outData, protocol.Encode(&stxn)...)
}
return writeFile(filename, outData, 0600)
}
func writeTxnToFile(client libgoal.Client, signTx bool, dataDir string, walletName string, tx transactions.Transaction, filename string) error {
stxn, err := createSignedTransaction(client, signTx, dataDir, walletName, tx, basics.Address{})
if err != nil {
return err
}
// Write the SignedTxn to the output file
return writeSignedTxnsToFile([]transactions.SignedTxn{stxn}, filename)
}
func getB64Args(args []string) [][]byte {
if len(args) == 0 {
return nil
}
programArgs := make([][]byte, len(args))
for i, argstr := range args {
if argstr == "" {
programArgs[i] = []byte{}
continue
}
var err error
programArgs[i], err = base64.StdEncoding.DecodeString(argstr)
if err != nil {
reportErrorf("arg[%d] decode error: %s", i, err)
}
}
return programArgs
}
func getProgramArgs() [][]byte {
return getB64Args(argB64Strings)
}
func parseNoteField(cmd *cobra.Command) []byte {
if cmd.Flags().Changed("noteb64") {
noteBytes, err := base64.StdEncoding.DecodeString(noteBase64)
if err != nil {
reportErrorf(malformedNote, noteBase64, err)
}
return noteBytes
}
if cmd.Flags().Changed("note") {
return []byte(noteText)
}
// Make sure that back-to-back, similar transactions will have a different txid
noteBytes := make([]byte, 8)
crypto.RandBytes(noteBytes[:])
return noteBytes
}
func parseLease(cmd *cobra.Command) (leaseBytes [32]byte) {
// Parse lease field
if cmd.Flags().Changed("lease") {
leaseBytesRaw, err := base64.StdEncoding.DecodeString(lease)
if err != nil {
reportErrorf(malformedLease, lease, err)
}
if len(leaseBytesRaw) != 32 {
reportErrorf(malformedLease, lease, fmt.Errorf("lease length %d != 32", len(leaseBytesRaw)))
}
copy(leaseBytes[:], leaseBytesRaw)
}
return
}
var sendCmd = &cobra.Command{
Use: "send",
Short: "Send money to an address",
Long: `Send money from one account to another. Note: by default, the money will be withdrawn from the default account. Creates a transaction sending amount tokens from fromAddr to toAddr. If the optional --fee is not provided, the transaction will use the recommended amount. If the optional --firstvalid and --lastvalid are provided, the transaction will only be valid from round firstValid to round lastValid. If broadcast of the transaction is successful, the transaction ID will be returned.`,
Args: validateNoPosArgsFn,
Run: func(cmd *cobra.Command, args []string) {
// -s is invalid without -o
if outFilename == "" && sign {
reportErrorln(soFlagError)
}
// --msig-params is invalid without -o
if outFilename == "" && msigParams != "" {
reportErrorln(noOutputFileError)
}
checkTxValidityPeriodCmdFlags(cmd)
dataDir := ensureSingleDataDir()
accountList := makeAccountsList(dataDir)
var fromAddressResolved string
var program []byte = nil
var programArgs [][]byte = nil
var lsig transactions.LogicSig
var err error
if progByteFile != "" {
if programSource != "" || logicSigFile != "" {
reportErrorln("should use at most one of --from-program/-F or --from-program-bytes/-P --logic-sig/-L")
}
program, err = readFile(progByteFile)
if err != nil {
reportErrorf("%s: %s", progByteFile, err)
}
} else if programSource != "" {
if logicSigFile != "" {
reportErrorln("should use at most one of --from-program/-F or --from-program-bytes/-P --logic-sig/-L")
}
program = assembleFile(programSource, false)
} else if logicSigFile != "" {
lsigFromArgs(&lsig)
}
if program != nil {
ph := logic.HashProgram(program)
pha := basics.Address(ph)
fromAddressResolved = pha.String()
programArgs = getProgramArgs()
} else {
// Check if from was specified, else use default
if account == "" {
account = accountList.getDefaultAccount()
}
// Resolving friendly names
fromAddressResolved = accountList.getAddressByName(account)
}
toAddressResolved := accountList.getAddressByName(toAddress)
// Parse notes and lease fields
noteBytes := parseNoteField(cmd)
leaseBytes := parseLease(cmd)
// If closing an account, resolve that address as well
var closeToAddressResolved string
if closeToAddress != "" {
closeToAddressResolved = accountList.getAddressByName(closeToAddress)
}
// If rekeying, parse that address
// (we don't use accountList.getAddressByName because this address likely doesn't correspond to an account)
var rekeyTo basics.Address
if rekeyToAddress != "" {
var err error
rekeyTo, err = basics.UnmarshalChecksumAddress(rekeyToAddress)
if err != nil {
reportErrorf(err.Error())
}
}
client := ensureFullClient(dataDir)
firstValid, lastValid, err = client.ComputeValidityRounds(firstValid, lastValid, numValidRounds)
if err != nil {
reportErrorf(err.Error())
}
payment, err := client.ConstructPayment(
fromAddressResolved, toAddressResolved, fee, amount, noteBytes, closeToAddressResolved,
leaseBytes, basics.Round(firstValid), basics.Round(lastValid),
)
if err != nil {
reportErrorf(errorConstructingTX, err)
}
if !rekeyTo.IsZero() {
payment.RekeyTo = rekeyTo
}
// ConstructPayment fills in the suggested fee when fee=0. But if the user actually used --fee=0 on the
// commandline, we ought to do what they asked (especially now that zero or low fees make sense in
// combination with other txns that cover the groups's fee.
explicitFee := cmd.Flags().Changed("fee")
if explicitFee {
payment.Fee = basics.MicroAlgos{Raw: fee}
}
var stx transactions.SignedTxn
if lsig.Logic != nil {
params, err := client.SuggestedParams()
if err != nil {
reportErrorf(errorNodeStatus, err)
}
proto := protocol.ConsensusVersion(params.ConsensusVersion)
uncheckedTxn := transactions.SignedTxn{
Txn: payment,
Lsig: lsig,
}
blockHeader := bookkeeping.BlockHeader{
UpgradeState: bookkeeping.UpgradeState{
CurrentProtocol: proto,
},
}
groupCtx, err := verify.PrepareGroupContext([]transactions.SignedTxn{uncheckedTxn}, blockHeader)
if err == nil {
err = verify.LogicSigSanityCheck(&uncheckedTxn, 0, groupCtx)
}
if err != nil {
reportErrorf("%s: txn[0] error %s", outFilename, err)
}
stx = uncheckedTxn
} else if program != nil {
stx = transactions.SignedTxn{
Txn: payment,
Lsig: transactions.LogicSig{
Logic: program,
Args: programArgs,
},
}
} else {
signTx := sign || (outFilename == "")
stx, err = createSignedTransaction(client, signTx, dataDir, walletName, payment, basics.Address{})
if err != nil {
reportErrorf(errorSigningTX, err)
}
}
// Handle the case where the user wants to send to an account that was rekeyed to a multisig account
if msigParams != "" {
// Decode params
params := strings.Split(msigParams, " ")
if len(params) < 3 {
reportErrorf(msigParseError, "Not enough arguments to create the multisig address.\nPlease make sure to specify the threshold and at least 2 addresses\n")
}
threshold, err := strconv.ParseUint(params[0], 10, 8)
if err != nil || threshold < 1 || threshold > 255 {
reportErrorf(msigParseError, "Failed to parse the threshold. Make sure it's a number between 1 and 255")
}
// Convert the addresses into public keys
pks := make([]crypto.PublicKey, len(params[1:]))
for i, addrStr := range params[1:] {
addr, err := basics.UnmarshalChecksumAddress(addrStr)
if err != nil {
reportErrorf(failDecodeAddressError, err)
}
pks[i] = crypto.PublicKey(addr)
}
addr, err := crypto.MultisigAddrGen(1, uint8(threshold), pks)
if err != nil {
reportErrorf(msigParseError, err)
}
// Generate the multisig and assign to the txn
stx.Msig = crypto.MultisigPreimageFromPKs(1, uint8(threshold), pks)
// Append the signer since it's a rekey txn
if basics.Address(addr) == stx.Txn.Sender {
reportWarnln(rekeySenderTargetSameError)
}
stx.AuthAddr = basics.Address(addr)
}
if outFilename == "" {
// Broadcast the tx
txid, err := client.BroadcastTransaction(stx)
if err != nil {
reportErrorf(errorBroadcastingTX, err)
}
// update information from Transaction
fee = stx.Txn.Fee.Raw
// Report tx details to user
reportInfof(infoTxIssued, amount, fromAddressResolved, toAddressResolved, txid, fee)
if !noWaitAfterSend {
_, err = waitForCommit(client, txid, lastValid)
if err != nil {
reportErrorf(err.Error())
}
}
} else {
if dumpForDryrun {
err = writeDryrunReqToFile(client, stx, outFilename)
} else {
err = writeFile(outFilename, protocol.Encode(&stx), 0600)
}
if err != nil {
reportErrorf(err.Error())
}
}
},
}
var rawsendCmd = &cobra.Command{
Use: "rawsend",
Short: "Send raw transactions",
Long: `Send raw transactions. The transactions must be stored in a file, encoded using msgpack as transactions.SignedTxn. Multiple transactions can be concatenated together in a file.`,
Args: validateNoPosArgsFn,
Run: func(cmd *cobra.Command, args []string) {
if rejectsFilename == "" {
rejectsFilename = txFilename + ".rej"
}
data, err := readFile(txFilename)
if err != nil {
reportErrorf(fileReadError, txFilename, err)
}
dec := protocol.NewDecoderBytes(data)
client := ensureAlgodClient(ensureSingleDataDir())
txnIDs := make(map[transactions.Txid]transactions.SignedTxn)
var txns []transactions.SignedTxn
for {
var txn transactions.SignedTxn
err = dec.Decode(&txn)
if err == io.EOF {
break
}
if err != nil {
reportErrorf(txDecodeError, txFilename, err)
}
_, present := txnIDs[txn.ID()]
if present {
reportErrorf(txDupError, txn.ID().String(), txFilename)
}
txnIDs[txn.ID()] = txn
txns = append(txns, txn)
}
txgroups := bookkeeping.SignedTxnsToGroups(txns)
txnErrors := make(map[transactions.Txid]string)
pendingTxns := make(map[transactions.Txid]string)
for _, txgroup := range txgroups {
// Broadcast the transaction
err := client.BroadcastTransactionGroup(txgroup)
if err != nil {
for _, txn := range txgroup {
txnErrors[txn.ID()] = err.Error()
}
reportWarnf(errorBroadcastingTX, err)
continue
}
for _, txn := range txgroup {
txidStr := txn.ID().String()
reportInfof(infoRawTxIssued, txidStr)
pendingTxns[txn.ID()] = txidStr
}
}
if noWaitAfterSend {
return
}
// Get current round information
stat, err := client.Status()
if err != nil {
reportErrorf(errorRequestFail, err)
}
for txid, txidStr := range pendingTxns {
for {
// Check if we know about the transaction yet
txn, err := client.PendingTransactionInformation(txidStr)
if err != nil {
txnErrors[txid] = err.Error()
reportWarnf(errorRequestFail, err)
continue
}
if txn.ConfirmedRound > 0 {
reportInfof(infoTxCommitted, txidStr, txn.ConfirmedRound)
break
}
if txn.PoolError != "" {
txnErrors[txid] = txn.PoolError
reportWarnf(txPoolError, txidStr, txn.PoolError)
continue
}
reportInfof(infoTxPending, txidStr, stat.LastRound)
stat, err = client.WaitForRound(stat.LastRound + 1)
if err != nil {
reportErrorf(errorRequestFail, err)
}
}
}
if len(txnErrors) > 0 {
fmt.Printf("Encountered errors in sending %d transactions:\n", len(txnErrors))
var rejectsData []byte
// Loop over transactions in the same order as the original file,
// to preserve transaction groups.
for _, txn := range txns {
txid := txn.ID()
errmsg, ok := txnErrors[txid]
if !ok {
continue
}
fmt.Printf(" %s: %s\n", txid, errmsg)
rejectsData = append(rejectsData, protocol.Encode(&txn)...)
}
f, err := os.OpenFile(rejectsFilename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
reportErrorf(fileWriteError, rejectsFilename, err.Error())
}
_, err = f.Write(rejectsData)
if err != nil {
reportErrorf(fileWriteError, rejectsFilename, err.Error())
}
f.Close()
fmt.Printf("Rejected transactions written to %s\n", rejectsFilename)
os.Exit(1)
}
},
}
var inspectCmd = &cobra.Command{
Use: "inspect [input file 1] [input file 2]...",
Short: "Print a transaction file",
Long: `Loads a transaction file, attempts to decode the transaction, and displays the decoded information.`,
Run: func(cmd *cobra.Command, args []string) {
for _, txFilename := range args {
data, err := readFile(txFilename)
if err != nil {
reportErrorf(fileReadError, txFilename, err)
}
dec := protocol.NewDecoderBytes(data)
count := 0
for {
var txn transactions.SignedTxn
err = dec.Decode(&txn)
if err == io.EOF {
break
}
if err != nil {
reportErrorf(txDecodeError, txFilename, err)
}
sti, err := inspectTxn(txn)
if err != nil {
reportErrorf(txDecodeError, txFilename, err)
}
fmt.Printf("%s[%d]\n%s\n\n", txFilename, count, string(protocol.EncodeJSON(sti)))
count++
}
}
},
}
func lsigFromArgs(lsig *transactions.LogicSig) {
lsigBytes, err := readFile(logicSigFile)
if err != nil {
reportErrorf("%s: read failed, %s", logicSigFile, err)
}
err = protocol.Decode(lsigBytes, lsig)
if err != nil {
reportErrorf("%s: decode failed, %s", logicSigFile, err)
}
lsig.Args = getProgramArgs()
}
func getProto(versArg string) (protocol.ConsensusVersion, config.ConsensusParams) {
cvers := protocol.ConsensusCurrentVersion
if versArg != "" {
cvers = protocol.ConsensusVersion(versArg)
} else {
dataDir := maybeSingleDataDir()
if dataDir != "" {
client := ensureAlgodClient(dataDir)
params, err := client.SuggestedParams()
if err == nil {
cvers = protocol.ConsensusVersion(params.ConsensusVersion)
}
// else warning message?
}
// else warning message?
}
proto, ok := config.Consensus[cvers]
if !ok {
fmt.Fprintf(os.Stderr, "Invalid consensus version. Possible versions:\n")
for xvers := range config.Consensus {
fmt.Fprintf(os.Stderr, "\t%s\n", xvers)
}
os.Exit(1)
}
return cvers, proto
}
var signCmd = &cobra.Command{
Use: "sign -i [input file] -o [output file]",
Short: "Sign a transaction file",
Long: `Sign the passed transaction file, which may contain one or more transactions. If the infile and the outfile are the same, this overwrites the file with the new, signed data.`,
Args: validateNoPosArgsFn,
Run: func(cmd *cobra.Command, _ []string) {
data, err := readFile(txFilename)
if err != nil {
reportErrorf(fileReadError, txFilename, err)
}
var lsig transactions.LogicSig
var authAddr basics.Address
var client libgoal.Client
var wh []byte
var pw []byte
if programSource != "" {
if logicSigFile != "" {
reportErrorln("goal clerk sign should have at most one of --program/-p or --logic-sig/-L")
}
lsig.Logic = assembleFile(programSource, false)
lsig.Args = getProgramArgs()
} else if logicSigFile != "" {
lsigFromArgs(&lsig)
}
if lsig.Logic == nil {
// sign the usual way
dataDir := ensureSingleDataDir()
client = ensureKmdClient(dataDir)
wh, pw = ensureWalletHandleMaybePassword(dataDir, walletName, true)
} else if signerAddress != "" {
authAddr, err = basics.UnmarshalChecksumAddress(signerAddress)
if err != nil {
reportErrorf("Signer invalid (%s): %v", signerAddress, err)
}
}
var outData []byte
dec := protocol.NewDecoderBytes(data)
// read the entire file and prepare in-memory copy of each signed transaction, with grouping.
txnGroups := make(map[crypto.Digest][]*transactions.SignedTxn)
var groupsOrder []crypto.Digest
txnIndex := make(map[*transactions.SignedTxn]int)
count := 0
for {
uncheckedTxn := new(transactions.SignedTxn)
err = dec.Decode(uncheckedTxn)
if err == io.EOF {
break
}
if err != nil {
reportErrorf(txDecodeError, txFilename, err)
}
group := uncheckedTxn.Txn.Group
if group.IsZero() {
// create a dummy group.
randGroupBytes := crypto.Digest{}
crypto.RandBytes(randGroupBytes[:])
group = randGroupBytes
}
if _, hasGroup := txnGroups[group]; !hasGroup {
// add a new group as needed.
groupsOrder = append(groupsOrder, group)
}
txnGroups[group] = append(txnGroups[group], uncheckedTxn)
txnIndex[uncheckedTxn] = count
count++
}
consensusVersion, _ := getProto(protoVersion)
contextHdr := bookkeeping.BlockHeader{
UpgradeState: bookkeeping.UpgradeState{
CurrentProtocol: consensusVersion,
},
}
for _, group := range groupsOrder {
txnGroup := []transactions.SignedTxn{}
for _, txn := range txnGroups[group] {
if lsig.Logic != nil {
txn.Lsig = lsig
if signerAddress != "" {
txn.AuthAddr = authAddr
}
}
txnGroup = append(txnGroup, *txn)
}
var groupCtx *verify.GroupContext
if lsig.Logic != nil {
groupCtx, err = verify.PrepareGroupContext(txnGroup, contextHdr)
if err != nil {
// this error has to be unsupported protocol
reportErrorf("%s: %v", txFilename, err)
}
}
for i, txn := range txnGroup {
var signedTxn transactions.SignedTxn
if lsig.Logic != nil {
err = verify.LogicSigSanityCheck(&txn, i, groupCtx)
if err != nil {
reportErrorf("%s: txn[%d] error %s", txFilename, txnIndex[txnGroups[group][i]], err)
}
signedTxn = txn
} else {
// sign the usual way
signedTxn, err = client.SignTransactionWithWalletAndSigner(wh, pw, signerAddress, txn.Txn)
if err != nil {
reportErrorf(errorSigningTX, err)
}
}
outData = append(outData, protocol.Encode(&signedTxn)...)
}
}
err = writeFile(outFilename, outData, 0600)
if err != nil {
reportErrorf(fileWriteError, outFilename, err)
}
},
}
var groupCmd = &cobra.Command{
Use: "group",
Short: "Group transactions together",
Long: `Form a transaction group. The input file must contain one or more unsigned transactions that will form a group. The output file will contain the same transactions, in order, with a group flag added to each transaction, which requires that the transactions must be committed together. The group command would retain the logic signature, if present, as the TEAL program could verify the group using a logic signature argument.`,
Args: validateNoPosArgsFn,
Run: func(cmd *cobra.Command, args []string) {
data, err := readFile(txFilename)
if err != nil {
reportErrorf(fileReadError, txFilename, err)
}
dec := protocol.NewDecoderBytes(data)
var stxns []transactions.SignedTxn
var group transactions.TxGroup
transactionIdx := 0
for {
var stxn transactions.SignedTxn
// we decode the file into a SignedTxn since we want to verify the absence of the signature as well as preserve the AuthAddr.
err = dec.Decode(&stxn)
if err == io.EOF {
break
}
if err != nil {
reportErrorf(txDecodeError, txFilename, err)
}
if !stxn.Txn.Group.IsZero() {
reportErrorf("Transaction #%d with ID of %s is already part of a group.", transactionIdx, stxn.ID().String())
}
if (!stxn.Sig.Blank()) || (!stxn.Msig.Blank()) {
reportErrorf("Transaction #%d with ID of %s is already signed", transactionIdx, stxn.ID().String())
}
stxns = append(stxns, stxn)
group.TxGroupHashes = append(group.TxGroupHashes, crypto.Digest(stxn.ID()))
transactionIdx++
}
groupHash := crypto.HashObj(group)
for i := range stxns {
stxns[i].Txn.Group = groupHash
}
err = writeSignedTxnsToFile(stxns, outFilename)
if err != nil {
reportErrorf(fileWriteError, outFilename, err)
}
},
}
var splitCmd = &cobra.Command{
Use: "split",
Short: "Split a file containing many transactions into one transaction per file",
Long: `Split a file containing many transactions. The input file must contain one or more transactions. These transactions will be written to individual files.`,
Args: validateNoPosArgsFn,
Run: func(cmd *cobra.Command, args []string) {
data, err := readFile(txFilename)
if err != nil {
reportErrorf(fileReadError, txFilename, err)
}
dec := protocol.NewDecoderBytes(data)
var txns []transactions.SignedTxn
for {
var txn transactions.SignedTxn
err = dec.Decode(&txn)
if err == io.EOF {
break
}
if err != nil {
reportErrorf(txDecodeError, txFilename, err)
}
txns = append(txns, txn)
}
outExt := filepath.Ext(outFilename)
outBase := outFilename[:len(outFilename)-len(outExt)]
for idx, txn := range txns {
fn := fmt.Sprintf("%s-%d%s", outBase, idx, outExt)
err = writeFile(fn, protocol.Encode(&txn), 0600)
if err != nil {
reportErrorf(fileWriteError, outFilename, err)
}
fmt.Printf("Wrote transaction %d to %s\n", idx, fn)
}
},
}
func mustReadFile(fname string) []byte {
contents, err := readFile(fname)
if err != nil {
reportErrorf("%s: %s", fname, err)
}
return contents
}
func assembleFileImpl(fname string, printWarnings bool) *logic.OpStream {
text, err := readFile(fname)
if err != nil {
reportErrorf("%s: %s", fname, err)
}
ops, err := logic.AssembleString(string(text))
if err != nil {
ops.ReportProblems(fname, os.Stderr)
reportErrorf("%s: %s", fname, err)
}
_, params := getProto(protoVersion)
if ops.HasStatefulOps {
if len(ops.Program) > config.MaxAvailableAppProgramLen {
reportErrorf(tealAppSize, fname, len(ops.Program), config.MaxAvailableAppProgramLen)
}
} else {
if uint64(len(ops.Program)) > params.LogicSigMaxSize {
reportErrorf(tealLogicSigSize, fname, len(ops.Program), params.LogicSigMaxSize)
}
}
if printWarnings && len(ops.Warnings) != 0 {
for _, warning := range ops.Warnings {
reportWarnRawln(warning.Error())
}
plural := "s"
if len(ops.Warnings) == 1 {
plural = ""
}
reportWarnRawf("%d warning%s", len(ops.Warnings), plural)
}
return ops
}
func assembleFile(fname string, printWarnings bool) (program []byte) {
ops := assembleFileImpl(fname, printWarnings)
return ops.Program
}
func assembleFileWithMap(fname string, printWarnings bool) ([]byte, logic.SourceMap) {
ops := assembleFileImpl(fname, printWarnings)
return ops.Program, logic.GetSourceMap([]string{fname}, ops.OffsetToLine)
}
func disassembleFile(fname, outname string) {
program, err := readFile(fname)
if err != nil {
reportErrorf("%s: %s", fname, err)
}
// try parsing it as a msgpack LogicSig
var lsig transactions.LogicSig
err = protocol.Decode(program, &lsig)
extra := ""
if err == nil {
// success, extract program to disassemble
program = lsig.Logic
if lsig.Sig != (crypto.Signature{}) || (!lsig.Msig.Blank()) || len(lsig.Args) > 0 {
nologic := lsig
nologic.Logic = nil