|
42 | 42 |
|
43 | 43 | // useLedger indicates that the Ledger device should be used. |
44 | 44 | 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 |
45 | 49 | ) |
46 | 50 |
|
47 | 51 | // walletCmd represents the wallet command. |
@@ -145,15 +149,15 @@ sure the device is connected, unlocked, and the Spacemesh app is open.`, |
145 | 149 |
|
146 | 150 | // readCmd reads an existing wallet file. |
147 | 151 | 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]", |
149 | 153 | DisableFlagsInUseLine: true, |
150 | 154 | Short: "Reads an existing wallet file", |
151 | 155 | Long: `This command can be used to verify whether an existing wallet file can be |
152 | 156 | successfully read and decrypted, whether the password to open the file is correct, etc. |
153 | 157 | It prints the accounts from the wallet file. By default it does not print private keys. |
154 | 158 | Add --private to print private keys. Add --full to print full keys. Add --base58 to print |
155 | 159 | 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.`, |
157 | 161 | Args: cobra.ExactArgs(1), |
158 | 162 | Run: func(cmd *cobra.Command, args []string) { |
159 | 163 | w, err := internal.LoadWallet(args[0], debug) |
@@ -184,8 +188,11 @@ only child keys).`, |
184 | 188 | {Number: 1, WidthMax: maxWidth, WidthMaxEnforcer: widthEnforcer}, |
185 | 189 | } |
186 | 190 |
|
187 | | - // TODO: add spacemesh address format (bech32) |
188 | | - // https://github.com/spacemeshos/smcli/issues/38 |
| 191 | + if !noAddress { |
| 192 | + header = append(header[:3], header[2:]...) |
| 193 | + header[2] = "address" |
| 194 | + } |
| 195 | + |
189 | 196 | if printPrivate { |
190 | 197 | caption = append(caption, fmt.Sprintf("Mnemonic: %s", w.Mnemonic())) |
191 | 198 | header = append(header[:2], header[1:]...) |
@@ -219,55 +226,42 @@ only child keys).`, |
219 | 226 | } |
220 | 227 | } |
221 | 228 |
|
222 | | - privKeyEncoder := func(privKey []byte) string { |
223 | | - if len(privKey) == 0 { |
224 | | - return "(none)" |
| 229 | + addRow := func(account *wallet.EDKeyPair) { |
| 230 | + row := make([]any, 0, 6) // Row len is 4 w/o address, up to 6 w/ priv key. |
| 231 | + row = append(row, encoder(account.Public)) |
| 232 | + |
| 233 | + if printPrivate { |
| 234 | + privKey := "(none)" |
| 235 | + if len(account.Private) > 0 { |
| 236 | + privKey = encoder(account.Private) |
| 237 | + } |
| 238 | + |
| 239 | + row = append(row, privKey) |
225 | 240 | } |
226 | | - return encoder(privKey) |
| 241 | + |
| 242 | + row = append(row, account.Path.String()) |
| 243 | + |
| 244 | + if !noAddress { |
| 245 | + row = append(row, types.GenerateAddress(account.Public).String()) |
| 246 | + } |
| 247 | + |
| 248 | + row = append(row, account.DisplayName, account.Created) |
| 249 | + |
| 250 | + t.AppendRow(row) |
227 | 251 | } |
228 | 252 |
|
229 | 253 | // print the master account |
230 | 254 | if printParent { |
231 | | - master := w.Secrets.MasterKeypair |
232 | | - if master != nil { |
233 | | - if printPrivate { |
234 | | - t.AppendRow(table.Row{ |
235 | | - encoder(master.Public), |
236 | | - privKeyEncoder(master.Private), |
237 | | - master.Path.String(), |
238 | | - master.DisplayName, |
239 | | - master.Created, |
240 | | - }) |
241 | | - } else { |
242 | | - t.AppendRow(table.Row{ |
243 | | - encoder(master.Public), |
244 | | - master.Path.String(), |
245 | | - master.DisplayName, |
246 | | - master.Created, |
247 | | - }) |
248 | | - } |
| 255 | + if master := w.Secrets.MasterKeypair; master != nil { |
| 256 | + addRow(master) |
249 | 257 | } |
250 | 258 | } |
251 | 259 |
|
252 | 260 | // print child accounts |
253 | 261 | for _, a := range w.Secrets.Accounts { |
254 | | - if printPrivate { |
255 | | - t.AppendRow(table.Row{ |
256 | | - encoder(a.Public), |
257 | | - privKeyEncoder(a.Private), |
258 | | - a.Path.String(), |
259 | | - a.DisplayName, |
260 | | - a.Created, |
261 | | - }) |
262 | | - } else { |
263 | | - t.AppendRow(table.Row{ |
264 | | - encoder(a.Public), |
265 | | - a.Path.String(), |
266 | | - a.DisplayName, |
267 | | - a.Created, |
268 | | - }) |
269 | | - } |
| 262 | + addRow(a) |
270 | 263 | } |
| 264 | + |
271 | 265 | t.Render() |
272 | 266 | }, |
273 | 267 | } |
@@ -317,6 +311,7 @@ func init() { |
317 | 311 | readCmd.Flags().BoolVar(&printBase58, "base58", false, "Print keys in base58 (rather than bech32)") |
318 | 312 | readCmd.Flags().BoolVar(&printHex, "hex", false, "Print keys in hex (rather than bech32)") |
319 | 313 | readCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent key (not only child keys)") |
| 314 | + readCmd.Flags().BoolVar(&noAddress, "no-address", false, "Do not print the address associated with the key") |
320 | 315 | readCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug mode") |
321 | 316 | createCmd.Flags().BoolVarP(&useLedger, "ledger", "l", false, "Create a wallet using a Ledger device") |
322 | 317 | addrCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent address (not only child addresses)") |
|
0 commit comments