Skip to content

Commit

Permalink
Merge pull request #11669 from brave/reset-wallet
Browse files Browse the repository at this point in the history
Various reset wallet fixes
  • Loading branch information
bbondy authored Dec 28, 2021
2 parents c1721b6 + 67a7860 commit be24026
Show file tree
Hide file tree
Showing 35 changed files with 365 additions and 78 deletions.
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/util/TokenUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/util/Validations.java",
"../../brave/android/java/org/chromium/chrome/browser/crypto_wallet/util/WalletNativeUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/custom_layout/HeightWrappingViewPager.java",
"../../brave/android/java/org/chromium/chrome/browser/custom_layout/NonSwipeableViewPager.java",
"../../brave/android/java/org/chromium/chrome/browser/custom_layout/VerticalViewPager.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.chromium.chrome.browser.crypto_wallet;

import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.brave_wallet.mojom.ErcTokenRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ default void keyringCreated() {}
@Override
default void keyringRestored() {}

@Override
default void keyringReset() {}

@Override
default void locked() {}

Expand Down
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/. */

package org.chromium.chrome.browser.crypto_wallet.util;

import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;

@JNINamespace("chrome::android")
public class WalletNativeUtils {
public static void resetWallet() {
WalletNativeUtilsJni.get().resetWallet();
}

@NativeMethods
interface Natives {
void resetWallet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,22 @@

import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.KeyringController;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.crypto_wallet.KeyringControllerFactory;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.system.MojoException;
import org.chromium.chrome.browser.crypto_wallet.util.WalletNativeUtils;
import org.chromium.ui.KeyboardVisibilityDelegate;

/**
* The preference used to reset Brave Wallet.
*/
public class BraveWalletResetPreference
extends Preference implements Preference.OnPreferenceClickListener, ConnectionErrorHandler {
extends Preference implements Preference.OnPreferenceClickListener {
private String TAG = "BraveWalletResetPreference";

private int mPrefAccentColor;
private final String mConfirmationPhrase;
private KeyringController mKeyringController;

/**
* Constructor for BraveWalletResetPreference.
Expand All @@ -58,8 +54,6 @@ public BraveWalletResetPreference(Context context, AttributeSet attrs) {
mConfirmationPhrase =
resources.getString(R.string.brave_wallet_reset_settings_confirmation_phrase);
setOnPreferenceClickListener(this);

InitKeyringController();
}

@Override
Expand All @@ -78,9 +72,6 @@ public boolean onPreferenceClick(Preference preference) {
@Override
public void onDetached() {
super.onDetached();
if (mKeyringController != null) {
mKeyringController.close();
}
}

private void showBraveWalletResetDialog() {
Expand All @@ -97,20 +88,15 @@ private void showBraveWalletResetDialog() {
@Override
public void onClick(DialogInterface dialog, int button) {
if (button == AlertDialog.BUTTON_POSITIVE) {
if (mKeyringController != null) {
String inputText = input.getText().toString().trim();
if (TextUtils.equals(inputText, mConfirmationPhrase)) {
Log.w(TAG, "Reset");
mKeyringController.reset();
Utils.setCryptoOnboarding(true);
}
mKeyringController.close();

// Force clear activity stack
launchBraveTabbedActivity();
} else {
Log.w(TAG, "mKeyringController is null");
String inputText = input.getText().toString().trim();
if (TextUtils.equals(inputText, mConfirmationPhrase)) {
Log.w(TAG, "Reset");
WalletNativeUtils.resetWallet();
Utils.setCryptoOnboarding(true);
}

// Force clear activity stack
launchBraveTabbedActivity();
} else {
dialog.dismiss();
}
Expand Down Expand Up @@ -153,21 +139,6 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
});
}

@Override
public void onConnectionError(MojoException e) {
mKeyringController.close();
mKeyringController = null;
InitKeyringController();
}

private void InitKeyringController() {
if (mKeyringController != null) {
return;
}

mKeyringController = KeyringControllerFactory.getInstance().getKeyringController(this);
}

private void launchBraveTabbedActivity() {
Intent intent =
new Intent(BraveActivity.getChromeTabbedActivity(), ChromeTabbedActivity.class);
Expand Down
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
1 change: 1 addition & 0 deletions browser/brave_wallet/android/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ brave_browser_brave_wallet_android_sources = [
"//brave/browser/brave_wallet/android/eth_tx_controller_factory_android.cc",
"//brave/browser/brave_wallet/android/keyring_controller_factory_android.cc",
"//brave/browser/brave_wallet/android/swap_controller_factory_android.cc",
"//brave/browser/brave_wallet/android/wallet_native_utils_android.cc",
]
24 changes: 24 additions & 0 deletions browser/brave_wallet/android/wallet_native_utils_android.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* 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 "base/android/jni_android.h"
#include "base/logging.h"
#include "brave/browser/brave_wallet/brave_wallet_reset.h"
#include "brave/build/android/jni_headers/WalletNativeUtils_jni.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"

namespace chrome {
namespace android {

static void JNI_WalletNativeUtils_ResetWallet(JNIEnv* env) {
auto* profile = ProfileManager::GetActiveUserProfile();
DCHECK(profile);

brave_wallet::ResetWallet(profile);
}

} // namespace android
} // namespace chrome
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
16 changes: 12 additions & 4 deletions browser/brave_wallet/keyring_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 @@ -73,6 +74,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 @@ -83,12 +85,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 @@ -850,6 +854,9 @@ TEST_F(KeyringControllerUnitTest, LockAndUnlock) {
TEST_F(KeyringControllerUnitTest, Reset) {
KeyringController controller(GetPrefs());
HDKeyring* keyring = controller.CreateKeyring(kDefaultKeyringId, "brave");
TestKeyringControllerObserver observer;
controller.AddObserver(observer.GetReceiver());

keyring->AddAccounts();
// Trigger account number saving
controller.Lock();
Expand All @@ -861,17 +868,18 @@ 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));
EXPECT_TRUE(controller.IsKeyringCreated(kDefaultKeyringId));
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.IsKeyringCreated(kDefaultKeyringId));
// 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
1 change: 1 addition & 0 deletions build/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ generate_jni("jni_headers") {
"//brave/android/java/org/chromium/chrome/browser/crypto_wallet/EthTxControllerFactory.java",
"//brave/android/java/org/chromium/chrome/browser/crypto_wallet/KeyringControllerFactory.java",
"//brave/android/java/org/chromium/chrome/browser/crypto_wallet/SwapControllerFactory.java",
"//brave/android/java/org/chromium/chrome/browser/crypto_wallet/util/WalletNativeUtils.java",
"//brave/android/java/org/chromium/chrome/browser/notifications/BraveNotificationPlatformBridge.java",
"//brave/android/java/org/chromium/chrome/browser/notifications/BraveNotificationSettingsBridge.java",
"//brave/android/java/org/chromium/chrome/browser/ntp_background_images/NTPBackgroundImagesBridge.java",
Expand Down
Loading

0 comments on commit be24026

Please sign in to comment.