From f325e95bf105b018aaff5871e375b800a8b2e279 Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 7 Aug 2024 17:13:21 +0800 Subject: [PATCH] fix_: enable light client for mobile (#5654) * fix_: enable light client for mobile * fix_: rename const name * fix_: disable store confirmation for mobile v1 by default --- ...obile_user_upgrading_from_v1_to_v2_test.go | 139 +++++++++++------- appdatabase/database.go | 14 ++ common/devices.go | 5 + 3 files changed, 102 insertions(+), 56 deletions(-) diff --git a/api/old_mobile_user_upgrading_from_v1_to_v2_test.go b/api/old_mobile_user_upgrading_from_v1_to_v2_test.go index 57e9f9d54..b51fcb1c4 100644 --- a/api/old_mobile_user_upgrading_from_v1_to_v2_test.go +++ b/api/old_mobile_user_upgrading_from_v1_to_v2_test.go @@ -6,6 +6,8 @@ import ( "strings" "testing" + d_common "github.com/status-im/status-go/common" + "github.com/status-im/status-go/appdatabase" "github.com/status-im/status-go/common/dbsetup" "github.com/status-im/status-go/sqlite" @@ -46,6 +48,8 @@ type OldMobileUserUpgradingFromV1ToV2Test struct { tmpdir string } +type PostLoginCheckCallback func(b *GethStatusBackend) + func (s *OldMobileUserUpgradingFromV1ToV2Test) SetupTest() { utils.Init() s.tmpdir = s.T().TempDir() @@ -56,72 +60,95 @@ func TestOldMobileUserUpgradingFromV1ToV2(t *testing.T) { suite.Run(t, new(OldMobileUserUpgradingFromV1ToV2Test)) } -func (s *OldMobileUserUpgradingFromV1ToV2Test) loginMobileUser() { +func (s *OldMobileUserUpgradingFromV1ToV2Test) loginMobileUser(check PostLoginCheckCallback) { b := NewGethStatusBackend() b.UpdateRootDataDir(s.tmpdir) s.Require().NoError(b.OpenAccounts()) s.Require().NoError(b.Login(oldMobileUserKeyUID, oldMobileUserPasswd)) - db, err := accounts.NewDB(b.appDB) - s.Require().NoError(err) - accs, err := db.GetAllAccounts() - s.Require().NoError(err) - s.Require().True(len(accs) == 6) - kps, err := db.GetAllKeypairs() - s.Require().NoError(err) - s.Require().True(len(kps) == 3) - // Create a map to categorize keypairs by their type - keypairMap := make(map[accounts.KeypairType][]*accounts.Keypair) - for _, kp := range kps { - keypairMap[kp.Type] = append(keypairMap[kp.Type], kp) - } + check(b) - // Check profile keypair - profileKps, ok := keypairMap[accounts.KeypairTypeProfile] - s.Require().True(ok, "Profile keypair not found") - s.Require().True(len(profileKps) == 1, "Unexpected number of profile keypairs") - s.Require().True(len(profileKps[0].Accounts) == 3) - for _, a := range profileKps[0].Accounts { - s.Require().Equal(a.KeyUID, oldMobileUserKeyUID) - } - - generator := b.AccountManager().AccountsGenerator() - // Check seed keypair - seedKps, ok := keypairMap[accounts.KeypairTypeSeed] - s.Require().True(ok, "Seed keypair not found") - s.Require().True(len(seedKps) == 1, "Unexpected number of seed keypairs") - s.Require().True(len(seedKps[0].Accounts) == 1) - info, err := generator.LoadAccount(seedKps[0].Accounts[0].Address.Hex(), oldMobileUserPasswd) - s.Require().NoError(err) - s.Require().Equal(seedKps[0].KeyUID, info.KeyUID) - s.Require().Equal(seedKps[0].Accounts[0].KeyUID, info.KeyUID) - mnemonicNoExtraSpaces := strings.Join(strings.Fields("vocal blouse script census island armor seek catch wool narrow peasant attract"), " ") - importedSeedAccountInfo, err := generator.ImportMnemonic(mnemonicNoExtraSpaces, "") - s.Require().NoError(err) - derivedAddresses, err := generator.DeriveAddresses(importedSeedAccountInfo.ID, paths) - s.Require().NoError(err) - s.Require().Equal(derivedAddresses[pathDefaultWallet].PublicKey, "0x04fde3e58a7379161da2adf033fbee076e2ba11fca8b07c4d06610b399911a60017e4c108eae243487d19e273f99c2d6af13ff5e330783f4389212092b01cc616c") - //following line shows: we're unable to calculate the right KeyUID with the wrong public key from existing records for the imported seed account - s.Require().False(importedSeedAccountInfo.KeyUID == seedKps[0].KeyUID) - - // Check key keypair - keyKps, ok := keypairMap[accounts.KeypairTypeKey] - s.Require().True(ok, "Key keypair not found") - s.Require().True(len(keyKps) == 1, "Unexpected number of key keypairs") - s.Require().True(len(keyKps[0].Accounts) == 1) - info, err = generator.LoadAccount(keyKps[0].Accounts[0].Address.Hex(), oldMobileUserPasswd) - s.Require().NoError(err) - s.Require().Equal(keyKps[0].KeyUID, info.KeyUID) - s.Require().Equal(keyKps[0].Accounts[0].KeyUID, info.KeyUID) - info, err = generator.ImportPrivateKey("c3ad0b50652318f845565c13761e5369ce75dcbc2a94616e15b829d4b07410fe") - s.Require().NoError(err) - s.Require().Equal(info.KeyUID, keyKps[0].KeyUID) s.Require().NoError(b.Logout()) } +func (s *OldMobileUserUpgradingFromV1ToV2Test) TestOptimizeMobileWakuV2SettingsForMobileV1() { + bkFunc := d_common.IsMobilePlatform + d_common.IsMobilePlatform = func() bool { + return true + } + defer func() { + d_common.IsMobilePlatform = bkFunc + }() + + s.loginMobileUser(func(b *GethStatusBackend) { + nc, err := b.GetNodeConfig() + s.Require().NoError(err) + s.Require().True(nc.WakuV2Config.LightClient) + s.Require().False(nc.WakuV2Config.EnableStoreConfirmationForMessagesSent) + }) +} + func (s *OldMobileUserUpgradingFromV1ToV2Test) TestLoginAndMigrationsStillWorkWithExistingMobileUser() { - s.loginMobileUser() - s.loginMobileUser() // Login twice to catch weird errors that only appear after logout + checkAfterLogin := func(b *GethStatusBackend) { + db, err := accounts.NewDB(b.appDB) + s.Require().NoError(err) + accs, err := db.GetAllAccounts() + s.Require().NoError(err) + s.Require().True(len(accs) == 6) + kps, err := db.GetAllKeypairs() + s.Require().NoError(err) + s.Require().True(len(kps) == 3) + + // Create a map to categorize keypairs by their type + keypairMap := make(map[accounts.KeypairType][]*accounts.Keypair) + for _, kp := range kps { + keypairMap[kp.Type] = append(keypairMap[kp.Type], kp) + } + + // Check profile keypair + profileKps, ok := keypairMap[accounts.KeypairTypeProfile] + s.Require().True(ok, "Profile keypair not found") + s.Require().True(len(profileKps) == 1, "Unexpected number of profile keypairs") + s.Require().True(len(profileKps[0].Accounts) == 3) + for _, a := range profileKps[0].Accounts { + s.Require().Equal(a.KeyUID, oldMobileUserKeyUID) + } + + generator := b.AccountManager().AccountsGenerator() + // Check seed keypair + seedKps, ok := keypairMap[accounts.KeypairTypeSeed] + s.Require().True(ok, "Seed keypair not found") + s.Require().True(len(seedKps) == 1, "Unexpected number of seed keypairs") + s.Require().True(len(seedKps[0].Accounts) == 1) + info, err := generator.LoadAccount(seedKps[0].Accounts[0].Address.Hex(), oldMobileUserPasswd) + s.Require().NoError(err) + s.Require().Equal(seedKps[0].KeyUID, info.KeyUID) + s.Require().Equal(seedKps[0].Accounts[0].KeyUID, info.KeyUID) + mnemonicNoExtraSpaces := strings.Join(strings.Fields("vocal blouse script census island armor seek catch wool narrow peasant attract"), " ") + importedSeedAccountInfo, err := generator.ImportMnemonic(mnemonicNoExtraSpaces, "") + s.Require().NoError(err) + derivedAddresses, err := generator.DeriveAddresses(importedSeedAccountInfo.ID, paths) + s.Require().NoError(err) + s.Require().Equal(derivedAddresses[pathDefaultWallet].PublicKey, "0x04fde3e58a7379161da2adf033fbee076e2ba11fca8b07c4d06610b399911a60017e4c108eae243487d19e273f99c2d6af13ff5e330783f4389212092b01cc616c") + //following line shows: we're unable to calculate the right KeyUID with the wrong public key from existing records for the imported seed account + s.Require().False(importedSeedAccountInfo.KeyUID == seedKps[0].KeyUID) + + // Check key keypair + keyKps, ok := keypairMap[accounts.KeypairTypeKey] + s.Require().True(ok, "Key keypair not found") + s.Require().True(len(keyKps) == 1, "Unexpected number of key keypairs") + s.Require().True(len(keyKps[0].Accounts) == 1) + info, err = generator.LoadAccount(keyKps[0].Accounts[0].Address.Hex(), oldMobileUserPasswd) + s.Require().NoError(err) + s.Require().Equal(keyKps[0].KeyUID, info.KeyUID) + s.Require().Equal(keyKps[0].Accounts[0].KeyUID, info.KeyUID) + info, err = generator.ImportPrivateKey("c3ad0b50652318f845565c13761e5369ce75dcbc2a94616e15b829d4b07410fe") + s.Require().NoError(err) + s.Require().Equal(info.KeyUID, keyKps[0].KeyUID) + } + + s.loginMobileUser(checkAfterLogin) + s.loginMobileUser(checkAfterLogin) // Login twice to catch weird errors that only appear after logout } // TestAddWalletAccount we should be able to add a wallet account after upgrading from mobile v1 diff --git a/appdatabase/database.go b/appdatabase/database.go index dbcce7424..fbbe66e67 100644 --- a/appdatabase/database.go +++ b/appdatabase/database.go @@ -6,6 +6,8 @@ import ( "encoding/json" "math/big" + d_common "github.com/status-im/status-go/common" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" @@ -61,6 +63,7 @@ func doMigration(db *sql.DB) error { postSteps := []*sqlite.PostStep{ {Version: 1662365868, CustomMigration: FixMissingKeyUIDForAccounts}, + {Version: 1720606449, CustomMigration: OptimizeMobileWakuV2SettingsForMobileV1}, } postSteps = append(postSteps, customSteps...) // Run all the new migrations @@ -87,6 +90,17 @@ func InitializeDB(path, password string, kdfIterationsNumber int) (*sql.DB, erro return db, nil } +func OptimizeMobileWakuV2SettingsForMobileV1(sqlTx *sql.Tx) error { + if d_common.IsMobilePlatform() { + _, err := sqlTx.Exec(`UPDATE wakuv2_config SET light_client = ?, enable_store_confirmation_for_messages_sent = ?`, true, false) + if err != nil { + log.Error("failed to enable light client and disable store confirmation for mobile v1", "err", err.Error()) + return err + } + } + return nil +} + func FixMissingKeyUIDForAccounts(sqlTx *sql.Tx) error { rows, err := sqlTx.Query(`SELECT address,pubkey FROM accounts WHERE pubkey IS NOT NULL AND type != '' AND type != 'generated'`) if err != nil { diff --git a/common/devices.go b/common/devices.go index 53fdb8bab..97c1c661b 100644 --- a/common/devices.go +++ b/common/devices.go @@ -4,9 +4,14 @@ import "runtime" const ( AndroidPlatform = "android" + IOSPlatform = "ios" WindowsPlatform = "windows" ) +var IsMobilePlatform = func() bool { + return OperatingSystemIs(AndroidPlatform) || OperatingSystemIs(IOSPlatform) +} + func OperatingSystemIs(targetOS string) bool { return runtime.GOOS == targetOS }