diff options
Diffstat (limited to 'accounts')
-rw-r--r-- | accounts/account_manager.go | 57 | ||||
-rw-r--r-- | accounts/accounts_test.go | 10 |
2 files changed, 29 insertions, 38 deletions
diff --git a/accounts/account_manager.go b/accounts/account_manager.go index c85304066..acf4d8e21 100644 --- a/accounts/account_manager.go +++ b/accounts/account_manager.go @@ -24,7 +24,6 @@ import ( crand "crypto/rand" "errors" "fmt" - "os" "sync" "time" @@ -70,8 +69,8 @@ func NewPlaintextManager(keydir string) *Manager { } } -func (am *Manager) HasAccount(addr common.Address) bool { - accounts, _ := am.Accounts() +func (am *Manager) HasAddress(addr common.Address) bool { + accounts := am.Accounts() for _, acct := range accounts { if acct.Address == addr { return true @@ -80,8 +79,8 @@ func (am *Manager) HasAccount(addr common.Address) bool { return false } -func (am *Manager) DeleteAccount(address common.Address, auth string) error { - return am.keyStore.DeleteKey(address, auth) +func (am *Manager) DeleteAccount(a Account, auth string) error { + return am.keyStore.DeleteKey(a.Address, auth) } func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error) { @@ -96,8 +95,8 @@ func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error) } // Unlock unlocks the given account indefinitely. -func (am *Manager) Unlock(addr common.Address, keyAuth string) error { - return am.TimedUnlock(addr, keyAuth, 0) +func (am *Manager) Unlock(a Account, keyAuth string) error { + return am.TimedUnlock(a, keyAuth, 0) } func (am *Manager) Lock(addr common.Address) error { @@ -117,8 +116,8 @@ func (am *Manager) Lock(addr common.Address) error { // // If the accout is already unlocked, TimedUnlock extends or shortens // the active unlock timeout. -func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time.Duration) error { - key, err := am.keyStore.GetKey(addr, keyAuth) +func (am *Manager) TimedUnlock(a Account, keyAuth string, timeout time.Duration) error { + key, err := am.keyStore.GetKey(a.Address, keyAuth) if err != nil { return err } @@ -126,7 +125,7 @@ func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time am.mutex.Lock() defer am.mutex.Unlock() var found bool - u, found = am.unlocked[addr] + u, found = am.unlocked[a.Address] if found { // terminate dropLater for this key to avoid unexpected drops. if u.abort != nil { @@ -135,11 +134,11 @@ func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time } if timeout > 0 { u = &unlocked{Key: key, abort: make(chan struct{})} - go am.expire(addr, u, timeout) + go am.expire(a.Address, u, timeout) } else { u = &unlocked{Key: key} } - am.unlocked[addr] = u + am.unlocked[a.Address] = u return nil } @@ -171,34 +170,26 @@ func (am *Manager) NewAccount(auth string) (Account, error) { return Account{Address: key.Address}, nil } -func (am *Manager) AddressByIndex(index int) (addr string, err error) { - var addrs []common.Address - addrs, err = am.keyStore.GetKeyAddresses() +func (am *Manager) AccountByIndex(index int) (Account, error) { + addrs, err := am.keyStore.GetKeyAddresses() if err != nil { - return + return Account{}, err } if index < 0 || index >= len(addrs) { - err = fmt.Errorf("index out of range: %d (should be 0-%d)", index, len(addrs)-1) - } else { - addr = addrs[index].Hex() + return Account{}, fmt.Errorf("account index %d not in range [0, %d]", index, len(addrs)-1) } - return + return Account{Address: addrs[index]}, nil } -func (am *Manager) Accounts() ([]Account, error) { - addresses, err := am.keyStore.GetKeyAddresses() - if os.IsNotExist(err) { - return nil, ErrNoKeys - } else if err != nil { - return nil, err - } +func (am *Manager) Accounts() []Account { + addresses, _ := am.keyStore.GetKeyAddresses() accounts := make([]Account, len(addresses)) for i, addr := range addresses { accounts[i] = Account{ Address: addr, } } - return accounts, err + return accounts } // zeroKey zeroes a private key in memory. @@ -211,8 +202,8 @@ func zeroKey(k *ecdsa.PrivateKey) { // USE WITH CAUTION = this will save an unencrypted private key on disk // no cli or js interface -func (am *Manager) Export(path string, addr common.Address, keyAuth string) error { - key, err := am.keyStore.GetKey(addr, keyAuth) +func (am *Manager) Export(path string, a Account, keyAuth string) error { + key, err := am.keyStore.GetKey(a.Address, keyAuth) if err != nil { return err } @@ -235,14 +226,14 @@ func (am *Manager) ImportECDSA(priv *ecdsa.PrivateKey, keyAuth string) (Account, return Account{Address: key.Address}, nil } -func (am *Manager) Update(addr common.Address, authFrom, authTo string) (err error) { +func (am *Manager) Update(a Account, authFrom, authTo string) (err error) { var key *Key - key, err = am.keyStore.GetKey(addr, authFrom) + key, err = am.keyStore.GetKey(a.Address, authFrom) if err == nil { err = am.keyStore.StoreKey(key, authTo) if err == nil { - am.keyStore.Cleanup(addr) + am.keyStore.Cleanup(a.Address) } } return diff --git a/accounts/accounts_test.go b/accounts/accounts_test.go index 4a1d1b848..0cb87a8f1 100644 --- a/accounts/accounts_test.go +++ b/accounts/accounts_test.go @@ -31,7 +31,7 @@ func TestSign(t *testing.T) { pass := "" // not used but required by API a1, err := am.NewAccount(pass) - am.Unlock(a1.Address, "") + am.Unlock(a1, "") _, err = am.Sign(a1, testSigData) if err != nil { @@ -53,7 +53,7 @@ func TestTimedUnlock(t *testing.T) { } // Signing with passphrase works - if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil { + if err = am.TimedUnlock(a1, pass, 100*time.Millisecond); err != nil { t.Fatal(err) } @@ -79,7 +79,7 @@ func TestOverrideUnlock(t *testing.T) { a1, err := am.NewAccount(pass) // Unlock indefinitely - if err = am.Unlock(a1.Address, pass); err != nil { + if err = am.Unlock(a1, pass); err != nil { t.Fatal(err) } @@ -90,7 +90,7 @@ func TestOverrideUnlock(t *testing.T) { } // reset unlock to a shorter period, invalidates the previous unlock - if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil { + if err = am.TimedUnlock(a1, pass, 100*time.Millisecond); err != nil { t.Fatal(err) } @@ -119,7 +119,7 @@ func TestSignRace(t *testing.T) { t.Fatal("could not create the test account", err) } - if err := am.TimedUnlock(a1.Address, "", 15*time.Millisecond); err != nil { + if err := am.TimedUnlock(a1, "", 15*time.Millisecond); err != nil { t.Fatal("could not unlock the test account", err) } end := time.Now().Add(500 * time.Millisecond) |