summaryrefslogtreecommitdiff
path: root/libgoal/libgoal.go
blob: b7f8cf519cf52f426b9a3324afdceda44a540f58 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
// Copyright (C) 2019-2021 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 libgoal

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	algodclient "github.com/algorand/go-algorand/daemon/algod/api/client"
	v2 "github.com/algorand/go-algorand/daemon/algod/api/server/v2"
	generatedV2 "github.com/algorand/go-algorand/daemon/algod/api/server/v2/generated"
	kmdclient "github.com/algorand/go-algorand/daemon/kmd/client"
	"github.com/algorand/go-algorand/rpcs"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/daemon/algod/api/server/v2/generated"
	"github.com/algorand/go-algorand/daemon/algod/api/spec/common"
	v1 "github.com/algorand/go-algorand/daemon/algod/api/spec/v1"
	"github.com/algorand/go-algorand/daemon/kmd/lib/kmdapi"
	"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/nodecontrol"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/util"
)

// defaultKMDTimeoutSecs is the default number of seconds after which kmd will
// kill itself if there are no requests. This can be overridden with
// SetKMDStartArgs
const defaultKMDTimeoutSecs = 60

// DefaultKMDDataDir is the name of the directory within the algod data directory where kmd data goes
const DefaultKMDDataDir = nodecontrol.DefaultKMDDataDir

// Client represents the entry point for all libgoal functions
type Client struct {
	nc                   nodecontrol.NodeController
	kmdStartArgs         nodecontrol.KMDStartArgs
	dataDir              string
	cacheDir             string
	consensus            config.ConsensusProtocols
	algodVersionAffinity algodclient.APIVersion
	kmdVersionAffinity   kmdclient.APIVersion
}

// ClientConfig is data to configure a Client
type ClientConfig struct {
	// AlgodDataDir is the data dir for `algod`
	AlgodDataDir string

	// KMDDataDir is the data dir for `kmd`, default ${HOME}/.algorand/kmd
	KMDDataDir string

	// CacheDir is a place to store some stuff
	CacheDir string

	// BinDir may be "" and it will be guesed
	BinDir string
}

// ClientType represents the type of client you need
// It ensures the specified type(s) can be initialized
// when the libgoal client is created.
// Any client type not specified will be initialized on-demand.
type ClientType int

const (
	// DynamicClient creates clients on-demand
	DynamicClient ClientType = iota
	// KmdClient ensures the kmd client can be initialized when created
	KmdClient
	// AlgodClient ensures the algod client can be initialized when created
	AlgodClient
	// FullClient ensures all clients can be initialized when created
	FullClient
)

// MakeClientWithBinDir creates and inits a libgoal.Client, additionally
// allowing the user to specify a binary directory
func MakeClientWithBinDir(binDir, dataDir, cacheDir string, clientType ClientType) (c Client, err error) {
	config := ClientConfig{
		BinDir:       binDir,
		AlgodDataDir: dataDir,
		CacheDir:     cacheDir,
	}
	err = c.init(config, clientType)
	return
}

// MakeClient creates and inits a libgoal.Client
func MakeClient(dataDir, cacheDir string, clientType ClientType) (c Client, err error) {
	binDir, err := util.ExeDir()
	if err != nil {
		return
	}
	config := ClientConfig{
		BinDir:       binDir,
		AlgodDataDir: dataDir,
		CacheDir:     cacheDir,
	}
	err = c.init(config, clientType)
	return
}

// MakeClientFromConfig creates a libgoal.Client from a config struct with many options.
func MakeClientFromConfig(config ClientConfig, clientType ClientType) (c Client, err error) {
	if config.BinDir == "" {
		config.BinDir, err = util.ExeDir()
		if err != nil {
			return
		}
	}
	err = c.init(config, clientType)
	return
}

// Init takes data directory path or an empty string if $ALGORAND_DATA is defined and initializes Client
func (c *Client) init(config ClientConfig, clientType ClientType) error {
	// check and assign dataDir
	dataDir, err := getDataDir(config.AlgodDataDir)
	if err != nil {
		return err
	}
	c.dataDir = dataDir
	c.cacheDir = config.CacheDir
	c.algodVersionAffinity = algodclient.APIVersionV1
	c.kmdVersionAffinity = kmdclient.APIVersionV1

	// Get node controller
	nc, err := getNodeController(config.BinDir, config.AlgodDataDir)
	if err != nil {
		return err
	}
	if config.KMDDataDir != "" {
		nc.SetKMDDataDir(config.KMDDataDir)
	} else {
		algodKmdPath, _ := filepath.Abs(filepath.Join(dataDir, DefaultKMDDataDir))
		nc.SetKMDDataDir(algodKmdPath)
	}
	c.nc = nc

	// Initialize default kmd start args
	c.kmdStartArgs = nodecontrol.KMDStartArgs{
		TimeoutSecs: defaultKMDTimeoutSecs,
	}

	if clientType == KmdClient || clientType == FullClient {
		_, err = c.ensureKmdClient()
		if err != nil {
			return err
		}
	}

	if clientType == AlgodClient || clientType == FullClient {
		_, err = c.ensureAlgodClient()
		if err != nil {
			return err
		}
	}

	c.consensus, err = nc.GetConsensus()
	if err != nil {
		return err
	}

	return nil
}

func (c *Client) ensureKmdClient() (*kmdclient.KMDClient, error) {
	kmd, err := c.getKMDClient()
	if err != nil {
		return nil, err
	}
	return &kmd, nil
}

func (c *Client) ensureAlgodClient() (*algodclient.RestClient, error) {
	algod, err := c.getAlgodClient()
	if err != nil {
		return nil, err
	}
	algod.SetAPIVersionAffinity(c.algodVersionAffinity)
	return &algod, err
}

// DataDir returns the Algorand's client data directory path
func (c *Client) DataDir() string {
	return c.dataDir
}

func getDataDir(dataDir string) (string, error) {
	// Get the target data directory to work against,
	// then handle the scenario where no data directory is provided.

	// Figure out what data directory to tell algod to use.
	// If not specified on cmdline with '-d', look for default in environment.
	dir := dataDir
	if dir == "" {
		dir = os.Getenv("ALGORAND_DATA")
	}
	if dir == "" {
		fmt.Println(errorNoDataDirectory.Error())
		return "", errorNoDataDirectory

	}
	return dir, nil
}

func getNodeController(binDir, dataDir string) (nc nodecontrol.NodeController, err error) {
	dataDir, err = getDataDir(dataDir)
	if err != nil {
		return nodecontrol.NodeController{}, nil
	}

	return nodecontrol.MakeNodeController(binDir, dataDir), nil
}

// SetKMDStartArgs sets the arguments used when starting kmd
func (c *Client) SetKMDStartArgs(args nodecontrol.KMDStartArgs) {
	c.kmdStartArgs = args
}

func (c *Client) getKMDClient() (kmdclient.KMDClient, error) {
	// Will return alreadyRunning = true if kmd already running
	_, err := c.nc.StartKMD(c.kmdStartArgs)
	if err != nil {
		return kmdclient.KMDClient{}, err
	}

	kmdClient, err := c.nc.KMDClient()
	if err != nil {
		return kmdclient.KMDClient{}, err
	}
	return kmdClient, nil
}

func (c *Client) getAlgodClient() (algodclient.RestClient, error) {
	algodClient, err := c.nc.AlgodClient()
	if err != nil {
		return algodclient.RestClient{}, err
	}
	return algodClient, nil
}

func (c *Client) ensureGenesisID() (string, error) {
	genesis, err := c.nc.GetGenesis()
	if err != nil {
		return "", err
	}
	return genesis.ID(), nil
}

// GenesisID fetches the genesis ID for the running algod node
func (c *Client) GenesisID() (string, error) {
	response, err := c.ensureGenesisID()

	if err != nil {
		return "", err
	}
	return response, nil
}

// FullStop stops the clients including graceful shutdown to algod and kmd
func (c *Client) FullStop() error {
	return c.nc.FullStop()
}

func (c *Client) checkHandleValidMaybeRenew(walletHandle []byte) bool {
	// Blank handles are definitely invalid
	if len(walletHandle) == 0 {
		return false
	}
	// Otherwise, check with kmd and possibly renew
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return false
	}
	_, err = kmd.RenewWalletHandle(walletHandle)
	return err == nil
}

// ListAddresses takes a wallet handle and returns the list of addresses associated with it. If no addresses are
// associated with the wallet, it returns an empty list.
func (c *Client) ListAddresses(walletHandle []byte) ([]string, error) {
	las, err := c.ListAddressesWithInfo(walletHandle)
	if err != nil {
		return nil, err
	}

	var addrs []string
	for _, la := range las {
		addrs = append(addrs, la.Addr)
	}

	return addrs, nil
}

// ListAddressesWithInfo takes a wallet handle and returns the list of
// addresses associated with it, along with additional information to
// indicate if an address is multisig or not.  If no addresses are
// associated with the wallet, it returns an empty list.
func (c *Client) ListAddressesWithInfo(walletHandle []byte) ([]ListedAddress, error) {
	// List the keys associated with the walletHandle
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return nil, err
	}
	response, err := kmd.ListKeys(walletHandle)
	if err != nil {
		return nil, err
	}
	// List multisig addresses as well
	response2, err := kmd.ListMultisigAddrs(walletHandle)
	if err != nil {
		return nil, err
	}

	var addresses []ListedAddress
	for _, addr := range response.Addresses {
		addresses = append(addresses, ListedAddress{
			Addr:     addr,
			Multisig: false,
		})
	}

	for _, addr := range response2.Addresses {
		addresses = append(addresses, ListedAddress{
			Addr:     addr,
			Multisig: true,
		})
	}

	return addresses, nil
}

// ListedAddress is an address returned by ListAddresses, with a flag
// to indicate whether it's a multisig address.
type ListedAddress struct {
	Addr     string
	Multisig bool
}

// DeleteAccount deletes an account.
func (c *Client) DeleteAccount(walletHandle []byte, walletPassword []byte, addr string) error {
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return err
	}

	_, err = kmd.DeleteKey(walletHandle, walletPassword, addr)
	return err
}

// GenerateAddress takes a wallet handle, generate an additional address for it and returns the public address
func (c *Client) GenerateAddress(walletHandle []byte) (string, error) {
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return "", err
	}
	resp, err := kmd.GenerateKey(walletHandle)
	if err != nil {
		return "", err
	}

	return resp.Address, nil
}

// CreateMultisigAccount takes a wallet handle, a list of (nonmultisig) addresses, and a threshold and creates (and returns) a multisig address
// TODO: Should these be raw public keys instead of addresses so users can't shoot themselves in the foot by passing in a multisig addr? Probably will become irrelevant after CSID changes.
func (c *Client) CreateMultisigAccount(walletHandle []byte, threshold uint8, addrs []string) (string, error) {
	// convert the addresses into public keys
	pks := make([]crypto.PublicKey, len(addrs))
	for i, addrStr := range addrs {
		addr, err := basics.UnmarshalChecksumAddress(addrStr)
		if err != nil {
			return "", err
		}
		pks[i] = crypto.PublicKey(addr)
	}
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return "", err
	}
	resp, err := kmd.ImportMultisigAddr(walletHandle, 1, threshold, pks)
	if err != nil {
		return "", err
	}

	return resp.Address, nil
}

// DeleteMultisigAccount deletes a multisig account.
func (c *Client) DeleteMultisigAccount(walletHandle []byte, walletPassword []byte, addr string) error {
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return err
	}

	_, err = kmd.DeleteMultisigAddr(walletHandle, walletPassword, addr)
	return err
}

// LookupMultisigAccount returns the threshold and public keys for a
// multisig address.
func (c *Client) LookupMultisigAccount(walletHandle []byte, multisigAddr string) (info MultisigInfo, err error) {
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return
	}

	resp, err := kmd.ExportMultisigAddr(walletHandle, multisigAddr)
	if err != nil {
		return
	}

	var pks []string
	for _, pk := range resp.PKs {
		addr := basics.Address(pk).String()
		pks = append(pks, addr)
	}

	info.Version = resp.Version
	info.Threshold = resp.Threshold
	info.PKs = pks
	return
}

// MultisigInfo represents the information about a multisig account.
type MultisigInfo struct {
	Version   uint8
	Threshold uint8
	PKs       []string
}

// SendPaymentFromWallet signs a transaction using the given wallet and returns the resulted transaction id
func (c *Client) SendPaymentFromWallet(walletHandle, pw []byte, from, to string, fee, amount uint64, note []byte, closeTo string, firstValid, lastValid basics.Round) (transactions.Transaction, error) {
	return c.SendPaymentFromWalletWithLease(walletHandle, pw, from, to, fee, amount, note, closeTo, [32]byte{}, firstValid, lastValid)
}

// SendPaymentFromWalletWithLease is like SendPaymentFromWallet, but with a custom lease.
func (c *Client) SendPaymentFromWalletWithLease(walletHandle, pw []byte, from, to string, fee, amount uint64, note []byte, closeTo string, lease [32]byte, firstValid, lastValid basics.Round) (transactions.Transaction, error) {
	// Build the transaction
	tx, err := c.ConstructPayment(from, to, fee, amount, note, closeTo, lease, firstValid, lastValid)
	if err != nil {
		return transactions.Transaction{}, err
	}

	return c.signAndBroadcastTransactionWithWallet(walletHandle, pw, tx)
}

func (c *Client) signAndBroadcastTransactionWithWallet(walletHandle, pw []byte, tx transactions.Transaction) (transactions.Transaction, error) {
	// Sign the transaction
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return transactions.Transaction{}, err
	}
	// TODO(rekeying) probably libgoal should allow passing in different public key to sign with
	resp0, err := kmd.SignTransaction(walletHandle, pw, crypto.PublicKey{}, tx)
	if err != nil {
		return transactions.Transaction{}, err
	}

	// Decode the SignedTxn
	var stx transactions.SignedTxn
	err = protocol.Decode(resp0.SignedTransaction, &stx)
	if err != nil {
		return transactions.Transaction{}, err
	}

	// Broadcast the transaction
	algod, err := c.ensureAlgodClient()
	if err != nil {
		return transactions.Transaction{}, err
	}

	_, err = algod.SendRawTransaction(stx)
	if err != nil {
		return transactions.Transaction{}, err
	}
	return tx, nil
}

// ComputeValidityRounds takes first, last and rounds provided by a user and resolves them into
// actual firstValid and lastValid.
// Resolution table
//
// validRounds | lastValid | result (lastValid)
// -------------------------------------------------
// 	  	 0     |     0     | firstValid + maxTxnLife
// 		 0     |     N     | lastValid
// 		 M     |     0     | first + validRounds - 1
// 		 M     |     M     | error
//
func (c *Client) ComputeValidityRounds(firstValid, lastValid, validRounds uint64) (uint64, uint64, error) {
	params, err := c.SuggestedParams()
	if err != nil {
		return 0, 0, err
	}
	cparams, ok := c.consensus[protocol.ConsensusVersion(params.ConsensusVersion)]
	if !ok {
		return 0, 0, fmt.Errorf("cannot construct transaction: unknown consensus protocol %s", params.ConsensusVersion)
	}

	return computeValidityRounds(firstValid, lastValid, validRounds, params.LastRound, cparams.MaxTxnLife)
}

func computeValidityRounds(firstValid, lastValid, validRounds, lastRound, maxTxnLife uint64) (uint64, uint64, error) {
	if validRounds != 0 && lastValid != 0 {
		return 0, 0, fmt.Errorf("cannot construct transaction: ambiguous input: lastValid = %d, validRounds = %d", lastValid, validRounds)
	}

	if firstValid == 0 {
		firstValid = lastRound + 1
	}

	if validRounds != 0 {
		// MaxTxnLife is the maximum difference between LastValid and FirstValid
		// so that validRounds = maxTxnLife+1 gives lastValid = firstValid + validRounds - 1 = firstValid + maxTxnLife
		if validRounds > maxTxnLife+1 {
			return 0, 0, fmt.Errorf("cannot construct transaction: txn validity period %d is greater than protocol max txn lifetime %d", validRounds-1, maxTxnLife)
		}
		lastValid = firstValid + validRounds - 1
	} else if lastValid == 0 {
		lastValid = firstValid + maxTxnLife
	}

	if firstValid > lastValid {
		return 0, 0, fmt.Errorf("cannot construct transaction: txn would first be valid on round %d which is after last valid round %d", firstValid, lastValid)
	} else if lastValid-firstValid > maxTxnLife {
		return 0, 0, fmt.Errorf("cannot construct transaction: txn validity period ( %d to %d ) is greater than protocol max txn lifetime %d", firstValid, lastValid, maxTxnLife)
	}

	return firstValid, lastValid, nil
}

// ConstructPayment builds a payment transaction to be signed
// If the fee is 0, the function will use the suggested one form the network
// Although firstValid and lastValid come pre-computed in a normal flow,
// additional validation is done by computeValidityRounds:
// if the lastValid is 0, firstValid + maxTxnLifetime will be used
// if the firstValid is 0, lastRound + 1 will be used
func (c *Client) ConstructPayment(from, to string, fee, amount uint64, note []byte, closeTo string, lease [32]byte, firstValid, lastValid basics.Round) (transactions.Transaction, error) {
	fromAddr, err := basics.UnmarshalChecksumAddress(from)
	if err != nil {
		return transactions.Transaction{}, err
	}

	var toAddr basics.Address
	if to != "" {
		toAddr, err = basics.UnmarshalChecksumAddress(to)
		if err != nil {
			return transactions.Transaction{}, err
		}
	}

	// Get current round, protocol, genesis ID
	params, err := c.SuggestedParams()
	if err != nil {
		return transactions.Transaction{}, err
	}

	cp, ok := c.consensus[protocol.ConsensusVersion(params.ConsensusVersion)]
	if !ok {
		return transactions.Transaction{}, fmt.Errorf("ConstructPayment: unknown consensus protocol %s", params.ConsensusVersion)
	}
	fv, lv, err := computeValidityRounds(uint64(firstValid), uint64(lastValid), 0, params.LastRound, cp.MaxTxnLife)
	if err != nil {
		return transactions.Transaction{}, err
	}

	tx := transactions.Transaction{
		Type: protocol.PaymentTx,
		Header: transactions.Header{
			Sender:     fromAddr,
			Fee:        basics.MicroAlgos{Raw: fee},
			FirstValid: basics.Round(fv),
			LastValid:  basics.Round(lv),
			Lease:      lease,
			Note:       note,
		},
		PaymentTxnFields: transactions.PaymentTxnFields{
			Receiver: toAddr,
			Amount:   basics.MicroAlgos{Raw: amount},
		},
	}

	// If requesting closing, put it in the transaction.  The protocol might
	// not support it, but in that case, better to fail the transaction,
	// because the user explicitly asked for it, and it's not supported.
	if closeTo != "" {
		closeToAddr, err := basics.UnmarshalChecksumAddress(closeTo)
		if err != nil {
			return transactions.Transaction{}, err
		}

		tx.PaymentTxnFields.CloseRemainderTo = closeToAddr
	}

	tx.Header.GenesisID = params.GenesisID

	// Check if the protocol supports genesis hash
	if cp.SupportGenesisHash {
		copy(tx.Header.GenesisHash[:], params.GenesisHash)
	}

	// Default to the suggested fee, if the caller didn't supply it
	// Fee is tricky, should taken care last. We encode the final transaction to get the size post signing and encoding
	// Then, we multiply it by the suggested fee per byte.
	if fee == 0 {
		tx.Fee = basics.MulAIntSaturate(basics.MicroAlgos{Raw: params.Fee}, tx.EstimateEncodedSize())
	}
	if tx.Fee.Raw < cp.MinTxnFee {
		tx.Fee.Raw = cp.MinTxnFee
	}

	return tx, nil
}

/* Algod Wrappers */

// Status returns the node status
func (c *Client) Status() (resp generatedV2.NodeStatusResponse, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.Status()
	}
	return
}

// AccountInformation takes an address and returns its information
func (c *Client) AccountInformation(account string) (resp v1.Account, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.AccountInformation(account)
	}
	return
}

// AccountInformationV2 takes an address and returns its information
func (c *Client) AccountInformationV2(account string) (resp generatedV2.Account, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.AccountInformationV2(account)
	}
	return
}

// AccountData takes an address and returns its basics.AccountData
func (c *Client) AccountData(account string) (accountData basics.AccountData, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		var resp []byte
		resp, err = algod.RawAccountInformationV2(account)
		if err == nil {
			err = protocol.Decode(resp, &accountData)
		}
	}
	return
}

// AssetInformation takes an asset's index and returns its information
func (c *Client) AssetInformation(index uint64) (resp v1.AssetParams, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.AssetInformation(index)
	}
	return
}

// AssetInformationV2 takes an asset's index and returns its information
func (c *Client) AssetInformationV2(index uint64) (resp generatedV2.Asset, err error) {
	algod, err := c.ensureAlgodClient()
	if err != nil {
		return
	}
	resp, err = algod.AssetInformationV2(index)
	if err != nil {
		return generatedV2.Asset{}, err
	}

	byteLen := func(p *[]byte) int {
		if p == nil {
			return 0
		}
		return len(*p)
	}

	// these conversions are in case the strings are not a UTF-8 printable
	if byteLen(resp.Params.NameB64) > 0 {
		resp.Params.Name = new(string)
		*resp.Params.Name = string(*resp.Params.NameB64)
	}
	if byteLen(resp.Params.UnitNameB64) > 0 {
		resp.Params.UnitName = new(string)
		*resp.Params.UnitName = string(*resp.Params.UnitNameB64)
	}
	if byteLen(resp.Params.UrlB64) > 0 {
		resp.Params.Url = new(string)
		*resp.Params.Url = string(*resp.Params.UrlB64)
	}
	return
}

// ApplicationInformation takes an app's index and returns its information
func (c *Client) ApplicationInformation(index uint64) (resp generatedV2.Application, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.ApplicationInformation(index)
	}
	return
}

// TransactionInformation takes an address and associated txid and return its information
func (c *Client) TransactionInformation(addr, txid string) (resp v1.Transaction, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.TransactionInformation(addr, txid)
	}
	return
}

// PendingTransactionInformation returns information about a recently issued
// transaction based on its txid.
func (c *Client) PendingTransactionInformation(txid string) (resp v1.Transaction, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.PendingTransactionInformation(txid)
	}
	return
}

// PendingTransactionInformationV2 returns information about a recently issued
// transaction based on its txid.
func (c *Client) PendingTransactionInformationV2(txid string) (resp generatedV2.PendingTransactionResponse, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.PendingTransactionInformationV2(txid)
	}
	return
}

// Block takes a round and returns its block
func (c *Client) Block(round uint64) (resp v1.Block, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.Block(round)
	}
	return
}

// RawBlock takes a round and returns its block
func (c *Client) RawBlock(round uint64) (resp v1.RawBlock, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.RawBlock(round)
	}
	return
}

// BookkeepingBlock takes a round and returns its block
func (c *Client) BookkeepingBlock(round uint64) (block bookkeeping.Block, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		var resp []byte
		resp, err = algod.RawBlock(round)
		if err == nil {
			var b rpcs.EncodedBlockCert
			err = protocol.DecodeReflect(resp, &b)
			if err != nil {
				return
			}
			block = b.Block
		}
	}
	return
}

// HealthCheck returns an error if something is wrong
func (c *Client) HealthCheck() error {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		err = algod.HealthCheck()
	}
	return err
}

// WaitForRound takes a round, waits until it appears and returns its status. This function blocks.
func (c *Client) WaitForRound(round uint64) (resp generatedV2.NodeStatusResponse, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.StatusAfterBlock(round)
	}
	return
}

// GetBalance takes an address and returns its total balance; if the address doesn't exist, it returns 0.
func (c *Client) GetBalance(address string) (uint64, error) {
	resp, err := c.AccountInformation(address)
	if err != nil {
		return 0, err
	}
	return resp.Amount, nil
}

// AlgodVersions return the list of supported API versions in algod
func (c Client) AlgodVersions() (resp common.Version, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.Versions()
	}
	return
}

// LedgerSupply returns the total number of algos in the system
func (c Client) LedgerSupply() (resp v1.Supply, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.LedgerSupply()
	}
	return
}

// CurrentRound returns the current known round
func (c Client) CurrentRound() (lastRound uint64, err error) {
	// Get current round
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err := algod.Status()
		if err == nil {
			lastRound = resp.LastRound
		}
	}
	return
}

// SuggestedFee returns the suggested fee per byte by the network
func (c *Client) SuggestedFee() (fee uint64, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err := algod.SuggestedFee()
		if err == nil {
			fee = resp.Fee
		}
	}
	return
}

// SuggestedParams returns the suggested parameters for a new transaction
func (c *Client) SuggestedParams() (params v1.TransactionParams, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		params, err = algod.SuggestedParams()
	}
	return
}

// GetPendingTransactions gets a snapshot of current pending transactions on the node.
// If maxTxns = 0, fetches as many transactions as possible.
func (c *Client) GetPendingTransactions(maxTxns uint64) (resp v1.PendingTransactions, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.GetPendingTransactions(maxTxns)
	}
	return
}

// GetPendingTransactionsByAddress gets a snapshot of current pending transactions on the node for the given address.
// If maxTxns = 0, fetches as many transactions as possible.
func (c *Client) GetPendingTransactionsByAddress(addr string, maxTxns uint64) (resp v1.PendingTransactions, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		resp, err = algod.PendingTransactionsByAddr(addr, maxTxns)
	}
	return
}

// AddParticipationKey takes a participation key file and sends it to the node.
// The key will be loaded into the system when the function returns successfully.
func (c *Client) AddParticipationKey(keyfile string) (resp generated.PostParticipationResponse, err error) {
	data, err := ioutil.ReadFile(keyfile)
	if err != nil {
		return
	}

	algod, err := c.ensureAlgodClient()
	if err != nil {
		return
	}

	return algod.PostParticipationKey(data)
}

// GetParticipationKeys gets the currently installed participation keys.
func (c *Client) GetParticipationKeys() (resp generated.ParticipationKeysResponse, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		return algod.GetParticipationKeys()
	}
	return
}

// GetParticipationKeyByID looks up a specific participation key by its participationID.
func (c *Client) GetParticipationKeyByID(id string) (resp generated.ParticipationKeyResponse, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		return algod.GetParticipationKeyByID(id)
	}
	return
}

// ExportKey exports the private key of the passed account, assuming it's available
func (c *Client) ExportKey(walletHandle []byte, password, account string) (resp kmdapi.APIV1POSTKeyExportResponse, err error) {
	kmd, err := c.ensureKmdClient()
	if err != nil {
		return
	}

	// export the secret key for the bidder
	req := kmdapi.APIV1POSTKeyExportRequest{
		WalletHandleToken: string(walletHandle),
		Address:           account,
		WalletPassword:    password,
	}
	resp = kmdapi.APIV1POSTKeyExportResponse{}
	err = kmd.DoV1Request(req, &resp)
	return resp, err
}

// ConsensusParams returns the consensus parameters for the protocol active at the specified round
func (c *Client) ConsensusParams(round uint64) (consensus config.ConsensusParams, err error) {
	block, err := c.Block(round)
	if err != nil {
		return
	}

	params, ok := c.consensus[protocol.ConsensusVersion(block.CurrentProtocol)]
	if !ok {
		err = fmt.Errorf("ConsensusParams: unknown consensus protocol %s", block.CurrentProtocol)
		return
	}

	return params, nil
}

// SetAPIVersionAffinity sets the desired client API version affinity of the algod and kmd clients.
func (c *Client) SetAPIVersionAffinity(algodVersionAffinity algodclient.APIVersion, kmdVersionAffinity kmdclient.APIVersion) {
	c.algodVersionAffinity = algodVersionAffinity
	c.kmdVersionAffinity = kmdVersionAffinity
}

// AbortCatchup aborts the currently running catchup
func (c *Client) AbortCatchup() error {
	algod, err := c.ensureAlgodClient()
	if err != nil {
		return err
	}
	// we need to ensure we're using the v2 status so that we would get the catchpoint information.
	algod.SetAPIVersionAffinity(algodclient.APIVersionV2)
	resp, err := algod.Status()
	if err != nil {
		return err
	}
	if resp.Catchpoint == nil || (*resp.Catchpoint) == "" {
		// no error - we were not catching up.
		return nil
	}
	_, err = algod.AbortCatchup(*resp.Catchpoint)
	if err != nil {
		return err
	}
	return nil
}

// Catchup start catching up to the give catchpoint label.
func (c *Client) Catchup(catchpointLabel string) error {
	algod, err := c.ensureAlgodClient()
	if err != nil {
		return err
	}
	_, err = algod.Catchup(catchpointLabel)
	if err != nil {
		return err
	}
	return nil
}

const defaultAppIdx = 1380011588

// MakeDryrunStateBytes function creates DryrunRequest data structure in serialized form according to the format
func MakeDryrunStateBytes(client Client, txnOrStxn interface{}, otherTxns []transactions.SignedTxn, otherAccts []basics.Address, proto string, format string) (result []byte, err error) {
	switch format {
	case "json":
		var gdr generatedV2.DryrunRequest
		gdr, err = MakeDryrunStateGenerated(client, txnOrStxn, otherTxns, otherAccts, proto)
		if err == nil {
			result = protocol.EncodeJSON(&gdr)
		}
		return
	case "msgp":
		var dr v2.DryrunRequest
		dr, err = MakeDryrunState(client, txnOrStxn, otherTxns, otherAccts, proto)
		if err == nil {
			result = protocol.EncodeReflect(&dr)
		}
		return
	default:
		return nil, fmt.Errorf("format %s not supported", format)
	}
}

// MakeDryrunState function creates v2.DryrunRequest data structure
func MakeDryrunState(client Client, txnOrStxn interface{}, otherTxns []transactions.SignedTxn, otherAccts []basics.Address, proto string) (dr v2.DryrunRequest, err error) {
	gdr, err := MakeDryrunStateGenerated(client, txnOrStxn, otherTxns, otherAccts, proto)
	if err != nil {
		return
	}
	return v2.DryrunRequestFromGenerated(&gdr)
}

// MakeDryrunStateGenerated function creates generatedV2.DryrunRequest data structure
func MakeDryrunStateGenerated(client Client, txnOrStxn interface{}, otherTxns []transactions.SignedTxn, otherAccts []basics.Address, proto string) (dr generatedV2.DryrunRequest, err error) {
	var txns []transactions.SignedTxn
	if txnOrStxn == nil {
		// empty input do nothing
	} else if txn, ok := txnOrStxn.(transactions.Transaction); ok {
		txns = append(txns, transactions.SignedTxn{Txn: txn})
	} else if stxn, ok := txnOrStxn.(transactions.SignedTxn); ok {
		txns = append(txns, stxn)
	} else {
		err = fmt.Errorf("unsupported txn type")
		return
	}

	txns = append(txns, otherTxns...)
	for i := range txns {
		enc := protocol.EncodeJSON(&txns[i])
		dr.Txns = append(dr.Txns, enc)
	}

	for _, txn := range txns {
		tx := txn.Txn
		if tx.Type == protocol.ApplicationCallTx {
			accounts := append(tx.Accounts, tx.Sender)
			accounts = append(accounts, otherAccts...)

			apps := []basics.AppIndex{tx.ApplicationID}
			apps = append(apps, tx.ForeignApps...)
			for _, appIdx := range apps {
				var appParams generatedV2.ApplicationParams
				if appIdx == 0 {
					// if it is an app create txn then use params from the txn
					appParams.ApprovalProgram = tx.ApprovalProgram
					appParams.ClearStateProgram = tx.ClearStateProgram
					appParams.GlobalStateSchema = &generatedV2.ApplicationStateSchema{
						NumUint:      tx.GlobalStateSchema.NumUint,
						NumByteSlice: tx.GlobalStateSchema.NumByteSlice,
					}
					appParams.LocalStateSchema = &generatedV2.ApplicationStateSchema{
						NumUint:      tx.LocalStateSchema.NumUint,
						NumByteSlice: tx.LocalStateSchema.NumByteSlice,
					}
					appParams.Creator = tx.Sender.String()
					// zero is not acceptable by ledger in dryrun/debugger
					appIdx = defaultAppIdx
				} else {
					// otherwise need to fetch app state
					var app generatedV2.Application
					if app, err = client.ApplicationInformation(uint64(appIdx)); err != nil {
						return
					}
					appParams = app.Params
					accounts = append(accounts, appIdx.Address())
				}
				dr.Apps = append(dr.Apps, generatedV2.Application{
					Id:     uint64(appIdx),
					Params: appParams,
				})
			}

			for _, acc := range accounts {
				var info generatedV2.Account
				if info, err = client.AccountInformationV2(acc.String()); err != nil {
					// ignore error - accounts might have app addresses that were not funded
					continue
				}
				dr.Accounts = append(dr.Accounts, info)
			}

			dr.ProtocolVersion = proto
			if dr.Round, err = client.CurrentRound(); err != nil {
				return
			}
			var b v1.Block
			if b, err = client.Block(dr.Round); err != nil {
				return
			}
			dr.LatestTimestamp = uint64(b.Timestamp)
		}
	}
	return
}

// Dryrun takes an app's index and returns its information
func (c *Client) Dryrun(data []byte) (resp generatedV2.DryrunResponse, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		data, err = algod.RawDryrun(data)
		if err != nil {
			return
		}
		err = json.Unmarshal(data, &resp)
	}
	return
}

// TxnProof returns a Merkle proof for a transaction in a block.
func (c *Client) TxnProof(txid string, round uint64) (resp generatedV2.ProofResponse, err error) {
	algod, err := c.ensureAlgodClient()
	if err == nil {
		return algod.Proof(txid, round)
	}
	return
}