From fda7b4c79d070f1cb4f5d7ef5b4d077d9dcf2774 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sun, 8 Mar 2015 00:18:13 +0100 Subject: [PATCH] accounts: use pointers consistently Account is now always a non-pointer. This will be important once the manager starts remembering accounts. AccountManager is now always a pointer because it contains locks and locks cannot be copied. --- accounts/account_manager.go | 26 +++++++++++--------------- accounts/accounts_test.go | 3 ++- xeth/xeth.go | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/accounts/account_manager.go b/accounts/account_manager.go index 3b7785231..86f9c5916 100644 --- a/accounts/account_manager.go +++ b/accounts/account_manager.go @@ -47,7 +47,6 @@ var ( ErrNoKeys = errors.New("no keys in store") ) -// TODO: better name for this struct? type Account struct { Address []byte } @@ -74,10 +73,10 @@ func (am *AccountManager) Coinbase() (addr []byte, err error) { } // MainAccount returns the primary account used for transactions. -func (am *AccountManager) Default() (*Account, error) { +func (am *AccountManager) Default() (Account, error) { // TODO: persist main account address on disk addr, err := am.firstAddr() - return &Account{Address: addr}, err + return Account{Address: addr}, err } func (am *AccountManager) firstAddr() ([]byte, error) { @@ -95,9 +94,9 @@ func (am *AccountManager) DeleteAccount(address []byte, auth string) error { return am.keyStore.DeleteKey(address, auth) } -func (am *AccountManager) Sign(fromAccount *Account, toSign []byte) (signature []byte, err error) { +func (am *AccountManager) Sign(a Account, toSign []byte) (signature []byte, err error) { am.mutex.RLock() - unlockedKey := am.unlockedKeys[string(fromAccount.Address)] + unlockedKey := am.unlockedKeys[string(a.Address)] am.mutex.RUnlock() if unlockedKey.Address == nil { return nil, ErrLocked @@ -106,28 +105,25 @@ func (am *AccountManager) Sign(fromAccount *Account, toSign []byte) (signature [ return signature, err } -func (am *AccountManager) SignLocked(fromAccount *Account, keyAuth string, toSign []byte) (signature []byte, err error) { - key, err := am.keyStore.GetKey(fromAccount.Address, keyAuth) +func (am *AccountManager) SignLocked(a Account, keyAuth string, toSign []byte) (signature []byte, err error) { + key, err := am.keyStore.GetKey(a.Address, keyAuth) if err != nil { return nil, err } am.mutex.RLock() - am.unlockedKeys[string(fromAccount.Address)] = *key + am.unlockedKeys[string(a.Address)] = *key am.mutex.RUnlock() - go unlockLater(am, fromAccount.Address) + go unlockLater(am, a.Address) signature, err = crypto.Sign(toSign, key.PrivateKey) return signature, err } -func (am AccountManager) NewAccount(auth string) (*Account, error) { +func (am *AccountManager) NewAccount(auth string) (Account, error) { key, err := am.keyStore.GenerateNewKey(crand.Reader, auth) if err != nil { - return nil, err + return Account{}, err } - ua := &Account{ - Address: key.Address, - } - return ua, err + return Account{Address: key.Address}, nil } func (am *AccountManager) Accounts() ([]Account, error) { diff --git a/accounts/accounts_test.go b/accounts/accounts_test.go index 44d1d72f1..d8187220a 100644 --- a/accounts/accounts_test.go +++ b/accounts/accounts_test.go @@ -3,10 +3,11 @@ package accounts import ( "testing" + "time" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/randentropy" "github.com/ethereum/go-ethereum/ethutil" - "time" ) func TestAccountManager(t *testing.T) { diff --git a/xeth/xeth.go b/xeth/xeth.go index 1ad62a7bf..187aa8c0f 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -311,7 +311,7 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt nonce := state.GetNonce(from) tx.SetNonce(nonce) - sig, err := self.accountManager.Sign(&accounts.Account{Address: from}, tx.Hash()) + sig, err := self.accountManager.Sign(accounts.Account{Address: from}, tx.Hash()) if err != nil { return "", err }