Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ZCash account creation & balance resolution #20351

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ source_set("brave_wallet") {
"swap_service_factory.h",
"tx_service_factory.cc",
"tx_service_factory.h",
"zcash_wallet_service_factory.cc",
"zcash_wallet_service_factory.h",
]
if (enable_ipfs_local_node) {
sources += [
Expand Down
93 changes: 93 additions & 0 deletions browser/brave_wallet/zcash_wallet_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_wallet/zcash_wallet_service_factory.h"

#include <utility>

#include "base/no_destructor.h"
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/components/brave_wallet/browser/zcash/zcash_wallet_service.h"
#include "brave/components/brave_wallet/common/common_utils.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/storage_partition.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

namespace brave_wallet {

// static
ZCashWalletServiceFactory* ZCashWalletServiceFactory::GetInstance() {
static base::NoDestructor<ZCashWalletServiceFactory> instance;
return instance.get();
}

// static
mojo::PendingRemote<mojom::ZCashWalletService>
ZCashWalletServiceFactory::GetForContext(content::BrowserContext* context) {
if (!IsAllowedForContext(context)) {
return mojo::PendingRemote<mojom::ZCashWalletService>();
}

if (!IsZCashEnabled()) {
return mojo::PendingRemote<mojom::ZCashWalletService>();
}

return static_cast<ZCashWalletService*>(
GetInstance()->GetServiceForBrowserContext(context, true))
->MakeRemote();
}

// static
ZCashWalletService* ZCashWalletServiceFactory::GetServiceForContext(
content::BrowserContext* context) {
if (!IsAllowedForContext(context)) {
return nullptr;
}
if (!IsZCashEnabled()) {
return nullptr;
}
return static_cast<ZCashWalletService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}

// static
void ZCashWalletServiceFactory::BindForContext(
content::BrowserContext* context,
mojo::PendingReceiver<mojom::ZCashWalletService> receiver) {
auto* zcash_service =
ZCashWalletServiceFactory::GetServiceForContext(context);
if (zcash_service) {
zcash_service->Bind(std::move(receiver));
}
}

ZCashWalletServiceFactory::ZCashWalletServiceFactory()
: BrowserContextKeyedServiceFactory(
"ZCashWalletService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(KeyringServiceFactory::GetInstance());
}

ZCashWalletServiceFactory::~ZCashWalletServiceFactory() = default;

KeyedService* ZCashWalletServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
auto* default_storage_partition = context->GetDefaultStoragePartition();
auto shared_url_loader_factory =
default_storage_partition->GetURLLoaderFactoryForBrowserProcess();
return new ZCashWalletService(
KeyringServiceFactory::GetServiceForContext(context),
user_prefs::UserPrefs::Get(context), shared_url_loader_factory);
}

content::BrowserContext* ZCashWalletServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}

} // namespace brave_wallet
54 changes: 54 additions & 0 deletions browser/brave_wallet/zcash_wallet_service_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_WALLET_ZCASH_WALLET_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_BRAVE_WALLET_ZCASH_WALLET_SERVICE_FACTORY_H_

#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/browser_context.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

namespace base {
template <typename T>
class NoDestructor;
} // namespace base

namespace brave_wallet {

class ZCashWalletService;

class ZCashWalletServiceFactory : public BrowserContextKeyedServiceFactory {
public:
ZCashWalletServiceFactory(const ZCashWalletServiceFactory&) = delete;
ZCashWalletServiceFactory& operator=(const ZCashWalletServiceFactory&) =
delete;

static mojo::PendingRemote<mojom::ZCashWalletService> GetForContext(
content::BrowserContext* context);
static ZCashWalletService* GetServiceForContext(
content::BrowserContext* context);
static ZCashWalletServiceFactory* GetInstance();
static void BindForContext(
content::BrowserContext* context,
mojo::PendingReceiver<mojom::ZCashWalletService> receiver);

private:
friend base::NoDestructor<ZCashWalletServiceFactory>;

ZCashWalletServiceFactory();
~ZCashWalletServiceFactory() override;

KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
};

} // namespace brave_wallet

#endif // BRAVE_BROWSER_BRAVE_WALLET_ZCASH_WALLET_SERVICE_FACTORY_H_
10 changes: 10 additions & 0 deletions browser/browser_context_keyed_service_factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/browser/brave_shields/ad_block_pref_service_factory.h"
#include "brave/browser/brave_wallet/asset_ratio_service_factory.h"
#include "brave/browser/brave_wallet/bitcoin_wallet_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_ipfs_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
Expand All @@ -21,6 +22,7 @@
#include "brave/browser/brave_wallet/simulation_service_factory.h"
#include "brave/browser/brave_wallet/swap_service_factory.h"
#include "brave/browser/brave_wallet/tx_service_factory.h"
#include "brave/browser/brave_wallet/zcash_wallet_service_factory.h"
#include "brave/browser/debounce/debounce_service_factory.h"
#include "brave/browser/ephemeral_storage/ephemeral_storage_service_factory.h"
#include "brave/browser/ethereum_remote_client/buildflags/buildflags.h"
Expand All @@ -35,6 +37,7 @@
#include "brave/components/ai_chat/common/buildflags/buildflags.h"
#include "brave/components/brave_perf_predictor/browser/named_third_party_registry_factory.h"
#include "brave/components/brave_vpn/common/buildflags/buildflags.h"
#include "brave/components/brave_wallet/common/common_utils.h"
#include "brave/components/commander/common/buildflags/buildflags.h"
#include "brave/components/greaselion/browser/buildflags/buildflags.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
Expand Down Expand Up @@ -150,6 +153,13 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
brave_wallet::TxServiceFactory::GetInstance();
brave_wallet::BraveWalletServiceFactory::GetInstance();

if (brave_wallet::IsBitcoinEnabled()) {
brave_wallet::BitcoinWalletServiceFactory::GetInstance();
}
if (brave_wallet::IsZCashEnabled()) {
brave_wallet::ZCashWalletServiceFactory::GetInstance();
}
cypt4 marked this conversation as resolved.
Show resolved Hide resolved

#if !BUILDFLAG(IS_ANDROID)
if (base::FeatureList::IsEnabled(commands::features::kBraveCommands)) {
commands::AcceleratorServiceFactory::GetInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/browser/brave_wallet/swap_service_factory.h"
#include "brave/browser/brave_wallet/tx_service_factory.h"
#include "brave/browser/brave_wallet/zcash_wallet_service_factory.h"

#include "brave/components/brave_wallet/browser/blockchain_registry.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.h"
Expand Down Expand Up @@ -123,6 +124,8 @@ void AndroidWalletPageUI::CreatePageHandler(
json_rpc_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::BitcoinWalletService>
bitcoin_rpc_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::ZCashWalletService>
zcash_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::SwapService>
swap_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::AssetRatioService>
Expand Down Expand Up @@ -160,6 +163,8 @@ void AndroidWalletPageUI::CreatePageHandler(
profile, std::move(json_rpc_service_receiver));
brave_wallet::BitcoinWalletServiceFactory::BindForContext(
profile, std::move(bitcoin_rpc_service_receiver));
brave_wallet::ZCashWalletServiceFactory::BindForContext(
profile, std::move(zcash_service_receiver));
brave_wallet::SwapServiceFactory::BindForContext(
profile, std::move(swap_service_receiver));
brave_wallet::AssetRatioServiceFactory::BindForContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class AndroidWalletPageUI : public ui::MojoWebUIController,
json_rpc_service,
mojo::PendingReceiver<brave_wallet::mojom::BitcoinWalletService>
bitcoin_rpc_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::ZCashWalletService>
zcash_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::SwapService> swap_service,
mojo::PendingReceiver<brave_wallet::mojom::AssetRatioService>
asset_ratio_service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void WalletHandler::GetWalletInfo(GetWalletInfoCallback callback) {
std::move(callback).Run(mojom::WalletInfo::New(
default_keyring->is_keyring_created, default_keyring->is_locked,
keyring_service_->IsWalletBackedUpSync(), IsFilecoinEnabled(),
IsSolanaEnabled(), IsBitcoinEnabled(), IsNftPinningEnabled(),
IsPanelV2Enabled()));
IsSolanaEnabled(), IsBitcoinEnabled(), IsZCashEnabled(),
IsNftPinningEnabled(), IsPanelV2Enabled()));
}

} // namespace brave_wallet
5 changes: 5 additions & 0 deletions browser/ui/webui/brave_wallet/wallet_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/browser/brave_wallet/swap_service_factory.h"
#include "brave/browser/brave_wallet/tx_service_factory.h"
#include "brave/browser/brave_wallet/zcash_wallet_service_factory.h"
#include "brave/browser/ui/webui/brave_wallet/wallet_common_ui.h"
#include "brave/browser/ui/webui/navigation_bar_data_provider.h"
#include "brave/components/brave_wallet/browser/asset_ratio_service.h"
Expand Down Expand Up @@ -102,6 +103,8 @@ void WalletPageUI::CreatePageHandler(
json_rpc_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::BitcoinWalletService>
bitcoin_rpc_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::ZCashWalletService>
zcash_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::SwapService>
swap_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::AssetRatioService>
Expand Down Expand Up @@ -140,6 +143,8 @@ void WalletPageUI::CreatePageHandler(
profile, std::move(json_rpc_service_receiver));
brave_wallet::BitcoinWalletServiceFactory::BindForContext(
profile, std::move(bitcoin_rpc_service_receiver));
brave_wallet::ZCashWalletServiceFactory::BindForContext(
profile, std::move(zcash_service_receiver));
brave_wallet::SwapServiceFactory::BindForContext(
profile, std::move(swap_service_receiver));
brave_wallet::AssetRatioServiceFactory::BindForContext(
Expand Down
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_wallet/wallet_page_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class WalletPageUI : public ui::MojoWebUIController,
json_rpc_service,
mojo::PendingReceiver<brave_wallet::mojom::BitcoinWalletService>
bitcoin_rpc_service,
mojo::PendingReceiver<brave_wallet::mojom::ZCashWalletService>
zcash_service,
mojo::PendingReceiver<brave_wallet::mojom::SwapService> swap_service,
mojo::PendingReceiver<brave_wallet::mojom::AssetRatioService>
asset_ratio_service,
Expand Down
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_wallet/wallet_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ void WalletPanelUI::CreatePanelHandler(
json_rpc_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::BitcoinWalletService>
bitcoin_rpc_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::ZCashWalletService>
zcash_rpc_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::SwapService>
swap_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::SimulationService>
Expand Down
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_wallet/wallet_panel_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class WalletPanelUI : public ui::MojoBubbleWebUIController,
json_rpc_service,
mojo::PendingReceiver<brave_wallet::mojom::BitcoinWalletService>
bitcoin_rpc_service,
mojo::PendingReceiver<brave_wallet::mojom::ZCashWalletService>
zcash_service,
mojo::PendingReceiver<brave_wallet::mojom::SwapService> swap_service,
mojo::PendingReceiver<brave_wallet::mojom::SimulationService>
simulation_service,
Expand Down
10 changes: 10 additions & 0 deletions components/brave_wallet/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import("//brave/build/config.gni")
import("//brave/components/ipfs/buildflags/buildflags.gni")
import("//build/config/features.gni")
import("//third_party/protobuf/proto_library.gni")
import("//tools/json_schema_compiler/json_schema_api.gni")

declare_args() {
Expand Down Expand Up @@ -204,6 +205,10 @@ static_library("browser") {
"unstoppable_domains_multichain_calls.h",
"wallet_data_files_installer.cc",
"wallet_data_files_installer.h",
"zcash/zcash_rpc.cc",
"zcash/zcash_rpc.h",
"zcash/zcash_wallet_service.cc",
"zcash/zcash_wallet_service.h",
]
if (enable_ipfs_local_node) {
sources += [
Expand All @@ -224,6 +229,7 @@ static_library("browser") {
":pref_names",
":transaction",
":utils",
":zcash_proto",
"//base",
"//brave/components/brave_component_updater/browser",
"//brave/components/brave_stats/browser",
Expand Down Expand Up @@ -472,6 +478,10 @@ generated_types("generated_swap_responses") {
]
}

proto_library("zcash_proto") {
sources = [ "zcash/protos/zcash_grpc_data.proto" ]
}

config("sardine_config") {
defines = [
"SARDINE_CLIENT_ID=\"$sardine_client_id\"",
Expand Down
5 changes: 5 additions & 0 deletions components/brave_wallet/browser/brave_wallet_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ constexpr webui::LocalizedString kLocalizedStrings[] = {
IDS_BRAVE_WALLET_CREATE_ACCOUNT_FILECOIN_DESCRIPTION},
{"braveWalletCreateAccountBitcoinDescription",
IDS_BRAVE_WALLET_CREATE_ACCOUNT_BITCOIN_DESCRIPTION},
{"braveWalletCreateAccountZCashDescription",
IDS_BRAVE_WALLET_CREATE_ACCOUNT_ZCASH_DESCRIPTION},
{"braveWalletFilecoinPrivateKeyProtocol",
IDS_BRAVE_WALLET_FILECOIN_PRIVATE_KEY_PROTOCOL},
{"braveWalletAddAccountImport", IDS_BRAVE_WALLET_ADD_ACCOUNT_IMPORT},
Expand Down Expand Up @@ -905,6 +907,8 @@ constexpr webui::LocalizedString kLocalizedStrings[] = {
IDS_BRAVE_WALLET_FIL_ACCOUNT_DESCRIPTION},
{"braveWalletBTCAccountDescrption",
IDS_BRAVE_WALLET_BTC_ACCOUNT_DESCRIPTION},
{"braveWalletZECAccountDescrption",
IDS_BRAVE_WALLET_ZEC_ACCOUNT_DESCRIPTION},
{"braveWalletShowNetworkLogoOnNftsTitle",
IDS_BRAVE_WALLET_SHOW_NETWORK_LOGO_ON_NFTS_TITLE},
{"braveWalletShowNetworkLogoOnNftsDescription",
Expand Down Expand Up @@ -1409,6 +1413,7 @@ constexpr char kERC721InterfaceId[] = "0x80ac58cd";
constexpr char kERC721MetadataInterfaceId[] = "0x5b5e139f";

constexpr char kBitcoinPrefKey[] = "bitcoin";
constexpr char kZCashPrefKey[] = "zcash";
constexpr char kEthereumPrefKey[] = "ethereum";
constexpr char kFilecoinPrefKey[] = "filecoin";
constexpr char kSolanaPrefKey[] = "solana";
Expand Down
Loading