Skip to content

Commit

Permalink
Move eTLDPlusOne to brave_wallet_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Mar 26, 2022
1 parent b6d79d9 commit e926740
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ source_set("brave_wallet_delegate") {
"//brave/components/brave_wallet/browser",
"//brave/components/brave_wallet/browser:constants",
"//brave/components/brave_wallet/browser:ethereum_permission_utils",
"//brave/components/brave_wallet/browser:utils",
"//brave/components/brave_wallet/common:mojom",
"//brave/components/resources:strings_grit",
"//components/content_settings/core/common",
Expand Down
2 changes: 1 addition & 1 deletion browser/brave_wallet/brave_wallet_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/common/brave_paths.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/ethereum_permission_utils.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/features.h"
#include "chrome/browser/profiles/profile.h"
Expand Down
2 changes: 1 addition & 1 deletion browser/brave_wallet/brave_wallet_service_delegate_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <utility>

#include "brave/components/brave_wallet/browser/ethereum_permission_utils.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/permissions/contexts/brave_ethereum_permission_context.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
Expand Down
4 changes: 2 additions & 2 deletions components/brave_wallet/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ static_library("browser") {
"//brave/components/bls/rs:cxx",
"//brave/components/brave_component_updater/browser",
"//brave/components/brave_stats/browser",
"//brave/components/brave_wallet/browser:ethereum_permission_utils",
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:common_constants",
"//brave/components/brave_wallet/common:mojom",
Expand Down Expand Up @@ -150,8 +149,8 @@ static_library("ethereum_permission_utils") {
]

deps = [
":utils",
"//base",
"//net",
"//third_party/re2",
"//url",
]
Expand Down Expand Up @@ -196,6 +195,7 @@ source_set("utils") {
"//brave/vendor/bip39wally-core-native:bip39wally-core",
"//components/prefs",
"//crypto",
"//net",
"//third_party/abseil-cpp:absl",
"//third_party/boringssl",
"//url",
Expand Down
6 changes: 6 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "crypto/random.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/boringssl/src/include/openssl/evp.h"
#include "url/gurl.h"
Expand Down Expand Up @@ -948,4 +949,9 @@ absl::optional<std::string> GetPrefKeyForCoinType(mojom::CoinType coin) {
return absl::nullopt;
}

std::string eTLDPlusOne(const GURL& url) {
return net::registry_controlled_domains::GetDomainAndRegistry(
url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
}

} // namespace brave_wallet
5 changes: 5 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ GURL GetFirstValidChainURL(const std::vector<std::string>& chain_urls);

absl::optional<std::string> GetPrefKeyForCoinType(mojom::CoinType coin);

/**
* Given an url, return eTLD + 1 for that URL
*/
std::string eTLDPlusOne(const GURL& url);

} // namespace brave_wallet

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_UTILS_H_
10 changes: 10 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1036,4 +1036,14 @@ TEST(BraveWalletUtilsUnitTest, GetCurrentChainId) {
mojom::kFilecoinMainnet);
}

TEST(BraveWalletUtilsUnitTest, eTLDPlusOne) {
EXPECT_EQ("", eTLDPlusOne(GURL()));
EXPECT_EQ("brave.com", eTLDPlusOne(GURL("https://blog.brave.com")));
EXPECT_EQ("brave.com", eTLDPlusOne(GURL("https://...brave.com")));
EXPECT_EQ("brave.com", eTLDPlusOne(GURL("https://a.b.c.d.brave.com/1")));
EXPECT_EQ("brave.github.io",
eTLDPlusOne(GURL("https://a.b.brave.github.io/example")));
EXPECT_EQ("", eTLDPlusOne(GURL("https://github.io")));
}

} // namespace brave_wallet
7 changes: 1 addition & 6 deletions components/brave_wallet/browser/ethereum_permission_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "third_party/re2/src/re2/re2.h"
#include "url/gurl.h"
#include "url/origin.h"
Expand Down Expand Up @@ -136,11 +136,6 @@ bool GetSubRequestOrigin(const GURL& old_origin,
return AddAccountToHost(old_origin, account, new_origin);
}

std::string eTLDPlusOne(const GURL& url) {
return net::registry_controlled_domains::GetDomainAndRegistry(
url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
}

GURL GetConnectWithSiteWebUIURL(const GURL& webui_base_url,
const std::vector<std::string>& accounts,
const std::string& origin) {
Expand Down
5 changes: 0 additions & 5 deletions components/brave_wallet/browser/ethereum_permission_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ bool GetSubRequestOrigin(const GURL& old_origin,
const std::string& account,
GURL* new_origin);

/**
* Given an url, return eTLD + 1 for that URL
*/
std::string eTLDPlusOne(const GURL& url);

/**
* Given accounts, and origin, return the WebUI URL for connecting with site
* (ethereum permission) request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,6 @@ TEST(EthereumPermissionUtilsUnitTest, GetSubRequestOrigin) {
EXPECT_EQ(new_origin, expected_new_origin_with_port);
}

TEST(EthereumPermissionUtilsUnitTest, eTLDPlusOne) {
EXPECT_EQ("", eTLDPlusOne(GURL()));
EXPECT_EQ("brave.com", eTLDPlusOne(GURL("https://blog.brave.com")));
EXPECT_EQ("brave.com", eTLDPlusOne(GURL("https://...brave.com")));
EXPECT_EQ("brave.com", eTLDPlusOne(GURL("https://a.b.c.d.brave.com/1")));
EXPECT_EQ("brave.github.io",
eTLDPlusOne(GURL("https://a.b.brave.github.io/example")));
EXPECT_EQ("", eTLDPlusOne(GURL("https://github.io")));
}

TEST(EthereumPermissionUtilsUnitTest, GetConnectWithSiteWebUIURL) {
GURL base_url("chrome://wallet-panel.top-chrome/");
std::vector<std::string> addrs = {
Expand Down

0 comments on commit e926740

Please sign in to comment.