|
45 | 45 | hrp string |
46 | 46 | ) |
47 | 47 |
|
| 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 | + |
48 | 72 | // walletCmd represents the wallet command. |
49 | 73 | var walletCmd = &cobra.Command{ |
50 | 74 | Use: "wallet", |
@@ -158,20 +182,7 @@ only child keys).`, |
158 | 182 | Run: func(cmd *cobra.Command, args []string) { |
159 | 183 | walletFn := args[0] |
160 | 184 |
|
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) |
175 | 186 | cobra.CheckErr(err) |
176 | 187 |
|
177 | 188 | widthEnforcer := func(col string, maxLen int) string { |
@@ -300,20 +311,7 @@ var signCmd = &cobra.Command{ |
300 | 311 | walletFn := args[0] |
301 | 312 | message := args[1] |
302 | 313 |
|
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) |
317 | 315 | cobra.CheckErr(err) |
318 | 316 |
|
319 | 317 | // Sign message using child account 0. |
|
0 commit comments