From 54bc1f72e156d4074e4492527fabab3913d12cb4 Mon Sep 17 00:00:00 2001 From: Anton Paymyshev Date: Mon, 2 Oct 2023 16:00:52 +0700 Subject: [PATCH] Account discovery to wallet service (#20324) * Run account discovery from wallet service * Cleanup KeyringInfo --- .../chrome/browser/app/domain/DappsModel.java | 2 +- .../browser/app/domain/KeyringModel.java | 2 +- .../observers/KeyringServiceObserverImpl.java | 6 +- .../asset_discovery_manager_unittest.cc | 10 +- .../brave_wallet_event_emitter_browsertest.cc | 53 +- .../brave_wallet_service_unittest.cc | 46 +- .../brave_wallet_sign_message_browsertest.cc | 14 +- .../eth_allowance_manager_unittest.cc | 19 +- .../ethereum_provider_browsertest.cc | 14 +- .../ethereum_provider_impl_unittest.cc | 29 +- .../external_wallets_importer_unittest.cc | 9 +- .../brave_wallet/keyring_service_unittest.cc | 722 ++++++------------ .../wallet_notification_service_unittest.cc | 9 +- .../send_or_sign_transaction_browsertest.cc | 16 +- .../solana_provider_browsertest.cc | 13 +- .../wallet_watch_asset_browsertest.cc | 14 +- .../wallet_button_notification_source.cc | 18 +- .../wallet_button_notification_source.h | 4 +- ..._button_notification_source_browsertest.cc | 19 +- .../common_handler/wallet_handler.cc | 5 +- .../android_page_appearing_browsertest.cc | 10 +- .../browser/brave_wallet_service.cc | 75 +- .../browser/brave_wallet_service.h | 8 + .../browser/eth_allowance_manager.cc | 8 +- .../browser/eth_tx_manager_unittest.cc | 9 +- .../browser/ethereum_provider_impl.cc | 14 +- .../browser/fil_tx_manager_unittest.cc | 11 +- .../brave_wallet/browser/keyring_service.cc | 31 +- .../brave_wallet/browser/keyring_service.h | 7 +- .../browser/keyring_service_observer_base.h | 2 +- .../browser/solana_keyring_unittest.cc | 13 +- .../browser/solana_provider_impl.cc | 25 +- .../browser/solana_transaction_unittest.cc | 11 +- .../browser/solana_tx_manager_unittest.cc | 8 +- components/brave_wallet/browser/test_utils.cc | 35 + components/brave_wallet/browser/test_utils.h | 14 + .../brave_wallet/common/brave_wallet.mojom | 5 +- .../brave_wallet/common/eth_request_helper.cc | 4 +- .../brave_wallet/common/eth_request_helper.h | 2 +- .../common/eth_request_helper_unittest.cc | 66 +- .../common/actions/wallet_actions.ts | 2 +- .../brave_wallet_ui/common/async/handlers.ts | 2 +- .../common/slices/wallet.slice.ts | 2 +- .../common/wallet_api_proxy_observers.ts | 4 +- 44 files changed, 545 insertions(+), 847 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/app/domain/DappsModel.java b/android/java/org/chromium/chrome/browser/app/domain/DappsModel.java index 2597d7a86ee2..69b53a90abe1 100644 --- a/android/java/org/chromium/chrome/browser/app/domain/DappsModel.java +++ b/android/java/org/chromium/chrome/browser/app/domain/DappsModel.java @@ -299,7 +299,7 @@ private void updatePendingAccountCreationRequest() { public void keyringCreated(@KeyringId.EnumType int keyringId) {} @Override - public void keyringRestored(@KeyringId.EnumType int keyringId) {} + public void walletRestored() {} @Override public void keyringReset() {} diff --git a/android/java/org/chromium/chrome/browser/app/domain/KeyringModel.java b/android/java/org/chromium/chrome/browser/app/domain/KeyringModel.java index 78f86234d158..0d440f91fcf2 100644 --- a/android/java/org/chromium/chrome/browser/app/domain/KeyringModel.java +++ b/android/java/org/chromium/chrome/browser/app/domain/KeyringModel.java @@ -165,7 +165,7 @@ public void keyringCreated(@KeyringId.EnumType int keyringId) { } @Override - public void keyringRestored(@KeyringId.EnumType int keyringId) { + public void walletRestored() { update(); } diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/observers/KeyringServiceObserverImpl.java b/android/java/org/chromium/chrome/browser/crypto_wallet/observers/KeyringServiceObserverImpl.java index 4052d1f096f5..a558e803ed25 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/observers/KeyringServiceObserverImpl.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/observers/KeyringServiceObserverImpl.java @@ -18,7 +18,7 @@ public interface KeyringServiceObserverImplDelegate { default void locked() {} default void backedUp() {} default void keyringCreated(@KeyringId.EnumType int keyringId) {} - default void keyringRestored(@KeyringId.EnumType int keyringId) {} + default void walletRestored() {} default void keyringReset() {} default void unlocked() {} default void accountsChanged() {} @@ -41,8 +41,8 @@ public void keyringCreated(@KeyringId.EnumType int keyringId) { } @Override - public void keyringRestored(@KeyringId.EnumType int keyringId) { - if (isActive()) getRef().keyringRestored(keyringId); + public void walletRestored() { + if (isActive()) getRef().walletRestored(); } @Override diff --git a/browser/brave_wallet/asset_discovery_manager_unittest.cc b/browser/brave_wallet/asset_discovery_manager_unittest.cc index 5c47e4285d71..512e6bf60048 100644 --- a/browser/brave_wallet/asset_discovery_manager_unittest.cc +++ b/browser/brave_wallet/asset_discovery_manager_unittest.cc @@ -39,12 +39,6 @@ namespace brave_wallet { -namespace { - -const char kPasswordBrave[] = "brave"; - -} // namespace - class TestBraveWalletServiceObserverForAssetDiscoveryManager : public brave_wallet::BraveWalletServiceObserverBase { public: @@ -303,8 +297,8 @@ TEST_F(AssetDiscoveryManagerUnitTest, AccountsAdded) { base::Time current_assets_last_discovered_at = GetPrefs()->GetTime(kBraveWalletLastDiscoveredAssetsAt); ASSERT_EQ(current_assets_last_discovered_at, base::Time()); - keyring_service_->RestoreWallet(kMnemonicDivideCruise, kPasswordBrave, false, - base::DoNothing()); + ASSERT_TRUE(keyring_service_->RestoreWalletSync(kMnemonicDivideCruise, + kTestWalletPassword, false)); wallet_service_observer_->WaitForOnDiscoverAssetsCompleted({}); base::Time previous_assets_last_discovered_at = current_assets_last_discovered_at; diff --git a/browser/brave_wallet/brave_wallet_event_emitter_browsertest.cc b/browser/brave_wallet/brave_wallet_event_emitter_browsertest.cc index 2aca6117cfc4..96c863de6f4b 100644 --- a/browser/brave_wallet/brave_wallet_event_emitter_browsertest.cc +++ b/browser/brave_wallet/brave_wallet_event_emitter_browsertest.cc @@ -16,6 +16,7 @@ #include "brave/components/brave_wallet/browser/json_rpc_service.h" #include "brave/components/brave_wallet/browser/keyring_service.h" #include "brave/components/brave_wallet/browser/permission_utils.h" +#include "brave/components/brave_wallet/browser/test_utils.h" #include "brave/components/brave_wallet/common/common_utils.h" #include "brave/components/brave_wallet/common/features.h" #include "brave/components/constants/brave_paths.h" @@ -128,48 +129,15 @@ class BraveWalletEventEmitterTest : public InProcessBrowserTest { return url::Origin::Create(web_contents()->GetLastCommittedURL()); } - void RestoreWallet() { - const char mnemonic[] = - "drip caution abandon festival order clown oven regular absorb " - "evidence crew where"; - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - mnemonic, "brave123", false, - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); - } + AccountUtils GetAccountUtils() { return AccountUtils(keyring_service_); } - void GetAddress(std::string* valid_address) { - ASSERT_NE(valid_address, nullptr); - - base::RunLoop run_loop; - keyring_service_->GetKeyringInfo( - brave_wallet::mojom::kDefaultKeyringId, - base::BindLambdaForTesting( - [&](brave_wallet::mojom::KeyringInfoPtr keyring_info) { - *valid_address = ""; - if (keyring_info->account_infos.size() > 0) { - *valid_address = keyring_info->account_infos[0]->address; - } - run_loop.Quit(); - })); - run_loop.Run(); + void RestoreWallet() { + ASSERT_TRUE(keyring_service_->RestoreWalletSync( + kMnemonicDripCaution, kTestWalletPassword, false)); } - void SetSelectedAccount(const std::string& address) { - base::RunLoop run_loop; - keyring_service_->SetSelectedAccount( - MakeAccountId(mojom::CoinType::ETH, - brave_wallet::mojom::kDefaultKeyringId, - mojom::AccountKind::kDerived, address), - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + void SetSelectedAccount(const mojom::AccountIdPtr& account_id) { + ASSERT_TRUE(keyring_service_->SetSelectedAccountSync(account_id.Clone())); } private: @@ -212,23 +180,22 @@ IN_PROC_BROWSER_TEST_F(BraveWalletEventEmitterTest, IN_PROC_BROWSER_TEST_F(BraveWalletEventEmitterTest, CheckForAnAccountChangedEvent) { RestoreWallet(); + auto eth_account = GetAccountUtils().EnsureEthAccount(0); GURL url = https_server()->GetURL("a.com", "/brave_wallet_event_emitter.html"); ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents(); - std::string address; - GetAddress(&address); url::Origin sub_request_origin; ASSERT_TRUE(brave_wallet::GetSubRequestOrigin( permissions::RequestType::kBraveEthereum, GetLastCommitedOrigin(), - address, &sub_request_origin)); + eth_account->address, &sub_request_origin)); host_content_settings_map()->SetContentSettingDefaultScope( sub_request_origin.GetURL(), GetLastCommitedOrigin().GetURL(), ContentSettingsType::BRAVE_ETHEREUM, ContentSetting::CONTENT_SETTING_ALLOW); - SetSelectedAccount(address); + SetSelectedAccount(eth_account->account_id); auto result_first = EvalJs(contents, CheckForEventScript("received_account_changed_event")); diff --git a/browser/brave_wallet/brave_wallet_service_unittest.cc b/browser/brave_wallet/brave_wallet_service_unittest.cc index 85923018eb34..0c5f6457e026 100644 --- a/browser/brave_wallet/brave_wallet_service_unittest.cc +++ b/browser/brave_wallet/brave_wallet_service_unittest.cc @@ -687,27 +687,14 @@ class BraveWalletServiceUnitTest : public testing::Test { run_loop.Run(); } - void CheckAddresses(const std::vector& addresses, - bool* valid_addresses) { - ASSERT_NE(valid_addresses, nullptr); - - base::RunLoop run_loop; - keyring_service_->GetKeyringInfo( - brave_wallet::mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - *valid_addresses = false; - if (keyring_info->account_infos.size() == addresses.size()) { - for (size_t i = 0; i < addresses.size(); ++i) { - *valid_addresses = - (keyring_info->account_infos[i]->address == addresses[i]); - if (!*valid_addresses) { - break; - } - } - } - run_loop.Quit(); - })); - run_loop.Run(); + std::vector GetAddresses() { + std::vector result; + for (auto& account_info : keyring_service_->GetAllAccountInfos()) { + if (account_info->account_id->coin == mojom::CoinType::ETH) { + result.push_back(account_info->address); + } + } + return result; } void AddSuggestToken(mojom::BlockchainTokenPtr suggested_token, @@ -2334,9 +2321,7 @@ TEST_F(BraveWalletServiceUnitTest, OnGetImportInfo) { l10n_util::GetStringUTF8(IDS_BRAVE_WALLET_IMPORT_INTERNAL_ERROR)); error_message.clear(); - const char* valid_mnemonic = - "drip caution abandon festival order clown oven regular absorb evidence " - "crew where"; + const char* valid_mnemonic = kMnemonicDripCaution; SimulateOnGetImportInfo(new_password, true, ImportInfo({valid_mnemonic, false, 3}), ImportError::kNone, &success, &error_message); @@ -2350,13 +2335,11 @@ TEST_F(BraveWalletServiceUnitTest, OnGetImportInfo) { EXPECT_TRUE(is_valid_password); EXPECT_TRUE(is_valid_mnemonic); - bool is_valid_addresses = false; const std::vector expected_addresses( {"0x084DCb94038af1715963F149079cE011C4B22961", "0xE60A2209372AF1049C4848B1bF0136258c35f268", "0xb41c52De621B42A3a186ae1e608073A546195C9C"}); - CheckAddresses(expected_addresses, &is_valid_addresses); - EXPECT_TRUE(is_valid_addresses); + EXPECT_EQ(expected_addresses, GetAddresses()); } const char* valid_legacy_mnemonic = @@ -2378,14 +2361,12 @@ TEST_F(BraveWalletServiceUnitTest, OnGetImportInfo) { EXPECT_TRUE(is_valid_password); EXPECT_TRUE(is_valid_mnemonic); - bool is_valid_addresses = false; const std::vector expected_addresses( {"0xea3C17c81E3baC3472d163b2c8b12ddDAa027874", "0xEc1BB5a4EC94dE9107222c103907CCC720fA3854", "0x8cb80Ef1d274ED215A4C08B31b77e5A813eD8Ea1", "0x3899D70A5D45368807E38Ef2c1EB5E4f07542e4f"}); - CheckAddresses(expected_addresses, &is_valid_addresses); - EXPECT_TRUE(is_valid_addresses); + EXPECT_EQ(expected_addresses, GetAddresses()); } const char* invalid_mnemonic = "not correct seed word"; @@ -3015,11 +2996,8 @@ TEST_F(BraveWalletServiceUnitTest, EnsureSelectedAccountForChain) { const char* new_password = "brave1234!"; bool success; std::string error_message; - const char* valid_mnemonic = - "drip caution abandon festival order clown oven regular absorb evidence " - "crew where"; SimulateOnGetImportInfo(new_password, true, - ImportInfo({valid_mnemonic, false, 1}), + ImportInfo({kMnemonicDripCaution, false, 1}), ImportError::kNone, &success, &error_message); auto accounts = std::move(keyring_service_->GetAllAccountsSync()->accounts); diff --git a/browser/brave_wallet/brave_wallet_sign_message_browsertest.cc b/browser/brave_wallet/brave_wallet_sign_message_browsertest.cc index 32d988d1e59b..60cf22073282 100644 --- a/browser/brave_wallet/brave_wallet_sign_message_browsertest.cc +++ b/browser/brave_wallet/brave_wallet_sign_message_browsertest.cc @@ -16,6 +16,7 @@ #include "brave/components/brave_wallet/browser/brave_wallet_service.h" #include "brave/components/brave_wallet/browser/brave_wallet_utils.h" #include "brave/components/brave_wallet/browser/keyring_service.h" +#include "brave/components/brave_wallet/browser/test_utils.h" #include "brave/components/brave_wallet/common/features.h" #include "brave/components/brave_wallet/common/hex_utils.h" #include "brave/components/constants/brave_paths.h" @@ -107,17 +108,8 @@ class BraveWalletSignMessageBrowserTest : public InProcessBrowserTest { net::EmbeddedTestServer* https_server() { return &https_server_; } void RestoreWallet() { - const char mnemonic[] = - "drip caution abandon festival order clown oven regular absorb " - "evidence crew where"; - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - mnemonic, "brave123", false, - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + ASSERT_TRUE(keyring_service_->RestoreWalletSync( + kMnemonicDripCaution, kTestWalletPassword, false)); } void UserGrantPermission(bool granted) { if (granted) { diff --git a/browser/brave_wallet/eth_allowance_manager_unittest.cc b/browser/brave_wallet/eth_allowance_manager_unittest.cc index 6db591417fc2..ae02ee958988 100644 --- a/browser/brave_wallet/eth_allowance_manager_unittest.cc +++ b/browser/brave_wallet/eth_allowance_manager_unittest.cc @@ -296,23 +296,22 @@ class EthAllowanceManagerUnitTest : public testing::Test { contract_addresses.push_back(tkn->contract_address); } } - keyring_service_->RestoreWallet(kMnemonicDivideCruise, kPasswordBrave, - false, base::DoNothing()); + ASSERT_TRUE(keyring_service_->RestoreWalletSync(kMnemonicDivideCruise, + kPasswordBrave, false)); for (int i = 0; i < (eth_account_count - 1); i++) { AddEthAccount("additonal eth account"); } - const auto keyring_info = keyring_service_->GetKeyringInfoSync( - brave_wallet::mojom::kDefaultKeyringId); std::vector account_addresses; - for (const auto& account_info : keyring_info->account_infos) { - std::string hex_account_address; - if (!PadHexEncodedParameter(account_info->address, - &hex_account_address)) { - ASSERT_TRUE(false); + for (const auto& account_info : keyring_service_->GetAllAccountInfos()) { + if (account_info->account_id->coin == mojom::CoinType::ETH) { + std::string hex_account_address; + ASSERT_TRUE(PadHexEncodedParameter(account_info->address, + &hex_account_address)); + account_addresses.push_back(hex_account_address); } - account_addresses.push_back(hex_account_address); } + auto responses = std::move(get_responses).Run(account_addresses, token_list_map); blockchain_registry->UpdateTokenList(std::move(token_list_map)); diff --git a/browser/brave_wallet/ethereum_provider_browsertest.cc b/browser/brave_wallet/ethereum_provider_browsertest.cc index 90dad6693524..f851ea8d8eed 100644 --- a/browser/brave_wallet/ethereum_provider_browsertest.cc +++ b/browser/brave_wallet/ethereum_provider_browsertest.cc @@ -10,6 +10,7 @@ #include "brave/browser/brave_wallet/keyring_service_factory.h" #include "brave/components/brave_wallet/browser/brave_wallet_utils.h" #include "brave/components/brave_wallet/browser/keyring_service.h" +#include "brave/components/brave_wallet/browser/test_utils.h" #include "brave/components/constants/brave_paths.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" @@ -94,17 +95,8 @@ class EthereumProviderBrowserTest : public InProcessBrowserTest { net::EmbeddedTestServer* https_server() { return &https_server_; } void RestoreWallet() { - const char mnemonic[] = - "drip caution abandon festival order clown oven regular absorb " - "evidence crew where"; - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - mnemonic, "brave123", false, - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + ASSERT_TRUE(keyring_service_->RestoreWalletSync( + kMnemonicDripCaution, kTestWalletPassword, false)); } void ReloadAndWaitForLoadStop(Browser* browser) { diff --git a/browser/brave_wallet/ethereum_provider_impl_unittest.cc b/browser/brave_wallet/ethereum_provider_impl_unittest.cc index d476688d54a6..4592c65fb7e7 100644 --- a/browser/brave_wallet/ethereum_provider_impl_unittest.cc +++ b/browser/brave_wallet/ethereum_provider_impl_unittest.cc @@ -288,14 +288,8 @@ class EthereumProviderImplUnitTest : public testing::Test { void RestoreWallet(const std::string& mnemonic, const std::string& password, bool is_legacy_brave_wallet) { - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - mnemonic, password, is_legacy_brave_wallet, - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + ASSERT_TRUE(keyring_service_->RestoreWalletSync(mnemonic, password, + is_legacy_brave_wallet)); } mojom::AccountInfoPtr AddHardwareAccount(const std::string& address) { @@ -735,16 +729,11 @@ class EthereumProviderImplUnitTest : public testing::Test { std::vector GetAddresses() { std::vector result; - base::RunLoop run_loop; - keyring_service_->GetKeyringInfo( - brave_wallet::mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - for (auto& account_info : keyring_info->account_infos) { - result.push_back(account_info->address); - } - run_loop.Quit(); - })); - run_loop.Run(); + for (const auto& account_info : keyring_service_->GetAllAccountInfos()) { + if (account_info->account_id->coin == mojom::CoinType::ETH) { + result.push_back(account_info->address); + } + } return result; } @@ -2725,7 +2714,7 @@ TEST_F(EthereumProviderImplUnitTest, AddSuggestToken) { } TEST_F(EthereumProviderImplUnitTest, GetEncryptionPublicKey) { - RestoreWallet(kMnemonicDivideCruise, "brave", false); + RestoreWallet(kMnemonicDivideCruise, kTestWalletPassword, false); auto account_0 = GetAccountUtils().EnsureEthAccount(0); CreateBraveWalletTabHelper(); @@ -2777,7 +2766,7 @@ TEST_F(EthereumProviderImplUnitTest, GetEncryptionPublicKey) { } TEST_F(EthereumProviderImplUnitTest, Decrypt) { - RestoreWallet(kMnemonicDivideCruise, "brave", false); + RestoreWallet(kMnemonicDivideCruise, kTestWalletPassword, false); auto account_0 = GetAccountUtils().EnsureEthAccount(0); auto address_0 = account_0->address; diff --git a/browser/brave_wallet/external_wallets_importer_unittest.cc b/browser/brave_wallet/external_wallets_importer_unittest.cc index 253106f08992..95dabb72a81e 100644 --- a/browser/brave_wallet/external_wallets_importer_unittest.cc +++ b/browser/brave_wallet/external_wallets_importer_unittest.cc @@ -9,16 +9,13 @@ #include "base/json/json_reader.h" #include "base/test/bind.h" -#include "base/test/task_environment.h" +#include "brave/components/brave_wallet/browser/test_utils.h" #include "chrome/test/base/testing_profile.h" #include "content/public/test/browser_task_environment.h" #include "testing/gtest/include/gtest/gtest.h" namespace brave_wallet { namespace { -const char* valid_mnemonic = - "drip caution abandon festival order clown oven regular absorb evidence " - "crew where"; // encrypted valid_mnemonic const char* valid_data = "{\"data\": {\"KeyringController\": {\"vault\": " @@ -218,7 +215,7 @@ TEST_F(ExternalWalletsImporterUnitTest, OnGetImportInfo) { SimulateGetImportInfo("brave4ever", valid_data, &result, &info, &error); EXPECT_TRUE(result); EXPECT_EQ(error, ImportError::kNone); - EXPECT_EQ(info.mnemonic, valid_mnemonic); + EXPECT_EQ(info.mnemonic, kMnemonicDripCaution); EXPECT_FALSE(info.is_legacy_crypto_wallets); EXPECT_EQ(info.number_of_accounts, 1u); } @@ -231,7 +228,7 @@ TEST_F(ExternalWalletsImporterUnitTest, OnGetImportInfo_UTF8Mnemonic) { &info, &error); EXPECT_TRUE(result); EXPECT_EQ(error, ImportError::kNone); - EXPECT_EQ(info.mnemonic, valid_mnemonic); + EXPECT_EQ(info.mnemonic, kMnemonicDripCaution); EXPECT_FALSE(info.is_legacy_crypto_wallets); EXPECT_EQ(info.number_of_accounts, 1u); } diff --git a/browser/brave_wallet/keyring_service_unittest.cc b/browser/brave_wallet/keyring_service_unittest.cc index 8298576fdf01..80e773dd89b0 100644 --- a/browser/brave_wallet/keyring_service_unittest.cc +++ b/browser/brave_wallet/keyring_service_unittest.cc @@ -107,7 +107,7 @@ class TestKeyringServiceObserver : public mojom::KeyringServiceObserver { MOCK_METHOD(void, AutoLockMinutesChanged, (), (override)); MOCK_METHOD(void, KeyringCreated, (mojom::KeyringId keyring_id), (override)); - MOCK_METHOD(void, KeyringRestored, (mojom::KeyringId keyring_id), (override)); + MOCK_METHOD(void, WalletRestored, (), (override)); MOCK_METHOD(void, KeyringReset, (), (override)); MOCK_METHOD(void, Locked, (), (override)); MOCK_METHOD(void, Unlocked, (), (override)); @@ -185,6 +185,10 @@ class KeyringServiceUnitTest : public testing::Test { return url_loader_factory_; } + scoped_refptr shared_url_loader_factory() { + return shared_url_loader_factory_; + } + bool HasPrefForKeyring(const std::string& key, mojom::KeyringId keyring_id) { return KeyringService::HasPrefForKeyring(*GetPrefs(), key, keyring_id); } @@ -209,8 +213,7 @@ class KeyringServiceUnitTest : public testing::Test { base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { ASSERT_EQ(keyring_info->id, keyring_id); if (!keyring_info->is_keyring_created && keyring_info->is_locked && - !keyring_info->is_backed_up && - keyring_info->account_infos.empty()) { + !keyring_info->is_backed_up) { result = true; } run_loop.Quit(); @@ -371,15 +374,8 @@ class KeyringServiceUnitTest : public testing::Test { const std::string& mnemonic, const std::string& password, bool is_legacy_brave_wallet) { - bool success = false; - base::RunLoop run_loop; - service->RestoreWallet(mnemonic, password, is_legacy_brave_wallet, - base::BindLambdaForTesting([&](bool v) { - success = v; - run_loop.Quit(); - })); - run_loop.Run(); - return success; + return service->RestoreWalletSync(mnemonic, password, + is_legacy_brave_wallet); } static mojom::AccountInfoPtr AddAccount(KeyringService* service, @@ -1094,17 +1090,6 @@ TEST_F(KeyringServiceUnitTest, GetKeyringInfo) { EXPECT_TRUE(keyring_info->is_keyring_created); EXPECT_FALSE(keyring_info->is_locked); EXPECT_FALSE(keyring_info->is_backed_up); - EXPECT_EQ(keyring_info->account_infos.size(), 1u); - EXPECT_FALSE(keyring_info->account_infos[0]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Account 1"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->coin, - mojom::CoinType::ETH); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->keyring_id, - mojom::kDefaultKeyringId); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kDerived); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->address, - keyring_info->account_infos[0]->address); callback_called = true; })); base::RunLoop().RunUntilIdle(); @@ -1122,11 +1107,6 @@ TEST_F(KeyringServiceUnitTest, GetKeyringInfo) { EXPECT_TRUE(keyring_info->is_keyring_created); EXPECT_FALSE(keyring_info->is_locked); EXPECT_TRUE(keyring_info->is_backed_up); - EXPECT_EQ(keyring_info->account_infos.size(), 2u); - EXPECT_FALSE(keyring_info->account_infos[0]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Account 1"); - EXPECT_FALSE(keyring_info->account_infos[1]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[1]->name, "Account5566"); callback_called = true; })); base::RunLoop().RunUntilIdle(); @@ -1469,7 +1449,7 @@ TEST_F(KeyringServiceUnitTest, CreateAndRestoreWallet) { NiceMock observer(service); - EXPECT_CALL(observer, KeyringRestored(_)).Times(0); + EXPECT_CALL(observer, WalletRestored()).Times(0); EXPECT_CALL(observer, KeyringCreated(mojom::kDefaultKeyringId)); EXPECT_CALL(observer, KeyringCreated(mojom::kFilecoinKeyringId)).Times(0); EXPECT_CALL(observer, KeyringCreated(mojom::kFilecoinTestnetKeyringId)) @@ -1529,20 +1509,14 @@ TEST_F(KeyringServiceUnitTest, CreateAndRestoreWallet) { }); observer.WaitAndVerify(); - EXPECT_CALL(observer, KeyringRestored(mojom::kDefaultKeyringId)); - EXPECT_CALL(observer, KeyringRestored(mojom::kFilecoinKeyringId)) - .Times(IsFilecoinEnabled()); - EXPECT_CALL(observer, KeyringRestored(mojom::kFilecoinTestnetKeyringId)) - .Times(IsFilecoinEnabled()); - EXPECT_CALL(observer, KeyringRestored(mojom::kSolanaKeyringId)) - .Times(IsSolanaEnabled()); + EXPECT_CALL(observer, WalletRestored()); EXPECT_CALL(observer, KeyringCreated(_)).Times(0); verify_restore_wallet.Run(); observer.WaitAndVerify(); // Restore twice consecutively should succeed and have only one account EXPECT_CALL(observer, KeyringCreated(_)).Times(0); - EXPECT_CALL(observer, KeyringRestored(_)).Times(0); + EXPECT_CALL(observer, WalletRestored()); verify_restore_wallet.Run(); observer.WaitAndVerify(); } @@ -1709,6 +1683,19 @@ TEST_F(KeyringServiceUnitTest, ImportedAccounts) { mojom::AccountKind::kImported, "0xxxxxxxxxx0"), kPasswordBrave)); + auto account_infos = GetAccountUtils(&service).AllEthAccounts(); + EXPECT_EQ(account_infos.size(), 3u); + EXPECT_FALSE(account_infos[0]->address.empty()); + EXPECT_EQ(account_infos[0]->name, "Account 1"); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kDerived); + // imported accounts + EXPECT_EQ(account_infos[1]->address, imported_accounts[0].address); + EXPECT_EQ(account_infos[1]->name, imported_accounts[0].name); + EXPECT_EQ(account_infos[1]->account_id->kind, mojom::AccountKind::kImported); + EXPECT_EQ(account_infos[2]->address, imported_accounts[2].address); + EXPECT_EQ(account_infos[2]->name, imported_accounts[2].name); + EXPECT_EQ(account_infos[2]->account_id->kind, mojom::AccountKind::kImported); + bool callback_called = false; service.GetKeyringInfo( mojom::kDefaultKeyringId, @@ -1717,25 +1704,7 @@ TEST_F(KeyringServiceUnitTest, ImportedAccounts) { EXPECT_TRUE(keyring_info->is_keyring_created); EXPECT_FALSE(keyring_info->is_locked); EXPECT_FALSE(keyring_info->is_backed_up); - EXPECT_EQ(keyring_info->account_infos.size(), 3u); - EXPECT_FALSE(keyring_info->account_infos[0]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Account 1"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kDerived); - // import accounts - EXPECT_EQ(keyring_info->account_infos[1]->address, - imported_accounts[0].address); - EXPECT_EQ(keyring_info->account_infos[1]->name, - imported_accounts[0].name); - EXPECT_EQ(keyring_info->account_infos[1]->account_id->kind, - mojom::AccountKind::kImported); - - EXPECT_EQ(keyring_info->account_infos[2]->address, - imported_accounts[2].address); - EXPECT_EQ(keyring_info->account_infos[2]->name, - imported_accounts[2].name); - EXPECT_EQ(keyring_info->account_infos[2]->account_id->kind, - mojom::AccountKind::kImported); + callback_called = true; })); base::RunLoop().RunUntilIdle(); @@ -1751,28 +1720,15 @@ TEST_F(KeyringServiceUnitTest, ImportedAccounts) { EXPECT_TRUE(Unlock(&service, "brave")); - callback_called = false; + account_infos = GetAccountUtils(&service).AllEthAccounts(); // Imported accounts should be restored - service.GetKeyringInfo( - mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->account_infos.size(), 3u); - EXPECT_EQ(keyring_info->account_infos[1]->address, - imported_accounts[0].address); - EXPECT_EQ(keyring_info->account_infos[1]->name, - imported_accounts[0].name); - EXPECT_EQ(keyring_info->account_infos[1]->account_id->kind, - mojom::AccountKind::kImported); - EXPECT_EQ(keyring_info->account_infos[2]->address, - imported_accounts[2].address); - EXPECT_EQ(keyring_info->account_infos[2]->name, - imported_accounts[2].name); - EXPECT_EQ(keyring_info->account_infos[2]->account_id->kind, - mojom::AccountKind::kImported); - callback_called = true; - })); - base::RunLoop().RunUntilIdle(); - EXPECT_TRUE(callback_called); + EXPECT_EQ(account_infos.size(), 3u); + EXPECT_EQ(account_infos[1]->address, imported_accounts[0].address); + EXPECT_EQ(account_infos[1]->name, imported_accounts[0].name); + EXPECT_EQ(account_infos[1]->account_id->kind, mojom::AccountKind::kImported); + EXPECT_EQ(account_infos[2]->address, imported_accounts[2].address); + EXPECT_EQ(account_infos[2]->name, imported_accounts[2].name); + EXPECT_EQ(account_infos[2]->account_id->kind, mojom::AccountKind::kImported); // Unlocked but with wrong password won't get private key. EXPECT_FALSE(EncodePrivateKeyForExport( @@ -2103,40 +2059,22 @@ TEST_F(KeyringServiceUnitTest, SetDefaultKeyringImportedAccountName) { EXPECT_EQ(imported_account.private_key, *private_key); } + auto account_infos = GetAccountUtils(&service).AllEthAccounts(); + // Only second imported account's name is updated. - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->id, mojom::kDefaultKeyringId); - EXPECT_TRUE(keyring_info->is_keyring_created); - EXPECT_FALSE(keyring_info->is_locked); - EXPECT_FALSE(keyring_info->is_backed_up); - EXPECT_EQ(keyring_info->account_infos.size(), 4u); - EXPECT_FALSE(keyring_info->account_infos[0]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Account 1"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kDerived); - EXPECT_EQ(keyring_info->account_infos[1]->address, - imported_accounts[0].address); - EXPECT_EQ(keyring_info->account_infos[1]->name, - imported_accounts[0].name); - EXPECT_EQ(keyring_info->account_infos[1]->account_id->kind, - mojom::AccountKind::kImported); - EXPECT_EQ(keyring_info->account_infos[2]->address, - imported_accounts[1].address); - EXPECT_EQ(keyring_info->account_infos[2]->name, kUpdatedName); - EXPECT_EQ(keyring_info->account_infos[2]->account_id->kind, - mojom::AccountKind::kImported); - EXPECT_EQ(keyring_info->account_infos[3]->address, - imported_accounts[2].address); - EXPECT_EQ(keyring_info->account_infos[3]->name, - imported_accounts[2].name); - EXPECT_EQ(keyring_info->account_infos[3]->account_id->kind, - mojom::AccountKind::kImported); - run_loop.Quit(); - })); - run_loop.Run(); + EXPECT_EQ(account_infos.size(), 4u); + EXPECT_FALSE(account_infos[0]->address.empty()); + EXPECT_EQ(account_infos[0]->name, "Account 1"); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kDerived); + EXPECT_EQ(account_infos[1]->address, imported_accounts[0].address); + EXPECT_EQ(account_infos[1]->name, imported_accounts[0].name); + EXPECT_EQ(account_infos[1]->account_id->kind, mojom::AccountKind::kImported); + EXPECT_EQ(account_infos[2]->address, imported_accounts[1].address); + EXPECT_EQ(account_infos[2]->name, kUpdatedName); + EXPECT_EQ(account_infos[2]->account_id->kind, mojom::AccountKind::kImported); + EXPECT_EQ(account_infos[3]->address, imported_accounts[2].address); + EXPECT_EQ(account_infos[3]->name, imported_accounts[2].name); + EXPECT_EQ(account_infos[3]->account_id->kind, mojom::AccountKind::kImported); } TEST_F(KeyringServiceUnitTest, RestoreLegacyBraveWallet) { @@ -2144,10 +2082,11 @@ TEST_F(KeyringServiceUnitTest, RestoreLegacyBraveWallet) { "cushion pitch impact album daring marine much annual budget social " "clarify balance rose almost area busy among bring hidden bind later " "capable pulp laundry"; - const char* mnemonic12 = - "drip caution abandon festival order clown oven regular absorb evidence " - "crew where"; + const char* mnemonic12 = kMnemonicDripCaution; KeyringService service(json_rpc_service(), GetPrefs(), GetLocalState()); + BraveWalletService brave_wallet_service(shared_url_loader_factory(), nullptr, + &service, json_rpc_service(), nullptr, + GetPrefs(), GetLocalState()); auto verify_restore_wallet = base::BindLambdaForTesting( [&service](const char* mnemonic, const char* address, bool is_legacy, bool expect_result) { @@ -2260,90 +2199,67 @@ TEST_F(KeyringServiceUnitTest, HardwareAccounts) { } { // Checking Default keyring accounts - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - const auto& accounts = keyring_info->account_infos; - EXPECT_EQ(accounts.size(), 3u); - - EXPECT_EQ(accounts[0]->address, "0x111"); - EXPECT_EQ(accounts[0]->name, "name 1"); - EXPECT_EQ(accounts[0]->account_id->kind, - mojom::AccountKind::kHardware); - ASSERT_TRUE(accounts[0]->hardware); - EXPECT_EQ(accounts[0]->hardware->device_id, "device1"); - EXPECT_EQ(accounts[0]->account_id->coin, mojom::CoinType::ETH); - - EXPECT_EQ(accounts[1]->address, "0x222"); - EXPECT_EQ(accounts[1]->name, "name 4"); - EXPECT_EQ(accounts[1]->account_id->kind, - mojom::AccountKind::kHardware); - ASSERT_TRUE(accounts[1]->hardware); - EXPECT_EQ(accounts[1]->hardware->device_id, "device1"); - EXPECT_EQ(accounts[1]->account_id->coin, mojom::CoinType::ETH); - - EXPECT_EQ(accounts[2]->address, "0xEA0"); - EXPECT_EQ(accounts[2]->name, "name 3"); - EXPECT_EQ(accounts[2]->account_id->kind, - mojom::AccountKind::kHardware); - ASSERT_TRUE(accounts[2]->hardware); - EXPECT_EQ(accounts[2]->hardware->device_id, "device2"); - EXPECT_EQ(accounts[2]->account_id->coin, mojom::CoinType::ETH); - - run_loop.Quit(); - })); - run_loop.Run(); + auto account_infos = GetAccountUtils(&service).AllEthAccounts(); + + EXPECT_EQ(account_infos.size(), 3u); + + EXPECT_EQ(account_infos[0]->address, "0x111"); + EXPECT_EQ(account_infos[0]->name, "name 1"); + EXPECT_EQ(account_infos[0]->account_id->kind, + mojom::AccountKind::kHardware); + ASSERT_TRUE(account_infos[0]->hardware); + EXPECT_EQ(account_infos[0]->hardware->device_id, "device1"); + EXPECT_EQ(account_infos[0]->account_id->coin, mojom::CoinType::ETH); + + EXPECT_EQ(account_infos[1]->address, "0x222"); + EXPECT_EQ(account_infos[1]->name, "name 4"); + EXPECT_EQ(account_infos[1]->account_id->kind, + mojom::AccountKind::kHardware); + ASSERT_TRUE(account_infos[1]->hardware); + EXPECT_EQ(account_infos[1]->hardware->device_id, "device1"); + EXPECT_EQ(account_infos[1]->account_id->coin, mojom::CoinType::ETH); + + EXPECT_EQ(account_infos[2]->address, "0xEA0"); + EXPECT_EQ(account_infos[2]->name, "name 3"); + EXPECT_EQ(account_infos[2]->account_id->kind, + mojom::AccountKind::kHardware); + ASSERT_TRUE(account_infos[2]->hardware); + EXPECT_EQ(account_infos[2]->hardware->device_id, "device2"); + EXPECT_EQ(account_infos[2]->account_id->coin, mojom::CoinType::ETH); } { // Checking Filecoin keyring accounts - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kFilecoinKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - const auto& accounts = keyring_info->account_infos; - EXPECT_EQ(accounts.size(), 2u); - - EXPECT_EQ(accounts[0]->address, "0x264"); - EXPECT_EQ(accounts[0]->name, "name 2"); - EXPECT_EQ(accounts[0]->account_id->kind, - mojom::AccountKind::kHardware); - ASSERT_TRUE(accounts[0]->hardware); - EXPECT_EQ(accounts[0]->hardware->device_id, "device1"); - EXPECT_EQ(accounts[0]->account_id->coin, mojom::CoinType::FIL); - - EXPECT_EQ(accounts[1]->address, "0xFIL"); - EXPECT_EQ(accounts[1]->name, "filecoin 1"); - EXPECT_EQ(accounts[1]->account_id->kind, - mojom::AccountKind::kHardware); - ASSERT_TRUE(accounts[1]->hardware); - EXPECT_EQ(accounts[1]->hardware->device_id, "device2"); - EXPECT_EQ(accounts[1]->account_id->coin, mojom::CoinType::FIL); - - run_loop.Quit(); - })); - run_loop.Run(); + auto account_infos = GetAccountUtils(&service).AllFilAccounts(); + EXPECT_EQ(account_infos.size(), 2u); + + EXPECT_EQ(account_infos[0]->address, "0x264"); + EXPECT_EQ(account_infos[0]->name, "name 2"); + EXPECT_EQ(account_infos[0]->account_id->kind, + mojom::AccountKind::kHardware); + ASSERT_TRUE(account_infos[0]->hardware); + EXPECT_EQ(account_infos[0]->hardware->device_id, "device1"); + EXPECT_EQ(account_infos[0]->account_id->coin, mojom::CoinType::FIL); + + EXPECT_EQ(account_infos[1]->address, "0xFIL"); + EXPECT_EQ(account_infos[1]->name, "filecoin 1"); + EXPECT_EQ(account_infos[1]->account_id->kind, + mojom::AccountKind::kHardware); + ASSERT_TRUE(account_infos[1]->hardware); + EXPECT_EQ(account_infos[1]->hardware->device_id, "device2"); + EXPECT_EQ(account_infos[1]->account_id->coin, mojom::CoinType::FIL); } { // Checking Filecoin keyring testnet accounts - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kFilecoinTestnetKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - const auto& accounts = keyring_info->account_infos; - EXPECT_EQ(accounts.size(), 1u); - - EXPECT_EQ(accounts[0]->address, "0xFILTEST"); - EXPECT_EQ(accounts[0]->name, "filecoin testnet 1"); - EXPECT_EQ(accounts[0]->account_id->kind, - mojom::AccountKind::kHardware); - ASSERT_TRUE(accounts[0]->hardware); - EXPECT_EQ(accounts[0]->hardware->device_id, "device2"); - EXPECT_EQ(accounts[0]->account_id->coin, mojom::CoinType::FIL); - - run_loop.Quit(); - })); - run_loop.Run(); + auto account_infos = GetAccountUtils(&service).AllFilTestAccounts(); + EXPECT_EQ(account_infos.size(), 1u); + + EXPECT_EQ(account_infos[0]->address, "0xFILTEST"); + EXPECT_EQ(account_infos[0]->name, "filecoin testnet 1"); + EXPECT_EQ(account_infos[0]->account_id->kind, + mojom::AccountKind::kHardware); + ASSERT_TRUE(account_infos[0]->hardware); + EXPECT_EQ(account_infos[0]->hardware->device_id, "device2"); + EXPECT_EQ(account_infos[0]->account_id->coin, mojom::CoinType::FIL); } EXPECT_CALL(observer, AccountsChanged()).Times(0); @@ -2396,24 +2312,15 @@ TEST_F(KeyringServiceUnitTest, HardwareAccounts) { "")); observer.WaitAndVerify(); - bool callback_called = false; - service.GetKeyringInfo( - mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - const auto& accounts = keyring_info->account_infos; - EXPECT_EQ(accounts.size(), size_t(1)); + auto account_infos = GetAccountUtils(&service).AllEthAccounts(); + EXPECT_EQ(account_infos.size(), size_t(1)); - EXPECT_EQ(accounts[0]->address, "0x222"); - EXPECT_EQ(accounts[0]->name, "name 4"); - EXPECT_EQ(accounts[0]->account_id->kind, mojom::AccountKind::kHardware); - ASSERT_TRUE(accounts[0]->hardware); - EXPECT_EQ(accounts[0]->hardware->device_id, "device1"); - EXPECT_EQ(accounts[0]->account_id->coin, mojom::CoinType::ETH); - - callback_called = true; - })); - base::RunLoop().RunUntilIdle(); - EXPECT_TRUE(callback_called); + EXPECT_EQ(account_infos[0]->address, "0x222"); + EXPECT_EQ(account_infos[0]->name, "name 4"); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kHardware); + ASSERT_TRUE(account_infos[0]->hardware); + EXPECT_EQ(account_infos[0]->hardware->device_id, "device1"); + EXPECT_EQ(account_infos[0]->account_id->coin, mojom::CoinType::ETH); // TODO(apaymshev): Fix this test so it tests real scenarios when we always have // fallback account when removing last HW account. @@ -2580,30 +2487,24 @@ TEST_F(KeyringServiceUnitTest, SelectAddedAccount) { "eth acc 1"); AddAccount(&service, mojom::CoinType::ETH, mojom::kDefaultKeyringId, "eth acc 2"); - AddAccount(&service, mojom::CoinType::ETH, mojom::kDefaultKeyringId, - "eth acc 3"); + auto last_eth = AddAccount(&service, mojom::CoinType::ETH, + mojom::kDefaultKeyringId, "eth acc 3"); AddAccount(&service, mojom::CoinType::SOL, mojom::kSolanaKeyringId, "sol acc 1"); AddAccount(&service, mojom::CoinType::SOL, mojom::kSolanaKeyringId, "sol acc 2"); - AddAccount(&service, mojom::CoinType::SOL, mojom::kSolanaKeyringId, - "sol acc 3"); + auto last_sol = AddAccount(&service, mojom::CoinType::SOL, + mojom::kSolanaKeyringId, "sol acc 3"); // Last added eth account becomes selected for eth dapp. - ASSERT_EQ( - service.GetSelectedEthereumDappAccount(), - service.GetKeyringInfoSync(mojom::kDefaultKeyringId)->account_infos[3]); + ASSERT_EQ(service.GetSelectedEthereumDappAccount(), last_eth); // Last added sol account becomes selected for sol dapp. - ASSERT_EQ( - service.GetSelectedSolanaDappAccount(), - service.GetKeyringInfoSync(mojom::kSolanaKeyringId)->account_infos[2]); + ASSERT_EQ(service.GetSelectedSolanaDappAccount(), last_sol); // Last added account becomes selected. - ASSERT_EQ( - service.GetSelectedWalletAccount(), - service.GetKeyringInfoSync(mojom::kSolanaKeyringId)->account_infos[2]); + ASSERT_EQ(service.GetSelectedWalletAccount(), last_sol); } TEST_F(KeyringServiceUnitTest, SelectAddedFilecoinAccount) { @@ -2621,12 +2522,10 @@ TEST_F(KeyringServiceUnitTest, SelectAddedFilecoinAccount) { "fil acc 1"); AddAccount(&service, mojom::CoinType::FIL, mojom::kFilecoinTestnetKeyringId, "fil acc 2"); - AddAccount(&service, mojom::CoinType::FIL, mojom::kFilecoinTestnetKeyringId, - "fil acc 3"); + auto last_fil = AddAccount(&service, mojom::CoinType::FIL, + mojom::kFilecoinTestnetKeyringId, "fil acc 3"); - ASSERT_EQ(service.GetSelectedWalletAccount(), - service.GetKeyringInfoSync(mojom::kFilecoinTestnetKeyringId) - ->account_infos[2]); + ASSERT_EQ(service.GetSelectedWalletAccount(), last_fil); } TEST_F(KeyringServiceUnitTest, SelectImportedFilecoinAccount) { @@ -2952,22 +2851,14 @@ TEST_F(KeyringServiceUnitTest, AddAccountsWithDefaultName) { service.AddAccountsWithDefaultName(mojom::CoinType::ETH, mojom::kDefaultKeyringId, 3); - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->id, mojom::kDefaultKeyringId); - EXPECT_TRUE(keyring_info->is_keyring_created); - EXPECT_EQ(keyring_info->account_infos.size(), 5u); - EXPECT_FALSE(keyring_info->account_infos[0]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Account 1"); - EXPECT_EQ(keyring_info->account_infos[1]->name, "AccountAAAAH"); - EXPECT_EQ(keyring_info->account_infos[2]->name, "Account 3"); - EXPECT_EQ(keyring_info->account_infos[3]->name, "Account 4"); - EXPECT_EQ(keyring_info->account_infos[4]->name, "Account 5"); - run_loop.Quit(); - })); - run_loop.Run(); + auto account_infos = GetAccountUtils(&service).AllEthAccounts(); + EXPECT_EQ(account_infos.size(), 5u); + EXPECT_FALSE(account_infos[0]->address.empty()); + EXPECT_EQ(account_infos[0]->name, "Account 1"); + EXPECT_EQ(account_infos[1]->name, "AccountAAAAH"); + EXPECT_EQ(account_infos[2]->name, "Account 3"); + EXPECT_EQ(account_infos[3]->name, "Account 4"); + EXPECT_EQ(account_infos[4]->name, "Account 5"); } TEST_F(KeyringServiceUnitTest, SignMessageByDefaultKeyring) { @@ -3089,42 +2980,17 @@ TEST_F(KeyringServiceUnitTest, SetAccountName_HardwareAccounts) { mojom::AccountKind::kHardware, "0xFILTEST"), "filecoin testnet 1 changed"); - { - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_FALSE(keyring_info->account_infos[1]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[1]->name, "name 1 changed"); - run_loop.Quit(); - })); - run_loop.Run(); - } + auto account_infos = GetAccountUtils(&service).AllEthAccounts(); + EXPECT_FALSE(account_infos[1]->address.empty()); + EXPECT_EQ(account_infos[1]->name, "name 1 changed"); - { - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kFilecoinKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_FALSE(keyring_info->account_infos[1]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[1]->name, "filecoin 1 changed"); - run_loop.Quit(); - })); - run_loop.Run(); - } + account_infos = GetAccountUtils(&service).AllFilAccounts(); + EXPECT_FALSE(account_infos[1]->address.empty()); + EXPECT_EQ(account_infos[1]->name, "filecoin 1 changed"); - { - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kFilecoinTestnetKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_FALSE(keyring_info->account_infos[0]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[0]->name, - "filecoin testnet 1 changed"); - run_loop.Quit(); - })); - run_loop.Run(); - } + account_infos = GetAccountUtils(&service).AllFilTestAccounts(); + EXPECT_FALSE(account_infos[0]->address.empty()); + EXPECT_EQ(account_infos[0]->name, "filecoin testnet 1 changed"); } TEST_F(KeyringServiceUnitTest, SetDefaultKeyringHardwareAccountName) { @@ -3195,35 +3061,21 @@ TEST_F(KeyringServiceUnitTest, SetDefaultKeyringHardwareAccountName) { observer.WaitAndVerify(); // Only second hardware account's name is updated. - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kDefaultKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->id, mojom::kDefaultKeyringId); - EXPECT_TRUE(keyring_info->is_keyring_created); - EXPECT_FALSE(keyring_info->is_locked); - EXPECT_FALSE(keyring_info->is_backed_up); - EXPECT_EQ(keyring_info->account_infos.size(), 4u); - EXPECT_FALSE(keyring_info->account_infos[0]->address.empty()); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Account 1"); - EXPECT_FALSE(keyring_info->account_infos[0]->hardware); - EXPECT_EQ(keyring_info->account_infos[1]->address, - hardware_accounts[0].address); - EXPECT_EQ(keyring_info->account_infos[1]->name, - hardware_accounts[0].name); - EXPECT_TRUE(keyring_info->account_infos[1]->hardware); - EXPECT_EQ(keyring_info->account_infos[2]->address, - hardware_accounts[1].address); - EXPECT_EQ(keyring_info->account_infos[2]->name, kUpdatedName); - EXPECT_TRUE(keyring_info->account_infos[2]->hardware); - EXPECT_EQ(keyring_info->account_infos[3]->address, - hardware_accounts[2].address); - EXPECT_EQ(keyring_info->account_infos[3]->name, - hardware_accounts[2].name); - EXPECT_TRUE(keyring_info->account_infos[3]->hardware); - run_loop.Quit(); - })); - run_loop.Run(); + auto account_infos = GetAccountUtils(&service).AllEthAccounts(); + + EXPECT_EQ(account_infos.size(), 4u); + EXPECT_FALSE(account_infos[0]->address.empty()); + EXPECT_EQ(account_infos[0]->name, "Account 1"); + EXPECT_FALSE(account_infos[0]->hardware); + EXPECT_EQ(account_infos[1]->address, hardware_accounts[0].address); + EXPECT_EQ(account_infos[1]->name, hardware_accounts[0].name); + EXPECT_TRUE(account_infos[1]->hardware); + EXPECT_EQ(account_infos[2]->address, hardware_accounts[1].address); + EXPECT_EQ(account_infos[2]->name, kUpdatedName); + EXPECT_TRUE(account_infos[2]->hardware); + EXPECT_EQ(account_infos[3]->address, hardware_accounts[2].address); + EXPECT_EQ(account_infos[3]->name, hardware_accounts[2].name); + EXPECT_TRUE(account_infos[3]->hardware); } TEST_F(KeyringServiceUnitTest, IsStrongPassword) { @@ -3355,7 +3207,7 @@ TEST_F(KeyringServiceUnitTest, AddFilecoinAccounts) { // Add FIL account will lazily create corresponding keyring EXPECT_CALL(observer, KeyringCreated(mojom::kFilecoinKeyringId)); - EXPECT_CALL(observer, KeyringRestored(mojom::kFilecoinKeyringId)).Times(0); + EXPECT_CALL(observer, WalletRestored()).Times(0); ASSERT_TRUE(AddAccount(&service, mojom::CoinType::FIL, mojom::kFilecoinKeyringId, "FIL account1")); EXPECT_TRUE(service.IsKeyringCreated(mojom::kFilecoinKeyringId)); @@ -3364,8 +3216,7 @@ TEST_F(KeyringServiceUnitTest, AddFilecoinAccounts) { // Add FIL with testnet network EXPECT_FALSE(service.IsKeyringCreated(mojom::kFilecoinTestnetKeyringId)); EXPECT_CALL(observer, KeyringCreated(mojom::kFilecoinTestnetKeyringId)); - EXPECT_CALL(observer, KeyringRestored(mojom::kFilecoinTestnetKeyringId)) - .Times(0); + EXPECT_CALL(observer, WalletRestored()).Times(0); ASSERT_TRUE(AddAccount(&service, mojom::CoinType::FIL, mojom::kFilecoinTestnetKeyringId, "FIL testnet account 1")); @@ -3384,30 +3235,26 @@ TEST_F(KeyringServiceUnitTest, AddFilecoinAccounts) { // FIL keyring already exists EXPECT_CALL(observer, KeyringCreated(mojom::kFilecoinKeyringId)).Times(0); - EXPECT_CALL(observer, KeyringRestored(mojom::kFilecoinKeyringId)).Times(0); + EXPECT_CALL(observer, WalletRestored()).Times(0); auto last_added_account = AddAccount(&service, mojom::CoinType::FIL, mojom::kFilecoinKeyringId, "FIL account2"); ASSERT_TRUE(last_added_account); observer.WaitAndVerify(); - auto keyring_info = service.GetKeyringInfoSync(mojom::kFilecoinKeyringId); - EXPECT_EQ(keyring_info->account_infos.size(), 2u); - EXPECT_EQ(keyring_info->account_infos[0]->name, "FIL account1"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kDerived); - EXPECT_EQ(keyring_info->account_infos[1]->name, "FIL account2"); - EXPECT_EQ(keyring_info->account_infos[1]->account_id->kind, - mojom::AccountKind::kDerived); - - keyring_info = service.GetKeyringInfoSync(mojom::kFilecoinTestnetKeyringId); - EXPECT_EQ(keyring_info->account_infos.size(), 2u); - EXPECT_EQ(keyring_info->account_infos[0]->name, "FIL testnet account 1"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kDerived); - EXPECT_EQ(keyring_info->account_infos[1]->name, "FIL testnet account 2"); - EXPECT_EQ(keyring_info->account_infos[1]->account_id->kind, - mojom::AccountKind::kDerived); + auto account_infos = GetAccountUtils(&service).AllFilAccounts(); + EXPECT_EQ(account_infos.size(), 2u); + EXPECT_EQ(account_infos[0]->name, "FIL account1"); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kDerived); + EXPECT_EQ(account_infos[1]->name, "FIL account2"); + EXPECT_EQ(account_infos[1]->account_id->kind, mojom::AccountKind::kDerived); + + account_infos = GetAccountUtils(&service).AllFilTestAccounts(); + EXPECT_EQ(account_infos.size(), 2u); + EXPECT_EQ(account_infos[0]->name, "FIL testnet account 1"); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kDerived); + EXPECT_EQ(account_infos[1]->name, "FIL testnet account 2"); + EXPECT_EQ(account_infos[1]->account_id->kind, mojom::AccountKind::kDerived); EXPECT_EQ(last_added_account, service.GetSelectedWalletAccount()); } @@ -3521,38 +3368,17 @@ TEST_F(KeyringServiceUnitTest, ImportFilecoinAccounts) { kPasswordBrave)); observer.WaitAndVerify(); - bool callback_called = false; - service.GetKeyringInfo( - mojom::kFilecoinTestnetKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->id, mojom::kFilecoinTestnetKeyringId); - EXPECT_TRUE(keyring_info->is_keyring_created); - EXPECT_FALSE(keyring_info->is_locked); - EXPECT_FALSE(keyring_info->is_backed_up); - EXPECT_EQ(keyring_info->account_infos.size(), - imported_testnet_accounts.size() - 1); - EXPECT_EQ(keyring_info->account_infos[0]->address, - imported_testnet_accounts[0].address); - EXPECT_EQ(keyring_info->account_infos[0]->name, - imported_testnet_accounts[0].name); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kImported); - EXPECT_EQ(keyring_info->account_infos[1]->address, - imported_testnet_accounts[2].address); - EXPECT_EQ(keyring_info->account_infos[1]->name, - imported_testnet_accounts[2].name); - EXPECT_EQ(keyring_info->account_infos[1]->account_id->kind, - mojom::AccountKind::kImported); - EXPECT_EQ(keyring_info->account_infos[2]->address, - imported_testnet_accounts[3].address); - EXPECT_EQ(keyring_info->account_infos[2]->name, - imported_testnet_accounts[3].name); - EXPECT_EQ(keyring_info->account_infos[2]->account_id->kind, - mojom::AccountKind::kImported); - callback_called = true; - })); - base::RunLoop().RunUntilIdle(); - EXPECT_TRUE(callback_called); + auto account_infos = GetAccountUtils(&service).AllFilTestAccounts(); + EXPECT_EQ(account_infos.size(), imported_testnet_accounts.size() - 1); + EXPECT_EQ(account_infos[0]->address, imported_testnet_accounts[0].address); + EXPECT_EQ(account_infos[0]->name, imported_testnet_accounts[0].name); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kImported); + EXPECT_EQ(account_infos[1]->address, imported_testnet_accounts[2].address); + EXPECT_EQ(account_infos[1]->name, imported_testnet_accounts[2].name); + EXPECT_EQ(account_infos[1]->account_id->kind, mojom::AccountKind::kImported); + EXPECT_EQ(account_infos[2]->address, imported_testnet_accounts[3].address); + EXPECT_EQ(account_infos[2]->name, imported_testnet_accounts[3].name); + EXPECT_EQ(account_infos[2]->account_id->kind, mojom::AccountKind::kImported); EXPECT_EQ(filecoin_testnet_keyring->GetImportedAccountsNumber(), imported_testnet_accounts.size() - 1); service.Lock(); @@ -3566,50 +3392,21 @@ TEST_F(KeyringServiceUnitTest, ImportFilecoinAccounts) { EXPECT_TRUE(Unlock(&service, "brave")); - callback_called = false; + account_infos = GetAccountUtils(&service).AllFilTestAccounts(); // Imported accounts should be restored - service.GetKeyringInfo( - mojom::kFilecoinTestnetKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->account_infos.size(), - imported_testnet_accounts.size() - 1); - EXPECT_EQ(keyring_info->account_infos[0]->address, - imported_testnet_accounts[0].address); - EXPECT_EQ(keyring_info->account_infos[0]->name, - imported_testnet_accounts[0].name); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kImported); - EXPECT_EQ(keyring_info->account_infos[1]->address, - imported_testnet_accounts[2].address); - EXPECT_EQ(keyring_info->account_infos[1]->name, - imported_testnet_accounts[2].name); - EXPECT_EQ(keyring_info->account_infos[1]->account_id->kind, - mojom::AccountKind::kImported); - callback_called = true; - })); - - EXPECT_TRUE(callback_called); - callback_called = false; - - base::RunLoop().RunUntilIdle(); - - service.GetKeyringInfo( - mojom::kFilecoinKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->account_infos.size(), - imported_mainnet_accounts.size() - 1); - EXPECT_EQ(keyring_info->account_infos[0]->address, - imported_mainnet_accounts[0].address); - EXPECT_EQ(keyring_info->account_infos[0]->name, - imported_mainnet_accounts[0].name); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kImported); - callback_called = true; - })); - - EXPECT_TRUE(callback_called); - - base::RunLoop().RunUntilIdle(); + EXPECT_EQ(account_infos.size(), imported_testnet_accounts.size() - 1); + EXPECT_EQ(account_infos[0]->address, imported_testnet_accounts[0].address); + EXPECT_EQ(account_infos[0]->name, imported_testnet_accounts[0].name); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kImported); + EXPECT_EQ(account_infos[1]->address, imported_testnet_accounts[2].address); + EXPECT_EQ(account_infos[1]->name, imported_testnet_accounts[2].name); + EXPECT_EQ(account_infos[1]->account_id->kind, mojom::AccountKind::kImported); + + account_infos = GetAccountUtils(&service).AllFilAccounts(); + EXPECT_EQ(account_infos.size(), imported_mainnet_accounts.size() - 1); + EXPECT_EQ(account_infos[0]->address, imported_mainnet_accounts[0].address); + EXPECT_EQ(account_infos[0]->name, imported_mainnet_accounts[0].name); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kImported); EXPECT_EQ(service.GetHDKeyringById(mojom::kFilecoinTestnetKeyringId) ->GetImportedAccountsNumber(), @@ -3766,22 +3563,13 @@ TEST_F(KeyringServiceUnitTest, SolanaKeyring) { service.Lock(); ASSERT_TRUE(Unlock(&service, "brave")); - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kSolanaKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->id, mojom::kSolanaKeyringId); - EXPECT_TRUE(keyring_info->is_keyring_created); - EXPECT_EQ(keyring_info->account_infos.size(), 2u); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Account 1"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kDerived); - EXPECT_EQ(keyring_info->account_infos[1]->name, "Account 2"); - EXPECT_EQ(keyring_info->account_infos[1]->account_id->kind, - mojom::AccountKind::kDerived); - run_loop.Quit(); - })); - run_loop.Run(); + auto account_infos = GetAccountUtils(&service).AllSolAccounts(); + EXPECT_EQ(account_infos.size(), 2u); + EXPECT_EQ(account_infos[0]->name, "Account 1"); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kDerived); + EXPECT_EQ(account_infos[1]->name, "Account 2"); + EXPECT_EQ(account_infos[1]->account_id->kind, mojom::AccountKind::kDerived); + service.Reset(); } { @@ -3790,8 +3578,7 @@ TEST_F(KeyringServiceUnitTest, SolanaKeyring) { // TODO(apaymyshev): Restore wallet always creates kSolanaKeyringId, but // CreateWallet does not do that. Needs cleanup. - EXPECT_CALL(observer, KeyringRestored(_)).Times(AnyNumber()); - EXPECT_CALL(observer, KeyringRestored(mojom::kSolanaKeyringId)); + EXPECT_CALL(observer, WalletRestored()); ASSERT_TRUE(RestoreWallet(&service, kMnemonicDivideCruise, "brave", false)); EXPECT_TRUE(service.IsKeyringCreated(mojom::kDefaultKeyringId)); EXPECT_TRUE(service.IsKeyringCreated(mojom::kSolanaKeyringId)); @@ -3805,21 +3592,13 @@ TEST_F(KeyringServiceUnitTest, SolanaKeyring) { EXPECT_TRUE(service.IsKeyringCreated(mojom::kSolanaKeyringId)); observer.WaitAndVerify(); - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kSolanaKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->id, mojom::kSolanaKeyringId); - EXPECT_TRUE(keyring_info->is_keyring_created); - EXPECT_EQ(keyring_info->account_infos.size(), 1u); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Account 1"); - EXPECT_EQ(keyring_info->account_infos[0]->address, - "BrG44HdsEhzapvs8bEqzvkq4egwevS3fRE6ze2ENo6S8"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kDerived); - run_loop.Quit(); - })); - run_loop.Run(); + auto account_infos = GetAccountUtils(&service).AllSolAccounts(); + EXPECT_EQ(account_infos.size(), 1u); + EXPECT_EQ(account_infos[0]->name, "Account 1"); + EXPECT_EQ(account_infos[0]->address, + "BrG44HdsEhzapvs8bEqzvkq4egwevS3fRE6ze2ENo6S8"); + EXPECT_EQ(account_infos[0]->account_id->kind, mojom::AccountKind::kDerived); + service.Reset(); } @@ -3877,41 +3656,24 @@ TEST_F(KeyringServiceUnitTest, SolanaKeyring) { ASSERT_TRUE( RemoveAccount(&service, imported_account->account_id, kPasswordBrave)); - base::RunLoop run_loop; - service.GetKeyringInfo( - mojom::kSolanaKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->id, mojom::kSolanaKeyringId); - EXPECT_TRUE(keyring_info->is_keyring_created); - ASSERT_EQ(keyring_info->account_infos.size(), 1u); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Imported Account 1"); - EXPECT_EQ(keyring_info->account_infos[0]->address, - "C5ukMV73nk32h52MjxtnZXTrrr7rupD9CTDDRnYYDRYQ"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kImported); - run_loop.Quit(); - })); - run_loop.Run(); + auto account_infos = GetAccountUtils(&service).AllSolAccounts(); + ASSERT_EQ(account_infos.size(), 1u); + EXPECT_EQ(account_infos[0]->name, "Imported Account 1"); + EXPECT_EQ(account_infos[0]->address, + "C5ukMV73nk32h52MjxtnZXTrrr7rupD9CTDDRnYYDRYQ"); + EXPECT_EQ(account_infos[0]->account_id->kind, + mojom::AccountKind::kImported); service.Lock(); EXPECT_TRUE(Unlock(&service, "brave")); - - base::RunLoop run_loop2; // imported accounts persist after lock & unlock - service.GetKeyringInfo( - mojom::kSolanaKeyringId, - base::BindLambdaForTesting([&](mojom::KeyringInfoPtr keyring_info) { - EXPECT_EQ(keyring_info->id, mojom::kSolanaKeyringId); - EXPECT_TRUE(keyring_info->is_keyring_created); - ASSERT_EQ(keyring_info->account_infos.size(), 1u); - EXPECT_EQ(keyring_info->account_infos[0]->name, "Imported Account 1"); - EXPECT_EQ(keyring_info->account_infos[0]->address, - "C5ukMV73nk32h52MjxtnZXTrrr7rupD9CTDDRnYYDRYQ"); - EXPECT_EQ(keyring_info->account_infos[0]->account_id->kind, - mojom::AccountKind::kImported); - run_loop2.Quit(); - })); - run_loop2.Run(); + account_infos = GetAccountUtils(&service).AllSolAccounts(); + ASSERT_EQ(account_infos.size(), 1u); + EXPECT_EQ(account_infos[0]->name, "Imported Account 1"); + EXPECT_EQ(account_infos[0]->address, + "C5ukMV73nk32h52MjxtnZXTrrr7rupD9CTDDRnYYDRYQ"); + EXPECT_EQ(account_infos[0]->account_id->kind, + mojom::AccountKind::kImported); service.Reset(); } @@ -4050,6 +3812,9 @@ class KeyringServiceAccountDiscoveryUnitTest : public KeyringServiceUnitTest { TEST_F(KeyringServiceAccountDiscoveryUnitTest, AccountDiscovery) { PrepareAccounts(mojom::CoinType::ETH, mojom::kDefaultKeyringId); KeyringService service(json_rpc_service(), GetPrefs(), GetLocalState()); + BraveWalletService brave_wallet_service(shared_url_loader_factory(), nullptr, + &service, json_rpc_service(), nullptr, + GetPrefs(), GetLocalState()); NiceMock observer(service); @@ -4085,6 +3850,9 @@ TEST_F(KeyringServiceAccountDiscoveryUnitTest, SolAccountDiscovery) { PrepareAccounts(mojom::CoinType::SOL, mojom::kSolanaKeyringId); KeyringService service(json_rpc_service(), GetPrefs(), GetLocalState()); + BraveWalletService brave_wallet_service(shared_url_loader_factory(), nullptr, + &service, json_rpc_service(), nullptr, + GetPrefs(), GetLocalState()); NiceMock observer(service); @@ -4122,6 +3890,9 @@ TEST_F(KeyringServiceAccountDiscoveryUnitTest, FilAccountDiscovery) { PrepareAccounts(mojom::CoinType::FIL, mojom::kFilecoinKeyringId); KeyringService service(json_rpc_service(), GetPrefs(), GetLocalState()); + BraveWalletService brave_wallet_service(shared_url_loader_factory(), nullptr, + &service, json_rpc_service(), nullptr, + GetPrefs(), GetLocalState()); NiceMock observer(service); @@ -4158,6 +3929,9 @@ TEST_F(KeyringServiceAccountDiscoveryUnitTest, StopsOnError) { PrepareAccounts(mojom::CoinType::ETH, mojom::kDefaultKeyringId); KeyringService service(json_rpc_service(), GetPrefs(), GetLocalState()); + BraveWalletService brave_wallet_service(shared_url_loader_factory(), nullptr, + &service, json_rpc_service(), nullptr, + GetPrefs(), GetLocalState()); NiceMock observer(service); @@ -4195,6 +3969,9 @@ TEST_F(KeyringServiceAccountDiscoveryUnitTest, ManuallyAddAccount) { PrepareAccounts(mojom::CoinType::ETH, mojom::kDefaultKeyringId); KeyringService service(json_rpc_service(), GetPrefs(), GetLocalState()); + BraveWalletService brave_wallet_service(shared_url_loader_factory(), nullptr, + &service, json_rpc_service(), nullptr, + GetPrefs(), GetLocalState()); NiceMock observer(service); @@ -4253,6 +4030,9 @@ TEST_F(KeyringServiceAccountDiscoveryUnitTest, RestoreWalletTwice) { PrepareAccounts(mojom::CoinType::ETH, mojom::kDefaultKeyringId); KeyringService service(json_rpc_service(), GetPrefs(), GetLocalState()); + BraveWalletService brave_wallet_service(shared_url_loader_factory(), nullptr, + &service, json_rpc_service(), nullptr, + GetPrefs(), GetLocalState()); std::vector requested_addresses; bool first_restore = true; @@ -4734,19 +4514,13 @@ TEST_F(KeyringServiceUnitTest, BitcoinReceiveChangeAddresses) { "abandon abandon about", "brave", false)); - EXPECT_THAT( - service.GetKeyringInfoSync(mojom::kBitcoinKeyring84Id)->account_infos, - testing::IsEmpty()); + EXPECT_THAT(GetAccountUtils(&service).AllBtcAccounts(), testing::IsEmpty()); auto added_account = AddAccount(&service, mojom::CoinType::BTC, mojom::kBitcoinKeyring84Id, "Btc Acc"); - EXPECT_THAT( - service.GetKeyringInfoSync(mojom::kBitcoinKeyring84Id)->account_infos, - testing::SizeIs(1u)); - auto btc_acc = service.GetKeyringInfoSync(mojom::kBitcoinKeyring84Id) - ->account_infos[0] - ->Clone(); + EXPECT_THAT(GetAccountUtils(&service).AllBtcAccounts(), testing::SizeIs(1u)); + auto btc_acc = GetAccountUtils(&service).AllBtcAccounts()[0]->Clone(); EXPECT_EQ(btc_acc, added_account); EXPECT_EQ(btc_acc->address, ""); EXPECT_EQ(btc_acc->name, "Btc Acc"); diff --git a/browser/brave_wallet/notifications/wallet_notification_service_unittest.cc b/browser/brave_wallet/notifications/wallet_notification_service_unittest.cc index 6fc897ebbc77..9004cc2c51b9 100644 --- a/browser/brave_wallet/notifications/wallet_notification_service_unittest.cc +++ b/browser/brave_wallet/notifications/wallet_notification_service_unittest.cc @@ -59,11 +59,12 @@ class WalletNotificationServiceUnitTest : public testing::Test { PrefService* prefs() { return profile_.GetPrefs(); } PrefService* local_state() { return &local_state_; } + AccountUtils GetAccountUtils() { + return AccountUtils(keyring_service_.get()); + } + mojom::AccountIdPtr EthAccount(size_t index) { - return keyring_service_ - ->GetKeyringInfoSync(brave_wallet::mojom::kDefaultKeyringId) - ->account_infos[index] - ->account_id->Clone(); + return GetAccountUtils().EnsureEthAccount(index)->account_id->Clone(); } bool ShouldDisplayNotifications(mojom::TransactionStatus status) { diff --git a/browser/brave_wallet/send_or_sign_transaction_browsertest.cc b/browser/brave_wallet/send_or_sign_transaction_browsertest.cc index c307b8271837..3b9020f8c357 100644 --- a/browser/brave_wallet/send_or_sign_transaction_browsertest.cc +++ b/browser/brave_wallet/send_or_sign_transaction_browsertest.cc @@ -22,6 +22,7 @@ #include "brave/components/brave_wallet/browser/keyring_service.h" #include "brave/components/brave_wallet/browser/permission_utils.h" #include "brave/components/brave_wallet/browser/pref_names.h" +#include "brave/components/brave_wallet/browser/test_utils.h" #include "brave/components/brave_wallet/browser/tx_service.h" #include "brave/components/brave_wallet/common/brave_wallet.mojom.h" #include "brave/components/brave_wallet/common/features.h" @@ -230,17 +231,8 @@ class SendOrSignTransactionBrowserTest : public InProcessBrowserTest { TestTxServiceObserver* observer() { return &observer_; } void RestoreWallet() { - const char mnemonic[] = - "drip caution abandon festival order clown oven regular absorb " - "evidence crew where"; - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - mnemonic, "brave123", false, - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + ASSERT_TRUE(keyring_service_->RestoreWalletSync( + kMnemonicDripCaution, kTestWalletPassword, false)); default_account_ = keyring_service_->GetAllAccountsSync()->accounts[0]->Clone(); @@ -257,7 +249,7 @@ class SendOrSignTransactionBrowserTest : public InProcessBrowserTest { void UnlockWallet() { base::RunLoop run_loop; - keyring_service_->Unlock("brave123", + keyring_service_->Unlock(kTestWalletPassword, base::BindLambdaForTesting([&](bool success) { ASSERT_TRUE(success); run_loop.Quit(); diff --git a/browser/brave_wallet/solana_provider_browsertest.cc b/browser/brave_wallet/solana_provider_browsertest.cc index 093913ce3f37..3a9108611f14 100644 --- a/browser/brave_wallet/solana_provider_browsertest.cc +++ b/browser/brave_wallet/solana_provider_browsertest.cc @@ -454,17 +454,8 @@ class SolanaProviderTest : public InProcessBrowserTest { } void RestoreWallet() { - const char mnemonic[] = - "scare piece awesome elite long drift control cabbage glass dash coral " - "angry"; - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - mnemonic, "brave123", false, - base::BindLambdaForTesting([&run_loop](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + ASSERT_TRUE(keyring_service_->RestoreWalletSync( + kMnemonicScarePiece, kTestWalletPassword, false)); EXPECT_EQ(kFirstAccount, GetAccountUtils().EnsureSolAccount(0)->address); EXPECT_EQ(kSecondAccount, GetAccountUtils().EnsureSolAccount(1)->address); diff --git a/browser/brave_wallet/wallet_watch_asset_browsertest.cc b/browser/brave_wallet/wallet_watch_asset_browsertest.cc index 7a4b600752aa..34220b538574 100644 --- a/browser/brave_wallet/wallet_watch_asset_browsertest.cc +++ b/browser/brave_wallet/wallet_watch_asset_browsertest.cc @@ -13,6 +13,7 @@ #include "brave/components/brave_wallet/browser/brave_wallet_service.h" #include "brave/components/brave_wallet/browser/brave_wallet_utils.h" #include "brave/components/brave_wallet/browser/keyring_service.h" +#include "brave/components/brave_wallet/browser/test_utils.h" #include "brave/components/constants/brave_paths.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" @@ -83,17 +84,8 @@ class WalletWatchAssetBrowserTest : public InProcessBrowserTest { net::EmbeddedTestServer* https_server() { return &https_server_; } void RestoreWallet() { - const char mnemonic[] = - "drip caution abandon festival order clown oven regular absorb " - "evidence crew where"; - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - mnemonic, "brave123", false, - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + ASSERT_TRUE(keyring_service_->RestoreWalletSync( + kMnemonicDripCaution, kTestWalletPassword, false)); } std::vector GetUserAssets() const { diff --git a/browser/ui/views/toolbar/wallet_button_notification_source.cc b/browser/ui/views/toolbar/wallet_button_notification_source.cc index ed6f24dfa234..3a37fd9214cd 100644 --- a/browser/ui/views/toolbar/wallet_button_notification_source.cc +++ b/browser/ui/views/toolbar/wallet_button_notification_source.cc @@ -114,22 +114,20 @@ void WalletButtonNotificationSource::OnTxStatusResolved(uint32_t count) { NotifyObservers(); } -void WalletButtonNotificationSource::OnKeyringReady( - brave_wallet::mojom::KeyringId keyring_id) { - if (keyring_id == brave_wallet::mojom::kDefaultKeyringId) { - prefs_->SetBoolean(kShouldShowWalletSuggestionBadge, false); - NotifyObservers(); - } +void WalletButtonNotificationSource::OnWalletReady() { + prefs_->SetBoolean(kShouldShowWalletSuggestionBadge, false); + NotifyObservers(); } void WalletButtonNotificationSource::KeyringCreated( brave_wallet::mojom::KeyringId keyring_id) { - OnKeyringReady(keyring_id); + if (keyring_id == brave_wallet::mojom::kDefaultKeyringId) { + OnWalletReady(); + } } -void WalletButtonNotificationSource::KeyringRestored( - brave_wallet::mojom::KeyringId keyring_id) { - OnKeyringReady(keyring_id); +void WalletButtonNotificationSource::WalletRestored() { + OnWalletReady(); } void WalletButtonNotificationSource::NotifyObservers() { diff --git a/browser/ui/views/toolbar/wallet_button_notification_source.h b/browser/ui/views/toolbar/wallet_button_notification_source.h index 2e43dc6e1377..3aa6020b0df2 100644 --- a/browser/ui/views/toolbar/wallet_button_notification_source.h +++ b/browser/ui/views/toolbar/wallet_button_notification_source.h @@ -55,9 +55,9 @@ class WalletButtonNotificationSource // brave_wallet::KeyringServiceObserverBase void KeyringCreated(brave_wallet::mojom::KeyringId keyring_id) override; - void KeyringRestored(brave_wallet::mojom::KeyringId keyring_id) override; + void WalletRestored() override; - void OnKeyringReady(brave_wallet::mojom::KeyringId keyring_id); + void OnWalletReady(); void CheckTxStatus(); void OnTxStatusResolved(uint32_t count); void OnKeyringInfoResolved(brave_wallet::mojom::KeyringInfoPtr keyring_info); diff --git a/browser/ui/views/toolbar/wallet_button_notification_source_browsertest.cc b/browser/ui/views/toolbar/wallet_button_notification_source_browsertest.cc index 5ca9b5993ee2..8276125a9259 100644 --- a/browser/ui/views/toolbar/wallet_button_notification_source_browsertest.cc +++ b/browser/ui/views/toolbar/wallet_button_notification_source_browsertest.cc @@ -45,24 +45,17 @@ class WalletButtonNotificationSourceTest : public InProcessBrowserTest { } void RestoreWallet() { - const char mnemonic[] = - "drip caution abandon festival order clown oven regular absorb " - "evidence crew where"; - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - mnemonic, "brave123", false, - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + ASSERT_TRUE(keyring_service_->RestoreWalletSync( + brave_wallet::kMnemonicDripCaution, brave_wallet::kTestWalletPassword, + false)); } void CreateWallet() { base::RunLoop run_loop; keyring_service_->CreateWallet( - "brave123", base::BindLambdaForTesting( - [&](const std::string&) { run_loop.Quit(); })); + brave_wallet::kTestWalletPassword, + base::BindLambdaForTesting( + [&](const std::string&) { run_loop.Quit(); })); run_loop.Run(); } diff --git a/browser/ui/webui/brave_wallet/common_handler/wallet_handler.cc b/browser/ui/webui/brave_wallet/common_handler/wallet_handler.cc index 95cbd6947e08..8399b25b5adc 100644 --- a/browser/ui/webui/brave_wallet/common_handler/wallet_handler.cc +++ b/browser/ui/webui/brave_wallet/common_handler/wallet_handler.cc @@ -38,8 +38,9 @@ void WalletHandler::GetWalletInfo(GetWalletInfoCallback callback) { DCHECK_EQ(default_keyring->id, mojom::kDefaultKeyringId); std::move(callback).Run(mojom::WalletInfo::New( default_keyring->is_keyring_created, default_keyring->is_locked, - default_keyring->is_backed_up, IsFilecoinEnabled(), IsSolanaEnabled(), - IsBitcoinEnabled(), IsNftPinningEnabled(), IsPanelV2Enabled())); + keyring_service_->IsWalletBackedUpSync(), IsFilecoinEnabled(), + IsSolanaEnabled(), IsBitcoinEnabled(), IsNftPinningEnabled(), + IsPanelV2Enabled())); } } // namespace brave_wallet diff --git a/components/brave_wallet/browser/android_page_appearing_browsertest.cc b/components/brave_wallet/browser/android_page_appearing_browsertest.cc index b959e7710411..26079a0619f3 100644 --- a/components/brave_wallet/browser/android_page_appearing_browsertest.cc +++ b/components/brave_wallet/browser/android_page_appearing_browsertest.cc @@ -281,14 +281,8 @@ class AndroidPageAppearingBrowserTest : public PlatformBrowserTest { asset_ratio_service_->SetAPIRequestHelperForTesting( shared_url_loader_factory_); - base::RunLoop run_loop; - keyring_service_->RestoreWallet( - kMnemonicDivideCruise, kPasswordBrave, false, - base::BindLambdaForTesting([&](bool success) { - ASSERT_TRUE(success); - run_loop.Quit(); - })); - run_loop.Run(); + ASSERT_TRUE(keyring_service_->RestoreWalletSync(kMnemonicDivideCruise, + kPasswordBrave, false)); TokenListMap token_list_map; ASSERT_TRUE( diff --git a/components/brave_wallet/browser/brave_wallet_service.cc b/components/brave_wallet/browser/brave_wallet_service.cc index 35394ec0479d..1005c04e425e 100644 --- a/components/brave_wallet/browser/brave_wallet_service.cc +++ b/components/brave_wallet/browser/brave_wallet_service.cc @@ -8,11 +8,13 @@ #include #include +#include "base/check_is_test.h" #include "base/notreached.h" #include "base/ranges/algorithm.h" #include "base/strings/strcat.h" #include "base/strings/string_util.h" #include "base/values.h" +#include "brave/components/brave_wallet/browser/account_discovery_manager.h" #include "brave/components/brave_wallet/browser/blockchain_registry.h" #include "brave/components/brave_wallet/browser/brave_wallet_constants.h" #include "brave/components/brave_wallet/browser/brave_wallet_prefs.h" @@ -215,8 +217,14 @@ BraveWalletService::BraveWalletService( url_loader_factory, this, json_rpc_service, keyring_service, simple_hash_client_.get(), profile_prefs); - CHECK(delegate_); - delegate_->AddObserver(this); + if (!delegate_) { + CHECK_IS_TEST(); + } else { + delegate_->AddObserver(this); + } + + keyring_service_->AddObserver( + keyring_observer_receiver_.BindNewPipeAndPassRemote()); DCHECK(profile_prefs_); @@ -1589,6 +1597,12 @@ void BraveWalletService::OnActiveOriginChanged( } } +void BraveWalletService::WalletRestored() { + account_discovery_manager_ = std::make_unique( + json_rpc_service_.get(), keyring_service_.get()); + account_discovery_manager_->StartDiscovery(); +} + void BraveWalletService::OnDiscoverAssetsStarted() { for (const auto& observer : observers_) { observer->OnDiscoverAssetsStarted(); @@ -1635,26 +1649,19 @@ void BraveWalletService::OnGetImportInfo( return; } - keyring_service_->RestoreWallet( - info.mnemonic, new_password, info.is_legacy_crypto_wallets, - base::BindOnce( - [](ImportFromExternalWalletCallback callback, - size_t number_of_accounts, KeyringService* keyring_service, - bool is_valid_mnemonic) { - if (!is_valid_mnemonic) { - std::move(callback).Run( - false, - l10n_util::GetStringUTF8(IDS_WALLET_INVALID_MNEMONIC_ERROR)); - return; - } - if (number_of_accounts > 1) { - keyring_service->AddAccountsWithDefaultName( - mojom::CoinType::ETH, mojom::kDefaultKeyringId, - number_of_accounts - 1); - } - std::move(callback).Run(is_valid_mnemonic, absl::nullopt); - }, - std::move(callback), info.number_of_accounts, keyring_service_)); + bool is_valid_mnemonic = keyring_service_->RestoreWalletSync( + info.mnemonic, new_password, info.is_legacy_crypto_wallets); + if (!is_valid_mnemonic) { + std::move(callback).Run( + false, l10n_util::GetStringUTF8(IDS_WALLET_INVALID_MNEMONIC_ERROR)); + return; + } + if (info.number_of_accounts > 1) { + keyring_service_->AddAccountsWithDefaultName(mojom::CoinType::ETH, + mojom::kDefaultKeyringId, + info.number_of_accounts - 1); + } + std::move(callback).Run(is_valid_mnemonic, absl::nullopt); } void BraveWalletService::AddSignMessageRequest( @@ -1963,22 +1970,19 @@ void BraveWalletService::DiscoverAssetsOnAllSupportedChains() { void BraveWalletService::DiscoverAssetsOnAllSupportedChains( bool bypass_rate_limit) { std::map> addresses; - // Fetch ETH addresses - mojom::KeyringInfoPtr keyring_info = keyring_service_->GetKeyringInfoSync( - brave_wallet::mojom::kDefaultKeyringId); - std::vector eth_account_addresses; - for (auto& account_info : keyring_info->account_infos) { - eth_account_addresses.push_back(account_info->address); - } - addresses[mojom::CoinType::ETH] = std::move(eth_account_addresses); + const auto& all_accounts = keyring_service_->GetAllAccountInfos(); - // Fetch SOL addresses - keyring_info = keyring_service_->GetKeyringInfoSync( - brave_wallet::mojom::kSolanaKeyringId); + std::vector eth_account_addresses; std::vector sol_account_addresses; - for (const auto& account_info : keyring_info->account_infos) { - sol_account_addresses.push_back(account_info->address); + for (auto& account_info : all_accounts) { + if (account_info->account_id->coin == mojom::CoinType::ETH) { + eth_account_addresses.push_back(account_info->address); + } + if (account_info->account_id->coin == mojom::CoinType::SOL) { + sol_account_addresses.push_back(account_info->address); + } } + addresses[mojom::CoinType::ETH] = std::move(eth_account_addresses); addresses[mojom::CoinType::SOL] = std::move(sol_account_addresses); // Discover assets owned by the SOL and ETH addresses on all supported chains @@ -2141,6 +2145,7 @@ void BraveWalletService::Reset() { if (keyring_service_) { keyring_service_->Reset(); } + account_discovery_manager_.reset(); for (const auto& observer : observers_) { observer->OnResetWallet(); diff --git a/components/brave_wallet/browser/brave_wallet_service.h b/components/brave_wallet/browser/brave_wallet_service.h index e9e39ff6ed6d..4dd185805d01 100644 --- a/components/brave_wallet/browser/brave_wallet_service.h +++ b/components/brave_wallet/browser/brave_wallet_service.h @@ -46,11 +46,13 @@ class KeyringService; class JsonRpcService; class TxService; class EthAllowanceManager; +class AccountDiscoveryManager; struct PendingDecryptRequest; struct PendingGetEncryptPublicKeyRequest; class BraveWalletService : public KeyedService, public mojom::BraveWalletService, + public KeyringServiceObserverBase, public BraveWalletServiceDelegate::Observer { public: using APIRequestHelper = api_request_helper::APIRequestHelper; @@ -255,6 +257,9 @@ class BraveWalletService : public KeyedService, // BraveWalletServiceDelegate::Observer: void OnActiveOriginChanged(const mojom::OriginInfoPtr& origin_info) override; + // KeyringServiceObserverBase: + void WalletRestored() override; + void OnDiscoverAssetsStarted(); void OnDiscoverAssetsCompleted( @@ -398,7 +403,10 @@ class BraveWalletService : public KeyedService, std::unique_ptr simple_hash_client_; std::unique_ptr asset_discovery_manager_; std::unique_ptr eth_allowance_manager_; + std::unique_ptr account_discovery_manager_; mojo::ReceiverSet receivers_; + mojo::Receiver + keyring_observer_receiver_{this}; PrefChangeRegistrar pref_change_registrar_; base::WeakPtrFactory weak_ptr_factory_; }; diff --git a/components/brave_wallet/browser/eth_allowance_manager.cc b/components/brave_wallet/browser/eth_allowance_manager.cc index 8ba843c9b551..52d766150efd 100644 --- a/components/brave_wallet/browser/eth_allowance_manager.cc +++ b/components/brave_wallet/browser/eth_allowance_manager.cc @@ -115,11 +115,11 @@ void EthAllowanceManager::DiscoverEthAllowancesOnAllSupportedChains( } discover_eth_allowance_callbacks_.push_back(std::move(callback)); - const auto keyring_info = - keyring_service_->GetKeyringInfoSync(mojom::kDefaultKeyringId); std::vector account_addresses; - for (const auto& account_info : keyring_info->account_infos) { - account_addresses.push_back(account_info->address); + for (const auto& account_info : keyring_service_->GetAllAccountInfos()) { + if (account_info->account_id->coin == mojom::CoinType::ETH) { + account_addresses.push_back(account_info->address); + } } if (account_addresses.empty()) { diff --git a/components/brave_wallet/browser/eth_tx_manager_unittest.cc b/components/brave_wallet/browser/eth_tx_manager_unittest.cc index 85aa276ab323..3906e380b3c5 100644 --- a/components/brave_wallet/browser/eth_tx_manager_unittest.cc +++ b/components/brave_wallet/browser/eth_tx_manager_unittest.cc @@ -285,13 +285,14 @@ class EthTxManagerUnitTest : public testing::Test { &data_)); } + AccountUtils GetAccountUtils() { + return AccountUtils(keyring_service_.get()); + } + mojom::AccountIdPtr from() { return EthAccount(0); } mojom::AccountIdPtr EthAccount(size_t index) { - return keyring_service_ - ->GetKeyringInfoSync(brave_wallet::mojom::kDefaultKeyringId) - ->account_infos[index] - ->account_id->Clone(); + return GetAccountUtils().EnsureEthAccount(index)->account_id->Clone(); } url::Origin GetOrigin() const { diff --git a/components/brave_wallet/browser/ethereum_provider_impl.cc b/components/brave_wallet/browser/ethereum_provider_impl.cc index 1ebcc658f2d9..9e16a70aa314 100644 --- a/components/brave_wallet/browser/ethereum_provider_impl.cc +++ b/components/brave_wallet/browser/ethereum_provider_impl.cc @@ -287,7 +287,7 @@ void EthereumProviderImpl::SendOrSignTransactionInternal( } if (ShouldCreate1559Tx(tx_data_1559.Clone(), chain->is_eip1559, - keyring_info->account_infos, from)) { + keyring_service_->GetAllAccountInfos(), account_id)) { // Set chain_id to current chain_id. tx_data_1559->chain_id = chain->chain_id; tx_service_->AddUnapprovedTransactionWithOrigin( @@ -1118,8 +1118,10 @@ void EthereumProviderImpl::RequestEthereumPermissions( } std::vector addresses; - for (const auto& account_info : keyring_info->account_infos) { - addresses.push_back(account_info->address); + for (auto& account_info : keyring_service_->GetAllAccountInfos()) { + if (account_info->account_id->coin == mojom::CoinType::ETH) { + addresses.push_back(account_info->address); + } } if (keyring_info->is_locked) { @@ -1238,8 +1240,10 @@ EthereumProviderImpl::GetAllowedAccounts(bool include_accounts_when_locked) { brave_wallet::mojom::kDefaultKeyringId); std::vector addresses; - for (const auto& account_info : keyring_info->account_infos) { - addresses.push_back(base::ToLowerASCII(account_info->address)); + for (const auto& account_info : keyring_service_->GetAllAccountInfos()) { + if (account_info->account_id->coin == mojom::CoinType::ETH) { + addresses.push_back(base::ToLowerASCII(account_info->address)); + } } const auto selected_account = diff --git a/components/brave_wallet/browser/fil_tx_manager_unittest.cc b/components/brave_wallet/browser/fil_tx_manager_unittest.cc index 658e0a8b4c65..e99260b755e2 100644 --- a/components/brave_wallet/browser/fil_tx_manager_unittest.cc +++ b/components/brave_wallet/browser/fil_tx_manager_unittest.cc @@ -97,15 +97,12 @@ class FilTxManagerUnitTest : public testing::Test { mojom::CoinType::FIL, mojom::kFilecoinTestnetKeyringId, "Account 1"); } - mojom::AccountIdPtr FilTestAcc(size_t index) { - return keyring_service_ - ->GetKeyringInfoSync(mojom::KeyringId::kFilecoinTestnet) - ->account_infos[index] - ->account_id.Clone(); + AccountUtils GetAccountUtils() { + return AccountUtils(keyring_service_.get()); } - std::string from(mojom::KeyringId keyring_id) { - return keyring_service_->GetHDKeyringById(keyring_id)->GetAddress(0); + mojom::AccountIdPtr FilTestAcc(size_t index) { + return GetAccountUtils().EnsureFilTestAccount(index)->account_id->Clone(); } void SetInterceptor(const GURL& expected_url, diff --git a/components/brave_wallet/browser/keyring_service.cc b/components/brave_wallet/browser/keyring_service.cc index e21f8632988a..594f8e02e201 100644 --- a/components/brave_wallet/browser/keyring_service.cc +++ b/components/brave_wallet/browser/keyring_service.cc @@ -972,9 +972,6 @@ HDKeyring* KeyringService::RestoreKeyring(mojom::KeyringId keyring_id, return nullptr; } - for (const auto& observer : observers_) { - observer->KeyringRestored(keyring_id); - } ResetAutoLockTimer(); return keyring; } @@ -994,7 +991,6 @@ mojom::KeyringInfoPtr KeyringService::GetKeyringInfoSync( backup_complete = value->GetBool(); } keyring_info->is_backed_up = backup_complete; - keyring_info->account_infos = GetAccountInfosForKeyring(keyring_id); return keyring_info; } @@ -1082,6 +1078,13 @@ void KeyringService::RestoreWallet(const std::string& mnemonic, const std::string& password, bool is_legacy_brave_wallet, RestoreWalletCallback callback) { + std::move(callback).Run( + RestoreWalletSync(mnemonic, password, is_legacy_brave_wallet)); +} + +bool KeyringService::RestoreWalletSync(const std::string& mnemonic, + const std::string& password, + bool is_legacy_brave_wallet) { auto* keyring = RestoreKeyring(mojom::kDefaultKeyringId, mnemonic, password, is_legacy_brave_wallet); if (keyring && !GetDerivedAccountsNumberForKeyring( @@ -1117,11 +1120,13 @@ void KeyringService::RestoreWallet(const std::string& mnemonic, ResetAllAccountInfosCache(); - account_discovery_manager_ = - std::make_unique(json_rpc_service_, this); - account_discovery_manager_->StartDiscovery(); + if (keyring) { + for (const auto& observer : observers_) { + observer->WalletRestored(); + } + } - std::move(callback).Run(keyring); + return !!keyring; } std::string KeyringService::GetMnemonicForKeyringImpl( @@ -1463,13 +1468,16 @@ bool KeyringService::RemoveImportedAccountInternal( } void KeyringService::IsWalletBackedUp(IsWalletBackedUpCallback callback) { - bool backup_complete = false; + std::move(callback).Run(IsWalletBackedUpSync()); +} + +bool KeyringService::IsWalletBackedUpSync() { const base::Value* value = GetPrefForKeyring(*profile_prefs_, kBackupComplete, mojom::kDefaultKeyringId); if (value) { - backup_complete = value->GetBool(); + return value->GetBool(); } - std::move(callback).Run(backup_complete); + return false; } void KeyringService::NotifyWalletBackupComplete() { @@ -1927,7 +1935,6 @@ void KeyringService::IsLocked(IsLockedCallback callback) { } void KeyringService::Reset(bool notify_observer) { - account_discovery_manager_.reset(); StopAutoLockTimer(); encryptors_.clear(); keyrings_.clear(); diff --git a/components/brave_wallet/browser/keyring_service.h b/components/brave_wallet/browser/keyring_service.h index ff0bfba0eb3c..ed880bd7077c 100644 --- a/components/brave_wallet/browser/keyring_service.h +++ b/components/brave_wallet/browser/keyring_service.h @@ -14,7 +14,6 @@ #include "base/gtest_prod_util.h" #include "base/memory/raw_ptr.h" #include "base/values.h" -#include "brave/components/brave_wallet/browser/account_discovery_manager.h" #include "brave/components/brave_wallet/browser/hd_keyring.h" #include "brave/components/brave_wallet/browser/password_encryptor.h" #include "brave/components/brave_wallet/common/brave_wallet.mojom.h" @@ -100,6 +99,9 @@ class KeyringService : public KeyedService, public mojom::KeyringService { const std::string& password, bool is_legacy_brave_wallet, RestoreWalletCallback callback) override; + bool RestoreWalletSync(const std::string& mnemonic, + const std::string& password, + bool is_legacy_brave_wallet); void Unlock(const std::string& password, UnlockCallback callback) override; void Lock() override; void IsLocked(IsLockedCallback callback) override; @@ -134,6 +136,7 @@ class KeyringService : public KeyedService, public mojom::KeyringService { const std::string& password, RemoveAccountCallback callback) override; void IsWalletBackedUp(IsWalletBackedUpCallback callback) override; + bool IsWalletBackedUpSync(); void NotifyWalletBackupComplete() override; mojom::KeyringInfoPtr GetKeyringInfoSync(mojom::KeyringId keyring_id); void GetKeyringInfo(mojom::KeyringId keyring_id, @@ -385,8 +388,6 @@ class KeyringService : public KeyedService, public mojom::KeyringService { mojo::RemoteSet observers_; mojo::ReceiverSet receivers_; - std::unique_ptr account_discovery_manager_; - KeyringService(const KeyringService&) = delete; KeyringService& operator=(const KeyringService&) = delete; }; diff --git a/components/brave_wallet/browser/keyring_service_observer_base.h b/components/brave_wallet/browser/keyring_service_observer_base.h index b477e099508f..12ae76ad5cc2 100644 --- a/components/brave_wallet/browser/keyring_service_observer_base.h +++ b/components/brave_wallet/browser/keyring_service_observer_base.h @@ -15,7 +15,7 @@ namespace brave_wallet { class KeyringServiceObserverBase : public mojom::KeyringServiceObserver { void KeyringCreated(mojom::KeyringId keyring_id) override {} - void KeyringRestored(mojom::KeyringId keyring_id) override {} + void WalletRestored() override {} void KeyringReset() override {} void Locked() override {} void Unlocked() override {} diff --git a/components/brave_wallet/browser/solana_keyring_unittest.cc b/components/brave_wallet/browser/solana_keyring_unittest.cc index 04971596f243..7cb6ce722271 100644 --- a/components/brave_wallet/browser/solana_keyring_unittest.cc +++ b/components/brave_wallet/browser/solana_keyring_unittest.cc @@ -8,6 +8,7 @@ #include "base/strings/string_number_conversions.h" #include "brave/components/brave_wallet/browser/brave_wallet_utils.h" #include "brave/components/brave_wallet/browser/solana_keyring.h" +#include "brave/components/brave_wallet/browser/test_utils.h" #include "brave/components/brave_wallet/common/brave_wallet.mojom.h" #include "brave/components/brave_wallet/common/brave_wallet_constants.h" #include "brave/components/brave_wallet/common/encoding_utils.h" @@ -16,16 +17,10 @@ namespace brave_wallet { -namespace { -const char mnemonic[] = - "scare piece awesome elite long drift control cabbage glass dash coral " - "angry"; -} // namespace - TEST(SolanaKeyringUnitTest, ConstructRootHDKey) { SolanaKeyring keyring; std::unique_ptr> seed = - MnemonicToSeed(std::string(mnemonic), ""); + MnemonicToSeed(kMnemonicScarePiece, ""); keyring.ConstructRootHDKey(*seed, "m/44'/501'"); EXPECT_EQ(keyring.root_->EncodePrivateKeyForExport(), @@ -36,7 +31,7 @@ TEST(SolanaKeyringUnitTest, ConstructRootHDKey) { TEST(SolanaKeyringUnitTest, Accounts) { SolanaKeyring keyring; std::unique_ptr> seed = - MnemonicToSeed(std::string(mnemonic), ""); + MnemonicToSeed(kMnemonicScarePiece, ""); keyring.ConstructRootHDKey(*seed, "m/44'/501'"); keyring.AddAccounts(1); @@ -91,7 +86,7 @@ TEST(SolanaKeyringUnitTest, Accounts) { TEST(SolanaKeyringUnitTest, SignMessage) { SolanaKeyring keyring; std::unique_ptr> seed = - MnemonicToSeed(std::string(mnemonic), ""); + MnemonicToSeed(kMnemonicScarePiece, ""); keyring.ConstructRootHDKey(*seed, "m/44'/501'"); keyring.AddAccounts(1); diff --git a/components/brave_wallet/browser/solana_provider_impl.cc b/components/brave_wallet/browser/solana_provider_impl.cc index 056b2ca7d76f..fb71fb374280 100644 --- a/components/brave_wallet/browser/solana_provider_impl.cc +++ b/components/brave_wallet/browser/solana_provider_impl.cc @@ -128,28 +128,27 @@ void SolanaProviderImpl::Connect(absl::optional arg, mojom::SolanaProviderError::kUserRejectedRequest, l10n_util::GetStringUTF8(IDS_WALLET_USER_REJECTED_REQUEST), ""); } else { - const auto keyring_info = keyring_service_->GetKeyringInfoSync( - brave_wallet::mojom::KeyringId::kSolana); + std::vector sol_accounts; std::vector addresses; - for (const auto& account_info : keyring_info->account_infos) { - addresses.push_back(account_info->address); + for (const auto& account_info : keyring_service_->GetAllAccountInfos()) { + if (account_info->account_id->coin == mojom::CoinType::SOL) { + sol_accounts.push_back(account_info.Clone()); + addresses.push_back(account_info->address); + } } // filter out already permitted accounts if exists const auto allowed_accounts = delegate_->GetAllowedAccounts(mojom::CoinType::SOL, addresses); if (allowed_accounts) { - addresses.erase(base::ranges::remove_if( - addresses, - [&allowed_accounts](const auto& address) { - return base::Contains(*allowed_accounts, address); - }), - addresses.end()); + base::EraseIf(addresses, [&allowed_accounts](const auto& address) { + return base::Contains(*allowed_accounts, address); + }); } delegate_->RequestPermissions( mojom::CoinType::SOL, addresses, - base::BindOnce( - &SolanaProviderImpl::OnConnect, weak_factory_.GetWeakPtr(), - std::move(keyring_info->account_infos), std::move(callback))); + base::BindOnce(&SolanaProviderImpl::OnConnect, + weak_factory_.GetWeakPtr(), std::move(sol_accounts), + std::move(callback))); } // To show wallet icon on android if wallet is unlocked diff --git a/components/brave_wallet/browser/solana_transaction_unittest.cc b/components/brave_wallet/browser/solana_transaction_unittest.cc index c75f47b12122..3e80bf63b1ea 100644 --- a/components/brave_wallet/browser/solana_transaction_unittest.cc +++ b/components/brave_wallet/browser/solana_transaction_unittest.cc @@ -66,15 +66,8 @@ class SolanaTransactionUnitTest : public testing::Test { const std::string& mnemonic, const std::string& password, bool is_legacy_brave_wallet) { - bool success = false; - base::RunLoop run_loop; - service->RestoreWallet(mnemonic, password, is_legacy_brave_wallet, - base::BindLambdaForTesting([&](bool v) { - success = v; - run_loop.Quit(); - })); - run_loop.Run(); - return success; + return service->RestoreWalletSync(mnemonic, password, + is_legacy_brave_wallet); } static mojom::AccountInfoPtr AddAccount(KeyringService* service, diff --git a/components/brave_wallet/browser/solana_tx_manager_unittest.cc b/components/brave_wallet/browser/solana_tx_manager_unittest.cc index 9a92bb4b51e6..94a46bd7d28c 100644 --- a/components/brave_wallet/browser/solana_tx_manager_unittest.cc +++ b/components/brave_wallet/browser/solana_tx_manager_unittest.cc @@ -107,10 +107,12 @@ class SolanaTxManagerUnitTest : public testing::Test { mojom::CoinType::SOL, mojom::kSolanaKeyringId, "New Account"); } + AccountUtils GetAccountUtils() { + return AccountUtils(keyring_service_.get()); + } + mojom::AccountIdPtr SolAccount(size_t index) { - return keyring_service_->GetKeyringInfoSync(mojom::KeyringId::kSolana) - ->account_infos[index] - ->account_id->Clone(); + return GetAccountUtils().EnsureSolAccount(index)->account_id->Clone(); } void SetInterceptor(const std::string& latest_blockhash, diff --git a/components/brave_wallet/browser/test_utils.cc b/components/brave_wallet/browser/test_utils.cc index fb2928ea2fd7..aa86521ca05d 100644 --- a/components/brave_wallet/browser/test_utils.cc +++ b/components/brave_wallet/browser/test_utils.cc @@ -189,6 +189,41 @@ mojom::AccountIdPtr AccountUtils::FindAccountIdByAddress( return nullptr; } +std::vector AccountUtils::AllAccounts( + mojom::KeyringId keyring_id) { + std::vector result; + for (auto& acc : keyring_service_->GetAllAccountInfos()) { + if (acc->account_id->keyring_id == keyring_id) { + result.push_back(acc->Clone()); + } + } + return result; +} + +std::vector AccountUtils::AllEthAccounts() { + return AllAccounts(mojom::KeyringId::kDefault); +} + +std::vector AccountUtils::AllSolAccounts() { + return AllAccounts(mojom::KeyringId::kSolana); +} + +std::vector AccountUtils::AllFilAccounts() { + return AllAccounts(mojom::KeyringId::kFilecoin); +} + +std::vector AccountUtils::AllFilTestAccounts() { + return AllAccounts(mojom::KeyringId::kFilecoinTestnet); +} + +std::vector AccountUtils::AllBtcAccounts() { + return AllAccounts(mojom::KeyringId::kBitcoin84); +} + +std::vector AccountUtils::AllBtcTestAccounts() { + return AllAccounts(mojom::KeyringId::kBitcoin84Testnet); +} + void WaitForTxStorageDelegateInitialized(TxStorageDelegate* delegate) { if (delegate->IsInitialized()) { return; diff --git a/components/brave_wallet/browser/test_utils.h b/components/brave_wallet/browser/test_utils.h index 53d131506526..9a64eae69afc 100644 --- a/components/brave_wallet/browser/test_utils.h +++ b/components/brave_wallet/browser/test_utils.h @@ -27,6 +27,12 @@ namespace brave_wallet { constexpr char kMnemonicDivideCruise[] = "divide cruise upon flag harsh carbon filter merit once advice bright " "drive"; +constexpr char kMnemonicDripCaution[] = + "drip caution abandon festival order clown oven regular absorb evidence " + "crew where"; +constexpr char kMnemonicScarePiece[] = + "scare piece awesome elite long drift control cabbage glass dash coral " + "angry"; constexpr char kTestWalletPassword[] = "brave"; class KeyringService; @@ -84,6 +90,14 @@ class AccountUtils { mojom::AccountIdPtr FindAccountIdByAddress(const std::string& address); + std::vector AllAccounts(mojom::KeyringId keyring_id); + std::vector AllEthAccounts(); + std::vector AllSolAccounts(); + std::vector AllFilAccounts(); + std::vector AllFilTestAccounts(); + std::vector AllBtcAccounts(); + std::vector AllBtcTestAccounts(); + private: raw_ptr keyring_service_; }; diff --git a/components/brave_wallet/common/brave_wallet.mojom b/components/brave_wallet/common/brave_wallet.mojom index 7d768bd7f142..2444c44f5177 100644 --- a/components/brave_wallet/common/brave_wallet.mojom +++ b/components/brave_wallet/common/brave_wallet.mojom @@ -705,7 +705,6 @@ struct KeyringInfo { bool is_keyring_created; bool is_locked; bool is_backed_up; - array account_infos; }; struct WalletInfo { @@ -762,8 +761,8 @@ interface KeyringServiceObserver { // Fired when a keyring is created KeyringCreated(KeyringId keyring_id); - // Fired when a keyring is restored - KeyringRestored(KeyringId keyring_id); + // Fired when wallet is restored + WalletRestored(); // Fired when a keyring is reset KeyringReset(); diff --git a/components/brave_wallet/common/eth_request_helper.cc b/components/brave_wallet/common/eth_request_helper.cc index e496297f3b16..2f4aa8aa41d3 100644 --- a/components/brave_wallet/common/eth_request_helper.cc +++ b/components/brave_wallet/common/eth_request_helper.cc @@ -191,11 +191,11 @@ mojom::TxData1559Ptr ParseEthTransaction1559Params(const std::string& json, bool ShouldCreate1559Tx(brave_wallet::mojom::TxData1559Ptr tx_data_1559, bool network_supports_eip1559, const std::vector& account_infos, - const std::string& address) { + const mojom::AccountIdPtr& account_id) { bool keyring_supports_eip1559 = true; auto account_it = base::ranges::find_if( account_infos, [&](const mojom::AccountInfoPtr& account) { - return base::EqualsCaseInsensitiveASCII(account->address, address); + return account->account_id == account_id; }); // Only ledger and trezor hardware keyrings support EIP-1559 at the moment. diff --git a/components/brave_wallet/common/eth_request_helper.h b/components/brave_wallet/common/eth_request_helper.h index 5bddab289b4f..c318de3d147c 100644 --- a/components/brave_wallet/common/eth_request_helper.h +++ b/components/brave_wallet/common/eth_request_helper.h @@ -27,7 +27,7 @@ mojom::TxData1559Ptr ParseEthTransaction1559Params(const std::string& json, bool ShouldCreate1559Tx(mojom::TxData1559Ptr tx_data_1559, bool network_supports_eip1559, const std::vector& account_infos, - const std::string& address); + const mojom::AccountIdPtr& account_id); bool NormalizeEthRequest(const std::string& input_json, std::string* output_json); diff --git a/components/brave_wallet/common/eth_request_helper_unittest.cc b/components/brave_wallet/common/eth_request_helper_unittest.cc index 3c1b7ce03e81..8c406c7e03a4 100644 --- a/components/brave_wallet/common/eth_request_helper_unittest.cc +++ b/components/brave_wallet/common/eth_request_helper_unittest.cc @@ -196,10 +196,10 @@ TEST(EthResponseHelperUnitTest, ShouldCreate1559Tx) { hw_address, "hw", mojom::HardwareInfo::New("m/44'/60'/1'/0/0", "Hardware", "123")); std::vector account_infos; - account_infos.push_back(std::move(primary_account)); - account_infos.push_back(std::move(ledger_account)); - account_infos.push_back(std::move(trezor_account)); - account_infos.push_back(std::move(hw_account)); + account_infos.push_back(primary_account.Clone()); + account_infos.push_back(ledger_account.Clone()); + account_infos.push_back(trezor_account.Clone()); + account_infos.push_back(hw_account.Clone()); // Test both EIP1559 and legacy gas fee fields are specified. std::string json( @@ -222,25 +222,21 @@ TEST(EthResponseHelperUnitTest, ShouldCreate1559Tx) { ASSERT_TRUE(tx_data); EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), true /* network_supports_eip1559 */, - account_infos, from)); - EXPECT_TRUE( - ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, ledger_address)); + account_infos, primary_account->account_id)); EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, - base::ToLowerASCII(ledger_address))); - EXPECT_TRUE( - ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, trezor_address)); + ledger_account->account_id)); EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, - base::ToLowerASCII(trezor_address))); + trezor_account->account_id)); // From is not found in the account infos, can happen when keyring is locked. - EXPECT_TRUE(ShouldCreate1559Tx( - tx_data.Clone(), true /* network_supports_eip1559 */, {}, from)); + EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), + true /* network_supports_eip1559 */, {}, + primary_account->account_id)); // Network doesn't support EIP1559 - EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, from)); + EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, + primary_account->account_id)); // Keyring doesn't support EIP1559 - EXPECT_FALSE( - ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, hw_address)); EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, - base::ToLowerASCII(hw_address))); + hw_account->account_id)); // Test only EIP1559 gas fee fields are specified. json = @@ -261,8 +257,9 @@ TEST(EthResponseHelperUnitTest, ShouldCreate1559Tx) { ASSERT_TRUE(tx_data); EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), true /* network_supports_eip1559 */, - account_infos, from)); - EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, from)); + account_infos, primary_account->account_id)); + EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, + primary_account->account_id)); // Test only legacy gas field is specified. json = @@ -281,8 +278,9 @@ TEST(EthResponseHelperUnitTest, ShouldCreate1559Tx) { ASSERT_TRUE(tx_data); EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), true /* network_supports_eip1559 */, - account_infos, from)); - EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, from)); + account_infos, primary_account->account_id)); + EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, + primary_account->account_id)); // Test no gas fee fields are specified. json = @@ -297,28 +295,24 @@ TEST(EthResponseHelperUnitTest, ShouldCreate1559Tx) { })"; tx_data = ParseEthTransaction1559Params(json, &from); ASSERT_TRUE(tx_data); - EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, from)); EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, - base::ToLowerASCII(from))); - EXPECT_TRUE( - ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, ledger_address)); + primary_account->account_id)); EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, - base::ToLowerASCII(ledger_address))); - EXPECT_TRUE( - ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, trezor_address)); + ledger_account->account_id)); EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, - base::ToLowerASCII(trezor_address))); + trezor_account->account_id)); // From is not found in the account infos, can happen when keyring is locked. - EXPECT_TRUE(ShouldCreate1559Tx( - tx_data.Clone(), true /* network_supports_eip1559 */, {}, from)); + EXPECT_TRUE(ShouldCreate1559Tx(tx_data.Clone(), + true /* network_supports_eip1559 */, {}, + primary_account->account_id)); - EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, from)); - EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, from)); + EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, + primary_account->account_id)); + EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), false, account_infos, + primary_account->account_id)); // Keyring does't support EIP1559 - EXPECT_FALSE( - ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, hw_address)); EXPECT_FALSE(ShouldCreate1559Tx(tx_data.Clone(), true, account_infos, - base::ToLowerASCII(hw_address))); + hw_account->account_id)); } TEST(EthResponseHelperUnitTest, ParseEthSignParams) { diff --git a/components/brave_wallet_ui/common/actions/wallet_actions.ts b/components/brave_wallet_ui/common/actions/wallet_actions.ts index f2a3b90baf9a..cb1c6b91c08e 100644 --- a/components/brave_wallet_ui/common/actions/wallet_actions.ts +++ b/components/brave_wallet_ui/common/actions/wallet_actions.ts @@ -32,7 +32,7 @@ export const { initialized, keyringCreated, keyringReset, - keyringRestored, + walletRestored, locked, lockWallet, portfolioTimelineUpdated, diff --git a/components/brave_wallet_ui/common/async/handlers.ts b/components/brave_wallet_ui/common/async/handlers.ts index 3c00b9f2c0e3..6ba7cbe3074f 100644 --- a/components/brave_wallet_ui/common/async/handlers.ts +++ b/components/brave_wallet_ui/common/async/handlers.ts @@ -125,7 +125,7 @@ handler.on(WalletActions.keyringCreated.type, async (store) => { await refreshWalletInfo(store) }) -handler.on(WalletActions.keyringRestored.type, async (store) => { +handler.on(WalletActions.walletRestored.type, async (store) => { await refreshWalletInfo(store) }) diff --git a/components/brave_wallet_ui/common/slices/wallet.slice.ts b/components/brave_wallet_ui/common/slices/wallet.slice.ts index 7f9ca4e627ba..4d403a67efdd 100644 --- a/components/brave_wallet_ui/common/slices/wallet.slice.ts +++ b/components/brave_wallet_ui/common/slices/wallet.slice.ts @@ -170,7 +170,7 @@ export const WalletAsyncActions = { selectAccount: createAction('selectAccount'), // should use apiProxy - keyringService getAllNetworks: createAction('getAllNetworks'), // alias to refreshFullNetworkList keyringCreated: createAction('keyringCreated'), - keyringRestored: createAction('keyringRestored'), + walletRestored: createAction('walletRestored'), keyringReset: createAction('keyringReset'), locked: createAction('locked'), unlocked: createAction('unlocked'), diff --git a/components/brave_wallet_ui/common/wallet_api_proxy_observers.ts b/components/brave_wallet_ui/common/wallet_api_proxy_observers.ts index 7220ae21f125..d207d522cfe7 100644 --- a/components/brave_wallet_ui/common/wallet_api_proxy_observers.ts +++ b/components/brave_wallet_ui/common/wallet_api_proxy_observers.ts @@ -57,8 +57,8 @@ export function makeJsonRpcServiceObserver (store: Store) { keyringCreated: function () { store.dispatch(WalletActions.keyringCreated()) }, - keyringRestored: function () { - store.dispatch(WalletActions.keyringRestored()) + walletRestored: function () { + store.dispatch(WalletActions.walletRestored()) }, keyringReset: function () { store.dispatch(WalletActions.keyringReset())