This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
avawallet.go
220 lines (208 loc) · 6.72 KB
/
avawallet.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
// Copyright © 2021 AVA Labs, Inc.
// All rights reserved.
package cmd
import (
"encoding/json"
"fmt"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avash/cfg"
"github.com/ava-labs/avash/node"
pmgr "github.com/ava-labs/avash/processmgr"
"github.com/spf13/cobra"
"github.com/ybbus/jsonrpc"
)
// AVAXWalletCmd represents the avaxwallet command
var AVAXWalletCmd = &cobra.Command{
Use: "avaxwallet",
Short: "Tools for interacting with AVAX Payments over the network.",
Long: `Tools for interacting with AVAX Payments over the network. Using this
command you can send, and get the status of a transaction.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
const (
defaultEncoding = formatting.CB58
)
// AVAXWalletNewKeyCmd creates a new private key
var AVAXWalletNewKeyCmd = &cobra.Command{
Use: "newkey",
Short: "Creates a random private key.",
Long: `Creates a random private key.`,
Run: func(cmd *cobra.Command, args []string) {
log := cfg.Config.Log
factory := crypto.FactorySECP256K1R{}
if skGen, err := factory.NewPrivateKey(); err == nil {
sk := skGen.(*crypto.PrivateKeySECP256K1R)
str, err := formatting.EncodeWithChecksum(defaultEncoding, sk.Bytes())
if err != nil {
log.Error("could not encode private key")
}
log.Info("PrivateKey: PrivateKey-%s", str)
} else {
log.Error("could not create private key")
}
},
}
// AVAXWalletSendCmd will send a transaction through a node
var AVAXWalletSendCmd = &cobra.Command{
Use: "send [node name] [tx string]",
Short: "Sends a transaction to a node.",
Long: `Sends a transaction to a node.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 2 {
log := cfg.Config.Log
if meta, err := pmgr.ProcManager.Metadata(args[0]); err == nil {
var md node.Metadata
metaBytes := []byte(meta)
if err := json.Unmarshal(metaBytes, &md); err == nil {
jrpcloc := fmt.Sprintf("http://%s:%s/ext/bc/avm", md.Serverhost, md.HTTPport)
rpcClient := jsonrpc.NewClient(jrpcloc)
response, err := rpcClient.Call("avm.issueTx", struct {
Tx string
}{
Tx: args[1],
})
if err != nil {
log.Error("error sent tx: %s", args[1])
log.Error("rpcClient returned error: %s", err.Error())
} else if response.Error != nil {
log.Error("error sent tx: %s", args[1])
log.Error("rpcClient returned error: %d, %s", response.Error.Code, response.Error.Message)
} else {
var s struct {
TxID string
}
err = response.GetObject(&s)
if err != nil {
log.Error("error on parsing response: %s", err.Error())
} else {
log.Info("TxID:%s", s.TxID)
}
}
} else {
log.Error("unable to unmarshal metadata for node %s: %s", args[0], err.Error())
}
} else {
log.Error("node not found: %s", args[0])
}
} else {
cmd.Help()
}
},
}
// AVAXWalletStatusCmd will get the status of a transaction for a particular node
var AVAXWalletStatusCmd = &cobra.Command{
Use: "status [node name] [tx id]",
Short: "Checks the status of a transaction on a node.",
Long: `Checks the status of a transaction on a node.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 2 {
log := cfg.Config.Log
if meta, err := pmgr.ProcManager.Metadata(args[0]); err == nil {
var md node.Metadata
metaBytes := []byte(meta)
if err := json.Unmarshal(metaBytes, &md); err == nil {
jrpcloc := fmt.Sprintf("http://%s:%s/ext/bc/avm", md.Serverhost, md.HTTPport)
rpcClient := jsonrpc.NewClient(jrpcloc)
response, err := rpcClient.Call("avm.getTxStatus", struct {
TxID string
}{
TxID: args[1],
})
if err != nil {
log.Error("error sent txid: %s", args[1])
log.Error("rpcClient returned error: %s", err.Error())
} else if response.Error != nil {
log.Error("error sent txid: %s", args[1])
log.Error("rpcClient returned error: %d, %s", response.Error.Code, response.Error.Message)
} else {
var s struct {
Status string
}
err = response.GetObject(&s)
if err != nil {
log.Error("error on parsing response: %s", err.Error())
} else {
log.Info("Status:%s", s.Status)
}
}
} else {
log.Error("unable to unmarshal metadata for node %s: %s", args[0], err.Error())
}
} else {
log.Error("node not found: %s", args[0])
}
} else {
cmd.Help()
}
},
}
// AVAXWalletGetBalanceCmd will get the balance of an address from a node
var AVAXWalletGetBalanceCmd = &cobra.Command{
Use: "balance [node name] [address]",
Short: "Checks the balance of an address from a node.",
Long: `Checks the balance of an address from a node.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 2 {
log := cfg.Config.Log
if meta, err := pmgr.ProcManager.Metadata(args[0]); err == nil {
var md node.Metadata
metaBytes := []byte(meta)
if err := json.Unmarshal(metaBytes, &md); err == nil {
jrpcloc := fmt.Sprintf("http://%s:%s/ext/bc/avm", md.Serverhost, md.HTTPport)
rpcClient := jsonrpc.NewClient(jrpcloc)
response, err := rpcClient.Call("avm.getBalance", struct {
Address string
AssetID string
}{
Address: args[1],
AssetID: "AVAX",
})
if err != nil {
log.Error("error sent address: %s", args[1])
log.Error("rpcClient returned error: %s", err.Error())
} else if response.Error != nil {
log.Error("error sent address: %s", args[1])
log.Error("rpcClient returned error: %d, %s", response.Error.Code, response.Error.Message)
} else {
var s struct {
Balance string
}
err = response.GetObject(&s)
if err != nil {
log.Error("error on parsing response: %s", err.Error())
} else {
log.Info("Balance: %s", s.Balance)
}
}
} else {
log.Error("unable to unmarshal metadata for node %s: %s", args[0], err.Error())
}
} else {
log.Error("node not found: %s", args[0])
}
} else {
cmd.Help()
}
},
}
/*
avaxwallet
create [wallet name] -> "wallet created: " + [wallet name]
addkey [wallet name] [private key] -> address
balance [node name] [address] -> uint
status [node name] [tx string] -> [status]
maketx [wallet name] [destination address] [amount] -> txString
refresh [node name] [wallet name] -> "wallet refreshed: " + [wallet name]
remove [wallet name] [tx string] -> "transaction removed: " + [tx string]
send [node name] [tx string] -> "sent tx: " [tx string]
newkey -> privateKey
*/
func init() {
AVAXWalletCmd.AddCommand(AVAXWalletNewKeyCmd)
AVAXWalletCmd.AddCommand(AVAXWalletGetBalanceCmd)
AVAXWalletCmd.AddCommand(AVAXWalletSendCmd)
AVAXWalletCmd.AddCommand(AVAXWalletStatusCmd)
}