Skip to content

Commit

Permalink
feat: add wallet address cmd
Browse files Browse the repository at this point in the history
The `wallet address` cmd lists all the addresses of a given wallet.

Related to spacemeshos#38
  • Loading branch information
0xjac committed Jul 13, 2023
1 parent 8662bd7 commit 9bc095c
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 8 deletions.
37 changes: 37 additions & 0 deletions cmd/internal/wallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package internal

import (
"fmt"
"os"

"github.com/hashicorp/go-secure-stdlib/password"

"github.com/spacemeshos/smcli/wallet"
)

// LoadWallet from a file, asks for the password from stdin.
func LoadWallet(path string, debug bool) (*wallet.Wallet, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}

defer f.Close()

fmt.Print("Enter wallet password: ")
pass, err := password.Read(os.Stdin)
fmt.Println()
if err != nil {
return nil, err
}

wk := wallet.NewKey(wallet.WithPasswordOnly([]byte(pass)))

w, err := wk.Open(f, debug)
if err != nil {
return nil, err
}

return w, nil

}
39 changes: 39 additions & 0 deletions cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/btcsuite/btcutil/base58"
"github.com/hashicorp/go-secure-stdlib/password"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spf13/cobra"

"github.com/spacemeshos/smcli/cmd/internal"
"github.com/spacemeshos/smcli/common"
"github.com/spacemeshos/smcli/wallet"
)
Expand Down Expand Up @@ -281,14 +283,51 @@ only child keys).`,
},
}

var addrCmd = &cobra.Command{
Use: "address [wallet file] [--parent]",
DisableFlagsInUseLine: true,
Short: "Show wallet addresses",
Long: "Show the addresses associated with the given wallet",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
w, err := internal.LoadWallet(args[0], debug)
cobra.CheckErr(err)

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.SetTitle("Wallet Addresses")
t.AppendHeader(table.Row{"address", "name"})

if printParent {
if master := w.Secrets.MasterKeypair; master != nil {
t.AppendRow(table.Row{
types.GenerateAddress(master.Public).String(),
master.DisplayName,
})
}
}

for _, account := range w.Secrets.Accounts {
t.AppendRow(table.Row{
types.GenerateAddress(account.Public).String(),
account.DisplayName,
})
}

t.Render()
},
}

func init() {
rootCmd.AddCommand(walletCmd)
walletCmd.AddCommand(createCmd)
walletCmd.AddCommand(readCmd)
walletCmd.AddCommand(addrCmd)
readCmd.Flags().BoolVarP(&printPrivate, "private", "p", false, "Print private keys")
readCmd.Flags().BoolVarP(&printFull, "full", "f", false, "Print full keys (no abbreviation)")
readCmd.Flags().BoolVar(&printBase58, "base58", false, "Print keys in base58 (rather than hex)")
readCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent key (not only child keys)")
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)")
}
31 changes: 28 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@ go 1.18
require (
github.com/btcsuite/btcutil v1.0.2
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/spacemeshos/go-spacemesh v0.3.13-beta.0
github.com/spacemeshos/smkeys v1.0.4
github.com/stretchr/testify v1.8.4
)

require (
github.com/c0mm4nd/go-ripemd v0.0.0-20200326052756-bd1759ad7d10 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/spacemeshos/go-scale v1.1.10 // indirect
github.com/spacemeshos/merkle-tree v0.2.2 // indirect
github.com/spacemeshos/poet v0.8.6 // indirect
github.com/spacemeshos/post v0.8.4 // indirect
github.com/spacemeshos/sha256-simd v0.1.0 // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/net v0.10.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand All @@ -31,10 +57,9 @@ require (
github.com/tyler-smith/go-bip39 v1.1.0
github.com/xdg-go/pbkdf2 v1.0.0
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
golang.org/x/text v0.10.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9bc095c

Please sign in to comment.