Skip to content

Commit

Permalink
Various reset wallet fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Dec 22, 2021
1 parent 208fd94 commit 3820bc8
Show file tree
Hide file tree
Showing 27 changed files with 301 additions and 38 deletions.
3 changes: 3 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,9 @@ Are you sure you want to do this?
<message name="IDS_SETTINGS_WALLET_RESET_CONFIRMATION" desc="Reset wallet settings confirmation message text">
Are you sure you want to reset your wallet? If your wallet is not backed up, resetting will cause you to lose all account data (including any associated funds). Type "<ph name="CONFIRMATION_PHRASE">$1<ex>Yes</ex></ph>" to confirm.
</message>
<message name="IDS_SETTINGS_WALLET_RESET_CONFIRMED" desc="Reset wallet settings confirmed message text">
Your wallet was reset.
</message>
<message name="IDS_SETTINGS_WALLET_RESET_CONFIRMATION_PHRASE" desc="The phrase users should type to reset wallet">
Yes
</message>
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ source_set("brave_wallet") {
"asset_ratio_controller_factory.h",
"brave_wallet_context_utils.cc",
"brave_wallet_context_utils.h",
"brave_wallet_reset.cc",
"brave_wallet_reset.h",
"brave_wallet_service_factory.cc",
"brave_wallet_service_factory.h",
"erc_token_images_source.cc",
Expand Down
36 changes: 36 additions & 0 deletions browser/brave_wallet/brave_wallet_reset.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_wallet/brave_wallet_reset.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/eth_tx_controller_factory.h"
#include "brave/browser/brave_wallet/keyring_controller_factory.h"
#include "brave/browser/brave_wallet/rpc_controller_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/eth_json_rpc_controller.h"
#include "brave/components/brave_wallet/browser/eth_tx_controller.h"
#include "brave/components/brave_wallet/browser/keyring_controller.h"

namespace brave_wallet {

void ResetWallet(content::BrowserContext* context) {
auto* eth_tx_controller =
brave_wallet::EthTxControllerFactory::GetControllerForContext(context);
eth_tx_controller->Reset();

auto* rpc_controller =
brave_wallet::RpcControllerFactory::GetControllerForContext(context);
rpc_controller->Reset();

auto* brave_wallet_service =
brave_wallet::BraveWalletServiceFactory::GetServiceForContext(context);
brave_wallet_service->Reset();

auto* keyring_controller =
brave_wallet::KeyringControllerFactory::GetControllerForContext(context);
keyring_controller->Reset();
}

} // namespace brave_wallet
21 changes: 21 additions & 0 deletions browser/brave_wallet/brave_wallet_reset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_RESET_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_RESET_H_

#include "content/public/browser/browser_context.h"

namespace content {
class BrowserContext;
} // namespace content

namespace brave_wallet {

void ResetWallet(content::BrowserContext* context);

} // namespace brave_wallet

#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_RESET_H_
36 changes: 36 additions & 0 deletions browser/brave_wallet/brave_wallet_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1525,4 +1525,40 @@ TEST_F(BraveWalletServiceUnitTest, GetUserAsset) {
mojom::kRopstenChainId));
}

TEST_F(BraveWalletServiceUnitTest, Reset) {
SetDefaultBaseCurrency("CAD");
SetDefaultBaseCryptocurrency("ETH");
mojom::ERCTokenPtr token1 = GetToken1();
bool callback_called;
bool success;
AddUserAsset(token1.Clone(), "0x1", &callback_called, &success);
EXPECT_TRUE(callback_called);
EXPECT_TRUE(success);
EXPECT_TRUE(GetPrefs()->HasPrefPath(kBraveWalletUserAssets));
EXPECT_TRUE(GetPrefs()->HasPrefPath(kDefaultBaseCurrency));
EXPECT_TRUE(GetPrefs()->HasPrefPath(kDefaultBaseCryptocurrency));
std::string address = "0xbe862ad9abfe6f22bcb087716c7d89a26051f74c";
std::string message = "0xAB";
auto request1 = mojom::SignMessageRequest::New(
1, address, std::string(message.begin(), message.end()));
service_->AddSignMessageRequest(
std::move(request1),
base::BindLambdaForTesting(
[](bool, const std::string&, const std::string&) {}));
mojom::ERCTokenPtr custom_token =
mojom::ERCToken::New("0x6b175474e89094C44Da98b954eEdeAC495271d1e",
"COLOR", "", true, false, "COLOR", 18, true, "");
AddSuggestToken(custom_token.Clone(), custom_token.Clone(), true);

service_->Reset();

EXPECT_FALSE(GetPrefs()->HasPrefPath(kBraveWalletUserAssets));
EXPECT_FALSE(GetPrefs()->HasPrefPath(kDefaultBaseCurrency));
EXPECT_FALSE(GetPrefs()->HasPrefPath(kDefaultBaseCryptocurrency));
EXPECT_TRUE(service_->sign_message_requests_.empty());
EXPECT_TRUE(service_->sign_message_callbacks_.empty());
EXPECT_TRUE(service_->add_suggest_token_callbacks_.empty());
EXPECT_TRUE(service_->add_suggest_token_requests_.empty());
}

} // namespace brave_wallet
15 changes: 11 additions & 4 deletions browser/brave_wallet/keyring_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class TestKeyringControllerObserver
// TODO(bbondy): We should be testing all of these observer events
void KeyringCreated() override {}
void KeyringRestored() override {}
void KeyringReset() override { keyringResetFired_ = true; }
void Locked() override {}
void Unlocked() override {}
void BackedUp() override {}
Expand All @@ -70,6 +71,7 @@ class TestKeyringControllerObserver
bool AutoLockMinutesChangedFired() { return autoLockMinutesChangedFired_; }
bool SelectedAccountChangedFired() { return selectedAccountChangedFired_; }
bool AccountsChangedFired() { return accountsChangedFired_; }
bool KeyringResetFired() { return keyringResetFired_; }

mojo::PendingRemote<brave_wallet::mojom::KeyringControllerObserver>
GetReceiver() {
Expand All @@ -80,12 +82,14 @@ class TestKeyringControllerObserver
autoLockMinutesChangedFired_ = false;
selectedAccountChangedFired_ = false;
accountsChangedFired_ = false;
keyringResetFired_ = false;
}

private:
bool autoLockMinutesChangedFired_ = false;
bool selectedAccountChangedFired_ = false;
bool accountsChangedFired_ = false;
bool keyringResetFired_ = false;
mojo::Receiver<brave_wallet::mojom::KeyringControllerObserver>
observer_receiver_{this};
};
Expand Down Expand Up @@ -814,6 +818,9 @@ TEST_F(KeyringControllerUnitTest, LockAndUnlock) {

TEST_F(KeyringControllerUnitTest, Reset) {
KeyringController controller(GetPrefs());
TestKeyringControllerObserver observer;
controller.AddObserver(observer.GetReceiver());

HDKeyring* keyring = controller.CreateDefaultKeyring("brave");
keyring->AddAccounts();
// Trigger account number saving
Expand All @@ -826,17 +833,17 @@ TEST_F(KeyringControllerUnitTest, Reset) {
GetPrefs()->Set(kBraveWalletCustomNetworks, base::ListValue());
GetPrefs()->SetString(kBraveWalletCurrentChainId,
brave_wallet::mojom::kMainnetChainId);
EXPECT_TRUE(GetPrefs()->HasPrefPath(kBraveWalletCustomNetworks));
EXPECT_TRUE(GetPrefs()->HasPrefPath(kBraveWalletCurrentChainId));
controller.Reset();
EXPECT_FALSE(HasPrefForKeyring(kPasswordEncryptorSalt, "default"));
EXPECT_FALSE(HasPrefForKeyring(kPasswordEncryptorNonce, "default"));
EXPECT_FALSE(HasPrefForKeyring(kEncryptedMnemonic, "default"));
EXPECT_FALSE(GetPrefs()->HasPrefPath(kBraveWalletKeyrings));
EXPECT_FALSE(GetPrefs()->HasPrefPath(kBraveWalletCustomNetworks));
EXPECT_FALSE(GetPrefs()->HasPrefPath(kBraveWalletCurrentChainId));
EXPECT_EQ(controller.default_keyring_, nullptr);
EXPECT_EQ(controller.encryptor_, nullptr);
EXPECT_FALSE(controller.IsDefaultKeyringCreated());
// Keyring observer fire
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(observer.KeyringResetFired());
}

TEST_F(KeyringControllerUnitTest, BackupComplete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class SettingsBraveWalletPage extends SettingsBraveWalletPageBase {
if (window.prompt(message) !== this.i18n('walletResetConfirmationPhrase'))
return
this.browserProxy_.resetWallet()
window.alert(this.i18n('walletResetConfirmed'))
}
}

Expand Down
7 changes: 2 additions & 5 deletions browser/ui/webui/settings/brave_default_extensions_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "brave/browser/brave_wallet/keyring_controller_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_reset.h"
#include "brave/browser/extensions/brave_component_loader.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_wallet/browser/keyring_controller.h"
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h"
#include "brave/components/decentralized_dns/buildflags/buildflags.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
Expand Down Expand Up @@ -207,9 +206,7 @@ void BraveDefaultExtensionsHandler::GetRestartNeeded(

void BraveDefaultExtensionsHandler::ResetWallet(
base::Value::ConstListView args) {
auto* keyring_controller =
brave_wallet::KeyringControllerFactory::GetControllerForContext(profile_);
keyring_controller->Reset();
brave_wallet::ResetWallet(profile_);
}

void BraveDefaultExtensionsHandler::SetWebTorrentEnabled(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
{"ipfsKeyExportError", IDS_SETTINGS_IPNS_KEYS_EXPORT_ERROR},
{"resetWallet", IDS_SETTINGS_WALLET_RESET},
{"walletResetConfirmation", IDS_SETTINGS_WALLET_RESET_CONFIRMATION},
{"walletResetConfirmed", IDS_SETTINGS_WALLET_RESET_CONFIRMED},
{"walletNetworksLinkTitle", IDS_SETTINGS_WALLET_NETWORKS_ITEM},
{"walletAddNetworkDialogTitle", IDS_SETTINGS_WALLET_ADD_NETWORK_TITLE},
{"walletAddNetworkInvalidURLInput",
Expand Down
20 changes: 16 additions & 4 deletions components/brave_wallet/browser/brave_wallet_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,28 @@ void RegisterProfilePrefsForMigration(
static_cast<int>(mojom::DefaultWallet::BraveWalletPreferExtension));
}

void ClearProfilePrefs(PrefService* prefs) {
void ClearEthJsonRpcControllerProfilePrefs(PrefService* prefs) {
DCHECK(prefs);
prefs->ClearPref(kBraveWalletCustomNetworks);
prefs->ClearPref(kBraveWalletCurrentChainId);
prefs->ClearPref(kBraveWalletTransactions);
prefs->ClearPref(kBraveWalletUserAssets);
prefs->ClearPref(kSupportEip1559OnLocalhostChain);
}

void ClearKeyringControllerProfilePrefs(PrefService* prefs) {
DCHECK(prefs);
prefs->ClearPref(kBraveWalletKeyrings);
prefs->ClearPref(kBraveWalletAutoLockMinutes);
prefs->ClearPref(kBraveWalletSelectedAccount);
prefs->ClearPref(kSupportEip1559OnLocalhostChain);
}

void ClearEthTxControllerProfilePrefs(PrefService* prefs) {
DCHECK(prefs);
prefs->ClearPref(kBraveWalletTransactions);
}

void ClearBraveWalletServicePrefs(PrefService* prefs) {
DCHECK(prefs);
prefs->ClearPref(kBraveWalletUserAssets);
prefs->ClearPref(kDefaultBaseCurrency);
prefs->ClearPref(kDefaultBaseCryptocurrency);
}
Expand Down
5 changes: 4 additions & 1 deletion components/brave_wallet/browser/brave_wallet_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ namespace brave_wallet {
void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
void RegisterProfilePrefsForMigration(
user_prefs::PrefRegistrySyncable* registry);
void ClearProfilePrefs(PrefService* prefs);
void ClearEthJsonRpcControllerProfilePrefs(PrefService* prefs);
void ClearKeyringControllerProfilePrefs(PrefService* prefs);
void ClearEthTxControllerProfilePrefs(PrefService* prefs);
void ClearBraveWalletServicePrefs(PrefService* prefs);
void MigrateObsoleteProfilePrefs(PrefService* prefs);

} // namespace brave_wallet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class BraveWalletProviderImpl final
// KeyringControllerObserver
void KeyringCreated() override {}
void KeyringRestored() override {}
void KeyringReset() override {}
void Locked() override;
void Unlocked() override;
void BackedUp() override {}
Expand Down
22 changes: 21 additions & 1 deletion components/brave_wallet/browser/brave_wallet_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/strings/string_util.h"
#include "base/values.h"
#include "brave/components/brave_stats/browser/brave_stats_updater_util.h"
#include "brave/components/brave_wallet/browser/brave_wallet_prefs.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/erc_token_registry.h"
#include "brave/components/brave_wallet/browser/keyring_controller.h"
Expand Down Expand Up @@ -755,7 +756,7 @@ void BraveWalletService::NotifyAddSuggestTokenRequestsProcessed(
}
}

void BraveWalletService::OnNetworkChanged() {
void BraveWalletService::CancelAllSuggestedTokenCallbacks() {
add_suggest_token_requests_.clear();
// Reject pending suggest token requests when network changed.
for (auto& callback : add_suggest_token_callbacks_)
Expand All @@ -765,4 +766,23 @@ void BraveWalletService::OnNetworkChanged() {
add_suggest_token_callbacks_.clear();
}

void BraveWalletService::CancelAllSignMessageCallbacks() {
while (!sign_message_requests_.empty()) {
auto callback = std::move(sign_message_callbacks_.front());
sign_message_requests_.pop_front();
sign_message_callbacks_.pop_front();
std::move(callback).Run(false, std::string(), std::string());
}
}

void BraveWalletService::OnNetworkChanged() {
CancelAllSuggestedTokenCallbacks();
}

void BraveWalletService::Reset() {
ClearBraveWalletServicePrefs(prefs_);
CancelAllSuggestedTokenCallbacks();
CancelAllSignMessageCallbacks();
}

} // namespace brave_wallet
7 changes: 7 additions & 0 deletions components/brave_wallet/browser/brave_wallet_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class BraveWalletService : public KeyedService,
// BraveWalletServiceDelegate::Observer:
void OnActiveOriginChanged(const std::string& origin) override;

// Resets things back to the original state of BraveWalletService.
// To be used when the Wallet is reset / erased
void Reset() override;

void RecordWalletUsage(base::Time wallet_last_used);

void AddSignMessageRequest(mojom::SignMessageRequestPtr request,
Expand All @@ -134,6 +138,7 @@ class BraveWalletService : public KeyedService,
FRIEND_TEST_ALL_PREFIXES(BraveWalletServiceUnitTest, AddSuggestToken);
FRIEND_TEST_ALL_PREFIXES(BraveWalletServiceUnitTest, GetUserAsset);
FRIEND_TEST_ALL_PREFIXES(BraveWalletServiceUnitTest, ImportFromMetaMask);
FRIEND_TEST_ALL_PREFIXES(BraveWalletServiceUnitTest, Reset);

void OnDefaultWalletChanged();
void OnDefaultBaseCurrencyChanged();
Expand Down Expand Up @@ -164,6 +169,8 @@ class BraveWalletService : public KeyedService,
bool is_erc721,
const std::string& chain_id);
void OnNetworkChanged();
void CancelAllSuggestedTokenCallbacks();
void CancelAllSignMessageCallbacks();

base::circular_deque<mojom::SignMessageRequestPtr> sign_message_requests_;
base::circular_deque<SignMessageRequestCallback> sign_message_callbacks_;
Expand Down
16 changes: 16 additions & 0 deletions components/brave_wallet/browser/eth_json_rpc_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/environment.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/components/brave_wallet/browser/brave_wallet_prefs.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/eth_data_builder.h"
#include "brave/components/brave_wallet/browser/eth_requests.h"
Expand Down Expand Up @@ -1066,4 +1067,19 @@ bool EthJsonRpcController::AddSwitchEthereumChainRequest(
return true;
}

void EthJsonRpcController::Reset() {
ClearEthJsonRpcControllerProfilePrefs(prefs_);
SetNetwork(prefs_->GetString(kBraveWalletCurrentChainId));

add_chain_pending_requests_.clear();
switch_chain_requests_.clear();
// Reject pending suggest token requests when network changed.
for (auto& callback : switch_chain_callbacks_) {
std::move(callback.second)
.Run(mojom::ProviderError::kUserRejectedRequest,
l10n_util::GetStringUTF8(IDS_WALLET_USER_REJECTED_REQUEST));
}
switch_chain_callbacks_.clear();
}

} // namespace brave_wallet
5 changes: 5 additions & 0 deletions components/brave_wallet/browser/eth_json_rpc_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ class EthJsonRpcController : public KeyedService,
const std::string& account_address,
GetERC721TokenBalanceCallback callback) override;

// Resets things back to the original state of BraveWalletService.
// To be used when the Wallet is reset / erased
void Reset() override;

using GetSupportsInterfaceCallback =
base::OnceCallback<void(bool success, bool is_supported)>;
void GetSupportsInterface(const std::string& contract_address,
Expand Down Expand Up @@ -303,6 +307,7 @@ class EthJsonRpcController : public KeyedService,
RequestCallback callback);

FRIEND_TEST_ALL_PREFIXES(EthJsonRpcControllerUnitTest, IsValidDomain);
FRIEND_TEST_ALL_PREFIXES(EthJsonRpcControllerUnitTest, Reset);
bool IsValidDomain(const std::string& domain);

void OnGetERC721OwnerOf(
Expand Down
Loading

0 comments on commit 3820bc8

Please sign in to comment.