Skip to content

Commit dd91c7c

Browse files
binacsrjl493456442
andauthored
cmd: abstract getPassPhrase functions into one (#21219)
* [cmd] Abstract `getPassPhrase` functions into one. * cmd/ethkey: fix compilation failure Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
1 parent c13df14 commit dd91c7c

6 files changed

Lines changed: 146 additions & 85 deletions

File tree

‎cmd/clef/main.go‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
"github.com/ethereum/go-ethereum/cmd/utils"
4141
"github.com/ethereum/go-ethereum/common"
4242
"github.com/ethereum/go-ethereum/common/hexutil"
43-
"github.com/ethereum/go-ethereum/console/prompt"
4443
"github.com/ethereum/go-ethereum/core/types"
4544
"github.com/ethereum/go-ethereum/crypto"
4645
"github.com/ethereum/go-ethereum/internal/ethapi"
@@ -289,7 +288,7 @@ func initializeSecrets(c *cli.Context) error {
289288
text := "The master seed of clef will be locked with a password.\nPlease specify a password. Do not forget this password!"
290289
var password string
291290
for {
292-
password = getPassPhrase(text, true)
291+
password = utils.GetPassPhrase(text, true)
293292
if err := core.ValidatePasswordFormat(password); err != nil {
294293
fmt.Printf("invalid password: %v\n", err)
295294
} else {
@@ -362,7 +361,7 @@ func setCredential(ctx *cli.Context) error {
362361
utils.Fatalf("Invalid address specified: %s", addr)
363362
}
364363
address := common.HexToAddress(addr)
365-
password := getPassPhrase("Please enter a password to store for this address:", true)
364+
password := utils.GetPassPhrase("Please enter a password to store for this address:", true)
366365
fmt.Println()
367366

368367
stretchedKey, err := readMasterKey(ctx, nil)
@@ -720,7 +719,7 @@ func readMasterKey(ctx *cli.Context, ui core.UIClientAPI) ([]byte, error) {
720719
}
721720
password = resp.Text
722721
} else {
723-
password = getPassPhrase("Decrypt master seed of clef", false)
722+
password = utils.GetPassPhrase("Decrypt master seed of clef", false)
724723
}
725724
masterSeed, err := decryptSeed(cipherKey, password)
726725
if err != nil {
@@ -915,27 +914,6 @@ func testExternalUI(api *core.SignerAPI) {
915914

916915
}
917916

918-
// getPassPhrase retrieves the password associated with clef, either fetched
919-
// from a list of preloaded passphrases, or requested interactively from the user.
920-
// TODO: there are many `getPassPhrase` functions, it will be better to abstract them into one.
921-
func getPassPhrase(query string, confirmation bool) string {
922-
fmt.Println(query)
923-
password, err := prompt.Stdin.PromptPassword("Password: ")
924-
if err != nil {
925-
utils.Fatalf("Failed to read password: %v", err)
926-
}
927-
if confirmation {
928-
confirm, err := prompt.Stdin.PromptPassword("Repeat password: ")
929-
if err != nil {
930-
utils.Fatalf("Failed to read password confirmation: %v", err)
931-
}
932-
if password != confirm {
933-
utils.Fatalf("Passwords do not match")
934-
}
935-
}
936-
return password
937-
}
938-
939917
type encryptedSeedStorage struct {
940918
Description string `json:"description"`
941919
Version int `json:"version"`

‎cmd/ethkey/changepassword.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Change the password of a keyfile.`,
6767
}
6868
newPhrase = strings.TrimRight(string(content), "\r\n")
6969
} else {
70-
newPhrase = promptPassphrase(true)
70+
newPhrase = utils.GetPassPhrase("", true)
7171
}
7272

7373
// Encrypt the key with the new passphrase.

‎cmd/ethkey/utils.go‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,10 @@ import (
2323
"strings"
2424

2525
"github.com/ethereum/go-ethereum/cmd/utils"
26-
"github.com/ethereum/go-ethereum/console/prompt"
2726
"github.com/ethereum/go-ethereum/crypto"
2827
"gopkg.in/urfave/cli.v1"
2928
)
3029

31-
// promptPassphrase prompts the user for a passphrase. Set confirmation to true
32-
// to require the user to confirm the passphrase.
33-
func promptPassphrase(confirmation bool) string {
34-
passphrase, err := prompt.Stdin.PromptPassword("Password: ")
35-
if err != nil {
36-
utils.Fatalf("Failed to read password: %v", err)
37-
}
38-
39-
if confirmation {
40-
confirm, err := prompt.Stdin.PromptPassword("Repeat password: ")
41-
if err != nil {
42-
utils.Fatalf("Failed to read password confirmation: %v", err)
43-
}
44-
if passphrase != confirm {
45-
utils.Fatalf("Passwords do not match")
46-
}
47-
}
48-
49-
return passphrase
50-
}
51-
5230
// getPassphrase obtains a passphrase given by the user. It first checks the
5331
// --passfile command line flag and ultimately prompts the user for a
5432
// passphrase.
@@ -65,7 +43,7 @@ func getPassphrase(ctx *cli.Context, confirmation bool) string {
6543
}
6644

6745
// Otherwise prompt the user for the passphrase.
68-
return promptPassphrase(confirmation)
46+
return utils.GetPassPhrase("", confirmation)
6947
}
7048

7149
// signHash is a helper function that calculates a hash for the given message

‎cmd/geth/accountcmd.go‎

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/ethereum/go-ethereum/accounts"
2424
"github.com/ethereum/go-ethereum/accounts/keystore"
2525
"github.com/ethereum/go-ethereum/cmd/utils"
26-
prompt2 "github.com/ethereum/go-ethereum/console/prompt"
2726
"github.com/ethereum/go-ethereum/crypto"
2827
"github.com/ethereum/go-ethereum/log"
2928
"gopkg.in/urfave/cli.v1"
@@ -212,7 +211,7 @@ func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []str
212211
}
213212
for trials := 0; trials < 3; trials++ {
214213
prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3)
215-
password := getPassPhrase(prompt, false, i, passwords)
214+
password := utils.GetPassPhraseWithList(prompt, false, i, passwords)
216215
err = ks.Unlock(account, password)
217216
if err == nil {
218217
log.Info("Unlocked account", "address", account.Address.Hex())
@@ -233,36 +232,6 @@ func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []str
233232
return accounts.Account{}, ""
234233
}
235234

236-
// getPassPhrase retrieves the password associated with an account, either fetched
237-
// from a list of preloaded passphrases, or requested interactively from the user.
238-
func getPassPhrase(prompt string, confirmation bool, i int, passwords []string) string {
239-
// If a list of passwords was supplied, retrieve from them
240-
if len(passwords) > 0 {
241-
if i < len(passwords) {
242-
return passwords[i]
243-
}
244-
return passwords[len(passwords)-1]
245-
}
246-
// Otherwise prompt the user for the password
247-
if prompt != "" {
248-
fmt.Println(prompt)
249-
}
250-
password, err := prompt2.Stdin.PromptPassword("Password: ")
251-
if err != nil {
252-
utils.Fatalf("Failed to read password: %v", err)
253-
}
254-
if confirmation {
255-
confirm, err := prompt2.Stdin.PromptPassword("Repeat password: ")
256-
if err != nil {
257-
utils.Fatalf("Failed to read password confirmation: %v", err)
258-
}
259-
if password != confirm {
260-
utils.Fatalf("Passwords do not match")
261-
}
262-
}
263-
return password
264-
}
265-
266235
func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrError, auth string) accounts.Account {
267236
fmt.Printf("Multiple key files exist for address %x:\n", err.Addr)
268237
for _, a := range err.Matches {
@@ -305,7 +274,7 @@ func accountCreate(ctx *cli.Context) error {
305274
utils.Fatalf("Failed to read configuration: %v", err)
306275
}
307276

308-
password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
277+
password := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
309278

310279
account, err := keystore.StoreKey(keydir, password, scryptN, scryptP)
311280

@@ -333,7 +302,7 @@ func accountUpdate(ctx *cli.Context) error {
333302

334303
for _, addr := range ctx.Args() {
335304
account, oldPassword := unlockAccount(ks, addr, 0, nil)
336-
newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil)
305+
newPassword := utils.GetPassPhraseWithList("Please give a new password. Do not forget this password.", true, 0, nil)
337306
if err := ks.Update(account, oldPassword, newPassword); err != nil {
338307
utils.Fatalf("Could not update the account: %v", err)
339308
}
@@ -352,7 +321,7 @@ func importWallet(ctx *cli.Context) error {
352321
}
353322

354323
stack, _ := makeConfigNode(ctx)
355-
passphrase := getPassPhrase("", false, 0, utils.MakePasswordList(ctx))
324+
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))
356325

357326
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
358327
acct, err := ks.ImportPreSaleKey(keyJSON, passphrase)
@@ -373,7 +342,7 @@ func accountImport(ctx *cli.Context) error {
373342
utils.Fatalf("Failed to load the private key: %v", err)
374343
}
375344
stack, _ := makeConfigNode(ctx)
376-
passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
345+
passphrase := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
377346

378347
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
379348
acct, err := ks.ImportECDSA(key, passphrase)

‎cmd/utils/prompt.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2020 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser 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+
// The go-ethereum library 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 Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package utils contains internal helper functions for go-ethereum commands.
18+
package utils
19+
20+
import (
21+
"fmt"
22+
23+
"github.com/ethereum/go-ethereum/console/prompt"
24+
)
25+
26+
// GetPassPhrase displays the given text(prompt) to the user and requests some textual
27+
// data to be entered, but one which must not be echoed out into the terminal.
28+
// The method returns the input provided by the user.
29+
func GetPassPhrase(text string, confirmation bool) string {
30+
if text != "" {
31+
fmt.Println(text)
32+
}
33+
password, err := prompt.Stdin.PromptPassword("Password: ")
34+
if err != nil {
35+
Fatalf("Failed to read password: %v", err)
36+
}
37+
if confirmation {
38+
confirm, err := prompt.Stdin.PromptPassword("Repeat password: ")
39+
if err != nil {
40+
Fatalf("Failed to read password confirmation: %v", err)
41+
}
42+
if password != confirm {
43+
Fatalf("Passwords do not match")
44+
}
45+
}
46+
return password
47+
}
48+
49+
// GetPassPhraseWithList retrieves the password associated with an account, either fetched
50+
// from a list of preloaded passphrases, or requested interactively from the user.
51+
func GetPassPhraseWithList(text string, confirmation bool, index int, passwords []string) string {
52+
// If a list of passwords was supplied, retrieve from them
53+
if len(passwords) > 0 {
54+
if index < len(passwords) {
55+
return passwords[index]
56+
}
57+
return passwords[len(passwords)-1]
58+
}
59+
// Otherwise prompt the user for the password
60+
password := GetPassPhrase(text, confirmation)
61+
return password
62+
}

‎cmd/utils/prompt_test.go‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2020 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser 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+
// The go-ethereum library 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 Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package utils contains internal helper functions for go-ethereum commands.
18+
package utils
19+
20+
import (
21+
"testing"
22+
)
23+
24+
func TestGetPassPhraseWithList(t *testing.T) {
25+
type args struct {
26+
text string
27+
confirmation bool
28+
index int
29+
passwords []string
30+
}
31+
tests := []struct {
32+
name string
33+
args args
34+
want string
35+
}{
36+
{
37+
"test1",
38+
args{
39+
"text1",
40+
false,
41+
0,
42+
[]string{"zero", "one", "two"},
43+
},
44+
"zero",
45+
},
46+
{
47+
"test2",
48+
args{
49+
"text2",
50+
false,
51+
5,
52+
[]string{"zero", "one", "two"},
53+
},
54+
"two",
55+
},
56+
{
57+
"test3",
58+
args{
59+
"text3",
60+
true,
61+
1,
62+
[]string{"zero", "one", "two"},
63+
},
64+
"one",
65+
},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
if got := GetPassPhraseWithList(tt.args.text, tt.args.confirmation, tt.args.index, tt.args.passwords); got != tt.want {
70+
t.Errorf("GetPassPhraseWithList() = %v, want %v", got, tt.want)
71+
}
72+
})
73+
}
74+
}

0 commit comments

Comments
 (0)