Skip to content

Commit

Permalink
Adds probi to the final grant callback
Browse files Browse the repository at this point in the history
Merges two statuses into one for the grant flow
  • Loading branch information
NejcZdovc committed Aug 17, 2018
1 parent 4d69c73 commit e90f72b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/bat/ledger/ledger_callback_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ LEDGER_EXPORT enum Result {
NO_LEDGER_STATE = 3,
INVALID_PUBLISHER_STATE = 4,
INVALID_LEDGER_STATE = 5,
CAPTCHA_FAILED = 6,
// some more useful result codes should go here
};

Expand Down
2 changes: 1 addition & 1 deletion include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class LEDGER_EXPORT LedgerClient {
virtual void GetGrantCaptcha() = 0;
virtual void OnGrantCaptcha(const std::string& image) = 0;
virtual void OnRecoverWallet(Result result, double balance, std::vector<ledger::Grant> grants) = 0;
virtual void OnGrantFinish(ledger::Result result, unsigned int statusCode, uint64_t experationDate) = 0;
virtual void OnGrantFinish(ledger::Result result, ledger::Grant grant) = 0;

virtual std::string URIEncode(const std::string& value) = 0;

Expand Down
20 changes: 14 additions & 6 deletions src/bat_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,8 @@ void BatClient::getGrantCallback(bool success, const std::string& response) {

void BatClient::setGrant(const std::string& captchaResponse, const std::string& promotionId) {
if (promotionId.empty() && state_->grant_.promotionId.empty()) {
ledger_->OnGrantFinish(ledger::Result::ERROR, 400, 0);
braveledger_bat_helper::GRANT properties;
ledger_->OnGrantFinish(ledger::Result::ERROR, properties);
return;
}

Expand Down Expand Up @@ -1034,21 +1035,28 @@ void BatClient::setGrantCallback(bool success, const std::string& response) {

std::string error;
unsigned int statusCode;
braveledger_bat_helper::GRANT grant;
braveledger_bat_helper::getJSONResponse(response, statusCode, error);

if (!success) {
ledger_->OnGrantFinish(ledger::Result::ERROR, statusCode, 0);
if (statusCode == 422) {
ledger_->OnGrantFinish(ledger::Result::CAPTCHA_FAILED, grant);
} else {
ledger_->OnGrantFinish(ledger::Result::ERROR, grant);
}
return;
}

uint64_t expiryTime;
bool ok = braveledger_bat_helper::getJSONGrant(response, expiryTime);
bool ok = braveledger_bat_helper::loadFromJson(grant, response);
if (!ok) {
ledger_->OnGrantFinish(ledger::Result::ERROR, 400, 0);
ledger_->OnGrantFinish(ledger::Result::ERROR, grant);
return;
}

ledger_->OnGrantFinish(ledger::Result::OK, statusCode, expiryTime);
grant.promotionId = state_->grant_.promotionId;
state_->grant_ = grant;

ledger_->OnGrantFinish(ledger::Result::OK, grant);
}

void BatClient::getGrantCaptcha() {
Expand Down
2 changes: 0 additions & 2 deletions src/bat_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,6 @@ namespace braveledger_bat_helper {

bool getJSONResponse(const std::string& json, unsigned int& statusCode, std::string& error);

bool getJSONGrant(const std::string& json, uint64_t& expiryTime);

std::vector<uint8_t> generateSeed();

std::vector<uint8_t> getHKDF(const std::vector<uint8_t>& seed);
Expand Down
10 changes: 8 additions & 2 deletions src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,14 @@ void LedgerImpl::SolveGrantCaptcha(const std::string& solution) const {
bat_client_->setGrant(solution, "");
}

void LedgerImpl::OnGrantFinish(ledger::Result result, unsigned int statusCode, uint64_t experationDate) {
ledger_client_->OnGrantFinish(result, statusCode, experationDate);
void LedgerImpl::OnGrantFinish(ledger::Result result, braveledger_bat_helper::GRANT grant) {
ledger::Grant newGrant;

newGrant.altcurrency = grant.altcurrency;
newGrant.probi = grant.probi;
newGrant.expiryTime = grant.expiryTime;

ledger_client_->OnGrantFinish(result, newGrant);
}

} // namespace bat_ledger
2 changes: 1 addition & 1 deletion src/ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class LedgerImpl : public ledger::Ledger,
void OnGrantCaptcha(const std::string& image);

void SolveGrantCaptcha(const std::string& solution) const override;
void OnGrantFinish(ledger::Result result, unsigned int statusCode, uint64_t expiryTime);
void OnGrantFinish(ledger::Result result, braveledger_bat_helper::GRANT grant);

std::string GetWalletPassphrase() const override;
void RecoverWallet(const std::string& passPhrase) const override;
Expand Down

0 comments on commit e90f72b

Please sign in to comment.