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

SNS domain resolving #15863

Merged
merged 2 commits into from
Nov 18, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public void onBindViewHolder(PreferenceViewHolder holder) {
mButtons.set(ResolveMethodTypes.DISABLED,
(RadioButtonWithDescription) holder.findViewById(
R.id.ens_resolve_method_disabled_radio_button));
mButtons.set(ResolveMethodTypes.ETHEREUM,
mButtons.set(ResolveMethodTypes.ENABLED,
(RadioButtonWithDescription) holder.findViewById(
R.id.ens_resolve_method_ethereum_radio_button));
R.id.ens_resolve_method_enabled_radio_button));

mSettingRadioButton = mButtons.get(mSetting);
mSettingRadioButton.setChecked(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public void onBindViewHolder(PreferenceViewHolder holder) {
mButtons.set(ResolveMethodTypes.DISABLED,
(RadioButtonWithDescription) holder.findViewById(
R.id.unstoppable_domains_resolve_method_disabled_radio_button));
mButtons.set(ResolveMethodTypes.ETHEREUM,
mButtons.set(ResolveMethodTypes.ENABLED,
(RadioButtonWithDescription) holder.findViewById(
R.id.unstoppable_domains_resolve_method_ethereum_radio_button));
R.id.unstoppable_domains_resolve_method_enabled_radio_button));

mSettingRadioButton = mButtons.get(mSetting);
mSettingRadioButton.setChecked(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
app:primaryText="@string/resolve_method_disabled" />

<org.chromium.components.browser_ui.widget.RadioButtonWithDescription
android:id="@+id/ens_resolve_method_ethereum_radio_button"
android:id="@+id/ens_resolve_method_enabled_radio_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/min_touch_target_size"
app:primaryText="@string/resolve_method_ethereum" />
app:primaryText="@string/resolve_method_enabled" />

</org.chromium.components.browser_ui.widget.RadioButtonWithDescriptionLayout>
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
app:primaryText="@string/resolve_method_disabled" />

<org.chromium.components.browser_ui.widget.RadioButtonWithDescription
android:id="@+id/unstoppable_domains_resolve_method_ethereum_radio_button"
android:id="@+id/unstoppable_domains_resolve_method_enabled_radio_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/min_touch_target_size"
app:primaryText="@string/resolve_method_ethereum" />
app:primaryText="@string/resolve_method_enabled" />

</org.chromium.components.browser_ui.widget.RadioButtonWithDescriptionLayout>
</LinearLayout>
9 changes: 6 additions & 3 deletions app/brave_settings_strings.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -706,17 +706,20 @@
Media router enables cast feature in the browser.
</message>
<message name="IDS_SETTINGS_RESOLVE_UNSTOPPABLE_DOMAINS_DESC" desc="The description for how to handle Unstoppable Domains">
Method to resolve Unstoppable Domains
Resolve Unstoppable Domains domain names
</message>
<message name="IDS_SETTINGS_RESOLVE_UNSTOPPABLE_DOMAINS_SUB_DESC" desc="The sub-description for how to handle Unstoppable Domains">
<ph name="BEGIN_LINK">&lt;a target="_blank" href="$1"&gt;</ph>Learn More<ph name="END_LINK">&lt;/a&gt;</ph> about Ethereum privacy considerations.
<ph name="BEGIN_LINK">&lt;a target="_blank" href="$1"&gt;</ph>Learn More<ph name="END_LINK">&lt;/a&gt;</ph> about Unstoppable Domains privacy considerations.
</message>
<message name="IDS_SETTINGS_RESOLVE_ENS_DESC" desc="The description for how to handle ENS">
Method to resolve Ethereum Name Service (ENS)
Resolve Ethereum Name Service (ENS) domain names
</message>
<message name="IDS_SETTINGS_ENABLE_ENS_OFFCHAIN_LOOKUP_TITLE" desc="ENS offchain lookup method title">
Allow ENS offchain lookup.
</message>
<message name="IDS_SETTINGS_RESOLVE_SNS_DESC" desc="The description for how to handle SNS">
Resolve Solana Name Service (SNS) domain names
</message>
<message name="IDS_SETTINGS_ENABLE_ENS_OFFCHAIN_LOOKUP_DESC" desc="ENS offchain lookup method desc">
<ph name="BEGIN_LINK">&lt;a target="_blank" href="$1"&gt;</ph>Learn More<ph name="END_LINK">&lt;/a&gt;</ph> about ENS offchain lookup privacy considerations.
</message>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class DecentralizedDnsNavigationThrottleBrowserTest
~DecentralizedDnsNavigationThrottleBrowserTest() override = default;

PrefService* local_state() { return g_browser_process->local_state(); }

private:
base::test::ScopedFeatureList scoped_feature{
brave_wallet::features::kBraveWalletSnsFeature};
};

IN_PROC_BROWSER_TEST_F(DecentralizedDnsNavigationThrottleBrowserTest,
Expand All @@ -103,7 +107,7 @@ IN_PROC_BROWSER_TEST_F(DecentralizedDnsNavigationThrottleBrowserTest,
SendInterstitialCommandSync(
browser(),
security_interstitials::SecurityInterstitialCommand::CMD_PROCEED);
EXPECT_EQ(static_cast<int>(ResolveMethodTypes::ETHEREUM),
EXPECT_EQ(static_cast<int>(ResolveMethodTypes::ENABLED),
local_state()->GetInteger(kUnstoppableDomainsResolveMethod));
}

Expand Down Expand Up @@ -144,7 +148,7 @@ IN_PROC_BROWSER_TEST_F(DecentralizedDnsNavigationThrottleBrowserTest,
SendInterstitialCommandSync(
browser(),
security_interstitials::SecurityInterstitialCommand::CMD_PROCEED);
EXPECT_EQ(static_cast<int>(ResolveMethodTypes::ETHEREUM),
EXPECT_EQ(static_cast<int>(ResolveMethodTypes::ENABLED),
local_state()->GetInteger(kENSResolveMethod));
}

Expand Down Expand Up @@ -194,7 +198,7 @@ class EnsL2OffchanLookupNavigationThrottleBrowserTest
IN_PROC_BROWSER_TEST_F(EnsL2OffchanLookupNavigationThrottleBrowserTest,
ShowENSOffchainLookupInterstitialAndProceed) {
local_state()->SetInteger(kENSResolveMethod,
static_cast<int>(ResolveMethodTypes::ETHEREUM));
static_cast<int>(ResolveMethodTypes::ENABLED));
SetEnsResolverResult(EnsResolverTaskResult({}, true), absl::nullopt);

EnsResolverTask::GetWorkOnTaskForTesting() =
Expand Down Expand Up @@ -227,7 +231,7 @@ IN_PROC_BROWSER_TEST_F(EnsL2OffchanLookupNavigationThrottleBrowserTest,
IN_PROC_BROWSER_TEST_F(EnsL2OffchanLookupNavigationThrottleBrowserTest,
ShowENSOffchainLookupInterstitialAndDontProceed) {
local_state()->SetInteger(kENSResolveMethod,
static_cast<int>(ResolveMethodTypes::ETHEREUM));
static_cast<int>(ResolveMethodTypes::ENABLED));
SetEnsResolverResult(EnsResolverTaskResult({}, true), absl::nullopt);

EnsResolverTask::GetWorkOnTaskForTesting() =
Expand Down Expand Up @@ -257,4 +261,44 @@ IN_PROC_BROWSER_TEST_F(EnsL2OffchanLookupNavigationThrottleBrowserTest,
GetEnsOffchainResolveMethod(local_state()));
}

IN_PROC_BROWSER_TEST_F(DecentralizedDnsNavigationThrottleBrowserTest,
ShowSnsInterstitialAndProceed) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("http://test.sol")));

content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();

EXPECT_TRUE(WaitForRenderFrameReady(web_contents->GetPrimaryMainFrame()));
EXPECT_EQ(DecentralizedDnsOptInPage::kTypeForTesting,
GetInterstitialType(web_contents));

EXPECT_EQ(static_cast<int>(ResolveMethodTypes::ASK),
local_state()->GetInteger(kSnsResolveMethod));
SendInterstitialCommandSync(
browser(),
security_interstitials::SecurityInterstitialCommand::CMD_PROCEED);
EXPECT_EQ(static_cast<int>(ResolveMethodTypes::ENABLED),
local_state()->GetInteger(kSnsResolveMethod));
}

IN_PROC_BROWSER_TEST_F(DecentralizedDnsNavigationThrottleBrowserTest,
ShowSnsInterstitialAndReject) {
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("http://test.sol")));

content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();

EXPECT_TRUE(WaitForRenderFrameReady(web_contents->GetPrimaryMainFrame()));
EXPECT_EQ(DecentralizedDnsOptInPage::kTypeForTesting,
GetInterstitialType(web_contents));

EXPECT_EQ(static_cast<int>(ResolveMethodTypes::ASK),
local_state()->GetInteger(kSnsResolveMethod));
SendInterstitialCommandSync(
browser(),
security_interstitials::SecurityInterstitialCommand::CMD_DONT_PROCEED);
EXPECT_EQ(static_cast<int>(ResolveMethodTypes::DISABLED),
local_state()->GetInteger(kSnsResolveMethod));
}

} // namespace decentralized_dns
8 changes: 4 additions & 4 deletions browser/decentralized_dns/test/utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ TEST_F(UtilsUnitTest, IsUnstoppableDomainsResolveMethodAsk) {
EXPECT_TRUE(IsUnstoppableDomainsResolveMethodAsk(local_state()));

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

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

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

Expand All @@ -61,15 +61,15 @@ TEST_F(UtilsUnitTest, IsENSResolveMethodAsk) {
EXPECT_TRUE(IsENSResolveMethodAsk(local_state()));

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() {
settings_api::PrefType::PREF_TYPE_NUMBER;
(*s_brave_allowlist)[decentralized_dns::kEnsOffchainResolveMethod] =
settings_api::PrefType::PREF_TYPE_NUMBER;
(*s_brave_allowlist)[decentralized_dns::kSnsResolveMethod] =
settings_api::PrefType::PREF_TYPE_NUMBER;

// Media router pref
(*s_brave_allowlist)[kEnableMediaRouterOnRestart] =
Expand Down
8 changes: 4 additions & 4 deletions browser/ipfs/content_browser_client_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLRewriteENS) {
ASSERT_FALSE(HandleIPFSURLRewrite(&ens_uri, browser_context()));
local_state()->SetInteger(
decentralized_dns::kENSResolveMethod,
static_cast<int>(decentralized_dns::ResolveMethodTypes::ETHEREUM));
static_cast<int>(decentralized_dns::ResolveMethodTypes::ENABLED));
EXPECT_TRUE(decentralized_dns::IsENSResolveMethodEthereum(local_state()));
ASSERT_TRUE(HandleIPFSURLRewrite(&ens_uri, browser_context()));
}
Expand Down Expand Up @@ -317,23 +317,23 @@ TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLRewriteCrypto) {
profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod, static_cast<int>(IPFSResolveMethodTypes::IPFS_LOCAL));
ASSERT_TRUE(
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::ETHEREUM));
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::ENABLED));
ASSERT_FALSE(
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::DISABLED));

profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_GATEWAY));
ASSERT_FALSE(
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::ETHEREUM));
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::ENABLED));
ASSERT_FALSE(
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::DISABLED));

profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(IPFSResolveMethodTypes::IPFS_DISABLED));
ASSERT_FALSE(
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::ETHEREUM));
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::ENABLED));
ASSERT_FALSE(
ResolveUnstoppableURL(decentralized_dns::ResolveMethodTypes::DISABLED));
}
Expand Down
29 changes: 29 additions & 0 deletions browser/net/decentralized_dns_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include <utility>
#include <vector>

#include "base/feature_list.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
#include "brave/components/brave_wallet/browser/json_rpc_service.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/decentralized_dns/core/constants.h"
#include "brave/components/decentralized_dns/core/utils.h"
#include "brave/components/ipfs/ipfs_utils.h"
Expand Down Expand Up @@ -57,6 +59,18 @@ int OnBeforeURLRequest_DecentralizedDnsPreRedirectWork(
return net::ERR_IO_PENDING;
}

if (base::FeatureList::IsEnabled(
brave_wallet::features::kBraveWalletSnsFeature) &&
IsSnsTLD(ctx->request_url) &&
IsSnsResolveMethodEnabled(g_browser_process->local_state())) {
json_rpc_service->SnsResolveHost(
ctx->request_url.host(),
base::BindOnce(&OnBeforeURLRequest_SnsRedirectWork, next_callback,
ctx));

return net::ERR_IO_PENDING;
}

return net::OK;
}

Expand Down Expand Up @@ -88,6 +102,21 @@ void OnBeforeURLRequest_EnsRedirectWork(
next_callback.Run();
}

void OnBeforeURLRequest_SnsRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx,
const GURL& url,
brave_wallet::mojom::SolanaProviderError error,
const std::string& error_message) {
if (error == brave_wallet::mojom::SolanaProviderError::kSuccess &&
url.is_valid()) {
ctx->new_url_spec = url.spec();
}

if (!next_callback.is_null())
next_callback.Run();
}

void OnBeforeURLRequest_UnstoppableDomainsRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx,
Expand Down
7 changes: 7 additions & 0 deletions browser/net/decentralized_dns_network_delegate_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ void OnBeforeURLRequest_EnsRedirectWork(
brave_wallet::mojom::ProviderError error,
const std::string& error_message);

void OnBeforeURLRequest_SnsRedirectWork(
const brave::ResponseCallback& next_callback,
std::shared_ptr<brave::BraveRequestInfo> ctx,
const GURL& url,
brave_wallet::mojom::SolanaProviderError error,
const std::string& error_message);

} // namespace decentralized_dns

#endif // BRAVE_BROWSER_NET_DECENTRALIZED_DNS_NETWORK_DELEGATE_HELPER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
EXPECT_TRUE(brave_request_info->new_url_spec.empty());

local_state()->SetInteger(kUnstoppableDomainsResolveMethod,
static_cast<int>(ResolveMethodTypes::ETHEREUM));
static_cast<int>(ResolveMethodTypes::ENABLED));
EXPECT_TRUE(IsUnstoppableDomainsResolveMethodEthereum(local_state()));

// No redirect for OTR context.
Expand Down Expand Up @@ -127,7 +127,7 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
EXPECT_TRUE(brave_request_info->new_url_spec.empty());

local_state()->SetInteger(kENSResolveMethod,
static_cast<int>(ResolveMethodTypes::ETHEREUM));
static_cast<int>(ResolveMethodTypes::ENABLED));
EXPECT_TRUE(IsENSResolveMethodEthereum(local_state()));
brave_request_info->request_url = GURL("http://brave.eth");
rc = OnBeforeURLRequest_DecentralizedDnsPreRedirectWork(base::DoNothing(),
Expand All @@ -139,7 +139,7 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
DecentralizedDnsPreRedirectTLDs) {
local_state()->SetInteger(kUnstoppableDomainsResolveMethod,
static_cast<int>(ResolveMethodTypes::ETHEREUM));
static_cast<int>(ResolveMethodTypes::ENABLED));
struct TestCase {
const char* url;
bool is_valid;
Expand Down Expand Up @@ -172,7 +172,7 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
UnstoppableDomainsRedirectWork) {
local_state()->SetInteger(kUnstoppableDomainsResolveMethod,
static_cast<int>(ResolveMethodTypes::ETHEREUM));
static_cast<int>(ResolveMethodTypes::ENABLED));

GURL url("http://brave.crypto");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);
Expand Down Expand Up @@ -298,4 +298,34 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
net::ERR_ENS_OFFCHAIN_LOOKUP_NOT_SELECTED);
}

TEST_F(DecentralizedDnsNetworkDelegateHelperTest, SnsRedirectWork) {
GURL url("http://test.sol");
auto brave_request_info = std::make_shared<brave::BraveRequestInfo>(url);

// No redirect for failed requests.
OnBeforeURLRequest_SnsRedirectWork(
base::DoNothing(), brave_request_info, {},
brave_wallet::mojom::SolanaProviderError::kInternalError, "todo");
EXPECT_TRUE(brave_request_info->new_url_spec.empty());

OnBeforeURLRequest_SnsRedirectWork(
base::DoNothing(), brave_request_info, {},
brave_wallet::mojom::SolanaProviderError::kSuccess, "");
EXPECT_TRUE(brave_request_info->new_url_spec.empty());

// No redirect for invalid url.
OnBeforeURLRequest_SnsRedirectWork(
base::DoNothing(), brave_request_info, GURL("invalid"),
brave_wallet::mojom::SolanaProviderError::kSuccess, "");
EXPECT_TRUE(brave_request_info->new_url_spec.empty());

// Redirect for valid url.
OnBeforeURLRequest_SnsRedirectWork(
base::DoNothing(), brave_request_info, GURL("https://brave.com"),
brave_wallet::mojom::SolanaProviderError::kSuccess, "");
EXPECT_EQ(brave_request_info->new_url_spec, GURL("https://brave.com"));

EXPECT_FALSE(brave_request_info->pending_error.has_value());
}

} // namespace decentralized_dns
Loading