Skip to content

Commit

Permalink
Avoid overwriting any extant Brave Rewards wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Robinson authored and bsclifton committed Nov 7, 2018
1 parent a4375ee commit 7c221a3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
20 changes: 16 additions & 4 deletions browser/importer/brave_profile_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions common/importer/brave_ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef BRAVE_COMMON_IMPORTER_BRAVE_LEDGER_H_
#define BRAVE_COMMON_IMPORTER_BRAVE_LEDGER_H_

#include <string>
#include <vector>

struct BraveLedger {
Expand All @@ -13,6 +14,7 @@ struct BraveLedger {
~BraveLedger();

std::vector<uint8_t> wallet_seed;
std::string passphrase;
};

#endif // BRAVE_COMMON_IMPORTER_BRAVE_LEDGER_H_
Original file line number Diff line number Diff line change
@@ -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 @@
Expand All @@ -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()

Expand All @@ -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_
24 changes: 18 additions & 6 deletions utility/importer/brave_importer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,20 +359,32 @@ std::vector<uint8_t> 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<base::Value> session_store_json = ParseBraveStateFile(
"session-store-1");
std::unique_ptr<base::Value> 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);
}

0 comments on commit 7c221a3

Please sign in to comment.