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) WalletSign(context.Context, address.Address, []byte) (*types.Signature, error)
WalletSignMessage(context.Context, address.Address, *types.Message) (*types.SignedMessage, error) WalletSignMessage(context.Context, address.Address, *types.Message) (*types.SignedMessage, error)
WalletDefaultAddress(context.Context) (address.Address, error) WalletDefaultAddress(context.Context) (address.Address, error)
WalletExport(context.Context, address.Address) ([]byte, error) WalletExport(context.Context, address.Address) (*types.KeyInfo, error)
WalletImport(context.Context, []byte) (address.Address, error) WalletImport(context.Context, *types.KeyInfo) (address.Address, error)
// Other // Other

View File

@ -67,8 +67,8 @@ type FullNodeStruct struct {
WalletSign func(context.Context, address.Address, []byte) (*types.Signature, error) `perm:"sign"` 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"` WalletSignMessage func(context.Context, address.Address, *types.Message) (*types.SignedMessage, error) `perm:"sign"`
WalletDefaultAddress func(context.Context) (address.Address, error) `perm:"write"` WalletDefaultAddress func(context.Context) (address.Address, error) `perm:"write"`
WalletExport func(context.Context, address.Address) ([]byte, error) `perm:"admin"` WalletExport func(context.Context, address.Address) (*types.KeyInfo, error) `perm:"admin"`
WalletImport func(context.Context, []byte) (address.Address, error) `perm:"admin"` WalletImport func(context.Context, *types.KeyInfo) (address.Address, error) `perm:"admin"`
MpoolGetNonce func(context.Context, address.Address) (uint64, error) `perm:"read"` 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) 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) return c.Internal.WalletExport(ctx, a)
} }
func (c *FullNodeStruct) WalletImport(ctx context.Context, b []byte) (address.Address, error) { func (c *FullNodeStruct) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) {
return c.Internal.WalletImport(ctx, b) return c.Internal.WalletImport(ctx, ki)
} }
func (c *FullNodeStruct) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) { func (c *FullNodeStruct) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) {

View File

@ -2,7 +2,6 @@ package wallet
import ( import (
"context" "context"
"encoding/json"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@ -97,26 +96,28 @@ func (w *Wallet) findKey(addr address.Address) (*Key, error) {
return k, nil 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) k, err := w.findKey(addr)
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to find key to export: %w", err) 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) { func (w *Wallet) Import(ki *types.KeyInfo) (address.Address, error) {
var ki types.KeyInfo w.lk.Lock()
if err := json.Unmarshal(kdata, &ki); err != nil { defer w.lk.Unlock()
return address.Undef, xerrors.Errorf("failed to unmarshal input data: %w", err)
}
k, err := NewKey(ki) k, err := NewKey(*ki)
if err != nil { if err != nil {
return address.Undef, xerrors.Errorf("failed to make key: %w", err) 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 return k.Address, nil
} }

View File

@ -2,9 +2,11 @@ package cli
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/address"
types "github.com/filecoin-project/go-lotus/chain/types"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
) )
@ -121,7 +123,12 @@ var walletExport = &cli.Command{
return err 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 { if err != nil {
return err return err
} }
@ -151,7 +158,12 @@ var walletImport = &cli.Command{
return err 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 { if err != nil {
return err return err
} }

View File

@ -69,10 +69,10 @@ func (a *WalletAPI) WalletDefaultAddress(ctx context.Context) (address.Address,
return addrs[0], nil 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) return a.Wallet.Export(addr)
} }
func (a *WalletAPI) WalletImport(ctx context.Context, b []byte) (address.Address, error) { func (a *WalletAPI) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) {
return a.Wallet.Import(b) return a.Wallet.Import(ki)
} }