Skip to content

Commit

Permalink
fix: include address when showing wallet
Browse files Browse the repository at this point in the history
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 spacemeshos#38
  • Loading branch information
0xjac committed Jul 13, 2023
1 parent c040065 commit 7e0c568
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var (

// useLedger indicates that the Ledger device should be used.
useLedger bool

// noAddress indicates that the address should not be shown when printing a key.
// This matches the old behavior of the read cmd.
noAddress bool
)

// walletCmd represents the wallet command.
Expand Down Expand Up @@ -176,8 +180,11 @@ only child keys).`,
{Number: 1, WidthMax: maxWidth, WidthMaxEnforcer: widthEnforcer},
}

// TODO: add spacemesh address format (bech32)
// https://github.com/spacemeshos/smcli/issues/38
if !noAddress {
header = append(header[:3], header[2:]...)
header[2] = "address"
}

if printPrivate {
caption = append(caption, fmt.Sprintf("Mnemonic: %s", w.Mnemonic()))
header = append(header[:2], header[1:]...)
Expand Down Expand Up @@ -218,55 +225,42 @@ only child keys).`,
}
}

privKeyEncoder := func(privKey []byte) string {
if len(privKey) == 0 {
return "(none)"
addRow := func(account *wallet.EDKeyPair) {
row := make([]interface{}, 0, 6) // Row len is 4 w/o address, up to 6 w/ priv key.
row = append(row, encoder(account.Public))

if printPrivate {
privKey := "(none)"
if len(account.Private) > 0 {
privKey = encoder(account.Private)
}

row = append(row, privKey)
}
return encoder(privKey)

row = append(row, account.Path.String())

if !noAddress {
row = append(row, types.GenerateAddress(account.Public).String())
}

row = append(row, account.DisplayName, account.Created)

t.AppendRow(row)
}

// print the master account
if printParent {
master := w.Secrets.MasterKeypair
if master != nil {
if printPrivate {
t.AppendRow(table.Row{
encoder(master.Public),
privKeyEncoder(master.Private),
master.Path.String(),
master.DisplayName,
master.Created,
})
} else {
t.AppendRow(table.Row{
encoder(master.Public),
master.Path.String(),
master.DisplayName,
master.Created,
})
}
if master := w.Secrets.MasterKeypair; master != nil {
addRow(master)
}
}

// print child accounts
for _, a := range w.Secrets.Accounts {
if printPrivate {
t.AppendRow(table.Row{
encoder(a.Public),
privKeyEncoder(a.Private),
a.Path.String(),
a.DisplayName,
a.Created,
})
} else {
t.AppendRow(table.Row{
encoder(a.Public),
a.Path.String(),
a.DisplayName,
a.Created,
})
}
addRow(a)
}

t.Render()
},
}
Expand Down Expand Up @@ -316,6 +310,7 @@ func init() {
readCmd.Flags().BoolVar(&printBase58, "base58", false, "Print keys in base58 (rather than bech32)")
readCmd.Flags().BoolVar(&printHex, "hex", false, "Print keys in hex (rather than bech32)")
readCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent key (not only child keys)")
readCmd.Flags().BoolVar(&noAddress, "noAddress", false, "Do not print the address associated with the key")
readCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug mode")
createCmd.Flags().BoolVarP(&useLedger, "ledger", "l", false, "Create a wallet using a Ledger device")
addrCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent address (not only child addresses)")
Expand Down

0 comments on commit 7e0c568

Please sign in to comment.