diff --git a/account/accounts.go b/account/accounts.go index 1ee931139..08d348c1d 100644 --- a/account/accounts.go +++ b/account/accounts.go @@ -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) } - mkInfo = generator.GeneratedAccountInfoFromExtKey(mnemonic, extKey) + acc := generator.NewAccount(nil, extKey) + mkInfo = acc.ToGeneratedAccountInfo("", mnemonic) // import created key into account keystore info.WalletAddress, info.WalletPubKey, err = m.importExtendedKey(extkeys.KeyPurposeWallet, extKey, password) diff --git a/account/generator/account.go b/account/generator/account.go index c1d1166a5..0c4c119e8 100644 --- a/account/generator/account.go +++ b/account/generator/account.go @@ -11,12 +11,23 @@ import ( "github.com/status-im/status-go/multiaccounts" ) -type account struct { +type Account struct { privateKey *ecdsa.PrivateKey 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)) addressHex := crypto.PubkeyToAddress(a.privateKey.PublicKey).Hex() @@ -26,8 +37,8 @@ func (a *account) toAccountInfo() AccountInfo { } } -func (a *account) toIdentifiedAccountInfo(id string) IdentifiedAccountInfo { - info := a.toAccountInfo() +func (a *Account) ToIdentifiedAccountInfo(id string) IdentifiedAccountInfo { + info := a.ToAccountInfo() keyUID := sha256.Sum256(crypto.FromECDSAPub(&a.privateKey.PublicKey)) keyUIDHex := types.EncodeHex(keyUID[:]) return IdentifiedAccountInfo{ @@ -37,8 +48,8 @@ func (a *account) toIdentifiedAccountInfo(id string) IdentifiedAccountInfo { } } -func (a *account) toGeneratedAccountInfo(id string, mnemonic string) GeneratedAccountInfo { - idInfo := a.toIdentifiedAccountInfo(id) +func (a *Account) ToGeneratedAccountInfo(id string, mnemonic string) GeneratedAccountInfo { + idInfo := a.ToIdentifiedAccountInfo(id) return GeneratedAccountInfo{ IdentifiedAccountInfo: idInfo, Mnemonic: mnemonic, @@ -63,6 +74,13 @@ type IdentifiedAccountInfo struct { 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. type GeneratedAccountInfo struct { IdentifiedAccountInfo @@ -81,19 +99,3 @@ type GeneratedAndDerivedAccountInfo struct { GeneratedAccountInfo 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, - } -} diff --git a/account/generator/generator.go b/account/generator/generator.go index 9060bb23c..16894b466 100644 --- a/account/generator/generator.go +++ b/account/generator/generator.go @@ -32,14 +32,14 @@ type AccountManager interface { type Generator struct { am AccountManager - accounts map[string]*account + accounts map[string]*Account sync.Mutex } func New(am AccountManager) *Generator { return &Generator{ 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 } - acc := &account{ + acc := &Account{ privateKey: privateKey, } id := g.addAccount(acc) - return acc.toIdentifiedAccountInfo(id), nil + return acc.ToIdentifiedAccountInfo(id), nil } 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 } - acc := &account{ + acc := &Account{ privateKey: key.PrivateKey, } id := g.addAccount(acc) - return acc.toIdentifiedAccountInfo(id), nil + return acc.ToIdentifiedAccountInfo(id), nil } 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) } - acc := &account{ + acc := &Account{ privateKey: masterExtendedKey.ToECDSA(), extendedKey: masterExtendedKey, } 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) { @@ -152,7 +152,7 @@ func (g *Generator) DeriveAddresses(accountID string, pathStrings []string) (map pathAccountsInfo := make(map[string]AccountInfo) for pathString, childAccount := range pathAccounts { - pathAccountsInfo[pathString] = childAccount.toAccountInfo() + pathAccountsInfo[pathString] = childAccount.ToAccountInfo() } return pathAccountsInfo, nil @@ -214,18 +214,18 @@ func (g *Generator) LoadAccount(address string, password string) (IdentifiedAcco return IdentifiedAccountInfo{}, err } - acc := &account{ + acc := &Account{ privateKey: key.PrivateKey, extendedKey: key.ExtendedKey, } 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) { - pathAccounts := make(map[string]*account) +func (g *Generator) deriveChildAccounts(acc *Account, pathStrings []string) (map[string]*Account, error) { + pathAccounts := make(map[string]*Account) for _, pathString := range pathStrings { childAccount, err := g.deriveChildAccount(acc, pathString) @@ -239,7 +239,7 @@ func (g *Generator) deriveChildAccounts(acc *account, pathStrings []string) (map 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) if err != nil { return nil, err @@ -258,13 +258,13 @@ func (g *Generator) deriveChildAccount(acc *account, pathString string) (*accoun return nil, err } - return &account{ + return &Account{ privateKey: childExtendedKey.ToECDSA(), extendedKey: childExtendedKey, }, 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 _, _, err := g.am.ImportSingleExtendedKey(acc.extendedKey, password); err != nil { return AccountInfo{}, err @@ -277,10 +277,10 @@ func (g *Generator) store(acc *account, password string) (AccountInfo, error) { 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() defer g.Unlock() @@ -295,10 +295,10 @@ func (g *Generator) Reset() { g.Lock() 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() defer g.Unlock() diff --git a/multiaccounts/database.go b/multiaccounts/database.go index 271f2791e..984311bdf 100644 --- a/multiaccounts/database.go +++ b/multiaccounts/database.go @@ -19,6 +19,10 @@ type Account struct { Images []images.IdentityImage `json:"images"` } +type MultiAccountMarshaller interface { + ToMultiAccount() *Account +} + type Database struct { db *sql.DB } diff --git a/protocol/messenger_test.go b/protocol/messenger_test.go index 98924ff6d..f300b4bcc 100644 --- a/protocol/messenger_test.go +++ b/protocol/messenger_test.go @@ -3,7 +3,6 @@ package protocol import ( "context" "crypto/ecdsa" - "crypto/sha256" "encoding/hex" "encoding/json" "errors" @@ -20,7 +19,7 @@ import ( "github.com/stretchr/testify/suite" "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" coretypes "github.com/status-im/status-go/eth-node/core/types" "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()) s.Require().NoError(err) + acc := generator.NewAccount(privateKey, nil) + iai := acc.ToIdentifiedAccountInfo("") + options := []Option{ WithCustomLogger(s.logger), WithMessagesPersistenceEnabled(), WithDatabaseConfig(":memory:", "some-key"), WithMultiAccounts(madb), + WithAccount(iai.ToMultiAccount()), } if s.enableDataSync { options = append(options, WithDatasync()) @@ -2521,8 +2524,8 @@ func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bo } func (s *MessengerSuite) TestChatIdentity() { - keyUIDBytes := sha256.Sum256(gethcrypto.FromECDSAPub(&s.m.identity.PublicKey)) - keyUID := types.EncodeHex(keyUIDBytes[:]) + keyUID := "0xdeadbeef" + s.m.account = &multiaccounts.Account{KeyUID: keyUID} err := s.m.multiAccounts.SaveAccount(multiaccounts.Account{Name: "string", KeyUID: keyUID}) s.Require().NoError(err) diff --git a/services/wakuext/api_test.go b/services/wakuext/api_test.go index 2f3419ab0..b1bb7a7e0 100644 --- a/services/wakuext/api_test.go +++ b/services/wakuext/api_test.go @@ -135,7 +135,9 @@ func TestInitProtocol(t *testing.T) { multiAccounts, err := multiaccounts.InitializeDB(tmpfile.Name()) 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) } @@ -196,7 +198,9 @@ func (s *ShhExtSuite) createAndAddNode() { privateKey, err := crypto.GenerateKey() 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) err = stack.Register(func(n *node.ServiceContext) (node.Service, error) {