|
| 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 utils |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/ethereum/go-ethereum/accounts" |
| 23 | + // "strings" |
| 24 | + "github.com/ethereum/go-ethereum/console" |
| 25 | + "github.com/ethereum/go-ethereum/logger" |
| 26 | + "github.com/ethereum/go-ethereum/logger/glog" |
| 27 | + "gopkg.in/urfave/cli.v1" |
| 28 | + // "github.com/peterh/liner" |
| 29 | +) |
| 30 | + |
| 31 | +// tries unlocking the specified account a few times. |
| 32 | +func UnlockAccount(ctx *cli.Context, accman *accounts.Manager, address string, i int, passwords []string) (accounts.Account, string) { |
| 33 | + account, err := MakeAddress(accman, address) |
| 34 | + if err != nil { |
| 35 | + Fatalf("Could not list accounts: %v", err) |
| 36 | + } |
| 37 | + for trials := 0; trials < 3; trials++ { |
| 38 | + prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3) |
| 39 | + password := GetPassPhrase(prompt, false, i, passwords) |
| 40 | + err = accman.Unlock(account, password) |
| 41 | + if err == nil { |
| 42 | + glog.V(logger.Info).Infof("Unlocked account %x", account.Address) |
| 43 | + return account, password |
| 44 | + } |
| 45 | + if err, ok := err.(*accounts.AmbiguousAddrError); ok { |
| 46 | + glog.V(logger.Info).Infof("Unlocked account %x", account.Address) |
| 47 | + return AmbiguousAddrRecovery(accman, err, password), password |
| 48 | + } |
| 49 | + if err != accounts.ErrDecrypt { |
| 50 | + // No need to prompt again if the error is not decryption-related. |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + // All trials expended to unlock account, bail out |
| 55 | + Fatalf("Failed to unlock account %s (%v)", address, err) |
| 56 | + return accounts.Account{}, "" |
| 57 | +} |
| 58 | + |
| 59 | +// getPassPhrase retrieves the passwor associated with an account, either fetched |
| 60 | +// from a list of preloaded passphrases, or requested interactively from the user. |
| 61 | +func GetPassPhrase(prompt string, confirmation bool, i int, passwords []string) string { |
| 62 | + // If a list of passwords was supplied, retrieve from them |
| 63 | + if len(passwords) > 0 { |
| 64 | + if i < len(passwords) { |
| 65 | + return passwords[i] |
| 66 | + } |
| 67 | + return passwords[len(passwords)-1] |
| 68 | + } |
| 69 | + // Otherwise prompt the user for the password |
| 70 | + if prompt != "" { |
| 71 | + fmt.Println(prompt) |
| 72 | + } |
| 73 | + password, err := console.Stdin.PromptPassword("Passphrase: ") |
| 74 | + if err != nil { |
| 75 | + Fatalf("Failed to read passphrase: %v", err) |
| 76 | + } |
| 77 | + if confirmation { |
| 78 | + confirm, err := console.Stdin.PromptPassword("Repeat passphrase: ") |
| 79 | + if err != nil { |
| 80 | + Fatalf("Failed to read passphrase confirmation: %v", err) |
| 81 | + } |
| 82 | + if password != confirm { |
| 83 | + Fatalf("Passphrases do not match") |
| 84 | + } |
| 85 | + } |
| 86 | + return password |
| 87 | +} |
| 88 | + |
| 89 | +func AmbiguousAddrRecovery(am *accounts.Manager, err *accounts.AmbiguousAddrError, auth string) accounts.Account { |
| 90 | + fmt.Printf("Multiple key files exist for address %x:\n", err.Addr) |
| 91 | + for _, a := range err.Matches { |
| 92 | + fmt.Println(" ", a.File) |
| 93 | + } |
| 94 | + fmt.Println("Testing your passphrase against all of them...") |
| 95 | + var match *accounts.Account |
| 96 | + for _, a := range err.Matches { |
| 97 | + if err := am.Unlock(a, auth); err == nil { |
| 98 | + match = &a |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | + if match == nil { |
| 103 | + Fatalf("None of the listed files could be unlocked.") |
| 104 | + } |
| 105 | + fmt.Printf("Your passphrase unlocked %s\n", match.File) |
| 106 | + fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:") |
| 107 | + for _, a := range err.Matches { |
| 108 | + if a != *match { |
| 109 | + fmt.Println(" ", a.File) |
| 110 | + } |
| 111 | + } |
| 112 | + return *match |
| 113 | +} |
0 commit comments