Skip to content

Commit fc2e33c

Browse files
committed
unlock multiple passes and obsolete primary
* multiple passwords allowed in password file * split on "\n", sideeffect: chop trailing slashes. fixes common mistake <(echo 'pass') * remove accounts.Primary method * do not fall back to primary account for mining
1 parent 1d72aaa commit fc2e33c

6 files changed

Lines changed: 27 additions & 41 deletions

File tree

‎accounts/account_manager.go‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,6 @@ func (am *Manager) HasAccount(addr common.Address) bool {
8181
return false
8282
}
8383

84-
func (am *Manager) Primary() (addr common.Address, err error) {
85-
addrs, err := am.keyStore.GetKeyAddresses()
86-
if os.IsNotExist(err) {
87-
return common.Address{}, ErrNoKeys
88-
} else if err != nil {
89-
return common.Address{}, err
90-
}
91-
if len(addrs) == 0 {
92-
return common.Address{}, ErrNoKeys
93-
}
94-
return addrs[0], nil
95-
}
96-
9784
func (am *Manager) DeleteAccount(address common.Address, auth string) error {
9885
return am.keyStore.DeleteKey(address, auth)
9986
}

‎accounts/accounts_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestSign(t *testing.T) {
1818
pass := "" // not used but required by API
1919
a1, err := am.NewAccount(pass)
2020
toSign := randentropy.GetEntropyCSPRNG(32)
21-
am.Unlock(a1.Address, "", 0)
21+
am.Unlock(a1.Address, "")
2222

2323
_, err = am.Sign(a1, toSign)
2424
if err != nil {

‎cmd/geth/js_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestAccounts(t *testing.T) {
140140
defer os.RemoveAll(tmp)
141141

142142
checkEvalJSON(t, repl, `eth.accounts`, `["`+testAddress+`"]`)
143-
checkEvalJSON(t, repl, `eth.coinbase`, `"`+testAddress+`"`)
143+
checkEvalJSON(t, repl, `eth.coinbase`, `"`+common.Address{}.Hex()+`"`)
144144

145145
val, err := repl.re.Run(`personal.newAccount("password")`)
146146
if err != nil {

‎cmd/geth/main.go‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ Note that exporting your key in unencrypted format is NOT supported.
153153
154154
Keys are stored under <DATADIR>/keys.
155155
It is safe to transfer the entire directory or the individual keys therein
156-
between ethereum nodes.
156+
between ethereum nodes by simply copying.
157157
Make sure you backup your keys regularly.
158158
159+
In order to use your account to send transactions, you need to unlock them using the
160+
'--unlock' option. The argument is a comma
161+
159162
And finally. DO NOT FORGET YOUR PASSWORD.
160163
`,
161164
Subcommands: []cli.Command{
@@ -430,7 +433,7 @@ func execJSFiles(ctx *cli.Context) {
430433
ethereum.WaitForShutdown()
431434
}
432435

433-
func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string) (passphrase string) {
436+
func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string, i int) {
434437
var err error
435438
// Load startup keys. XXX we are going to need a different format
436439

@@ -441,7 +444,7 @@ func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string) (pass
441444
attempts := 3
442445
for tries := 0; tries < attempts; tries++ {
443446
msg := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", account, tries+1, attempts)
444-
passphrase = getPassPhrase(ctx, msg, false)
447+
passphrase := getPassPhrase(ctx, msg, false, i)
445448
err = am.Unlock(common.HexToAddress(account), passphrase)
446449
if err == nil {
447450
break
@@ -451,7 +454,6 @@ func unlockAccount(ctx *cli.Context, am *accounts.Manager, account string) (pass
451454
utils.Fatalf("Unlock account failed '%v'", err)
452455
}
453456
fmt.Printf("Account '%s' unlocked.\n", account)
454-
return
455457
}
456458

457459
func blockRecovery(ctx *cli.Context) {
@@ -492,16 +494,12 @@ func startEth(ctx *cli.Context, eth *eth.Ethereum) {
492494

493495
account := ctx.GlobalString(utils.UnlockedAccountFlag.Name)
494496
accounts := strings.Split(account, " ")
495-
for _, account := range accounts {
497+
for i, account := range accounts {
496498
if len(account) > 0 {
497499
if account == "primary" {
498-
primaryAcc, err := am.Primary()
499-
if err != nil {
500-
utils.Fatalf("no primary account: %v", err)
501-
}
502-
account = primaryAcc.Hex()
500+
utils.Fatalf("the 'primary' keyword is deprecated. You can use indexes, but the indexes are not permanent, they can change if you add external keys, export your keys or copy your keystore to another node.")
503501
}
504-
unlockAccount(ctx, am, account)
502+
unlockAccount(ctx, am, account, i)
505503
}
506504
}
507505
// Start auxiliary services if enabled.
@@ -535,7 +533,7 @@ func accountList(ctx *cli.Context) {
535533
}
536534
}
537535

538-
func getPassPhrase(ctx *cli.Context, desc string, confirmation bool) (passphrase string) {
536+
func getPassPhrase(ctx *cli.Context, desc string, confirmation bool, i int) (passphrase string) {
539537
passfile := ctx.GlobalString(utils.PasswordFileFlag.Name)
540538
if len(passfile) == 0 {
541539
fmt.Println(desc)
@@ -559,14 +557,22 @@ func getPassPhrase(ctx *cli.Context, desc string, confirmation bool) (passphrase
559557
if err != nil {
560558
utils.Fatalf("Unable to read password file '%s': %v", passfile, err)
561559
}
562-
passphrase = string(passbytes)
560+
// this is backwards compatible if the same password unlocks several accounts
561+
// it also has the consequence that trailing newlines will not count as part
562+
// of the password, so --password <(echo -n 'pass') will now work without -n
563+
passphrases := strings.Split(string(passbytes), "\n")
564+
if i >= len(passphrases) {
565+
passphrase = passphrases[len(passphrases)-1]
566+
} else {
567+
passphrase = passphrases[i]
568+
}
563569
}
564570
return
565571
}
566572

567573
func accountCreate(ctx *cli.Context) {
568574
am := utils.MakeAccountManager(ctx)
569-
passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true)
575+
passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0)
570576
acct, err := am.NewAccount(passphrase)
571577
if err != nil {
572578
utils.Fatalf("Could not create the account: %v", err)
@@ -585,7 +591,7 @@ func importWallet(ctx *cli.Context) {
585591
}
586592

587593
am := utils.MakeAccountManager(ctx)
588-
passphrase := getPassPhrase(ctx, "", false)
594+
passphrase := getPassPhrase(ctx, "", false, 0)
589595

590596
acct, err := am.ImportPreSaleKey(keyJson, passphrase)
591597
if err != nil {
@@ -600,7 +606,7 @@ func accountImport(ctx *cli.Context) {
600606
utils.Fatalf("keyfile must be given as argument")
601607
}
602608
am := utils.MakeAccountManager(ctx)
603-
passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true)
609+
passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0)
604610
acct, err := am.Import(keyfile, passphrase)
605611
if err != nil {
606612
utils.Fatalf("Could not create the account: %v", err)

‎cmd/utils/flags.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ var (
133133

134134
UnlockedAccountFlag = cli.StringFlag{
135135
Name: "unlock",
136-
Usage: "Unlock the account given until this program exits (prompts for password). '--unlock primary' unlocks the primary account",
136+
Usage: "Unlock the account given until this program exits (prompts for password). '--unlock n' unlocks the n-th account in order or creation.",
137137
Value: "",
138138
}
139139
PasswordFileFlag = cli.StringFlag{

‎eth/backend.go‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,8 @@ func (s *Ethereum) StartMining(threads int) error {
464464
func (s *Ethereum) Etherbase() (eb common.Address, err error) {
465465
eb = s.etherbase
466466
if (eb == common.Address{}) {
467-
primary, err := s.accountManager.Primary()
468-
if err != nil {
469-
return eb, err
470-
}
471-
if (primary == common.Address{}) {
472-
err = fmt.Errorf("no accounts found")
473-
return eb, err
474-
}
475-
eb = primary
467+
err = fmt.Errorf("no accounts found")
468+
return eb, err
476469
}
477470
return eb, nil
478471
}

0 commit comments

Comments
 (0)