Skip to content

Commit

Permalink
Merge pull request #6428 from brave/pr6384_claim-200_1.13.x
Browse files Browse the repository at this point in the history
Fixes claim for existing wallets (uplift to 1.13.x)
  • Loading branch information
kjozwiak authored Aug 25, 2020
2 parents 391947b + 513eb36 commit bda7fb7
Show file tree
Hide file tree
Showing 18 changed files with 11 additions and 297 deletions.
1 change: 0 additions & 1 deletion components/brave_rewards/browser/external_wallet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace brave_rewards {
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: 0 additions & 1 deletion components/brave_rewards/browser/external_wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ 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: 0 additions & 7 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3186,7 +3186,6 @@ 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 @@ -3255,11 +3254,6 @@ 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 @@ -3285,7 +3279,6 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,6 @@ void RewardsBrowserTestResponse::LoadMocks() {
path.AppendASCII("uphold_commit_resp.json"),
&uphold_commit_resp_));

ASSERT_TRUE(base::ReadFileToString(
path.AppendASCII("uphold_addresses_resp.json"),
&uphold_addresses_resp_));

std::vector<std::string> publisher_keys {
"bumpsmack.com",
"duckduckgo.com",
Expand Down Expand Up @@ -340,11 +336,6 @@ void RewardsBrowserTestResponse::Get(
"commit",
base::CompareCase::INSENSITIVE_ASCII)) {
*response = uphold_commit_resp_;
} else if (base::EndsWith(
url,
"addresses",
base::CompareCase::INSENSITIVE_ASCII)) {
*response = uphold_addresses_resp_;
} else {
*response = rewards_browsertest_util::GetUpholdCard(
external_balance_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class RewardsBrowserTestResponse {
std::string uphold_auth_resp_;
std::string uphold_transactions_resp_;
std::string uphold_commit_resp_;
std::string uphold_addresses_resp_;
std::string user_funds_balance_resp_;

std::vector<Request> requests_;
Expand Down
11 changes: 0 additions & 11 deletions test/data/rewards-data/uphold_addresses_resp.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ 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 @@ -139,7 +139,7 @@ ledger::Result ParseRecoverWallet(
// POST /v3/wallet/uphold/{payment_id}/claim
//
// Success:
// OK (201)
// OK (200)
//
// Response Format:
// {Empty body}
Expand Down Expand Up @@ -175,7 +175,7 @@ ledger::Result CheckClaimWallet(const ledger::UrlResponse& response) {
return ledger::Result::LEDGER_ERROR;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +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)),
v3_(std::make_unique<StateMigrationV3>()),
v4_(std::make_unique<StateMigrationV4>(ledger)),
ledger_(ledger) {
DCHECK(v1_ && v2_ && v3_ && v4_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,18 @@
* 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;

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);
}

// In this migration we migrated anon address to uphold wallet in preferences
// because anon address was removed we can also remove this step
// Ref: https://github.com/brave/brave-browser/issues/11150
callback(ledger::Result::LEDGER_OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,16 @@
#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();
~StateMigrationV3();

void Migrate(ledger::ResultCallback callback);

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

} // namespace braveledger_state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ void Uphold::GetUser(GetUserCallback callback) {
user_->Get(callback);
}

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

void Uphold::SaveTransferFee(ledger::TransferFeePtr transfer_fee) {
if (!transfer_fee) {
BLOG(0, "Transfer fee is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class Uphold {

void GetUser(GetUserCallback callback);

void CreateAnonAddressIfNecessary(ledger::ResultCallback callback);

void OnTimer(const uint32_t timer_id);

private:
Expand Down
161 changes: 0 additions & 161 deletions vendor/bat-native-ledger/src/bat/ledger/internal/uphold/uphold_card.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,165 +242,4 @@ void UpholdCard::OnUpdate(
callback(ledger::Result::LEDGER_OK);
}

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

const auto headers = RequestAuthorization(wallet->token);
const std::string path = base::StringPrintf(
"/v0/me/cards/%s/addresses",
wallet->address.c_str());

auto address_callback = std::bind(&UpholdCard::OnGetCardAddresses,
this,
_1,
callback);

const auto url = GetAPIUrl(path);
ledger_->LoadURL(
url,
headers,
"",
"application/json",
ledger::UrlMethod::GET,
address_callback);
}

void UpholdCard::OnGetCardAddresses(
const ledger::UrlResponse& response,
GetCardAddressesCallback callback) {
BLOG(6, ledger::UrlResponseToString(__func__, response));

std::map<std::string, std::string> addresses;
const ledger::Result result =
braveledger_response_util::ParseUpholdGetCardAddresses(
response,
&addresses);

if (result == ledger::Result::EXPIRED_TOKEN) {
BLOG(0, "Expired token");
callback(ledger::Result::EXPIRED_TOKEN, {});
uphold_->DisconnectWallet();
return;
}

if (result != ledger::Result::LEDGER_OK) {
BLOG(0, "Couldn't get card addresses");
callback(ledger::Result::LEDGER_ERROR, {});
return;
}

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

void UpholdCard::CreateAnonAddressIfNecessary(ledger::ResultCallback callback) {
auto address_callback = std::bind(&UpholdCard::OnCreateAnonAddressIfNecessary,
this,
_1,
_2,
callback);

GetCardAddresses(address_callback);
}

void UpholdCard::OnCreateAnonAddressIfNecessary(
ledger::Result result,
std::map<std::string, std::string> addresses,
ledger::ResultCallback callback) {
if (result == ledger::Result::LEDGER_OK && addresses.size() > 0) {
auto iter = addresses.find(kAnonID);
if (iter != addresses.end() && !iter->second.empty()) {
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(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);
return;
}

const auto headers = RequestAuthorization(wallet->token);
const std::string path = base::StringPrintf(
"/v0/me/cards/%s/addresses",
wallet->address.c_str());

const std::string payload = base::StringPrintf(
R"({
"network": "%s"
})",
kAnonID);

auto anon_callback = std::bind(&UpholdCard::OnCreateAnonAddress,
this,
_1,
callback);

const auto url = GetAPIUrl(path);
ledger_->LoadURL(
url,
headers,
payload,
"application/json",
ledger::UrlMethod::POST,
anon_callback);
}

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

std::string id;
const ledger::Result result =
braveledger_response_util::ParseUpholdCreateCard(response, &id);
if (result == ledger::Result::EXPIRED_TOKEN) {
BLOG(0, "Expired token");
callback(ledger::Result::EXPIRED_TOKEN);
uphold_->DisconnectWallet();
return;
}

if (result != ledger::Result::LEDGER_OK || id.empty()) {
BLOG(0, "Couldn't create anon address");
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;
}

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

} // namespace braveledger_uphold
Loading

0 comments on commit bda7fb7

Please sign in to comment.