From 871e72282099acd770f8233a731aca4dbd98cdb9 Mon Sep 17 00:00:00 2001 From: chris-j-h Date: Mon, 8 Jul 2019 16:43:04 +0100 Subject: [PATCH] Move account handling to the vaultService Also implement the unsupported Derive and SelfDerive methods --- accounts/vault/vault_wallet.go | 23 +++++++++++++---------- accounts/vault/vault_wallet_test.go | 14 +++++++++----- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/accounts/vault/vault_wallet.go b/accounts/vault/vault_wallet.go index fc4f76c55..cba96c03e 100644 --- a/accounts/vault/vault_wallet.go +++ b/accounts/vault/vault_wallet.go @@ -16,7 +16,6 @@ type vaultWallet struct { url accounts.URL vault vaultService updateFeed *event.Feed - accounts []accounts.Account } // vault related behaviour that will be specific to each vault type @@ -24,6 +23,7 @@ type vaultService interface { status() (string, error) open() error close() error + accounts() []accounts.Account } func newHashicorpWallet(config hashicorpWalletConfig, updateFeed *event.Feed) (vaultWallet, error) { @@ -63,10 +63,7 @@ func (w vaultWallet) Close() error { } func (w vaultWallet) Accounts() []accounts.Account { - cpy := make([]accounts.Account, len(w.accounts)) - copy(cpy, w.accounts) - - return cpy + return w.vault.accounts() } func (w vaultWallet) Contains(account accounts.Account) bool { @@ -74,7 +71,7 @@ func (w vaultWallet) Contains(account accounts.Account) bool { return x.Address == y.Address && (x.URL == y.URL || x.URL == accounts.URL{} || y.URL == accounts.URL{}) } - accts := w.accounts + accts := w.Accounts() for _, a := range accts { if equal(a, account) { @@ -85,12 +82,10 @@ func (w vaultWallet) Contains(account accounts.Account) bool { } func (w vaultWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) { - panic("implement me") + return accounts.Account{}, accounts.ErrNotSupported } -func (w vaultWallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) { - panic("implement me") -} +func (w vaultWallet) SelfDerive(base accounts.DerivationPath, chain ethereum.ChainStateReader) {} func (w vaultWallet) SignHash(account accounts.Account, hash []byte) ([]byte, error) { panic("implement me") @@ -111,6 +106,7 @@ func (w vaultWallet) SignTxWithPassphrase(account accounts.Account, passphrase s type hashicorpService struct { client *api.Client config hashicorpClientConfig + accts []accounts.Account } const ( @@ -237,3 +233,10 @@ func (h *hashicorpService) close() error { return nil } + +func (h *hashicorpService) accounts() []accounts.Account { + cpy := make([]accounts.Account, len(h.accts)) + copy(cpy, h.accts) + + return cpy +} diff --git a/accounts/vault/vault_wallet_test.go b/accounts/vault/vault_wallet_test.go index e05638750..cc557b015 100644 --- a/accounts/vault/vault_wallet_test.go +++ b/accounts/vault/vault_wallet_test.go @@ -554,18 +554,20 @@ func TestVaultWallet_Close_Hashicorp_ReturnsStateToBeforeOpen(t *testing.T) { func TestVaultWallet_Accounts_ReturnsCopyOfAccountsInWallet(t *testing.T) { w := vaultWallet{ - accounts: []accounts.Account{{URL: accounts.URL{Scheme: "http", Path: "url:1"}}}, + vault: &hashicorpService{accts: []accounts.Account{{URL: accounts.URL{Scheme: "http", Path: "url:1"}}}}, } got := w.Accounts() - if !cmp.Equal(w.accounts, got) { - t.Fatalf("want: %v, got: %v", w.accounts, got) + v := w.vault.(*hashicorpService) + + if !cmp.Equal(v.accts, got) { + t.Fatalf("want: %v, got: %v", v.accts, got) } got[0].URL = accounts.URL{Scheme: "http", Path: "changed:1"} - if cmp.Equal(w.accounts, got) { + if cmp.Equal(v.accts, got) { t.Fatalf("changes to the returned accounts should not change the wallet's record of accounts") } } @@ -603,7 +605,9 @@ func TestVaultWallet_Contains(t *testing.T) { for name, tt := range tests { t.Run(name, func(t *testing.T) { - w := vaultWallet{accounts: tt.accts} + w := vaultWallet{ + vault: &hashicorpService{accts: tt.accts}, + } got := w.Contains(tt.toFind)