Skip to content

Commit f6d0707

Browse files
committed
fix: display pubkeys in bech32 format
- Public keys are displayed in the bech32 format by default. - The old hex format is still available with the `--hex` flag. Related to #38
1 parent 944223d commit f6d0707

2 files changed

Lines changed: 48 additions & 58 deletions

File tree

cmd/wallet.go

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/btcsuite/btcutil/base58"
13+
"github.com/cosmos/btcutil/bech32"
1314
"github.com/hashicorp/go-secure-stdlib/password"
1415
"github.com/jedib0t/go-pretty/v6/table"
1516
"github.com/spacemeshos/go-spacemesh/common/types"
@@ -33,6 +34,9 @@ var (
3334
// printBase58 indicates that keys should be printed in base58 format.
3435
printBase58 bool
3536

37+
// printHex indicates that keys should be printed in Hex format.
38+
printHex bool
39+
3640
// printParent indicates that the parent key should be printed.
3741
printParent bool
3842

@@ -141,34 +145,22 @@ sure the device is connected, unlocked, and the Spacemesh app is open.`,
141145

142146
// readCmd reads an existing wallet file.
143147
var readCmd = &cobra.Command{
144-
Use: "read [wallet file] [--full/-f] [--private/-p] [--base58]",
145-
Short: "Reads an existing wallet file",
148+
Use: "read [wallet file] [--full/-f] [--private/-p] [--parent] [--base58] [--hex]",
149+
DisableFlagsInUseLine: true,
150+
Short: "Reads an existing wallet file",
146151
Long: `This command can be used to verify whether an existing wallet file can be
147152
successfully read and decrypted, whether the password to open the file is correct, etc.
148153
It prints the accounts from the wallet file. By default it does not print private keys.
149154
Add --private to print private keys. Add --full to print full keys. Add --base58 to print
150-
keys in base58 format rather than hexadecimal. Add --parent to print parent key (and not
155+
keys in base58 format or --hex for hexdecimal rather than bech32. Add --parent to print parent key (and not
151156
only child keys).`,
152157
Args: cobra.ExactArgs(1),
153158
Run: func(cmd *cobra.Command, args []string) {
154-
walletFn := args[0]
155-
156-
// make sure the file exists
157-
f, err := os.Open(walletFn)
158-
cobra.CheckErr(err)
159-
defer f.Close()
160-
161-
// get the password
162-
fmt.Print("Enter wallet password: ")
163-
password, err := password.Read(os.Stdin)
164-
fmt.Println()
165-
cobra.CheckErr(err)
166-
167-
// attempt to read it
168-
wk := wallet.NewKey(wallet.WithPasswordOnly([]byte(password)))
169-
w, err := wk.Open(f, debug)
159+
w, err := internal.LoadWallet(args[0], debug)
170160
cobra.CheckErr(err)
171161

162+
caption := make([]string, 0, 2)
163+
maxWidth := 20
172164
widthEnforcer := func(col string, maxLen int) string {
173165
if len(col) <= maxLen {
174166
return col
@@ -179,55 +171,52 @@ only child keys).`,
179171
return fmt.Sprintf("%s..%s", col[:maxLen-7], col[len(col)-5:])
180172
}
181173

182-
t := table.NewWriter()
183-
t.SetOutputMirror(os.Stdout)
184-
t.SetTitle("Wallet Contents")
185-
caption := ""
186-
if printPrivate {
187-
caption = fmt.Sprintf("Mnemonic: %s", w.Mnemonic())
188-
}
189-
if !printFull {
190-
if printPrivate {
191-
caption += "\n"
192-
}
193-
caption += "To print full keys, use the --full flag."
194-
}
195-
t.SetCaption(caption)
196-
maxWidth := 20
174+
header := table.Row{"pubkey", "path", "name", "created"}
175+
197176
if printFull {
198177
// full key is 64 bytes which is 128 chars in hex, need to print at least this much
199178
maxWidth = 150
179+
} else {
180+
caption = append(caption, "To print full keys, use the --full flag.")
181+
}
182+
183+
colCfgs := []table.ColumnConfig{
184+
{Number: 1, WidthMax: maxWidth, WidthMaxEnforcer: widthEnforcer},
200185
}
186+
201187
// TODO: add spacemesh address format (bech32)
202188
// https://github.com/spacemeshos/smcli/issues/38
203189
if printPrivate {
204-
t.AppendHeader(table.Row{
205-
"pubkey",
206-
"privkey",
207-
"path",
208-
"name",
209-
"created",
210-
})
211-
t.SetColumnConfigs([]table.ColumnConfig{
212-
{Number: 1, WidthMax: maxWidth, WidthMaxEnforcer: widthEnforcer},
213-
{Number: 2, WidthMax: maxWidth, WidthMaxEnforcer: widthEnforcer},
214-
})
215-
} else {
216-
t.AppendHeader(table.Row{
217-
"pubkey",
218-
"path",
219-
"name",
220-
"created",
221-
})
222-
t.SetColumnConfigs([]table.ColumnConfig{
223-
{Number: 1, WidthMax: maxWidth, WidthMaxEnforcer: widthEnforcer},
190+
caption = append(caption, fmt.Sprintf("Mnemonic: %s", w.Mnemonic()))
191+
header = append(header[:2], header[1:]...)
192+
header[1] = "privkey"
193+
colCfgs = append(colCfgs, table.ColumnConfig{
194+
Number: 2, WidthMax: maxWidth, WidthMaxEnforcer: widthEnforcer,
224195
})
225196
}
226197

198+
t := table.NewWriter()
199+
t.SetOutputMirror(os.Stdout)
200+
t.SetTitle("Wallet Contents")
201+
t.SetCaption(strings.Join(caption, "\n"))
202+
t.AppendHeader(header)
203+
t.SetColumnConfigs(colCfgs)
204+
227205
// set the encoder
228-
encoder := hex.EncodeToString
229-
if printBase58 {
206+
var encoder func([]byte) string
207+
switch {
208+
case printBase58:
230209
encoder = base58.Encode
210+
case printHex:
211+
encoder = hex.EncodeToString
212+
default:
213+
encoder = func(data []byte) string {
214+
dataConverted, err := bech32.ConvertBits(data, 8, 5, true)
215+
cobra.CheckErr(err)
216+
encoded, err := bech32.Encode(types.NetworkHRP(), dataConverted)
217+
cobra.CheckErr(err)
218+
return encoded
219+
}
231220
}
232221

233222
privKeyEncoder := func(privKey []byte) string {
@@ -325,7 +314,8 @@ func init() {
325314
walletCmd.AddCommand(addrCmd)
326315
readCmd.Flags().BoolVarP(&printPrivate, "private", "p", false, "Print private keys")
327316
readCmd.Flags().BoolVarP(&printFull, "full", "f", false, "Print full keys (no abbreviation)")
328-
readCmd.Flags().BoolVar(&printBase58, "base58", false, "Print keys in base58 (rather than hex)")
317+
readCmd.Flags().BoolVar(&printBase58, "base58", false, "Print keys in base58 (rather than bech32)")
318+
readCmd.Flags().BoolVar(&printHex, "hex", false, "Print keys in hex (rather than bech32)")
329319
readCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent key (not only child keys)")
330320
readCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug mode")
331321
createCmd.Flags().BoolVarP(&useLedger, "ledger", "l", false, "Create a wallet using a Ledger device")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/btcsuite/btcutil v1.0.2
7+
github.com/cosmos/btcutil v1.0.5
78
github.com/jedib0t/go-pretty/v6 v6.4.6
89
github.com/spacemeshos/economics v0.1.0
910
github.com/spacemeshos/go-spacemesh v1.0.2
@@ -15,7 +16,6 @@ require (
1516
github.com/beorn7/perks v1.0.1 // indirect
1617
github.com/c0mm4nd/go-ripemd v0.0.0-20200326052756-bd1759ad7d10 // indirect
1718
github.com/cespare/xxhash/v2 v2.2.0 // indirect
18-
github.com/cosmos/btcutil v1.0.5 // indirect
1919
github.com/go-llsqlite/llsqlite v0.0.0-20230612031458-a9e271fe723a // indirect
2020
github.com/golang/mock v1.6.0 // indirect
2121
github.com/golang/protobuf v1.5.3 // indirect

0 commit comments

Comments
 (0)