Fix failing tests due to account not present in test env

This commit is contained in:
Samuel Hawksby-Robinson 2020-12-15 15:06:59 +00:00 committed by Andrea Maria Piana
parent ca80140c1a
commit e0eb96a992
6 changed files with 63 additions and 49 deletions

View File

@ -82,7 +82,8 @@ func (m *Manager) CreateAccount(password string) (generator.GeneratedAccountInfo
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.GeneratedAccountInfoFromExtKey(mnemonic, extKey) acc := generator.NewAccount(nil, extKey)
mkInfo = acc.ToGeneratedAccountInfo("", mnemonic)
// 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

@ -11,12 +11,23 @@ import (
"github.com/status-im/status-go/multiaccounts" "github.com/status-im/status-go/multiaccounts"
) )
type account struct { type Account struct {
privateKey *ecdsa.PrivateKey privateKey *ecdsa.PrivateKey
extendedKey *extkeys.ExtendedKey extendedKey *extkeys.ExtendedKey
} }
func (a *account) toAccountInfo() AccountInfo { func NewAccount(privateKey *ecdsa.PrivateKey, extKey *extkeys.ExtendedKey) Account {
if privateKey == nil {
privateKey = extKey.ToECDSA()
}
return Account{
privateKey: privateKey,
extendedKey: extKey,
}
}
func (a *Account) ToAccountInfo() AccountInfo {
publicKeyHex := types.EncodeHex(crypto.FromECDSAPub(&a.privateKey.PublicKey)) publicKeyHex := types.EncodeHex(crypto.FromECDSAPub(&a.privateKey.PublicKey))
addressHex := crypto.PubkeyToAddress(a.privateKey.PublicKey).Hex() addressHex := crypto.PubkeyToAddress(a.privateKey.PublicKey).Hex()
@ -26,8 +37,8 @@ func (a *account) toAccountInfo() AccountInfo {
} }
} }
func (a *account) toIdentifiedAccountInfo(id string) IdentifiedAccountInfo { func (a *Account) ToIdentifiedAccountInfo(id string) IdentifiedAccountInfo {
info := a.toAccountInfo() info := a.ToAccountInfo()
keyUID := sha256.Sum256(crypto.FromECDSAPub(&a.privateKey.PublicKey)) keyUID := sha256.Sum256(crypto.FromECDSAPub(&a.privateKey.PublicKey))
keyUIDHex := types.EncodeHex(keyUID[:]) keyUIDHex := types.EncodeHex(keyUID[:])
return IdentifiedAccountInfo{ return IdentifiedAccountInfo{
@ -37,8 +48,8 @@ func (a *account) toIdentifiedAccountInfo(id string) IdentifiedAccountInfo {
} }
} }
func (a *account) toGeneratedAccountInfo(id string, mnemonic string) GeneratedAccountInfo { func (a *Account) ToGeneratedAccountInfo(id string, mnemonic string) GeneratedAccountInfo {
idInfo := a.toIdentifiedAccountInfo(id) idInfo := a.ToIdentifiedAccountInfo(id)
return GeneratedAccountInfo{ return GeneratedAccountInfo{
IdentifiedAccountInfo: idInfo, IdentifiedAccountInfo: idInfo,
Mnemonic: mnemonic, Mnemonic: mnemonic,
@ -63,6 +74,13 @@ type IdentifiedAccountInfo struct {
KeyUID string `json:"keyUid"` KeyUID string `json:"keyUid"`
} }
func (iai *IdentifiedAccountInfo) ToMultiAccount() *multiaccounts.Account {
return &multiaccounts.Account{
Timestamp: time.Now().Unix(),
KeyUID: iai.KeyUID,
}
}
// GeneratedAccountInfo contains IdentifiedAccountInfo and the mnemonic of an account. // GeneratedAccountInfo contains IdentifiedAccountInfo and the mnemonic of an account.
type GeneratedAccountInfo struct { type GeneratedAccountInfo struct {
IdentifiedAccountInfo IdentifiedAccountInfo
@ -81,19 +99,3 @@ type GeneratedAndDerivedAccountInfo struct {
GeneratedAccountInfo GeneratedAccountInfo
Derived map[string]AccountInfo `json:"derived"` Derived map[string]AccountInfo `json:"derived"`
} }
func GeneratedAccountInfoFromExtKey(mnemonic string, extKey *extkeys.ExtendedKey) GeneratedAccountInfo {
acc := account{
privateKey: extKey.ToECDSA(),
extendedKey: extKey,
}
return acc.toGeneratedAccountInfo("", mnemonic)
}
func (ga *GeneratedAccountInfo) ToMultiAccount() *multiaccounts.Account {
return &multiaccounts.Account{
Timestamp: time.Now().Unix(),
KeyUID: ga.KeyUID,
}
}

View File

@ -32,14 +32,14 @@ type AccountManager interface {
type Generator struct { type Generator struct {
am AccountManager am AccountManager
accounts map[string]*account accounts map[string]*Account
sync.Mutex sync.Mutex
} }
func New(am AccountManager) *Generator { func New(am AccountManager) *Generator {
return &Generator{ return &Generator{
am: am, am: am,
accounts: make(map[string]*account), accounts: make(map[string]*Account),
} }
} }
@ -76,13 +76,13 @@ func (g *Generator) ImportPrivateKey(privateKeyHex string) (IdentifiedAccountInf
return IdentifiedAccountInfo{}, err return IdentifiedAccountInfo{}, err
} }
acc := &account{ acc := &Account{
privateKey: privateKey, privateKey: privateKey,
} }
id := g.addAccount(acc) id := g.addAccount(acc)
return acc.toIdentifiedAccountInfo(id), nil return acc.ToIdentifiedAccountInfo(id), nil
} }
func (g *Generator) ImportJSONKey(json string, password string) (IdentifiedAccountInfo, error) { func (g *Generator) ImportJSONKey(json string, password string) (IdentifiedAccountInfo, error) {
@ -91,13 +91,13 @@ func (g *Generator) ImportJSONKey(json string, password string) (IdentifiedAccou
return IdentifiedAccountInfo{}, err return IdentifiedAccountInfo{}, err
} }
acc := &account{ acc := &Account{
privateKey: key.PrivateKey, privateKey: key.PrivateKey,
} }
id := g.addAccount(acc) id := g.addAccount(acc)
return acc.toIdentifiedAccountInfo(id), nil return acc.ToIdentifiedAccountInfo(id), nil
} }
func (g *Generator) ImportMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error) { func (g *Generator) ImportMnemonic(mnemonicPhrase string, bip39Passphrase string) (GeneratedAccountInfo, error) {
@ -107,14 +107,14 @@ func (g *Generator) ImportMnemonic(mnemonicPhrase string, bip39Passphrase string
return GeneratedAccountInfo{}, fmt.Errorf("can not create master extended key: %v", err) return GeneratedAccountInfo{}, fmt.Errorf("can not create master extended key: %v", err)
} }
acc := &account{ acc := &Account{
privateKey: masterExtendedKey.ToECDSA(), privateKey: masterExtendedKey.ToECDSA(),
extendedKey: masterExtendedKey, extendedKey: masterExtendedKey,
} }
id := g.addAccount(acc) id := g.addAccount(acc)
return acc.toGeneratedAccountInfo(id, mnemonicPhrase), nil return acc.ToGeneratedAccountInfo(id, mnemonicPhrase), nil
} }
func (g *Generator) GenerateAndDeriveAddresses(mnemonicPhraseLength int, n int, bip39Passphrase string, pathStrings []string) ([]GeneratedAndDerivedAccountInfo, error) { func (g *Generator) GenerateAndDeriveAddresses(mnemonicPhraseLength int, n int, bip39Passphrase string, pathStrings []string) ([]GeneratedAndDerivedAccountInfo, error) {
@ -152,7 +152,7 @@ func (g *Generator) DeriveAddresses(accountID string, pathStrings []string) (map
pathAccountsInfo := make(map[string]AccountInfo) pathAccountsInfo := make(map[string]AccountInfo)
for pathString, childAccount := range pathAccounts { for pathString, childAccount := range pathAccounts {
pathAccountsInfo[pathString] = childAccount.toAccountInfo() pathAccountsInfo[pathString] = childAccount.ToAccountInfo()
} }
return pathAccountsInfo, nil return pathAccountsInfo, nil
@ -214,18 +214,18 @@ func (g *Generator) LoadAccount(address string, password string) (IdentifiedAcco
return IdentifiedAccountInfo{}, err return IdentifiedAccountInfo{}, err
} }
acc := &account{ acc := &Account{
privateKey: key.PrivateKey, privateKey: key.PrivateKey,
extendedKey: key.ExtendedKey, extendedKey: key.ExtendedKey,
} }
id := g.addAccount(acc) id := g.addAccount(acc)
return acc.toIdentifiedAccountInfo(id), nil return acc.ToIdentifiedAccountInfo(id), nil
} }
func (g *Generator) deriveChildAccounts(acc *account, pathStrings []string) (map[string]*account, error) { func (g *Generator) deriveChildAccounts(acc *Account, pathStrings []string) (map[string]*Account, error) {
pathAccounts := make(map[string]*account) pathAccounts := make(map[string]*Account)
for _, pathString := range pathStrings { for _, pathString := range pathStrings {
childAccount, err := g.deriveChildAccount(acc, pathString) childAccount, err := g.deriveChildAccount(acc, pathString)
@ -239,7 +239,7 @@ func (g *Generator) deriveChildAccounts(acc *account, pathStrings []string) (map
return pathAccounts, nil return pathAccounts, nil
} }
func (g *Generator) deriveChildAccount(acc *account, pathString string) (*account, error) { func (g *Generator) deriveChildAccount(acc *Account, pathString string) (*Account, error) {
_, path, err := decodePath(pathString) _, path, err := decodePath(pathString)
if err != nil { if err != nil {
return nil, err return nil, err
@ -258,13 +258,13 @@ func (g *Generator) deriveChildAccount(acc *account, pathString string) (*accoun
return nil, err return nil, err
} }
return &account{ return &Account{
privateKey: childExtendedKey.ToECDSA(), privateKey: childExtendedKey.ToECDSA(),
extendedKey: childExtendedKey, extendedKey: childExtendedKey,
}, nil }, nil
} }
func (g *Generator) store(acc *account, password string) (AccountInfo, error) { func (g *Generator) store(acc *Account, password string) (AccountInfo, error) {
if acc.extendedKey != nil { if acc.extendedKey != nil {
if _, _, err := g.am.ImportSingleExtendedKey(acc.extendedKey, password); err != nil { if _, _, err := g.am.ImportSingleExtendedKey(acc.extendedKey, password); err != nil {
return AccountInfo{}, err return AccountInfo{}, err
@ -277,10 +277,10 @@ func (g *Generator) store(acc *account, password string) (AccountInfo, error) {
g.Reset() g.Reset()
return acc.toAccountInfo(), nil return acc.ToAccountInfo(), nil
} }
func (g *Generator) addAccount(acc *account) string { func (g *Generator) addAccount(acc *Account) string {
g.Lock() g.Lock()
defer g.Unlock() defer g.Unlock()
@ -295,10 +295,10 @@ func (g *Generator) Reset() {
g.Lock() g.Lock()
defer g.Unlock() defer g.Unlock()
g.accounts = make(map[string]*account) g.accounts = make(map[string]*Account)
} }
func (g *Generator) findAccount(accountID string) (*account, error) { func (g *Generator) findAccount(accountID string) (*Account, error) {
g.Lock() g.Lock()
defer g.Unlock() defer g.Unlock()

View File

@ -19,6 +19,10 @@ type Account struct {
Images []images.IdentityImage `json:"images"` Images []images.IdentityImage `json:"images"`
} }
type MultiAccountMarshaller interface {
ToMultiAccount() *Account
}
type Database struct { type Database struct {
db *sql.DB db *sql.DB
} }

View File

@ -3,7 +3,6 @@ package protocol
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/sha256"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
@ -20,7 +19,7 @@ import (
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"go.uber.org/zap" "go.uber.org/zap"
gethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/status-im/status-go/account/generator"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth" gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
coretypes "github.com/status-im/status-go/eth-node/core/types" coretypes "github.com/status-im/status-go/eth-node/core/types"
"github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/crypto"
@ -119,11 +118,15 @@ func (s *MessengerSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.P
madb, err := multiaccounts.InitializeDB(tmpfile.Name()) madb, err := multiaccounts.InitializeDB(tmpfile.Name())
s.Require().NoError(err) s.Require().NoError(err)
acc := generator.NewAccount(privateKey, nil)
iai := acc.ToIdentifiedAccountInfo("")
options := []Option{ options := []Option{
WithCustomLogger(s.logger), WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(), WithMessagesPersistenceEnabled(),
WithDatabaseConfig(":memory:", "some-key"), WithDatabaseConfig(":memory:", "some-key"),
WithMultiAccounts(madb), WithMultiAccounts(madb),
WithAccount(iai.ToMultiAccount()),
} }
if s.enableDataSync { if s.enableDataSync {
options = append(options, WithDatasync()) options = append(options, WithDatasync())
@ -2521,8 +2524,8 @@ func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bo
} }
func (s *MessengerSuite) TestChatIdentity() { func (s *MessengerSuite) TestChatIdentity() {
keyUIDBytes := sha256.Sum256(gethcrypto.FromECDSAPub(&s.m.identity.PublicKey)) keyUID := "0xdeadbeef"
keyUID := types.EncodeHex(keyUIDBytes[:]) s.m.account = &multiaccounts.Account{KeyUID: keyUID}
err := s.m.multiAccounts.SaveAccount(multiaccounts.Account{Name: "string", KeyUID: keyUID}) err := s.m.multiAccounts.SaveAccount(multiaccounts.Account{Name: "string", KeyUID: keyUID})
s.Require().NoError(err) s.Require().NoError(err)

View File

@ -135,7 +135,9 @@ func TestInitProtocol(t *testing.T) {
multiAccounts, err := multiaccounts.InitializeDB(tmpfile.Name()) multiAccounts, err := multiaccounts.InitializeDB(tmpfile.Name())
require.NoError(t, err) require.NoError(t, err)
err = service.InitProtocol(privateKey, sqlDB, multiAccounts, zap.NewNop()) acc := &multiaccounts.Account{ KeyUID: "0xdeadbeef" }
err = service.InitProtocol(privateKey, sqlDB, multiAccounts, acc, zap.NewNop())
require.NoError(t, err) require.NoError(t, err)
} }
@ -196,7 +198,9 @@ func (s *ShhExtSuite) createAndAddNode() {
privateKey, err := crypto.GenerateKey() privateKey, err := crypto.GenerateKey()
s.NoError(err) s.NoError(err)
err = service.InitProtocol(privateKey, sqlDB, multiAccounts, zap.NewNop()) acc := &multiaccounts.Account{ KeyUID: "0xdeadbeef" }
err = service.InitProtocol(privateKey, sqlDB, multiAccounts, acc, zap.NewNop())
s.NoError(err) s.NoError(err)
err = stack.Register(func(n *node.ServiceContext) (node.Service, error) { err = stack.Register(func(n *node.ServiceContext) (node.Service, error) {