@@ -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"
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,8 +145,9 @@ sure the device is connected, unlocked, and the Spacemesh app is open.`,
141145
142146// readCmd reads an existing wallet file.
143147var 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
147152successfully read and decrypted, whether the password to open the file is correct, etc.
148153It prints the accounts from the wallet file. By default it does not print private keys.
@@ -151,24 +156,11 @@ keys in base58 format rather than hexadecimal. Add --parent to print parent key
151156only 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,51 @@ 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
197- if printFull {
198- // full key is 64 bytes which is 128 chars in hex, need to print at least this much
199- maxWidth = 150
174+ header := table.Row {"pubkey" , "path" , "name" , "created" }
175+ colCfgs := []table.ColumnConfig {
176+ {Number : 1 , WidthMax : maxWidth , WidthMaxEnforcer : widthEnforcer },
200177 }
178+
201179 // TODO: add spacemesh address format (bech32)
202180 // https://github.com/spacemeshos/smcli/issues/38
203181 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 },
182+ caption = append (caption , fmt .Sprintf ("Mnemonic: %s" , w .Mnemonic ()))
183+ header = append (header [:2 ], header [1 :]... )
184+ header [1 ] = "privkey"
185+ colCfgs = append (colCfgs , table.ColumnConfig {
186+ Number : 2 , WidthMax : maxWidth , WidthMaxEnforcer : widthEnforcer ,
214187 })
188+ }
189+
190+ if printFull {
191+ // full key is 64 bytes which is 128 chars in hex, need to print at least this much
192+ maxWidth = 150
215193 } 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 },
224- })
194+ caption = append (caption , "To print full keys, use the --full flag." )
225195 }
226196
197+ t := table .NewWriter ()
198+ t .SetOutputMirror (os .Stdout )
199+ t .SetTitle ("Wallet Contents" )
200+ t .SetCaption (strings .Join (caption , "\n " ))
201+ t .AppendHeader (header )
202+ t .SetColumnConfigs (colCfgs )
203+
227204 // set the encoder
228- encoder := hex .EncodeToString
229- if printBase58 {
205+ var encoder func ([]byte ) string
206+ switch {
207+ case printBase58 :
230208 encoder = base58 .Encode
209+ case printHex :
210+ encoder = hex .EncodeToString
211+ default :
212+ encoder = func (data []byte ) string {
213+ dataConverted , err := bech32 .ConvertBits (data , 8 , 5 , true )
214+ cobra .CheckErr (err )
215+ encoded , err := bech32 .Encode (types .NetworkHRP (), dataConverted )
216+ cobra .CheckErr (err )
217+ return encoded
218+ }
231219 }
232220
233221 privKeyEncoder := func (privKey []byte ) string {
@@ -325,7 +313,8 @@ func init() {
325313 walletCmd .AddCommand (addrCmd )
326314 readCmd .Flags ().BoolVarP (& printPrivate , "private" , "p" , false , "Print private keys" )
327315 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)" )
316+ readCmd .Flags ().BoolVar (& printBase58 , "base58" , false , "Print keys in base58 (rather than bech32)" )
317+ readCmd .Flags ().BoolVar (& printHex , "hex" , false , "Print keys in hex (rather than bech32)" )
329318 readCmd .Flags ().BoolVar (& printParent , "parent" , false , "Print parent key (not only child keys)" )
330319 readCmd .PersistentFlags ().BoolVarP (& debug , "debug" , "d" , false , "enable debug mode" )
331320 createCmd .Flags ().BoolVarP (& useLedger , "ledger" , "l" , false , "Create a wallet using a Ledger device" )
0 commit comments