Skip to content

Commit

Permalink
Merge pull request #6063 from brave/migrate-anon-address
Browse files Browse the repository at this point in the history
Migrates anon address to uphold wallet object
  • Loading branch information
NejcZdovc authored Jul 14, 2020
2 parents 5ad6ac6 + 0d3c7af commit d64823f
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 66 deletions.
14 changes: 2 additions & 12 deletions components/brave_rewards/browser/external_wallet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,14 @@ namespace brave_rewards {

ExternalWallet::~ExternalWallet() { }

ExternalWallet::ExternalWallet(const ExternalWallet &properties) {
token = properties.token;
address = properties.address;
status = properties.status;
type = properties.type;
verify_url = properties.verify_url;
add_url = properties.add_url;
withdraw_url = properties.withdraw_url;
user_name = properties.user_name;
account_url = properties.account_url;
login_url = properties.login_url;
}
ExternalWallet::ExternalWallet(const ExternalWallet& properties) = default;

std::string ExternalWallet::toJson() {
std::string json_wallet;
base::Value dict(base::Value::Type::DICTIONARY);
dict.SetStringKey("token", token);
dict.SetStringKey("address", address);
dict.SetStringKey("anon_address", anon_address);

// enum class WalletStatus : int32_t
dict.SetIntKey("status", static_cast<int32_t>(status));
Expand Down
1 change: 1 addition & 0 deletions components/brave_rewards/browser/external_wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct ExternalWallet {

std::string token;
std::string address;
std::string anon_address;
uint32_t status;
std::string type;
std::string verify_url;
Expand Down
7 changes: 7 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3152,6 +3152,7 @@ void RewardsServiceImpl::SaveExternalWallet(const std::string& wallet_type,
new_wallet.SetStringKey("withdraw_url", wallet->withdraw_url);
new_wallet.SetStringKey("account_url", wallet->account_url);
new_wallet.SetStringKey("login_url", wallet->login_url);
new_wallet.SetStringKey("anon_address", wallet->anon_address);

new_wallets.SetKey(wallet_type, std::move(new_wallet));

Expand Down Expand Up @@ -3225,6 +3226,11 @@ RewardsServiceImpl::GetExternalWallets() {
wallet->login_url = *login_url;
}

auto* anon_address = it.second.FindStringKey("anon_address");
if (anon_address) {
wallet->anon_address = *anon_address;
}

wallets.insert(std::make_pair(it.first, std::move(wallet)));
}

Expand All @@ -3250,6 +3256,7 @@ void RewardsServiceImpl::OnGetExternalWallet(
external->user_name = wallet->user_name;
external->account_url = wallet->account_url;
external->login_url = wallet->login_url;
external->anon_address = wallet->anon_address;
}

std::move(callback).Run(static_cast<int>(result), std::move(external));
Expand Down
2 changes: 1 addition & 1 deletion components/brave_rewards/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern const char kRewardsBadgeText[];

// Defined in native-ledger
extern const char kStateServerPublisherListStamp[];
extern const char kStateUpholdAnonAddress[];
extern const char kStateUpholdAnonAddress[]; // DEPRECATED
extern const char kStatePromotionLastFetchStamp[];
extern const char kStatePromotionCorruptedMigrated[];
extern const char kStateAnonTransferChecked[];
Expand Down
2 changes: 2 additions & 0 deletions vendor/bat-native-ledger/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ source_set("ledger") {
"src/bat/ledger/internal/state/state_migration_v1.h",
"src/bat/ledger/internal/state/state_migration_v2.cc",
"src/bat/ledger/internal/state/state_migration_v2.h",
"src/bat/ledger/internal/state/state_migration_v3.cc",
"src/bat/ledger/internal/state/state_migration_v3.h",
"src/bat/ledger/internal/state/state_util.cc",
"src/bat/ledger/internal/state/state_util.h",
"src/bat/ledger/internal/legacy/client_state.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ struct ExternalWallet {
string type;
string token;
string address;
string anon_address;
WalletStatus status;
string verify_url;
string add_url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace ledger {
const char kStateEnabled[] = "enabled";
const char kStateServerPublisherListStamp[] = "server_publisher_list_stamp";
const char kStateUpholdAnonAddress[] = "uphold_anon_address";
const char kStateUpholdAnonAddress[] = "uphold_anon_address"; // DEPRECATED
const char kStatePromotionLastFetchStamp[] = "promotion_last_fetch_stamp";
const char kStatePromotionCorruptedMigrated[] =
"promotion_corrupted_migrated2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using std::placeholders::_1;

namespace {

const int kCurrentVersionNumber = 2;
const int kCurrentVersionNumber = 3;

} // namespace

Expand All @@ -22,6 +22,7 @@ namespace braveledger_state {
StateMigration::StateMigration(bat_ledger::LedgerImpl* ledger) :
v1_(std::make_unique<StateMigrationV1>(ledger)),
v2_(std::make_unique<StateMigrationV2>(ledger)),
v3_(std::make_unique<StateMigrationV3>(ledger)),
ledger_(ledger) {
DCHECK(v1_ && v2_);
}
Expand Down Expand Up @@ -52,6 +53,10 @@ void StateMigration::Migrate(ledger::ResultCallback callback) {
v2_->Migrate(migrate_callback);
return;
}
case 3: {
v3_->Migrate(migrate_callback);
return;
}
}

BLOG(0, "Migration version is not handled " << new_version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "bat/ledger/internal/state/state_migration_v1.h"
#include "bat/ledger/internal/state/state_migration_v2.h"
#include "bat/ledger/internal/state/state_migration_v3.h"
#include "bat/ledger/ledger.h"

namespace bat_ledger {
Expand All @@ -34,6 +35,7 @@ class StateMigration {

std::unique_ptr<StateMigrationV1> v1_;
std::unique_ptr<StateMigrationV2> v2_;
std::unique_ptr<StateMigrationV3> v3_;
bat_ledger::LedgerImpl* ledger_; // NOT OWNED
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright (c) 2020 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 <utility>

#include "bat/ledger/global_constants.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/state/state_keys.h"
#include "bat/ledger/internal/state/state_migration_v3.h"
#include "bat/ledger/internal/state/state_util.h"
#include "bat/ledger/internal/uphold/uphold_util.h"

namespace braveledger_state {

StateMigrationV3::StateMigrationV3(bat_ledger::LedgerImpl* ledger) :
ledger_(ledger) {
}

StateMigrationV3::~StateMigrationV3() = default;

void StateMigrationV3::Migrate(ledger::ResultCallback callback) {
const std::string anon_address =
ledger_->GetStringState(ledger::kStateUpholdAnonAddress);

if (!anon_address.empty()) {
auto wallets = ledger_->GetExternalWallets();
auto wallet = braveledger_uphold::GetWallet(std::move(wallets));
if (!wallet) {
BLOG(0, "Wallet is null, but we can't recover");
callback(ledger::Result::LEDGER_OK);
return;
}

wallet->anon_address = anon_address;
ledger_->SaveExternalWallet(ledger::kWalletUphold, std::move(wallet));
ledger_->ClearState(ledger::kStateUpholdAnonAddress);
}

callback(ledger::Result::LEDGER_OK);
}

} // namespace braveledger_state
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Copyright (c) 2020 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 BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V3_H_
#define BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V3_H_

#include <memory>
#include <string>

#include "bat/ledger/ledger.h"

namespace bat_ledger {
class LedgerImpl;
}

namespace braveledger_state {

class StateMigrationV3 {
public:
explicit StateMigrationV3(bat_ledger::LedgerImpl* ledger);
~StateMigrationV3();

void Migrate(ledger::ResultCallback callback);

private:
bat_ledger::LedgerImpl* ledger_; // NOT OWNED
};

} // namespace braveledger_state

#endif // BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V3_H_
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void Uphold::GetUser(GetUserCallback callback) {
user_->Get(callback);
}

void Uphold::CreateAnonAddressIfNecessary(CreateAnonAddressCallback callback) {
void Uphold::CreateAnonAddressIfNecessary(ledger::ResultCallback callback) {
card_->CreateAnonAddressIfNecessary(callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class UpholdWallet;
using FetchBalanceCallback = std::function<void(ledger::Result, double)>;
using CreateCardCallback =
std::function<void(ledger::Result, const std::string&)>;
using CreateAnonAddressCallback =
std::function<void(ledger::Result, const std::string&)>;

class Uphold {
public:
Expand Down Expand Up @@ -73,7 +71,7 @@ class Uphold {

void GetUser(GetUserCallback callback);

void CreateAnonAddressIfNecessary(CreateAnonAddressCallback callback);
void CreateAnonAddressIfNecessary(ledger::ResultCallback callback);

void OnTimer(const uint32_t timer_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@ void UpholdCard::OnGetCardAddresses(
callback(ledger::Result::LEDGER_OK, result);
}

void UpholdCard::CreateAnonAddressIfNecessary(
CreateAnonAddressCallback callback) {
void UpholdCard::CreateAnonAddressIfNecessary(ledger::ResultCallback callback) {
auto address_callback = std::bind(&UpholdCard::OnCreateAnonAddressIfNecessary,
this,
_1,
Expand All @@ -394,24 +393,34 @@ void UpholdCard::CreateAnonAddressIfNecessary(
void UpholdCard::OnCreateAnonAddressIfNecessary(
ledger::Result result,
std::map<std::string, std::string> addresses,
CreateAnonAddressCallback callback) {
ledger::ResultCallback callback) {
if (result == ledger::Result::LEDGER_OK && addresses.size() > 0) {
auto iter = addresses.find(kAnonID);
if (iter != addresses.end() && !iter->second.empty()) {
callback(ledger::Result::LEDGER_OK, iter->second);
auto wallets = ledger_->GetExternalWallets();
auto wallet = GetWallet(std::move(wallets));
if (!wallet) {
BLOG(0, "Wallet is null");
callback(ledger::Result::LEDGER_ERROR);
return;
}

wallet->anon_address = iter->second;
ledger_->SaveExternalWallet(ledger::kWalletUphold, std::move(wallet));
callback(ledger::Result::LEDGER_OK);
return;
}
}

CreateAnonAddress(callback);
}

void UpholdCard::CreateAnonAddress(CreateAnonAddressCallback callback) {
void UpholdCard::CreateAnonAddress(ledger::ResultCallback callback) {
auto wallets = ledger_->GetExternalWallets();
auto wallet = GetWallet(std::move(wallets));
if (!wallet) {
BLOG(0, "Wallet is null");
callback(ledger::Result::LEDGER_ERROR, "");
callback(ledger::Result::LEDGER_ERROR);
return;
}

Expand Down Expand Up @@ -443,42 +452,52 @@ void UpholdCard::CreateAnonAddress(CreateAnonAddressCallback callback) {

void UpholdCard::OnCreateAnonAddress(
const ledger::UrlResponse& response,
CreateAnonAddressCallback callback) {
ledger::ResultCallback callback) {
BLOG(6, ledger::UrlResponseToString(__func__, response));

if (response.status_code == net::HTTP_UNAUTHORIZED) {
callback(ledger::Result::EXPIRED_TOKEN, "");
callback(ledger::Result::EXPIRED_TOKEN);
uphold_->DisconnectWallet();
return;
}

if (response.status_code != net::HTTP_OK) {
callback(ledger::Result::LEDGER_ERROR, "");
callback(ledger::Result::LEDGER_ERROR);
return;
}

base::Optional<base::Value> value = base::JSONReader::Read(response.body);
if (!value || !value->is_dict()) {
BLOG(0, "Response is not JSON");
callback(ledger::Result::LEDGER_ERROR, "");
callback(ledger::Result::LEDGER_ERROR);
return;
}

base::DictionaryValue* dictionary = nullptr;
if (!value->GetAsDictionary(&dictionary)) {
BLOG(0, "Response is not JSON");
callback(ledger::Result::LEDGER_ERROR, "");
callback(ledger::Result::LEDGER_ERROR);
return;
}

const auto* id = dictionary->FindStringKey("id");
if (!id) {
BLOG(0, "ID not found");
callback(ledger::Result::LEDGER_ERROR, "");
callback(ledger::Result::LEDGER_ERROR);
return;
}

auto wallets = ledger_->GetExternalWallets();
auto wallet = GetWallet(std::move(wallets));
if (!wallet) {
BLOG(0, "Wallet is null");
callback(ledger::Result::LEDGER_ERROR);
return;
}

callback(ledger::Result::LEDGER_OK, *id);
wallet->anon_address = *id;
ledger_->SaveExternalWallet(ledger::kWalletUphold, std::move(wallet));
callback(ledger::Result::LEDGER_OK);
}

} // namespace braveledger_uphold
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class UpholdCard {

void CreateIfNecessary(CreateCardCallback callback);

void CreateAnonAddressIfNecessary(CreateAnonAddressCallback callback);
void CreateAnonAddressIfNecessary(ledger::ResultCallback callback);

private:
void OnCreateIfNecessary(
Expand Down Expand Up @@ -86,14 +86,14 @@ class UpholdCard {
void OnCreateAnonAddressIfNecessary(
ledger::Result result,
std::map<std::string, std::string> addresses,
CreateAnonAddressCallback callback);
ledger::ResultCallback callback);

void CreateAnonAddress(
CreateAnonAddressCallback callback);
ledger::ResultCallback callback);

void OnCreateAnonAddress(
const ledger::UrlResponse& response,
CreateAnonAddressCallback callback);
ledger::ResultCallback callback);

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
Uphold* uphold_; // NOT OWNED
Expand Down
Loading

0 comments on commit d64823f

Please sign in to comment.