-
Notifications
You must be signed in to change notification settings - Fork 2
/
account.go
384 lines (325 loc) · 10.2 KB
/
account.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
package cli
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/btcsuite/btcutil/base58"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func (cli *CLI) buildAccountCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "account [new|list|update]",
Short: "Manage NewChain accounts",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
return
},
}
cmd.AddCommand(cli.buildAccountNewCmd())
cmd.AddCommand(cli.buildAccountListCmd())
cmd.AddCommand(cli.buildAccountConvertCmd())
cmd.AddCommand(cli.buildAccountUpdateCmd())
cmd.AddCommand(cli.buildAccountImportCmd())
cmd.AddCommand(cli.buildAccountExportCmd())
return cmd
}
func (cli *CLI) buildAccountNewCmd() *cobra.Command {
accountNewCmd := &cobra.Command{
Use: "new [-n number] [--faucet] [-s] [-l]",
Short: "create a new account",
Args: cobra.MinimumNArgs(0),
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
if cmd.Flags().Changed("light") {
light, _ := cmd.Flags().GetBool("light")
standard, _ := cmd.Flags().GetBool("standard")
if light && !standard {
cli.wallet = keystore.NewKeyStore(cli.walletPath,
keystore.LightScryptN, keystore.LightScryptP)
}
}
numOfNew, err := cmd.Flags().GetInt("numOfNew")
if err != nil {
numOfNew = viper.GetInt("account.numOfNew")
}
faucet, _ := cmd.Flags().GetBool("faucet")
aList, err := cli.createAccount(numOfNew)
if err != nil {
fmt.Println(err)
return
}
for _, a := range aList {
fmt.Println(a.String())
if faucet {
getFaucet(cli.rpcURL, a.String())
}
}
},
}
accountNewCmd.Flags().IntP("numOfNew", "n", 1, "number of the new account")
accountNewCmd.Flags().Bool("faucet", false, "get faucet for new account")
accountNewCmd.Flags().BoolP("standard", "s", false, "use the standard scrypt for keystore")
accountNewCmd.Flags().BoolP("light", "l", false, "use the light scrypt for keystore")
return accountNewCmd
}
func (cli *CLI) createAccount(numOfNew int) ([]common.Address, error) {
if cli.wallet == nil {
cli.wallet = keystore.NewKeyStore(cli.walletPath,
keystore.StandardScryptN, keystore.StandardScryptP)
}
walletPassword, err := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true)
if err != nil {
return nil, err
}
if numOfNew <= 0 {
fmt.Printf("number[%d] of new account less then 1\n", numOfNew)
numOfNew = 1
}
aList := make([]common.Address, 0)
for i := 0; i < numOfNew; i++ {
account, err := cli.wallet.NewAccount(walletPassword)
if err != nil {
return nil, fmt.Errorf("account error: %v", err)
}
aList = append(aList, account.Address)
}
return aList, nil
}
func (cli *CLI) buildAccountListCmd() *cobra.Command {
accountListCmd := &cobra.Command{
Use: "list",
Short: "list all accounts in the wallet path",
Args: cobra.MinimumNArgs(0),
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
if err := cli.openWallet(true); err != nil {
fmt.Println(err)
return
}
for _, account := range cli.wallet.Accounts() {
fmt.Println(account.Address.Hex())
}
},
}
return accountListCmd
}
func (cli *CLI) buildAccountConvertCmd() *cobra.Command {
accountListCmd := &cobra.Command{
Use: "convert",
Short: "convert address to NewChainAddress",
Args: cobra.MinimumNArgs(1),
DisableFlagsInUseLine: true,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
chainID := DefaultChainID
err := cli.BuildClient()
if err != nil {
fmt.Printf("Error: build client error(%v), use chainID as %s\n", err, chainID.String())
} else {
networkID, err := cli.client.NetworkID(context.Background())
if err != nil {
fmt.Printf("Error: get chainID error(%v), use chainID as %s\n", err, chainID.String())
} else {
chainID = networkID
}
}
for _, addressStr := range args {
if common.IsHexAddress(addressStr) {
address := common.HexToAddress(addressStr)
fmt.Println(address.String(), addressToNew(chainID.Bytes(), address))
continue
}
address, err := newToAddress(chainID.Bytes(), addressStr)
if err != nil {
fmt.Println(err, addressStr)
continue
}
fmt.Println(address.String(), addressStr)
}
},
}
return accountListCmd
}
func (cli *CLI) buildAccountUpdateCmd() *cobra.Command {
accountNewCmd := &cobra.Command{
Use: "update <address> [-s]",
Short: "Update an existing account",
Args: cobra.MinimumNArgs(1),
DisableFlagsInUseLine: true,
Run: func(cmd *cobra.Command, args []string) {
standard, _ := cmd.Flags().GetBool("standard")
if standard {
cli.wallet = keystore.NewKeyStore(cli.walletPath,
keystore.StandardScryptN, keystore.StandardScryptP)
} else {
cli.wallet = keystore.NewKeyStore(cli.walletPath,
keystore.LightScryptN, keystore.LightScryptP)
}
addrStr := args[0]
var address common.Address
if !common.IsHexAddress(addrStr) {
fmt.Println("Error: No accounts specified to update")
return
}
address = common.HexToAddress(addrStr)
account := accounts.Account{Address: address}
if account.Address == (common.Address{}) {
fmt.Println("Error: ", errRequiredFromAddress)
return
}
if _, err := cli.wallet.Find(account); err != nil {
fmt.Printf("Error: %v (%s)\n", err, account.Address.String())
return
}
var err error
walletPassword := cli.tran.Password
var trials int
for trials = 0; trials < 3; trials++ {
prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", account.Address.String(), trials+1, 3)
if walletPassword == "" {
walletPassword, _ = getPassPhrase(prompt, false)
} else {
fmt.Println(prompt, "\nUse the the password has set")
}
err = cli.wallet.Unlock(account, walletPassword)
if err == nil {
break
}
walletPassword = ""
}
if trials >= 3 {
if err != nil {
fmt.Println("Error: unlock account error: ", err)
return
}
fmt.Printf("Error: failed to unlock account %s (%v)\n", account.Address.String(), err)
return
}
newWalletPassword, err := getPassPhrase("Please give a new password. Do not forget this password.", true)
if err != nil {
fmt.Println("Error: getPassPhrase error: ", err)
return
}
if err := cli.wallet.Update(account, walletPassword, newWalletPassword); err != nil {
fmt.Println("Error: udpate account error: ", err)
return
}
fmt.Println("Successfully updated the account: ", account.Address.String())
},
}
accountNewCmd.Flags().BoolP("standard", "s", false, "use the standard scrypt for keystore")
return accountNewCmd
}
func addressToNew(chainID []byte, address common.Address) string {
input := append(chainID, address.Bytes()...)
return "NEW" + base58.CheckEncode(input, 0)
}
func newToAddress(chainID []byte, newAddress string) (common.Address, error) {
if newAddress[:3] != "NEW" {
return common.Address{}, errors.New("not NEW address")
}
decoded, version, err := base58.CheckDecode(newAddress[3:])
if err != nil {
return common.Address{}, err
}
if version != 0 {
return common.Address{}, errors.New("illegal version")
}
if len(decoded) < 20 {
return common.Address{}, errors.New("illegal decoded length")
}
if !bytes.Equal(decoded[:len(decoded)-20], chainID) {
return common.Address{}, errors.New("illegal ChainID")
}
address := common.BytesToAddress(decoded[len(decoded)-20:])
return address, nil
}
func (cli *CLI) buildAccountImportCmd() *cobra.Command {
accountListCmd := &cobra.Command{
Use: "import <hexkey>",
Short: "import hex private key to wallet",
Args: cobra.MinimumNArgs(1),
DisableFlagsInUseLine: true,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
hexkey := args[0]
hexkeylen := len(hexkey)
if hexkeylen > 1 {
if hexkey[0:2] == "0x" || hexkey[0:2] == "0X" {
hexkey = hexkey[2:]
}
}
pkey, err := crypto.HexToECDSA(hexkey)
if err != nil {
fmt.Println(err)
return
}
if err := cli.openWallet(false); err != nil {
fmt.Println(err)
return
}
walletPassword, err := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true)
if err != nil {
fmt.Println("Error: ", err)
return
}
a, err := cli.wallet.ImportECDSA(pkey, walletPassword)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(a.Address.String())
},
}
return accountListCmd
}
func (cli *CLI) buildAccountExportCmd() *cobra.Command {
accountListCmd := &cobra.Command{
Use: "export <hexAddress>",
Short: "export hex private key of the specified address",
Args: cobra.MinimumNArgs(1),
DisableFlagsInUseLine: true,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
hexAddress := args[0]
if !common.IsHexAddress(hexAddress) {
fmt.Printf("Error: %s is not valid hex-encoded address\n", hexAddress)
return
}
address := common.HexToAddress(hexAddress)
account := accounts.Account{Address: address}
if err := cli.openWallet(true); err != nil {
fmt.Println(err)
return
}
if !cli.wallet.HasAddress(address) {
fmt.Println("The given address is not present")
return
}
var err error
walletPassword := cli.tran.Password
if walletPassword == "" {
prompt := fmt.Sprintf("Unlocking account %s", account.Address.String())
walletPassword, _ = getPassPhrase(prompt, false)
}
keyJSON, err := cli.wallet.Export(account, walletPassword, walletPassword)
if err != nil {
fmt.Println(err)
return
}
key, err := keystore.DecryptKey(keyJSON, walletPassword)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(common.ToHex(key.PrivateKey.D.Bytes()))
},
}
return accountListCmd
}