Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Adds wallet properties #26

Merged
merged 1 commit into from
Aug 1, 2018
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
1 change: 1 addition & 0 deletions include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class LEDGER_EXPORT Ledger {
virtual unsigned int GetPublisherMinVisits() const = 0;
virtual bool GetPublisherAllowNonVerified() const = 0;
virtual double GetContributionAmount() const = 0;
virtual void GetWalletProperties() const = 0;
};

} // namespace ledger
Expand Down
3 changes: 3 additions & 0 deletions include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "bat/ledger/ledger_task_runner.h"
#include "bat/ledger/ledger_url_loader.h"
#include "bat/ledger/publisher_info.h"
#include "bat/ledger/wallet_info.h"

namespace ledger {

Expand All @@ -35,6 +36,8 @@ class LEDGER_EXPORT LedgerClient {
// called when the wallet creation has completed
virtual std::string GenerateGUID() const = 0;
virtual void OnWalletCreated(Result result) = 0;
virtual void GetWalletProperties() = 0;
virtual void OnWalletProperties(ledger::WalletInfo) = 0;
virtual void OnReconcileComplete(Result result,
const std::string& viewing_id) = 0;

Expand Down
38 changes: 38 additions & 0 deletions include/bat/ledger/wallet_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* 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 BAT_LEDGER_WALLET_INFO_HANDLER_
#define BAT_LEDGER_WALLET_INFO_HANDLER_

#include <string>
#include <map>
#include <vector>

#include "bat/ledger/export.h"

namespace ledger {

struct GRANT {
std::string altcurrency;
std::string probi;
unsigned int expiryTime;
};

LEDGER_EXPORT struct WalletInfo {
WalletInfo();
~WalletInfo();
WalletInfo(const WalletInfo& info);
std::string altcurrency_;
std::string probi_;
Copy link
Contributor

Choose a reason for hiding this comment

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

probi?

Copy link
Contributor Author

@NejcZdovc NejcZdovc Jul 27, 2018

Choose a reason for hiding this comment

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

it's the representation of balance but it's a big number with 18 places. Same thing as satoshi for BTC. So 15 balance is 15000000000000000000 probi

double balance_;
std::map<std::string, double> rates_;
std::vector<double> parameters_choices_;
std::vector<double> parameters_range_;
unsigned int parameters_days_;
std::vector<GRANT> grants_;
};

} // namespace ledger

#endif // BAT_LEDGER_WALLET_INFO_HANDLER_
12 changes: 12 additions & 0 deletions src/bat/ledger/ledger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,16 @@ ledger::Ledger* Ledger::CreateInstance(LedgerClient* client) {
return new bat_ledger::LedgerImpl(client);
}

WalletInfo::WalletInfo () : balance_(0), parameters_days_(0) {}
WalletInfo::~WalletInfo () {}
WalletInfo::WalletInfo (const ledger::WalletInfo &info) {
altcurrency_ = info.altcurrency_;
probi_ = info.probi_;
balance_ = info.balance_;
rates_ = info.rates_;
parameters_choices_ = info.parameters_choices_;
parameters_range_ = info.parameters_range_;
parameters_days_ = info.parameters_days_;
grants_ = info.grants_;
}
}
72 changes: 41 additions & 31 deletions src/bat_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@ using namespace std::placeholders;

namespace braveledger_bat_client {

// namespace {
// std::string GetBalanceURL(const std::string& path, const std::string& prefix) {
// std::string url;
// if (braveledger_ledger::g_isProduction) {
// url = BALANCE_PRODUCTION_SERVER;
// } else {
// url = BALANCE_STAGING_SERVER;
// }

// return url + prefix + path;
// }
// }

BatClient::BatClient(bat_ledger::LedgerImpl* ledger) :
ledger_(ledger),
state_(new braveledger_bat_helper::CLIENT_STATE_ST()),
Expand All @@ -44,12 +31,20 @@ BatClient::BatClient(bat_ledger::LedgerImpl* ledger) :
BatClient::~BatClient() {
}

std::string BatClient::buildURL(const std::string& path, const std::string& prefix) {
std::string BatClient::buildURL(const std::string& path, const std::string& prefix, const bool isBalance = false) {
std::string url;
if (braveledger_ledger::g_isProduction) {
url = LEDGER_PRODUCTION_SERVER;
if (isBalance) {
if (braveledger_ledger::g_isProduction) {
url = BALANCE_PRODUCTION_SERVER;
} else {
url = BALANCE_STAGING_SERVER;
}
} else {
url = LEDGER_STAGING_SERVER;
if (braveledger_ledger::g_isProduction) {
url = LEDGER_PRODUCTION_SERVER;
} else {
url = LEDGER_STAGING_SERVER;
}
}

return url + prefix + path;
Expand Down Expand Up @@ -214,20 +209,35 @@ const std::string& BatClient::getLTCAddress() const {
return state_->walletInfo_.addressLTC_;
}

// void BatClient::getWalletProperties(const braveledger_bat_helper::FETCH_CALLBACK_EXTRA_DATA_ST& extraData) {
// auto request_id = ledger_->LoadURL(
// GetBalanceURL((std::string)WALLET_PROPERTIES + state_->walletInfo_.paymentId_ + WALLET_PROPERTIES_END, ""),
// std::vector<std::string>(),
// "",
// "",
// ledger::URL_METHOD::GET, &handler_);
// handler_.AddRequestHandler(std::move(request_id),
// std::bind(&BatClient::publisherTimestampCallback,
// this,
// _1,
// _2,
// extra_data));
// }
void BatClient::getWalletProperties() {
std::string path = (std::string)WALLET_PROPERTIES + state_->walletInfo_.paymentId_ + WALLET_PROPERTIES_END;
auto request_id = ledger_->LoadURL(
buildURL(path, PREFIX_V2, true),
std::vector<std::string>(),
"",
"",
ledger::URL_METHOD::GET,
&handler_);

handler_.AddRequestHandler(std::move(request_id),
std::bind(&BatClient::walletPropertiesCallback,
this,
_1,
_2));
}

void BatClient::walletPropertiesCallback(bool success,
const std::string& response) {
if (!success) {
LOG(ERROR) << "walletPropertiesCallback error";
return;
}

braveledger_bat_helper::WALLET_PROPERTIES_ST properties;
braveledger_bat_helper::loadFromJson(properties, response);
state_->walletProperties_ = properties;
ledger_->OnWalletProperties(properties);
}

bool BatClient::isReadyForReconcile() {
// TODO real check of reconcile timestamp
Expand Down
5 changes: 3 additions & 2 deletions src/bat_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ class BatClient : public ledger::LedgerCallbackHandler {
void votePublishers(const std::vector<std::string>& publishers, const std::string& viewingId);
void prepareBallots();
std::string getWalletPassphrase();
void walletPropertiesCallback(bool success, const std::string& response);
void recoverWallet(const std::string& passPhrase);
void getPromotion(const std::string& lang, const std::string& forPaymentId);
void setPromotion(const std::string& promotionId, const std::string& captchaResponse);
void getPromotionCaptcha();
//void getWalletProperties(const braveledger_bat_helper::FETCH_CALLBACK_EXTRA_DATA_ST& extraData);
void getWalletProperties();

private:
void saveState();
Expand Down Expand Up @@ -76,7 +77,7 @@ class BatClient : public ledger::LedgerCallbackHandler {
void viewingCredentials(const std::string& proofStringified, const std::string& anonizeViewingId);
void viewingCredentialsCallback(bool result, const std::string& response);
std::string getAnonizeProof(const std::string& registrarVK, const std::string& id, std::string& preFlight);
std::string buildURL(const std::string& path, const std::string& prefix);
std::string buildURL(const std::string& path, const std::string& prefix, const bool isBalance);

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
std::unique_ptr<braveledger_bat_helper::CLIENT_STATE_ST> state_;
Expand Down
66 changes: 57 additions & 9 deletions src/bat_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <ctime>
#include <memory>
#include <iostream>
#include <string>

#include <openssl/base64.h>
#include <openssl/digest.h>
Expand Down Expand Up @@ -741,33 +742,53 @@ namespace braveledger_bat_helper {
WINNERS_ST::~WINNERS_ST() {}

/////////////////////////////////////////////////////////////////////////////
WALLET_PROPERTIES_ST::WALLET_PROPERTIES_ST() {}
WALLET_PROPERTIES_ST::WALLET_PROPERTIES_ST() : balance_(0), parameters_days_(0) {}

WALLET_PROPERTIES_ST::~WALLET_PROPERTIES_ST() {}

WALLET_PROPERTIES_ST::WALLET_PROPERTIES_ST(const WALLET_PROPERTIES_ST &properties) {
altcurrency_ = properties.altcurrency_;
probi_ = properties.probi_;
balance_ = properties.balance_;
rates_ = properties.rates_;
parameters_choices_ = properties.parameters_choices_;
parameters_range_ = properties.parameters_range_;
parameters_days_ = properties.parameters_days_;
grants_ = properties.grants_;
}

bool WALLET_PROPERTIES_ST::loadFromJson(const std::string & json) {
rapidjson::Document d;
d.Parse(json.c_str());

//has parser errors or wrong types
bool error = d.HasParseError();
if (false == error) {
error = !(d.HasMember("altcurrency") && d["altcurrency"].IsString() &&
d.HasMember("balance") && d["balance"].IsDouble() &&
error = !(
d.HasMember("altcurrency") && d["altcurrency"].IsString() &&
d.HasMember("balance") && d["balance"].IsString() &&
d.HasMember("probi") && d["probi"].IsString() &&
d.HasMember("rates") && d["rates"].IsObject() &&
d.HasMember("parameters") && d["parameters"].IsObject() );
d.HasMember("parameters") && d["parameters"].IsObject()
);
}

if (false == error) {
altcurrency_ = d["altcurrency"].GetString();
balance_ = d["balance"].GetDouble();
balance_ = std::stod(d["balance"].GetString());
probi_ = d["probi"].GetString();

for (auto & i : d["rates"].GetObject()) {
rates_.insert(std::make_pair(i.name.GetString(), i.value.GetDouble()));
}
double value = 0.0;

parameters_currency_ = d["parameters"]["adFree"]["currency"].GetString();
parameters_fee_ = d["parameters"]["adFree"]["fee"]["BAT"].GetDouble();
// For some reason BTC is returned as string, where others are double
if (i.value.IsDouble()) {
value = i.value.GetDouble();
} else if (i.value.IsString()) {
value = std::stod(i.value.GetString());
}
rates_.insert(std::make_pair(i.name.GetString(), value));
}

for (auto & i : d["parameters"]["adFree"]["choices"]["BAT"].GetArray()) {
parameters_choices_.push_back(i.GetDouble());
Expand All @@ -778,10 +799,37 @@ namespace braveledger_bat_helper {
}

parameters_days_ = d["parameters"]["adFree"]["days"].GetUint();

if (d.HasMember("grants") && d["grants"].IsObject()) {
for (auto &i : d["grants"].GetArray()) {
GRANT grant;
auto obj = i.GetObject();
if (obj.HasMember("probi")) {
grant.probi = obj["probi"].GetString();
}

if (obj.HasMember("altcurrency")) {
grant.altcurrency = obj["altcurrency"].GetString();
}

if (obj.HasMember("expiryTime")) {
grant.expiryTime = obj["expiryTime"].GetUint64();
}

grants_.push_back(grant);
}
} else {
grants_.clear();
}
}
return !error;
}

/////////////////////////////////////////////////////////////////////////////
GRANT::GRANT() : expiryTime(0) {}

GRANT::~GRANT() {}

/////////////////////////////////////////////////////////////////////////////
SURVEYOR_INFO_ST::SURVEYOR_INFO_ST() {}

Expand Down
44 changes: 27 additions & 17 deletions src/bat_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ namespace braveledger_bat_helper {
std::vector<BATCH_VOTES_INFO_ST> batchVotesInfo_;
};

struct GRANT {
GRANT();
~GRANT();
std::string altcurrency;
std::string probi;
uint64_t expiryTime;
};

struct WALLET_PROPERTIES_ST {
WALLET_PROPERTIES_ST();
~WALLET_PROPERTIES_ST();
WALLET_PROPERTIES_ST(const WALLET_PROPERTIES_ST& properties);

//load from json string
bool loadFromJson(const std::string & json);

std::string altcurrency_;
std::string probi_;
double balance_;
Copy link
Contributor

Choose a reason for hiding this comment

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

probably it was missed in previous implementation, the same as above about default values for double balance_; unsigned int parameters_days_;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

std::map<std::string, double> rates_;
std::vector<double> parameters_choices_;
std::vector<double> parameters_range_;
unsigned int parameters_days_;
std::vector<GRANT> grants_;
};

struct CLIENT_STATE_ST {
CLIENT_STATE_ST();
CLIENT_STATE_ST(const CLIENT_STATE_ST&);
Expand All @@ -158,6 +184,7 @@ namespace braveledger_bat_helper {
bool loadFromJson(const std::string & json);

WALLET_INFO_ST walletInfo_;
WALLET_PROPERTIES_ST walletProperties_;
uint64_t bootStamp_ = 0u;
uint64_t reconcileStamp_ = 0u;
std::string personaId_;
Expand Down Expand Up @@ -209,23 +236,6 @@ namespace braveledger_bat_helper {
unsigned int votes_ = 0;
};

struct WALLET_PROPERTIES_ST {
WALLET_PROPERTIES_ST();
~WALLET_PROPERTIES_ST();

//load from json string
bool loadFromJson(const std::string & json);

std::string altcurrency_;
double balance_;
std::map<std::string, double> rates_;
std::string parameters_currency_;
double parameters_fee_;
std::vector<double> parameters_choices_;
std::vector<double> parameters_range_;
unsigned int parameters_days_;
};

struct SURVEYOR_INFO_ST {
SURVEYOR_INFO_ST();
~SURVEYOR_INFO_ST();
Expand Down
Loading