lotus/cli/wallet.go

191 lines
3.4 KiB
Go
Raw Normal View History

2019-07-13 00:41:32 +00:00
package cli
import (
2019-10-08 09:17:03 +00:00
"encoding/hex"
2019-10-08 09:46:36 +00:00
"encoding/json"
2019-07-13 00:41:32 +00:00
"fmt"
2019-10-09 00:22:18 +00:00
"io/ioutil"
"os"
"strings"
2019-07-13 00:41:32 +00:00
2019-07-18 20:26:04 +00:00
"github.com/filecoin-project/go-lotus/chain/address"
2019-10-08 09:46:36 +00:00
types "github.com/filecoin-project/go-lotus/chain/types"
2019-07-13 00:41:32 +00:00
"gopkg.in/urfave/cli.v2"
)
var walletCmd = &cli.Command{
Name: "wallet",
Usage: "Manage wallet",
Subcommands: []*cli.Command{
walletNew,
walletList,
2019-07-18 20:26:04 +00:00
walletBalance,
2019-10-08 09:17:03 +00:00
walletExport,
walletImport,
2019-07-13 00:41:32 +00:00
},
}
var walletNew = &cli.Command{
Name: "new",
Usage: "Generate a new key of the given type",
ArgsUsage: "[bls|secp256k1]",
2019-07-13 00:41:32 +00:00
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
2019-07-13 00:41:32 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-07-18 23:16:23 +00:00
ctx := ReqContext(cctx)
2019-07-13 00:41:32 +00:00
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 {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
2019-07-13 00:41:32 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-07-18 23:16:23 +00:00
ctx := ReqContext(cctx)
2019-07-13 00:41:32 +00:00
addrs, err := api.WalletList(ctx)
if err != nil {
return err
}
for _, addr := range addrs {
fmt.Println(addr.String())
}
return nil
},
}
2019-07-18 20:26:04 +00:00
var walletBalance = &cli.Command{
Name: "balance",
Usage: "Get account balance",
ArgsUsage: "[account address]",
2019-07-18 20:26:04 +00:00
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
2019-07-18 20:26:04 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-07-18 23:16:23 +00:00
ctx := ReqContext(cctx)
2019-07-18 20:26:04 +00:00
var addr address.Address
if cctx.Args().First() != "" {
addr, err = address.NewFromString(cctx.Args().First())
} else {
addr, err = api.WalletDefaultAddress(ctx)
}
2019-07-18 20:26:04 +00:00
if err != nil {
return err
}
balance, err := api.WalletBalance(ctx, addr)
if err != nil {
return err
}
fmt.Printf("%s\n", balance.String())
return nil
},
}
2019-10-08 09:17:03 +00:00
var walletExport = &cli.Command{
Name: "export",
Usage: "export keys",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
if !cctx.Args().Present() {
return fmt.Errorf("must specify key to export")
}
addr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
2019-10-08 09:46:36 +00:00
ki, err := api.WalletExport(ctx, addr)
if err != nil {
return err
}
b, err := json.Marshal(ki)
2019-10-08 09:17:03 +00:00
if err != nil {
return err
}
fmt.Println(hex.EncodeToString(b))
return nil
},
}
var walletImport = &cli.Command{
Name: "import",
Usage: "import keys",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
2019-10-09 00:22:18 +00:00
var data []byte
if !cctx.Args().Present() || cctx.Args().First() == "-" {
indata, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
dec, err := hex.DecodeString(strings.TrimSpace(string(indata)))
if err != nil {
return err
}
data = dec
} else {
fdata, err := ioutil.ReadFile(cctx.Args().First())
if err != nil {
return err
}
data = fdata
2019-10-08 09:17:03 +00:00
}
2019-10-08 09:46:36 +00:00
var ki types.KeyInfo
if err := json.Unmarshal(data, &ki); err != nil {
return err
}
addr, err := api.WalletImport(ctx, &ki)
2019-10-08 09:17:03 +00:00
if err != nil {
return err
}
fmt.Printf("imported key %s successfully!", addr)
return nil
},
}