Changes to tests to include account where required for account injection

This commit is contained in:
Samuel Hawksby-Robinson 2020-12-10 13:38:58 +00:00 committed by Andrea Maria Piana
parent 31a885ced8
commit 829108c96f
6 changed files with 45 additions and 19 deletions

View File

@ -61,8 +61,8 @@ func (m *Manager) AccountsGenerator() *generator.Generator {
// BIP44-compatible keys are generated: CKD#1 is stored as account key, CKD#2 stored as sub-account root // 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 // Public key of CKD#1 is returned, with CKD#2 securely encoded into account key file (to be used for
// sub-account derivations) // sub-account derivations)
func (m *Manager) CreateAccount(password string) (generator.IdentifiedAccountInfo, Info, string, error) { func (m *Manager) CreateAccount(password string) (generator.GeneratedAccountInfo, Info, string, error) {
var mkInfo generator.IdentifiedAccountInfo var mkInfo generator.GeneratedAccountInfo
info := Info{} info := Info{}
// generate mnemonic phrase // generate mnemonic phrase
@ -82,7 +82,7 @@ func (m *Manager) CreateAccount(password string) (generator.IdentifiedAccountInf
return mkInfo, info, "", fmt.Errorf("can not create master extended key: %v", err) return mkInfo, info, "", fmt.Errorf("can not create master extended key: %v", err)
} }
mkInfo = generator.IdentifiedAccountInfoFromExtKey(extKey) mkInfo = generator.GeneratedAccountInfoFromExtKey(mnemonic, extKey)
// import created key into account keystore // import created key into account keystore
info.WalletAddress, info.WalletPubKey, err = m.importExtendedKey(extkeys.KeyPurposeWallet, extKey, password) info.WalletAddress, info.WalletPubKey, err = m.importExtendedKey(extkeys.KeyPurposeWallet, extKey, password)

View File

@ -3,10 +3,12 @@ package generator
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/sha256" "crypto/sha256"
"time"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/extkeys" "github.com/status-im/status-go/extkeys"
"github.com/status-im/status-go/multiaccounts"
) )
type account struct { type account struct {
@ -80,11 +82,18 @@ type GeneratedAndDerivedAccountInfo struct {
Derived map[string]AccountInfo `json:"derived"` Derived map[string]AccountInfo `json:"derived"`
} }
func IdentifiedAccountInfoFromExtKey(extKey *extkeys.ExtendedKey) IdentifiedAccountInfo { func GeneratedAccountInfoFromExtKey(mnemonic string, extKey *extkeys.ExtendedKey) GeneratedAccountInfo {
acc := account{ acc := account{
privateKey: extKey.ToECDSA(), privateKey: extKey.ToECDSA(),
extendedKey: extKey, extendedKey: extKey,
} }
return acc.toIdentifiedAccountInfo("") return acc.toGeneratedAccountInfo("", mnemonic)
}
func (ga *GeneratedAccountInfo) ToMultiAccount() *multiaccounts.Account {
return &multiaccounts.Account{
Timestamp: time.Now().Unix(),
KeyUID: ga.KeyUID,
}
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts"
) )
// errors // errors
@ -15,10 +16,11 @@ var (
) )
type LoginParams struct { type LoginParams struct {
ChatAddress types.Address `json:"chatAddress"` ChatAddress types.Address `json:"chatAddress"`
Password string `json:"password"` Password string `json:"password"`
MainAccount types.Address `json:"mainAccount"` MainAccount types.Address `json:"mainAccount"`
WatchAddresses []types.Address `json:"watchAddresses"` WatchAddresses []types.Address `json:"watchAddresses"`
MultiAccount *multiaccounts.Account `json:"multiAccount"`
} }
type ErrZeroAddress struct { type ErrZeroAddress struct {

View File

@ -5,6 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/status-im/status-go/account/generator"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"os" "os"
@ -181,15 +182,20 @@ func TestBackendAccountsConcurrently(t *testing.T) {
var wgCreateAccounts sync.WaitGroup var wgCreateAccounts sync.WaitGroup
count := 3 count := 3
addressCh := make(chan [3]string, count) // use buffered channel to avoid blocking type AccountData struct {
MasterAccount generator.GeneratedAccountInfo
AccountInfo account.Info
Password string
}
addressCh := make(chan AccountData, count) // use buffered channel to avoid blocking
// create new accounts concurrently // create new accounts concurrently
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
wgCreateAccounts.Add(1) wgCreateAccounts.Add(1)
go func(pass string) { go func(pass string) {
_, accountInfo, _, err := backend.AccountManager().CreateAccount(pass) MKInfo, accountInfo, _, err := backend.AccountManager().CreateAccount(pass)
assert.NoError(t, err) assert.NoError(t, err)
addressCh <- [...]string{accountInfo.WalletAddress, accountInfo.ChatAddress, pass} addressCh <- AccountData{MKInfo, accountInfo, pass}
wgCreateAccounts.Done() wgCreateAccounts.Done()
}("password-00" + fmt.Sprint(i)) }("password-00" + fmt.Sprint(i))
} }
@ -200,17 +206,18 @@ func TestBackendAccountsConcurrently(t *testing.T) {
// select, reselect or logout concurrently // select, reselect or logout concurrently
var wg sync.WaitGroup var wg sync.WaitGroup
for tuple := range addressCh { for accountData := range addressCh {
wg.Add(1) wg.Add(1)
go func(tuple [3]string) { go func(accountData AccountData) {
loginParams := account.LoginParams{ loginParams := account.LoginParams{
MainAccount: types.HexToAddress(tuple[0]), MainAccount: types.HexToAddress(accountData.AccountInfo.WalletAddress),
ChatAddress: types.HexToAddress(tuple[1]), ChatAddress: types.HexToAddress(accountData.AccountInfo.ChatAddress),
Password: tuple[2], Password: accountData.Password,
MultiAccount: accountData.MasterAccount.ToMultiAccount(),
} }
assert.NoError(t, backend.SelectAccount(loginParams)) assert.NoError(t, backend.SelectAccount(loginParams))
wg.Done() wg.Done()
}(tuple) }(accountData)
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -235,6 +242,8 @@ func TestBackendInjectChatAccount(t *testing.T) {
require.NoError(t, backend.StopNode()) require.NoError(t, backend.StopNode())
}() }()
backend.account = &multiaccounts.Account{KeyUID: "0xdeadbeef"}
chatPrivKey, err := gethcrypto.GenerateKey() chatPrivKey, err := gethcrypto.GenerateKey()
require.NoError(t, err) require.NoError(t, err)
encryptionPrivKey, err := gethcrypto.GenerateKey() encryptionPrivKey, err := gethcrypto.GenerateKey()
@ -446,6 +455,8 @@ func TestSignHash(t *testing.T) {
require.NoError(t, backend.StopNode()) require.NoError(t, backend.StopNode())
}() }()
backend.account = &multiaccounts.Account{KeyUID: "0xdeadbeef"}
var testCases = []struct { var testCases = []struct {
name string name string
chatPrivKeyHex string chatPrivKeyHex string

View File

@ -1101,6 +1101,10 @@ func (b *GethStatusBackend) SelectAccount(loginParams account.LoginParams) error
return err return err
} }
if loginParams.MultiAccount != nil {
b.account = loginParams.MultiAccount
}
if err := b.injectAccountsIntoServices(); err != nil { if err := b.injectAccountsIntoServices(); err != nil {
return err return err
} }

View File

@ -24,7 +24,7 @@ type WhisperService interface {
type AccountManager interface { type AccountManager interface {
AddressToDecryptedAccount(string, string) (types.Account, *types.Key, error) AddressToDecryptedAccount(string, string) (types.Account, *types.Key, error)
SelectAccount(account.LoginParams) error SelectAccount(account.LoginParams) error
CreateAccount(password string) (mkInfo generator.IdentifiedAccountInfo, accountInfo account.Info, mnemonic string, err error) CreateAccount(password string) (mkInfo generator.GeneratedAccountInfo, accountInfo account.Info, mnemonic string, err error)
} }
// Service represents our own implementation of status status operations. // Service represents our own implementation of status status operations.