Skip to content

Commit

Permalink
Exposed ENS/SNS/UD prefs through mojo (#17323)
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassive committed Feb 24, 2023
1 parent 8fd8e64 commit b1cb85e
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 105 deletions.
28 changes: 20 additions & 8 deletions browser/decentralized_dns/test/utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ TEST_F(UtilsUnitTest, IsUnstoppableDomainsTLD) {
TEST_F(UtilsUnitTest, IsUnstoppableDomainsResolveMethodAsk) {
EXPECT_TRUE(IsUnstoppableDomainsResolveMethodAsk(local_state()));

local_state()->SetInteger(kUnstoppableDomainsResolveMethod,
static_cast<int>(ResolveMethodTypes::ENABLED));
SetUnstoppableDomainsResolveMethod(local_state(),
ResolveMethodTypes::ENABLED);
EXPECT_FALSE(IsUnstoppableDomainsResolveMethodAsk(local_state()));
}

TEST_F(UtilsUnitTest, IsUnstoppableDomainsResolveMethodEnabled) {
EXPECT_FALSE(IsUnstoppableDomainsResolveMethodEnabled(local_state()));

local_state()->SetInteger(kUnstoppableDomainsResolveMethod,
static_cast<int>(ResolveMethodTypes::ENABLED));
SetUnstoppableDomainsResolveMethod(local_state(),
ResolveMethodTypes::ENABLED);
EXPECT_TRUE(IsUnstoppableDomainsResolveMethodEnabled(local_state()));
}

Expand All @@ -67,19 +67,31 @@ TEST_F(UtilsUnitTest, IsSnsTLD) {
TEST_F(UtilsUnitTest, IsENSResolveMethodAsk) {
EXPECT_TRUE(IsENSResolveMethodAsk(local_state()));

local_state()->SetInteger(kENSResolveMethod,
static_cast<int>(ResolveMethodTypes::ENABLED));
SetENSResolveMethod(local_state(), ResolveMethodTypes::ENABLED);
EXPECT_FALSE(IsENSResolveMethodAsk(local_state()));
}

TEST_F(UtilsUnitTest, IsENSResolveMethodEnabledd) {
EXPECT_FALSE(IsENSResolveMethodEnabled(local_state()));

local_state()->SetInteger(kENSResolveMethod,
static_cast<int>(ResolveMethodTypes::ENABLED));
SetENSResolveMethod(local_state(), ResolveMethodTypes::ENABLED);
EXPECT_TRUE(IsENSResolveMethodEnabled(local_state()));
}

TEST_F(UtilsUnitTest, IsSnsResolveMethodAsk) {
EXPECT_TRUE(IsSnsResolveMethodAsk(local_state()));

SetSnsResolveMethod(local_state(), ResolveMethodTypes::ENABLED);
EXPECT_FALSE(IsSnsResolveMethodAsk(local_state()));
}

TEST_F(UtilsUnitTest, IsSnsResolveMethodEnabledd) {
EXPECT_FALSE(IsSnsResolveMethodEnabled(local_state()));

SetSnsResolveMethod(local_state(), ResolveMethodTypes::ENABLED);
EXPECT_TRUE(IsSnsResolveMethodEnabled(local_state()));
}

TEST_F(UtilsUnitTest, ResolveMethodMigration) {
EXPECT_TRUE(IsUnstoppableDomainsResolveMethodAsk(local_state()));
EXPECT_TRUE(IsENSResolveMethodAsk(local_state()));
Expand Down
16 changes: 6 additions & 10 deletions components/brave_wallet/browser/ethereum_provider_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
#include "base/containers/contains.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/components/brave_wallet/browser/brave_wallet_provider_delegate.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/eth_response_parser.h"
#include "brave/components/brave_wallet/browser/json_rpc_service.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/tx_service.h"
Expand Down Expand Up @@ -92,8 +89,7 @@ EthereumProviderImpl::EthereumProviderImpl(
brave_wallet_service_(brave_wallet_service),
eth_block_tracker_(json_rpc_service),
eth_logs_tracker_(json_rpc_service),
prefs_(prefs),
weak_factory_(this) {
prefs_(prefs) {
DCHECK(json_rpc_service);
json_rpc_service_->AddObserver(
rpc_observer_receiver_.BindNewPipeAndPassRemote());
Expand Down Expand Up @@ -1536,13 +1532,13 @@ void EthereumProviderImpl::OnUpdateKnownAccounts(
if (error != mojom::ProviderError::kSuccess) {
return;
}
bool accounts_changed = allowed_accounts != known_allowed_accounts;
known_allowed_accounts = allowed_accounts;
if (!first_known_accounts_check && events_listener_.is_bound() &&
bool accounts_changed = allowed_accounts != known_allowed_accounts_;
known_allowed_accounts_ = allowed_accounts;
if (!first_known_accounts_check_ && events_listener_.is_bound() &&
accounts_changed) {
events_listener_->AccountsChangedEvent(known_allowed_accounts);
events_listener_->AccountsChangedEvent(known_allowed_accounts_);
}
first_known_accounts_check = false;
first_known_accounts_check_ = false;
}

void EthereumProviderImpl::Web3ClientVersion(RequestCallback callback,
Expand Down
7 changes: 3 additions & 4 deletions components/brave_wallet/browser/ethereum_provider_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "brave/components/brave_wallet/browser/eth_block_tracker.h"
#include "brave/components/brave_wallet/browser/eth_logs_tracker.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/web3_provider_constants.h"
#include "components/content_settings/core/browser/content_settings_observer.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
Expand Down Expand Up @@ -404,15 +403,15 @@ class EthereumProviderImpl final
mojo::Receiver<mojom::TxServiceObserver> tx_observer_receiver_{this};
mojo::Receiver<brave_wallet::mojom::KeyringServiceObserver>
keyring_observer_receiver_{this};
std::vector<std::string> known_allowed_accounts;
std::vector<std::string> known_allowed_accounts_;
std::vector<std::string> eth_subscriptions_;
std::vector<std::string> eth_log_subscriptions_;
EthBlockTracker eth_block_tracker_;
EthLogsTracker eth_logs_tracker_;
bool first_known_accounts_check = true;
bool first_known_accounts_check_ = true;
PrefService* prefs_ = nullptr;
bool wallet_onboarding_shown_ = false;
base::WeakPtrFactory<EthereumProviderImpl> weak_factory_;
base::WeakPtrFactory<EthereumProviderImpl> weak_factory_{this};
};

} // namespace brave_wallet
Expand Down
118 changes: 107 additions & 11 deletions components/brave_wallet/browser/json_rpc_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "base/base64.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
Expand Down Expand Up @@ -42,10 +41,7 @@
#include "brave/components/brave_wallet/common/eth_address.h"
#include "brave/components/brave_wallet/common/eth_request_helper.h"
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/brave_wallet/common/hash_utils.h"
#include "brave/components/brave_wallet/common/hex_utils.h"
#include "brave/components/brave_wallet/common/value_conversion_utils.h"
#include "brave/components/brave_wallet/common/web3_provider_constants.h"
#include "brave/components/decentralized_dns/core/constants.h"
#include "brave/components/decentralized_dns/core/utils.h"
#include "components/grit/brave_components_strings.h"
Expand All @@ -57,10 +53,13 @@
#include "url/origin.h"

using api_request_helper::APIRequestHelper;
using decentralized_dns::EnsOffchainResolveMethod;

namespace {

using brave_wallet::mojom::ResolveMethod;
using decentralized_dns::EnsOffchainResolveMethod;
using decentralized_dns::ResolveMethodTypes;

// The domain name should be a-z | A-Z | 0-9 and hyphen(-).
// The domain name should not start or end with hyphen (-).
// The domain name can be a subdomain.
Expand Down Expand Up @@ -135,10 +134,66 @@ bool EnsOffchainPrefDisabled(PrefService* local_state_prefs) {
EnsOffchainResolveMethod::kDisabled;
}

void SetEnsOffchainPref(PrefService* local_state_prefs, bool enabled) {
decentralized_dns::SetEnsOffchainResolveMethod(
local_state_prefs, enabled ? EnsOffchainResolveMethod::kEnabled
: EnsOffchainResolveMethod::kDisabled);
brave_wallet::mojom::ResolveMethod ToMojomResolveMethod(
decentralized_dns::ResolveMethodTypes method) {
switch (method) {
case ResolveMethodTypes::ASK:
return ResolveMethod::kAsk;
case ResolveMethodTypes::DISABLED:
return ResolveMethod::kDisabled;
case ResolveMethodTypes::ENABLED:
return ResolveMethod::kEnabled;
case ResolveMethodTypes::DEPRECATED_DNS_OVER_HTTPS:
break;
}

NOTREACHED();
return ResolveMethod::kDisabled;
}

decentralized_dns::ResolveMethodTypes FromMojomResolveMethod(
brave_wallet::mojom::ResolveMethod method) {
switch (method) {
case ResolveMethod::kAsk:
return ResolveMethodTypes::ASK;
case ResolveMethod::kDisabled:
return ResolveMethodTypes::DISABLED;
case ResolveMethod::kEnabled:
return ResolveMethodTypes::ENABLED;
}

NOTREACHED();
return ResolveMethodTypes::DISABLED;
}

brave_wallet::mojom::ResolveMethod ToMojomEnsOffchainResolveMethod(
decentralized_dns::EnsOffchainResolveMethod method) {
switch (method) {
case EnsOffchainResolveMethod::kAsk:
return ResolveMethod::kAsk;
case EnsOffchainResolveMethod::kDisabled:
return ResolveMethod::kDisabled;
case EnsOffchainResolveMethod::kEnabled:
return ResolveMethod::kEnabled;
}

NOTREACHED();
return ResolveMethod::kDisabled;
}

decentralized_dns::EnsOffchainResolveMethod FromMojomEnsOffchainResolveMethod(
brave_wallet::mojom::ResolveMethod method) {
switch (method) {
case ResolveMethod::kAsk:
return EnsOffchainResolveMethod::kAsk;
case ResolveMethod::kDisabled:
return EnsOffchainResolveMethod::kDisabled;
case ResolveMethod::kEnabled:
return EnsOffchainResolveMethod::kEnabled;
}

NOTREACHED();
return EnsOffchainResolveMethod::kDisabled;
}

namespace solana {
Expand Down Expand Up @@ -1421,8 +1476,49 @@ void JsonRpcService::OnEnsGetContentHash(EnsGetContentHashCallback callback,
"");
}

void JsonRpcService::EnableEnsOffchainLookup() {
SetEnsOffchainPref(local_state_prefs_, true);
void JsonRpcService::GetUnstoppableDomainsResolveMethod(
GetUnstoppableDomainsResolveMethodCallback callback) {
std::move(callback).Run(ToMojomResolveMethod(
decentralized_dns::GetUnstoppableDomainsResolveMethod(
local_state_prefs_)));
}

void JsonRpcService::GetEnsResolveMethod(GetEnsResolveMethodCallback callback) {
std::move(callback).Run(ToMojomResolveMethod(
decentralized_dns::GetENSResolveMethod(local_state_prefs_)));
}

void JsonRpcService::GetEnsOffchainLookupResolveMethod(
GetEnsOffchainLookupResolveMethodCallback callback) {
std::move(callback).Run(ToMojomEnsOffchainResolveMethod(
decentralized_dns::GetEnsOffchainResolveMethod(local_state_prefs_)));
}

void JsonRpcService::GetSnsResolveMethod(GetSnsResolveMethodCallback callback) {
std::move(callback).Run(ToMojomResolveMethod(
decentralized_dns::GetSnsResolveMethod(local_state_prefs_)));
}

void JsonRpcService::SetUnstoppableDomainsResolveMethod(
mojom::ResolveMethod method) {
decentralized_dns::SetUnstoppableDomainsResolveMethod(
local_state_prefs_, FromMojomResolveMethod(method));
}

void JsonRpcService::SetEnsResolveMethod(mojom::ResolveMethod method) {
decentralized_dns::SetENSResolveMethod(local_state_prefs_,
FromMojomResolveMethod(method));
}

void JsonRpcService::SetEnsOffchainLookupResolveMethod(
mojom::ResolveMethod method) {
decentralized_dns::SetEnsOffchainResolveMethod(
local_state_prefs_, FromMojomEnsOffchainResolveMethod(method));
}

void JsonRpcService::SetSnsResolveMethod(mojom::ResolveMethod method) {
decentralized_dns::SetSnsResolveMethod(local_state_prefs_,
FromMojomResolveMethod(method));
}

void JsonRpcService::EnsGetEthAddr(const std::string& domain,
Expand Down
14 changes: 13 additions & 1 deletion components/brave_wallet/browser/json_rpc_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,19 @@ class JsonRpcService : public KeyedService, public mojom::JsonRpcService {

void EnsGetContentHash(const std::string& domain,
EnsGetContentHashCallback callback);
void EnableEnsOffchainLookup() override;

void GetUnstoppableDomainsResolveMethod(
GetUnstoppableDomainsResolveMethodCallback callback) override;
void GetEnsResolveMethod(GetEnsResolveMethodCallback callback) override;
void GetEnsOffchainLookupResolveMethod(
GetEnsOffchainLookupResolveMethodCallback callback) override;
void GetSnsResolveMethod(GetSnsResolveMethodCallback callback) override;

void SetUnstoppableDomainsResolveMethod(mojom::ResolveMethod method) override;
void SetEnsResolveMethod(mojom::ResolveMethod method) override;
void SetEnsOffchainLookupResolveMethod(mojom::ResolveMethod method) override;
void SetSnsResolveMethod(mojom::ResolveMethod method) override;

void EnsGetEthAddr(const std::string& domain,
EnsGetEthAddrCallback callback) override;
void SnsGetSolAddr(const std::string& domain,
Expand Down
Loading

0 comments on commit b1cb85e

Please sign in to comment.