Skip to content

Commit

Permalink
Merge pull request #82 from brave-intl/register_errors2
Browse files Browse the repository at this point in the history
don't allow multiple concurrent init calls
  • Loading branch information
NejcZdovc authored Sep 5, 2018
2 parents 511774a + 847899f commit 6da42fa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
3 changes: 2 additions & 1 deletion include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class LEDGER_EXPORT Ledger {
static Ledger* CreateInstance(LedgerClient* client);

virtual void Initialize() = 0;
virtual void CreateWallet() = 0;
// returns false if wallet initialization is already in progress
virtual bool CreateWallet() = 0;
virtual void Reconcile() = 0;

virtual void MakePayment(const PaymentData& payment_data) = 0;
Expand Down
27 changes: 19 additions & 8 deletions src/bat_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void BatClient::registerPersona() {
void BatClient::requestCredentialsCallback(bool result, const std::string& response) {
//LOG(ERROR) << "!!!response == " << response;
if (!result) {
// TODO errors handling
ledger_->OnWalletInitialized(ledger::Result::BAD_REGISTRATION_RESPONSE);
return;
}
if (state_->personaId_.empty()) {
Expand All @@ -65,10 +65,17 @@ void BatClient::requestCredentialsCallback(bool result, const std::string& respo
state_->userId_.erase(std::remove(state_->userId_.begin(), state_->userId_.end(), '-'), state_->userId_.end());
state_->userId_.erase(12, 1);

braveledger_bat_helper::getJSONValue(REGISTRARVK_FIELDNAME, response, state_->registrarVK_);
if (!braveledger_bat_helper::getJSONValue(REGISTRARVK_FIELDNAME, response, state_->registrarVK_)) {
ledger_->OnWalletInitialized(ledger::Result::BAD_REGISTRATION_RESPONSE);
return;
}
DCHECK(!state_->registrarVK_.empty());
std::string proof = getAnonizeProof(state_->registrarVK_, state_->userId_, state_->preFlight_);

if (proof.empty()) {
ledger_->OnWalletInitialized(ledger::Result::BAD_REGISTRATION_RESPONSE);
return;
}
state_->walletInfo_.keyInfoSeed_ = braveledger_bat_helper::generateSeed();
std::vector<uint8_t> secretKey = braveledger_bat_helper::getHKDF(state_->walletInfo_.keyInfoSeed_);
std::vector<uint8_t> publicKey;
Expand Down Expand Up @@ -115,24 +122,25 @@ std::string BatClient::getAnonizeProof(const std::string& registrarVK, const std
if (nullptr != cred) {
preFlight = cred;
free((void*)cred);
} else {
return "";
}
DCHECK(!preFlight.empty());
const char* proofTemp = registerUserMessage(preFlight.c_str(), registrarVK.c_str());
std::string proof;
if (nullptr != proofTemp) {
proof = proofTemp;
free((void*)proofTemp);
} else {
return "";
}
DCHECK(!proof.empty());

return proof;
}

void BatClient::registerPersonaCallback(bool result,
const std::string& response) {
if (!result) {
// TODO error handling
ledger_->OnWalletInitialized(ledger::Result::ERROR);
ledger_->OnWalletInitialized(ledger::Result::BAD_REGISTRATION_RESPONSE);
return;
}

Expand All @@ -151,12 +159,15 @@ void BatClient::registerPersonaCallback(bool result,
return;
}

braveledger_bat_helper::getJSONWalletInfo(response, state_->walletInfo_, state_->fee_currency_, state_->fee_amount_, state_->days_);
if (!braveledger_bat_helper::getJSONWalletInfo(response, state_->walletInfo_, state_->fee_currency_, state_->fee_amount_, state_->days_)) {
ledger_->OnWalletInitialized(ledger::Result::REGISTRATION_VERIFICATION_FAILED);
return;
}
state_->bootStamp_ = braveledger_bat_helper::currentTime() * 1000;
state_->reconcileStamp_ = state_->bootStamp_ + state_->days_ * 24 * 60 * 60 * 1000;

saveState();
ledger_->OnWalletInitialized(ledger::Result::OK);
saveState();
}

void BatClient::setContributionAmount(const double& amount) {
Expand Down
34 changes: 24 additions & 10 deletions src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ LedgerImpl::LedgerImpl(ledger::LedgerClient* client) :
bat_publishers_(new BatPublishers(this)),
bat_get_media_(new BatGetMedia(this)),
initialized_(false),
initializing_(false),
last_tab_active_time_(0),
last_shown_tab_id_(-1),
last_pub_load_timer_id_ (0u){
Expand All @@ -37,15 +38,22 @@ LedgerImpl::~LedgerImpl() {
}

void LedgerImpl::Initialize() {
DCHECK(!initializing_);
initializing_ = true;
LoadLedgerState(this);
}

void LedgerImpl::CreateWallet() {
bool LedgerImpl::CreateWallet() {
if (initializing_)
return false;

initializing_ = true;
if (initialized_) {
OnWalletInitialized(ledger::Result::ERROR);
return;
return false;
}
bat_client_->registerPersona();
return true;
}

void LedgerImpl::AddRecurringPayment(const std::string& publisher_id, const double& value) {
Expand Down Expand Up @@ -128,7 +136,7 @@ void LedgerImpl::OnXHRLoad(
uint32_t tab_id,
const std::string& url,
const std::map<std::string, std::string>& parts,
const std::string& first_party_url,
const std::string& first_party_url,
const std::string& referrer,
const ledger::VisitData& visit_data) {
// TODO
Expand All @@ -146,7 +154,7 @@ void LedgerImpl::OnXHRLoad(

void LedgerImpl::OnPostData(
const std::string& url,
const std::string& first_party_url,
const std::string& first_party_url,
const std::string& referrer,
const std::string& post_data,
const ledger::VisitData& visit_data) {
Expand Down Expand Up @@ -174,10 +182,13 @@ void LedgerImpl::OnLedgerStateLoaded(ledger::Result result,
const std::string& data) {
if (result == ledger::Result::OK) {
if (!bat_client_->loadState(data)) {
OnWalletInitialized(ledger::Result::INVALID_LEDGER_STATE);
result = ledger::Result::INVALID_LEDGER_STATE;
}
} else {
}

if (result == ledger::Result::OK) {
OnWalletInitialized(result);
return;
}

LoadPublisherState(this);
Expand All @@ -196,7 +207,6 @@ void LedgerImpl::OnPublisherStateLoaded(ledger::Result result,
}

OnWalletInitialized(result);
LoadPublisherList(this);
}

void LedgerImpl::SaveLedgerState(const std::string& data) {
Expand Down Expand Up @@ -231,9 +241,13 @@ std::string LedgerImpl::GenerateGUID() const {
}

void LedgerImpl::OnWalletInitialized(ledger::Result result) {
if (result == ledger::Result::OK)
initialized_ = true;
initializing_ = false;
ledger_client_->OnWalletInitialized(result);

if (result == ledger::Result::OK) {
initialized_ = true;
RefreshPublishersList(false);
}
}

std::unique_ptr<ledger::LedgerURLLoader> LedgerImpl::LoadURL(const std::string& url,
Expand Down Expand Up @@ -268,7 +282,7 @@ void LedgerImpl::SetPublisherInfo(std::unique_ptr<ledger::PublisherInfo> info,
std::bind(&LedgerImpl::OnSetPublisherInfo, this, callback, _1, _2));
}

void LedgerImpl::SetMediaPublisherInfo(const uint64_t& duration,
void LedgerImpl::SetMediaPublisherInfo(const uint64_t& duration,
std::unique_ptr<ledger::MediaPublisherInfo> media_publisher_info,
const ledger::VisitData& visit_data,
ledger::MediaPublisherInfoCallback callback) {
Expand Down
9 changes: 5 additions & 4 deletions src/ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ class LedgerImpl : public ledger::Ledger,
std::string GenerateGUID() const;
void Initialize() override;
void Reconcile() override;
void CreateWallet() override;
bool CreateWallet() override;

void SetPublisherInfo(std::unique_ptr<ledger::PublisherInfo> publisher_info,
ledger::PublisherInfoCallback callback) override;
void GetPublisherInfo(const ledger::PublisherInfoFilter& filter,
ledger::PublisherInfoCallback callback) override;
void GetMediaPublisherInfo(const std::string& publisher_key,
ledger::MediaPublisherInfoCallback callback) override;
void SetMediaPublisherInfo(const uint64_t& duration,
void SetMediaPublisherInfo(const uint64_t& duration,
std::unique_ptr<ledger::MediaPublisherInfo> media_publisher_info,
const ledger::VisitData& visit_data,
ledger::MediaPublisherInfoCallback callback) override;
Expand Down Expand Up @@ -149,12 +149,12 @@ class LedgerImpl : public ledger::Ledger,
uint32_t tab_id,
const std::string& url,
const std::map<std::string, std::string>& parts,
const std::string& first_party_url,
const std::string& first_party_url,
const std::string& referrer,
const ledger::VisitData& visit_data) override;
void OnPostData(
const std::string& url,
const std::string& first_party_url,
const std::string& first_party_url,
const std::string& referrer,
const std::string& post_data,
const ledger::VisitData& visit_data) override;
Expand Down Expand Up @@ -190,6 +190,7 @@ class LedgerImpl : public ledger::Ledger,
std::unique_ptr<braveledger_bat_publishers::BatPublishers> bat_publishers_;
std::unique_ptr<braveledger_bat_get_media::BatGetMedia> bat_get_media_;
bool initialized_;
bool initializing_;

URLRequestHandler handler_;

Expand Down

0 comments on commit 6da42fa

Please sign in to comment.