Skip to content

Commit 2bce2ae

Browse files
committed
Extract wallet opening into helper function
1 parent c0251e2 commit 2bce2ae

1 file changed

Lines changed: 26 additions & 28 deletions

File tree

cmd/wallet.go

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ var (
4545
hrp string
4646
)
4747

48+
func openWallet(walletFn string) (*wallet.Wallet, error) {
49+
// make sure the file exists
50+
f, err := os.Open(walletFn)
51+
cobra.CheckErr(err)
52+
defer f.Close()
53+
54+
// get the password
55+
fmt.Print("Enter wallet password: ")
56+
password, err := password.Read(os.Stdin)
57+
fmt.Println()
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
// attempt to read it
63+
wk := wallet.NewKey(wallet.WithPasswordOnly([]byte(password)))
64+
w, err := wk.Open(f, debug)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
return w, nil
70+
}
71+
4872
// walletCmd represents the wallet command.
4973
var walletCmd = &cobra.Command{
5074
Use: "wallet",
@@ -158,20 +182,7 @@ only child keys).`,
158182
Run: func(cmd *cobra.Command, args []string) {
159183
walletFn := args[0]
160184

161-
// make sure the file exists
162-
f, err := os.Open(walletFn)
163-
cobra.CheckErr(err)
164-
defer f.Close()
165-
166-
// get the password
167-
fmt.Print("Enter wallet password: ")
168-
password, err := password.Read(os.Stdin)
169-
fmt.Println()
170-
cobra.CheckErr(err)
171-
172-
// attempt to read it
173-
wk := wallet.NewKey(wallet.WithPasswordOnly([]byte(password)))
174-
w, err := wk.Open(f, debug)
185+
w, err := openWallet(walletFn)
175186
cobra.CheckErr(err)
176187

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

303-
// make sure the file exists
304-
f, err := os.Open(walletFn)
305-
cobra.CheckErr(err)
306-
defer f.Close()
307-
308-
// get the password
309-
fmt.Print("Enter wallet password: ")
310-
password, err := password.Read(os.Stdin)
311-
fmt.Println()
312-
cobra.CheckErr(err)
313-
314-
// attempt to read it
315-
wk := wallet.NewKey(wallet.WithPasswordOnly([]byte(password)))
316-
w, err := wk.Open(f, debug)
314+
w, err := openWallet(walletFn)
317315
cobra.CheckErr(err)
318316

319317
// Sign message using child account 0.

0 commit comments

Comments
 (0)