-
Notifications
You must be signed in to change notification settings - Fork 862
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #17901 - Claim endpoint for gemini needs a deposit address
- Loading branch information
Showing
16 changed files
with
419 additions
and
53 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
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
113 changes: 113 additions & 0 deletions
113
...ger/src/bat/ledger/internal/endpoint/gemini/post_recipient_id/post_recipient_id_gemini.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,113 @@ | ||
/* Copyright (c) 2021 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/gemini/post_recipient_id/post_recipient_id_gemini.h" | ||
|
||
#include <utility> | ||
|
||
#include "base/base64.h" | ||
#include "base/guid.h" | ||
#include "base/json/json_reader.h" | ||
#include "base/json/json_writer.h" | ||
#include "bat/ledger/internal/endpoint/gemini/gemini_utils.h" | ||
#include "bat/ledger/internal/ledger_impl.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
using std::placeholders::_1; | ||
|
||
namespace ledger { | ||
namespace endpoint { | ||
namespace gemini { | ||
|
||
PostRecipientId::PostRecipientId(LedgerImpl* ledger) : ledger_(ledger) { | ||
DCHECK(ledger_); | ||
} | ||
|
||
PostRecipientId::~PostRecipientId() = default; | ||
|
||
std::string PostRecipientId::GetUrl() { | ||
return GetApiServerUrl("/v1/payments/recipientIds"); | ||
} | ||
|
||
type::Result PostRecipientId::ParseBody(const std::string& body, | ||
std::string* recipient_id) { | ||
DCHECK(recipient_id); | ||
|
||
absl::optional<base::Value> value = base::JSONReader::Read(body); | ||
if (!value || !value->is_dict()) { | ||
BLOG(0, "Invalid JSON"); | ||
return type::Result::LEDGER_ERROR; | ||
} | ||
|
||
base::DictionaryValue* dictionary = nullptr; | ||
if (!value->GetAsDictionary(&dictionary)) { | ||
BLOG(0, "Invalid JSON"); | ||
return type::Result::LEDGER_ERROR; | ||
} | ||
|
||
const auto* result = dictionary->FindStringKey("result"); | ||
if (!result || *result != "OK") { | ||
BLOG(0, "Failed creating recipient_id"); | ||
return type::Result::LEDGER_ERROR; | ||
} | ||
|
||
const auto* id = dictionary->FindStringKey("recipient_id"); | ||
if (!id) { | ||
BLOG(0, "Response missing a recipient_id"); | ||
return type::Result::LEDGER_ERROR; | ||
} | ||
|
||
*recipient_id = *id; | ||
return type::Result::LEDGER_OK; | ||
} | ||
|
||
std::string PostRecipientId::GeneratePayload() { | ||
base::Value payload(base::Value::Type::DICTIONARY); | ||
payload.SetStringKey("label", base::GenerateGUID()); | ||
|
||
std::string json; | ||
base::JSONWriter::Write(payload, &json); | ||
|
||
std::string base64; | ||
base::Base64Encode(base::StringPiece(json), &base64); | ||
return base64; | ||
} | ||
|
||
void PostRecipientId::Request(const std::string& token, | ||
PostRecipientIdCallback callback) { | ||
auto url_callback = | ||
std::bind(&PostRecipientId::OnRequest, this, _1, token, callback); | ||
|
||
auto request = type::UrlRequest::New(); | ||
auto payload = GeneratePayload(); | ||
|
||
request->url = GetUrl(); | ||
request->method = type::UrlMethod::POST; | ||
request->headers = RequestAuthorization(token); | ||
request->headers.push_back("X-GEMINI-PAYLOAD: " + payload); | ||
|
||
ledger_->LoadURL(std::move(request), url_callback); | ||
} | ||
|
||
void PostRecipientId::OnRequest(const type::UrlResponse& response, | ||
const std::string& token, | ||
PostRecipientIdCallback callback) { | ||
ledger::LogUrlResponse(__func__, response); | ||
|
||
type::Result result = CheckStatusCode(response.status_code); | ||
|
||
if (result != type::Result::LEDGER_OK) { | ||
callback(result, "", ""); | ||
return; | ||
} | ||
|
||
std::string recipient_id; | ||
result = ParseBody(response.body, &recipient_id); | ||
callback(result, recipient_id, token); | ||
} | ||
|
||
} // namespace gemini | ||
} // namespace endpoint | ||
} // namespace ledger |
75 changes: 75 additions & 0 deletions
75
...dger/src/bat/ledger/internal/endpoint/gemini/post_recipient_id/post_recipient_id_gemini.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,75 @@ | ||
/* Copyright (c) 2021 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 BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_ENDPOINT_GEMINI_POST_RECIPIENT_ID_POST_RECIPIENT_ID_GEMINI_H_ | ||
#define BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_ENDPOINT_GEMINI_POST_RECIPIENT_ID_POST_RECIPIENT_ID_GEMINI_H_ | ||
|
||
#include <string> | ||
|
||
#include "bat/ledger/ledger.h" | ||
|
||
// POST https://api.gemini.com/v1/payments/recipientIds | ||
// Payload: | ||
// { | ||
// "label": <uuid> | ||
// } | ||
// | ||
// Headers: | ||
// Authorization: Bearer <token> | ||
// X-GEMINI-PAYLOAD: base64-payload | ||
// | ||
// Request body: | ||
// {} | ||
// | ||
// Success code: | ||
// HTTP_OK (200) | ||
// | ||
// Error codes: | ||
// HTTP_UNAUTHORIZED (401) | ||
// HTTP_NOT_FOUND (404) | ||
// | ||
// Response body: | ||
// { | ||
// "result": "OK", | ||
// "recipient_id": "60f9be89-ada7-486d-9cef-f6d3a10886d7", | ||
// "label": <uuid> | ||
// } | ||
|
||
namespace ledger { | ||
class LedgerImpl; | ||
|
||
namespace endpoint { | ||
namespace gemini { | ||
|
||
using PostRecipientIdCallback = | ||
std::function<void(const type::Result result, | ||
const std::string recipient_id, | ||
const std::string token)>; | ||
|
||
class PostRecipientId { | ||
public: | ||
explicit PostRecipientId(LedgerImpl* ledger); | ||
~PostRecipientId(); | ||
|
||
void Request(const std::string& token, PostRecipientIdCallback callback); | ||
|
||
private: | ||
std::string GetUrl(); | ||
|
||
type::Result ParseBody(const std::string& body, std::string* recipient_id); | ||
|
||
void OnRequest(const type::UrlResponse& response, | ||
const std::string& token, | ||
PostRecipientIdCallback callback); | ||
std::string GeneratePayload(); | ||
|
||
LedgerImpl* ledger_; // NOT OWNED | ||
}; | ||
|
||
} // namespace gemini | ||
} // namespace endpoint | ||
} // namespace ledger | ||
|
||
#endif // BRAVE_VENDOR_BAT_NATIVE_LEDGER_SRC_BAT_LEDGER_INTERNAL_ENDPOINT_GEMINI_POST_RECIPIENT_ID_POST_RECIPIENT_ID_GEMINI_H_ |
Oops, something went wrong.