Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove semi-verified state #9032

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion components/brave_rewards/browser/rewards_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
#endif
registry->RegisterUint64Pref(prefs::kPromotionLastFetchStamp, 0ull);
registry->RegisterBooleanPref(prefs::kPromotionCorruptedMigrated, false);
registry->RegisterBooleanPref(prefs::kAnonTransferChecked, false);
registry->RegisterIntegerPref(prefs::kVersion, 0);
registry->RegisterIntegerPref(prefs::kMinVisitTime, 8);
registry->RegisterIntegerPref(prefs::kMinVisits, 1);
Expand Down
1 change: 0 additions & 1 deletion components/brave_rewards/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const char kPromotionLastFetchStamp[] =
"brave.rewards.promotion_last_fetch_stamp";
const char kPromotionCorruptedMigrated[] =
"brave.rewards.promotion_corrupted_migrated2";
const char kAnonTransferChecked[] = "brave.rewards.anon_transfer_checked";
const char kVersion[] = "brave.rewards.version";
const char kMinVisitTime[] = "brave.rewards.ac.min_visit_time";
const char kMinVisits[] = "brave.rewards.ac.min_visits";
Expand Down
1 change: 0 additions & 1 deletion components/brave_rewards/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ extern const char kServerPublisherListStamp[];
extern const char kUpholdAnonAddress[]; // DEPRECATED
extern const char kPromotionLastFetchStamp[];
extern const char kPromotionCorruptedMigrated[];
extern const char kAnonTransferChecked[];
extern const char kVersion[];
extern const char kMinVisitTime[];
extern const char kMinVisits[];
Expand Down
2 changes: 0 additions & 2 deletions vendor/bat-native-ledger/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,6 @@ source_set("ledger") {
"src/bat/ledger/internal/wallet/wallet.h",
"src/bat/ledger/internal/wallet/wallet_balance.cc",
"src/bat/ledger/internal/wallet/wallet_balance.h",
"src/bat/ledger/internal/wallet/wallet_claim.cc",
"src/bat/ledger/internal/wallet/wallet_claim.h",
"src/bat/ledger/internal/wallet/wallet_create.cc",
"src/bat/ledger/internal/wallet/wallet_create.h",
"src/bat/ledger/internal/wallet/wallet_recover.cc",
Expand Down
11 changes: 0 additions & 11 deletions vendor/bat-native-ledger/src/bat/ledger/internal/state/state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,5 @@ uint64_t State::GetPromotionLastFetchStamp() {
return ledger_->ledger_client()->GetUint64State(kPromotionLastFetchStamp);
}

void State::SetAnonTransferChecked(const bool checked) {
ledger_->database()->SaveEventLog(
kAnonTransferChecked,
std::to_string(checked));
ledger_->ledger_client()->SetBooleanState(kAnonTransferChecked, checked);
}

bool State::GetAnonTransferChecked() {
return ledger_->ledger_client()->GetBooleanState(kAnonTransferChecked);
}

} // namespace state
} // namespace ledger
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ class State {

uint64_t GetPromotionLastFetchStamp();

void SetAnonTransferChecked(const bool checked);

bool GetAnonTransferChecked();

private:
LedgerImpl* ledger_; // NOT OWNED
std::unique_ptr<StateMigration> migration_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const char kUpholdAnonAddress[] = "uphold_anon_address"; // DEPRECATED
const char kPromotionLastFetchStamp[] = "promotion_last_fetch_stamp";
const char kPromotionCorruptedMigrated[] =
"promotion_corrupted_migrated2";
const char kAnonTransferChecked[] = "anon_transfer_checked";
const char kAnonTransferChecked[] = "anon_transfer_checked"; // DEPRECATED
const char kVersion[] = "version";
const char kMinVisitTime[] = "ac.min_visit_time";
const char kMinVisits[] = "ac.min_visits";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ using std::placeholders::_3;
namespace ledger {
namespace uphold {

UpholdAuthorization::UpholdAuthorization(LedgerImpl* ledger) :
ledger_(ledger),
uphold_server_(std::make_unique<endpoint::UpholdServer>(ledger)) {
}
UpholdAuthorization::UpholdAuthorization(LedgerImpl* ledger)
: ledger_(ledger),
promotion_server_(std::make_unique<endpoint::PromotionServer>(ledger)),
uphold_server_(std::make_unique<endpoint::UpholdServer>(ledger)) {}

UpholdAuthorization::~UpholdAuthorization() = default;

void UpholdAuthorization::Authorize(
const base::flat_map<std::string, std::string>& args,
ledger::ExternalWalletAuthorizationCallback callback) {
ledger::ExternalWalletAuthorizationCallback callback) const {
auto wallet = GetWallet(ledger_);

if (!wallet) {
Expand Down Expand Up @@ -99,19 +99,16 @@ void UpholdAuthorization::Authorize(
return;
}

auto url_callback = std::bind(&UpholdAuthorization::OnAuthorize,
this,
_1,
_2,
callback);
auto url_callback =
std::bind(&UpholdAuthorization::OnAuthorize, this, _1, _2, callback);

uphold_server_->post_oauth()->Request(code, url_callback);
}

void UpholdAuthorization::OnAuthorize(
const type::Result result,
const std::string& token,
ledger::ExternalWalletAuthorizationCallback callback) {
ledger::ExternalWalletAuthorizationCallback callback) const {
if (result == type::Result::EXPIRED_TOKEN) {
BLOG(0, "Expired token");
callback(type::Result::EXPIRED_TOKEN, {});
Expand Down Expand Up @@ -152,25 +149,17 @@ void UpholdAuthorization::OnAuthorize(
break;
}

// After a login, we want to attempt to relink the user's payment ID to their
// Uphold wallet address. Clear the flag that will cause relinking to be
// skipped.
ledger_->state()->SetAnonTransferChecked(false);

ledger_->uphold()->SetWallet(wallet_ptr->Clone());

auto user_callback = std::bind(&UpholdAuthorization::OnGetUser,
this,
_1,
_2,
callback);
auto user_callback =
std::bind(&UpholdAuthorization::OnGetUser, this, _1, _2, callback);
ledger_->uphold()->GetUser(user_callback);
}

void UpholdAuthorization::OnGetUser(
const type::Result result,
const User& user,
ledger::ExternalWalletAuthorizationCallback callback) {
ledger::ExternalWalletAuthorizationCallback callback) const {
auto wallet_ptr = GetWallet(ledger_);
base::flat_map<std::string, std::string> args;

Expand All @@ -181,17 +170,13 @@ void UpholdAuthorization::OnGetUser(
}

if (user.status == UserStatus::OK) {
wallet_ptr->status = user.verified
? type::WalletStatus::VERIFIED
: type::WalletStatus::CONNECTED;
wallet_ptr->status = user.verified ? type::WalletStatus::VERIFIED
: type::WalletStatus::CONNECTED;
ledger_->uphold()->SetWallet(wallet_ptr->Clone());

if (wallet_ptr->address.empty()) {
auto new_callback = std::bind(&UpholdAuthorization::OnCardCreate,
this,
_1,
_2,
callback);
auto new_callback =
std::bind(&UpholdAuthorization::OnCardCreate, this, _1, _2, callback);
ledger_->uphold()->CreateCard(new_callback);
return;
}
Expand All @@ -211,7 +196,7 @@ void UpholdAuthorization::OnGetUser(
void UpholdAuthorization::OnCardCreate(
const type::Result result,
const std::string& address,
ledger::ExternalWalletAuthorizationCallback callback) {
ledger::ExternalWalletAuthorizationCallback callback) const {
if (result == type::Result::LEDGER_ERROR) {
BLOG(0, "Card creation");
callback(type::Result::LEDGER_ERROR, {});
Expand All @@ -229,12 +214,95 @@ void UpholdAuthorization::OnCardCreate(
address.substr(0, 5));
}

base::flat_map<std::string, std::string> args;
if (wallet_ptr->status != type::WalletStatus::VERIFIED) {
base::flat_map<std::string, std::string> args{};
args["redirect_url"] = GetSecondStepVerify();
callback(type::Result::LEDGER_OK, args);
}

callback(type::Result::LEDGER_OK, args);
GetAnonFunds(
std::bind(&UpholdAuthorization::OnGetAnonFunds, this, _1, _2, callback));
}

void UpholdAuthorization::GetAnonFunds(
endpoint::promotion::GetWalletBalanceCallback callback) const {
// if we don't have user funds in anon card anymore
// we can skip balance server ping
if (!ledger_->state()->GetFetchOldBalanceEnabled()) {
callback(type::Result::LEDGER_OK, type::Balance::New());
return;
}

const auto wallet = ledger_->wallet()->GetWallet();
if (!wallet) {
BLOG(1, "Wallet is not created!");
ledger_->state()->SetFetchOldBalanceEnabled(false);
callback(type::Result::LEDGER_OK, type::Balance::New());
return;
}

if (wallet->payment_id.empty()) {
BLOG(0, "Payment ID is empty!");
Copy link
Member

@bsclifton bsclifton Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely not something we should address with this PR, but I'm curious (@zenparsing, @emerick, @szilardszaloki) if it would make sense to switch to using constants for the info levels?

// BAT Ledger verbose levels:
//
// 0 Error
// 1 Info
// 5 URL request
// 6 URL response
// 7 URL response (with large body)
// 8 Database queries
// 9 Detailed debugging (response headers, etc)

We could address by making an enum

enum LedgerLogLevel {
    Error,
    Info,
    UrlRequest = 5,
    UrlResponse,
    UrlResponseWithBody,
    DbQueries
};
// ...
if (wallet->payment_id.empty()) {
    BLOG(LedgerLogLevel::Error, "Payment ID is empty!");
    // ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a lot more informative.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely like that idea.

callback(type::Result::LEDGER_ERROR, nullptr);
return;
}

promotion_server_->get_wallet_balance()->Request(callback);
}

void UpholdAuthorization::OnGetAnonFunds(
const type::Result result,
type::BalancePtr balance,
ledger::ExternalWalletAuthorizationCallback callback) const {
if (result != type::Result::LEDGER_OK || !balance) {
BLOG(0, "Couldn't get anonymous funds!");
callback(type::Result::LEDGER_ERROR, {});
return;
}

if (balance->user_funds == 0.0) { // == floating-point comparison!
ledger_->state()->SetFetchOldBalanceEnabled(false);
}

LinkWallet(balance->user_funds,
std::bind(&UpholdAuthorization::OnLinkWallet, this, _1, callback));
}

void UpholdAuthorization::LinkWallet(
const double user_funds,
ledger::endpoint::promotion::PostClaimUpholdCallback callback) const {
if (!uphold::GetWallet(ledger_)) {
BLOG(0, "Wallet is null!");
callback(type::Result::LEDGER_ERROR);
return;
}

promotion_server_->post_claim_uphold()->Request(user_funds, callback);
}

void UpholdAuthorization::OnLinkWallet(
const type::Result result,
ledger::ExternalWalletAuthorizationCallback callback) const {
if (result == type::Result::LEDGER_OK) {
callback(type::Result::LEDGER_OK, {});
return;
}

if (result == type::Result::ALREADY_EXISTS) {
ledger_->ledger_client()->ShowNotification("wallet_device_limit_reached",
{}, [](type::Result) {});

std::string event_text = "uphold";
if (auto wallet_ptr = uphold::GetWallet(ledger_))
event_text += "/" + wallet_ptr->address.substr(0, 5);

ledger_->database()->SaveEventLog(log::kDeviceLimitReached, event_text);

callback(type::Result::ALREADY_EXISTS, {});
return;
}

callback(type::Result::LEDGER_ERROR, {});
}

} // namespace uphold
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* 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_UPHOLD_UPHOLD_AUTHORIZATION_H_
#define BRAVELEDGER_UPHOLD_UPHOLD_AUTHORIZATION_H_
#ifndef BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_AUTHORIZATION_H_
#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_AUTHORIZATION_H_

#include <memory>
#include <string>

#include "base/containers/flat_map.h"
#include "bat/ledger/internal/endpoint/promotion/promotion_server.h"
#include "bat/ledger/internal/endpoint/uphold/uphold_server.h"
#include "bat/ledger/internal/uphold/uphold_user.h"
#include "bat/ledger/ledger.h"
Expand All @@ -25,30 +26,44 @@ class UpholdAuthorization {

~UpholdAuthorization();

void Authorize(
const base::flat_map<std::string, std::string>& args,
ledger::ExternalWalletAuthorizationCallback callback);
void Authorize(const base::flat_map<std::string, std::string>& args,
ledger::ExternalWalletAuthorizationCallback callback) const;

private:
void OnAuthorize(
const type::Result result,
const std::string& token,
ledger::ExternalWalletAuthorizationCallback callback);
void GetAnonFunds(
szilardszaloki marked this conversation as resolved.
Show resolved Hide resolved
endpoint::promotion::GetWalletBalanceCallback callback) const;

void OnGetUser(
const type::Result result,
const User& user,
ledger::ExternalWalletAuthorizationCallback callback);
void LinkWallet(const double user_funds,
endpoint::promotion::PostClaimUpholdCallback callback) const;

void OnAuthorize(const type::Result result,
const std::string& token,
ledger::ExternalWalletAuthorizationCallback callback) const;

void OnGetUser(const type::Result result,
const User& user,
ledger::ExternalWalletAuthorizationCallback callback) const;

void OnCardCreate(
void OnCardCreate(const type::Result result,
const std::string& address,
ledger::ExternalWalletAuthorizationCallback callback) const;

void OnGetAnonFunds(
const type::Result result,
const std::string& address,
ledger::ExternalWalletAuthorizationCallback callback);
type::BalancePtr balance,
ledger::ExternalWalletAuthorizationCallback callback) const;

void OnLinkWallet(const type::Result result,
ledger::ExternalWalletAuthorizationCallback callback) const;

LedgerImpl* ledger_; // NOT OWNED
std::unique_ptr<endpoint::PromotionServer> promotion_server_;
std::unique_ptr<endpoint::UpholdServer> uphold_server_;

FRIEND_TEST_ALL_PREFIXES(GetAnonFundsTest, AnonFundsFlow);
FRIEND_TEST_ALL_PREFIXES(LinkWalletTest, LinkWalletFlow);
};

} // namespace uphold
} // namespace ledger
#endif // BRAVELEDGER_UPHOLD_UPHOLD_AUTHORIZATION_H_
#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_UPHOLD_UPHOLD_AUTHORIZATION_H_
Loading