From 7c221a3abcd830036d252011542b2e7239b8e394 Mon Sep 17 00:00:00 2001 From: Garrett Robinson Date: Fri, 2 Nov 2018 11:17:16 -0700 Subject: [PATCH] Avoid overwriting any extant Brave Rewards wallet --- browser/importer/brave_profile_writer.cc | 20 ++++++++++++---- common/importer/brave_ledger.h | 2 ++ ...import_process_param_traits_macros.h.patch | 5 ++-- utility/importer/brave_importer.cc | 24 ++++++++++++++----- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/browser/importer/brave_profile_writer.cc b/browser/importer/brave_profile_writer.cc index 8cb0757b19e4..42a4bdac3948 100644 --- a/browser/importer/brave_profile_writer.cc +++ b/browser/importer/brave_profile_writer.cc @@ -6,6 +6,8 @@ #include "brave/common/importer/brave_ledger.h" #include "brave/common/importer/brave_stats.h" #include "brave/common/pref_names.h" +#include "brave/components/brave_rewards/browser/rewards_service.h" +#include "brave/components/brave_rewards/browser/rewards_service_factory.h" #include "brave/utility/importer/brave_importer.h" #include "base/time/time.h" @@ -66,9 +68,19 @@ void BraveProfileWriter::UpdateStats(const BraveStats& stats) { } void BraveProfileWriter::UpdateLedger(const BraveLedger& ledger) { - LOG(ERROR) << "Reached BraveProfileWriter::UpdateLedger stub"; - // DEBUG verify successful IPC - for (const auto v : ledger.wallet_seed) { - LOG(ERROR) << (int)v; + brave_rewards::RewardsService* rewards_service = + brave_rewards::RewardsServiceFactory::GetForProfile(profile_); + if (!rewards_service) { + LOG(ERROR) << "Failed to get RewardsService for profile."; + return; } + + // Avoid overwriting Brave Rewards wallet if one already exists. + if (rewards_service->IsWalletCreated()) { + LOG(ERROR) << "Brave Rewards wallet already exists, canceling Brave Payments import."; + // TODO communicate this failure mode to the user + return; + } + + LOG(INFO) << "ledger passphrase: " << ledger.passphrase; } diff --git a/common/importer/brave_ledger.h b/common/importer/brave_ledger.h index fdc855adb7ca..888c2adc08ec 100644 --- a/common/importer/brave_ledger.h +++ b/common/importer/brave_ledger.h @@ -5,6 +5,7 @@ #ifndef BRAVE_COMMON_IMPORTER_BRAVE_LEDGER_H_ #define BRAVE_COMMON_IMPORTER_BRAVE_LEDGER_H_ +#include #include struct BraveLedger { @@ -13,6 +14,7 @@ struct BraveLedger { ~BraveLedger(); std::vector wallet_seed; + std::string passphrase; }; #endif // BRAVE_COMMON_IMPORTER_BRAVE_LEDGER_H_ diff --git a/patches/chrome-common-importer-profile_import_process_param_traits_macros.h.patch b/patches/chrome-common-importer-profile_import_process_param_traits_macros.h.patch index 1746c6b32fcb..82bb92a4cd7f 100644 --- a/patches/chrome-common-importer-profile_import_process_param_traits_macros.h.patch +++ b/patches/chrome-common-importer-profile_import_process_param_traits_macros.h.patch @@ -1,5 +1,5 @@ diff --git a/chrome/common/importer/profile_import_process_param_traits_macros.h b/chrome/common/importer/profile_import_process_param_traits_macros.h -index dd005641f4349c017bc65d843d8fe49ec6122f64..18ef89f4d6fe9b134523d69fc103c90b10b283d8 100644 +index dd005641f4349c017bc65d843d8fe49ec6122f64..6b6f2e38d887bd9b36c34c135174d428c5912aac 100644 --- a/chrome/common/importer/profile_import_process_param_traits_macros.h +++ b/chrome/common/importer/profile_import_process_param_traits_macros.h @@ -14,6 +14,8 @@ @@ -11,7 +11,7 @@ index dd005641f4349c017bc65d843d8fe49ec6122f64..18ef89f4d6fe9b134523d69fc103c90b #include "chrome/common/common_param_traits_macros.h" #include "chrome/common/importer/imported_bookmark_entry.h" #include "chrome/common/importer/importer_autofill_form_data_entry.h" -@@ -91,4 +93,14 @@ IPC_STRUCT_TRAITS_BEGIN(importer::ImporterIE7PasswordInfo) +@@ -91,4 +93,15 @@ IPC_STRUCT_TRAITS_BEGIN(importer::ImporterIE7PasswordInfo) IPC_STRUCT_TRAITS_MEMBER(date_created) IPC_STRUCT_TRAITS_END() @@ -23,6 +23,7 @@ index dd005641f4349c017bc65d843d8fe49ec6122f64..18ef89f4d6fe9b134523d69fc103c90b + +IPC_STRUCT_TRAITS_BEGIN(BraveLedger) + IPC_STRUCT_TRAITS_MEMBER(wallet_seed) ++ IPC_STRUCT_TRAITS_MEMBER(passphrase) +IPC_STRUCT_TRAITS_END() + #endif // CHROME_COMMON_IMPORTER_PROFILE_IMPORT_PROCESS_PARAM_TRAITS_MACROS_H_ diff --git a/utility/importer/brave_importer.cc b/utility/importer/brave_importer.cc index 435ba4cff1fc..02e6f35b3bdc 100644 --- a/utility/importer/brave_importer.cc +++ b/utility/importer/brave_importer.cc @@ -359,20 +359,32 @@ std::vector ParseWalletSeed(const base::Value& ledger_state_json) { return wallet_seed; } +std::string ParseWalletPassphrase(const base::Value& session_store_json) { + const base::Value* wallet_passphrase_value = session_store_json.FindPathOfType( + {"ledger", "info", "passphrase"}, + base::Value::Type::STRING); + if (!wallet_passphrase_value) { + LOG(ERROR) << "Wallet passphrase not found in session-store-1"; + return {}; + } + + return wallet_passphrase_value->GetString(); +} + void BraveImporter::ImportLedger() { + std::unique_ptr session_store_json = ParseBraveStateFile( + "session-store-1"); std::unique_ptr ledger_state_json = ParseBraveStateFile( "ledger-state.json"); - if (!ledger_state_json) + if (!(session_store_json && ledger_state_json)) return; - auto wallet_seed = ParseWalletSeed(*ledger_state_json); - if (wallet_seed.empty()) { - LOG(ERROR) << "Failed parsing wallet seed"; + auto passphrase = ParseWalletPassphrase(*session_store_json); + if (passphrase.empty()) return; - } BraveLedger ledger; - ledger.wallet_seed = wallet_seed; + ledger.passphrase = passphrase; bridge_->UpdateLedger(ledger); }