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

Adds WalletStatus change events for bitFlyer and Gemini. #14320

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "bat/ledger/internal/wallet/wallet_util.h"
#include "brave_base/random.h"

using ledger::wallet::OnWalletStatusChange;
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
Expand Down Expand Up @@ -165,15 +166,16 @@ void Bitflyer::DisconnectWallet(const bool manual) {
}

BLOG(1, "Disconnecting wallet");
ledger_->database()->SaveEventLog(log::kWalletDisconnected,
std::string(constant::kWalletBitflyer) +
(!wallet->address.empty() ? "/" : "") +
wallet->address.substr(0, 5));
const std::string wallet_address = wallet->address;

wallet = ::ledger::wallet::ResetWallet(std::move(wallet));
const auto from = wallet->status;
wallet = ledger::wallet::ResetWallet(std::move(wallet));
if (manual) {
wallet->status = type::WalletStatus::NOT_CONNECTED;
}
const auto to = wallet->status;

OnWalletStatusChange(ledger_, from, to);

const bool shutting_down = ledger_->IsShuttingDown();

Expand All @@ -182,11 +184,16 @@ void Bitflyer::DisconnectWallet(const bool manual) {
ledger::notifications::kWalletDisconnected, {}, [](type::Result) {});
}

SetWallet(wallet->Clone());
SetWallet(std::move(wallet));

if (!shutting_down) {
ledger_->ledger_client()->WalletDisconnected(constant::kWalletBitflyer);
}

ledger_->database()->SaveEventLog(log::kWalletDisconnected,
std::string(constant::kWalletBitflyer) +
(!wallet_address.empty() ? "/" : "") +
wallet_address.substr(0, 5));
}

void Bitflyer::SaveTransferFee(const std::string& contribution_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#include "bat/ledger/internal/logging/event_log_keys.h"
#include "bat/ledger/internal/logging/event_log_util.h"
#include "bat/ledger/internal/notifications/notification_keys.h"
#include "bat/ledger/internal/wallet/wallet_util.h"
#include "crypto/sha2.h"

using ledger::wallet::OnWalletStatusChange;
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
Expand Down Expand Up @@ -205,20 +207,20 @@ void BitflyerAuthorization::OnClaimWallet(
}
}

const auto from = wallet_ptr->status;
const auto to = wallet_ptr->status = type::WalletStatus::VERIFIED;
wallet_ptr->token = token;
wallet_ptr->address = address;

switch (wallet_ptr->status) {
case type::WalletStatus::NOT_CONNECTED:
case type::WalletStatus::DISCONNECTED_NOT_VERIFIED:
case type::WalletStatus::DISCONNECTED_VERIFIED:
wallet_ptr->status = type::WalletStatus::VERIFIED;
break;
default:
break;
if (!ledger_->bitflyer()->SetWallet(std::move(wallet_ptr))) {
BLOG(0, "Unable to set bitFlyer wallet!");
return callback(type::Result::LEDGER_ERROR, {});
}

ledger_->bitflyer()->SetWallet(std::move(wallet_ptr));
OnWalletStatusChange(ledger_, from, to);
ledger_->database()->SaveEventLog(
log::kWalletVerified,
constant::kWalletBitflyer + std::string("/") + address.substr(0, 5));
callback(type::Result::LEDGER_OK, {});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#include "bat/ledger/internal/common/random_util.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/logging/event_log_keys.h"
#include "bat/ledger/internal/wallet/wallet_util.h"

using ledger::wallet::OnWalletStatusChange;

namespace ledger {
namespace bitflyer {
Expand All @@ -25,6 +28,12 @@ void BitflyerWallet::Generate(ledger::ResultCallback callback) {
wallet = type::ExternalWallet::New();
wallet->type = constant::kWalletBitflyer;
wallet->status = type::WalletStatus::NOT_CONNECTED;
if (!ledger_->bitflyer()->SetWallet(wallet->Clone())) {
BLOG(0, "Unable to set bitFlyer wallet!");
return std::move(callback).Run(type::Result::LEDGER_ERROR);
}

OnWalletStatusChange(ledger_, {}, wallet->status);
}

if (wallet->one_time_string.empty()) {
Expand All @@ -35,14 +44,23 @@ void BitflyerWallet::Generate(ledger::ResultCallback callback) {
wallet->code_verifier = util::GeneratePKCECodeVerifier();
}

absl::optional<type::WalletStatus> from;
if (wallet->token.empty() &&
(wallet->status == type::WalletStatus::PENDING ||
wallet->status == type::WalletStatus::CONNECTED)) {
from = wallet->status;
wallet->status = type::WalletStatus::NOT_CONNECTED;
}

wallet = GenerateLinks(std::move(wallet));
ledger_->bitflyer()->SetWallet(wallet->Clone());
if (!ledger_->bitflyer()->SetWallet(wallet->Clone())) {
BLOG(0, "Unable to set bitFlyer wallet!");
return std::move(callback).Run(type::Result::LEDGER_ERROR);
}

if (from) {
OnWalletStatusChange(ledger_, from, wallet->status);
}

if (wallet->status == type::WalletStatus::VERIFIED ||
wallet->status == type::WalletStatus::DISCONNECTED_VERIFIED) {
Expand Down
19 changes: 13 additions & 6 deletions vendor/bat-native-ledger/src/bat/ledger/internal/gemini/gemini.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "bat/ledger/internal/wallet/wallet_util.h"
#include "brave_base/random.h"

using ledger::wallet::OnWalletStatusChange;
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
Expand Down Expand Up @@ -166,15 +167,16 @@ void Gemini::DisconnectWallet(const bool manual) {
}

BLOG(1, "Disconnecting wallet");
ledger_->database()->SaveEventLog(log::kWalletDisconnected,
std::string(constant::kWalletGemini) +
(!wallet->address.empty() ? "/" : "") +
wallet->address.substr(0, 5));
const std::string wallet_address = wallet->address;

wallet = ::ledger::wallet::ResetWallet(std::move(wallet));
const auto from = wallet->status;
wallet = ledger::wallet::ResetWallet(std::move(wallet));
if (manual) {
wallet->status = type::WalletStatus::NOT_CONNECTED;
}
const auto to = wallet->status;

OnWalletStatusChange(ledger_, from, to);

const bool shutting_down = ledger_->IsShuttingDown();

Expand All @@ -183,11 +185,16 @@ void Gemini::DisconnectWallet(const bool manual) {
ledger::notifications::kWalletDisconnected, {}, [](type::Result) {});
}

SetWallet(wallet->Clone());
SetWallet(std::move(wallet));

if (!shutting_down) {
ledger_->ledger_client()->WalletDisconnected(constant::kWalletGemini);
}

ledger_->database()->SaveEventLog(log::kWalletDisconnected,
std::string(constant::kWalletGemini) +
(!wallet_address.empty() ? "/" : "") +
wallet_address.substr(0, 5));
}

void Gemini::SaveTransferFee(const std::string& contribution_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#include "bat/ledger/internal/logging/event_log_keys.h"
#include "bat/ledger/internal/logging/event_log_util.h"
#include "bat/ledger/internal/notifications/notification_keys.h"
#include "bat/ledger/internal/wallet/wallet_util.h"
#include "crypto/sha2.h"

using ledger::wallet::OnWalletStatusChange;
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
Expand Down Expand Up @@ -236,19 +238,19 @@ void GeminiAuthorization::OnClaimWallet(
}
}

const auto from = wallet_ptr->status;
const auto to = wallet_ptr->status = type::WalletStatus::VERIFIED;
wallet_ptr->address = recipient_id;

switch (wallet_ptr->status) {
case type::WalletStatus::NOT_CONNECTED:
case type::WalletStatus::DISCONNECTED_NOT_VERIFIED:
case type::WalletStatus::DISCONNECTED_VERIFIED:
wallet_ptr->status = type::WalletStatus::VERIFIED;
break;
default:
break;
if (!ledger_->gemini()->SetWallet(std::move(wallet_ptr))) {
BLOG(0, "Unable to set Gemini wallet!");
return callback(type::Result::LEDGER_ERROR, {});
}

ledger_->gemini()->SetWallet(std::move(wallet_ptr));
OnWalletStatusChange(ledger_, from, to);
ledger_->database()->SaveEventLog(
log::kWalletVerified,
constant::kWalletGemini + std::string("/") + recipient_id.substr(0, 5));
callback(type::Result::LEDGER_OK, {});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include "bat/ledger/internal/gemini/gemini_wallet.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/logging/event_log_keys.h"
#include "bat/ledger/internal/wallet/wallet_util.h"

using ledger::wallet::OnWalletStatusChange;

namespace ledger {
namespace gemini {
Expand All @@ -26,19 +29,34 @@ void GeminiWallet::Generate(ledger::ResultCallback callback) {
wallet = type::ExternalWallet::New();
wallet->type = constant::kWalletGemini;
wallet->status = type::WalletStatus::NOT_CONNECTED;
if (!ledger_->gemini()->SetWallet(wallet->Clone())) {
BLOG(0, "Unable to set Gemini wallet!");
return std::move(callback).Run(type::Result::LEDGER_ERROR);
}

OnWalletStatusChange(ledger_, {}, wallet->status);
}

if (wallet->one_time_string.empty()) {
wallet->one_time_string = util::GenerateRandomHexString();
}

absl::optional<type::WalletStatus> from;
if (wallet->token.empty() &&
(wallet->status == type::WalletStatus::PENDING)) {
from = wallet->status;
wallet->status = type::WalletStatus::NOT_CONNECTED;
}

wallet = GenerateLinks(std::move(wallet));
ledger_->gemini()->SetWallet(wallet->Clone());
if (!ledger_->gemini()->SetWallet(wallet->Clone())) {
BLOG(0, "Unable to set Gemini wallet!");
return std::move(callback).Run(type::Result::LEDGER_ERROR);
}

if (from) {
OnWalletStatusChange(ledger_, from, wallet->status);
}

if (wallet->status == type::WalletStatus::VERIFIED ||
wallet->status == type::WalletStatus::DISCONNECTED_VERIFIED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "bat/ledger/internal/wallet/wallet_util.h"
#include "brave_base/random.h"

using ledger::wallet::OnWalletStatusChange;
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
Expand Down Expand Up @@ -184,7 +185,7 @@ void Uphold::DisconnectWallet(const absl::optional<std::string>& notification) {
}

BLOG(1, "Disconnecting wallet");
const std::string uphold_wallet_address = wallet->address;
const std::string wallet_address = wallet->address;

const bool manual = !notification.has_value();

Expand All @@ -211,11 +212,10 @@ void Uphold::DisconnectWallet(const absl::optional<std::string>& notification) {
ledger_->ledger_client()->WalletDisconnected(constant::kWalletUphold);
}

ledger_->database()->SaveEventLog(
log::kWalletDisconnected,
std::string(constant::kWalletUphold) +
(!uphold_wallet_address.empty() ? "/" : "") +
uphold_wallet_address.substr(0, 5));
ledger_->database()->SaveEventLog(log::kWalletDisconnected,
std::string(constant::kWalletUphold) +
(!wallet_address.empty() ? "/" : "") +
wallet_address.substr(0, 5));
}

void Uphold::GetUser(GetUserCallback callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/logging/event_log_keys.h"
#include "bat/ledger/internal/uphold/uphold_util.h"
#include "bat/ledger/internal/wallet/wallet_util.h"

using ledger::wallet::OnWalletStatusChange;
using std::placeholders::_1;
using std::placeholders::_2;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
#include "base/strings/stringprintf.h"
#include "bat/ledger/buildflags.h"
#include "bat/ledger/global_constants.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/logging/event_log_keys.h"
#include "bat/ledger/internal/state/state_keys.h"
#include "bat/ledger/internal/uphold/uphold_util.h"
#include "crypto/random.h"
Expand Down Expand Up @@ -132,37 +130,6 @@ type::ExternalWalletPtr GenerateLinks(type::ExternalWalletPtr wallet) {
return wallet;
}

template <typename T, typename... Ts>
bool one_of(T&& t, Ts&&... ts) {
bool match = false;

static_cast<void>(std::initializer_list<bool>{
(match = match || std::forward<T>(t) == std::forward<Ts>(ts))...});

return match;
}

void OnWalletStatusChange(LedgerImpl* ledger,
absl::optional<type::WalletStatus> from,
type::WalletStatus to) {
DCHECK(ledger);
DCHECK(!from ||
one_of(*from, type::WalletStatus::NOT_CONNECTED,
type::WalletStatus::DISCONNECTED_VERIFIED,
type::WalletStatus::PENDING, type::WalletStatus::VERIFIED));
DCHECK(one_of(to, type::WalletStatus::NOT_CONNECTED,
type::WalletStatus::DISCONNECTED_VERIFIED,
type::WalletStatus::PENDING, type::WalletStatus::VERIFIED));

std::ostringstream oss{};
if (from) {
oss << *from << ' ';
}
oss << "==> " << to;

ledger->database()->SaveEventLog(log::kWalletStatusChange, oss.str());
}

void CheckWalletState(const type::ExternalWallet* wallet) {
if (!wallet)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ std::string GetActivityUrl(const std::string& address);

type::ExternalWalletPtr GenerateLinks(type::ExternalWalletPtr wallet);

void OnWalletStatusChange(LedgerImpl* ledger,
absl::optional<type::WalletStatus> from,
type::WalletStatus to);

void CheckWalletState(const type::ExternalWallet* wallet);

} // namespace uphold
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#include "bat/ledger/internal/logging/event_log_util.h"
#include "bat/ledger/internal/notifications/notification_keys.h"
#include "bat/ledger/internal/uphold/uphold_util.h"
#include "bat/ledger/internal/wallet/wallet_util.h"

using ledger::uphold::Capabilities;
using ledger::wallet::OnWalletStatusChange;

namespace ledger {
namespace uphold {
Expand Down
Loading