diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc index 474bd903d5e2..e1c67de4da00 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.cc @@ -172,66 +172,50 @@ bool ConfirmationsImpl::FromJSON(const std::string& json) { } // Catalog issuers - auto* catalog_issuers_value = dictionary->FindKey("catalog_issuers"); - if (!catalog_issuers_value) { - return false; + if (!GetCatalogIssuersFromJSON(dictionary.get())) { + BLOG(WARNING) << "Failed to get catalog issuers from JSON: " << json; } - base::DictionaryValue* catalog_issuers_dictionary; - if (!catalog_issuers_value->GetAsDictionary(&catalog_issuers_dictionary)) { - return false; + // Transaction history + if (!GetTransactionHistoryFromJSON(dictionary.get())) { + BLOG(WARNING) << "Failed to get transaction history from JSON: " << json; } - std::string public_key; - std::map catalog_issuers; - if (!GetCatalogIssuersFromDictionary(catalog_issuers_dictionary, &public_key, - &catalog_issuers)) { - return false; + // Unblinded tokens + if (!GetUnblindedTokensFromJSON(dictionary.get())) { + BLOG(WARNING) << "Failed to get unblinded tokens from JSON: " << json; } - // Transaction history - auto* transaction_history_value = dictionary->FindKey("transaction_history"); - if (!transaction_history_value) { - return false; + // Unblinded payment tokens + if (!GetUnblindedPaymentTokensFromJSON(dictionary.get())) { + BLOG(WARNING) << + "Failed to get unblinded payment tokens from JSON: " << json; } - base::DictionaryValue* transaction_history_dictionary; - if (!transaction_history_value->GetAsDictionary( - &transaction_history_dictionary)) { - return false; - } + return true; +} - std::vector transaction_history; - if (!GetTransactionHistoryFromDictionary(transaction_history_dictionary, - &transaction_history)) { +bool ConfirmationsImpl::GetCatalogIssuersFromJSON( + base::DictionaryValue* dictionary) { + auto* catalog_issuers_value = dictionary->FindKey("catalog_issuers"); + if (!catalog_issuers_value) { return false; } - transaction_history_ = transaction_history; - - // Unblinded tokens - auto* unblinded_tokens_value = dictionary->FindKey("unblinded_tokens"); - if (!unblinded_tokens_value) { + base::DictionaryValue* catalog_issuers_dictionary; + if (!catalog_issuers_value->GetAsDictionary(&catalog_issuers_dictionary)) { return false; } - base::ListValue unblinded_token_values(unblinded_tokens_value->GetList()); - - // Unblinded payment tokens - auto* unblinded_payment_tokens_value = - dictionary->FindKey("unblinded_payment_tokens"); - if (!unblinded_payment_tokens_value) { + std::string public_key; + std::map catalog_issuers; + if (!GetCatalogIssuersFromDictionary(dictionary, &public_key, + &catalog_issuers)) { return false; } - base::ListValue unblinded_payment_token_values( - unblinded_payment_tokens_value->GetList()); - - // Update state public_key_ = public_key; catalog_issuers_ = catalog_issuers; - unblinded_tokens_->SetTokensFromList(unblinded_token_values); - unblinded_payment_tokens_->SetTokensFromList(unblinded_payment_token_values); return true; } @@ -285,6 +269,30 @@ bool ConfirmationsImpl::GetCatalogIssuersFromDictionary( return true; } +bool ConfirmationsImpl::GetTransactionHistoryFromJSON( + base::DictionaryValue* dictionary) { + auto* transaction_history_value = dictionary->FindKey("transaction_history"); + if (!transaction_history_value) { + return false; + } + + base::DictionaryValue* transaction_history_dictionary; + if (!transaction_history_value->GetAsDictionary( + &transaction_history_dictionary)) { + return false; + } + + std::vector transaction_history; + if (!GetTransactionHistoryFromDictionary(transaction_history_dictionary, + &transaction_history)) { + return false; + } + + transaction_history_ = transaction_history; + + return true; +} + bool ConfirmationsImpl::GetTransactionHistoryFromDictionary( base::DictionaryValue* dictionary, std::vector* transaction_history) { @@ -294,6 +302,7 @@ bool ConfirmationsImpl::GetTransactionHistoryFromDictionary( // Transaction auto* transactions_value = dictionary->FindKey("transactions"); if (!transactions_value) { + DCHECK(false) << "Transactions history dictionary missing transactions"; return false; } @@ -302,7 +311,8 @@ bool ConfirmationsImpl::GetTransactionHistoryFromDictionary( for (auto& transaction_value : transactions_list_value) { base::DictionaryValue* transaction_dictionary; if (!transaction_value.GetAsDictionary(&transaction_dictionary)) { - return false; + DCHECK(false) << "Transaction should be a dictionary"; + continue; } TransactionInfo info; @@ -310,28 +320,36 @@ bool ConfirmationsImpl::GetTransactionHistoryFromDictionary( // Timestamp auto* timestamp_in_seconds_value = transaction_dictionary->FindKey("timestamp_in_seconds"); - if (!timestamp_in_seconds_value) { - return false; + if (timestamp_in_seconds_value) { + info.timestamp_in_seconds = + std::stoull(timestamp_in_seconds_value->GetString()); + } else { + // timestamp missing, fallback to default + auto now = base::Time::Now(); + info.timestamp_in_seconds = + static_cast((now - base::Time()).InSeconds()); } - info.timestamp_in_seconds = - std::stoull(timestamp_in_seconds_value->GetString()); // Estimated redemption value auto* estimated_redemption_value_value = transaction_dictionary->FindKey("estimated_redemption_value"); - if (!estimated_redemption_value_value) { - return false; + if (estimated_redemption_value_value) { + info.estimated_redemption_value = + estimated_redemption_value_value->GetDouble(); + } else { + // estimated redemption value missing, fallback to default + info.estimated_redemption_value = 0.0; } - info.estimated_redemption_value = - estimated_redemption_value_value->GetDouble(); - // Confirmation type + // Confirmation type (>= 0.63.8) auto* confirmation_type_value = transaction_dictionary->FindKey("confirmation_type"); - if (!confirmation_type_value) { - return false; + if (confirmation_type_value) { + info.confirmation_type = confirmation_type_value->GetString(); + } else { + // confirmation type missing, fallback to default + info.confirmation_type = kConfirmationTypeView; } - info.confirmation_type = confirmation_type_value->GetString(); transaction_history->push_back(info); } @@ -339,6 +357,37 @@ bool ConfirmationsImpl::GetTransactionHistoryFromDictionary( return true; } +bool ConfirmationsImpl::GetUnblindedTokensFromJSON( + base::DictionaryValue* dictionary) { + auto* unblinded_tokens_value = dictionary->FindKey("unblinded_tokens"); + if (!unblinded_tokens_value) { + return false; + } + + base::ListValue unblinded_token_values(unblinded_tokens_value->GetList()); + + unblinded_tokens_->SetTokensFromList(unblinded_token_values); + + return true; +} + +bool ConfirmationsImpl::GetUnblindedPaymentTokensFromJSON( + base::DictionaryValue* dictionary) { + auto* unblinded_payment_tokens_value = + dictionary->FindKey("unblinded_payment_tokens"); + if (!unblinded_payment_tokens_value) { + return false; + } + + base::ListValue unblinded_payment_token_values( + unblinded_payment_tokens_value->GetList()); + + unblinded_payment_tokens_->SetTokensFromList( + unblinded_payment_token_values); + + return true; +} + void ConfirmationsImpl::SaveState() { BLOG(INFO) << "Saving confirmations state"; @@ -490,7 +539,7 @@ double ConfirmationsImpl::GetEstimatedRedemptionValue( auto name = it->second; if (!re2::RE2::Replace(&name, "BAT", "")) { BLOG(ERROR) << "Could not estimate redemption value due to catalog" - << " issuer name missing BAT"; + << " issuer name missing BAT"; } estimated_redemption_value = stod(name); diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h index 6c773c31439b..0f16c24de359 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_impl.h @@ -125,15 +125,25 @@ class ConfirmationsImpl : public Confirmations { bool FromJSON(const std::string& json); + bool GetCatalogIssuersFromJSON( + base::DictionaryValue* dictionary); bool GetCatalogIssuersFromDictionary( base::DictionaryValue* dictionary, std::string* public_key, std::map* issuers) const; + bool GetTransactionHistoryFromJSON( + base::DictionaryValue* dictionary); bool GetTransactionHistoryFromDictionary( base::DictionaryValue* dictionary, std::vector* transaction_history); + bool GetUnblindedTokensFromJSON( + base::DictionaryValue* dictionary); + + bool GetUnblindedPaymentTokensFromJSON( + base::DictionaryValue* dictionary); + // Confirmations::Client ConfirmationsClient* confirmations_client_; // NOT OWNED diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_unblinded_tokens_unittest.cc b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_unblinded_tokens_unittest.cc index 959b05d08034..615113eebb8f 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_unblinded_tokens_unittest.cc +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_unblinded_tokens_unittest.cc @@ -153,18 +153,21 @@ class ConfirmationsUnblindedTokensTest : public ::testing::Test { return unblinded_tokens; } - base::ListValue GetUnblindedTokensAsList(const int count) { + base::Value GetUnblindedTokensAsList(const int count) { base::Value list(base::Value::Type::LIST); - // auto tokens = GetUnblindedTokens(count); - // for (const auto& token : tokens) { - // auto token_base64 = token.encode_base64(); - // auto token_value = base::Value(token_base64); - // list.GetList().push_back(std::move(token_value)); - // } + auto tokens = GetUnblindedTokens(count); - base::ListValue list_values(list.GetList()); - return list_values; + for (const auto& token : tokens) { + base::Value dictionary(base::Value::Type::DICTIONARY); + dictionary.SetKey("unblinded_token", base::Value( + token.unblinded_token.encode_base64())); + dictionary.SetKey("public_key", base::Value(token.public_key)); + + list.GetList().push_back(std::move(dictionary)); + } + + return list; } }; @@ -208,6 +211,8 @@ TEST_F(ConfirmationsUnblindedTokensTest, GetAllTokens_Exist) { std::string expected_public_key = "RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="; // NOLINT + EXPECT_EQ(tokens.size(), expected_unblinded_tokens_base64.size()); + unsigned int index = 0; for (const auto& token_info : tokens) { auto expected_unblinded_token_base64 = @@ -250,6 +255,7 @@ TEST_F(ConfirmationsUnblindedTokensTest, GetTokensAsList_Exist) { // Assert base::ListValue list_values(list.GetList()); + EXPECT_EQ(list_values.GetSize(), unblinded_tokens.size()); for (auto& value : list_values) { base::DictionaryValue* dictionary; if (!value.GetAsDictionary(&dictionary)) { @@ -323,6 +329,7 @@ TEST_F(ConfirmationsUnblindedTokensTest, SetTokens_Exist) { // Assert unsigned int index = 0; auto tokens = unblinded_tokens_->GetAllTokens(); + EXPECT_EQ(tokens.size(), unblinded_tokens.size()); for (const auto& token_info : tokens) { auto expected_token_info = unblinded_tokens.at(index); if (token_info.unblinded_token != expected_token_info.unblinded_token) { @@ -386,6 +393,8 @@ TEST_F(ConfirmationsUnblindedTokensTest, SetTokensFromList) { auto tokens = unblinded_tokens_->GetAllTokens(); + EXPECT_EQ(tokens.size(), expected_unblinded_tokens_base64.size()); + unsigned int index = 0; for (const auto& token_info : tokens) { auto expected_unblinded_token_base64 = diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/unblinded_tokens.cc b/vendor/bat-native-confirmations/src/bat/confirmations/internal/unblinded_tokens.cc index d1881e3b8b1b..ad208e506d6e 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/unblinded_tokens.cc +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/unblinded_tokens.cc @@ -49,7 +49,7 @@ void UnblindedTokens::SetTokens( confirmations_->SaveState(); } -bool UnblindedTokens::SetTokensFromList(const base::Value& list) { +void UnblindedTokens::SetTokensFromList(const base::Value& list) { base::ListValue list_values(list.GetList()); std::vector tokens; @@ -64,20 +64,23 @@ bool UnblindedTokens::SetTokensFromList(const base::Value& list) { } else { base::DictionaryValue* dictionary; if (!value.GetAsDictionary(&dictionary)) { - return false; + DCHECK(false) << "Unblinded token should be a dictionary"; + continue; } // Unblinded token auto* unblinded_token_value = dictionary->FindKey("unblinded_token"); if (!unblinded_token_value) { - return false; + DCHECK(false) << "Unblinded token dictionary missing unblinded_token"; + continue; } unblinded_token = unblinded_token_value->GetString(); // Public key auto* public_key_value = dictionary->FindKey("public_key"); if (!public_key_value) { - return false; + DCHECK(false) << "Unblinded token dictionary missing public_key"; + continue; } public_key = public_key_value->GetString(); } @@ -90,8 +93,6 @@ bool UnblindedTokens::SetTokensFromList(const base::Value& list) { } SetTokens(tokens); - - return true; } void UnblindedTokens::AddTokens( diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/unblinded_tokens.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/unblinded_tokens.h index 0918449e4e26..688266194229 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/unblinded_tokens.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/unblinded_tokens.h @@ -27,7 +27,7 @@ class UnblindedTokens { base::Value GetTokensAsList(); void SetTokens(const std::vector& tokens); - bool SetTokensFromList(const base::Value& list); + void SetTokensFromList(const base::Value& list); void AddTokens(const std::vector& tokens);