make magik happier

This commit is contained in:
whyrusleeping 2019-10-08 18:46:36 +09:00
parent b40de6995b
commit 80b40e7e70
5 changed files with 34 additions and 21 deletions

View File

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

View File

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

View File

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

View File

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

View File

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