Skip to content

Commit

Permalink
Extract wallet opening into helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
xearl4 committed Mar 9, 2024
1 parent bc7641e commit cccdf31
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ var (
hrp string
)

func openWallet(walletFn string) (*wallet.Wallet, error) {
// make sure the file exists
f, err := os.Open(walletFn)
cobra.CheckErr(err)
defer f.Close()

// get the password
fmt.Print("Enter wallet password: ")
password, err := password.Read(os.Stdin)
fmt.Println()
if err != nil {
return nil, err
}

// attempt to read it
wk := wallet.NewKey(wallet.WithPasswordOnly([]byte(password)))
w, err := wk.Open(f, debug)
if err != nil {
return nil, err
}

return w, nil
}

// walletCmd represents the wallet command.
var walletCmd = &cobra.Command{
Use: "wallet",
Expand Down Expand Up @@ -158,20 +182,7 @@ only child keys).`,
Run: func(cmd *cobra.Command, args []string) {
walletFn := args[0]

// make sure the file exists
f, err := os.Open(walletFn)
cobra.CheckErr(err)
defer f.Close()

// get the password
fmt.Print("Enter wallet password: ")
password, err := password.Read(os.Stdin)
fmt.Println()
cobra.CheckErr(err)

// attempt to read it
wk := wallet.NewKey(wallet.WithPasswordOnly([]byte(password)))
w, err := wk.Open(f, debug)
w, err := openWallet(walletFn)
cobra.CheckErr(err)

widthEnforcer := func(col string, maxLen int) string {
Expand Down Expand Up @@ -300,20 +311,7 @@ var signCmd = &cobra.Command{
walletFn := args[0]
message := args[1]

// make sure the file exists
f, err := os.Open(walletFn)
cobra.CheckErr(err)
defer f.Close()

// get the password
fmt.Print("Enter wallet password: ")
password, err := password.Read(os.Stdin)
fmt.Println()
cobra.CheckErr(err)

// attempt to read it
wk := wallet.NewKey(wallet.WithPasswordOnly([]byte(password)))
w, err := wk.Open(f, debug)
w, err := openWallet(walletFn)
cobra.CheckErr(err)

// Sign message using child account 0.
Expand Down

0 comments on commit cccdf31

Please sign in to comment.