Skip to content

Commit e5f0c14

Browse files
committed
fix: include address when showing wallet
The addresses associated with a wallet file are now displayed when the file is read. The flag `--no-address` hides the address, similar to the old behavior. Related to #38
1 parent d70dcfc commit e5f0c14

1 file changed

Lines changed: 37 additions & 42 deletions

File tree

cmd/wallet.go

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ var (
4242

4343
// useLedger indicates that the Ledger device should be used.
4444
useLedger bool
45+
46+
// noAddress indicates that the address should not be shown when printing a key.
47+
// This matches the old behavior of the read cmd.
48+
noAddress bool
4549
)
4650

4751
// walletCmd represents the wallet command.
@@ -145,15 +149,15 @@ sure the device is connected, unlocked, and the Spacemesh app is open.`,
145149

146150
// readCmd reads an existing wallet file.
147151
var readCmd = &cobra.Command{
148-
Use: "read [wallet file] [--full/-f] [--private/-p] [--parent] [--base58] [--hex]",
152+
Use: "read [wallet file] [--full/-f] [--private/-p] [--parent] [--base58] [--hex] [--no-address]",
149153
DisableFlagsInUseLine: true,
150154
Short: "Reads an existing wallet file",
151155
Long: `This command can be used to verify whether an existing wallet file can be
152156
successfully read and decrypted, whether the password to open the file is correct, etc.
153157
It prints the accounts from the wallet file. By default it does not print private keys.
154158
Add --private to print private keys. Add --full to print full keys. Add --base58 to print
155159
keys in base58 format or --hex for hexdecimal rather than bech32. Add --parent to print parent key (and not
156-
only child keys).`,
160+
only child keys). Add --no-address to not print the address.`,
157161
Args: cobra.ExactArgs(1),
158162
Run: func(cmd *cobra.Command, args []string) {
159163
w, err := internal.LoadWallet(args[0], debug)
@@ -176,8 +180,11 @@ only child keys).`,
176180
{Number: 1, WidthMax: maxWidth, WidthMaxEnforcer: widthEnforcer},
177181
}
178182

179-
// TODO: add spacemesh address format (bech32)
180-
// https://github.com/spacemeshos/smcli/issues/38
183+
if !noAddress {
184+
header = append(header[:3], header[2:]...)
185+
header[2] = "address"
186+
}
187+
181188
if printPrivate {
182189
caption = append(caption, fmt.Sprintf("Mnemonic: %s", w.Mnemonic()))
183190
header = append(header[:2], header[1:]...)
@@ -218,55 +225,42 @@ only child keys).`,
218225
}
219226
}
220227

221-
privKeyEncoder := func(privKey []byte) string {
222-
if len(privKey) == 0 {
223-
return "(none)"
228+
addRow := func(account *wallet.EDKeyPair) {
229+
row := make([]interface{}, 0, 6) // Row len is 4 w/o address, up to 6 w/ priv key.
230+
row = append(row, encoder(account.Public))
231+
232+
if printPrivate {
233+
privKey := "(none)"
234+
if len(account.Private) > 0 {
235+
privKey = encoder(account.Private)
236+
}
237+
238+
row = append(row, privKey)
224239
}
225-
return encoder(privKey)
240+
241+
row = append(row, account.Path.String())
242+
243+
if !noAddress {
244+
row = append(row, types.GenerateAddress(account.Public).String())
245+
}
246+
247+
row = append(row, account.DisplayName, account.Created)
248+
249+
t.AppendRow(row)
226250
}
227251

228252
// print the master account
229253
if printParent {
230-
master := w.Secrets.MasterKeypair
231-
if master != nil {
232-
if printPrivate {
233-
t.AppendRow(table.Row{
234-
encoder(master.Public),
235-
privKeyEncoder(master.Private),
236-
master.Path.String(),
237-
master.DisplayName,
238-
master.Created,
239-
})
240-
} else {
241-
t.AppendRow(table.Row{
242-
encoder(master.Public),
243-
master.Path.String(),
244-
master.DisplayName,
245-
master.Created,
246-
})
247-
}
254+
if master := w.Secrets.MasterKeypair; master != nil {
255+
addRow(master)
248256
}
249257
}
250258

251259
// print child accounts
252260
for _, a := range w.Secrets.Accounts {
253-
if printPrivate {
254-
t.AppendRow(table.Row{
255-
encoder(a.Public),
256-
privKeyEncoder(a.Private),
257-
a.Path.String(),
258-
a.DisplayName,
259-
a.Created,
260-
})
261-
} else {
262-
t.AppendRow(table.Row{
263-
encoder(a.Public),
264-
a.Path.String(),
265-
a.DisplayName,
266-
a.Created,
267-
})
268-
}
261+
addRow(a)
269262
}
263+
270264
t.Render()
271265
},
272266
}
@@ -316,6 +310,7 @@ func init() {
316310
readCmd.Flags().BoolVar(&printBase58, "base58", false, "Print keys in base58 (rather than bech32)")
317311
readCmd.Flags().BoolVar(&printHex, "hex", false, "Print keys in hex (rather than bech32)")
318312
readCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent key (not only child keys)")
313+
readCmd.Flags().BoolVar(&noAddress, "no-address", false, "Do not print the address associated with the key")
319314
readCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug mode")
320315
createCmd.Flags().BoolVarP(&useLedger, "ledger", "l", false, "Create a wallet using a Ledger device")
321316
addrCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent address (not only child addresses)")

0 commit comments

Comments
 (0)