diff --git a/api/api.go b/api/api.go index 91555f7cc..5ab48fd59 100644 --- a/api/api.go +++ b/api/api.go @@ -61,9 +61,10 @@ type API interface { // wallet + WalletNew(context.Context, string) (address.Address, error) + WalletList(context.Context) ([]address.Address, error) // // import // // export - // // list // // (on cli - cmd to list associations) // dht diff --git a/api/struct.go b/api/struct.go index 4a8e8662c..e2e83ce68 100644 --- a/api/struct.go +++ b/api/struct.go @@ -24,6 +24,9 @@ type Struct struct { MinerStart func(context.Context, address.Address) error MinerCreateBlock func(context.Context, address.Address, *chain.TipSet, []chain.Ticket, chain.ElectionProof, []*chain.SignedMessage) (*chain.BlockMsg, error) + WalletNew func(context.Context, string) (address.Address, error) + WalletList func(context.Context) ([]address.Address, error) + NetPeers func(context.Context) ([]peer.AddrInfo, error) NetConnect func(context.Context, peer.AddrInfo) error NetAddrsListen func(context.Context) (peer.AddrInfo, error) @@ -76,4 +79,12 @@ func (c *Struct) Version(ctx context.Context) (Version, error) { return c.Internal.Version(ctx) } +func (c *Struct) WalletNew(ctx context.Context, typ string) (address.Address, error) { + return c.Internal.WalletNew(ctx, typ) +} + +func (c *Struct) WalletList(ctx context.Context) ([]address.Address, error) { + return c.Internal.WalletList(ctx) +} + var _ API = &Struct{} diff --git a/chain/wallet.go b/chain/wallet.go index 96fc4f924..9ef260203 100644 --- a/chain/wallet.go +++ b/chain/wallet.go @@ -3,6 +3,7 @@ package chain import ( "encoding/binary" "fmt" + "sort" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/lib/bls-signatures" @@ -133,6 +134,17 @@ func (w *Wallet) Import(kdata []byte) (address.Address, error) { panic("nyi") } +func (w *Wallet) ListAddrs() []address.Address { + var out []address.Address + for a := range w.keys { + out = append(out, a) + } + sort.Slice(out, func(i, j int) bool { + return out[i].String() < out[j].String() + }) + return out +} + func (w *Wallet) GenerateKey(typ string) (address.Address, error) { switch typ { case KTSecp256k1: diff --git a/cli/cmd.go b/cli/cmd.go index 414312e67..ea548076e 100644 --- a/cli/cmd.go +++ b/cli/cmd.go @@ -64,4 +64,5 @@ var Commands = []*cli.Command{ versionCmd, mpoolCmd, minerCmd, + walletCmd, } diff --git a/cli/wallet.go b/cli/wallet.go new file mode 100644 index 000000000..ab8e3f394 --- /dev/null +++ b/cli/wallet.go @@ -0,0 +1,64 @@ +package cli + +import ( + "fmt" + + "gopkg.in/urfave/cli.v2" +) + +var walletCmd = &cli.Command{ + Name: "wallet", + Usage: "Manage wallet", + Subcommands: []*cli.Command{ + walletNew, + walletList, + }, +} + +var walletNew = &cli.Command{ + Name: "new", + Usage: "Generate a new key of the given type (bls or secp256k1)", + Action: func(cctx *cli.Context) error { + api, err := getAPI(cctx) + if err != nil { + return err + } + ctx := reqContext(cctx) + + t := cctx.Args().First() + if t == "" { + t = "bls" + } + + nk, err := api.WalletNew(ctx, t) + if err != nil { + return err + } + + fmt.Println(nk.String()) + + return nil + }, +} + +var walletList = &cli.Command{ + Name: "list", + Usage: "List wallet address", + Action: func(cctx *cli.Context) error { + api, err := getAPI(cctx) + if err != nil { + return err + } + ctx := reqContext(cctx) + + addrs, err := api.WalletList(ctx) + if err != nil { + return err + } + + for _, addr := range addrs { + fmt.Println(addr.String()) + } + return nil + }, +} diff --git a/node/api.go b/node/api.go index 7736c5d2a..c0066dde1 100644 --- a/node/api.go +++ b/node/api.go @@ -20,6 +20,7 @@ type API struct { Chain *chain.ChainStore PubSub *pubsub.PubSub Mpool *chain.MessagePool + Wallet *chain.Wallet } func (a *API) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error { @@ -101,6 +102,14 @@ func (a *API) NetPeers(context.Context) ([]peer.AddrInfo, error) { return out, nil } +func (a *API) WalletNew(ctx context.Context, typ string) (address.Address, error) { + return a.Wallet.GenerateKey(typ) +} + +func (a *API) WalletList(ctx context.Context) ([]address.Address, error) { + return a.Wallet.ListAddrs(), nil +} + func (a *API) NetConnect(ctx context.Context, p peer.AddrInfo) error { return a.Host.Connect(ctx, p) }