Skip to content

Commit

Permalink
Allow changing to custom RPC urls for known chains (#13480)
Browse files Browse the repository at this point in the history
* some refactoring

* edit known networks

* add tests
  • Loading branch information
supermassive authored Jun 2, 2022
1 parent 5909ae8 commit 3e6c82e
Show file tree
Hide file tree
Showing 18 changed files with 473 additions and 397 deletions.
45 changes: 17 additions & 28 deletions browser/brave_wallet/brave_wallet_ethereum_chain_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,18 @@ class BraveWalletEthereumChainTest : public InProcessBrowserTest {
->GetServiceForContext(browser()->profile());
}

std::vector<brave_wallet::mojom::NetworkInfoPtr> GetAllEthCustomChains() {
return brave_wallet::GetAllEthCustomChains(
browser()->profile()->GetPrefs());
}

private:
std::unique_ptr<net::EmbeddedTestServer> https_server_;
base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest, AddEthereumChainApproved) {
std::vector<brave_wallet::mojom::NetworkInfoPtr> result;
auto* prefs = browser()->profile()->GetPrefs();
brave_wallet::GetAllEthCustomChains(prefs, &result);
ASSERT_TRUE(result.empty());
ASSERT_TRUE(GetAllEthCustomChains().empty());
GURL url = GetWalletEthereumChainPageURL();
base::RunLoop loop;
content::WebContents* contents =
Expand All @@ -206,9 +208,8 @@ IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest, AddEthereumChainApproved) {
auto result_first = EvalJs(contents, kScriptWaitForEvent,
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY);
EXPECT_EQ(base::Value(true), result_first.value);
brave_wallet::GetAllEthCustomChains(prefs, &result);
ASSERT_FALSE(result.empty());
EXPECT_EQ(result.front()->chain_id, kSomeChainId);
ASSERT_FALSE(GetAllEthCustomChains().empty());
EXPECT_EQ(GetAllEthCustomChains().front()->chain_id, kSomeChainId);
EXPECT_EQ(GetJsonRpcService()->GetChainId(brave_wallet::mojom::CoinType::ETH),
kSomeChainId);
}
Expand Down Expand Up @@ -290,10 +291,7 @@ IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest,

IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest,
AddDifferentChainsNoSwitch) {
std::vector<brave_wallet::mojom::NetworkInfoPtr> result;
auto* prefs = browser()->profile()->GetPrefs();
brave_wallet::GetAllEthCustomChains(prefs, &result);
ASSERT_TRUE(result.empty());
ASSERT_TRUE(GetAllEthCustomChains().empty());

GURL urlA = GetWalletEthereumChainPageURL();
content::WebContents* contentsA =
Expand Down Expand Up @@ -335,19 +333,15 @@ IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest,
EXPECT_EQ(base::Value(false), rejected_same_id.value);
base::RunLoop().RunUntilIdle();
// Chain should still exist though
brave_wallet::GetAllEthCustomChains(prefs, &result);
ASSERT_FALSE(result.empty());
EXPECT_EQ(result.front()->chain_id, "0x11");
ASSERT_FALSE(GetAllEthCustomChains().empty());
EXPECT_EQ(GetAllEthCustomChains().front()->chain_id, "0x11");
// But current chain should not change
EXPECT_EQ(GetJsonRpcService()->GetChainId(brave_wallet::mojom::CoinType::ETH),
"0x1");
}

IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest, AddDifferentChainsSwitch) {
std::vector<brave_wallet::mojom::NetworkInfoPtr> result;
auto* prefs = browser()->profile()->GetPrefs();
brave_wallet::GetAllEthCustomChains(prefs, &result);
ASSERT_TRUE(result.empty());
ASSERT_TRUE(GetAllEthCustomChains().empty());

GURL urlA = GetWalletEthereumChainPageURL();

Expand Down Expand Up @@ -388,9 +382,8 @@ IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest, AddDifferentChainsSwitch) {
content::EXECUTE_SCRIPT_USE_MANUAL_REPLY);
EXPECT_EQ(base::Value(true), rejected_same_id.value);
base::RunLoop().RunUntilIdle();
brave_wallet::GetAllEthCustomChains(prefs, &result);
ASSERT_FALSE(result.empty());
EXPECT_EQ(result.front()->chain_id, "0x11");
ASSERT_FALSE(GetAllEthCustomChains().empty());
EXPECT_EQ(GetAllEthCustomChains().front()->chain_id, "0x11");
EXPECT_EQ(GetJsonRpcService()->GetChainId(brave_wallet::mojom::CoinType::ETH),
"0x11");
}
Expand Down Expand Up @@ -425,15 +418,11 @@ IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest, AddChainAndCloseTab) {

ASSERT_TRUE(tab_helperB->IsShowingBubble());
browser()->tab_strip_model()->CloseSelectedTabs();
std::vector<brave_wallet::mojom::NetworkInfoPtr> result;
auto* prefs = browser()->profile()->GetPrefs();
brave_wallet::GetAllEthCustomChains(prefs, &result);
ASSERT_TRUE(result.empty());
ASSERT_TRUE(GetAllEthCustomChains().empty());
GetJsonRpcService()->AddEthereumChainRequestCompleted("0x11", true);
base::RunLoop().RunUntilIdle();
brave_wallet::GetAllEthCustomChains(prefs, &result);
ASSERT_FALSE(result.empty());
EXPECT_EQ(result.front()->chain_id, "0x11");
ASSERT_FALSE(GetAllEthCustomChains().empty());
EXPECT_EQ(GetAllEthCustomChains().front()->chain_id, "0x11");
}

IN_PROC_BROWSER_TEST_F(BraveWalletEthereumChainTest, AddBrokenChain) {
Expand Down
8 changes: 2 additions & 6 deletions browser/brave_wallet/brave_wallet_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -765,21 +765,17 @@ TEST_F(BraveWalletServiceUnitTest, DefaultAssets) {
}
}

std::vector<mojom::NetworkInfoPtr> chains;
GetAllKnownSolChains(&chains);
mojom::BlockchainTokenPtr sol_token = sol_token_->Clone();
for (const auto& chain : chains) {
for (const auto& chain : GetAllKnownSolChains()) {
std::vector<mojom::BlockchainTokenPtr> tokens;
sol_token->chain_id = chain->chain_id;
GetUserAssets(chain->chain_id, mojom::CoinType::SOL, &tokens);
EXPECT_EQ(tokens.size(), 1u) << chain->chain_id;
EXPECT_EQ(sol_token, tokens[0]) << chain->chain_id;
}

chains.clear();
GetAllKnownFilChains(&chains);
mojom::BlockchainTokenPtr fil_token = fil_token_->Clone();
for (const auto& chain : chains) {
for (const auto& chain : GetAllKnownFilChains()) {
std::vector<mojom::BlockchainTokenPtr> tokens;
fil_token->chain_id = chain->chain_id;
GetUserAssets(chain->chain_id, mojom::CoinType::FIL, &tokens);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Polymer({
}
},
addNewNetwork: function(payload) {
this.browserProxy_.addEthereumChain(JSON.stringify(payload))
this.browserProxy_.addEthereumChain(payload)
.then(([success, errorMessage]) => {
this.setSubmissionResult(success, errorMessage)
if (success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class SettingsWalletNetworksSubpage extends SettingsWalletNetworksSubpageBase {
this.browserProxy_.getCustomNetworksList().then(payload => {
if (!payload)
return
this.networks = JSON.parse(payload)
this.networks = payload
this.notifyKeylist()
})
}
Expand Down
92 changes: 30 additions & 62 deletions browser/ui/webui/settings/brave_wallet_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
#include <vector>

#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.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 All @@ -25,43 +25,8 @@
#include "content/public/browser/web_ui.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

void RemoveEthereumChain(PrefService* prefs,
const std::string& chain_id_to_remove) {
brave_wallet::RemoveCustomNetwork(prefs, chain_id_to_remove);
}

brave_wallet::mojom::NetworkInfoPtr GetEthereumChain(
const std::string& payload,
std::string* error_message) {
CHECK(error_message);
error_message->clear();
base::JSONReader::ValueWithError value_with_error =
base::JSONReader::ReadAndReturnValueWithError(
payload, base::JSON_PARSE_CHROMIUM_EXTENSIONS |
base::JSONParserOptions::JSON_PARSE_RFC);
absl::optional<base::Value>& records_v = value_with_error.value;
if (!records_v) {
*error_message = l10n_util::GetStringUTF8(
IDS_SETTINGS_WALLET_NETWORKS_SUMBISSION_FAILED);
return nullptr;
}

brave_wallet::mojom::NetworkInfoPtr chain =
brave_wallet::ValueToEthNetworkInfo(records_v.value());
if (!chain) {
*error_message = l10n_util::GetStringUTF8(
IDS_SETTINGS_WALLET_NETWORKS_SUMBISSION_FAILED);
return nullptr;
}
return chain;
}

} // namespace

BraveWalletHandler::BraveWalletHandler() {}
BraveWalletHandler::~BraveWalletHandler() {}
BraveWalletHandler::BraveWalletHandler() = default;
BraveWalletHandler::~BraveWalletHandler() = default;

void BraveWalletHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
Expand Down Expand Up @@ -98,23 +63,28 @@ void BraveWalletHandler::RemoveEthereumChain(const base::Value::List& args) {
CHECK_EQ(args.size(), 2U);
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
AllowJavascript();
::RemoveEthereumChain(prefs, args[1].GetString());
brave_wallet::RemoveCustomNetwork(prefs, args[1].GetString());
ResolveJavascriptCallback(args[0], base::Value(true));
}

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);
std::vector<brave_wallet::mojom::NetworkInfoPtr> custom_chains;
brave_wallet::GetAllEthCustomChains(prefs, &custom_chains);
for (const auto& it : custom_chains) {
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));
}
}
std::string json_string;
base::JSONWriter::Write(list, &json_string);
AllowJavascript();
ResolveJavascriptCallback(args[0], base::Value(json_string));
ResolveJavascriptCallback(args[0], std::move(list));
}

void BraveWalletHandler::OnAddEthereumChain(
Expand All @@ -134,28 +104,26 @@ void BraveWalletHandler::OnAddEthereumChain(
void BraveWalletHandler::AddEthereumChain(const base::Value::List& args) {
CHECK_EQ(args.size(), 2U);
AllowJavascript();
std::string error_message;
auto* json_rpc_service =
brave_wallet::JsonRpcServiceFactory::GetServiceForContext(
Profile::FromWebUI(web_ui()));

auto chain = GetEthereumChain(args[1].GetString(), &error_message);
if (chain && json_rpc_service) {
json_rpc_service->AddEthereumChain(
chain->Clone(),
base::BindOnce(&BraveWalletHandler::OnAddEthereumChain,
weak_ptr_factory_.GetWeakPtr(), args[0].Clone()));
brave_wallet::mojom::NetworkInfoPtr chain =
brave_wallet::ValueToEthNetworkInfo(args[1]);

if (!chain || !json_rpc_service) {
base::ListValue result;
result.Append(base::Value(false));
result.Append(base::Value(l10n_util::GetStringUTF8(
IDS_SETTINGS_WALLET_NETWORKS_SUMBISSION_FAILED)));
ResolveJavascriptCallback(args[0], std::move(result));
return;
}
auto message = error_message.empty()
? l10n_util::GetStringUTF8(
IDS_SETTINGS_WALLET_NETWORKS_SUMBISSION_FAILED)
: error_message;

base::ListValue result;
result.Append(base::Value(false));
result.Append(base::Value(message));
ResolveJavascriptCallback(args[0], std::move(result));
json_rpc_service->AddEthereumChain(
std::move(chain),
base::BindOnce(&BraveWalletHandler::OnAddEthereumChain,
weak_ptr_factory_.GetWeakPtr(), args[0].Clone()));
}

void BraveWalletHandler::SetActiveNetwork(const base::Value::List& args) {
Expand Down
Loading

0 comments on commit 3e6c82e

Please sign in to comment.