-
Notifications
You must be signed in to change notification settings - Fork 90
/
main.go
152 lines (132 loc) · 4.29 KB
/
main.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
package main
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/algorand/go-algorand-sdk/client/kmd"
"github.com/algorand/go-algorand-sdk/client/v2/algod"
"github.com/algorand/go-algorand-sdk/crypto"
"github.com/algorand/go-algorand-sdk/future"
"github.com/algorand/go-algorand-sdk/types"
)
// CHANGE ME
const (
exampleWalletName = "unencrypted-default-wallet"
exampleWalletPassword = ""
exampleWalletDriver = kmd.DefaultWalletDriver
)
var (
kmdAddress = "http://localhost:4002"
kmdToken = strings.Repeat("a", 64)
algodAddress = "http://localhost:4001"
algodToken = strings.Repeat("a", 64)
)
func main() {
// Create a kmd client
kmdClient, err := kmd.MakeClient(kmdAddress, kmdToken)
if err != nil {
fmt.Printf("failed to make kmd client: %s\n", err)
return
}
fmt.Println("Made a kmd client")
// Create an algod client
algodClient, err := algod.MakeClient(algodAddress, algodToken)
if err != nil {
fmt.Printf("failed to make algod client: %s\n", err)
return
}
// Print algod status
nodeStatus, err := algodClient.Status().Do(context.Background())
if err != nil {
fmt.Printf("error getting algod status: %s\n", err)
return
}
fmt.Printf("algod last round: %d\n", nodeStatus.LastRound)
// List existing wallets, and check if our example wallet already exists
resp0, err := kmdClient.ListWallets()
if err != nil {
fmt.Printf("error listing wallets: %s\n", err)
return
}
fmt.Printf("Got %d wallet(s):\n", len(resp0.Wallets))
var exampleExists bool
var exampleWalletID string
for _, wallet := range resp0.Wallets {
fmt.Printf("ID: %s\tName: %s\n", wallet.ID, wallet.Name)
if wallet.Name == exampleWalletName {
exampleWalletID = wallet.ID
exampleExists = true
}
}
// Create the example wallet, if it doesn't already exist
if !exampleExists {
resp1, err := kmdClient.CreateWallet(exampleWalletName, exampleWalletPassword, exampleWalletDriver, types.MasterDerivationKey{})
if err != nil {
fmt.Printf("error creating wallet: %s\n", err)
return
}
exampleWalletID = resp1.Wallet.ID
fmt.Printf("Created wallet '%s' with ID: %s\n", resp1.Wallet.Name, exampleWalletID)
}
// Get a wallet handle
resp2, err := kmdClient.InitWalletHandle(exampleWalletID, exampleWalletPassword)
if err != nil {
fmt.Printf("Error initializing wallet: %s\n", err)
return
}
// Extract the wallet handle
exampleWalletHandleToken := resp2.WalletHandleToken
// Generate some addresses in the wallet
fmt.Println("Generating 10 addresses")
var addresses []string
for i := 0; i < 10; i++ {
resp3, err := kmdClient.GenerateKey(exampleWalletHandleToken)
if err != nil {
fmt.Printf("Error generating key: %s\n", err)
return
}
fmt.Printf("Generated address %s\n", resp3.Address)
addresses = append(addresses, resp3.Address)
}
// Extract the private key of the first address
fmt.Printf("Extracting private key for %s\n", addresses[0])
resp4, err := kmdClient.ExportKey(exampleWalletHandleToken, exampleWalletPassword, addresses[0])
if err != nil {
fmt.Printf("Error extracting secret key: %s\n", err)
return
}
privateKey := resp4.PrivateKey
// Get the suggested transaction parameters
txParams, err := algodClient.SuggestedParams().Do(context.Background())
if err != nil {
fmt.Printf("error getting suggested tx params: %s\n", err)
return
}
tx, err := future.MakePaymentTxn(addresses[0], addresses[1], 100, nil, "", txParams)
if err != nil {
fmt.Printf("Error creating transaction: %s\n", err)
return
}
fmt.Printf("Made unsigned transaction: %+v\n", tx)
fmt.Println("Signing transaction with go-algo-sdk library function (not kmd)")
txid, stx, err := crypto.SignTransaction(privateKey, tx)
if err != nil {
fmt.Printf("Failed to sign transaction: %s\n", err)
return
}
fmt.Printf("Made signed transaction with TxID %s: %x\n", txid, stx)
// Sign the same transaction with kmd
fmt.Println("Signing same transaction with kmd")
resp5, err := kmdClient.SignTransaction(exampleWalletHandleToken, exampleWalletPassword, tx)
if err != nil {
fmt.Printf("Failed to sign transaction with kmd: %s\n", err)
return
}
fmt.Printf("kmd made signed transaction with bytes: %x\n", resp5.SignedTransaction)
if bytes.Equal(resp5.SignedTransaction, stx) {
fmt.Println("signed transactions match!")
} else {
fmt.Println("signed transactions don't match!")
}
}