Skip to content

Commit

Permalink
edit known networks
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassive committed May 31, 2022
1 parent dcb9f79 commit dec5af0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 18 deletions.
15 changes: 13 additions & 2 deletions browser/ui/webui/settings/brave_wallet_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#include <vector>

#include "base/bind.h"
#include "base/feature_list.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/json_rpc_service.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/brave_wallet/common/value_conversion_utils.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -69,8 +71,17 @@ void BraveWalletHandler::GetCustomNetworksList(const base::Value::List& args) {
CHECK_EQ(args.size(), 1U);
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
base::Value list(base::Value::Type::LIST);
for (const auto& it : brave_wallet::GetAllEthCustomChains(prefs)) {
list.Append(brave_wallet::EthNetworkInfoToValue(*it));

if (base::FeatureList::IsEnabled(
brave_wallet::features::kBraveWalletEditKnownNetworksFeature)) {
for (const auto& it : brave_wallet::GetAllChains(
prefs, brave_wallet::mojom::CoinType::ETH)) {
list.Append(brave_wallet::EthNetworkInfoToValue(*it));
}
} else {
for (const auto& it : brave_wallet::GetAllEthCustomChains(prefs)) {
list.Append(brave_wallet::EthNetworkInfoToValue(*it));
}
}
AllowJavascript();
ResolveJavascriptCallback(args[0], std::move(list));
Expand Down
39 changes: 32 additions & 7 deletions components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ const base::flat_map<std::string, std::string> kInfuraSubdomains = {
{brave_wallet::mojom::kGoerliChainId, "goerli"},
{brave_wallet::mojom::kKovanChainId, "kovan"}};

const base::flat_set<std::string> kInfuraChains = {
brave_wallet::mojom::kMainnetChainId,
brave_wallet::mojom::kPolygonMainnetChainId,
brave_wallet::mojom::kRinkebyChainId,
brave_wallet::mojom::kRopstenChainId,
brave_wallet::mojom::kGoerliChainId,
brave_wallet::mojom::kKovanChainId};

const base::flat_map<std::string, std::string> kSolanaSubdomains = {
{brave_wallet::mojom::kSolanaMainnet, "mainnet"},
{brave_wallet::mojom::kSolanaTestnet, "testnet"},
Expand All @@ -308,14 +316,13 @@ constexpr const char kEnsRegistryContractAddress[] =

std::string GetInfuraURLForKnownChainId(const std::string& chain_id) {
if (chain_id == brave_wallet::mojom::kPolygonMainnetChainId) {
return kPolygonMainnetEndpoint + GetInfuraProjectID();
return kPolygonMainnetEndpoint;
}

auto subdomain = brave_wallet::GetInfuraSubdomainForKnownChainId(chain_id);
if (subdomain.empty())
return std::string();
return base::StringPrintf("https://%s-infura.brave.com/%s", subdomain.c_str(),
GetInfuraProjectID().c_str());
return base::StringPrintf("https://%s-infura.brave.com/", subdomain.c_str());
}

const base::Value::List* GetEthCustomNetworksList(PrefService* prefs) {
Expand Down Expand Up @@ -352,6 +359,24 @@ std::vector<mojom::NetworkInfoPtr> MergeEthChains(

} // namespace

GURL AddInfuraProjectId(const GURL& url) {
DCHECK(url.is_valid()) << url.possibly_invalid_spec();
GURL::Replacements replacements;
auto path = GetInfuraProjectID();
replacements.SetPathStr(path);
return url.ReplaceComponents(replacements);
}

GURL MaybeAddInfuraProjectId(const GURL& url) {
DCHECK(url.is_valid()) << url.possibly_invalid_spec();
for (const auto& infura_chain_id : kInfuraChains) {
if (GetInfuraURLForKnownChainId(infura_chain_id) == url) {
return AddInfuraProjectId(url);
}
}
return url;
}

mojom::NetworkInfoPtr GetKnownEthChain(PrefService* prefs,
const std::string& chain_id) {
for (const auto& network : kKnownEthNetworks) {
Expand Down Expand Up @@ -840,11 +865,11 @@ GURL GetNetworkURL(PrefService* prefs,
const std::string& chain_id,
mojom::CoinType coin) {
if (coin == mojom::CoinType::ETH) {
// TODO(apaymyshev): reorder
if (auto custom_chain = GetCustomEthChain(prefs, chain_id)) {
return GetFirstValidChainURL(custom_chain->rpc_urls);
return MaybeAddInfuraProjectId(
GetFirstValidChainURL(custom_chain->rpc_urls));
} else if (auto known_chain = GetKnownEthChain(prefs, chain_id)) {
return GURL(known_chain->rpc_urls.front());
return MaybeAddInfuraProjectId(GURL(known_chain->rpc_urls.front()));
}
} else if (coin == mojom::CoinType::SOL) {
for (const auto& network : kKnownSolNetworks) {
Expand Down Expand Up @@ -1053,7 +1078,7 @@ std::string GetDefaultBaseCryptocurrency(PrefService* prefs) {
GURL GetUnstoppableDomainsRpcUrl(const std::string& chain_id) {
if (chain_id == brave_wallet::mojom::kMainnetChainId ||
chain_id == brave_wallet::mojom::kPolygonMainnetChainId) {
return GURL(GetInfuraURLForKnownChainId(chain_id));
return AddInfuraProjectId(GURL(GetInfuraURLForKnownChainId(chain_id)));
}

NOTREACHED();
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ GURL GetNetworkURL(PrefService* prefs,
const std::string& chain_id,
mojom::CoinType coin);
std::string GetInfuraSubdomainForKnownChainId(const std::string& chain_id);
GURL AddInfuraProjectId(const GURL& url);
GURL MaybeAddInfuraProjectId(const GURL& url);
mojom::NetworkInfoPtr GetKnownEthChain(PrefService* prefs,
const std::string& chain_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,12 @@ TEST(BraveWalletUtilsUnitTest, GetNetworkURLTest) {
values.push_back(EthNetworkInfoToValue(chain2));
UpdateCustomNetworks(&prefs, &values);
for (const auto& chain : GetAllKnownEthChains(&prefs)) {
EXPECT_EQ(GURL(chain->rpc_urls.front()),
// Brave proxies should have infura key added to path.
GURL rpc_url(chain->rpc_urls.front());
if (base::EndsWith(rpc_url.host(), "brave.com"))
rpc_url = AddInfuraProjectId(rpc_url);

EXPECT_EQ(rpc_url,
GetNetworkURL(&prefs, chain->chain_id, mojom::CoinType::ETH));
}
EXPECT_EQ(GURL(chain1.rpc_urls.front()),
Expand Down
4 changes: 2 additions & 2 deletions components/brave_wallet/browser/json_rpc_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void JsonRpcService::GetPendingAddChainRequests(
void JsonRpcService::AddEthereumChain(mojom::NetworkInfoPtr chain,
AddEthereumChainCallback callback) {
auto chain_id = chain->chain_id;
GURL url = GetFirstValidChainURL(chain->rpc_urls);
GURL url = MaybeAddInfuraProjectId(GetFirstValidChainURL(chain->rpc_urls));

if (!url.is_valid()) {
std::move(callback).Run(
Expand Down Expand Up @@ -314,7 +314,7 @@ void JsonRpcService::AddEthereumChainForOrigin(
l10n_util::GetStringUTF8(IDS_WALLET_ALREADY_IN_PROGRESS_ERROR));
return;
}
GURL url = GetFirstValidChainURL(chain->rpc_urls);
GURL url = MaybeAddInfuraProjectId(GetFirstValidChainURL(chain->rpc_urls));
if (!url.is_valid()) {
std::move(callback).Run(
chain->chain_id, mojom::ProviderError::kUserRejectedRequest,
Expand Down
11 changes: 5 additions & 6 deletions components/brave_wallet/browser/json_rpc_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ class JsonRpcServiceUnitTest : public testing::Test {
}

void SetUDENSInterceptor(const std::string& chain_id) {
GURL network_url =
brave_wallet::GetNetworkURL(prefs(), chain_id, mojom::CoinType::ETH);
GURL network_url = AddInfuraProjectId(
GetNetworkURL(prefs(), chain_id, mojom::CoinType::ETH));
ASSERT_TRUE(network_url.is_valid());
url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
[&, network_url](const network::ResourceRequest& request) {
Expand Down Expand Up @@ -458,8 +458,7 @@ class JsonRpcServiceUnitTest : public testing::Test {
net::HttpStatusCode supports_interface_status = net::HTTP_OK,
net::HttpStatusCode token_uri_status = net::HTTP_OK,
net::HttpStatusCode metadata_status = net::HTTP_OK) {
GURL network_url =
brave_wallet::GetNetworkURL(prefs(), chain_id, mojom::CoinType::ETH);
GURL network_url = GetNetworkURL(prefs(), chain_id, mojom::CoinType::ETH);
ASSERT_TRUE(network_url.is_valid());
url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
[&, interface_id, supports_interface_provider_response,
Expand Down Expand Up @@ -1867,12 +1866,12 @@ class UnstoppableDomainsUnitTest : public JsonRpcServiceUnitTest {
void SetResponse(const GURL& rpc_url, const std::string& response) {
if (response.empty()) {
EXPECT_TRUE(url_loader_factory_.SimulateResponseForPendingRequest(
rpc_url.spec(), "", net::HTTP_REQUEST_TIMEOUT));
AddInfuraProjectId(rpc_url).spec(), "", net::HTTP_REQUEST_TIMEOUT));
return;
}

EXPECT_TRUE(url_loader_factory_.SimulateResponseForPendingRequest(
rpc_url.spec(), response, net::HTTP_OK));
AddInfuraProjectId(rpc_url).spec(), response, net::HTTP_OK));
}
};

Expand Down
3 changes: 3 additions & 0 deletions components/brave_wallet/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ const base::Feature kBraveWalletSolanaFeature{
const base::Feature kBraveWalletSolanaProviderFeature{
"BraveWalletSolanaProvider", base::FEATURE_DISABLED_BY_DEFAULT};

const base::Feature kBraveWalletEditKnownNetworksFeature{
"BraveWalletEditKnownNetworks", base::FEATURE_DISABLED_BY_DEFAULT};

} // namespace features
} // namespace brave_wallet
1 change: 1 addition & 0 deletions components/brave_wallet/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern const base::Feature kNativeBraveWalletFeature;
extern const base::Feature kBraveWalletFilecoinFeature;
extern const base::Feature kBraveWalletSolanaFeature;
extern const base::Feature kBraveWalletSolanaProviderFeature;
extern const base::Feature kBraveWalletEditKnownNetworksFeature;

} // namespace features
} // namespace brave_wallet
Expand Down

0 comments on commit dec5af0

Please sign in to comment.