github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/cmd/gath/accountcmd.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 23 "github.com/atheioschain/go-atheios/accounts" 24 "github.com/atheioschain/go-atheios/accounts/keystore" 25 "github.com/atheioschain/go-atheios/cmd/utils" 26 "github.com/atheioschain/go-atheios/console" 27 "github.com/atheioschain/go-atheios/crypto" 28 "github.com/atheioschain/go-atheios/logger" 29 "github.com/atheioschain/go-atheios/logger/glog" 30 "gopkg.in/urfave/cli.v1" 31 ) 32 33 var ( 34 walletCommand = cli.Command{ 35 Name: "wallet", 36 Usage: "Manage Ethereum presale wallets", 37 ArgsUsage: "", 38 Category: "ACCOUNT COMMANDS", 39 Description: ` 40 gath wallet import /path/to/my/presale.wallet 41 42 will prompt for your password and imports your ether presale account. 43 It can be used non-interactively with the --password option taking a 44 passwordfile as argument containing the wallet password in plaintext. 45 46 `, 47 Subcommands: []cli.Command{ 48 { 49 Action: importWallet, 50 Name: "import", 51 Usage: "Import Ethereum presale wallet", 52 ArgsUsage: "<keyFile>", 53 Description: ` 54 TODO: Please write this 55 `, 56 }, 57 }, 58 } 59 accountCommand = cli.Command{ 60 Action: accountList, 61 Name: "account", 62 Usage: "Manage accounts", 63 ArgsUsage: "", 64 Category: "ACCOUNT COMMANDS", 65 Description: ` 66 Manage accounts lets you create new accounts, list all existing accounts, 67 import a private key into a new account. 68 69 ' help' shows a list of subcommands or help for one subcommand. 70 71 It supports interactive mode, when you are prompted for password as well as 72 non-interactive mode where passwords are supplied via a given password file. 73 Non-interactive mode is only meant for scripted use on test networks or known 74 safe environments. 75 76 Make sure you remember the password you gave when creating a new account (with 77 either new or import). Without it you are not able to unlock your account. 78 79 Note that exporting your key in unencrypted format is NOT supported. 80 81 Keys are stored under <DATADIR>/keystore. 82 It is safe to transfer the entire directory or the individual keys therein 83 between ethereum nodes by simply copying. 84 Make sure you backup your keys regularly. 85 86 In order to use your account to send transactions, you need to unlock them using 87 the '--unlock' option. The argument is a space separated list of addresses or 88 indexes. If used non-interactively with a passwordfile, the file should contain 89 the respective passwords one per line. If you unlock n accounts and the password 90 file contains less than n entries, then the last password is meant to apply to 91 all remaining accounts. 92 93 And finally. DO NOT FORGET YOUR PASSWORD. 94 `, 95 Subcommands: []cli.Command{ 96 { 97 Action: accountList, 98 Name: "list", 99 Usage: "Print account addresses", 100 ArgsUsage: " ", 101 Description: ` 102 TODO: Please write this 103 `, 104 }, 105 { 106 Action: accountCreate, 107 Name: "new", 108 Usage: "Create a new account", 109 ArgsUsage: " ", 110 Description: ` 111 gath account new 112 113 Creates a new account. Prints the address. 114 115 The account is saved in encrypted format, you are prompted for a passphrase. 116 117 You must remember this passphrase to unlock your account in the future. 118 119 For non-interactive use the passphrase can be specified with the --password flag: 120 121 gath --password <passwordfile> account new 122 123 Note, this is meant to be used for testing only, it is a bad idea to save your 124 password to file or expose in any other way. 125 `, 126 }, 127 { 128 Action: accountUpdate, 129 Name: "update", 130 Usage: "Update an existing account", 131 ArgsUsage: "<address>", 132 Description: ` 133 gath account update <address> 134 135 Update an existing account. 136 137 The account is saved in the newest version in encrypted format, you are prompted 138 for a passphrase to unlock the account and another to save the updated file. 139 140 This same command can therefore be used to migrate an account of a deprecated 141 format to the newest format or change the password for an account. 142 143 For non-interactive use the passphrase can be specified with the --password flag: 144 145 gath --password <passwordfile> account update <address> 146 147 Since only one password can be given, only format update can be performed, 148 changing your password is only possible interactively. 149 `, 150 }, 151 { 152 Action: accountImport, 153 Name: "import", 154 Usage: "Import a private key into a new account", 155 ArgsUsage: "<keyFile>", 156 Description: ` 157 gath account import <keyfile> 158 159 Imports an unencrypted private key from <keyfile> and creates a new account. 160 Prints the address. 161 162 The keyfile is assumed to contain an unencrypted private key in hexadecimal format. 163 164 The account is saved in encrypted format, you are prompted for a passphrase. 165 166 You must remember this passphrase to unlock your account in the future. 167 168 For non-interactive use the passphrase can be specified with the -password flag: 169 170 gath --password <passwordfile> account import <keyfile> 171 172 Note: 173 As you can directly copy your encrypted accounts to another ethereum instance, 174 this import mechanism is not needed when you transfer an account between 175 nodes. 176 `, 177 }, 178 }, 179 } 180 ) 181 182 func accountList(ctx *cli.Context) error { 183 stack := utils.MakeNode(ctx, clientIdentifier, gitCommit) 184 185 var index int 186 for _, wallet := range stack.AccountManager().Wallets() { 187 for _, account := range wallet.Accounts() { 188 fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL) 189 index++ 190 } 191 } 192 return nil 193 } 194 195 // tries unlocking the specified account a few times. 196 func unlockAccount(ctx *cli.Context, ks *keystore.KeyStore, address string, i int, passwords []string) (accounts.Account, string) { 197 account, err := utils.MakeAddress(ks, address) 198 if err != nil { 199 utils.Fatalf("Could not list accounts: %v", err) 200 } 201 for trials := 0; trials < 3; trials++ { 202 prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3) 203 password := getPassPhrase(prompt, false, i, passwords) 204 err = ks.Unlock(account, password) 205 if err == nil { 206 glog.V(logger.Info).Infof("Unlocked account %x", account.Address) 207 return account, password 208 } 209 if err, ok := err.(*keystore.AmbiguousAddrError); ok { 210 glog.V(logger.Info).Infof("Unlocked account %x", account.Address) 211 return ambiguousAddrRecovery(ks, err, password), password 212 } 213 if err != keystore.ErrDecrypt { 214 // No need to prompt again if the error is not decryption-related. 215 break 216 } 217 } 218 // All trials expended to unlock account, bail out 219 utils.Fatalf("Failed to unlock account %s (%v)", address, err) 220 return accounts.Account{}, "" 221 } 222 223 // getPassPhrase retrieves the passwor associated with an account, either fetched 224 // from a list of preloaded passphrases, or requested interactively from the user. 225 func getPassPhrase(prompt string, confirmation bool, i int, passwords []string) string { 226 // If a list of passwords was supplied, retrieve from them 227 if len(passwords) > 0 { 228 if i < len(passwords) { 229 return passwords[i] 230 } 231 return passwords[len(passwords)-1] 232 } 233 // Otherwise prompt the user for the password 234 if prompt != "" { 235 fmt.Println(prompt) 236 } 237 password, err := console.Stdin.PromptPassword("Passphrase: ") 238 if err != nil { 239 utils.Fatalf("Failed to read passphrase: %v", err) 240 } 241 if confirmation { 242 confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ") 243 if err != nil { 244 utils.Fatalf("Failed to read passphrase confirmation: %v", err) 245 } 246 if password != confirm { 247 utils.Fatalf("Passphrases do not match") 248 } 249 } 250 return password 251 } 252 253 func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrError, auth string) accounts.Account { 254 fmt.Printf("Multiple key files exist for address %x:\n", err.Addr) 255 for _, a := range err.Matches { 256 fmt.Println(" ", a.URL) 257 } 258 fmt.Println("Testing your passphrase against all of them...") 259 var match *accounts.Account 260 for _, a := range err.Matches { 261 if err := ks.Unlock(a, auth); err == nil { 262 match = &a 263 break 264 } 265 } 266 if match == nil { 267 utils.Fatalf("None of the listed files could be unlocked.") 268 } 269 fmt.Printf("Your passphrase unlocked %s\n", match.URL) 270 fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:") 271 for _, a := range err.Matches { 272 if a != *match { 273 fmt.Println(" ", a.URL) 274 } 275 } 276 return *match 277 } 278 279 // accountCreate creates a new account into the keystore defined by the CLI flags. 280 func accountCreate(ctx *cli.Context) error { 281 stack := utils.MakeNode(ctx, clientIdentifier, gitCommit) 282 password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx)) 283 284 ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 285 account, err := ks.NewAccount(password) 286 if err != nil { 287 utils.Fatalf("Failed to create account: %v", err) 288 } 289 fmt.Printf("Address: {%x}\n", account.Address) 290 return nil 291 } 292 293 // accountUpdate transitions an account from a previous format to the current 294 // one, also providing the possibility to change the pass-phrase. 295 func accountUpdate(ctx *cli.Context) error { 296 if len(ctx.Args()) == 0 { 297 utils.Fatalf("No accounts specified to update") 298 } 299 stack := utils.MakeNode(ctx, clientIdentifier, gitCommit) 300 ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 301 302 account, oldPassword := unlockAccount(ctx, ks, ctx.Args().First(), 0, nil) 303 newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil) 304 if err := ks.Update(account, oldPassword, newPassword); err != nil { 305 utils.Fatalf("Could not update the account: %v", err) 306 } 307 return nil 308 } 309 310 func importWallet(ctx *cli.Context) error { 311 keyfile := ctx.Args().First() 312 if len(keyfile) == 0 { 313 utils.Fatalf("keyfile must be given as argument") 314 } 315 keyJson, err := ioutil.ReadFile(keyfile) 316 if err != nil { 317 utils.Fatalf("Could not read wallet file: %v", err) 318 } 319 320 stack := utils.MakeNode(ctx, clientIdentifier, gitCommit) 321 passphrase := getPassPhrase("", false, 0, utils.MakePasswordList(ctx)) 322 323 ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 324 acct, err := ks.ImportPreSaleKey(keyJson, passphrase) 325 if err != nil { 326 utils.Fatalf("%v", err) 327 } 328 fmt.Printf("Address: {%x}\n", acct.Address) 329 return nil 330 } 331 332 func accountImport(ctx *cli.Context) error { 333 keyfile := ctx.Args().First() 334 if len(keyfile) == 0 { 335 utils.Fatalf("keyfile must be given as argument") 336 } 337 key, err := crypto.LoadECDSA(keyfile) 338 if err != nil { 339 utils.Fatalf("Failed to load the private key: %v", err) 340 } 341 stack := utils.MakeNode(ctx, clientIdentifier, gitCommit) 342 passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx)) 343 344 ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore) 345 acct, err := ks.ImportECDSA(key, passphrase) 346 if err != nil { 347 utils.Fatalf("Could not create the account: %v", err) 348 } 349 fmt.Printf("Address: {%x}\n", acct.Address) 350 return nil 351 }