Merge pull request #9409 from brave/wallet-data-files
Add Wallet data files updater
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "chrome/browser/component_updater/registration.h"
|
||||
|
||||
#define RegisterComponentsForUpdate RegisterComponentsForUpdate_ChromiumImpl
|
||||
#include "../../../../../chrome/browser/component_updater/registration.cc" // NOLINT
|
||||
#undef RegisterComponentsForUpdate
|
||||
|
||||
#include "brave/components/brave_wallet/common/buildflags/buildflags.h"
|
||||
#include "chrome/browser/browser_process.h"
|
||||
|
||||
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
|
||||
#include "brave/components/brave_wallet/browser/wallet_data_files_installer.h"
|
||||
#endif
|
||||
|
||||
namespace component_updater {
|
||||
|
||||
void RegisterComponentsForUpdate(bool is_off_the_record_profile,
|
||||
PrefService* profile_prefs,
|
||||
const base::FilePath& profile_path) {
|
||||
RegisterComponentsForUpdate_ChromiumImpl(is_off_the_record_profile,
|
||||
profile_prefs, profile_path);
|
||||
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
|
||||
ComponentUpdateService* cus = g_browser_process->component_updater();
|
||||
brave_wallet::RegisterWalletDataFilesComponent(cus);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace component_updater
|
||||
@@ -26,6 +26,10 @@ static_library("browser") {
|
||||
"brave_wallet_utils.h",
|
||||
"eip2930_transaction.cc",
|
||||
"eip2930_transaction.h",
|
||||
"erc_token_list_parser.cc",
|
||||
"erc_token_list_parser.h",
|
||||
"erc_token_registry.cc",
|
||||
"erc_token_registry.h",
|
||||
"eth_address.cc",
|
||||
"eth_address.h",
|
||||
"eth_call_data_builder.cc",
|
||||
@@ -64,18 +68,22 @@ static_library("browser") {
|
||||
"swap_controller.h",
|
||||
"swap_response_parser.cc",
|
||||
"swap_response_parser.h",
|
||||
"wallet_data_files_installer.cc",
|
||||
"wallet_data_files_installer.h",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//base",
|
||||
"//base/util/values:values_util",
|
||||
"//brave/components/api_request_helper",
|
||||
"//brave/components/brave_component_updater/browser",
|
||||
"//brave/components/brave_wallet/common",
|
||||
"//brave/components/brave_wallet/common:mojom",
|
||||
"//brave/third_party/bitcoin-core",
|
||||
"//brave/third_party/bitcoin-core:secp256k1",
|
||||
"//brave/third_party/ethash",
|
||||
"//brave/vendor/bip39wally-core-native:bip39wally-core",
|
||||
"//components/component_updater",
|
||||
"//components/keyed_service/core",
|
||||
"//components/prefs",
|
||||
"//components/sync_preferences",
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "brave/components/brave_wallet/browser/erc_token_list_parser.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "base/json/json_reader.h"
|
||||
#include "base/logging.h"
|
||||
|
||||
namespace {
|
||||
|
||||
bool ParseResultFromDict(const base::DictionaryValue* response_dict,
|
||||
const std::string& key,
|
||||
std::string* output_val) {
|
||||
auto* val = response_dict->FindStringKey(key);
|
||||
if (!val)
|
||||
return false;
|
||||
*output_val = *val;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace brave_wallet {
|
||||
|
||||
bool ParseTokenList(const std::string& json,
|
||||
std::vector<mojom::ERCTokenPtr>* token_list) {
|
||||
DCHECK(token_list);
|
||||
|
||||
// {
|
||||
// "0x0D8775F648430679A709E98d2b0Cb6250d2887EF": {
|
||||
// "name": "Basic Attention Token",
|
||||
// "logo": "bat.svg",
|
||||
// "erc20": true,
|
||||
// "symbol": "BAT",
|
||||
// "decimals": 18
|
||||
// },
|
||||
// "0x06012c8cf97BEaD5deAe237070F9587f8E7A266d": {
|
||||
// "name": "Crypto Kitties",
|
||||
// "logo": "CryptoKitties-Kitty-13733.svg",
|
||||
// "erc20": false,
|
||||
// "erc721": true,
|
||||
// "symbol": "CK",
|
||||
// "decimals": 0
|
||||
// },
|
||||
// "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984": {
|
||||
// "name": "Uniswap",
|
||||
// "logo": "uni.svg",
|
||||
// "erc20": true,
|
||||
// "symbol": "UNI",
|
||||
// "decimals": 18
|
||||
// }
|
||||
// }
|
||||
|
||||
base::JSONReader::ValueWithError value_with_error =
|
||||
base::JSONReader::ReadAndReturnValueWithError(
|
||||
json, base::JSONParserOptions::JSON_PARSE_RFC);
|
||||
auto& records_v = value_with_error.value;
|
||||
if (!records_v) {
|
||||
LOG(ERROR) << "Invalid response, could not parse JSON, JSON is: " << json;
|
||||
return false;
|
||||
}
|
||||
|
||||
const base::DictionaryValue* response_dict;
|
||||
if (!records_v->GetAsDictionary(&response_dict)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& erc_token_value_pair : response_dict->DictItems()) {
|
||||
auto erc_token = brave_wallet::mojom::ERCToken::New();
|
||||
erc_token->contract_address = erc_token_value_pair.first;
|
||||
const base::DictionaryValue* erc_token_value;
|
||||
if (!erc_token_value_pair.second.GetAsDictionary(&erc_token_value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
absl::optional<bool> is_erc20_opt = erc_token_value->FindBoolKey("erc20");
|
||||
if (is_erc20_opt)
|
||||
erc_token->is_erc20 = *is_erc20_opt;
|
||||
else
|
||||
erc_token->is_erc20 = false;
|
||||
|
||||
absl::optional<bool> is_erc721_opt = erc_token_value->FindBoolKey("erc721");
|
||||
if (is_erc721_opt)
|
||||
erc_token->is_erc721 = *is_erc721_opt;
|
||||
else
|
||||
erc_token->is_erc721 = false;
|
||||
|
||||
if (!ParseResultFromDict(erc_token_value, "symbol", &erc_token->symbol)) {
|
||||
continue;
|
||||
}
|
||||
if (!ParseResultFromDict(erc_token_value, "name", &erc_token->name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
absl::optional<int> decimals_opt = erc_token_value->FindIntKey("decimals");
|
||||
if (decimals_opt)
|
||||
erc_token->decimals = *decimals_opt;
|
||||
else
|
||||
continue;
|
||||
|
||||
token_list->push_back(std::move(erc_token));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace brave_wallet
|
||||
@@ -0,0 +1,21 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ERC_TOKEN_LIST_PARSER_H_
|
||||
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ERC_TOKEN_LIST_PARSER_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
|
||||
|
||||
namespace brave_wallet {
|
||||
|
||||
bool ParseTokenList(const std::string& json,
|
||||
std::vector<mojom::ERCTokenPtr>* token_list);
|
||||
|
||||
} // namespace brave_wallet
|
||||
|
||||
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ERC_TOKEN_LIST_PARSER_H_
|
||||
@@ -0,0 +1,87 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "brave/components/brave_wallet/browser/erc_token_list_parser.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
namespace brave_wallet {
|
||||
|
||||
TEST(ParseTokenListUnitTest, ParseTokenList) {
|
||||
std::string json(R"(
|
||||
{
|
||||
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d": {
|
||||
"name": "Crypto Kitties",
|
||||
"logo": "CryptoKitties-Kitty-13733.svg",
|
||||
"erc20": false,
|
||||
"erc721": true,
|
||||
"symbol": "CK",
|
||||
"decimals": 0
|
||||
},
|
||||
"0x0D8775F648430679A709E98d2b0Cb6250d2887EF": {
|
||||
"name": "Basic Attention Token",
|
||||
"logo": "bat.svg",
|
||||
"erc20": true,
|
||||
"symbol": "BAT",
|
||||
"decimals": 18
|
||||
},
|
||||
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984": {
|
||||
"name": "Uniswap",
|
||||
"logo": "uni.svg",
|
||||
"erc20": true,
|
||||
"symbol": "UNI",
|
||||
"decimals": 18
|
||||
}
|
||||
}
|
||||
)");
|
||||
std::vector<mojom::ERCTokenPtr> token_list;
|
||||
ASSERT_TRUE(ParseTokenList(json, &token_list));
|
||||
ASSERT_EQ(token_list.size(), 3UL);
|
||||
|
||||
ASSERT_EQ(token_list[0]->name, "Crypto Kitties");
|
||||
ASSERT_EQ(token_list[0]->contract_address,
|
||||
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d");
|
||||
ASSERT_FALSE(token_list[0]->is_erc20);
|
||||
ASSERT_TRUE(token_list[0]->is_erc721);
|
||||
ASSERT_EQ(token_list[0]->symbol, "CK");
|
||||
ASSERT_EQ(token_list[0]->decimals, 0);
|
||||
|
||||
ASSERT_EQ(token_list[1]->name, "Basic Attention Token");
|
||||
ASSERT_EQ(token_list[1]->contract_address,
|
||||
"0x0D8775F648430679A709E98d2b0Cb6250d2887EF");
|
||||
ASSERT_TRUE(token_list[1]->is_erc20);
|
||||
ASSERT_FALSE(token_list[1]->is_erc721);
|
||||
ASSERT_EQ(token_list[1]->symbol, "BAT");
|
||||
ASSERT_EQ(token_list[1]->decimals, 18);
|
||||
|
||||
ASSERT_EQ(token_list[2]->name, "Uniswap");
|
||||
ASSERT_EQ(token_list[2]->contract_address,
|
||||
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984");
|
||||
ASSERT_TRUE(token_list[2]->is_erc20);
|
||||
ASSERT_FALSE(token_list[2]->is_erc721);
|
||||
ASSERT_EQ(token_list[2]->symbol, "UNI");
|
||||
ASSERT_EQ(token_list[2]->decimals, 18);
|
||||
|
||||
token_list.clear();
|
||||
json = R"({})";
|
||||
ASSERT_TRUE(ParseTokenList(json, &token_list));
|
||||
ASSERT_TRUE(token_list.empty());
|
||||
json = R"({"0x0D8775F648430679A709E98d2b0Cb6250d2887EF": 3})";
|
||||
ASSERT_FALSE(ParseTokenList(json, &token_list));
|
||||
json = R"({"0x0D8775F648430679A709E98d2b0Cb6250d2887EF": {}})";
|
||||
ASSERT_EQ(token_list.size(), 0UL);
|
||||
ASSERT_TRUE(ParseTokenList(json, &token_list));
|
||||
json = "3";
|
||||
ASSERT_FALSE(ParseTokenList(json, &token_list));
|
||||
json = "[3]";
|
||||
ASSERT_FALSE(ParseTokenList(json, &token_list));
|
||||
json = "";
|
||||
ASSERT_FALSE(ParseTokenList(json, &token_list));
|
||||
}
|
||||
|
||||
} // namespace brave_wallet
|
||||
@@ -0,0 +1,65 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "brave/components/brave_wallet/browser/erc_token_registry.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace brave_wallet {
|
||||
|
||||
ERCTokenRegistry::ERCTokenRegistry() = default;
|
||||
|
||||
ERCTokenRegistry::~ERCTokenRegistry() = default;
|
||||
|
||||
ERCTokenRegistry* ERCTokenRegistry::GetInstance() {
|
||||
return base::Singleton<ERCTokenRegistry>::get();
|
||||
}
|
||||
|
||||
void ERCTokenRegistry::UpdateTokenList(
|
||||
std::vector<mojom::ERCTokenPtr> erc_tokens) {
|
||||
erc_tokens_ = std::move(erc_tokens);
|
||||
}
|
||||
|
||||
mojom::ERCTokenPtr ERCTokenRegistry::GetTokenByContract(
|
||||
const std::string& contract) {
|
||||
auto token_it =
|
||||
std::find_if(erc_tokens_.begin(), erc_tokens_.end(),
|
||||
[&](const mojom::ERCTokenPtr& current_token) {
|
||||
return current_token->contract_address == contract;
|
||||
});
|
||||
|
||||
if (token_it == erc_tokens_.end())
|
||||
return nullptr;
|
||||
|
||||
return token_it->Clone();
|
||||
}
|
||||
|
||||
mojom::ERCTokenPtr ERCTokenRegistry::GetTokenBySymbol(
|
||||
const std::string& symbol) {
|
||||
auto token_it = std::find_if(erc_tokens_.begin(), erc_tokens_.end(),
|
||||
[&](const mojom::ERCTokenPtr& current_token) {
|
||||
return current_token->symbol == symbol;
|
||||
});
|
||||
|
||||
if (token_it == erc_tokens_.end())
|
||||
return nullptr;
|
||||
|
||||
return token_it->Clone();
|
||||
}
|
||||
|
||||
std::vector<mojom::ERCTokenPtr> ERCTokenRegistry::GetAllTokens() {
|
||||
std::vector<brave_wallet::mojom::ERCTokenPtr> erc_tokens_copy(
|
||||
erc_tokens_.size());
|
||||
std::transform(erc_tokens_.begin(), erc_tokens_.end(),
|
||||
erc_tokens_copy.begin(),
|
||||
[](const brave_wallet::mojom::ERCTokenPtr& current_token)
|
||||
-> brave_wallet::mojom::ERCTokenPtr {
|
||||
return current_token.Clone();
|
||||
});
|
||||
return erc_tokens_copy;
|
||||
}
|
||||
|
||||
} // namespace brave_wallet
|
||||
@@ -0,0 +1,41 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ERC_TOKEN_REGISTRY_H_
|
||||
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ERC_TOKEN_REGISTRY_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "base/memory/singleton.h"
|
||||
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
|
||||
#include "build/build_config.h"
|
||||
|
||||
namespace brave_wallet {
|
||||
|
||||
class ERCTokenRegistry {
|
||||
public:
|
||||
ERCTokenRegistry(const ERCTokenRegistry&) = delete;
|
||||
ERCTokenRegistry& operator=(const ERCTokenRegistry&) = delete;
|
||||
|
||||
static ERCTokenRegistry* GetInstance();
|
||||
bool ParseERCTokens(const std::string& token_list);
|
||||
void UpdateTokenList(std::vector<mojom::ERCTokenPtr> erc_tokens);
|
||||
mojom::ERCTokenPtr GetTokenByContract(const std::string& contract);
|
||||
mojom::ERCTokenPtr GetTokenBySymbol(const std::string& symbol);
|
||||
std::vector<mojom::ERCTokenPtr> GetAllTokens();
|
||||
|
||||
protected:
|
||||
std::vector<mojom::ERCTokenPtr> erc_tokens_;
|
||||
friend struct base::DefaultSingletonTraits<ERCTokenRegistry>;
|
||||
|
||||
ERCTokenRegistry();
|
||||
virtual ~ERCTokenRegistry();
|
||||
};
|
||||
|
||||
} // namespace brave_wallet
|
||||
|
||||
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_ERC_TOKEN_REGISTRY_H_
|
||||
@@ -0,0 +1,114 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "brave/components/brave_wallet/browser/erc_token_list_parser.h"
|
||||
#include "brave/components/brave_wallet/browser/erc_token_registry.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
namespace brave_wallet {
|
||||
|
||||
namespace {
|
||||
|
||||
const char token_list_json[] = R"(
|
||||
{
|
||||
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d": {
|
||||
"name": "Crypto Kitties",
|
||||
"logo": "CryptoKitties-Kitty-13733.svg",
|
||||
"erc20": false,
|
||||
"erc721": true,
|
||||
"symbol": "CK",
|
||||
"decimals": 0
|
||||
},
|
||||
"0x0D8775F648430679A709E98d2b0Cb6250d2887EF": {
|
||||
"name": "Basic Attention Token",
|
||||
"logo": "bat.svg",
|
||||
"erc20": true,
|
||||
"symbol": "BAT",
|
||||
"decimals": 18
|
||||
},
|
||||
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984": {
|
||||
"name": "Uniswap",
|
||||
"logo": "uni.svg",
|
||||
"erc20": true,
|
||||
"symbol": "UNI",
|
||||
"decimals": 18
|
||||
},
|
||||
"0x6090A6e47849629b7245Dfa1Ca21D94cd15878Ef": {
|
||||
"name": "ENS Registrar",
|
||||
"logo": "ens.svg"
|
||||
}
|
||||
})";
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(ERCTokenRegistryUnitTest, GetAllTokens) {
|
||||
auto* registry = ERCTokenRegistry::GetInstance();
|
||||
|
||||
std::vector<mojom::ERCTokenPtr> input_erc_tokens;
|
||||
ASSERT_TRUE(ParseTokenList(token_list_json, &input_erc_tokens));
|
||||
registry->UpdateTokenList(std::move(input_erc_tokens));
|
||||
|
||||
std::vector<mojom::ERCTokenPtr> token_list = registry->GetAllTokens();
|
||||
// ENS Registrar should not be parsed because it doesn't have decimals
|
||||
// nor a symbol defined
|
||||
ASSERT_EQ(token_list.size(), 3UL);
|
||||
|
||||
ASSERT_EQ(token_list[0]->name, "Crypto Kitties");
|
||||
ASSERT_EQ(token_list[0]->contract_address,
|
||||
"0x06012c8cf97BEaD5deAe237070F9587f8E7A266d");
|
||||
ASSERT_FALSE(token_list[0]->is_erc20);
|
||||
ASSERT_TRUE(token_list[0]->is_erc721);
|
||||
ASSERT_EQ(token_list[0]->symbol, "CK");
|
||||
ASSERT_EQ(token_list[0]->decimals, 0);
|
||||
|
||||
ASSERT_EQ(token_list[1]->name, "Basic Attention Token");
|
||||
ASSERT_EQ(token_list[1]->contract_address,
|
||||
"0x0D8775F648430679A709E98d2b0Cb6250d2887EF");
|
||||
ASSERT_TRUE(token_list[1]->is_erc20);
|
||||
ASSERT_FALSE(token_list[1]->is_erc721);
|
||||
ASSERT_EQ(token_list[1]->symbol, "BAT");
|
||||
ASSERT_EQ(token_list[1]->decimals, 18);
|
||||
|
||||
ASSERT_EQ(token_list[2]->name, "Uniswap");
|
||||
ASSERT_EQ(token_list[2]->contract_address,
|
||||
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984");
|
||||
ASSERT_TRUE(token_list[2]->is_erc20);
|
||||
ASSERT_FALSE(token_list[2]->is_erc721);
|
||||
ASSERT_EQ(token_list[2]->symbol, "UNI");
|
||||
ASSERT_EQ(token_list[2]->decimals, 18);
|
||||
}
|
||||
|
||||
TEST(ERCTokenRegistryUnitTest, GetTokenByContract) {
|
||||
auto* registry = ERCTokenRegistry::GetInstance();
|
||||
|
||||
std::vector<mojom::ERCTokenPtr> input_erc_tokens;
|
||||
ASSERT_TRUE(ParseTokenList(token_list_json, &input_erc_tokens));
|
||||
registry->UpdateTokenList(std::move(input_erc_tokens));
|
||||
|
||||
auto token = registry->GetTokenByContract(
|
||||
"0x0D8775F648430679A709E98d2b0Cb6250d2887EF");
|
||||
ASSERT_EQ(token->symbol, "BAT");
|
||||
ASSERT_FALSE(registry->GetTokenByContract(
|
||||
"0xCCC775F648430679A709E98d2b0Cb6250d2887EF"));
|
||||
}
|
||||
|
||||
TEST(ERCTokenRegistryUnitTest, GetTokenBySymbol) {
|
||||
auto* registry = ERCTokenRegistry::GetInstance();
|
||||
|
||||
std::vector<mojom::ERCTokenPtr> input_erc_tokens;
|
||||
ASSERT_TRUE(ParseTokenList(token_list_json, &input_erc_tokens));
|
||||
registry->UpdateTokenList(std::move(input_erc_tokens));
|
||||
|
||||
auto token = registry->GetTokenBySymbol("BAT");
|
||||
ASSERT_EQ(token->contract_address,
|
||||
"0x0D8775F648430679A709E98d2b0Cb6250d2887EF");
|
||||
ASSERT_FALSE(registry->GetTokenBySymbol("BRB"));
|
||||
}
|
||||
|
||||
} // namespace brave_wallet
|
||||
@@ -15,6 +15,8 @@ source_set("brave_wallet_unit_tests") {
|
||||
"//brave/components/brave_wallet/browser/brave_wallet_types_unittest.cc",
|
||||
"//brave/components/brave_wallet/browser/brave_wallet_utils_unittest.cc",
|
||||
"//brave/components/brave_wallet/browser/eip2930_transaction_unittest.cc",
|
||||
"//brave/components/brave_wallet/browser/erc_token_list_parser_unittest.cc",
|
||||
"//brave/components/brave_wallet/browser/erc_token_registry_unittest.cc",
|
||||
"//brave/components/brave_wallet/browser/eth_address_unittest.cc",
|
||||
"//brave/components/brave_wallet/browser/eth_call_data_builder_unittest.cc",
|
||||
"//brave/components/brave_wallet/browser/eth_json_rpc_controller_unittest.cc",
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "brave/components/brave_wallet/browser/wallet_data_files_installer.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "base/base64.h"
|
||||
#include "base/bind.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/files/file_util.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/task/thread_pool.h"
|
||||
#include "base/values.h"
|
||||
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
|
||||
#include "brave/components/brave_wallet/browser/erc_token_list_parser.h"
|
||||
#include "brave/components/brave_wallet/browser/erc_token_registry.h"
|
||||
#include "components/component_updater/component_installer.h"
|
||||
#include "components/component_updater/component_updater_service.h"
|
||||
#include "crypto/sha2.h"
|
||||
|
||||
namespace brave_wallet {
|
||||
|
||||
namespace {
|
||||
|
||||
// CRX public key hash. The extension id is: bbckkcdiepaecefgfnibemejliemjnio
|
||||
// Getting the public key:
|
||||
// openssl rsa -in ./wallet.pem -pubout -outform DER | openssl base64 -A >
|
||||
// wallet.pub
|
||||
// openssl rsa -in ~/Desktop/wallet/wallet.pem -pubout
|
||||
// -outform DER | shasum -a 256 | head -c32 | tr 0-9a-f a-p | mvim -
|
||||
const uint8_t kWalletDataFilesSha2Hash[] = {
|
||||
0x11, 0x2a, 0xa2, 0x38, 0x4f, 0x4, 0x24, 0x56, 0x5d, 0x81, 0x4c,
|
||||
0x49, 0xb8, 0x4c, 0x9d, 0x8e, 0xeb, 0xb3, 0xbd, 0x55, 0xdc, 0xf7,
|
||||
0xc0, 0x3e, 0x9b, 0x2a, 0xc2, 0xf5, 0x6a, 0x37, 0x71, 0x67};
|
||||
const char kWalletDataFilesDisplayName[] = "Brave Wallet data files";
|
||||
const char kWalletBaseDirectory[] = "BraveWallet";
|
||||
|
||||
static_assert(base::size(kWalletDataFilesSha2Hash) == crypto::kSHA256Length,
|
||||
"Wrong hash length");
|
||||
|
||||
} // namespace
|
||||
|
||||
class WalletDataFilesInstallerPolicy
|
||||
: public component_updater::ComponentInstallerPolicy {
|
||||
public:
|
||||
WalletDataFilesInstallerPolicy();
|
||||
~WalletDataFilesInstallerPolicy() override = default;
|
||||
|
||||
private:
|
||||
// The following methods override ComponentInstallerPolicy.
|
||||
bool SupportsGroupPolicyEnabledComponentUpdates() const override;
|
||||
bool RequiresNetworkEncryption() const override;
|
||||
update_client::CrxInstaller::Result OnCustomInstall(
|
||||
const base::DictionaryValue& manifest,
|
||||
const base::FilePath& install_dir) override;
|
||||
void OnCustomUninstall() override;
|
||||
bool VerifyInstallation(const base::DictionaryValue& manifest,
|
||||
const base::FilePath& install_dir) const override;
|
||||
void ComponentReady(const base::Version& version,
|
||||
const base::FilePath& path,
|
||||
std::unique_ptr<base::DictionaryValue> manifest) override;
|
||||
base::FilePath GetRelativeInstallDir() const override;
|
||||
void GetHash(std::vector<uint8_t>* hash) const override;
|
||||
std::string GetName() const override;
|
||||
update_client::InstallerAttributes GetInstallerAttributes() const override;
|
||||
|
||||
std::vector<mojom::ERCTokenPtr> TokenListReady(
|
||||
const base::FilePath& install_dir,
|
||||
std::unique_ptr<base::DictionaryValue> manifest);
|
||||
void UpdateTokenRegistry(std::vector<mojom::ERCTokenPtr>);
|
||||
|
||||
WalletDataFilesInstallerPolicy(const WalletDataFilesInstallerPolicy&) =
|
||||
delete;
|
||||
WalletDataFilesInstallerPolicy operator=(
|
||||
const WalletDataFilesInstallerPolicy&) = delete;
|
||||
};
|
||||
|
||||
WalletDataFilesInstallerPolicy::WalletDataFilesInstallerPolicy() = default;
|
||||
|
||||
bool WalletDataFilesInstallerPolicy::
|
||||
SupportsGroupPolicyEnabledComponentUpdates() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WalletDataFilesInstallerPolicy::RequiresNetworkEncryption() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
update_client::CrxInstaller::Result
|
||||
WalletDataFilesInstallerPolicy::OnCustomInstall(
|
||||
const base::DictionaryValue& manifest,
|
||||
const base::FilePath& install_dir) {
|
||||
return update_client::CrxInstaller::Result(update_client::InstallError::NONE);
|
||||
}
|
||||
|
||||
void WalletDataFilesInstallerPolicy::OnCustomUninstall() {}
|
||||
|
||||
void WalletDataFilesInstallerPolicy::ComponentReady(
|
||||
const base::Version& version,
|
||||
const base::FilePath& path,
|
||||
std::unique_ptr<base::DictionaryValue> manifest) {
|
||||
base::ThreadPool::PostTaskAndReplyWithResult(
|
||||
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
|
||||
base::BindOnce(&WalletDataFilesInstallerPolicy::TokenListReady,
|
||||
base::Unretained(this), path, std::move(manifest)),
|
||||
base::BindOnce(&WalletDataFilesInstallerPolicy::UpdateTokenRegistry,
|
||||
base::Unretained(this)));
|
||||
}
|
||||
|
||||
bool WalletDataFilesInstallerPolicy::VerifyInstallation(
|
||||
const base::DictionaryValue& manifest,
|
||||
const base::FilePath& install_dir) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
// The base directory on Windows looks like:
|
||||
// <profile>\AppData\Local\Google\Chrome\User Data\BraveWallet\.
|
||||
base::FilePath WalletDataFilesInstallerPolicy::GetRelativeInstallDir() const {
|
||||
return base::FilePath::FromUTF8Unsafe(kWalletBaseDirectory);
|
||||
}
|
||||
|
||||
void WalletDataFilesInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const {
|
||||
hash->assign(kWalletDataFilesSha2Hash,
|
||||
kWalletDataFilesSha2Hash + base::size(kWalletDataFilesSha2Hash));
|
||||
}
|
||||
|
||||
std::string WalletDataFilesInstallerPolicy::GetName() const {
|
||||
return kWalletDataFilesDisplayName;
|
||||
}
|
||||
|
||||
update_client::InstallerAttributes
|
||||
WalletDataFilesInstallerPolicy::GetInstallerAttributes() const {
|
||||
return update_client::InstallerAttributes();
|
||||
}
|
||||
|
||||
std::vector<mojom::ERCTokenPtr> WalletDataFilesInstallerPolicy::TokenListReady(
|
||||
const base::FilePath& install_dir,
|
||||
std::unique_ptr<base::DictionaryValue> manifest) {
|
||||
std::vector<mojom::ERCTokenPtr> erc_tokens;
|
||||
// On some platforms (e.g. Mac) we use symlinks for paths. Convert paths to
|
||||
// absolute paths to avoid unexpected failure. base::MakeAbsoluteFilePath()
|
||||
// requires IO so it can only be done in this function.
|
||||
const base::FilePath absolute_install_dir =
|
||||
base::MakeAbsoluteFilePath(install_dir);
|
||||
|
||||
if (absolute_install_dir.empty()) {
|
||||
LOG(ERROR) << "Failed to get absolute install path.";
|
||||
return erc_tokens;
|
||||
}
|
||||
|
||||
const base::FilePath token_list_json_path =
|
||||
absolute_install_dir.AppendASCII("contract-map.json");
|
||||
std::string token_list_json;
|
||||
if (!base::ReadFileToString(token_list_json_path, &token_list_json)) {
|
||||
LOG(ERROR) << "Can't read token list file.";
|
||||
}
|
||||
|
||||
if (!ParseTokenList(token_list_json, &erc_tokens)) {
|
||||
LOG(ERROR) << "Can't parse token list.";
|
||||
erc_tokens.clear();
|
||||
}
|
||||
|
||||
return erc_tokens;
|
||||
}
|
||||
|
||||
void WalletDataFilesInstallerPolicy::UpdateTokenRegistry(
|
||||
std::vector<mojom::ERCTokenPtr> erc_tokens) {
|
||||
ERCTokenRegistry::GetInstance()->UpdateTokenList(std::move(erc_tokens));
|
||||
}
|
||||
|
||||
void RegisterWalletDataFilesComponent(
|
||||
component_updater::ComponentUpdateService* cus) {
|
||||
if (brave_wallet::IsNativeWalletEnabled()) {
|
||||
auto installer =
|
||||
base::MakeRefCounted<component_updater::ComponentInstaller>(
|
||||
std::make_unique<WalletDataFilesInstallerPolicy>());
|
||||
installer->Register(cus, base::OnceClosure());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace brave_wallet
|
||||
@@ -0,0 +1,20 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_WALLET_DATA_FILES_INSTALLER_H_
|
||||
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_WALLET_DATA_FILES_INSTALLER_H_
|
||||
|
||||
namespace component_updater {
|
||||
class ComponentUpdateService;
|
||||
}
|
||||
|
||||
namespace brave_wallet {
|
||||
|
||||
void RegisterWalletDataFilesComponent(
|
||||
component_updater::ComponentUpdateService* cus);
|
||||
|
||||
} // namespace brave_wallet
|
||||
|
||||
#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_WALLET_DATA_FILES_INSTALLER_H_
|
||||
@@ -137,6 +137,15 @@ interface WalletHandler {
|
||||
NotifyWalletBackupComplete();
|
||||
};
|
||||
|
||||
struct ERCToken {
|
||||
string contract_address;
|
||||
string name;
|
||||
bool is_erc20;
|
||||
bool is_erc721;
|
||||
string symbol;
|
||||
int32 decimals;
|
||||
};
|
||||
|
||||
// WebUI-side handler for requests from the browser.
|
||||
interface Page {
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user