Skip to content

Commit

Permalink
Add setting to select web3 provider
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Jan 24, 2020
1 parent 4992bc1 commit d2ad4f5
Show file tree
Hide file tree
Showing 50 changed files with 1,058 additions and 125 deletions.
18 changes: 15 additions & 3 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,20 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_WEBTORRENT_ENABLED_DESC" desc="The description for WebTorrent switch in settings">
Uses WebTorrent to display torrents directly in the browser. Supports torrent files and magnet links.
</message>
<message name="IDS_SETTINGS_BRAVE_WALLET_ENABLED_DESC" desc="The description for Brave Wallet switch in settings">
Provides an Ethereum wallet and Dapp browser inside Brave.
<message name="IDS_BRAVE_WALLET_WEB3_PROVIDER_ASK" desc="Select control value for which web3 provider to use">
Ask
</message>
<message name="IDS_BRAVE_WALLET_WEB3_PROVIDER_NONE" desc="Select control value for which web3 provider to use">
None
</message>
<message name="IDS_BRAVE_WALLET_WEB3_PROVIDER_CRYPTO_WALLETS" desc="Select control value for which web3 provider to use">
Crypto Wallets
</message>
<message name="IDS_BRAVE_WALLET_WEB3_PROVIDER_METAMASK" desc="Select control value for which web3 provider to use">
MetaMask
</message>
<message name="IDS_SETTINGS_BRAVE_WEB3_PROVIDER_DESC" desc="The description for Brave web3 provider select control in settings">
Web3 provider for using Dapps
</message>
<message name="IDS_SETTINGS_HANGOUTS_ENABLED_DESC" desc="The description for Hangouts switch in settings">
Uses Hangouts component to enable screen sharing and other features in the browser.
Expand Down Expand Up @@ -658,7 +670,7 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
</message>
<!-- Web3 detection -->
<message name="IDS_BRAVE_CRYPTO_WALLETS_METAMASK_INFOBAR_TEXT" desc="Text that shows on the info bar when a Dapp is detected">
Crypto Wallets is now available in Brave. You can't use MetaMask and Crypto Wallets at the same time.
Would you like to use Brave's Crypto Wallets or MetaMask as your Dapp provider?
</message>
<message name="IDS_BRAVE_CRYPTO_WALLETS_INFOBAR_TEXT" desc="Text that shows on the info bar when a Dapp is detected">
Set up Crypto Wallets to interact with this app and others like it.
Expand Down
1 change: 1 addition & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ source_set("browser_process") {
if (brave_wallet_enabled) {
deps += [
"//brave/components/brave_wallet/browser",
"brave_wallet",
]
}

Expand Down
13 changes: 5 additions & 8 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,11 @@ bool BraveContentBrowserClient::HandleURLOverrideRewrite(GURL* url,
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
if (url->SchemeIs(content::kChromeUIScheme) &&
url->host() == ethereum_remote_client_host) {
Profile* profile = Profile::FromBrowserContext(browser_context);
if (profile->GetPrefs()->GetBoolean(kBraveWalletEnabled)) {
auto* registry = extensions::ExtensionRegistry::Get(browser_context);
if (registry->ready_extensions().GetByID(
ethereum_remote_client_extension_id)) {
*url = GURL(ethereum_remote_client_base_url);
return true;
}
auto* registry = extensions::ExtensionRegistry::Get(browser_context);
if (registry->ready_extensions().GetByID(
ethereum_remote_client_extension_id)) {
*url = GURL(ethereum_remote_client_base_url);
return true;
}
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "brave/browser/brave_profile_prefs.h"

#include "brave/browser/themes/brave_dark_mode_utils.h"
#include "brave/common/brave_wallet_constants.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_sync/brave_sync_prefs.h"
Expand Down Expand Up @@ -188,7 +189,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
// Brave Wallet
registry->RegisterStringPref(kBraveWalletAES256GCMSivNonce, "");
registry->RegisterStringPref(kBraveWalletEncryptedSeed, "");
registry->RegisterBooleanPref(kBraveWalletEnabled, true);
registry->RegisterIntegerPref(kBraveWalletWeb3Provider,
static_cast<int>(BraveWalletWeb3ProviderTypes::ASK));

// Autocomplete in address bar
registry->RegisterBooleanPref(kAutocompleteEnabled, true);
Expand Down
6 changes: 4 additions & 2 deletions browser/brave_profile_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/common/pref_names.h"
#include "brave/components/brave_wayback_machine/buildflags.h"
#include "brave/common/brave_wallet_constants.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
Expand Down Expand Up @@ -60,8 +61,9 @@ IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, MiscBravePrefs) {
browser()->profile()->GetPrefs()->GetBoolean(kHideBraveRewardsButton));
EXPECT_FALSE(
browser()->profile()->GetPrefs()->GetBoolean(kIPFSCompanionEnabled));
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(kBraveWalletEnabled));
EXPECT_EQ(
browser()->profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider),
static_cast<int>(BraveWalletWeb3ProviderTypes::ASK));

#if !BUILDFLAG(USE_GCM_FROM_PLATFORM)
EXPECT_FALSE(
Expand Down
16 changes: 16 additions & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import("//brave/components/brave_wallet/browser/buildflags/buildflags.gni")

assert(brave_wallet_enabled)

source_set("brave_wallet") {
sources = [
"brave_wallet_service_factory.h",
"brave_wallet_service_factory.cc",
"brave_wallet_utils.cc",
"brave_wallet_utils.h",
]
deps = [
"//components/prefs",
"//extensions/browser",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* 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/components/brave_wallet/browser/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"

#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
Expand Down Expand Up @@ -32,6 +32,7 @@ BraveWalletServiceFactory::~BraveWalletServiceFactory() {

KeyedService* BraveWalletServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
LOG(ERROR) << "-=====BUILD SERBVICE INSTANCE FOR wallet";
return new BraveWalletService(Profile::FromBrowserContext(context));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* 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_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_FACTORY_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_FACTORY_H_
#ifndef BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_

#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
Expand All @@ -31,4 +31,4 @@ class BraveWalletServiceFactory : public BrowserContextKeyedServiceFactory {
DISALLOW_COPY_AND_ASSIGN(BraveWalletServiceFactory);
};

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_FACTORY_H_
#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_
56 changes: 56 additions & 0 deletions browser/brave_wallet/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Copyright (c) 2019 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/widevine/widevine_utils.h"

#include "brave/common/brave_wallet_constants.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_registry_simple.h"
#include "extensions/browser/extension_prefs.h"

namespace brave_wallet {

void MigrateBraveWalletPrefs(Profile* profile) {
LOG(ERROR) << "===checking if should do migration of prefs";
PrefService* prefs = profile->GetPrefs();
if (!prefs->HasPrefPath(kBraveWalletEnabledDeprecated)) {
LOG(ERROR) << "===Skipping brave wallet enabled check because it doesn't exist";
return;
}
LOG(ERROR) << "====proceeding with brave wallet enabled migration";
bool wallet_enabled = prefs->GetBoolean(kBraveWalletEnabledDeprecated);
bool has_crypto_wallets = extensions::ExtensionPrefs::Get(profile)->
HasPrefForExtension(ethereum_remote_client_extension_id);
bool has_metamask = extensions::ExtensionPrefs::Get(profile)->
HasPrefForExtension(metamask_extension_id);

BraveWalletWeb3ProviderTypes provider =
BraveWalletWeb3ProviderTypes::ASK;
if (!wallet_enabled && has_metamask) {
// If Crypto Wallets was disabled and MetaMask is installed, set to MetaMask
provider = BraveWalletWeb3ProviderTypes::METAMASK;
} else if (!wallet_enabled && !has_metamask) {
// If Crypto Wallets is diabled, and MetaMask not installed, set None
provider = BraveWalletWeb3ProviderTypes::NONE;
} else if (wallet_enabled && has_metamask) {
// If Crypto Wallets is enabled, and MetaMask is installed, set
// to Crypto Wallets
provider = BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS;
} else if (has_crypto_wallets && wallet_enabled) {
// If CryptoWallets is enabled and installed, but MetaMask is not
// installed, set Crypto Wallets.
provider = BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS;
} else if (!has_crypto_wallets && wallet_enabled) {
// If CryptoWallets is enabled and not installed yet, and MetaMask is not
// installed, set Ask
provider = BraveWalletWeb3ProviderTypes::ASK;
}
prefs->SetInteger(kBraveWalletWeb3Provider, static_cast<int>(provider));
prefs->ClearPref(kBraveWalletEnabledDeprecated);
}

} // brave_wallet
17 changes: 17 additions & 0 deletions browser/brave_wallet/brave_wallet_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (c) 2019 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_UTILS_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_

class Profile;

namespace brave_wallet {

void MigrateBraveWalletPrefs(Profile* profile);

} // namespace brave_wallet

#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_
132 changes: 132 additions & 0 deletions browser/brave_wallet/brave_wallet_utils_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* Copyright (c) 2019 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_utils.h"

#include "brave/common/brave_wallet_constants.h"
#include "brave/common/pref_names.h"
#include "brave/common/extensions/extension_constants.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension_builder.h"
#include "testing/gtest/include/gtest/gtest.h"

class BraveWalletUtilsUnitTest : public testing::Test {
public:
void SetUp() override {
profile_ = CreateProfile();
}

Profile* profile() {
return profile_.get();
}

void AddCryptoWallets() {
AddExtension(ethereum_remote_client_extension_id, crypto_wallets_extension);
}

void AddMetaMask() {
AddExtension(metamask_extension_id, metamask_extension);
}

private:
void AddExtension(const std::string& extension_id,
scoped_refptr<const extensions::Extension>& extension) {
extensions::DictionaryBuilder manifest;
manifest.Set("name", "ext")
.Set("version", "0.1")
.Set("manifest_version", 2);
extension = extensions::ExtensionBuilder()
.SetManifest(manifest.Build())
.SetID(extension_id)
.Build();
ASSERT_TRUE(extension);
extensions::ExtensionPrefs::Get(profile())->UpdateExtensionPref(extension_id,
"test", std::make_unique<base::Value>(""));
extensions::ExtensionRegistry::Get(profile())->AddEnabled(extension.get());
}

std::unique_ptr<TestingProfile> CreateProfile() {
TestingProfile::Builder builder;
auto prefs =
std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
RegisterUserProfilePrefs(prefs->registry());
prefs->registry()->RegisterBooleanPref(kBraveWalletEnabledDeprecated, true);
builder.SetPrefService(std::move(prefs));
return builder.Build();
}

content::BrowserTaskEnvironment task_environment_;
std::unique_ptr<TestingProfile> profile_;
scoped_refptr<const extensions::Extension> metamask_extension;
scoped_refptr<const extensions::Extension> crypto_wallets_extension;
};

// If Crypto Wallets was disabled and MetaMask is installed, set to MetaMask
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationMMCryptoWalletsDisabled) {
AddMetaMask();
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, false);

brave_wallet::MigrateBraveWalletPrefs(profile());

auto provider = static_cast<BraveWalletWeb3ProviderTypes>(
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider));
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::METAMASK);
}

// If Crypto Wallets is diabled, and MetaMask not installed, set None
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationCryptoWalletsDisabled) {
AddCryptoWallets();
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, false);

brave_wallet::MigrateBraveWalletPrefs(profile());

auto provider = static_cast<BraveWalletWeb3ProviderTypes>(
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider));
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::NONE);
}

// If Crypto Wallets is enabled, and MetaMask is installed, set
// to Crypto Wallets
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationCryptoWalletsAndMMInstalled) {
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, true);
AddCryptoWallets();
AddMetaMask();

brave_wallet::MigrateBraveWalletPrefs(profile());

auto provider = static_cast<BraveWalletWeb3ProviderTypes>(
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider));
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS);
}

// If CryptoWallets is enabled and installed, but MetaMask is not
// installed, set Crypto Wallets.
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationCryptoWalletsInstalled) {
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, true);
AddCryptoWallets();

brave_wallet::MigrateBraveWalletPrefs(profile());

auto provider = static_cast<BraveWalletWeb3ProviderTypes>(
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider));
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS);
}

// If CryptoWallets is enabled and not installed yet, and MetaMask is not
// installed, set Ask
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationNothingInstalled) {
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, true);

brave_wallet::MigrateBraveWalletPrefs(profile());

auto provider = static_cast<BraveWalletWeb3ProviderTypes>(
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider));
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::ASK);
}
9 changes: 9 additions & 0 deletions browser/browser_context_keyed_service_factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "brave/components/brave_ads/browser/ads_service_factory.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/greaselion/browser/buildflags/buildflags.h"
#include "brave/components/brave_wallet/browser/buildflags/buildflags.h"

#if BUILDFLAG(ENABLE_GREASELION)
#include "brave/browser/greaselion/greaselion_service_factory.h"
Expand All @@ -21,6 +22,10 @@
#include "brave/browser/ui/bookmark/bookmark_prefs_service_factory.h"
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#endif

namespace brave {

void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
Expand All @@ -37,6 +42,10 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
#if !defined(OS_ANDROID)
BookmarkPrefsServiceFactory::GetInstance();
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
BraveWalletServiceFactory::GetInstance();
#endif
}

} // namespace brave
Loading

0 comments on commit d2ad4f5

Please sign in to comment.