func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int, inputpassphrases []string) (addrHex, auth string, passphrases []string) { utils.CheckLegalese(ctx.GlobalString(utils.DataDirFlag.Name)) var err error passphrases = inputpassphrases addrHex, err = utils.ParamToAddress(addr, am) if err == nil { // Attempt to unlock the account 3 times attempts := 3 for tries := 0; tries < attempts; tries++ { msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", addr, tries+1, attempts) auth, passphrases = getPassPhrase(ctx, msg, false, i, passphrases) err = am.Unlock(common.HexToAddress(addrHex), auth) if err == nil || passphrases != nil { break } } } if err != nil { utils.Fatalf("Unlock account '%s' (%v) failed: %v", addr, addrHex, err) } fmt.Printf("Account '%s' (%v) unlocked.\n", addr, addrHex) return }
func ambiguousAddrRecovery(am *accounts.Manager, err *accounts.AmbiguousAddrError, auth string) accounts.Account { fmt.Printf("Multiple key files exist for address %x:\n", err.Addr) for _, a := range err.Matches { fmt.Println(" ", a.File) } fmt.Println("Testing your passphrase against all of them...") var match *accounts.Account for _, a := range err.Matches { if err := am.Unlock(a, auth); err == nil { match = &a break } } if match == nil { utils.Fatalf("None of the listed files could be unlocked.") } fmt.Printf("Your passphrase unlocked %s\n", match.File) fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:") for _, a := range err.Matches { if a != *match { fmt.Println(" ", a.File) } } return *match }
// tries unlocking the specified account a few times. func unlockAccount(ctx *cli.Context, accman *accounts.Manager, address string, i int, passwords []string) (accounts.Account, string) { account, err := utils.MakeAddress(accman, address) if err != nil { utils.Fatalf("Could not list accounts: %v", err) } for trials := 0; trials < 3; trials++ { prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3) password := getPassPhrase(prompt, false, i, passwords) err = accman.Unlock(account, password) if err == nil { glog.V(logger.Info).Infof("Unlocked account %x", account.Address) return account, password } if err, ok := err.(*accounts.AmbiguousAddrError); ok { glog.V(logger.Info).Infof("Unlocked account %x", account.Address) return ambiguousAddrRecovery(accman, err, password), password } if err != accounts.ErrDecrypt { // No need to prompt again if the error is not decryption-related. break } } // All trials expended to unlock account, bail out utils.Fatalf("Failed to unlock account %s (%v)", address, err) return accounts.Account{}, "" }
// MakeAddress converts an account specified directly as a hex encoded string or // a key index in the key store to an internal account representation. func MakeAddress(accman *accounts.Manager, account string) (accounts.Account, error) { // If the specified account is a valid address, return it if common.IsHexAddress(account) { return accounts.Account{Address: common.HexToAddress(account)}, nil } // Otherwise try to interpret the account as a keystore index index, err := strconv.Atoi(account) if err != nil { return accounts.Account{}, fmt.Errorf("invalid account address or index %q", account) } return accman.AccountByIndex(index) }
// InsertPreState populates the given database with the genesis // accounts defined by the test. func (t *BlockTest) InsertPreState(db ethdb.Database, am *accounts.Manager) (*state.StateDB, error) { statedb, err := state.New(common.Hash{}, db) if err != nil { return nil, err } for addrString, acct := range t.preAccounts { addr, err := hex.DecodeString(addrString) if err != nil { return nil, err } code, err := hex.DecodeString(strings.TrimPrefix(acct.Code, "0x")) if err != nil { return nil, err } balance, ok := new(big.Int).SetString(acct.Balance, 0) if !ok { return nil, err } nonce, err := strconv.ParseUint(prepInt(16, acct.Nonce), 16, 64) if err != nil { return nil, err } if acct.PrivateKey != "" { privkey, err := hex.DecodeString(strings.TrimPrefix(acct.PrivateKey, "0x")) err = crypto.ImportBlockTestKey(privkey) err = am.TimedUnlock(common.BytesToAddress(addr), "", 999999*time.Second) if err != nil { return nil, err } } obj := statedb.CreateAccount(common.HexToAddress(addrString)) obj.SetCode(code) obj.SetBalance(balance) obj.SetNonce(nonce) for k, v := range acct.Storage { statedb.SetState(common.HexToAddress(addrString), common.HexToHash(k), common.HexToHash(v)) } } root, err := statedb.Commit() if err != nil { return nil, fmt.Errorf("error writing state: %v", err) } if t.Genesis.Root() != root { return nil, fmt.Errorf("computed state root does not match genesis block: genesis=%x computed=%x", t.Genesis.Root().Bytes()[:4], root.Bytes()[:4]) } return statedb, nil }
func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string) (passphrase string) { var err error // Load startup keys. XXX we are going to need a different format // Attempt to unlock the account passphrase = getPassPhrase(ctx, "") accbytes := common.FromHex(account) if len(accbytes) == 0 { utils.Fatalf("Invalid account address '%s'", account) } err = am.Unlock(accbytes, passphrase) if err != nil { utils.Fatalf("Unlock account failed '%v'", err) } return }
func ParamToAddress(addr string, am *accounts.Manager) (addrHex string, err error) { if !((len(addr) == 40) || (len(addr) == 42)) { // with or without 0x index, err := strconv.Atoi(addr) if err != nil { Fatalf("Invalid account address '%s'", addr) } addrHex, err = am.AddressByIndex(index) if err != nil { return "", err } } else { addrHex = addr } return }
// MakeAddress converts an account specified directly as a hex encoded string or // a key index in the key store to an internal account representation. func MakeAddress(accman *accounts.Manager, account string) (a common.Address, err error) { // If the specified account is a valid address, return it if common.IsHexAddress(account) { return common.HexToAddress(account), nil } // Otherwise try to interpret the account as a keystore index index, err := strconv.Atoi(account) if err != nil { return a, fmt.Errorf("invalid account address or index %q", account) } hex, err := accman.AddressByIndex(index) if err != nil { return a, fmt.Errorf("can't get account #%d (%v)", index, err) } return common.HexToAddress(hex), nil }
// MakeEtherbase retrieves the etherbase either from the directly specified // command line flags or from the keystore if CLI indexed. func MakeEtherbase(accman *accounts.Manager, ctx *cli.Context) common.Address { accounts := accman.Accounts() if !ctx.GlobalIsSet(EtherbaseFlag.Name) && len(accounts) == 0 { glog.V(logger.Error).Infoln("WARNING: No etherbase set and no accounts found as default") return common.Address{} } etherbase := ctx.GlobalString(EtherbaseFlag.Name) if etherbase == "" { return common.Address{} } // If the specified etherbase is a valid address, return it account, err := MakeAddress(accman, etherbase) if err != nil { Fatalf("Option %q: %v", EtherbaseFlag.Name, err) } return account.Address }
func unlockAccount(ctx *cli.Context, am *accounts.Manager, addr string, i int) (addrHex, auth string) { var err error addrHex, err = utils.ParamToAddress(addr, am) if err == nil { // Attempt to unlock the account 3 times attempts := 3 for tries := 0; tries < attempts; tries++ { msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", addr, tries+1, attempts) auth = getPassPhrase(ctx, msg, false, i) err = am.Unlock(common.HexToAddress(addrHex), auth) if err == nil { break } } } if err != nil { utils.Fatalf("Unlock account failed '%v'", err) } fmt.Printf("Account '%s' unlocked.\n", addr) return }
func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string) (passphrase string) { var err error // Load startup keys. XXX we are going to need a different format if !((len(account) == 40) || (len(account) == 42)) { // with or without 0x utils.Fatalf("Invalid account address '%s'", account) } // Attempt to unlock the account 3 times attempts := 3 for tries := 0; tries < attempts; tries++ { msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", account, tries+1, attempts) passphrase = getPassPhrase(ctx, msg, false) err = am.Unlock(common.HexToAddress(account), passphrase) if err == nil { break } } if err != nil { utils.Fatalf("Unlock account failed '%v'", err) } fmt.Printf("Account '%s' unlocked.\n", account) return }