Skip to content

Commit

Permalink
Merge pull request #4960 from brave/creds-table
Browse files Browse the repository at this point in the history
Decouples credentials logic from promotions
  • Loading branch information
NejcZdovc authored Mar 19, 2020
2 parents 5c4a1ac + ec3a638 commit 80efc08
Show file tree
Hide file tree
Showing 50 changed files with 2,651 additions and 1,100 deletions.
113 changes: 113 additions & 0 deletions components/brave_rewards/browser/rewards_database_browsertest.cc

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions components/definitions/rewards.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ declare namespace Rewards {
export enum PromotionStatus {
ACTIVE = 0,
ATTESTED = 1,
CLAIMED = 2,
SIGNED_TOKENS = 3,
FINISHED = 4,
OVER = 5
}
Expand Down
2 changes: 0 additions & 2 deletions components/definitions/rewardsExtensions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ declare namespace RewardsExtension {
export enum PromotionStatus {
ACTIVE = 0,
ATTESTED = 1,
CLAIMED = 2,
SIGNED_TOKENS = 3,
FINISHED = 4,
OVER = 5
}
Expand Down
2 changes: 1 addition & 1 deletion test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ test("brave_unit_tests") {
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/test/niceware_partial_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/database/database_util_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/promotion/promotion_util_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/credentials/credentials_util_unittest.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.cc",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h",
"//brave/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl_mock.cc",
Expand Down
Binary file modified test/data/rewards-data/migration/publisher_info_db_v15
Binary file not shown.
Binary file modified test/data/rewards-data/migration/publisher_info_db_v16
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ index|contribution_info_publishers_contribution_id_index|contribution_info_publi
index|contribution_info_publishers_publisher_key_index|contribution_info_publishers|CREATE INDEX contribution_info_publishers_publisher_key_index ON contribution_info_publishers (publisher_key)
index|contribution_queue_publishers_contribution_queue_id_index|contribution_queue_publishers|CREATE INDEX contribution_queue_publishers_contribution_queue_id_index ON contribution_queue_publishers (contribution_queue_id)
index|contribution_queue_publishers_publisher_key_index|contribution_queue_publishers|CREATE INDEX contribution_queue_publishers_publisher_key_index ON contribution_queue_publishers (publisher_key)
index|creds_batch_trigger_id_index|creds_batch|CREATE INDEX creds_batch_trigger_id_index ON creds_batch (trigger_id)
index|creds_batch_trigger_type_index|creds_batch|CREATE INDEX creds_batch_trigger_type_index ON creds_batch (trigger_type)
index|media_publisher_info_media_key_index|media_publisher_info|CREATE INDEX media_publisher_info_media_key_index ON media_publisher_info (media_key)
index|media_publisher_info_publisher_id_index|media_publisher_info|CREATE INDEX media_publisher_info_publisher_id_index ON media_publisher_info (publisher_id)
index|pending_contribution_publisher_id_index|pending_contribution|CREATE INDEX pending_contribution_publisher_id_index ON pending_contribution (publisher_id)
index|promotion_creds_promotion_id_index|promotion_creds|CREATE INDEX promotion_creds_promotion_id_index ON promotion_creds (promotion_id)
index|promotion_promotion_id_index|promotion|CREATE INDEX promotion_promotion_id_index ON promotion (promotion_id)
index|recurring_donation_publisher_id_index|recurring_donation|CREATE INDEX recurring_donation_publisher_id_index ON recurring_donation (publisher_id)
index|server_publisher_amounts_publisher_key_index|server_publisher_amounts|CREATE INDEX server_publisher_amounts_publisher_key_index ON server_publisher_amounts (publisher_key)
Expand All @@ -15,32 +16,33 @@ index|server_publisher_info_publisher_key_index|server_publisher_info|CREATE IND
index|server_publisher_links_publisher_key_index|server_publisher_links|CREATE INDEX server_publisher_links_publisher_key_index ON server_publisher_links (publisher_key)
index|sqlite_autoindex_activity_info_1|activity_info|
index|sqlite_autoindex_contribution_info_1|contribution_info|
index|sqlite_autoindex_creds_batch_1|creds_batch|
index|sqlite_autoindex_creds_batch_2|creds_batch|
index|sqlite_autoindex_media_publisher_info_1|media_publisher_info|
index|sqlite_autoindex_meta_1|meta|
index|sqlite_autoindex_promotion_1|promotion|
index|sqlite_autoindex_promotion_creds_1|promotion_creds|
index|sqlite_autoindex_publisher_info_1|publisher_info|
index|sqlite_autoindex_recurring_donation_1|recurring_donation|
index|sqlite_autoindex_server_publisher_amounts_1|server_publisher_amounts|
index|sqlite_autoindex_server_publisher_banner_1|server_publisher_banner|
index|sqlite_autoindex_server_publisher_info_1|server_publisher_info|
index|sqlite_autoindex_server_publisher_links_1|server_publisher_links|
index|unblinded_tokens_promotion_id_index|unblinded_tokens|CREATE INDEX unblinded_tokens_promotion_id_index ON unblinded_tokens (promotion_id)
index|unblinded_tokens_creds_id_index|unblinded_tokens|CREATE INDEX unblinded_tokens_creds_id_index ON unblinded_tokens (creds_id)
table|activity_info|activity_info|CREATE TABLE activity_info (publisher_id LONGVARCHAR NOT NULL,duration INTEGER DEFAULT 0 NOT NULL,visits INTEGER DEFAULT 0 NOT NULL,score DOUBLE DEFAULT 0 NOT NULL,percent INTEGER DEFAULT 0 NOT NULL,weight DOUBLE DEFAULT 0 NOT NULL,reconcile_stamp INTEGER DEFAULT 0 NOT NULL,CONSTRAINT activity_unique UNIQUE (publisher_id, reconcile_stamp))
table|contribution_info|contribution_info|CREATE TABLE contribution_info (contribution_id TEXT NOT NULL,amount DOUBLE NOT NULL,type INTEGER NOT NULL,step INTEGER NOT NULL DEFAULT -1,retry_count INTEGER NOT NULL DEFAULT -1,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, processor INTEGER NOT NULL DEFAULT 1,PRIMARY KEY (contribution_id))
table|contribution_info_publishers|contribution_info_publishers|CREATE TABLE contribution_info_publishers (contribution_id TEXT NOT NULL,publisher_key TEXT NOT NULL,total_amount DOUBLE NOT NULL,contributed_amount DOUBLE)
table|contribution_queue|contribution_queue|CREATE TABLE contribution_queue (contribution_queue_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,type INTEGER NOT NULL,amount DOUBLE NOT NULL,partial INTEGER NOT NULL DEFAULT 0,created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)
table|contribution_queue_publishers|contribution_queue_publishers|CREATE TABLE contribution_queue_publishers (contribution_queue_id INTEGER NOT NULL,publisher_key TEXT NOT NULL,amount_percent DOUBLE NOT NULL)
table|creds_batch|creds_batch|CREATE TABLE creds_batch (creds_id TEXT PRIMARY KEY NOT NULL,trigger_id TEXT NOT NULL,trigger_type INT NOT NULL,creds TEXT NOT NULL,blinded_creds TEXT NOT NULL,signed_creds TEXT,public_key TEXT,batch_proof TEXT,status INT NOT NULL DEFAULT 0,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,CONSTRAINT creds_batch_unique UNIQUE (trigger_id, trigger_type))
table|media_publisher_info|media_publisher_info|CREATE TABLE media_publisher_info (media_key TEXT NOT NULL PRIMARY KEY UNIQUE,publisher_id LONGVARCHAR NOT NULL)
table|meta|meta|CREATE TABLE meta(key LONGVARCHAR NOT NULL UNIQUE PRIMARY KEY, value LONGVARCHAR)
table|pending_contribution|pending_contribution|CREATE TABLE pending_contribution (pending_contribution_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,publisher_id LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,added_date INTEGER DEFAULT 0 NOT NULL,viewing_id LONGVARCHAR NOT NULL,type INTEGER NOT NULL)
table|promotion|promotion|CREATE TABLE promotion (promotion_id TEXT NOT NULL,version INTEGER NOT NULL,type INTEGER NOT NULL,public_keys TEXT NOT NULL,suggestions INTEGER NOT NULL DEFAULT 0,approximate_value DOUBLE NOT NULL DEFAULT 0,status INTEGER NOT NULL DEFAULT 0,expires_at TIMESTAMP NOT NULL,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, claimed_at TIMESTAMP,PRIMARY KEY (promotion_id))
table|promotion_creds|promotion_creds|CREATE TABLE promotion_creds (promotion_id TEXT UNIQUE NOT NULL,tokens TEXT NOT NULL,blinded_creds TEXT NOT NULL,signed_creds TEXT,public_key TEXT,batch_proof TEXT,claim_id TEXT)
table|promotion|promotion|CREATE TABLE promotion (promotion_id TEXT NOT NULL,version INTEGER NOT NULL,type INTEGER NOT NULL,public_keys TEXT NOT NULL,suggestions INTEGER NOT NULL DEFAULT 0,approximate_value DOUBLE NOT NULL DEFAULT 0,status INTEGER NOT NULL DEFAULT 0,expires_at TIMESTAMP NOT NULL,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, claimed_at TIMESTAMP, claim_id TEXT,PRIMARY KEY (promotion_id))
table|publisher_info|publisher_info|CREATE TABLE publisher_info(publisher_id LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,excluded INTEGER DEFAULT 0 NOT NULL,name TEXT NOT NULL,favIcon TEXT NOT NULL,url TEXT NOT NULL,provider TEXT NOT NULL)
table|recurring_donation|recurring_donation|CREATE TABLE recurring_donation (publisher_id LONGVARCHAR NOT NULL PRIMARY KEY UNIQUE,amount DOUBLE DEFAULT 0 NOT NULL,added_date INTEGER DEFAULT 0 NOT NULL)
table|server_publisher_amounts|server_publisher_amounts|CREATE TABLE server_publisher_amounts (publisher_key LONGVARCHAR NOT NULL,amount DOUBLE DEFAULT 0 NOT NULL,CONSTRAINT server_publisher_amounts_unique UNIQUE (publisher_key, amount))
table|server_publisher_banner|server_publisher_banner|CREATE TABLE server_publisher_banner (publisher_key LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,title TEXT,description TEXT,background TEXT,logo TEXT)
table|server_publisher_info|server_publisher_info|CREATE TABLE server_publisher_info (publisher_key LONGVARCHAR PRIMARY KEY NOT NULL UNIQUE,status INTEGER DEFAULT 0 NOT NULL,excluded INTEGER DEFAULT 0 NOT NULL,address TEXT NOT NULL)
table|server_publisher_links|server_publisher_links|CREATE TABLE server_publisher_links (publisher_key LONGVARCHAR NOT NULL,provider TEXT,link TEXT,CONSTRAINT server_publisher_links_unique UNIQUE (publisher_key, provider))
table|sqlite_sequence|sqlite_sequence|CREATE TABLE sqlite_sequence(name,seq)
table|unblinded_tokens|unblinded_tokens|CREATE TABLE unblinded_tokens (token_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,token_value TEXT,public_key TEXT,value DOUBLE NOT NULL DEFAULT 0,promotion_id TEXT,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)
table|unblinded_tokens|unblinded_tokens|CREATE TABLE unblinded_tokens (token_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,token_value TEXT,public_key TEXT,value DOUBLE NOT NULL DEFAULT 0,creds_id TEXT,expires_at TIMESTAMP NOT NULL DEFAULT 0,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)
11 changes: 11 additions & 0 deletions vendor/bat-native-ledger/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ source_set("ledger") {
"src/bat/ledger/internal/contribution/phase_two.h",
"src/bat/ledger/internal/contribution/unverified.cc",
"src/bat/ledger/internal/contribution/unverified.h",
"src/bat/ledger/internal/credentials/credentials.h",
"src/bat/ledger/internal/credentials/credentials_common.cc",
"src/bat/ledger/internal/credentials/credentials_common.h",
"src/bat/ledger/internal/credentials/credentials_factory.cc",
"src/bat/ledger/internal/credentials/credentials_factory.h",
"src/bat/ledger/internal/credentials/credentials_promotion.cc",
"src/bat/ledger/internal/credentials/credentials_promotion.h",
"src/bat/ledger/internal/credentials/credentials_util.cc",
"src/bat/ledger/internal/credentials/credentials_util.h",
"src/bat/ledger/internal/database/database.cc",
"src/bat/ledger/internal/database/database.h",
"src/bat/ledger/internal/database/database_activity_info.cc",
Expand All @@ -133,6 +142,8 @@ source_set("ledger") {
"src/bat/ledger/internal/database/database_contribution_queue.h",
"src/bat/ledger/internal/database/database_contribution_queue_publishers.cc",
"src/bat/ledger/internal/database/database_contribution_queue_publishers.h",
"src/bat/ledger/internal/database/database_creds_batch.cc",
"src/bat/ledger/internal/database/database_creds_batch.h",
"src/bat/ledger/internal/database/database_initialize.cc",
"src/bat/ledger/internal/database/database_initialize.h",
"src/bat/ledger/internal/database/database_media_publisher_info.cc",
Expand Down
4 changes: 4 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ using RunDBTransactionCallback = std::function<void(DBCommandResponsePtr)>;
using GetCreateScriptCallback =
std::function<void(const std::string&, const int)>;

using GetCredsBatchCallback = std::function<void(CredsBatchPtr)>;
using GetAllCredsBatchCallback = std::function<void(CredsBatchList)>;
using GetPromotionListCallback = std::function<void(PromotionList)>;

class LEDGER_EXPORT LedgerClient {
public:
virtual ~LedgerClient() = default;
Expand Down
11 changes: 8 additions & 3 deletions vendor/bat-native-ledger/include/bat/ledger/mojom_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ using ContributionRetry = mojom::ContributionRetry;

using ContributionStep = mojom::ContributionStep;

using CredsBatch = mojom::CredsBatch;
using CredsBatchPtr = mojom::CredsBatchPtr;
using CredsBatchList = std::vector<CredsBatchPtr>;

using CredsBatchType = mojom::CredsBatchType;

using CredsBatchStatus = mojom::CredsBatchStatus;

using Environment = ledger::mojom::Environment;

using ExcludeFilter = mojom::ExcludeFilter;
Expand Down Expand Up @@ -98,9 +106,6 @@ using PromotionMap = std::map<std::string, PromotionPtr>;

using PromotionType = mojom::PromotionType;

using PromotionCreds = mojom::PromotionCreds;
using PromotionCredsPtr = mojom::PromotionCredsPtr;

using PromotionStatus = mojom::PromotionStatus;

using PublisherBanner = mojom::PublisherBanner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ enum Result {
BAT_NOT_ALLOWED = 25,
ALREADY_EXISTS = 26,
SAFETYNET_ATTESTATION_FAILED = 27,
DATABASE_INIT_FAILED = 28
DATABASE_INIT_FAILED = 28,
RETRY = 29
};

enum PublisherStatus {
Expand Down Expand Up @@ -355,8 +356,6 @@ enum PromotionType {
enum PromotionStatus {
ACTIVE = 0,
ATTESTED = 1,
CLAIMED = 2,
SIGNED_TOKENS = 3,
FINISHED = 4,
OVER = 5
};
Expand All @@ -372,15 +371,6 @@ struct Promotion {
uint64 expires_at;
uint64 claimed_at;
bool legacy_claimed;
PromotionCreds? credentials;
};

struct PromotionCreds {
string tokens; // JSONStringArray
string blinded_creds; // JSONStringArray
string signed_creds; // JSONStringArray
string public_key;
string batch_proof;
string claim_id;
};

Expand All @@ -389,7 +379,7 @@ struct UnblindedToken {
string token_value;
string public_key;
double value;
string promotion_id;
string creds_id;
uint64 expires_at;
};

Expand Down Expand Up @@ -436,3 +426,30 @@ struct MonthlyReportInfo {
array<TransactionReportInfo> transactions;
array<ContributionReportInfo> contributions;
};

enum CredsBatchType {
NONE = 0,
PROMOTION = 1
};

enum CredsBatchStatus {
NONE = 0,
BLINDED = 1,
CLAIMED = 2,
SIGNED = 3,
FINISHED = 4
};

struct CredsBatch {
string creds_id;
int32 size;
string creds; // JSONStringArray
string blinded_creds; // JSONStringArray
string signed_creds; // JSONStringArray
string public_key;
string batch_proof;
string trigger_id;
CredsBatchType trigger_type;
CredsBatchStatus status;
};

Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,6 @@ ledger::ContributionQueuePtr FromStringToContributionQueue(
}

std::string FromPromotionToString(const ledger::PromotionPtr info) {
base::Value credentials(base::Value::Type::DICTIONARY);
if (info->credentials) {
credentials.SetStringKey("tokens", info->credentials->tokens);
credentials.SetStringKey("blinded_creds", info->credentials->blinded_creds);
credentials.SetStringKey("signed_creds", info->credentials->signed_creds);
credentials.SetStringKey("public_key", info->credentials->public_key);
credentials.SetStringKey("batch_proof", info->credentials->batch_proof);
credentials.SetStringKey("claim_id", info->credentials->claim_id);
}

base::Value promotion(base::Value::Type::DICTIONARY);
promotion.SetStringKey("id", info->id);
promotion.SetStringKey("public_keys", info->public_keys);
Expand All @@ -114,7 +104,6 @@ std::string FromPromotionToString(const ledger::PromotionPtr info) {
promotion.SetIntKey("type", static_cast<int>(info->type));
promotion.SetIntKey("suggestions", info->suggestions);
promotion.SetIntKey("status", static_cast<int>(info->status));
promotion.SetKey("credentials", std::move(credentials));
promotion.SetBoolKey("legacy_claimed", info->legacy_claimed);

std::string json;
Expand Down Expand Up @@ -186,43 +175,6 @@ ledger::PromotionPtr FromStringToPromotion(const std::string& data) {
promotion->legacy_claimed = *legacy_claimed;
}

auto* credentials = dictionary->FindDictKey("credentials");
if (credentials) {
auto creds = ledger::PromotionCreds::New();

auto* tokens = credentials->FindStringKey("tokens");
if (tokens) {
creds->tokens = *tokens;
}

auto* blinded_creds = credentials->FindStringKey("blinded_creds");
if (blinded_creds) {
creds->blinded_creds = *blinded_creds;
}

auto* signed_creds = credentials->FindStringKey("signed_creds");
if (signed_creds) {
creds->signed_creds = *signed_creds;
}

auto* public_key = credentials->FindStringKey("public_key");
if (public_key) {
creds->public_key = *public_key;
}

auto* batch_proof = credentials->FindStringKey("batch_proof");
if (batch_proof) {
creds->batch_proof = *batch_proof;
}

auto* claim_id = credentials->FindStringKey("claim_id");
if (claim_id) {
creds->claim_id = *claim_id;
}

promotion->credentials = std::move(creds);
}

return promotion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,6 @@

namespace braveledger_helper {

std::vector<Token> Security::GenerateTokens(const int count) {
DCHECK_GT(count, 0);
std::vector<Token> tokens;

for (auto i = 0; i < count; i++) {
auto token = Token::random();
tokens.push_back(token);
}

return tokens;
}

std::vector<BlindedToken> Security::BlindTokens(
const std::vector<Token>& tokens) {
DCHECK_NE(tokens.size(), 0UL);

std::vector<BlindedToken> blinded_tokens;
for (unsigned int i = 0; i < tokens.size(); i++) {
auto token = tokens.at(i);
auto blinded_token = token.blind();

blinded_tokens.push_back(blinded_token);
}

return blinded_tokens;
}

std::string Security::GetBase64(const std::vector<uint8_t>& data) {
DCHECK_NE(data.size(), 0UL);
size_t size = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,10 @@
#include <string>
#include <vector>

#include "wrapper.hpp"

using challenge_bypass_ristretto::Token;
using challenge_bypass_ristretto::BlindedToken;

namespace braveledger_helper {

class Security {
public:
static std::vector<Token> GenerateTokens(const int count);

static std::vector<BlindedToken> BlindTokens(
const std::vector<Token>& tokens);

static std::vector<uint8_t> GetSHA256(const std::string& string);

static std::string GetBase64(const std::vector<uint8_t>& data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void Unblinded::OnUnblindedTokens(
new_item.token_value = item->token_value;
new_item.public_key = item->public_key;
new_item.value = item->value;
new_item.promotion_id = item->promotion_id;
new_item.creds_id = item->creds_id;
new_item.expires_at = item->expires_at;

converted_list.push_back(new_item);
Expand Down
Loading

0 comments on commit 80efc08

Please sign in to comment.