wallet: support account removal

This commit is contained in:
Evgenii Stratonikov 2020-03-13 16:11:49 +03:00
parent d742733e26
commit 03d0a6519a
2 changed files with 20 additions and 1 deletions

View file

@ -2,6 +2,7 @@ package wallet
import (
"encoding/json"
"errors"
"io"
"os"
@ -102,6 +103,19 @@ func (w *Wallet) AddAccount(acc *Account) {
w.Accounts = append(w.Accounts, acc)
}
// RemoveAccount removes an Account with the specified addr
// from the wallet.
func (w *Wallet) RemoveAccount(addr string) error {
for i, acc := range w.Accounts {
if acc.Address == addr {
copy(w.Accounts[i:], w.Accounts[i+1:])
w.Accounts = w.Accounts[:len(w.Accounts)-1]
return nil
}
}
return errors.New("account wasn't found")
}
// AddToken adds new token to a wallet.
func (w *Wallet) AddToken(tok *Token) {
w.Extra.Tokens = append(w.Extra.Tokens, tok)

View file

@ -48,7 +48,7 @@ func TestAddAccount(t *testing.T) {
privateKey: nil,
publicKey: nil,
wif: "",
Address: "",
Address: "real",
EncryptedWIF: "",
Label: "",
Contract: nil,
@ -57,6 +57,11 @@ func TestAddAccount(t *testing.T) {
})
accounts := wallet.Accounts
require.Len(t, accounts, 1)
require.Error(t, wallet.RemoveAccount("abc"))
require.Len(t, wallet.Accounts, 1)
require.NoError(t, wallet.RemoveAccount("real"))
require.Len(t, wallet.Accounts, 0)
}
func TestPath(t *testing.T) {