Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeHealht][wallet] Avoid NoDestructor in eth_data_builder.cc #26683

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 51 additions & 51 deletions components/brave_wallet/browser/eth_data_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,55 @@
#include <optional>

#include "base/check.h"
#include "base/containers/fixed_flat_map.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "brave/components/brave_wallet/browser/brave_wallet_constants.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/eth_abi_utils.h"
#include "brave/components/brave_wallet/common/fil_address.h"
#include "brave/components/brave_wallet/common/hash_utils.h"
#include "brave/components/brave_wallet/common/hex_utils.h"
#include "third_party/abseil-cpp/absl/strings/str_format.h"

namespace brave_wallet {

namespace {

constexpr auto kIdToVersionMappings =
base::MakeFixedFlatMap<std::string_view, std::string_view>({
{"0x1", "ERC20"},
{"0x38", "BEP20"},
{"0x63564c40", "HRC20"},
{"0x64", "XDAI"},
{"0x7a", "FUSE"},
{"0x89", "MATIC"},
{"0xa", "OP"},
{"0xa4b1", "AETH"},
{"0xa86a", "AVAX"},
{"0xfa", "FANTOM"},
});

bool IsValidHostLabelCharacter(char c, bool is_first_char) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || (!is_first_char && c == '-') || c == '_';
}

std::optional<std::string> ChainIdToVersion(const std::string& symbol,
const std::string& chain_id) {
static base::NoDestructor<std::map<std::string, std::string>> mapping({
{"0x1", "ERC20"},
{"0x38", "BEP20"},
{"0x63564c40", "HRC20"},
{"0x64", "XDAI"},
{"0x7a", "FUSE"},
{"0x89", "MATIC"},
{"0xa", "OP"},
{"0xa4b1", "AETH"},
{"0xa86a", "AVAX"},
{"0xfa", "FANTOM"},
});

std::optional<std::string_view> ChainIdToVersion(std::string_view symbol,
std::string_view chain_id) {
// Special case for crypto.FTM.version.OPERA.address.
if (symbol == "FTM" && chain_id == "0xfa") {
return "OPERA";
}

auto it = mapping->find(chain_id);
if (it != mapping->end()) {
return it->second;
auto it = kIdToVersionMappings.find(chain_id);
if (it == kIdToVersionMappings.end()) {
return std::nullopt;
}
return std::nullopt;
return it->second;
}

} // namespace
Expand All @@ -79,7 +81,7 @@ std::optional<std::vector<uint8_t>> Forward(const FilAddress& fil_address) {

namespace erc20 {

bool Transfer(const std::string& to_address,
bool Transfer(std::string_view to_address,
SergeyZhukovsky marked this conversation as resolved.
Show resolved Hide resolved
uint256_t amount,
std::string* data) {
const std::string function_hash =
Expand All @@ -97,7 +99,7 @@ bool Transfer(const std::string& to_address,
return ConcatHexStrings(hex_strings, data);
}

bool BalanceOf(const std::string& address, std::string* data) {
bool BalanceOf(std::string_view address, std::string* data) {
const std::string function_hash = GetFunctionHash("balanceOf(address)");
std::string params;
if (!brave_wallet::PadHexEncodedParameter(address, &params)) {
Expand All @@ -106,7 +108,7 @@ bool BalanceOf(const std::string& address, std::string* data) {
return brave_wallet::ConcatHexStrings(function_hash, params, data);
}

bool Approve(const std::string& spender_address,
bool Approve(std::string_view spender_address,
uint256_t amount,
std::string* data) {
const std::string function_hash = GetFunctionHash("approve(address,uint256)");
Expand All @@ -123,8 +125,8 @@ bool Approve(const std::string& spender_address,
return ConcatHexStrings(hex_strings, data);
}

bool Allowance(const std::string& owner_address,
const std::string& spender_address,
bool Allowance(std::string_view owner_address,
std::string_view spender_address,
std::string* data) {
const std::string function_hash =
GetFunctionHash("allowance(address,address)");
Expand All @@ -148,8 +150,8 @@ bool Allowance(const std::string& owner_address,
namespace erc721 {

bool TransferFromOrSafeTransferFrom(bool is_safe_transfer_from,
const std::string& from,
const std::string& to,
std::string_view from,
std::string_view to,
uint256_t token_id,
std::string* data) {
const std::string function_hash =
Expand Down Expand Up @@ -203,8 +205,8 @@ bool TokenUri(uint256_t token_id, std::string* data) {

namespace erc1155 {

bool SafeTransferFrom(const std::string& from,
const std::string& to,
bool SafeTransferFrom(std::string_view from,
std::string_view to,
uint256_t token_id,
uint256_t value,
std::string* data) {
Expand Down Expand Up @@ -257,7 +259,7 @@ bool SafeTransferFrom(const std::string& from,
return ConcatHexStrings(hex_strings, data);
}

bool BalanceOf(const std::string& owner_address,
bool BalanceOf(std::string_view owner_address,
uint256_t token_id,
std::string* data) {
const std::string function_hash =
Expand Down Expand Up @@ -289,11 +291,12 @@ bool Uri(uint256_t token_id, std::string* data) {

namespace erc165 {

bool SupportsInterface(const std::string& interface_id, std::string* data) {
bool SupportsInterface(std::string_view interface_id, std::string* data) {
if (!IsValidHexString(interface_id) || interface_id.length() != 10) {
return false;
}
std::string padded_interface_id = interface_id + std::string(56, '0');
std::string padded_interface_id =
base::StrCat({interface_id, std::string(56, '0')});

const std::string function_hash =
GetFunctionHash("supportsInterface(bytes4)");
Expand All @@ -311,7 +314,7 @@ std::vector<uint8_t> SupportsInterface(eth_abi::Span4 interface) {
namespace unstoppable_domains {

std::optional<std::string> GetMany(const std::vector<std::string>& keys,
const std::string& domain) {
std::string_view domain) {
const std::string function_hash =
GetFunctionHash("getMany(string[],uint256)");

Expand All @@ -337,24 +340,22 @@ std::optional<std::string> GetMany(const std::vector<std::string>& keys,
return data;
}

std::vector<std::string> MakeEthLookupKeyList(const std::string& symbol,
const std::string& chain_id) {
std::vector<std::string> MakeEthLookupKeyList(std::string_view symbol,
std::string_view chain_id) {
auto upper_symbol = base::ToUpperASCII(symbol);
std::vector<std::string> lookup_keys;
// crypto.<TICKER>.version.<VERSION>.address
if (auto version = ChainIdToVersion(upper_symbol, chain_id)) {
if (!(upper_symbol == "ETH" && version == "ERC20")) {
// No such key as 'crypto.ETH.version.ERC20.address'. 'crypto.ETH.address'
// would be used instead.
lookup_keys.push_back(base::StringPrintf("crypto.%s.version.%s.address",
upper_symbol.c_str(),
version->c_str()));
lookup_keys.push_back(absl::StrFormat("crypto.%s.version.%s.address",
upper_symbol, *version));
}
}
// crypto.<TICKER>.address
if (symbol != "ETH") {
lookup_keys.push_back(
base::StringPrintf("crypto.%s.address", upper_symbol.c_str()));
lookup_keys.push_back(absl::StrFormat("crypto.%s.address", upper_symbol));
}

// crypto.ETH.address
Expand All @@ -363,13 +364,12 @@ std::vector<std::string> MakeEthLookupKeyList(const std::string& symbol,
return lookup_keys;
}

std::vector<std::string> MakeSolLookupKeyList(const std::string& symbol) {
std::vector<std::string> MakeSolLookupKeyList(std::string_view symbol) {
std::vector<std::string> lookup_keys;
// crypto.<TICKER>.version.SOLANA.address
if (symbol != "SOL") {
lookup_keys.push_back(
base::StringPrintf("crypto.%s.version.SOLANA.address",
base::ToUpperASCII(symbol).c_str()));
lookup_keys.push_back(absl::StrFormat("crypto.%s.version.SOLANA.address",
base::ToUpperASCII(symbol)));
}

// crypto.SOL.address
Expand All @@ -387,10 +387,10 @@ std::vector<std::string> MakeFilLookupKeyList() {
return lookup_keys;
}

std::vector<uint8_t> GetWalletAddr(const std::string& domain,
std::vector<uint8_t> GetWalletAddr(std::string_view domain,
mojom::CoinType coin,
const std::string& symbol,
const std::string& chain_id) {
std::string_view symbol,
std::string_view chain_id) {
std::vector<std::string> key_list;
switch (coin) {
case mojom::CoinType::ETH:
Expand All @@ -417,7 +417,7 @@ std::vector<uint8_t> GetWalletAddr(const std::string& domain,

namespace ens {

std::string Resolver(const std::string& domain) {
std::string Resolver(std::string_view domain) {
const std::string function_hash = GetFunctionHash("resolver(bytes32)");
std::string tokenID = ToHex(Namehash(domain));
std::vector<std::string> hex_strings = {function_hash, tokenID};
Expand All @@ -430,7 +430,7 @@ std::string Resolver(const std::string& domain) {
// https://docs.ens.domains/ens-improvement-proposals/ensip-10-wildcard-resolution#specification
// Similar to chromium's `DNSDomainFromDot` but without length limitation and
// support of terminal dot.
std::optional<std::vector<uint8_t>> DnsEncode(const std::string& dotted_name) {
std::optional<std::vector<uint8_t>> DnsEncode(std::string_view dotted_name) {
std::vector<uint8_t> result;
result.resize(dotted_name.size() + 2);
result.front() = '.'; // Placeholder for first label length.
Expand Down Expand Up @@ -460,7 +460,7 @@ std::optional<std::vector<uint8_t>> DnsEncode(const std::string& dotted_name) {
namespace balance_scanner {

std::optional<std::string> TokensBalance(
const std::string& owner_address,
std::string_view owner_address,
const std::vector<std::string>& contract_addresses) {
const std::string function_hash =
GetFunctionHash("tokensBalance(address,address[])");
Expand Down
46 changes: 23 additions & 23 deletions components/brave_wallet/browser/eth_data_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/types/optional_ref.h"
#include "base/values.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/brave_wallet_types.h"
Expand All @@ -29,17 +31,15 @@ std::optional<std::vector<uint8_t>> Forward(const FilAddress& fil_address);
namespace erc20 {

// Allows transferring ERC20 tokens
bool Transfer(const std::string& to_address,
uint256_t amount,
std::string* data);
bool Transfer(std::string_view to_address, uint256_t amount, std::string* data);
// Returns the balance of an address
bool BalanceOf(const std::string& address, std::string* data);
bool BalanceOf(std::string_view address, std::string* data);
// Approves the use of funds to an address
bool Approve(const std::string& spender_address,
bool Approve(std::string_view spender_address,
uint256_t amount,
std::string* data);
bool Allowance(const std::string& owner_address,
const std::string& spender_address,
bool Allowance(std::string_view owner_address,
std::string_view spender_address,
std::string* data);

} // namespace erc20
Expand All @@ -48,8 +48,8 @@ namespace erc721 {

// Transfer ownership of an NFT.
bool TransferFromOrSafeTransferFrom(bool is_safe_transfer_from,
const std::string& from,
const std::string& to,
std::string_view from,
std::string_view to,
uint256_t token_id,
std::string* data);

Expand All @@ -64,14 +64,14 @@ bool TokenUri(uint256_t token_id, std::string* data);
namespace erc1155 {

// Transfer the ownership of token from one address to another address.
bool SafeTransferFrom(const std::string& from_address,
const std::string& to_address,
bool SafeTransferFrom(std::string_view from_address,
std::string_view to_address,
uint256_t token_id,
uint256_t value,
std::string* data);

// Return the balance of an address for a token ID.
bool BalanceOf(const std::string& owner_address,
bool BalanceOf(std::string_view owner_address,
uint256_t token_id,
std::string* data);

Expand All @@ -85,7 +85,7 @@ namespace erc165 {
// supportsInterface(bytes4)
inline constexpr uint8_t kSupportsInterfaceBytes4[] = {0x01, 0xff, 0xc9, 0xa7};

bool SupportsInterface(const std::string& interface_id, std::string* data);
bool SupportsInterface(std::string_view interface_id, std::string* data);
std::vector<uint8_t> SupportsInterface(eth_abi::Span4 interface);

} // namespace erc165
Expand All @@ -97,32 +97,32 @@ inline constexpr uint8_t kGetManySelector[] = {0x1b, 0xd8, 0xcc, 0x1a};

// Get mutiple record values mapped with keys of the target domain.
std::optional<std::string> GetMany(const std::vector<std::string>& keys,
const std::string& domain);
std::string_view domain);

std::vector<std::string> MakeEthLookupKeyList(const std::string& symbol,
const std::string& chain_id);
std::vector<std::string> MakeSolLookupKeyList(const std::string& symbol);
std::vector<std::string> MakeEthLookupKeyList(std::string_view symbol,
std::string_view chain_id);
std::vector<std::string> MakeSolLookupKeyList(std::string_view symbol);
std::vector<std::string> MakeFilLookupKeyList();

std::vector<uint8_t> GetWalletAddr(const std::string& domain,
std::vector<uint8_t> GetWalletAddr(std::string_view domain,
mojom::CoinType coin,
const std::string& symbol,
const std::string& chain_id);
std::string_view symbol,
std::string_view chain_id);

} // namespace unstoppable_domains

namespace ens {

std::string Resolver(const std::string& domain);
std::string Resolver(std::string_view domain);

std::optional<std::vector<uint8_t>> DnsEncode(const std::string& dotted_name);
std::optional<std::vector<uint8_t>> DnsEncode(std::string_view dotted_name);

} // namespace ens

namespace balance_scanner {

std::optional<std::string> TokensBalance(
const std::string& owner_address,
std::string_view owner_address,
const std::vector<std::string>& contract_addresses);

} // namespace balance_scanner
Expand Down
6 changes: 3 additions & 3 deletions components/brave_wallet/common/hash_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ KeccakHashArray KeccakHash(base::span<const uint8_t> input) {
return result;
}

std::string GetFunctionHash(const std::string& input) {
std::string GetFunctionHash(std::string_view input) {
return ToHex(GetFunctionHashBytes4(input));
}

eth_abi::Bytes4 GetFunctionHashBytes4(const std::string& input) {
eth_abi::Bytes4 GetFunctionHashBytes4(std::string_view input) {
eth_abi::Bytes4 bytes_result;
base::span(bytes_result)
.copy_from(base::span(KeccakHash(base::as_byte_span(input))).first<4>());
return bytes_result;
}

eth_abi::Bytes32 Namehash(const std::string& name) {
eth_abi::Bytes32 Namehash(std::string_view name) {
eth_abi::Bytes32 hash = {};
auto labels = SplitStringPiece(name, ".", base::KEEP_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
Expand Down
Loading
Loading