Skip to content

Commit

Permalink
prompt: minror refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Aug 20, 2022
1 parent 92acdcb commit d9d257a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 48 deletions.
57 changes: 10 additions & 47 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ import (

// ProvideSeed is used to prompt for the wallet seed which maybe required during
// upgrades.
func ProvideSeed() ([]byte, error) {
reader := bufio.NewReader(os.Stdin)
func ProvideSeed() func() ([]byte, error) {
return func() ([]byte, error) {
return provideSeed(bufio.NewReader(os.Stdin))
}
}
func provideSeed(reader *bufio.Reader) ([]byte, error) {
for {
fmt.Print("Enter existing wallet seed: ")
seedStr, err := reader.ReadString('\n')
Expand Down Expand Up @@ -229,30 +233,9 @@ func PublicPass(reader *bufio.Reader, privPass []byte,
return configPubPassphrase, nil
}
}

for {
pubPass, err = promptPass(reader, "Enter the public "+
"passphrase for your new wallet", true)
if err != nil {
return nil, err
}

if bytes.Equal(pubPass, privPass) {
useSamePass, err := promptListBool(reader,
"Are you sure want to use the same passphrase "+
"for public and private data?", "no")
if err != nil {
return nil, err
}

if useSamePass {
break
}

continue
}

break
pubPass, err = provideSeed(reader)
if err != nil {
return nil, err
}

fmt.Println("NOTE: Use the --walletpass option to configure your " +
Expand Down Expand Up @@ -304,25 +287,5 @@ func Seed(reader *bufio.Reader) ([]byte, error) {
return seed, nil
}

for {
fmt.Print("Enter existing wallet seed: ")
seedStr, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
seedStr = strings.TrimSpace(strings.ToLower(seedStr))

seed, err := hex.DecodeString(seedStr)
if err != nil || len(seed) < hdkeychain.MinSeedBytes ||
len(seed) > hdkeychain.MaxSeedBytes {

fmt.Printf("Invalid seed specified. Must be a "+
"hexadecimal value that is at least %d bits and "+
"at most %d bits\n", hdkeychain.MinSeedBytes*8,
hdkeychain.MaxSeedBytes*8)
continue
}

return seed, nil
}
return provideSeed(reader)
}
2 changes: 1 addition & 1 deletion wallet/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (l *Loader) OpenExistingWallet(pubPassphrase []byte, canConsolePrompt bool)
var cbs *waddrmgr.OpenCallbacks
if canConsolePrompt {
cbs = &waddrmgr.OpenCallbacks{
ObtainSeed: prompt.ProvideSeed,
ObtainSeed: prompt.ProvideSeed(),
ObtainPrivatePass: prompt.ProvidePrivPassphrase,
}
} else {
Expand Down

0 comments on commit d9d257a

Please sign in to comment.