Add delete functionality to the wallet

This commit is contained in:
Aayush Rajasekaran 2020-06-05 19:04:23 -04:00
parent 9e2603c1bf
commit 49b27da426
5 changed files with 58 additions and 4 deletions

View File

@ -101,6 +101,7 @@ type FullNode interface {
WalletSetDefault(context.Context, address.Address) error
WalletExport(context.Context, address.Address) (*types.KeyInfo, error)
WalletImport(context.Context, *types.KeyInfo) (address.Address, error)
WalletDelete(context.Context, address.Address) error
// Other

View File

@ -104,6 +104,7 @@ type FullNodeStruct struct {
WalletSetDefault func(context.Context, 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"`
WalletDelete func(context.Context, address.Address) error `perm:"write"`
ClientImport func(ctx context.Context, ref api.FileRef) (cid.Cid, error) `perm:"admin"`
ClientListImports func(ctx context.Context) ([]api.Import, error) `perm:"write"`
@ -427,6 +428,10 @@ func (c *FullNodeStruct) WalletImport(ctx context.Context, ki *types.KeyInfo) (a
return c.Internal.WalletImport(ctx, ki)
}
func (c *FullNodeStruct) WalletDelete(ctx context.Context, addr address.Address) error {
return c.Internal.WalletDelete(ctx, addr)
}
func (c *FullNodeStruct) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) {
return c.Internal.MpoolGetNonce(ctx, addr)
}

View File

@ -22,10 +22,11 @@ import (
var log = logging.Logger("wallet")
const (
KNamePrefix = "wallet-"
KDefault = "default"
KTBLS = "bls"
KTSecp256k1 = "secp256k1"
KNamePrefix = "wallet-"
KTrashPrefix = "trash-"
KDefault = "default"
KTBLS = "bls"
KTSecp256k1 = "secp256k1"
)
type Wallet struct {
@ -230,6 +231,23 @@ func (w *Wallet) HasKey(addr address.Address) (bool, error) {
return k != nil, nil
}
func (w *Wallet) DeleteKey(addr address.Address) error {
k, err := w.findKey(addr)
if err != nil {
return xerrors.Errorf("failed to delete key %s : %w", addr, err)
}
if err := w.keystore.Put(KTrashPrefix+k.Address.String(), k.KeyInfo); err != nil {
return xerrors.Errorf("failed to mark key %s as trashed: %w", addr, err)
}
if err := w.keystore.Delete(KNamePrefix + k.Address.String()); err != nil {
return xerrors.Errorf("failed to delete key %s: %w", addr, err)
}
return nil
}
type Key struct {
types.KeyInfo

View File

@ -31,6 +31,7 @@ var walletCmd = &cli.Command{
walletSetDefault,
walletSign,
walletVerify,
walletDelete,
},
}
@ -375,3 +376,28 @@ var walletVerify = &cli.Command{
}
},
}
var walletDelete = &cli.Command{
Name: "delete",
Usage: "Delete an account from the wallet",
ArgsUsage: "<address> ",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
if !cctx.Args().Present() || cctx.NArg() != 1 {
return fmt.Errorf("must specify address to delete")
}
addr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
return api.WalletDelete(ctx, addr)
},
}

View File

@ -76,3 +76,7 @@ func (a *WalletAPI) WalletExport(ctx context.Context, addr address.Address) (*ty
func (a *WalletAPI) WalletImport(ctx context.Context, ki *types.KeyInfo) (address.Address, error) {
return a.Wallet.Import(ki)
}
func (a *WalletAPI) WalletDelete(ctx context.Context, addr address.Address) error {
return a.Wallet.DeleteKey(addr)
}