-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6974 from brave/claim-ios-17
Adds claim api for brave wallet (uplift to 1.17.x)
- Loading branch information
Showing
22 changed files
with
529 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...ve-ledger/src/bat/ledger/internal/endpoint/promotion/post_claim_brave/post_claim_brave.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
67 changes: 67 additions & 0 deletions
67
...ive-ledger/src/bat/ledger/internal/endpoint/promotion/post_claim_brave/post_claim_brave.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
Oops, something went wrong.