vendor: rebase fixes

This commit is contained in:
Victor Farazdagi 2017-02-27 15:52:10 +03:00
parent bea12ee9fe
commit 1cc8259589
8 changed files with 85 additions and 74 deletions

View File

@ -5,6 +5,8 @@ go:
sudo: false
dist: trusty
install:
- go get golang.org/x/tools/cmd/cover

View File

@ -137,7 +137,7 @@ func testRestartNodeRPC(t *testing.T) bool {
func testCreateChildAccount(t *testing.T) bool {
geth.Logout() // to make sure that we start with empty account (which might get populated during previous tests)
accountManager, err := geth.NodeManagerInstance().AccountManager()
keyStore, err := geth.NodeManagerInstance().AccountKeyStore()
if err != nil {
t.Error(err)
return false
@ -159,14 +159,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 := geth.ParseAccountString(accountManager, address)
account, err := geth.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
account, key, err := accountManager.AccountDecryptedKey(account, newAccountPassword)
account, key, err := keyStore.AccountDecryptedKey(account, newAccountPassword)
if err != nil {
t.Errorf("can not obtain decrypted account key: %v", err)
return false
@ -268,7 +268,7 @@ func testCreateChildAccount(t *testing.T) bool {
}
func testRecoverAccount(t *testing.T) bool {
accountManager, _ := geth.NodeManagerInstance().AccountManager()
keyStore, _ := geth.NodeManagerInstance().AccountKeyStore()
// create an account
address, pubKey, mnemonic, err := geth.CreateAccount(newAccountPassword)
@ -297,19 +297,19 @@ func testRecoverAccount(t *testing.T) bool {
}
// now test recovering, but make sure that account/key file is removed i.e. simulate recovering on a new device
account, err := geth.ParseAccountString(accountManager, address)
account, err := geth.ParseAccountString(address)
if err != nil {
t.Errorf("can not get account from address: %v", err)
}
account, key, err := accountManager.AccountDecryptedKey(account, newAccountPassword)
account, key, err := keyStore.AccountDecryptedKey(account, newAccountPassword)
if err != nil {
t.Errorf("can not obtain decrypted account key: %v", err)
return false
}
extChild2String := key.ExtendedKey.String()
if err := accountManager.Delete(account, newAccountPassword); err != nil {
if err := keyStore.Delete(account, newAccountPassword); err != nil {
t.Errorf("cannot remove account: %v", err)
}
@ -331,7 +331,7 @@ func testRecoverAccount(t *testing.T) bool {
}
// make sure that extended key exists and is imported ok too
account, key, err = accountManager.AccountDecryptedKey(account, newAccountPassword)
account, key, err = keyStore.AccountDecryptedKey(account, newAccountPassword)
if err != nil {
t.Errorf("can not obtain decrypted account key: %v", err)
return false

View File

@ -54,7 +54,7 @@ func CreateAccount(password string) (address, pubKey, mnemonic string, err error
// Otherwise (when parentAddress != ""), child is derived directly from parent.
func CreateChildAccount(parentAddress, password string) (address, pubKey string, err error) {
nodeManager := NodeManagerInstance()
accountManager, err := nodeManager.AccountManager()
keyStore, err := nodeManager.AccountKeyStore()
if err != nil {
return "", "", err
}
@ -67,13 +67,13 @@ func CreateChildAccount(parentAddress, password string) (address, pubKey string,
return "", "", ErrNoAccountSelected
}
account, err := ParseAccountString(accountManager, parentAddress)
account, err := ParseAccountString(parentAddress)
if err != nil {
return "", "", ErrAddressToAccountMappingFailure
}
// make sure that given password can decrypt key associated with a given parent address
account, accountKey, err := accountManager.AccountDecryptedKey(account, password)
account, accountKey, err := keyStore.AccountDecryptedKey(account, password)
if err != nil {
return "", "", fmt.Errorf("%s: %v", ErrAccountToKeyMappingFailure.Error(), err)
}
@ -88,7 +88,7 @@ func CreateChildAccount(parentAddress, password string) (address, pubKey string,
if err != nil {
return "", "", err
}
accountManager.IncSubAccountIndex(account, password)
keyStore.IncSubAccountIndex(account, password)
accountKey.SubAccountIndex++
// import derived key into account keystore
@ -129,17 +129,17 @@ func RecoverAccount(password, mnemonic string) (address, pubKey string, err erro
// all previous identities are removed).
func SelectAccount(address, password string) error {
nodeManager := NodeManagerInstance()
accountManager, err := nodeManager.AccountManager()
keyStore, err := nodeManager.AccountKeyStore()
if err != nil {
return err
}
account, err := ParseAccountString(accountManager, address)
account, err := ParseAccountString(address)
if err != nil {
return ErrAddressToAccountMappingFailure
}
account, accountKey, err := accountManager.AccountDecryptedKey(account, password)
account, accountKey, err := keyStore.AccountDecryptedKey(account, password)
if err != nil {
return fmt.Errorf("%s: %v", ErrAccountToKeyMappingFailure.Error(), err)
}
@ -195,20 +195,20 @@ func UnlockAccount(address, password string, seconds int) 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 importExtendedKey(extKey *extkeys.ExtendedKey, password string) (address, pubKey string, err error) {
accountManager, err := NodeManagerInstance().AccountManager()
keyStore, err := NodeManagerInstance().AccountKeyStore()
if err != nil {
return "", "", err
}
// imports extended key, create key file (if necessary)
account, err := accountManager.ImportExtendedKey(extKey, password)
account, err := keyStore.ImportExtendedKey(extKey, password)
if err != nil {
return "", "", err
}
address = fmt.Sprintf("%x", account.Address)
// obtain public key to return
account, key, err := accountManager.AccountDecryptedKey(account, password)
account, key, err := keyStore.AccountDecryptedKey(account, password)
if err != nil {
return address, "", err
}
@ -217,24 +217,24 @@ func importExtendedKey(extKey *extkeys.ExtendedKey, password string) (address, p
return
}
func onAccountsListRequest(entities []accounts.Account) []accounts.Account {
func onAccountsListRequest(entities []common.Address) []common.Address {
nodeManager := NodeManagerInstance()
if nodeManager.SelectedAccount == nil {
return []accounts.Account{}
return []common.Address{}
}
refreshSelectedAccount()
filtered := make([]accounts.Account, 0)
filtered := make([]common.Address, 0)
for _, account := range entities {
// main account
if nodeManager.SelectedAccount.Address.Hex() == account.Address.Hex() {
if nodeManager.SelectedAccount.Address.Hex() == account.Hex() {
filtered = append(filtered, account)
} else {
// sub accounts
for _, subAccount := range nodeManager.SelectedAccount.SubAccounts {
if subAccount.Address.Hex() == account.Address.Hex() {
if subAccount.Address.Hex() == account.Hex() {
filtered = append(filtered, account)
}
}
@ -274,7 +274,7 @@ func refreshSelectedAccount() {
// The extKey is CKD#2 := root of sub-accounts of the main account
func findSubAccounts(extKey *extkeys.ExtendedKey, subAccountIndex uint32) ([]accounts.Account, error) {
nodeManager := NodeManagerInstance()
accountManager, err := nodeManager.AccountManager()
keyStore, err := nodeManager.AccountKeyStore()
if err != nil {
return []accounts.Account{}, err
}
@ -292,7 +292,7 @@ func findSubAccounts(extKey *extkeys.ExtendedKey, subAccountIndex uint32) ([]acc
}
// see if any of the gathered addresses actually exist in cached accounts list
for _, cachedAccount := range accountManager.Accounts() {
for _, cachedAccount := range keyStore.Accounts() {
for _, possibleAddress := range subAccountAddresses {
if possibleAddress.Hex() == cachedAccount.Address.Hex() {
subAccounts = append(subAccounts, cachedAccount)

View File

@ -56,9 +56,9 @@ func TestAccountsList(t *testing.T) {
t.Error("exactly single account is expected (main account)")
return
}
if string(accounts[0].Address.Hex()) != "0x"+address {
if string(accounts[0].Hex()) != "0x"+address {
t.Errorf("main account is not retured as the first key: got %s, expected %s",
accounts[0].Address.Hex(), "0x"+address)
accounts[0].Hex(), "0x"+address)
return
}
@ -75,13 +75,13 @@ func TestAccountsList(t *testing.T) {
t.Error("exactly 2 accounts are expected (main + sub-account 1)")
return
}
if string(accounts[0].Address.Hex()) != "0x"+address {
if string(accounts[0].Hex()) != "0x"+address {
t.Errorf("main account is not retured as the first key: got %s, expected %s",
accounts[0].Address.Hex(), "0x"+address)
accounts[0].Hex(), "0x"+address)
return
}
if string(accounts[1].Address.Hex()) != "0x"+subAccount1 {
t.Errorf("subAcount1 not returned: got %s, expected %s", accounts[1].Address.Hex(), "0x"+subAccount1)
if string(accounts[1].Hex()) != "0x"+subAccount1 {
t.Errorf("subAcount1 not returned: got %s, expected %s", accounts[1].Hex(), "0x"+subAccount1)
return
}
@ -101,21 +101,21 @@ func TestAccountsList(t *testing.T) {
t.Errorf("unexpected number of accounts: expected %d, got %d", 3, len(accounts))
return
}
if string(accounts[0].Address.Hex()) != "0x"+address {
if string(accounts[0].Hex()) != "0x"+address {
t.Errorf("main account is not retured as the first key: got %s, expected %s",
accounts[0].Address.Hex(), "0x"+address)
accounts[0].Hex(), "0x"+address)
return
}
subAccount1MatchesKey1 := string(accounts[1].Address.Hex()) != "0x"+subAccount1
subAccount1MatchesKey2 := string(accounts[2].Address.Hex()) != "0x"+subAccount1
subAccount1MatchesKey1 := string(accounts[1].Hex()) != "0x"+subAccount1
subAccount1MatchesKey2 := string(accounts[2].Hex()) != "0x"+subAccount1
if !subAccount1MatchesKey1 && !subAccount1MatchesKey2 {
t.Errorf("subAcount1 not returned: got %s, expected %s", accounts[1].Address.Hex(), "0x"+subAccount1)
t.Errorf("subAcount1 not returned: got %s, expected %s", accounts[1].Hex(), "0x"+subAccount1)
return
}
subAccount2MatchesKey1 := string(accounts[1].Address.Hex()) != "0x"+subAccount2
subAccount2MatchesKey2 := string(accounts[2].Address.Hex()) != "0x"+subAccount2
subAccount2MatchesKey1 := string(accounts[1].Hex()) != "0x"+subAccount2
subAccount2MatchesKey2 := string(accounts[2].Hex()) != "0x"+subAccount2
if !subAccount2MatchesKey1 && !subAccount2MatchesKey2 {
t.Errorf("subAcount2 not returned: got %s, expected %s", accounts[2].Address.Hex(), "0x"+subAccount1)
t.Errorf("subAcount2 not returned: got %s, expected %s", accounts[2].Hex(), "0x"+subAccount1)
return
}
}
@ -129,7 +129,7 @@ func TestCreateChildAccount(t *testing.T) {
geth.Logout() // to make sure that we start with empty account (which might get populated during previous tests)
accountManager, err := geth.NodeManagerInstance().AccountManager()
keyStore, err := geth.NodeManagerInstance().AccountKeyStore()
if err != nil {
t.Error(err)
return
@ -143,14 +143,14 @@ func TestCreateChildAccount(t *testing.T) {
}
t.Logf("Account created: {address: %s, key: %s, mnemonic:%s}", address, pubKey, mnemonic)
account, err := geth.ParseAccountString(accountManager, address)
account, err := geth.ParseAccountString(address)
if err != nil {
t.Errorf("can not get account from address: %v", err)
return
}
// obtain decrypted key, and make sure that extended key (which will be used as root for sub-accounts) is present
account, key, err := accountManager.AccountDecryptedKey(account, newAccountPassword)
account, key, err := keyStore.AccountDecryptedKey(account, newAccountPassword)
if err != nil {
t.Errorf("can not obtain decrypted account key: %v", err)
return
@ -216,7 +216,7 @@ func TestRecoverAccount(t *testing.T) {
return
}
accountManager, _ := geth.NodeManagerInstance().AccountManager()
keyStore, _ := geth.NodeManagerInstance().AccountKeyStore()
// create an account
address, pubKey, mnemonic, err := geth.CreateAccount(newAccountPassword)
@ -237,19 +237,19 @@ func TestRecoverAccount(t *testing.T) {
}
// now test recovering, but make sure that account/key file is removed i.e. simulate recovering on a new device
account, err := geth.ParseAccountString(accountManager, address)
account, err := geth.ParseAccountString(address)
if err != nil {
t.Errorf("can not get account from address: %v", err)
}
account, key, err := accountManager.AccountDecryptedKey(account, newAccountPassword)
account, key, err := keyStore.AccountDecryptedKey(account, newAccountPassword)
if err != nil {
t.Errorf("can not obtain decrypted account key: %v", err)
return
}
extChild2String := key.ExtendedKey.String()
if err := accountManager.Delete(account, newAccountPassword); err != nil {
if err := keyStore.Delete(account, newAccountPassword); err != nil {
t.Errorf("cannot remove account: %v", err)
}
@ -263,7 +263,7 @@ func TestRecoverAccount(t *testing.T) {
}
// make sure that extended key exists and is imported ok too
account, key, err = accountManager.AccountDecryptedKey(account, newAccountPassword)
account, key, err = keyStore.AccountDecryptedKey(account, newAccountPassword)
if err != nil {
t.Errorf("can not obtain decrypted account key: %v", err)
return

View File

@ -12,6 +12,7 @@ import (
"sync"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/les"
"github.com/ethereum/go-ethereum/logger"
@ -25,7 +26,7 @@ import (
// SelectedExtKey is a container for currently selected (logged in) account
type SelectedExtKey struct {
Address common.Address
AccountKey *accounts.Key
AccountKey *keystore.Key
SubAccounts []accounts.Account
}
@ -56,6 +57,7 @@ var (
ErrNodeMakeFailure = errors.New("error creating p2p node")
ErrNodeStartFailure = errors.New("error starting p2p node")
ErrInvalidNodeAPI = errors.New("no node API connected")
ErrAccountKeyStoreMissing = errors.New("account key store is not set")
)
var (
@ -218,6 +220,30 @@ func (m *NodeManager) AccountManager() (*accounts.Manager, error) {
return m.node.geth.AccountManager(), nil
}
// AccountKeyStore exposes reference to accounts key store
func (m *NodeManager) AccountKeyStore() (*keystore.KeyStore, error) {
if m == nil || !m.NodeInited() {
return nil, ErrInvalidGethNode
}
accountManager, err := m.AccountManager()
if err != nil {
return nil, err
}
backends := accountManager.Backends(keystore.KeyStoreType)
if len(backends) == 0 {
return nil, ErrAccountKeyStoreMissing
}
keyStore, ok := backends[0].(*keystore.KeyStore)
if !ok {
return nil, ErrAccountKeyStoreMissing
}
return keyStore, nil
}
// LightEthereumService exposes LES
func (m *NodeManager) LightEthereumService() (*les.LightEthereum, error) {
if m == nil || !m.NodeInited() {

View File

@ -6,7 +6,7 @@ import (
"math/big"
"strconv"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/les/status"
@ -67,7 +67,7 @@ func sendTransactionErrorCode(err error) string {
}
switch err {
case accounts.ErrDecrypt:
case keystore.ErrDecrypt:
return SendTransactionPasswordErrorCode
case status.ErrQueuedTxTimedOut:
return SendTransactionTimeoutErrorCode

View File

@ -7,7 +7,7 @@ import (
"testing"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/les/status"
@ -154,7 +154,7 @@ func TestDoubleCompleteQueuedTransactions(t *testing.T) {
// try with wrong password
// make sure that tx is NOT removed from the queue (by re-trying with the correct password)
if _, err = geth.CompleteTransaction(txId, testAddressPassword+"wrong"); err != accounts.ErrDecrypt {
if _, err = geth.CompleteTransaction(txId, testAddressPassword+"wrong"); err != keystore.ErrDecrypt {
t.Errorf("expects wrong password error, but call succeeded (or got another error: %v)", err)
return
}

View File

@ -13,7 +13,6 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"sync"
"time"
@ -189,12 +188,7 @@ func PanicAfter(waitSeconds time.Duration, abort chan struct{}, desc string) {
}
func FromAddress(accountAddress string) common.Address {
accountManager, err := NodeManagerInstance().AccountManager()
if err != nil {
return common.Address{}
}
from, err := ParseAccountString(accountManager, accountAddress)
from, err := ParseAccountString(accountAddress)
if err != nil {
return common.Address{}
}
@ -203,12 +197,7 @@ func FromAddress(accountAddress string) common.Address {
}
func ToAddress(accountAddress string) *common.Address {
accountManager, err := NodeManagerInstance().AccountManager()
if err != nil {
return nil
}
to, err := ParseAccountString(accountManager, accountAddress)
to, err := ParseAccountString(accountAddress)
if err != nil {
return nil
}
@ -216,18 +205,12 @@ func ToAddress(accountAddress string) *common.Address {
return &to.Address
}
// parseAccount parses hex encoded string or key index in the accounts key store
// and converts it to an internal account representation.
func ParseAccountString(accman *accounts.Manager, account string) (accounts.Account, error) {
// ParseAccountString parses hex encoded string and returns is as accounts.Account.
func ParseAccountString(account string) (accounts.Account, error) {
// valid address, convert to account
if common.IsHexAddress(account) {
return accounts.Account{Address: common.HexToAddress(account)}, nil
}
// valid key index, return account referenced by that key
index, err := strconv.Atoi(account)
if err != nil {
return accounts.Account{}, ErrInvalidAccountAddressOrKey
}
return accman.AccountByIndex(index)
return accounts.Account{}, ErrInvalidAccountAddressOrKey
}