Move accounts to separate package (#360)

This commit is contained in:
Ivan Daniluk 2017-09-26 15:44:26 +02:00 committed by GitHub
parent 14a04c1b20
commit 93492cf606
10 changed files with 50 additions and 43 deletions

View File

@ -19,6 +19,7 @@ import (
"fmt"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params"
@ -360,14 +361,14 @@ func testCreateChildAccount(t *testing.T) bool {
address, pubKey, mnemonic := createAccountResponse.Address, createAccountResponse.PubKey, createAccountResponse.Mnemonic
t.Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
account, err := common.ParseAccountString(address)
acct, err := common.ParseAccountString(address)
if err != nil {
t.Errorf("can not get account from address: %v", err)
return false
}
// obtain decrypted key, and make sure that extended key (which will be used as root for sub-accounts) is present
_, key, err := keyStore.AccountDecryptedKey(account, TestConfig.Account1.Password)
_, key, err := keyStore.AccountDecryptedKey(acct, TestConfig.Account1.Password)
if err != nil {
t.Errorf("can not obtain decrypted account key: %v", err)
return false
@ -387,7 +388,7 @@ func testCreateChildAccount(t *testing.T) bool {
return false
}
if createSubAccountResponse.Error != node.ErrNoAccountSelected.Error() {
if createSubAccountResponse.Error != account.ErrNoAccountSelected.Error() {
t.Errorf("expected error is not returned (tried to create sub-account w/o login): %v", createSubAccountResponse.Error)
return false
}

View File

@ -5,8 +5,8 @@ import (
"fmt"
"path/filepath"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params"
"gopkg.in/urfave/cli.v1"
)
@ -247,14 +247,14 @@ func injectAccountIntoWhisper(address, password string) error {
return err
}
account, err := common.ParseAccountString(address)
acct, err := common.ParseAccountString(address)
if err != nil {
return node.ErrAddressToAccountMappingFailure
return account.ErrAddressToAccountMappingFailure
}
_, accountKey, err := keyStore.AccountDecryptedKey(account, password)
_, accountKey, err := keyStore.AccountDecryptedKey(acct, password)
if err != nil {
return fmt.Errorf("%s: %v", node.ErrAccountToKeyMappingFailure.Error(), err)
return fmt.Errorf("%s: %v", account.ErrAccountToKeyMappingFailure.Error(), err)
}
whisperService, err := nodeManager.WhisperService()

View File

@ -1,4 +1,4 @@
package node
package account
import (
"bytes"
@ -28,15 +28,15 @@ var (
ErrInvalidMasterKeyCreated = errors.New("can not create master extended key")
)
// AccountManager represents account manager interface
type AccountManager struct {
// Manager represents account manager interface
type Manager struct {
nodeManager common.NodeManager
selectedAccount *common.SelectedExtKey // account that was processed during the last call to SelectAccount()
}
// NewAccountManager returns new node account manager
func NewAccountManager(nodeManager common.NodeManager) *AccountManager {
return &AccountManager{
// NewManager returns new node account manager
func NewManager(nodeManager common.NodeManager) *Manager {
return &Manager{
nodeManager: nodeManager,
}
}
@ -45,7 +45,7 @@ func NewAccountManager(nodeManager common.NodeManager) *AccountManager {
// BIP44-compatible keys are generated: CKD#1 is stored as account key, CKD#2 stored as sub-account root
// Public key of CKD#1 is returned, with CKD#2 securely encoded into account key file (to be used for
// sub-account derivations)
func (m *AccountManager) CreateAccount(password string) (address, pubKey, mnemonic string, err error) {
func (m *Manager) CreateAccount(password string) (address, pubKey, mnemonic string, err error) {
// generate mnemonic phrase
mn := extkeys.NewMnemonic(extkeys.Salt)
mnemonic, err = mn.MnemonicPhrase(128, extkeys.EnglishLanguage)
@ -71,7 +71,7 @@ func (m *AccountManager) CreateAccount(password string) (address, pubKey, mnemon
// CreateChildAccount creates sub-account for an account identified by parent address.
// CKD#2 is used as root for master accounts (when parentAddress is "").
// Otherwise (when parentAddress != ""), child is derived directly from parent.
func (m *AccountManager) CreateChildAccount(parentAddress, password string) (address, pubKey string, err error) {
func (m *Manager) CreateChildAccount(parentAddress, password string) (address, pubKey string, err error) {
keyStore, err := m.nodeManager.AccountKeyStore()
if err != nil {
return "", "", err
@ -127,7 +127,7 @@ func (m *AccountManager) CreateChildAccount(parentAddress, password string) (add
// RecoverAccount re-creates master key using given details.
// Once master key is re-generated, it is inserted into keystore (if not already there).
func (m *AccountManager) RecoverAccount(password, mnemonic string) (address, pubKey string, err error) {
func (m *Manager) RecoverAccount(password, mnemonic string) (address, pubKey string, err error) {
// re-create extended key (see BIP32)
mn := extkeys.NewMnemonic(extkeys.Salt)
extKey, err := extkeys.NewMaster(mn.MnemonicSeed(mnemonic, password), []byte(extkeys.Salt))
@ -146,7 +146,7 @@ func (m *AccountManager) RecoverAccount(password, mnemonic string) (address, pub
// VerifyAccountPassword tries to decrypt a given account key file, with a provided password.
// If no error is returned, then account is considered verified.
func (m *AccountManager) VerifyAccountPassword(keyStoreDir, address, password string) (*keystore.Key, error) {
func (m *Manager) VerifyAccountPassword(keyStoreDir, address, password string) (*keystore.Key, error) {
var err error
var keyJSON []byte
@ -197,7 +197,7 @@ func (m *AccountManager) VerifyAccountPassword(keyStoreDir, address, password st
// SelectAccount selects current account, by verifying that address has corresponding account which can be decrypted
// using provided password. Once verification is done, decrypted key is injected into Whisper (as a single identity,
// all previous identities are removed).
func (m *AccountManager) SelectAccount(address, password string) error {
func (m *Manager) SelectAccount(address, password string) error {
keyStore, err := m.nodeManager.AccountKeyStore()
if err != nil {
return err
@ -237,7 +237,7 @@ func (m *AccountManager) SelectAccount(address, password string) error {
}
// SelectedAccount returns currently selected account
func (m *AccountManager) SelectedAccount() (*common.SelectedExtKey, error) {
func (m *Manager) SelectedAccount() (*common.SelectedExtKey, error) {
if m.selectedAccount == nil {
return nil, ErrNoAccountSelected
}
@ -245,7 +245,7 @@ func (m *AccountManager) SelectedAccount() (*common.SelectedExtKey, error) {
}
// ReSelectAccount selects previously selected account, often, after node restart.
func (m *AccountManager) ReSelectAccount() error {
func (m *Manager) ReSelectAccount() error {
selectedAccount := m.selectedAccount
if selectedAccount == nil {
return nil
@ -264,7 +264,7 @@ func (m *AccountManager) ReSelectAccount() error {
}
// Logout clears whisper identities
func (m *AccountManager) Logout() error {
func (m *Manager) Logout() error {
whisperService, err := m.nodeManager.WhisperService()
if err != nil {
return err
@ -282,7 +282,7 @@ func (m *AccountManager) Logout() error {
// importExtendedKey processes incoming extended key, extracts required info and creates corresponding account key.
// Once account key is formed, that key is put (if not already) into keystore i.e. key is *encoded* into key file.
func (m *AccountManager) importExtendedKey(extKey *extkeys.ExtendedKey, password string) (address, pubKey string, err error) {
func (m *Manager) importExtendedKey(extKey *extkeys.ExtendedKey, password string) (address, pubKey string, err error) {
keyStore, err := m.nodeManager.AccountKeyStore()
if err != nil {
return "", "", err
@ -307,7 +307,7 @@ func (m *AccountManager) importExtendedKey(extKey *extkeys.ExtendedKey, password
// Accounts returns list of addresses for selected account, including
// subaccounts.
func (m *AccountManager) Accounts() ([]gethcommon.Address, error) {
func (m *Manager) Accounts() ([]gethcommon.Address, error) {
am, err := m.nodeManager.AccountManager()
if err != nil {
return nil, err
@ -345,14 +345,14 @@ func (m *AccountManager) Accounts() ([]gethcommon.Address, error) {
}
// AccountsRPCHandler returns RPC Handler for the Accounts() method.
func (m *AccountManager) AccountsRPCHandler() rpc.Handler {
func (m *Manager) AccountsRPCHandler() rpc.Handler {
return func(context.Context, ...interface{}) (interface{}, error) {
return m.Accounts()
}
}
// refreshSelectedAccount re-populates list of sub-accounts of the currently selected account (if any)
func (m *AccountManager) refreshSelectedAccount() {
func (m *Manager) refreshSelectedAccount() {
if m.selectedAccount == nil {
return
}
@ -377,7 +377,7 @@ func (m *AccountManager) refreshSelectedAccount() {
// findSubAccounts traverses cached accounts and adds as a sub-accounts any
// that belong to the currently selected account.
// The extKey is CKD#2 := root of sub-accounts of the main account
func (m *AccountManager) findSubAccounts(extKey *extkeys.ExtendedKey, subAccountIndex uint32) ([]accounts.Account, error) {
func (m *Manager) findSubAccounts(extKey *extkeys.ExtendedKey, subAccountIndex uint32) ([]accounts.Account, error) {
keyStore, err := m.nodeManager.AccountKeyStore()
if err != nil {
return []accounts.Account{}, err
@ -411,7 +411,7 @@ func (m *AccountManager) findSubAccounts(extKey *extkeys.ExtendedKey, subAccount
// AddressToDecryptedAccount tries to load decrypted key for a given account.
// The running node, has a keystore directory which is loaded on start. Key file
// for a given address is expected to be in that directory prior to node start.
func (m *AccountManager) AddressToDecryptedAccount(address, password string) (accounts.Account, *keystore.Key, error) {
func (m *Manager) AddressToDecryptedAccount(address, password string) (accounts.Account, *keystore.Key, error) {
keyStore, err := m.nodeManager.AccountKeyStore()
if err != nil {
return accounts.Account{}, nil, err

View File

@ -1,4 +1,4 @@
package node_test
package account_test
import (
"errors"
@ -10,6 +10,7 @@ import (
"testing"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/node"
. "github.com/status-im/status-go/geth/testing"
@ -35,7 +36,7 @@ func (s *AccountsTestSuite) TestVerifyAccountPassword() {
require := s.Require()
require.NotNil(s.NodeManager)
accountManager := node.NewAccountManager(nil)
accountManager := account.NewManager(nil)
require.NotNil(accountManager)
keyStoreDir, err := ioutil.TempDir(os.TempDir(), "accounts")

View File

@ -5,6 +5,7 @@ import (
"sync"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/jail"
"github.com/status-im/status-go/geth/log"
@ -29,7 +30,7 @@ func NewStatusBackend() *StatusBackend {
defer log.Info("Status backend initialized")
nodeManager := node.NewNodeManager()
accountManager := node.NewAccountManager(nodeManager)
accountManager := account.NewManager(nodeManager)
txQueueManager := node.NewTxQueueManager(nodeManager, accountManager)
return &StatusBackend{

View File

@ -4,8 +4,8 @@ import (
"errors"
"fmt"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params"
. "github.com/status-im/status-go/geth/testing"
)
@ -94,17 +94,17 @@ func (s *BackendTestSuite) TestCreateChildAccount() {
require.NoError(err)
s.T().Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
account, err := common.ParseAccountString(address)
acct, err := common.ParseAccountString(address)
require.NoError(err, "can not get account from address")
// obtain decrypted key, and make sure that extended key (which will be used as root for sub-accounts) is present
_, key, err := keyStore.AccountDecryptedKey(account, TestConfig.Account1.Password)
_, key, err := keyStore.AccountDecryptedKey(acct, TestConfig.Account1.Password)
require.NoError(err, "can not obtain decrypted account key")
require.NotNil(key.ExtendedKey, "CKD#2 has not been generated for new account")
// try creating sub-account, w/o selecting main account i.e. w/o login to main account
_, _, err = s.backend.AccountManager().CreateChildAccount("", TestConfig.Account1.Password)
require.EqualError(node.ErrNoAccountSelected, err.Error(), "expected error is not returned (tried to create sub-account w/o login)")
require.EqualError(account.ErrNoAccountSelected, err.Error(), "expected error is not returned (tried to create sub-account w/o login)")
err = s.backend.AccountManager().SelectAccount(address, TestConfig.Account1.Password)
require.NoError(err, "cannot select account")
@ -265,7 +265,7 @@ func (s *BackendTestSuite) TestSelectedAccountOnRestart() {
// make sure that no account is selected by default
selectedAccount, err := s.backend.AccountManager().SelectedAccount()
require.EqualError(node.ErrNoAccountSelected, err.Error(), "account selected, but should not be")
require.EqualError(account.ErrNoAccountSelected, err.Error(), "account selected, but should not be")
require.Nil(selectedAccount)
// select account
@ -329,7 +329,7 @@ func (s *BackendTestSuite) TestSelectedAccountOnRestart() {
require.False(whisperService.HasKeyPair(pubKey1), "identity should not be present, but it is still present in whisper")
selectedAccount, err = s.backend.AccountManager().SelectedAccount()
require.EqualError(node.ErrNoAccountSelected, err.Error())
require.EqualError(account.ErrNoAccountSelected, err.Error())
require.Nil(selectedAccount)
}

View File

@ -9,6 +9,7 @@ import (
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/les"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/api"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/jail"
@ -427,8 +428,8 @@ func (s *BackendTestSuite) TestRaceConditions() {
log.Info("AccountManager()")
instance := s.backend.AccountManager()
s.NotNil(instance)
s.IsType(&node.AccountManager{}, instance)
s.T().Logf("AccountManager(), result: %v", instance)
s.IsType(&account.Manager{}, instance)
s.T().Logf("Manager(), result: %v", instance)
progress <- struct{}{}
},
func(config *params.NodeConfig) {

View File

@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/keystore"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node"
@ -52,7 +53,7 @@ func (s *BackendTestSuite) TestSendContractTx() {
)
s.EqualError(
err,
node.ErrNoAccountSelected.Error(),
account.ErrNoAccountSelected.Error(),
fmt.Sprintf("expected error on queued transaction[%v] not thrown", event["id"]),
)
@ -146,7 +147,7 @@ func (s *BackendTestSuite) TestSendEtherTx() {
)
s.EqualError(
err,
node.ErrNoAccountSelected.Error(),
account.ErrNoAccountSelected.Error(),
fmt.Sprintf("expected error on queued transaction[%v] not thrown", event["id"]),
)

View File

@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/jail"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params"
@ -40,7 +41,7 @@ func (s *JailTestSuite) SetupTest() {
nodeManager := node.NewNodeManager()
require.NotNil(nodeManager)
accountManager := node.NewAccountManager(nodeManager)
accountManager := account.NewManager(nodeManager)
require.NotNil(accountManager)
txQueueManager := node.NewTxQueueManager(nodeManager, accountManager)

View File

@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
"github.com/status-im/status-go/geth/account"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/params"
. "github.com/status-im/status-go/geth/testing"
@ -40,7 +41,7 @@ func (s *WhisperTestSuite) TestWhisperFilterRace() {
whisperAPI := whisper.NewPublicWhisperAPI(whisperService)
require.NotNil(whisperAPI)
accountManager := node.NewAccountManager(s.NodeManager)
accountManager := account.NewManager(s.NodeManager)
require.NotNil(accountManager)
// account1