Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds claim api for vg #4708

Merged
merged 2 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/brave_rewards/browser/rewards_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
#endif
registry->RegisterUint64Pref(prefs::kStatePromotionLastFetchStamp, 0ull);
registry->RegisterBooleanPref(prefs::kStatePromotionCorruptedMigrated, false);
registry->RegisterBooleanPref(prefs::kStateAnonTransferChecked, false);
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
}

} // namespace brave_rewards
1 change: 1 addition & 0 deletions components/brave_rewards/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ const char kStatePromotionLastFetchStamp[] =
"brave.rewards.promotion_last_fetch_stamp";
const char kStatePromotionCorruptedMigrated[] =
"brave.rewards.promotion_corrupted_migrated";
const char kStateAnonTransferChecked[] = "brave.rewards.anon_transfer_checked";
} // namespace prefs
} // namespace brave_rewards
1 change: 1 addition & 0 deletions components/brave_rewards/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern const char kStateServerPublisherListStamp[];
extern const char kStateUpholdAnonAddress[];
extern const char kStatePromotionLastFetchStamp[];
extern const char kStatePromotionCorruptedMigrated[];
extern const char kStateAnonTransferChecked[];

extern const char kUseRewardsStagingServer[];
} // namespace prefs
Expand Down
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ test("brave_unit_tests") {
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_activity_info_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_unblinded_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_util_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/phase_two_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/helper_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/media/reddit_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 @@ -183,6 +183,8 @@ source_set("ledger") {
"src/bat/ledger/internal/media/youtube.cc",
"src/bat/ledger/internal/promotion/promotion.cc",
"src/bat/ledger/internal/promotion/promotion.h",
"src/bat/ledger/internal/promotion/promotion_transfer.cc",
"src/bat/ledger/internal/promotion/promotion_transfer.h",
"src/bat/ledger/internal/promotion/promotion_util.cc",
"src/bat/ledger/internal/promotion/promotion_util.h",
"src/bat/ledger/internal/properties/ballot_properties.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ using ResultCallback = std::function<void(const Result)>;
using GetFirstContributionQueueCallback =
std::function<void(ContributionQueuePtr)>;
using GetPromotionCallback = std::function<void(PromotionPtr)>;
using GetAllUnblindedTokensCallback = std::function<void(UnblindedTokenList)>;
using GetUnblindedTokenListCallback = std::function<void(UnblindedTokenList)>;
using GetAllPromotionsCallback = std::function<void(PromotionMap)>;

using GetTransactionReportCallback =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,15 @@
#include "bat/ledger/internal/bat_util.h"
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/contribution/contribution_unblinded.h"
#include "bat/ledger/internal/contribution/contribution_util.h"
#include "bat/ledger/internal/request/promotion_requests.h"
#include "brave_base/random.h"
#include "net/http/http_status_code.h"

#include "wrapper.hpp" // NOLINT

using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

using challenge_bypass_ristretto::UnblindedToken;
using challenge_bypass_ristretto::VerificationKey;
using challenge_bypass_ristretto::VerificationSignature;

namespace {

std::string ConvertTypeToString(const ledger::RewardsType type) {
Expand All @@ -46,35 +41,6 @@ std::string ConvertTypeToString(const ledger::RewardsType type) {
}
}

void GenerateSuggestionMock(
const ledger::UnblindedToken& token,
const std::string& suggestion_encoded,
base::Value* result) {
result->SetStringKey("t", token.token_value);
result->SetStringKey("publicKey", token.public_key);
result->SetStringKey("signature", token.token_value);
}

void GenerateSuggestion(
const ledger::UnblindedToken& token,
const std::string& suggestion_encoded,
base::Value* result) {
UnblindedToken unblinded = UnblindedToken::decode_base64(token.token_value);
VerificationKey verification_key = unblinded.derive_verification_key();
VerificationSignature signature = verification_key.sign(suggestion_encoded);
const std::string pre_image = unblinded.preimage().encode_base64();

if (challenge_bypass_ristretto::exception_occurred()) {
challenge_bypass_ristretto::TokenException e =
challenge_bypass_ristretto::get_last_exception();
return;
}

result->SetStringKey("t", pre_image);
result->SetStringKey("publicKey", token.public_key);
result->SetStringKey("signature", signature.encode_base64());
}

bool HasTokenExpired(const ledger::UnblindedToken& token) {
const uint64_t now = static_cast<uint64_t>(base::Time::Now().ToDoubleT());

Expand All @@ -95,13 +61,27 @@ std::string GenerateTokenPayload(
base::Base64Encode(suggestion_json, &suggestion_encoded);

base::Value credentials(base::Value::Type::LIST);
for (auto & item : list) {
for (auto& item : list) {
base::Value token(base::Value::Type::DICTIONARY);
bool success;
if (ledger::is_testing) {
GenerateSuggestionMock(item, suggestion_encoded, &token);
success = braveledger_contribution::GenerateSuggestionMock(
item.token_value,
item.public_key,
suggestion_encoded,
&token);
} else {
GenerateSuggestion(item, suggestion_encoded, &token);
success = braveledger_contribution::GenerateSuggestion(
item.token_value,
item.public_key,
suggestion_encoded,
&token);
}

if (!success) {
continue;
}

credentials.GetList().push_back(std::move(token));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ TEST_F(UnblindedTest, NotEnoughFunds) {

ON_CALL(*mock_ledger_impl_, GetAllUnblindedTokens(_))
.WillByDefault(
Invoke([](ledger::GetAllUnblindedTokensCallback callback) {
Invoke([](ledger::GetUnblindedTokenListCallback callback) {
ledger::UnblindedTokenList list;

auto info = ledger::UnblindedToken::New();
Expand All @@ -92,7 +92,7 @@ TEST_F(UnblindedTest, PromotionExpiredDeleteToken) {

ON_CALL(*mock_ledger_impl_, GetAllUnblindedTokens(_))
.WillByDefault(
Invoke([](ledger::GetAllUnblindedTokensCallback callback) {
Invoke([](ledger::GetUnblindedTokenListCallback callback) {
ledger::UnblindedTokenList list;

auto info = ledger::UnblindedToken::New();
Expand Down Expand Up @@ -123,7 +123,7 @@ TEST_F(UnblindedTest, PromotionExpiredDeleteTokensNotEnoughFunds) {

ON_CALL(*mock_ledger_impl_, GetAllUnblindedTokens(_))
.WillByDefault(
Invoke([](ledger::GetAllUnblindedTokensCallback callback) {
Invoke([](ledger::GetUnblindedTokenListCallback callback) {
ledger::UnblindedTokenList list;

auto info = ledger::UnblindedToken::New();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

#include "bat/ledger/internal/contribution/contribution_util.h"

#include "wrapper.hpp" // NOLINT

using challenge_bypass_ristretto::UnblindedToken;
using challenge_bypass_ristretto::VerificationKey;
using challenge_bypass_ristretto::VerificationSignature;

namespace braveledger_contribution {

ledger::ReconcileDirections
Expand Down Expand Up @@ -49,4 +55,41 @@ ledger::ReportType GetReportTypeFromRewardsType(
}
}

bool GenerateSuggestion(
const std::string& token_value,
const std::string& public_key,
const std::string& suggestion_encoded,
base::Value* result) {
if (token_value.empty() || public_key.empty() || suggestion_encoded.empty()) {
return false;
}

UnblindedToken unblinded = UnblindedToken::decode_base64(token_value);
VerificationKey verification_key = unblinded.derive_verification_key();
VerificationSignature signature = verification_key.sign(suggestion_encoded);
const std::string pre_image = unblinded.preimage().encode_base64();

if (challenge_bypass_ristretto::exception_occurred()) {
challenge_bypass_ristretto::TokenException e =
challenge_bypass_ristretto::get_last_exception();
return false;
}

result->SetStringKey("t", pre_image);
result->SetStringKey("publicKey", public_key);
result->SetStringKey("signature", signature.encode_base64());
return true;
}

bool GenerateSuggestionMock(
const std::string& token_value,
const std::string& public_key,
const std::string& suggestion_encoded,
base::Value* result) {
result->SetStringKey("t", token_value);
result->SetStringKey("publicKey", public_key);
result->SetStringKey("signature", token_value);
return true;
}

} // namespace braveledger_contribution
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <map>
#include <string>

#include "base/values.h"
#include "bat/ledger/mojom_structs.h"
#include "bat/ledger/internal/properties/reconcile_direction_properties.h"

Expand All @@ -20,6 +21,18 @@ FromContributionQueuePublishersToReconcileDirections(

ledger::ReportType GetReportTypeFromRewardsType(const ledger::RewardsType type);

bool GenerateSuggestion(
const std::string& token_value,
const std::string& public_key,
const std::string& suggestion_encoded,
base::Value* result);

bool GenerateSuggestionMock(
const std::string& token_value,
const std::string& public_key,
const std::string& suggestion_encoded,
base::Value* result);

} // namespace braveledger_contribution

#endif // BRAVELEDGER_CONTRIBUTION_CONTRIBUTION_UTIL_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* 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 "base/values.h"
#include "bat/ledger/internal/contribution/contribution_util.h"
#include "testing/gtest/include/gtest/gtest.h"

// npm run test -- brave_unit_tests --filter=ContributionUtilTest.*

namespace braveledger_contribution {

class ContributionUtilTest : public testing::Test {};

TEST_F(ContributionUtilTest, GenerateSuggestionEmptyToken) {
base::Value token(base::Value::Type::DICTIONARY);
bool success = GenerateSuggestion(
"",
"rqQ1Tz26C4mv33ld7xpcLhuX1sWaD+s7VMnuX6cokT4=",
"some id",
&token);
ASSERT_FALSE(success);
}

TEST_F(ContributionUtilTest, GenerateSuggestionEmptyPublicKey) {
base::Value token(base::Value::Type::DICTIONARY);
bool success = GenerateSuggestion(
"Twsi4QeUNcOOW/IFnYcGLHgLsfVco0oPZ+cl3YeZMQgb4NB8E29Ts+2/pQA54VksqGpg/DtmPRBV4FlQ1VXbKmiwO9BI7jDXSN33CCb+rSBe1PCG1LtigUOfzaIVF3c5", // NOLINT
"",
"some id",
&token);
ASSERT_FALSE(success);
}

TEST_F(ContributionUtilTest, GenerateSuggestionEmptyPayload) {
base::Value token(base::Value::Type::DICTIONARY);
bool success = GenerateSuggestion(
"Twsi4QeUNcOOW/IFnYcGLHgLsfVco0oPZ+cl3YeZMQgb4NB8E29Ts+2/pQA54VksqGpg/DtmPRBV4FlQ1VXbKmiwO9BI7jDXSN33CCb+rSBe1PCG1LtigUOfzaIVF3c5", // NOLINT
"rqQ1Tz26C4mv33ld7xpcLhuX1sWaD+s7VMnuX6cokT4=",
"",
&token);
ASSERT_FALSE(success);
}

TEST_F(ContributionUtilTest, GenerateSuggestionGenerated) {
const std::string public_key =
"rqQ1Tz26C4mv33ld7xpcLhuX1sWaD+s7VMnuX6cokT4=";
base::Value token(base::Value::Type::DICTIONARY);
bool success = GenerateSuggestion(
"Twsi4QeUNcOOW/IFnYcGLHgLsfVco0oPZ+cl3YeZMQgb4NB8E29Ts+2/pQA54VksqGpg/DtmPRBV4FlQ1VXbKmiwO9BI7jDXSN33CCb+rSBe1PCG1LtigUOfzaIVF3c5", // NOLINT
public_key,
"some id",
&token);
ASSERT_TRUE(success);
ASSERT_EQ(*token.FindStringKey("t"), "Twsi4QeUNcOOW/IFnYcGLHgLsfVco0oPZ+cl3YeZMQgb4NB8E29Ts+2/pQA54VksqGpg/DtmPRBV4FlQ1VXbKg=="); // NOLINT
ASSERT_EQ(*token.FindStringKey("publicKey"), public_key);
ASSERT_EQ(*token.FindStringKey("signature"), "qnQvRh1dWoc/YKAGVYgP4PljOoK10T8ryMqLGY6RFc3Gig8mZCzmuGH5IQtVtCZ0x42/pOFKfX3rUpzIL4wPUw=="); // NOLINT
}

} // namespace braveledger_contribution
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ void Database::SaveUnblindedTokenList(
}

void Database::GetAllUnblindedTokens(
ledger::GetAllUnblindedTokensCallback callback) {
ledger::GetUnblindedTokenListCallback callback) {
unblinded_token_->GetAllRecords(callback);
}

Expand All @@ -345,4 +345,10 @@ void Database::DeleteUnblindedTokensForPromotion(
unblinded_token_->DeleteRecordsForPromotion(promotion_id, callback);
}

void Database::GetUnblindedTokensByPromotionType(
const std::vector<ledger::PromotionType>& promotion_types,
ledger::GetUnblindedTokenListCallback callback) {
unblinded_token_->GetRecordsByPromotionType(promotion_types, callback);
}

} // namespace braveledger_database
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Database {
ledger::ResultCallback callback);

void GetAllUnblindedTokens(
ledger::GetAllUnblindedTokensCallback callback);
ledger::GetUnblindedTokenListCallback callback);

void DeleteUnblindedTokens(
const std::vector<std::string>& ids,
Expand All @@ -236,6 +236,10 @@ class Database {
const std::string& promotion_id,
ledger::ResultCallback callback);

void GetUnblindedTokensByPromotionType(
const std::vector<ledger::PromotionType>& promotion_types,
ledger::GetUnblindedTokenListCallback callback);

private:
std::unique_ptr<DatabaseInitialize> initialize_;
std::unique_ptr<DatabaseActivityInfo> activity_info_;
Expand Down
Loading