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 authored and Douglashdaniel committed Mar 30, 2022
1 parent c9a600e commit a369c51
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ source_set("brave_wallet_delegate") {
"//brave/browser/brave_wallet",
"//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
5 changes: 3 additions & 2 deletions 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 Expand Up @@ -163,8 +163,9 @@ void BraveWalletServiceDelegateImpl::TabChangedAt(

void BraveWalletServiceDelegateImpl::FireActiveOriginChanged() {
const std::string origin = GetActiveOriginInternal();
const std::string etld_plus_one = eTLDPlusOne(GURL(origin));
for (auto& observer : observer_list_)
observer.OnActiveOriginChanged(origin, eTLDPlusOne(GURL(origin)));
observer.OnActiveOriginChanged(origin, etld_plus_one);
}

std::string BraveWalletServiceDelegateImpl::GetActiveOriginInternal() {
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 @@ -119,7 +119,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 @@ -154,8 +153,8 @@ static_library("ethereum_permission_utils") {
]

deps = [
":utils",
"//base",
"//net",
"//third_party/re2",
"//url",
]
Expand Down Expand Up @@ -200,6 +199,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
6 changes: 3 additions & 3 deletions components/brave_wallet_ui/common/actions/wallet_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
NewUnapprovedTxAdded,
UnapprovedTxUpdated,
TransactionStatusChanged,
ActiveOriginChanged,
DefaultWalletChanged,
DefaultBaseCurrencyChanged,
DefaultBaseCryptocurrencyChanged,
Expand All @@ -38,7 +37,8 @@ import {
WalletInfoBase,
WalletInfo,
DefaultCurrencies,
GetPriceReturnInfo
GetPriceReturnInfo,
OriginInfo
} from '../../constants/types'

export const initialize = createAction('initialize')
Expand Down Expand Up @@ -89,7 +89,7 @@ export const rejectAllTransactions = createAction('rejectAllTransactions')
export const setAccountTransactions = createAction<AccountTransactions>('setAccountTransactions')
export const defaultWalletUpdated = createAction<BraveWallet.DefaultWallet>('defaultWalletUpdated')
export const setSelectedAccount = createAction<WalletAccountType>('setSelectedAccount')
export const activeOriginChanged = createAction<ActiveOriginChanged>('activeOriginChanged')
export const activeOriginChanged = createAction<OriginInfo>('activeOriginChanged')
export const refreshGasEstimates = createAction('refreshGasEstimates')
export const setGasEstimates = createAction<BraveWallet.GasEstimation1559>('setGasEstimates')
export const updateUnapprovedTransactionGasFields = createAction<UpdateUnapprovedTransactionGasFieldsType>('updateUnapprovedTransactionGasFields')
Expand Down
5 changes: 0 additions & 5 deletions components/brave_wallet_ui/common/constants/action_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ export type SwapParamsPayloadType = {
full: boolean
}

export type ActiveOriginChanged = {
origin: string
eTldPlusOne: string
}

export type UpdateUnapprovedTransactionGasFieldsType = {
txMetaId: string
gasLimit: string
Expand Down
6 changes: 3 additions & 3 deletions components/brave_wallet_ui/common/reducers/wallet_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
WalletInfo,
DefaultCurrencies,
GetPriceReturnInfo,
GetNativeAssetBalancesPayload
GetNativeAssetBalancesPayload,
OriginInfo
} from '../../constants/types'
import {
ActiveOriginChanged,
IsEip1559Changed,
NewUnapprovedTxAdded,
SetTransactionProviderErrorType,
Expand Down Expand Up @@ -370,7 +370,7 @@ export const createWalletReducer = (initialState: WalletState) => {
}
})

reducer.on(WalletActions.activeOriginChanged, (state: WalletState, payload: ActiveOriginChanged): WalletState => {
reducer.on(WalletActions.activeOriginChanged, (state: WalletState, payload: OriginInfo): WalletState => {
return {
...state,
activeOrigin: payload
Expand Down

0 comments on commit a369c51

Please sign in to comment.