diff --git a/api/api.go b/api/api.go index 64ba41f69..26a28a405 100644 --- a/api/api.go +++ b/api/api.go @@ -89,8 +89,8 @@ type FullNode interface { WalletSign(context.Context, address.Address, []byte) (*types.Signature, error) WalletSignMessage(context.Context, address.Address, *types.Message) (*types.SignedMessage, error) WalletDefaultAddress(context.Context) (address.Address, error) - WalletExport(context.Context, address.Address) ([]byte, error) - WalletImport(context.Context, []byte) (address.Address, error) + WalletExport(context.Context, address.Address) (*types.KeyInfo, error) + WalletImport(context.Context, *types.KeyInfo) (address.Address, error) // Other diff --git a/api/struct.go b/api/struct.go index 1ea621942..49cd0b222 100644 --- a/api/struct.go +++ b/api/struct.go @@ -67,8 +67,8 @@ type FullNodeStruct struct { WalletSign func(context.Context, address.Address, []byte) (*types.Signature, error) `perm:"sign"` WalletSignMessage func(context.Context, address.Address, *types.Message) (*types.SignedMessage, error) `perm:"sign"` WalletDefaultAddress func(context.Context) (address.Address, error) `perm:"write"` - WalletExport func(context.Context, address.Address) ([]byte, error) `perm:"admin"` - WalletImport func(context.Context, []byte) (address.Address, error) `perm:"admin"` + WalletExport func(context.Context, address.Address) (*types.KeyInfo, error) `perm:"admin"` + WalletImport func(context.Context, *types.KeyInfo) (address.Address, error) `perm:"admin"` MpoolGetNonce func(context.Context, address.Address) (uint64, error) `perm:"read"` @@ -271,12 +271,12 @@ func (c *FullNodeStruct) WalletDefaultAddress(ctx context.Context) (address.Addr return c.Internal.WalletDefaultAddress(ctx) } -func (c *FullNodeStruct) WalletExport(ctx context.Context, a address.Address) ([]byte, error) { +func (c *FullNodeStruct) WalletExport(ctx context.Context, a address.Address) (*types.KeyInfo, error) { return c.Internal.WalletExport(ctx, a) } -func (c *FullNodeStruct) WalletImport(ctx context.Context, b []byte) (address.Address, error) { - return c.Internal.WalletImport(ctx, b) +func (c *FullNodeStruct) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) { + return c.Internal.WalletImport(ctx, ki) } func (c *FullNodeStruct) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) { diff --git a/chain/wallet/wallet.go b/chain/wallet/wallet.go index eee341672..3f3ee86c2 100644 --- a/chain/wallet/wallet.go +++ b/chain/wallet/wallet.go @@ -2,7 +2,6 @@ package wallet import ( "context" - "encoding/json" "sort" "strings" "sync" @@ -97,26 +96,28 @@ func (w *Wallet) findKey(addr address.Address) (*Key, error) { return k, nil } -func (w *Wallet) Export(addr address.Address) ([]byte, error) { +func (w *Wallet) Export(addr address.Address) (*types.KeyInfo, error) { k, err := w.findKey(addr) if err != nil { return nil, xerrors.Errorf("failed to find key to export: %w", err) } - return json.Marshal(k) + return &k.KeyInfo, nil } -func (w *Wallet) Import(kdata []byte) (address.Address, error) { - var ki types.KeyInfo - if err := json.Unmarshal(kdata, &ki); err != nil { - return address.Undef, xerrors.Errorf("failed to unmarshal input data: %w", err) - } +func (w *Wallet) Import(ki *types.KeyInfo) (address.Address, error) { + w.lk.Lock() + defer w.lk.Unlock() - k, err := NewKey(ki) + k, err := NewKey(*ki) if err != nil { return address.Undef, xerrors.Errorf("failed to make key: %w", err) } + if err := w.keystore.Put(KNamePrefix+k.Address.String(), k.KeyInfo); err != nil { + return address.Undef, xerrors.Errorf("saving to keystore: %w", err) + } + return k.Address, nil } diff --git a/cli/wallet.go b/cli/wallet.go index bbbf931a6..07d0c0b99 100644 --- a/cli/wallet.go +++ b/cli/wallet.go @@ -2,9 +2,11 @@ package cli import ( "encoding/hex" + "encoding/json" "fmt" "github.com/filecoin-project/go-lotus/chain/address" + types "github.com/filecoin-project/go-lotus/chain/types" "gopkg.in/urfave/cli.v2" ) @@ -121,7 +123,12 @@ var walletExport = &cli.Command{ return err } - b, err := api.WalletExport(ctx, addr) + ki, err := api.WalletExport(ctx, addr) + if err != nil { + return err + } + + b, err := json.Marshal(ki) if err != nil { return err } @@ -151,7 +158,12 @@ var walletImport = &cli.Command{ return err } - addr, err := api.WalletImport(ctx, data) + var ki types.KeyInfo + if err := json.Unmarshal(data, &ki); err != nil { + return err + } + + addr, err := api.WalletImport(ctx, &ki) if err != nil { return err } diff --git a/node/impl/full/wallet.go b/node/impl/full/wallet.go index 90c740351..338159bd8 100644 --- a/node/impl/full/wallet.go +++ b/node/impl/full/wallet.go @@ -69,10 +69,10 @@ func (a *WalletAPI) WalletDefaultAddress(ctx context.Context) (address.Address, return addrs[0], nil } -func (a *WalletAPI) WalletExport(ctx context.Context, addr address.Address) ([]byte, error) { +func (a *WalletAPI) WalletExport(ctx context.Context, addr address.Address) (*types.KeyInfo, error) { return a.Wallet.Export(addr) } -func (a *WalletAPI) WalletImport(ctx context.Context, b []byte) (address.Address, error) { - return a.Wallet.Import(b) +func (a *WalletAPI) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) { + return a.Wallet.Import(ki) }