Skip to content

Commit

Permalink
Account discovery to wallet service (#20324)
Browse files Browse the repository at this point in the history
* Run account discovery from wallet service

* Cleanup KeyringInfo
  • Loading branch information
supermassive authored Oct 2, 2023
1 parent 56b6164 commit 54bc1f7
Show file tree
Hide file tree
Showing 44 changed files with 545 additions and 847 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void keyringCreated(@KeyringId.EnumType int keyringId) {
}

@Override
public void keyringRestored(@KeyringId.EnumType int keyringId) {
public void walletRestored() {
update();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions browser/brave_wallet/asset_discovery_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@

namespace brave_wallet {

namespace {

const char kPasswordBrave[] = "brave";

} // namespace

class TestBraveWalletServiceObserverForAssetDiscoveryManager
: public brave_wallet::BraveWalletServiceObserverBase {
public:
Expand Down Expand Up @@ -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;
Expand Down
53 changes: 10 additions & 43 deletions browser/brave_wallet/brave_wallet_event_emitter_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"));
Expand Down
46 changes: 12 additions & 34 deletions browser/brave_wallet/brave_wallet_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -687,27 +687,14 @@ class BraveWalletServiceUnitTest : public testing::Test {
run_loop.Run();
}

void CheckAddresses(const std::vector<std::string>& 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<std::string> GetAddresses() {
std::vector<std::string> 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,
Expand Down Expand Up @@ -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);
Expand All @@ -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<std::string> 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 =
Expand All @@ -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<std::string> 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";
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 3 additions & 11 deletions browser/brave_wallet/brave_wallet_sign_message_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 9 additions & 10 deletions browser/brave_wallet/eth_allowance_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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));
Expand Down
14 changes: 3 additions & 11 deletions browser/brave_wallet/ethereum_provider_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 9 additions & 20 deletions browser/brave_wallet/ethereum_provider_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -735,16 +729,11 @@ class EthereumProviderImplUnitTest : public testing::Test {

std::vector<std::string> GetAddresses() {
std::vector<std::string> 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;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 54bc1f7

Please sign in to comment.