Fix account not being stored

This commit is contained in:
Andrea Maria Piana 2023-10-10 17:12:38 +01:00
parent 1be356af93
commit bafdf08529
5 changed files with 99 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package api
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/hex"
@ -33,6 +34,7 @@ import (
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/services/typeddata"
walletservice "github.com/status-im/status-go/services/wallet"
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/sqlite"
"github.com/status-im/status-go/t/helpers"
@ -768,7 +770,8 @@ func TestLoginAccount(t *testing.T) {
c <- struct{}{}
}
})
require.NoError(t, b.CreateAccountAndLogin(createAccountRequest))
_, err := b.CreateAccountAndLogin(createAccountRequest)
require.NoError(t, err)
require.NoError(t, b.Logout())
require.NoError(t, b.StopNode())
@ -1290,3 +1293,68 @@ func TestChangeDatabasePassword(t *testing.T) {
require.NotNil(t, key)
require.Equal(t, acc.Address, key.Address)
}
func TestCreateWallet(t *testing.T) {
utils.Init()
password := "some-password2"
tmpdir := t.TempDir()
b := NewGethStatusBackend()
createAccountRequest := &requests.CreateAccount{
DisplayName: "some-display-name",
CustomizationColor: "#ffffff",
Password: password,
BackupDisabledDataDir: tmpdir,
NetworkID: 1,
LogFilePath: tmpdir + "/log",
}
c := make(chan interface{}, 10)
signal.SetMobileSignalHandler(func(data []byte) {
if strings.Contains(string(data), "node.login") {
c <- struct{}{}
}
})
account, err := b.CreateAccountAndLogin(createAccountRequest)
require.NoError(t, err)
statusNode := b.statusNode
require.NotNil(t, statusNode)
walletService := statusNode.WalletService()
require.NotNil(t, walletService)
walletAPI := walletservice.NewAPI(walletService)
paths := []string{"m/44'/60'/0'/0/1"}
db, err := accounts.NewDB(b.appDB)
require.NoError(t, err)
walletRootAddress, err := db.GetWalletRootAddress()
require.NoError(t, err)
masterRootAddress, err := db.GetMasterAddress()
require.NoError(t, err)
fmt.Println("WALLET ROOT", walletRootAddress.String())
fmt.Println("MASTER ROOT", masterRootAddress.String())
derivedAddress, err := walletAPI.GetDerivedAddresses(context.Background(), password, walletRootAddress.String(), paths)
require.NoError(t, err)
require.Len(t, derivedAddress, 1)
fmt.Println("DERVIED", derivedAddress)
accountsService := statusNode.AccountService()
require.NotNil(t, accountsService)
accountsAPI := accountsService.AccountsAPI()
err = accountsAPI.AddAccount(context.Background(), password, &accounts.Account{
KeyUID: account.KeyUID,
Type: accounts.AccountTypeGenerated,
PublicKey: derivedAddress[0].PublicKey,
Emoji: "some",
ColorID: "so",
Name: "some name",
Path: derivedAddress[0].Path,
})
require.NoError(t, err)
}

View File

@ -43,7 +43,7 @@ func TestCreateAccountAndLogin(t *testing.T) {
err := json.Unmarshal([]byte(requestJSON), &request)
require.NoError(t, err)
statusBackend := NewGethStatusBackend()
err = statusBackend.CreateAccountAndLogin(&request)
_, err = statusBackend.CreateAccountAndLogin(&request)
require.NoError(t, err)
t.Logf("TestCreateAccountAndLogin: create account user1 and login successfully")
// wait waku node start working
@ -57,6 +57,6 @@ func TestCreateAccountAndLogin(t *testing.T) {
requestJSON = fmt.Sprintf(requestJSONTemplateString, rootDir, "user2", rootDir)
err = json.Unmarshal([]byte(requestJSON), &request)
require.NoError(t, err)
err = statusBackend.CreateAccountAndLogin(&request)
_, err = statusBackend.CreateAccountAndLogin(&request)
require.NoError(t, err)
}

View File

@ -1236,10 +1236,10 @@ func (b *GethStatusBackend) ConvertToKeycardAccount(account multiaccounts.Accoun
return nil
}
func (b *GethStatusBackend) RestoreAccountAndLogin(request *requests.RestoreAccount) error {
func (b *GethStatusBackend) RestoreAccountAndLogin(request *requests.RestoreAccount) (*multiaccounts.Account, error) {
if err := request.Validate(); err != nil {
return err
return nil, err
}
return b.generateOrImportAccount(request.Mnemonic, 0, &request.CreateAccount)
@ -1256,14 +1256,14 @@ func (b *GethStatusBackend) GetKeyUIDByMnemonic(mnemonic string) (string, error)
return info.KeyUID, nil
}
func (b *GethStatusBackend) generateOrImportAccount(mnemonic string, customizationColorClock uint64, request *requests.CreateAccount) error {
func (b *GethStatusBackend) generateOrImportAccount(mnemonic string, customizationColorClock uint64, request *requests.CreateAccount) (*multiaccounts.Account, error) {
keystoreDir := keystoreRelativePath
b.UpdateRootDataDir(request.BackupDisabledDataDir)
err := b.OpenAccounts()
if err != nil {
b.log.Error("failed open accounts", err)
return err
return nil, err
}
accountGenerator := b.accountManager.AccountsGenerator()
@ -1275,30 +1275,35 @@ func (b *GethStatusBackend) generateOrImportAccount(mnemonic string, customizati
info = generatedAccountInfos[0]
if err != nil {
return err
return nil, err
}
} else {
info, err = accountGenerator.ImportMnemonic(mnemonic, "")
if err != nil {
return err
return nil, err
}
}
derivedAddresses, err := accountGenerator.DeriveAddresses(info.ID, paths)
if err != nil {
return err
return nil, err
}
userKeyStoreDir := filepath.Join(keystoreDir, info.KeyUID)
// Initialize keystore dir with account
if err := b.accountManager.InitKeystore(filepath.Join(b.rootDataDir, userKeyStoreDir)); err != nil {
return err
return nil, err
}
_, err = accountGenerator.StoreAccount(info.ID, request.Password)
if err != nil {
return nil, err
}
_, err = accountGenerator.StoreDerivedAccounts(info.ID, request.Password, paths)
if err != nil {
return err
return nil, err
}
account := multiaccounts.Account{
@ -1311,14 +1316,14 @@ func (b *GethStatusBackend) generateOrImportAccount(mnemonic string, customizati
if request.ImagePath != "" {
iis, err := images.GenerateIdentityImages(request.ImagePath, 0, 0, 1000, 1000)
if err != nil {
return err
return nil, err
}
account.Images = iis
}
settings, err := defaultSettings(info, derivedAddresses, nil)
if err != nil {
return err
return nil, err
}
settings.DeviceName = request.DeviceName
@ -1334,7 +1339,7 @@ func (b *GethStatusBackend) generateOrImportAccount(mnemonic string, customizati
nodeConfig, err := defaultNodeConfig(settings.InstallationID, request)
if err != nil {
return err
return nil, err
}
if mnemonic != "" {
nodeConfig.ProcessBackedupMessages = true
@ -1369,17 +1374,16 @@ func (b *GethStatusBackend) generateOrImportAccount(mnemonic string, customizati
err = b.StartNodeWithAccountAndInitialConfig(account, request.Password, *settings, nodeConfig, subAccounts)
if err != nil {
b.log.Error("start node", err)
return err
return nil, err
}
return nil
return &account, nil
}
func (b *GethStatusBackend) CreateAccountAndLogin(request *requests.CreateAccount) error {
func (b *GethStatusBackend) CreateAccountAndLogin(request *requests.CreateAccount) (*multiaccounts.Account, error) {
if err := request.Validate(); err != nil {
return err
return nil, err
}
return b.generateOrImportAccount("", 1, request)
}

View File

@ -279,7 +279,7 @@ func CreateAccountAndLogin(requestJSON string) string {
api.RunAsync(func() error {
log.Debug("starting a node and creating config")
err := statusBackend.CreateAccountAndLogin(&request)
_, err := statusBackend.CreateAccountAndLogin(&request)
if err != nil {
log.Error("failed to create account", "error", err)
return err
@ -328,7 +328,7 @@ func RestoreAccountAndLogin(requestJSON string) string {
api.RunAsync(func() error {
log.Debug("starting a node and restoring account")
err := statusBackend.RestoreAccountAndLogin(&request)
_, err := statusBackend.RestoreAccountAndLogin(&request)
if err != nil {
log.Error("failed to restore account", "error", err)
return err

View File

@ -55,7 +55,7 @@ func (s *Service) APIs() []rpc.API {
{
Namespace: "accounts",
Version: "0.1.0",
Service: NewAccountsAPI(s.manager, s.config, s.db, s.feed, &s.messenger),
Service: s.AccountsAPI(),
},
{
Namespace: "multiaccounts",
@ -65,6 +65,10 @@ func (s *Service) APIs() []rpc.API {
}
}
func (s *Service) AccountsAPI() *API {
return NewAccountsAPI(s.manager, s.config, s.db, s.feed, &s.messenger)
}
// Protocols returns list of p2p protocols.
func (s *Service) Protocols() []p2p.Protocol {
return nil