Merge pull request #1492 from filecoin-project/feat/wallet-import-gfc

allow wallet import to import keys in go-filecoin key format
This commit is contained in:
Łukasz Magiera 2020-04-02 00:04:22 +02:00 committed by GitHub
commit 67a47f428f

View File

@ -13,6 +13,7 @@ import (
types "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/specs-actors/actors/crypto"
"golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2"
)
@ -202,6 +203,13 @@ var walletImport = &cli.Command{
Name: "import",
Usage: "import keys",
ArgsUsage: "[<path> (optional, will read from stdin if omitted)]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Usage: "specify input format for key",
Value: "hex-lotus",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
@ -210,7 +218,7 @@ var walletImport = &cli.Command{
defer closer()
ctx := ReqContext(cctx)
var hexdata []byte
var inpdata []byte
if !cctx.Args().Present() || cctx.Args().First() == "-" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter private key: ")
@ -218,24 +226,55 @@ var walletImport = &cli.Command{
if err != nil {
return err
}
hexdata = indata
inpdata = indata
} else {
fdata, err := ioutil.ReadFile(cctx.Args().First())
if err != nil {
return err
}
hexdata = fdata
inpdata = fdata
}
data, err := hex.DecodeString(strings.TrimSpace(string(hexdata)))
var ki types.KeyInfo
switch cctx.String("format") {
case "hex-lotus":
data, err := hex.DecodeString(strings.TrimSpace(string(inpdata)))
if err != nil {
return err
}
var ki types.KeyInfo
if err := json.Unmarshal(data, &ki); err != nil {
return err
}
case "json-lotus":
if err := json.Unmarshal(inpdata, &ki); err != nil {
return err
}
case "gfc-json":
var f struct {
KeyInfo []struct {
PrivateKey []byte
SigType int
}
}
if err := json.Unmarshal(inpdata, &f); err != nil {
return xerrors.Errorf("failed to parse go-filecoin key: %s", err)
}
gk := f.KeyInfo[0]
ki.PrivateKey = gk.PrivateKey
switch gk.SigType {
case 1:
ki.Type = wallet.KTSecp256k1
case 2:
ki.Type = wallet.KTBLS
default:
return fmt.Errorf("unrecognized key type: %d", gk.SigType)
}
default:
return fmt.Errorf("unrecognized format: %s", cctx.String("format"))
}
addr, err := api.WalletImport(ctx, &ki)
if err != nil {