Skip to content

Commit

Permalink
Remote wallet backends
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Sep 5, 2020
1 parent f90cfda commit 780f6db
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/lotus-fountain
/lotus-stats
/lotus-bench
/lotus-wallet
/lotus-pcr
/bench.json
/lotuspond/front/node_modules
/lotuspond/front/build
Expand Down
10 changes: 2 additions & 8 deletions chain/wallet/interface.go → api/api_wallet.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package wallet
package api

import (
"context"

"github.com/filecoin-project/go-address"

"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/specs-actors/actors/crypto"
)

type Wallet interface {
type WalletAPI interface {
WalletNew(context.Context, crypto.SigType) (address.Address, error)
WalletHas(context.Context, address.Address) (bool, error)
WalletList(context.Context) ([]address.Address, error)
Expand All @@ -20,8 +19,3 @@ type Wallet interface {
WalletImport(context.Context, *types.KeyInfo) (address.Address, error)
WalletDelete(context.Context, address.Address) error
}

type Default interface {
GetDefault() (address.Address, error)
SetDefault(a address.Address) error
}
6 changes: 6 additions & 0 deletions api/apistruct/permissioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ func PermissionedWorkerAPI(a api.WorkerAPI) api.WorkerAPI {
auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.Internal)
return &out
}

func PermissionedWalletAPI(a api.WalletAPI) api.WalletAPI {
var out WalletStruct
auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.Internal)
return &out
}
46 changes: 46 additions & 0 deletions api/apistruct/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,19 @@ type WorkerStruct struct {
}
}

type WalletStruct struct {
Internal struct {
WalletNew func(context.Context, crypto.SigType) (address.Address, error) `perm:"write"`
WalletHas func(context.Context, address.Address) (bool, error) `perm:"write"`
WalletList func(context.Context) ([]address.Address, error) `perm:"write"`
WalletSign func(context.Context, address.Address, []byte) (*crypto.Signature, error) `perm:"sign"`
WalletSignMessage func(context.Context, address.Address, *types.Message) (*types.SignedMessage, error) `perm:"sign"`
WalletExport func(context.Context, address.Address) (*types.KeyInfo, error) `perm:"admin"`
WalletImport func(context.Context, *types.KeyInfo) (address.Address, error) `perm:"admin"`
WalletDelete func(context.Context, address.Address) error `perm:"write"`
}
}

// CommonStruct

func (c *CommonStruct) AuthVerify(ctx context.Context, token string) ([]auth.Permission, error) {
Expand Down Expand Up @@ -1271,7 +1284,40 @@ func (w *WorkerStruct) Closing(ctx context.Context) (<-chan struct{}, error) {
return w.Internal.Closing(ctx)
}

func (c *WalletStruct) WalletNew(ctx context.Context, typ crypto.SigType) (address.Address, error) {
return c.Internal.WalletNew(ctx, typ)
}

func (c *WalletStruct) WalletHas(ctx context.Context, addr address.Address) (bool, error) {
return c.Internal.WalletHas(ctx, addr)
}

func (c *WalletStruct) WalletList(ctx context.Context) ([]address.Address, error) {
return c.Internal.WalletList(ctx)
}

func (c *WalletStruct) WalletSign(ctx context.Context, k address.Address, msg []byte) (*crypto.Signature, error) {
return c.Internal.WalletSign(ctx, k, msg)
}

func (c *WalletStruct) WalletSignMessage(ctx context.Context, k address.Address, msg *types.Message) (*types.SignedMessage, error) {
return c.Internal.WalletSignMessage(ctx, k, msg)
}

func (c *WalletStruct) WalletExport(ctx context.Context, a address.Address) (*types.KeyInfo, error) {
return c.Internal.WalletExport(ctx, a)
}

func (c *WalletStruct) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) {
return c.Internal.WalletImport(ctx, ki)
}

func (c *WalletStruct) WalletDelete(ctx context.Context, addr address.Address) error {
return c.Internal.WalletDelete(ctx, addr)
}

var _ api.Common = &CommonStruct{}
var _ api.FullNode = &FullNodeStruct{}
var _ api.StorageMiner = &StorageMinerStruct{}
var _ api.WorkerAPI = &WorkerStruct{}
var _ api.WalletAPI = &WalletStruct{}
12 changes: 12 additions & 0 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ func NewWorkerRPC(ctx context.Context, addr string, requestHeader http.Header) (

return &res, closer, err
}

func NewWalletRPC(ctx context.Context, addr string, requestHeader http.Header) (api.WalletAPI, jsonrpc.ClientCloser, error) {
var res apistruct.WalletStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{
&res.Internal,
},
requestHeader,
)

return &res, closer, err
}
51 changes: 51 additions & 0 deletions chain/wallet/remotewallet/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package remotewallet

import (
"context"
"go.uber.org/fx"
"golang.org/x/xerrors"
"net/http"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client"
"github.com/filecoin-project/lotus/node/modules/helpers"
)

type RemoteWallet struct {
api.WalletAPI
}

func SetupRemoteWallet(url string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle) (*RemoteWallet, error) {
/*sp := strings.SplitN(env, ":", 2)
if len(sp) != 2 {
log.Warnf("invalid env(%s) value, missing token or address", envKey)
} else {
ma, err := multiaddr.NewMultiaddr(sp[1])
if err != nil {
return APIInfo{}, xerrors.Errorf("could not parse multiaddr from env(%s): %w", envKey, err)
}
return APIInfo{
Addr: ma,
Token: []byte(sp[0]),
}, nil
}*/

headers := http.Header{}
/*headers.Add("Authorization", "Bearer "+token)*/

wapi, closer, err := client.NewWalletRPC(mctx, url, headers)
if err != nil {
return nil, xerrors.Errorf("creating jsonrpc client: %w", err)
}

lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
closer()
return nil
},
})

return &RemoteWallet{wapi}, nil
}
}
8 changes: 7 additions & 1 deletion chain/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/filecoin-project/go-address"

"github.com/filecoin-project/lotus/api"
_ "github.com/filecoin-project/lotus/lib/sigs/bls" // enable bls signatures
_ "github.com/filecoin-project/lotus/lib/sigs/secp" // enable secp signatures

Expand All @@ -36,6 +37,11 @@ type LocalWallet struct {
lk sync.Mutex
}

type Default interface {
GetDefault() (address.Address, error)
SetDefault(a address.Address) error
}

func NewWallet(keystore types.KeyStore) (*LocalWallet, error) {
w := &LocalWallet{
keys: make(map[address.Address]*Key),
Expand Down Expand Up @@ -250,4 +256,4 @@ func (w *LocalWallet) WalletDelete(ctx context.Context, addr address.Address) er
return nil
}

var _ Wallet = &LocalWallet{}
var _ api.WalletAPI = &LocalWallet{}
68 changes: 68 additions & 0 deletions cmd/lotus-wallet/logged.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"context"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/specs-actors/actors/crypto"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types"
)

type LoggedWallet struct {
under api.WalletAPI
}

func (c *LoggedWallet) WalletNew(ctx context.Context, typ crypto.SigType) (address.Address, error) {
n, err := typ.Name()
if err != nil {
return address.Address{}, err
}

log.Infow("WalletNew", "type", n)

return c.under.WalletNew(ctx, typ)
}

func (c *LoggedWallet) WalletHas(ctx context.Context, addr address.Address) (bool, error) {
log.Infow("WalletHas", "address", addr)

return c.under.WalletHas(ctx, addr)
}

func (c *LoggedWallet) WalletList(ctx context.Context) ([]address.Address, error) {
log.Infow("WalletList")

return c.under.WalletList(ctx)
}

func (c *LoggedWallet) WalletSign(ctx context.Context, k address.Address, msg []byte) (*crypto.Signature, error) {
log.Infow("WalletSign", "address", k)

return c.under.WalletSign(ctx, k, msg)
}

func (c *LoggedWallet) WalletSignMessage(ctx context.Context, k address.Address, msg *types.Message) (*types.SignedMessage, error) {
log.Infow("WalletSignMessage", "address", k)

return c.under.WalletSignMessage(ctx, k, msg)
}

func (c *LoggedWallet) WalletExport(ctx context.Context, a address.Address) (*types.KeyInfo, error) {
log.Infow("WalletExport", "address", a)

return c.under.WalletExport(ctx, a)
}

func (c *LoggedWallet) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) {
log.Infow("WalletImport", "type", ki.Type)

return c.under.WalletImport(ctx, ki)
}

func (c *LoggedWallet) WalletDelete(ctx context.Context, addr address.Address) error {
log.Infow("WalletDelete", "address", addr)

return c.under.WalletDelete(ctx, addr)
}
Loading

0 comments on commit 780f6db

Please sign in to comment.