From 5912e6fd1b0a876458cfcad22fc7d093795c07aa Mon Sep 17 00:00:00 2001 From: Anton Paymyshev Date: Tue, 20 Dec 2022 13:46:11 +0700 Subject: [PATCH 1/2] Wallet address resolution for Android --- .../activities/BuySendSwapActivity.java | 71 +++++++++++++++++++ .../crypto_wallet/util/WalletNativeUtils.java | 15 ++++ .../res/layout/activity_buy_send_swap.xml | 10 +++ .../android/wallet_native_utils_android.cc | 26 +++++++ .../decentralized_dns/test/utils_unittest.cc | 39 +++++----- browser/ipfs/content_browser_client_helper.cc | 28 +++++--- .../content_browser_client_helper_unittest.cc | 4 +- ...centralized_dns_network_delegate_helper.cc | 10 +-- ...ed_dns_network_delegate_helper_unittest.cc | 8 +-- .../android/strings/android_brave_strings.grd | 3 + ...ized_dns_interstitial_controller_client.cc | 6 +- .../decentralized_dns_navigation_throttle.cc | 6 +- .../content/decentralized_dns_opt_in_page.cc | 8 +-- components/decentralized_dns/core/utils.cc | 16 ++--- components/decentralized_dns/core/utils.h | 11 +-- 15 files changed, 202 insertions(+), 59 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java index 6f0d8180a57a..579d83db818d 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java @@ -49,6 +49,7 @@ import com.google.android.gms.vision.barcode.BarcodeDetector; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import org.chromium.base.BraveFeatureList; import org.chromium.base.Log; import org.chromium.brave_wallet.mojom.AccountInfo; import org.chromium.brave_wallet.mojom.BlockchainToken; @@ -83,7 +84,9 @@ import org.chromium.chrome.browser.crypto_wallet.util.TokenUtils; import org.chromium.chrome.browser.crypto_wallet.util.Utils; import org.chromium.chrome.browser.crypto_wallet.util.Validations; +import org.chromium.chrome.browser.crypto_wallet.util.WalletNativeUtils; import org.chromium.chrome.browser.crypto_wallet.util.WalletUtils; +import org.chromium.chrome.browser.flags.ChromeFeatureList; import org.chromium.chrome.browser.qrreader.BarcodeTracker; import org.chromium.chrome.browser.qrreader.BarcodeTrackerFactory; import org.chromium.chrome.browser.qrreader.CameraSource; @@ -169,6 +172,7 @@ public int getValue() { private EditText mFromValueText; private EditText mToValueText; private EditText mSendToAddrText; + private TextView mResolvedAddrText; private TextView mMarketPriceValueText; private TextView mFromBalanceText; private TextView mToBalanceText; @@ -759,6 +763,8 @@ private void adjustControls() { toleranceSection.setVisibility(View.GONE); } + mResolvedAddrText = findViewById(R.id.resolved_addr_text); + // Individual if (mActivityType == ActivityType.BUY) { TextView fromBuyText = findViewById(R.id.from_buy_text); @@ -919,6 +925,10 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { String value = mFromValueText.getText().toString(); if (mActivityType == ActivityType.SEND) { String to = mSendToAddrText.getText().toString(); + if (!mResolvedAddrText.getText().toString().isEmpty()) { + to = mResolvedAddrText.getText().toString(); + } + if (to.isEmpty()) { return; } @@ -1176,6 +1186,59 @@ public void onBackPressed() { super.onBackPressed(); } + private void onResolveWalletAddressDone(String domain, String result) { + if (!domain.equals(mSendToAddrText.getText().toString())) { + return; + } + + if (result == null || result.isEmpty()) { + mResolvedAddrText.setVisibility(View.GONE); + mResolvedAddrText.setText(""); + String notRegsteredErrorText = String.format( + getString(R.string.wallet_domain_not_registered_error_text), domain); + + setSendToFromValueValidationResult(notRegsteredErrorText, true, true); + } else { + mResolvedAddrText.setVisibility(View.VISIBLE); + mResolvedAddrText.setText(result); + setSendToFromValueValidationResult("", false, true); + } + } + + private boolean maybeResolveWalletAddress() { + String domain = mSendToAddrText.getText().toString(); + + if (WalletNativeUtils.isUnstoppableDomainsTld(domain)) { + mJsonRpcService.unstoppableDomainsGetWalletAddr( + domain, mCurrentBlockchainToken, (response, errorResponse, errorString) -> { + onResolveWalletAddressDone(domain, response); + }); + return true; + } + + if (mCurrentBlockchainToken.coin == CoinType.ETH && WalletNativeUtils.isEnsTld(domain)) { + mJsonRpcService.ensGetEthAddr(domain, null, + (response, requireOffchainConsent, errorResponse, errorString) -> { + onResolveWalletAddressDone(domain, response); + }); + return true; + } + + if (ChromeFeatureList.isEnabled(BraveFeatureList.BRAVE_WALLET_SNS)) { + if (mCurrentBlockchainToken.coin == CoinType.SOL + && WalletNativeUtils.isSnsTld(domain)) { + mJsonRpcService.snsGetSolAddr(domain, (response, errorResponse, errorString) -> { + onResolveWalletAddressDone(domain, response); + }); + return true; + } + } + + mResolvedAddrText.setVisibility(View.GONE); + mResolvedAddrText.setText(""); + return false; + } + private TextWatcher getTextWatcherFromToValueText(boolean from) { return new FilterTextFromToValueText(from); } @@ -1210,6 +1273,8 @@ public FilterTextWatcherSendToAddr() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { + if (maybeResolveWalletAddress()) return; + String fromAccountAddress = mCustomAccountAdapter.getAccountAddressAtPosition( mAccountSpinner.getSelectedItemPosition()); @@ -1237,6 +1302,8 @@ public OnFocusChangeListenerToSend() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { + if (maybeResolveWalletAddress()) return; + String fromAccountAddress = mCustomAccountAdapter.getAccountAddressAtPosition( mAccountSpinner.getSelectedItemPosition()); String receiverAccountAddress = ((EditText) v).getText().toString(); @@ -1459,6 +1526,10 @@ token.symbol, getResources().getDisplayMetrics().density, assetText, this, true, enableDisableSwapButton(); getSendSwapQuota(true, false); } + + if (mActivityType == ActivityType.SEND) { + maybeResolveWalletAddress(); + } } private void enableDisableSwapButton() { diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/util/WalletNativeUtils.java b/android/java/org/chromium/chrome/browser/crypto_wallet/util/WalletNativeUtils.java index 396c0b1aadd0..99cb5e2624b6 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/util/WalletNativeUtils.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/util/WalletNativeUtils.java @@ -15,8 +15,23 @@ public static void resetWallet(Profile profile) { WalletNativeUtilsJni.get().resetWallet(profile); } + public static boolean isUnstoppableDomainsTld(String domain) { + return WalletNativeUtilsJni.get().isUnstoppableDomainsTld(domain); + } + + public static boolean isEnsTld(String domain) { + return WalletNativeUtilsJni.get().isEnsTld(domain); + } + + public static boolean isSnsTld(String domain) { + return WalletNativeUtilsJni.get().isSnsTld(domain); + } + @NativeMethods interface Natives { void resetWallet(Profile profile); + boolean isUnstoppableDomainsTld(String domain); + boolean isEnsTld(String domain); + boolean isSnsTld(String domain); } } diff --git a/android/java/res/layout/activity_buy_send_swap.xml b/android/java/res/layout/activity_buy_send_swap.xml index 5997d548f999..46e18a8a959d 100644 --- a/android/java/res/layout/activity_buy_send_swap.xml +++ b/android/java/res/layout/activity_buy_send_swap.xml @@ -329,6 +329,16 @@ + + Reset(); } +static jboolean JNI_WalletNativeUtils_IsUnstoppableDomainsTld( + JNIEnv* env, + const base::android::JavaParamRef& domain) { + auto domain_string = base::android::ConvertJavaStringToUTF8(env, domain); + + return decentralized_dns::IsUnstoppableDomainsTLD(domain_string); +} + +static jboolean JNI_WalletNativeUtils_IsEnsTld( + JNIEnv* env, + const base::android::JavaParamRef& domain) { + auto domain_string = base::android::ConvertJavaStringToUTF8(env, domain); + + return decentralized_dns::IsENSTLD(domain_string); +} + +static jboolean JNI_WalletNativeUtils_IsSnsTld( + JNIEnv* env, + const base::android::JavaParamRef& domain) { + auto domain_string = base::android::ConvertJavaStringToUTF8(env, domain); + + return decentralized_dns::IsSnsTLD(domain_string); +} + } // namespace android } // namespace chrome diff --git a/browser/decentralized_dns/test/utils_unittest.cc b/browser/decentralized_dns/test/utils_unittest.cc index 0a1a9a7c0080..c627a63a49e2 100644 --- a/browser/decentralized_dns/test/utils_unittest.cc +++ b/browser/decentralized_dns/test/utils_unittest.cc @@ -28,10 +28,10 @@ class UtilsUnitTest : public testing::Test { }; TEST_F(UtilsUnitTest, IsUnstoppableDomainsTLD) { - EXPECT_TRUE(IsUnstoppableDomainsTLD(GURL("http://test.crypto"))); - EXPECT_FALSE(IsUnstoppableDomainsTLD(GURL("http://test.com"))); - EXPECT_FALSE(IsUnstoppableDomainsTLD(GURL("http://test.eth"))); - EXPECT_FALSE(IsUnstoppableDomainsTLD(GURL("http://crypto"))); + EXPECT_TRUE(IsUnstoppableDomainsTLD("test.crypto")); + EXPECT_FALSE(IsUnstoppableDomainsTLD("test.com")); + EXPECT_FALSE(IsUnstoppableDomainsTLD("test.eth")); + EXPECT_FALSE(IsUnstoppableDomainsTLD("crypto")); } TEST_F(UtilsUnitTest, IsUnstoppableDomainsResolveMethodAsk) { @@ -42,19 +42,26 @@ TEST_F(UtilsUnitTest, IsUnstoppableDomainsResolveMethodAsk) { EXPECT_FALSE(IsUnstoppableDomainsResolveMethodAsk(local_state())); } -TEST_F(UtilsUnitTest, IsUnstoppableDomainsResolveMethodEthereum) { - EXPECT_FALSE(IsUnstoppableDomainsResolveMethodEthereum(local_state())); +TEST_F(UtilsUnitTest, IsUnstoppableDomainsResolveMethodEnabled) { + EXPECT_FALSE(IsUnstoppableDomainsResolveMethodEnabled(local_state())); local_state()->SetInteger(kUnstoppableDomainsResolveMethod, static_cast(ResolveMethodTypes::ENABLED)); - EXPECT_TRUE(IsUnstoppableDomainsResolveMethodEthereum(local_state())); + EXPECT_TRUE(IsUnstoppableDomainsResolveMethodEnabled(local_state())); } TEST_F(UtilsUnitTest, IsENSTLD) { - EXPECT_TRUE(IsENSTLD(GURL("http://test.eth"))); - EXPECT_FALSE(IsENSTLD(GURL("http://test.com"))); - EXPECT_FALSE(IsENSTLD(GURL("http://test.crypto"))); - EXPECT_FALSE(IsENSTLD(GURL("http://eth"))); + EXPECT_TRUE(IsENSTLD("test.eth")); + EXPECT_FALSE(IsENSTLD("test.com")); + EXPECT_FALSE(IsENSTLD("test.crypto")); + EXPECT_FALSE(IsENSTLD("eth")); +} + +TEST_F(UtilsUnitTest, IsSnsTLD) { + EXPECT_TRUE(IsSnsTLD("test.sol")); + EXPECT_FALSE(IsSnsTLD("test.com")); + EXPECT_FALSE(IsSnsTLD("test.crypto")); + EXPECT_FALSE(IsSnsTLD("eth")); } TEST_F(UtilsUnitTest, IsENSResolveMethodAsk) { @@ -65,12 +72,12 @@ TEST_F(UtilsUnitTest, IsENSResolveMethodAsk) { EXPECT_FALSE(IsENSResolveMethodAsk(local_state())); } -TEST_F(UtilsUnitTest, IsENSResolveMethodEthereum) { - EXPECT_FALSE(IsENSResolveMethodEthereum(local_state())); +TEST_F(UtilsUnitTest, IsENSResolveMethodEnabledd) { + EXPECT_FALSE(IsENSResolveMethodEnabled(local_state())); local_state()->SetInteger(kENSResolveMethod, static_cast(ResolveMethodTypes::ENABLED)); - EXPECT_TRUE(IsENSResolveMethodEthereum(local_state())); + EXPECT_TRUE(IsENSResolveMethodEnabled(local_state())); } TEST_F(UtilsUnitTest, ResolveMethodMigration) { @@ -85,8 +92,8 @@ TEST_F(UtilsUnitTest, ResolveMethodMigration) { static_cast(ResolveMethodTypes::DEPRECATED_DNS_OVER_HTTPS)); EXPECT_FALSE(IsUnstoppableDomainsResolveMethodAsk(local_state())); EXPECT_FALSE(IsENSResolveMethodAsk(local_state())); - EXPECT_FALSE(IsUnstoppableDomainsResolveMethodEthereum(local_state())); - EXPECT_FALSE(IsENSResolveMethodEthereum(local_state())); + EXPECT_FALSE(IsUnstoppableDomainsResolveMethodEnabled(local_state())); + EXPECT_FALSE(IsENSResolveMethodEnabled(local_state())); MigrateObsoleteLocalStatePrefs(local_state()); EXPECT_FALSE(local_state()->HasPrefPath(kUnstoppableDomainsResolveMethod)); diff --git a/browser/ipfs/content_browser_client_helper.cc b/browser/ipfs/content_browser_client_helper.cc index fa55029dabc5..09fa3d8fd557 100644 --- a/browser/ipfs/content_browser_client_helper.cc +++ b/browser/ipfs/content_browser_client_helper.cc @@ -80,16 +80,26 @@ bool HandleIPFSURLRewrite(GURL* url, content::BrowserContext* browser_context) { } } - bool resolve_ens = decentralized_dns::IsENSTLD(*url) && - decentralized_dns::IsENSResolveMethodEthereum( - g_browser_process->local_state()); - bool resolve_ud = - decentralized_dns::IsUnstoppableDomainsTLD(*url) && - decentralized_dns::IsUnstoppableDomainsResolveMethodEthereum( - g_browser_process->local_state()); - if ((resolve_ens || resolve_ud) && IsLocalGatewayConfigured(prefs)) { - return true; + if (IsLocalGatewayConfigured(prefs)) { + if (decentralized_dns::IsENSTLD(url->host_piece()) && + decentralized_dns::IsENSResolveMethodEnabled( + g_browser_process->local_state())) { + return true; + } + + if (decentralized_dns::IsSnsTLD(url->host_piece()) && + decentralized_dns::IsSnsResolveMethodEnabled( + g_browser_process->local_state())) { + return true; + } + + if (decentralized_dns::IsUnstoppableDomainsTLD(url->host_piece()) && + decentralized_dns::IsUnstoppableDomainsResolveMethodEnabled( + g_browser_process->local_state())) { + return true; + } } + return false; } diff --git a/browser/ipfs/content_browser_client_helper_unittest.cc b/browser/ipfs/content_browser_client_helper_unittest.cc index 6ee47dff7451..3c36df8d2395 100644 --- a/browser/ipfs/content_browser_client_helper_unittest.cc +++ b/browser/ipfs/content_browser_client_helper_unittest.cc @@ -192,13 +192,13 @@ TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLRewriteLocal) { TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLRewriteENS) { profile()->GetPrefs()->SetInteger( kIPFSResolveMethod, static_cast(IPFSResolveMethodTypes::IPFS_LOCAL)); - EXPECT_FALSE(decentralized_dns::IsENSResolveMethodEthereum(local_state())); + EXPECT_FALSE(decentralized_dns::IsENSResolveMethodEnabled(local_state())); GURL ens_uri("https://brave.eth"); ASSERT_FALSE(HandleIPFSURLRewrite(&ens_uri, browser_context())); local_state()->SetInteger( decentralized_dns::kENSResolveMethod, static_cast(decentralized_dns::ResolveMethodTypes::ENABLED)); - EXPECT_TRUE(decentralized_dns::IsENSResolveMethodEthereum(local_state())); + EXPECT_TRUE(decentralized_dns::IsENSResolveMethodEnabled(local_state())); ASSERT_TRUE(HandleIPFSURLRewrite(&ens_uri, browser_context())); } diff --git a/browser/net/decentralized_dns_network_delegate_helper.cc b/browser/net/decentralized_dns_network_delegate_helper.cc index db55495d2861..17d0f203f406 100644 --- a/browser/net/decentralized_dns_network_delegate_helper.cc +++ b/browser/net/decentralized_dns_network_delegate_helper.cc @@ -38,8 +38,8 @@ int OnBeforeURLRequest_DecentralizedDnsPreRedirectWork( if (!json_rpc_service) return net::OK; - if (IsUnstoppableDomainsTLD(ctx->request_url) && - IsUnstoppableDomainsResolveMethodEthereum( + if (IsUnstoppableDomainsTLD(ctx->request_url.host_piece()) && + IsUnstoppableDomainsResolveMethodEnabled( g_browser_process->local_state())) { json_rpc_service->UnstoppableDomainsResolveDns( ctx->request_url.host(), @@ -49,8 +49,8 @@ int OnBeforeURLRequest_DecentralizedDnsPreRedirectWork( return net::ERR_IO_PENDING; } - if (IsENSTLD(ctx->request_url) && - IsENSResolveMethodEthereum(g_browser_process->local_state())) { + if (IsENSTLD(ctx->request_url.host_piece()) && + IsENSResolveMethodEnabled(g_browser_process->local_state())) { json_rpc_service->EnsGetContentHash( ctx->request_url.host(), base::BindOnce(&OnBeforeURLRequest_EnsRedirectWork, next_callback, @@ -61,7 +61,7 @@ int OnBeforeURLRequest_DecentralizedDnsPreRedirectWork( if (base::FeatureList::IsEnabled( brave_wallet::features::kBraveWalletSnsFeature) && - IsSnsTLD(ctx->request_url) && + IsSnsTLD(ctx->request_url.host_piece()) && IsSnsResolveMethodEnabled(g_browser_process->local_state())) { json_rpc_service->SnsResolveHost( ctx->request_url.host(), diff --git a/browser/net/decentralized_dns_network_delegate_helper_unittest.cc b/browser/net/decentralized_dns_network_delegate_helper_unittest.cc index 5bd6d617cf8e..0222772ac35a 100644 --- a/browser/net/decentralized_dns_network_delegate_helper_unittest.cc +++ b/browser/net/decentralized_dns_network_delegate_helper_unittest.cc @@ -87,7 +87,7 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest, brave_request_info->browser_context = profile(); // No redirect if resolve method is not set to Ethereum. - EXPECT_FALSE(IsUnstoppableDomainsResolveMethodEthereum(local_state())); + EXPECT_FALSE(IsUnstoppableDomainsResolveMethodEnabled(local_state())); int rc = OnBeforeURLRequest_DecentralizedDnsPreRedirectWork( base::DoNothing(), brave_request_info); EXPECT_EQ(rc, net::OK); @@ -95,7 +95,7 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest, local_state()->SetInteger(kUnstoppableDomainsResolveMethod, static_cast(ResolveMethodTypes::ENABLED)); - EXPECT_TRUE(IsUnstoppableDomainsResolveMethodEthereum(local_state())); + EXPECT_TRUE(IsUnstoppableDomainsResolveMethodEnabled(local_state())); // No redirect for OTR context. brave_request_info->browser_context = @@ -119,7 +119,7 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest, EXPECT_EQ(rc, net::ERR_IO_PENDING); // No redirect if ENS resolve method is not set to Ethereum. - EXPECT_FALSE(IsENSResolveMethodEthereum(local_state())); + EXPECT_FALSE(IsENSResolveMethodEnabled(local_state())); brave_request_info->request_url = GURL("http://brave.eth"); rc = OnBeforeURLRequest_DecentralizedDnsPreRedirectWork(base::DoNothing(), brave_request_info); @@ -128,7 +128,7 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest, local_state()->SetInteger(kENSResolveMethod, static_cast(ResolveMethodTypes::ENABLED)); - EXPECT_TRUE(IsENSResolveMethodEthereum(local_state())); + EXPECT_TRUE(IsENSResolveMethodEnabled(local_state())); brave_request_info->request_url = GURL("http://brave.eth"); rc = OnBeforeURLRequest_DecentralizedDnsPreRedirectWork(base::DoNothing(), brave_request_info); diff --git a/browser/ui/android/strings/android_brave_strings.grd b/browser/ui/android/strings/android_brave_strings.grd index 1e911c68a05d..4cc11704187b 100644 --- a/browser/ui/android/strings/android_brave_strings.grd +++ b/browser/ui/android/strings/android_brave_strings.grd @@ -3327,6 +3327,9 @@ If you don't accept this request, VPN will not reconnect and your internet conne Secondary Networks + + Domain %1$s is not registered + diff --git a/components/decentralized_dns/content/decentralized_dns_interstitial_controller_client.cc b/components/decentralized_dns/content/decentralized_dns_interstitial_controller_client.cc index a309885ad60f..7a4dcbad3462 100644 --- a/components/decentralized_dns/content/decentralized_dns_interstitial_controller_client.cc +++ b/components/decentralized_dns/content/decentralized_dns_interstitial_controller_client.cc @@ -56,11 +56,11 @@ void DecentralizedDnsInterstitialControllerClient::SetResolveMethodAndReload( ResolveMethodTypes type) { DCHECK(local_state_); const char* pref_name = nullptr; - if (IsUnstoppableDomainsTLD(request_url_)) { + if (IsUnstoppableDomainsTLD(request_url_.host_piece())) { pref_name = kUnstoppableDomainsResolveMethod; - } else if (IsENSTLD(request_url_)) { + } else if (IsENSTLD(request_url_.host_piece())) { pref_name = kENSResolveMethod; - } else if (IsSnsTLD(request_url_)) { + } else if (IsSnsTLD(request_url_.host_piece())) { pref_name = kSnsResolveMethod; } else { NOTREACHED(); diff --git a/components/decentralized_dns/content/decentralized_dns_navigation_throttle.cc b/components/decentralized_dns/content/decentralized_dns_navigation_throttle.cc index 148b37b2d4dc..c1e9fad52a73 100644 --- a/components/decentralized_dns/content/decentralized_dns_navigation_throttle.cc +++ b/components/decentralized_dns/content/decentralized_dns_navigation_throttle.cc @@ -59,12 +59,12 @@ DecentralizedDnsNavigationThrottle::~DecentralizedDnsNavigationThrottle() = content::NavigationThrottle::ThrottleCheckResult DecentralizedDnsNavigationThrottle::WillStartRequest() { GURL url = navigation_handle()->GetURL(); - if ((IsUnstoppableDomainsTLD(url) && + if ((IsUnstoppableDomainsTLD(url.host_piece()) && IsUnstoppableDomainsResolveMethodAsk(local_state_)) || - (IsENSTLD(url) && IsENSResolveMethodAsk(local_state_)) || + (IsENSTLD(url.host_piece()) && IsENSResolveMethodAsk(local_state_)) || (base::FeatureList::IsEnabled( brave_wallet::features::kBraveWalletSnsFeature) && - IsSnsTLD(url) && IsSnsResolveMethodAsk(local_state_))) { + IsSnsTLD(url.host_piece()) && IsSnsResolveMethodAsk(local_state_))) { base::SequencedTaskRunnerHandle::Get()->PostTask( FROM_HERE, base::BindOnce(&DecentralizedDnsNavigationThrottle::ShowInterstitial, diff --git a/components/decentralized_dns/content/decentralized_dns_opt_in_page.cc b/components/decentralized_dns/content/decentralized_dns_opt_in_page.cc index 32fe35cf5be6..e01209eaf7a3 100644 --- a/components/decentralized_dns/content/decentralized_dns_opt_in_page.cc +++ b/components/decentralized_dns/content/decentralized_dns_opt_in_page.cc @@ -75,7 +75,7 @@ void DecentralizedDnsOptInPage::PopulateInterstitialStrings( u"https://www.coinbase.com/legal/cloud/terms-of-service/", u"https://www.coinbase.com/legal/privacy/"}; - if (IsUnstoppableDomainsTLD(request_url_)) { + if (IsUnstoppableDomainsTLD(request_url_.host_piece())) { load_time_data.Set("tabTitle", brave_l10n::GetLocalizedResourceUTF16String( IDS_UNSTOPPABLE_DOMAINS_OPT_IN_TITLE)); load_time_data.Set("heading", brave_l10n::GetLocalizedResourceUTF16String( @@ -87,7 +87,7 @@ void DecentralizedDnsOptInPage::PopulateInterstitialStrings( brave_l10n::GetLocalizedResourceUTF16String( IDS_UNSTOPPABLE_DOMAINS_OPT_IN_PRIMARY_PARAGRAPH), infura_links, nullptr)); - } else if (IsENSTLD(request_url_)) { + } else if (IsENSTLD(request_url_.host_piece())) { load_time_data.Set("tabTitle", brave_l10n::GetLocalizedResourceUTF16String( IDS_ENS_OPT_IN_TITLE)); load_time_data.Set("heading", brave_l10n::GetLocalizedResourceUTF16String( @@ -97,7 +97,7 @@ void DecentralizedDnsOptInPage::PopulateInterstitialStrings( brave_l10n::GetLocalizedResourceUTF16String( IDS_ENS_OPT_IN_PRIMARY_PARAGRAPH), infura_links, nullptr)); - } else if (IsSnsTLD(request_url_)) { + } else if (IsSnsTLD(request_url_.host_piece())) { load_time_data.Set("tabTitle", brave_l10n::GetLocalizedResourceUTF16String( IDS_SNS_OPT_IN_TITLE)); load_time_data.Set("heading", brave_l10n::GetLocalizedResourceUTF16String( @@ -111,7 +111,7 @@ void DecentralizedDnsOptInPage::PopulateInterstitialStrings( NOTREACHED(); } - if (IsSnsTLD(request_url_)) { + if (IsSnsTLD(request_url_.host_piece())) { load_time_data.Set( "primaryButtonText", brave_l10n::GetLocalizedResourceUTF16String( diff --git a/components/decentralized_dns/core/utils.cc b/components/decentralized_dns/core/utils.cc index b41d180230eb..746b8fd7c38b 100644 --- a/components/decentralized_dns/core/utils.cc +++ b/components/decentralized_dns/core/utils.cc @@ -40,9 +40,9 @@ void MigrateObsoleteLocalStatePrefs(PrefService* local_state) { } } -bool IsUnstoppableDomainsTLD(const GURL& url) { +bool IsUnstoppableDomainsTLD(const base::StringPiece& host) { for (auto* domain : kUnstoppableDomains) { - if (base::EndsWith(url.host_piece(), domain)) + if (base::EndsWith(host, domain)) return true; } return false; @@ -57,7 +57,7 @@ bool IsUnstoppableDomainsResolveMethodAsk(PrefService* local_state) { static_cast(ResolveMethodTypes::ASK); } -bool IsUnstoppableDomainsResolveMethodEthereum(PrefService* local_state) { +bool IsUnstoppableDomainsResolveMethodEnabled(PrefService* local_state) { if (!local_state) { return false; // Treat it as disabled. } @@ -66,8 +66,8 @@ bool IsUnstoppableDomainsResolveMethodEthereum(PrefService* local_state) { static_cast(ResolveMethodTypes::ENABLED); } -bool IsENSTLD(const GURL& url) { - return base::EndsWith(url.host_piece(), kEthDomain); +bool IsENSTLD(const base::StringPiece& host) { + return base::EndsWith(host, kEthDomain); } bool IsENSResolveMethodAsk(PrefService* local_state) { @@ -79,7 +79,7 @@ bool IsENSResolveMethodAsk(PrefService* local_state) { static_cast(ResolveMethodTypes::ASK); } -bool IsENSResolveMethodEthereum(PrefService* local_state) { +bool IsENSResolveMethodEnabled(PrefService* local_state) { if (!local_state) { return false; // Treat it as disabled. } @@ -98,8 +98,8 @@ EnsOffchainResolveMethod GetEnsOffchainResolveMethod(PrefService* local_state) { local_state->GetInteger(kEnsOffchainResolveMethod)); } -bool IsSnsTLD(const GURL& url) { - return base::EndsWith(url.host_piece(), kSolDomain); +bool IsSnsTLD(const base::StringPiece& host) { + return base::EndsWith(host, kSolDomain); } bool IsSnsResolveMethodAsk(PrefService* local_state) { diff --git a/components/decentralized_dns/core/utils.h b/components/decentralized_dns/core/utils.h index a0deaa9b77a6..bf05d2fe1370 100644 --- a/components/decentralized_dns/core/utils.h +++ b/components/decentralized_dns/core/utils.h @@ -6,6 +6,7 @@ #ifndef BRAVE_COMPONENTS_DECENTRALIZED_DNS_CORE_UTILS_H_ #define BRAVE_COMPONENTS_DECENTRALIZED_DNS_CORE_UTILS_H_ +#include "base/strings/string_piece.h" #include "brave/components/decentralized_dns/core/constants.h" class GURL; @@ -17,19 +18,19 @@ namespace decentralized_dns { void RegisterLocalStatePrefs(PrefRegistrySimple* registry); void MigrateObsoleteLocalStatePrefs(PrefService* local_state); -bool IsUnstoppableDomainsTLD(const GURL& url); +bool IsUnstoppableDomainsTLD(const base::StringPiece& host); bool IsUnstoppableDomainsResolveMethodAsk(PrefService* local_state); -bool IsUnstoppableDomainsResolveMethodEthereum(PrefService* local_state); +bool IsUnstoppableDomainsResolveMethodEnabled(PrefService* local_state); -bool IsENSTLD(const GURL& url); +bool IsENSTLD(const base::StringPiece& host); bool IsENSResolveMethodAsk(PrefService* local_state); -bool IsENSResolveMethodEthereum(PrefService* local_state); +bool IsENSResolveMethodEnabled(PrefService* local_state); void SetEnsOffchainResolveMethod(PrefService* local_state, EnsOffchainResolveMethod method); EnsOffchainResolveMethod GetEnsOffchainResolveMethod(PrefService* local_state); -bool IsSnsTLD(const GURL& url); +bool IsSnsTLD(const base::StringPiece& host); bool IsSnsResolveMethodAsk(PrefService* local_state); bool IsSnsResolveMethodEnabled(PrefService* local_state); From 6e3952c127cc6d560e2b33a2e4afb1dfc58d7bbb Mon Sep 17 00:00:00 2001 From: Anton Paymyshev Date: Wed, 21 Dec 2022 11:51:48 +0700 Subject: [PATCH 2/2] review fixes --- .../activities/BuySendSwapActivity.java | 4 +-- .../content_browser_client_helper_unittest.cc | 29 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java index 579d83db818d..bab776e4b326 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java @@ -1194,10 +1194,10 @@ private void onResolveWalletAddressDone(String domain, String result) { if (result == null || result.isEmpty()) { mResolvedAddrText.setVisibility(View.GONE); mResolvedAddrText.setText(""); - String notRegsteredErrorText = String.format( + String notRegisteredErrorText = String.format( getString(R.string.wallet_domain_not_registered_error_text), domain); - setSendToFromValueValidationResult(notRegsteredErrorText, true, true); + setSendToFromValueValidationResult(notRegisteredErrorText, true, true); } else { mResolvedAddrText.setVisibility(View.VISIBLE); mResolvedAddrText.setText(result); diff --git a/browser/ipfs/content_browser_client_helper_unittest.cc b/browser/ipfs/content_browser_client_helper_unittest.cc index 3c36df8d2395..6a9d8c06a1a6 100644 --- a/browser/ipfs/content_browser_client_helper_unittest.cc +++ b/browser/ipfs/content_browser_client_helper_unittest.cc @@ -189,9 +189,10 @@ TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLRewriteLocal) { ASSERT_TRUE(HandleIPFSURLRewrite(&ipfs_uri, browser_context())); } -TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLRewriteENS) { +TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLRewriteDDns) { profile()->GetPrefs()->SetInteger( kIPFSResolveMethod, static_cast(IPFSResolveMethodTypes::IPFS_LOCAL)); + EXPECT_FALSE(decentralized_dns::IsENSResolveMethodEnabled(local_state())); GURL ens_uri("https://brave.eth"); ASSERT_FALSE(HandleIPFSURLRewrite(&ens_uri, browser_context())); @@ -200,6 +201,32 @@ TEST_F(ContentBrowserClientHelperUnitTest, HandleIPFSURLRewriteENS) { static_cast(decentralized_dns::ResolveMethodTypes::ENABLED)); EXPECT_TRUE(decentralized_dns::IsENSResolveMethodEnabled(local_state())); ASSERT_TRUE(HandleIPFSURLRewrite(&ens_uri, browser_context())); + + EXPECT_FALSE(decentralized_dns::IsSnsResolveMethodEnabled(local_state())); + GURL sns_uri("https://brave.sol"); + ASSERT_FALSE(HandleIPFSURLRewrite(&sns_uri, browser_context())); + local_state()->SetInteger( + decentralized_dns::kSnsResolveMethod, + static_cast(decentralized_dns::ResolveMethodTypes::ENABLED)); + EXPECT_TRUE(decentralized_dns::IsSnsResolveMethodEnabled(local_state())); + ASSERT_TRUE(HandleIPFSURLRewrite(&sns_uri, browser_context())); + + EXPECT_FALSE(decentralized_dns::IsUnstoppableDomainsResolveMethodEnabled( + local_state())); + GURL ud_uri("https://brave.crypto"); + ASSERT_FALSE(HandleIPFSURLRewrite(&ud_uri, browser_context())); + local_state()->SetInteger( + decentralized_dns::kUnstoppableDomainsResolveMethod, + static_cast(decentralized_dns::ResolveMethodTypes::ENABLED)); + EXPECT_TRUE(decentralized_dns::IsUnstoppableDomainsResolveMethodEnabled( + local_state())); + ASSERT_TRUE(HandleIPFSURLRewrite(&ud_uri, browser_context())); + + profile()->GetPrefs()->SetInteger( + kIPFSResolveMethod, static_cast(IPFSResolveMethodTypes::IPFS_ASK)); + ASSERT_FALSE(HandleIPFSURLRewrite(&ens_uri, browser_context())); + ASSERT_FALSE(HandleIPFSURLRewrite(&sns_uri, browser_context())); + ASSERT_FALSE(HandleIPFSURLRewrite(&ud_uri, browser_context())); } TEST_F(ContentBrowserClientHelperUnitTest, HandleIPNSURLRewriteLocal) {