Skip to content

Commit

Permalink
Uplift of #27199 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Jan 15, 2025
1 parent 7d3e983 commit a610ba5
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 13 deletions.
39 changes: 39 additions & 0 deletions browser/net/decentralized_dns_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
auto eth_spec = brave_wallet::NetworkManager::GetUnstoppableDomainsRpcUrl(
brave_wallet::mojom::kMainnetChainId)
.spec();
auto base_spec = brave_wallet::NetworkManager::GetUnstoppableDomainsRpcUrl(
brave_wallet::mojom::kBaseMainnetChainId)
.spec();

// No redirect for failed requests.
EXPECT_EQ(net::ERR_IO_PENDING,
Expand All @@ -236,6 +239,11 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
brave_wallet::MakeJsonRpcStringArrayResponse(
{"", "", "", "", "", "https://brave.com"}),
net::HTTP_REQUEST_TIMEOUT);
test_url_loader_factory().SimulateResponseForPendingRequest(
base_spec,
brave_wallet::MakeJsonRpcStringArrayResponse(
{"", "", "", "", "", "https://brave.com"}),
net::HTTP_REQUEST_TIMEOUT);
task_environment_.RunUntilIdle();
EXPECT_TRUE(brave_request_info->new_url_spec.empty());

Expand All @@ -248,6 +256,11 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
brave_wallet::MakeJsonRpcStringArrayResponse(
{"", "", "", "", "", "https://brave.com"}),
net::HTTP_OK);
test_url_loader_factory().SimulateResponseForPendingRequest(
base_spec,
brave_wallet::MakeJsonRpcStringArrayResponse(
{"", "", "", "", "", "https://brave.com/base"}),
net::HTTP_OK);
test_url_loader_factory().SimulateResponseForPendingRequest(
eth_spec,
brave_wallet::MakeJsonRpcStringArrayResponse(
Expand All @@ -257,6 +270,28 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
task_environment_.RunUntilIdle();
EXPECT_EQ(brave_request_info->new_url_spec, "https://brave.com/");

// Base result.
EXPECT_EQ(net::ERR_IO_PENDING,
OnBeforeURLRequest_DecentralizedDnsPreRedirectWork(
base::DoNothing(), brave_request_info));
test_url_loader_factory().SimulateResponseForPendingRequest(
polygon_spec,
brave_wallet::MakeJsonRpcStringArrayResponse({"", "", "", "", "", ""}),
net::HTTP_OK);
test_url_loader_factory().SimulateResponseForPendingRequest(
base_spec,
brave_wallet::MakeJsonRpcStringArrayResponse(
{"", "", "", "", "", "https://brave.com/base"}),
net::HTTP_OK);
test_url_loader_factory().SimulateResponseForPendingRequest(
eth_spec,
brave_wallet::MakeJsonRpcStringArrayResponse(
{"QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR", "", "", "", "",
""}),
net::HTTP_OK);
task_environment_.RunUntilIdle();
EXPECT_EQ(brave_request_info->new_url_spec, "https://brave.com/base");

// Eth result.
EXPECT_EQ(net::ERR_IO_PENDING,
OnBeforeURLRequest_DecentralizedDnsPreRedirectWork(
Expand All @@ -265,6 +300,10 @@ TEST_F(DecentralizedDnsNetworkDelegateHelperTest,
polygon_spec,
brave_wallet::MakeJsonRpcStringArrayResponse({"", "", "", "", "", ""}),
net::HTTP_OK);
test_url_loader_factory().SimulateResponseForPendingRequest(
base_spec,
brave_wallet::MakeJsonRpcStringArrayResponse({"", "", "", "", "", ""}),
net::HTTP_OK);
test_url_loader_factory().SimulateResponseForPendingRequest(
eth_spec,
brave_wallet::MakeJsonRpcStringArrayResponse(
Expand Down
15 changes: 9 additions & 6 deletions components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ bool IsValidEntropySize(size_t entropy_size) {
return true;
}

// Addresses taken from https://docs.unstoppabledomains.com/developer-toolkit/
// smart-contracts/uns-smart-contracts/#proxyreader
const base::flat_map<std::string, std::string>
const base::flat_map<std::string_view, std::string_view>
kUnstoppableDomainsProxyReaderContractAddressMap = {
// https://github.com/unstoppabledomains/uns/blob/abd9e12409094dd6ea8611ebffdade8db49c4b56/uns-config.json#L76
{brave_wallet::mojom::kMainnetChainId,
"0xc3C2BAB5e3e52DBF311b2aAcEf2e40344f19494E"},
"0x578853aa776Eef10CeE6c4dd2B5862bdcE767A8B"},
// https://github.com/unstoppabledomains/uns/blob/abd9e12409094dd6ea8611ebffdade8db49c4b56/uns-config.json#L221
{brave_wallet::mojom::kPolygonMainnetChainId,
"0xA3f32c8cd786dc089Bd1fC175F2707223aeE5d00"}};
"0x91EDd8708062bd4233f4Dd0FCE15A7cb4d500091"},
// https://github.com/unstoppabledomains/uns/blob/abd9e12409094dd6ea8611ebffdade8db49c4b56/uns-config.json#L545
{brave_wallet::mojom::kBaseMainnetChainId,
"0x78c4b414e1abdf0de267deda01dffd4cd0817a16"}};

constexpr const char kEnsRegistryContractAddress[] =
"0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
Expand Down Expand Up @@ -550,7 +553,7 @@ std::string GetDefaultBaseCryptocurrency(PrefService* prefs) {
return prefs->GetString(kDefaultBaseCryptocurrency);
}

std::string GetUnstoppableDomainsProxyReaderContractAddress(
std::string_view GetUnstoppableDomainsProxyReaderContractAddress(
std::string_view chain_id) {
std::string chain_id_lower = base::ToLowerASCII(chain_id);
if (kUnstoppableDomainsProxyReaderContractAddressMap.contains(
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/brave_wallet_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void SetDefaultBaseCryptocurrency(PrefService* prefs,
std::string_view cryptocurrency);
std::string GetDefaultBaseCryptocurrency(PrefService* prefs);

std::string GetUnstoppableDomainsProxyReaderContractAddress(
std::string_view GetUnstoppableDomainsProxyReaderContractAddress(
std::string_view chain_id);
std::string GetEnsRegistryContractAddress(std::string_view chain_id);

Expand Down
11 changes: 6 additions & 5 deletions components/brave_wallet/browser/json_rpc_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1715,8 +1715,9 @@ void JsonRpcService::UnstoppableDomainsResolveDns(
base::BindOnce(&JsonRpcService::OnUnstoppableDomainsResolveDns,
weak_ptr_factory_.GetWeakPtr(), domain, chain_id);
auto eth_call = eth::eth_call(
"", GetUnstoppableDomainsProxyReaderContractAddress(chain_id), "", "",
"", *data, kEthereumBlockTagLatest);
"",
std::string(GetUnstoppableDomainsProxyReaderContractAddress(chain_id)),
"", "", "", *data, kEthereumBlockTagLatest);
RequestInternal(std::move(eth_call), true,
NetworkManager::GetUnstoppableDomainsRpcUrl(chain_id),
std::move(internal_callback));
Expand Down Expand Up @@ -1787,9 +1788,9 @@ void JsonRpcService::UnstoppableDomainsGetWalletAddr(
auto internal_callback =
base::BindOnce(&JsonRpcService::OnUnstoppableDomainsGetWalletAddr,
weak_ptr_factory_.GetWeakPtr(), key, chain_id);
auto eth_call =
eth::eth_call(GetUnstoppableDomainsProxyReaderContractAddress(chain_id),
ToHex(call_data));
auto eth_call = eth::eth_call(
std::string(GetUnstoppableDomainsProxyReaderContractAddress(chain_id)),
ToHex(call_data));
RequestInternal(std::move(eth_call), true,
NetworkManager::GetUnstoppableDomainsRpcUrl(chain_id),
std::move(internal_callback));
Expand Down
82 changes: 82 additions & 0 deletions components/brave_wallet/browser/json_rpc_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3233,6 +3233,14 @@ class UnstoppableDomainsUnitTest : public JsonRpcServiceUnitTest {
polygon_endpoint_handler_->AddEthCallHandler(
polygon_getmany_call_handler_.get());

base_endpoint_handler_ = std::make_unique<JsonRpcEndpointHandler>(
NetworkManager::GetUnstoppableDomainsRpcUrl(
mojom::kBaseMainnetChainId));
base_getmany_call_handler_ = std::make_unique<UDGetManyCallHandler>(
EthAddress::FromHex(GetUnstoppableDomainsProxyReaderContractAddress(
mojom::kBaseMainnetChainId)));
base_endpoint_handler_->AddEthCallHandler(base_getmany_call_handler_.get());

url_loader_factory_.SetInterceptor(base::BindRepeating(
&UnstoppableDomainsUnitTest::HandleRequest, base::Unretained(this)));
}
Expand All @@ -3245,6 +3253,10 @@ class UnstoppableDomainsUnitTest : public JsonRpcServiceUnitTest {
static constexpr char k0x3a2f3fAddr[] =
"0x3a2f3f7aab82d69036763cfd3f755975f84496e6";

// Base: test.bald -> 0x1111111111111111111111111111111111111111
static constexpr char k0x111111Addr[] =
"0x1111111111111111111111111111111111111111";

void SetEthResponse(const std::string& domain, const std::string& response) {
eth_mainnet_getmany_call_handler_->Reset();
eth_mainnet_getmany_call_handler_->AddItem(domain, "crypto.ETH.address",
Expand Down Expand Up @@ -3272,6 +3284,18 @@ class UnstoppableDomainsUnitTest : public JsonRpcServiceUnitTest {
polygon_getmany_call_handler_->Reset();
polygon_getmany_call_handler_->SetRawResponse("timeout");
}
void SetBaseResponse(const std::string& domain, const std::string& response) {
base_getmany_call_handler_->Reset();
base_getmany_call_handler_->AddItem(domain, "crypto.ETH.address", response);
}
void SetBaseRawResponse(const std::string& response) {
base_getmany_call_handler_->Reset();
base_getmany_call_handler_->SetRawResponse(response);
}
void SetBaseTimeoutResponse() {
base_getmany_call_handler_->Reset();
base_getmany_call_handler_->SetRawResponse("timeout");
}

std::string DnsIpfsResponse() const {
return MakeJsonRpcStringArrayResponse(
Expand Down Expand Up @@ -3299,9 +3323,11 @@ class UnstoppableDomainsUnitTest : public JsonRpcServiceUnitTest {
protected:
std::unique_ptr<JsonRpcEndpointHandler> eth_mainnet_endpoint_handler_;
std::unique_ptr<JsonRpcEndpointHandler> polygon_endpoint_handler_;
std::unique_ptr<JsonRpcEndpointHandler> base_endpoint_handler_;

std::unique_ptr<UDGetManyCallHandler> eth_mainnet_getmany_call_handler_;
std::unique_ptr<UDGetManyCallHandler> polygon_getmany_call_handler_;
std::unique_ptr<UDGetManyCallHandler> base_getmany_call_handler_;

void HandleRequest(const network::ResourceRequest& request) {
url_loader_factory_.ClearResponses();
Expand All @@ -3320,6 +3346,13 @@ class UnstoppableDomainsUnitTest : public JsonRpcServiceUnitTest {
} else {
url_loader_factory_.AddResponse(request.url.spec(), *response);
}
} else if ((response = base_endpoint_handler_->HandleRequest(request))) {
if (response == "timeout") {
url_loader_factory_.AddResponse(request.url.spec(), "",
net::HTTP_REQUEST_TIMEOUT);
} else {
url_loader_factory_.AddResponse(request.url.spec(), *response);
}
} else {
url_loader_factory_.AddResponse(request.url.spec(), "",
net::HTTP_INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -3389,6 +3422,25 @@ TEST_F(UnstoppableDomainsUnitTest, GetWalletAddr_PolygonResult) {
task_environment_.RunUntilIdle();
}

TEST_F(UnstoppableDomainsUnitTest, GetWalletAddr_BaseResult) {
base::MockCallback<GetWalletAddrCallback> callback;
EXPECT_CALL(callback, Run(k0x111111Addr, mojom::ProviderError::kSuccess, ""));
SetEthResponse("javajobs.crypto", "");
SetPolygonResponse("javajobs.crypto", "");
SetBaseResponse("javajobs.crypto", k0x111111Addr);
json_rpc_service_->UnstoppableDomainsGetWalletAddr(
"javajobs.crypto", MakeToken(), callback.Get());
WaitAndVerify(&callback);

EXPECT_CALL(callback, Run(k0x111111Addr, mojom::ProviderError::kSuccess, ""));
SetEthResponse("javajobs.crypto", k0x8aaD44Addr);
SetPolygonResponse("javajobs.crypto", "");
SetBaseResponse("javajobs.crypto", k0x111111Addr);
json_rpc_service_->UnstoppableDomainsGetWalletAddr(
"javajobs.crypto", MakeToken(), callback.Get());
WaitAndVerify(&callback);
}

TEST_F(UnstoppableDomainsUnitTest, GetWalletAddr_FallbackToEthMainnet) {
base::MockCallback<GetWalletAddrCallback> callback;
EXPECT_CALL(callback, Run(k0x8aaD44Addr, mojom::ProviderError::kSuccess, ""));
Expand Down Expand Up @@ -3571,6 +3623,36 @@ TEST_F(UnstoppableDomainsUnitTest, ResolveDns_PolygonResult) {
WaitAndVerify(&callback);
}

TEST_F(UnstoppableDomainsUnitTest, ResolveDns_BaseResult) {
base::MockCallback<ResolveDnsCallback> callback;
EXPECT_CALL(callback, Run(std::optional<GURL>("https://brave.com"),
mojom::ProviderError::kSuccess, ""));
SetEthTimeoutResponse();
SetPolygonRawResponse(DnsEmptyResponse());
SetBaseRawResponse(DnsBraveResponse());
json_rpc_service_->UnstoppableDomainsResolveDns("brave.crypto",
callback.Get());
WaitAndVerify(&callback);

EXPECT_CALL(callback, Run(std::optional<GURL>("https://brave.com"),
mojom::ProviderError::kSuccess, ""));
SetEthRawResponse(DnsIpfsResponse());
SetPolygonRawResponse(DnsEmptyResponse());
SetBaseRawResponse(DnsBraveResponse());
json_rpc_service_->UnstoppableDomainsResolveDns("brave.crypto",
callback.Get());
WaitAndVerify(&callback);

EXPECT_CALL(callback, Run(std::optional<GURL>("https://brave.com"),
mojom::ProviderError::kSuccess, ""));
SetEthRawResponse(DnsEmptyResponse());
SetPolygonRawResponse(DnsEmptyResponse());
SetBaseRawResponse(DnsBraveResponse());
json_rpc_service_->UnstoppableDomainsResolveDns("brave.crypto",
callback.Get());
WaitAndVerify(&callback);
}

TEST_F(UnstoppableDomainsUnitTest, ResolveDns_FallbackToEthMainnet) {
base::MockCallback<ResolveDnsCallback> callback;
EXPECT_CALL(
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/network_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,8 @@ GURL NetworkManager::GetUnstoppableDomainsRpcUrl(std::string_view chain_id) {
return GetEthMainnet()->rpc_endpoints.front();
} else if (chain_id == mojom::kPolygonMainnetChainId) {
return GetPolygonMainnet()->rpc_endpoints.front();
} else if (chain_id == mojom::kBaseMainnetChainId) {
return GetBaseMainnet()->rpc_endpoints.front();
}

NOTREACHED();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ MultichainCall<ResultType>::GetEffectiveResponse() {
return nullptr;
}

auto base_result = responses_.find(mojom::kBaseMainnetChainId);
if (base_result == responses_.end()) {
return nullptr;
}

auto eth_mainnet_result = responses_.find(mojom::kMainnetChainId);
if (eth_mainnet_result == responses_.end()) {
return nullptr;
Expand All @@ -63,6 +68,10 @@ MultichainCall<ResultType>::GetEffectiveResponse() {
return &polygon_result->second;
}

if (base_result->second.result || base_result->second.error) {
return &base_result->second;
}

return &eth_mainnet_result->second;
}

Expand All @@ -86,7 +95,8 @@ bool MultichainCall<ResultType>::MaybeResolveCallbacks() {
template <class KeyType, class ResultType>
std::vector<std::string> MultichainCalls<KeyType, ResultType>::GetChains()
const {
return {mojom::kPolygonMainnetChainId, mojom::kMainnetChainId};
return {mojom::kPolygonMainnetChainId, mojom::kBaseMainnetChainId,
mojom::kMainnetChainId};
}

template <class KeyType, class ResultType>
Expand Down
Loading

0 comments on commit a610ba5

Please sign in to comment.