Skip to content

Commit

Permalink
feat: add delete wallet address cmd (#5326)
Browse files Browse the repository at this point in the history
Co-authored-by: 一页素书 <2931107265@qq.com>
  • Loading branch information
diwufeiwen and ta0li authored Sep 26, 2022
1 parent 4209f7f commit 93f8066
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 13 deletions.
17 changes: 11 additions & 6 deletions app/submodule/wallet/remotewallet/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type remoteWallet struct {
}

func (w *remoteWallet) Addresses(ctx context.Context) []address.Address {
wallets, err := w.IWallet.WalletList(context.Background())
wallets, err := w.IWallet.WalletList(ctx)
if err != nil {
return make([]address.Address, 0)
}
Expand Down Expand Up @@ -51,28 +51,33 @@ func SetupRemoteWallet(info string) (wallet.WalletIntersection, error) {
}

func (w *remoteWallet) HasAddress(ctx context.Context, addr address.Address) bool {
exist, err := w.IWallet.WalletHas(context.Background(), addr)
exist, err := w.IWallet.WalletHas(ctx, addr)
if err != nil {
return false
}
return exist
}

func (w *remoteWallet) NewAddress(ctx context.Context, protocol address.Protocol) (address.Address, error) {
return w.IWallet.WalletNew(context.Background(), GetKeyType(protocol))
return w.IWallet.WalletNew(ctx, GetKeyType(protocol))
}

func (w *remoteWallet) DeleteAddress(ctx context.Context, addr address.Address) error {
return w.IWallet.WalletDelete(ctx, addr)
}

func (w *remoteWallet) Import(ctx context.Context, key *crypto.KeyInfo) (address.Address, error) {
return w.IWallet.WalletImport(context.Background(), ConvertRemoteKeyInfo(key))
return w.IWallet.WalletImport(ctx, ConvertRemoteKeyInfo(key))
}

func (w *remoteWallet) Export(ctx context.Context, addr address.Address, password string) (*crypto.KeyInfo, error) {
key, err := w.IWallet.WalletExport(context.Background(), addr)
key, err := w.IWallet.WalletExport(ctx, addr)
if err != nil {
return nil, err
}
return ConvertLocalKeyInfo(key), nil
}

func (w *remoteWallet) WalletSign(ctx context.Context, keyAddr address.Address, msg []byte, meta types.MsgMeta) (*crypto.Signature, error) {
return w.IWallet.WalletSign(context.Background(), keyAddr, msg, meta)
return w.IWallet.WalletSign(ctx, keyAddr, msg, meta)
}
5 changes: 5 additions & 0 deletions app/submodule/wallet/wallet_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ func (walletAPI *WalletAPI) WalletExport(ctx context.Context, addr address.Addre
return remotewallet.ConvertRemoteKeyInfo(ki), nil
}

// WalletDelete delete the given walletModule address
func (walletAPI *WalletAPI) WalletDelete(ctx context.Context, addr address.Address) error {
return walletAPI.adapter.DeleteAddress(ctx, addr)
}

// WalletSign signs the given bytes using the given address.
func (walletAPI *WalletAPI) WalletSign(ctx context.Context, k address.Address, msg []byte, meta types.MsgMeta) (*crypto.Signature, error) {
keyAddr, err := walletAPI.walletModule.Chain.Stmgr.ResolveToKeyAddress(ctx, k, nil)
Expand Down
38 changes: 37 additions & 1 deletion cmd/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"os"
"strings"

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

cmds "github.com/ipfs/go-ipfs-cmds"
files "github.com/ipfs/go-ipfs-files"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"

"github.com/filecoin-project/venus/app/node"
"github.com/filecoin-project/venus/cmd/tablewriter"
"github.com/filecoin-project/venus/pkg/crypto"
Expand All @@ -37,6 +39,7 @@ var walletCmd = &cmds.Command{
"ls": addrsLsCmd,
"new": addrsNewCmd,
"default": defaultAddressCmd,
"delete": addrsDeleteCmd,
"set-default": setDefaultAddressCmd,
"lock": lockedCmd,
"unlock": unlockedCmd,
Expand Down Expand Up @@ -85,6 +88,39 @@ var addrsNewCmd = &cmds.Command{
},
}

var addrsDeleteCmd = &cmds.Command{
Arguments: []cmds.Argument{
cmds.StringArg("address", true, false, "wallet address"),
},
Options: []cmds.Option{
cmds.BoolOption("really-do-it", "Actually send transaction performing the action").WithDefault(false),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
if really := req.Options["really-do-it"].(bool); !really {
return fmt.Errorf("pass --really-do-it to actually execute this action")
}

addr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}

if !env.(*node.Env).WalletAPI.HasPassword(req.Context) {
return errMissPassword
}
if env.(*node.Env).WalletAPI.WalletState(req.Context) == wallet.Lock {
return errWalletLocked
}

err = env.(*node.Env).WalletAPI.WalletDelete(req.Context, addr)
if err != nil {
return err
}

return printOneString(re, "Delete successfully!")
},
}

var addrsLsCmd = &cmds.Command{
Options: []cmds.Option{
cmds.BoolOption("addr-only", "Only print addresses"),
Expand Down
2 changes: 2 additions & 0 deletions pkg/wallet/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Backend interface {
// Contains returns true if this backend stores the passed in address.
HasAddress(context.Context, address.Address) bool

DeleteAddress(context.Context, address.Address) error

// Sign cryptographically signs data with the private key associated with an address.
SignBytes(context.Context, []byte, address.Address) (*crypto.Signature, error)

Expand Down
16 changes: 16 additions & 0 deletions pkg/wallet/dsbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ func (backend *DSBackend) putKeyInfo(ctx context.Context, ki *crypto.KeyInfo) er
return nil
}

func (backend *DSBackend) DeleteAddress(ctx context.Context, addr address.Address) error {
backend.lk.RLock()
defer backend.lk.RUnlock()

if _, ok := backend.cache[addr]; ok {
err := backend.ds.Delete(ctx, ds.NewKey(addr.String()))
if err != nil {
return err
}
delete(backend.cache, addr)
return nil
}

return errors.New("backend does not contain address")
}

// SignBytes cryptographically signs `data` using the private key `priv`.
func (backend *DSBackend) SignBytes(ctx context.Context, data []byte, addr address.Address) (*crypto.Signature, error) {
backend.lk.Lock()
Expand Down
5 changes: 5 additions & 0 deletions pkg/wallet/dsbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func TestDSBackendSimple(t *testing.T) {
assert.NoError(t, err)

assert.True(t, fs2.HasAddress(ctx, addr))

t.Log("delete the address")
err = fs2.DeleteAddress(ctx, addr)
assert.NoError(t, err)
assert.False(t, fs2.HasAddress(ctx, addr))
}

func TestDSBackendKeyPairMatchAddress(t *testing.T) {
Expand Down
24 changes: 22 additions & 2 deletions pkg/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ var walletLog = logging.Logger("wallet")
// WalletIntersection
// nolint
type WalletIntersection interface {
HasAddress(ctx context.Context, a address.Address) bool
HasAddress(ctx context.Context, addr address.Address) bool
Addresses(ctx context.Context) []address.Address
NewAddress(ctx context.Context, p address.Protocol) (address.Address, error)
NewAddress(ctx context.Context, protocol address.Protocol) (address.Address, error)
DeleteAddress(ctx context.Context, addr address.Address) error
Import(ctx context.Context, ki *crypto.KeyInfo) (address.Address, error)
Export(ctx context.Context, addr address.Address, password string) (*crypto.KeyInfo, error)
WalletSign(ctx context.Context, keyAddr address.Address, msg []byte, meta types.MsgMeta) (*crypto.Signature, error)
Expand Down Expand Up @@ -133,6 +134,25 @@ func (w *Wallet) NewAddress(ctx context.Context, p address.Protocol) (address.Ad
return backend.NewAddress(ctx, p)
}

// DeleteAddress delete the given address from stored.
func (w *Wallet) DeleteAddress(ctx context.Context, addr address.Address) error {
w.lk.Lock()
defer w.lk.Unlock()

for _, backends := range w.backends {
for _, backend := range backends {
if backend.HasAddress(ctx, addr) {
err := backend.DeleteAddress(ctx, addr)
if err != nil {
return err
}
}
}
}

return nil
}

// GetPubKeyForAddress returns the public key in the keystore associated with
// the given address.
func (w *Wallet) GetPubKeyForAddress(ctx context.Context, addr address.Address) ([]byte, error) {
Expand Down
12 changes: 10 additions & 2 deletions pkg/wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestWalletSimple(t *testing.T) {
w, fs := newWalletAndDSBackend(t)

t.Log("create a new address in the backend")
addr, err := fs.NewAddress(context.Background(), address.SECP256K1)
addr, err := fs.NewAddress(ctx, address.SECP256K1)
assert.NoError(t, err)

t.Log("test HasAddress")
Expand All @@ -62,7 +62,7 @@ func TestWalletSimple(t *testing.T) {
assert.Equal(t, list[0], addr)

t.Log("addresses are sorted")
addr2, err := fs.NewAddress(context.Background(), address.SECP256K1)
addr2, err := fs.NewAddress(ctx, address.SECP256K1)
assert.NoError(t, err)

if bytes.Compare(addr2.Bytes(), addr.Bytes()) < 0 {
Expand All @@ -74,6 +74,14 @@ func TestWalletSimple(t *testing.T) {
assert.Equal(t, list[0], addr)
assert.Equal(t, list[1], addr2)
}

t.Log("test DeleteAddress")
err = fs.DeleteAddress(ctx, addr)
assert.NoError(t, err)
err = fs.DeleteAddress(ctx, addr2)
assert.NoError(t, err)
assert.False(t, w.HasAddress(ctx, addr))
assert.False(t, w.HasAddress(ctx, addr2))
}

func TestWalletBLSKeys(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions venus-shared/api/chain/v0/method.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
* [WalletAddresses](#WalletAddresses)
* [WalletBalance](#WalletBalance)
* [WalletDefaultAddress](#WalletDefaultAddress)
* [WalletDelete](#WalletDelete)
* [WalletExport](#WalletExport)
* [WalletHas](#WalletHas)
* [WalletImport](#WalletImport)
Expand Down Expand Up @@ -5495,6 +5496,20 @@ Inputs: `[]`

Response: `"f01234"`

### WalletDelete


Perms: admin

Inputs:
```json
[
"f01234"
]
```

Response: `{}`

### WalletExport


Expand Down
14 changes: 14 additions & 0 deletions venus-shared/api/chain/v0/mock/mock_fullnode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions venus-shared/api/chain/v0/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions venus-shared/api/chain/v0/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type IWallet interface {
WalletSign(ctx context.Context, k address.Address, msg []byte, meta types.MsgMeta) (*crypto.Signature, error) //perm:sign
WalletExport(ctx context.Context, addr address.Address, password string) (*types.KeyInfo, error) //perm:admin
WalletImport(ctx context.Context, key *types.KeyInfo) (address.Address, error) //perm:admin
WalletDelete(ctx context.Context, addr address.Address) error //perm:admin
WalletHas(ctx context.Context, addr address.Address) (bool, error) //perm:write
WalletNewAddress(ctx context.Context, protocol address.Protocol) (address.Address, error) //perm:write
WalletBalance(ctx context.Context, addr address.Address) (abi.TokenAmount, error) //perm:read
Expand Down
15 changes: 15 additions & 0 deletions venus-shared/api/chain/v1/method.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
* [WalletAddresses](#WalletAddresses)
* [WalletBalance](#WalletBalance)
* [WalletDefaultAddress](#WalletDefaultAddress)
* [WalletDelete](#WalletDelete)
* [WalletExport](#WalletExport)
* [WalletHas](#WalletHas)
* [WalletImport](#WalletImport)
Expand Down Expand Up @@ -6016,6 +6017,20 @@ Inputs: `[]`

Response: `"f01234"`

### WalletDelete


Perms: admin

Inputs:
```json
[
"f01234"
]
```

Response: `{}`

### WalletExport


Expand Down
14 changes: 14 additions & 0 deletions venus-shared/api/chain/v1/mock/mock_fullnode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions venus-shared/api/chain/v1/proxy_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions venus-shared/api/chain/v1/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type IWallet interface {
WalletSign(ctx context.Context, k address.Address, msg []byte, meta types.MsgMeta) (*crypto.Signature, error) //perm:sign
WalletExport(ctx context.Context, addr address.Address, password string) (*types.KeyInfo, error) //perm:admin
WalletImport(ctx context.Context, key *types.KeyInfo) (address.Address, error) //perm:admin
WalletDelete(ctx context.Context, addr address.Address) error //perm:admin
WalletHas(ctx context.Context, addr address.Address) (bool, error) //perm:write
WalletNewAddress(ctx context.Context, protocol address.Protocol) (address.Address, error) //perm:write
WalletBalance(ctx context.Context, addr address.Address) (abi.TokenAmount, error) //perm:read
Expand Down
Loading

0 comments on commit 93f8066

Please sign in to comment.