Skip to content

Commit

Permalink
Merge pull request #6974 from brave/claim-ios-17
Browse files Browse the repository at this point in the history
Adds claim api for brave wallet (uplift to 1.17.x)
  • Loading branch information
kjozwiak authored Nov 11, 2020
2 parents 9775143 + 53720e6 commit 91a7ce7
Show file tree
Hide file tree
Showing 22 changed files with 529 additions and 36 deletions.
8 changes: 6 additions & 2 deletions components/brave_rewards/browser/static_values.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const std::vector<std::string> kOnlyAnonWalletCountries = {
};

#if defined(OS_ANDROID)
const std::map<std::string, bool> kBoolOptions = {};
const std::map<std::string, bool> kBoolOptions = {
{ledger::option::kClaimUGP, false}
};

const std::map<std::string, int> kIntegerOptions = {};

Expand All @@ -36,7 +38,9 @@ const std::vector<std::string> kOnlyAnonWalletCountries = {
{ledger::option::kPublisherListRefreshInterval,
7* base::Time::kHoursPerDay * base::Time::kSecondsPerHour}};
#else
const std::map<std::string, bool> kBoolOptions = {};
const std::map<std::string, bool> kBoolOptions = {
{ledger::option::kClaimUGP, false}
};

const std::map<std::string, int> kIntegerOptions = {};

Expand Down
1 change: 1 addition & 0 deletions components/brave_rewards/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ source_set("brave_rewards_unit_tests") {
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/get_wallet_balance/get_wallet_balance_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/post_bat_loss/post_bat_loss_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/post_captcha/post_captcha_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/post_claim_brave/post_claim_brave_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/post_claim_uphold/post_claim_uphold_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/post_clobbered_claims/post_clobbered_claims_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/endpoint/promotion/post_creds/post_creds_unittest.cc",
Expand Down
2 changes: 2 additions & 0 deletions vendor/bat-native-ledger/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ source_set("ledger") {
"src/bat/ledger/internal/endpoint/promotion/post_bat_loss/post_bat_loss.h",
"src/bat/ledger/internal/endpoint/promotion/post_captcha/post_captcha.cc",
"src/bat/ledger/internal/endpoint/promotion/post_captcha/post_captcha.h",
"src/bat/ledger/internal/endpoint/promotion/post_claim_brave/post_claim_brave.cc",
"src/bat/ledger/internal/endpoint/promotion/post_claim_brave/post_claim_brave.h",
"src/bat/ledger/internal/endpoint/promotion/post_claim_uphold/post_claim_uphold.cc",
"src/bat/ledger/internal/endpoint/promotion/post_claim_uphold/post_claim_uphold.h",
"src/bat/ledger/internal/endpoint/promotion/post_clobbered_claims/post_clobbered_claims.cc",
Expand Down
9 changes: 9 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ using GetPublisherInfoCallback =

using GetBraveWalletCallback = std::function<void(type::BraveWalletPtr)>;

using GetTransferableAmountCallback = std::function<void(double)>;

class LEDGER_EXPORT Ledger {
public:
static bool IsMediaLink(
Expand Down Expand Up @@ -393,6 +395,13 @@ class LEDGER_EXPORT Ledger {
virtual void GetBraveWallet(GetBraveWalletCallback callback) = 0;

virtual std::string GetWalletPassphrase() const = 0;

virtual void LinkBraveWallet(
const std::string& destination_payment_id,
ResultCallback callback) = 0;

virtual void GetTransferableAmount(
GetTransferableAmountCallback callback) = 0;
};

} // namespace ledger
Expand Down
1 change: 1 addition & 0 deletions vendor/bat-native-ledger/include/bat/ledger/option_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace ledger {
namespace option {

const char kPublisherListRefreshInterval[] = "publisher_list_refresh_interval";
const char kClaimUGP[] = "claim_ugp";

} // namespace option
} // namespace ledger
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* 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/. */
#include "bat/ledger/internal/endpoint/promotion/post_claim_brave/post_claim_brave.h"

#include <utility>

#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"
#include "bat/ledger/internal/common/request_util.h"
#include "bat/ledger/internal/endpoint/promotion/promotions_util.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "net/http/http_status_code.h"

using std::placeholders::_1;

namespace {

std::string GetPath(const std::string& payment_id) {
return base::StringPrintf(
"/v3/wallet/brave/%s/claim",
payment_id.c_str());
}

} // namespace

namespace ledger {
namespace endpoint {
namespace promotion {

PostClaimBrave::PostClaimBrave(LedgerImpl* ledger) : ledger_(ledger) {
DCHECK(ledger_);
}

PostClaimBrave::~PostClaimBrave() = default;

std::string PostClaimBrave::GetUrl() {
const auto wallet = ledger_->wallet()->GetWallet();
if (!wallet) {
BLOG(0, "Wallet is null");
return "";
}

const std::string& path = GetPath(wallet->payment_id);

return GetServerUrl(path);
}

std::string PostClaimBrave::GeneratePayload(
const std::string& destination_payment_id) {
base::Value payload(base::Value::Type::DICTIONARY);
payload.SetStringKey("depositDestination", destination_payment_id);
std::string json;
base::JSONWriter::Write(payload, &json);

return json;
}

type::Result PostClaimBrave::CheckStatusCode(const int status_code) {
if (status_code == net::HTTP_BAD_REQUEST) {
BLOG(0, "Invalid request");
return type::Result::LEDGER_ERROR;
}

if (status_code == net::HTTP_NOT_FOUND) {
BLOG(0, "Not found");
return type::Result::NOT_FOUND;
}

if (status_code == net::HTTP_CONFLICT) {
BLOG(0, "Not found");
return type::Result::ALREADY_EXISTS;
}

if (status_code == net::HTTP_INTERNAL_SERVER_ERROR) {
BLOG(0, "Internal server error");
return type::Result::LEDGER_ERROR;
}

if (status_code != net::HTTP_OK) {
return type::Result::LEDGER_ERROR;
}

return type::Result::LEDGER_OK;
}

void PostClaimBrave::Request(
const std::string& destination_payment_id,
PostClaimBraveCallback callback) {
auto url_callback = std::bind(&PostClaimBrave::OnRequest,
this,
_1,
callback);
const std::string& payload = GeneratePayload(destination_payment_id);

const auto wallet = ledger_->wallet()->GetWallet();
if (!wallet) {
BLOG(0, "Wallet is null");
callback(type::Result::LEDGER_ERROR);
return;
}

const auto sign_url = base::StringPrintf(
"post %s",
GetPath(wallet->payment_id).c_str());
auto headers = util::BuildSignHeaders(
sign_url,
payload,
wallet->payment_id,
wallet->recovery_seed);

auto request = type::UrlRequest::New();
request->url = GetUrl();
request->content = payload;
request->headers = headers;
request->content_type = "application/json; charset=utf-8";
request->method = type::UrlMethod::POST;
ledger_->LoadURL(std::move(request), url_callback);
}

void PostClaimBrave::OnRequest(
const type::UrlResponse& response,
PostClaimBraveCallback callback) {
ledger::LogUrlResponse(__func__, response);
callback(CheckStatusCode(response.status_code));
}

} // namespace promotion
} // namespace endpoint
} // namespace ledger
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* 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 BRAVELEDGER_ENDPOINT_PROMOTION_POST_CLAIM_BRAVE_POST_CLAIM_BRAVE_H_
#define BRAVELEDGER_ENDPOINT_PROMOTION_POST_CLAIM_BRAVE_POST_CLAIM_BRAVE_H_

#include <string>

#include "bat/ledger/ledger.h"

// POST /v3/wallet/brave/{payment_id}/claim
//
// Request body:
// {
// "depositDestination": "83b3b77b-e7c3-455b-adda-e476fa0656d2"
// }
//
// Success code:
// HTTP_OK (200)
//
// Error codes:
// HTTP_BAD_REQUEST (400)
// HTTP_NOT_FOUND (404)
// HTTP_CONFLICT (409)
// HTTP_INTERNAL_SERVER_ERROR (500)
//
// Response body:
// {Empty}

namespace ledger {
class LedgerImpl;

namespace endpoint {
namespace promotion {

using PostClaimBraveCallback = std::function<void(const type::Result result)>;

class PostClaimBrave {
public:
explicit PostClaimBrave(LedgerImpl* ledger);
~PostClaimBrave();

void Request(
const std::string& destination_payment_id,
PostClaimBraveCallback callback);

private:
std::string GetUrl();

std::string GeneratePayload(const std::string& destination_payment_id);

type::Result CheckStatusCode(const int status_code);

void OnRequest(
const type::UrlResponse& response,
PostClaimBraveCallback callback);

LedgerImpl* ledger_; // NOT OWNED
};

} // namespace promotion
} // namespace endpoint
} // namespace ledger

#endif // BRAVELEDGER_ENDPOINT_PROMOTION_POST_CLAIM_BRAVE_POST_CLAIM_BRAVE_H_
Loading

0 comments on commit 91a7ce7

Please sign in to comment.