Skip to content

Commit

Permalink
Implement ZCash account creation & balance resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
cypt4 committed Oct 3, 2023
1 parent 54bc1f7 commit fcc11eb
Show file tree
Hide file tree
Showing 50 changed files with 1,038 additions and 15 deletions.
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_
6 changes: 6 additions & 0 deletions browser/browser_context_keyed_service_factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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 +36,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 +152,10 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
brave_wallet::TxServiceFactory::GetInstance();
brave_wallet::BraveWalletServiceFactory::GetInstance();

if (brave_wallet::IsZCashEnabled()) {
brave_wallet::ZCashWalletServiceFactory::GetInstance();
}

#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 @@ -470,6 +476,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 @@ -322,6 +322,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 @@ -894,6 +896,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 @@ -1398,6 +1402,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

0 comments on commit fcc11eb

Please sign in to comment.