Merge pull request #28 from filecoin-project/feat/wallet-cmds

add wallet new and list commands
This commit is contained in:
Łukasz Magiera 2019-07-13 14:35:04 +02:00 committed by GitHub
commit 9102b03e50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 1 deletions

View File

@ -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

View File

@ -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{}

View File

@ -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:

View File

@ -64,4 +64,5 @@ var Commands = []*cli.Command{
versionCmd,
mpoolCmd,
minerCmd,
walletCmd,
}

64
cli/wallet.go Normal file
View File

@ -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
},
}

View File

@ -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)
}