From 8d5310a3a5d50eec0d38a4b9edf34109b4dbe707 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 31 Jan 2023 15:22:43 -0500 Subject: [PATCH] Cleanup Brave Ads tokens/wallet --- components/brave_ads/test/BUILD.gn | 2 - components/services/bat_ads/bat_ads_impl.cc | 4 +- components/services/bat_ads/bat_ads_impl.h | 2 +- .../bat_ads/public/interfaces/bat_ads.mojom | 2 +- vendor/bat-native-ads/DEPS | 1 + vendor/bat-native-ads/include/bat/ads/ads.h | 2 +- .../src/bat/ads/internal/account/account.cc | 27 +- .../src/bat/ads/internal/account/account.h | 4 +- .../ads/internal/account/account_unittest.cc | 32 +- .../conversion_user_data_builder_unittest.cc | 2 +- .../user_data/rotating_hash_user_data.cc | 2 +- ...edeem_unblinded_payment_tokens_unittest.cc | 10 +- ...nded_payment_tokens_url_request_builder.cc | 4 +- ...ent_tokens_url_request_builder_unittest.cc | 29 +- .../get_signed_tokens_url_request_builder.cc | 2 +- ...ned_tokens_url_request_builder_unittest.cc | 12 +- .../refill_unblinded_tokens.cc | 19 + .../refill_unblinded_tokens_unittest.cc | 28 +- ...quest_signed_tokens_url_request_builder.cc | 34 +- ...ned_tokens_url_request_builder_unittest.cc | 29 +- .../bat/ads/internal/account/wallet/wallet.cc | 20 +- .../bat/ads/internal/account/wallet/wallet.h | 5 +- .../internal/account/wallet/wallet_info.cc | 5 +- .../ads/internal/account/wallet/wallet_info.h | 3 +- .../account/wallet/wallet_unittest.cc | 62 +--- .../account/wallet/wallet_unittest_util.cc | 36 +- .../account/wallet/wallet_unittest_util.h | 9 +- .../src/bat/ads/internal/ads_impl.cc | 5 +- .../src/bat/ads/internal/ads_impl.h | 2 +- .../common/crypto/crypto_unittest_util.cc | 31 -- .../common/crypto/crypto_unittest_util.h | 21 -- .../ads/internal/common/crypto/crypto_util.cc | 189 +++++----- .../ads/internal/common/crypto/crypto_util.h | 26 +- .../common/crypto/crypto_util_unittest.cc | 193 +++------- .../internal/common/crypto/key_pair_info.cc | 6 +- .../internal/common/crypto/key_pair_info.h | 4 +- .../internal/common/unittest/unittest_base.cc | 16 +- .../internal/common/unittest/unittest_base.h | 2 - .../internal/conversions/conversions_util.cc | 8 +- ...iable_conversion_envelope_unittest_util.cc | 8 +- ...fiable_conversion_envelope_unittest_util.h | 2 +- .../confirmation_state_manager.cc | 351 ++++++++++-------- .../confirmation_state_manager.h | 12 +- .../text_processing/embedding_processing.cc | 2 +- .../unblinded_tokens/unblinded_token_info.cc | 3 +- .../unblinded_tokens/unblinded_token_info.h | 2 + .../unblinded_tokens/unblinded_token_util.cc | 3 +- .../unblinded_token_value_util.cc | 10 + .../unblinded_token_value_util_unittest.cc | 2 + .../unblinded_tokens_unittest_util.cc | 20 +- .../unblinded_tokens_unittest_util.h | 15 +- ...text_embedding_html_event_unittest_util.cc | 2 +- 52 files changed, 649 insertions(+), 673 deletions(-) delete mode 100644 vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.cc delete mode 100644 vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.h diff --git a/components/brave_ads/test/BUILD.gn b/components/brave_ads/test/BUILD.gn index 8e9c59b2aa43..dd7cf7c85d71 100644 --- a/components/brave_ads/test/BUILD.gn +++ b/components/brave_ads/test/BUILD.gn @@ -198,8 +198,6 @@ source_set("brave_ads_unit_tests") { "//brave/vendor/bat-native-ads/src/bat/ads/internal/common/calendar/calendar_leap_year_util_unittest.cc", "//brave/vendor/bat-native-ads/src/bat/ads/internal/common/calendar/calendar_util_unittest.cc", "//brave/vendor/bat-native-ads/src/bat/ads/internal/common/containers/container_util_unittest.cc", - "//brave/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.cc", - "//brave/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.h", "//brave/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util_unittest.cc", "//brave/vendor/bat-native-ads/src/bat/ads/internal/common/locale/subdivision_code_util_unittest.cc", "//brave/vendor/bat-native-ads/src/bat/ads/internal/common/numbers/number_util_unittest.cc", diff --git a/components/services/bat_ads/bat_ads_impl.cc b/components/services/bat_ads/bat_ads_impl.cc index 8a4ad6a77d71..42b659774959 100644 --- a/components/services/bat_ads/bat_ads_impl.cc +++ b/components/services/bat_ads/bat_ads_impl.cc @@ -226,8 +226,8 @@ void BatAdsImpl::RemoveAllHistory(RemoveAllHistoryCallback callback) { } void BatAdsImpl::OnRewardsWalletDidChange(const std::string& payment_id, - const std::string& seed) { - ads_->OnRewardsWalletDidChange(payment_id, seed); + const std::string& recovery_seed) { + ads_->OnRewardsWalletDidChange(payment_id, recovery_seed); } void BatAdsImpl::GetHistory(const base::Time from_time, diff --git a/components/services/bat_ads/bat_ads_impl.h b/components/services/bat_ads/bat_ads_impl.h index e44aa1baee80..9e13259516c3 100644 --- a/components/services/bat_ads/bat_ads_impl.h +++ b/components/services/bat_ads/bat_ads_impl.h @@ -78,7 +78,7 @@ class BatAdsImpl : public mojom::BatAds { void OnDidCloseTab(int32_t tab_id) override; void OnRewardsWalletDidChange(const std::string& payment_id, - const std::string& seed) override; + const std::string& recovery_seed) override; void GetStatementOfAccounts(GetStatementOfAccountsCallback callback) override; diff --git a/components/services/bat_ads/public/interfaces/bat_ads.mojom b/components/services/bat_ads/public/interfaces/bat_ads.mojom index 17c28c39468c..d247c2759e6e 100644 --- a/components/services/bat_ads/public/interfaces/bat_ads.mojom +++ b/components/services/bat_ads/public/interfaces/bat_ads.mojom @@ -136,7 +136,7 @@ interface BatAds { OnDidCloseTab(int32 tab_id); // Account - OnRewardsWalletDidChange(string payment_id, string seed); + OnRewardsWalletDidChange(string payment_id, string recovery_seed); GetStatementOfAccounts() => (ads.mojom.StatementInfo? statement); diff --git a/vendor/bat-native-ads/DEPS b/vendor/bat-native-ads/DEPS index e49cb3663355..74f8f293af3b 100644 --- a/vendor/bat-native-ads/DEPS +++ b/vendor/bat-native-ads/DEPS @@ -1,4 +1,5 @@ include_rules = [ "+absl/types/optional.h", "+bat/ads", + "+third_party/boringssl/src/include/openssl/curve25519.h", ] diff --git a/vendor/bat-native-ads/include/bat/ads/ads.h b/vendor/bat-native-ads/include/bat/ads/ads.h index 3972cc97b529..b985f2696ed7 100644 --- a/vendor/bat-native-ads/include/bat/ads/ads.h +++ b/vendor/bat-native-ads/include/bat/ads/ads.h @@ -143,7 +143,7 @@ class ADS_EXPORT Ads { // Called when the user's Brave Rewards wallet has changed. virtual void OnRewardsWalletDidChange(const std::string& payment_id, - const std::string& seed) = 0; + const std::string& recovery_seed) = 0; // Called to get the statement of accounts. The callback takes one argument - // |mojom::StatementInfo| containing info of the obtained statement of diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/account.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/account.cc index 462516533dc0..51c5dbcd2211 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/account.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/account.cc @@ -6,7 +6,10 @@ #include "bat/ads/internal/account/account.h" #include +#include +#include "absl/types/optional.h" +#include "base/base64.h" #include "base/check_op.h" #include "base/functional/bind.h" #include "bat/ads/internal/account/account_util.h" @@ -74,10 +77,19 @@ void Account::RemoveObserver(AccountObserver* observer) { observers_.RemoveObserver(observer); } -void Account::SetWallet(const std::string& id, const std::string& seed) { +void Account::SetWallet(const std::string& payment_id, + const std::string& recovery_seed) { + const absl::optional> raw_recovery_seed = + base::Base64Decode(recovery_seed); + if (!raw_recovery_seed) { + BLOG(0, "Failed to set wallet"); + NotifyInvalidWallet(); + return; + } + const WalletInfo last_wallet_copy = GetWallet(); - if (!wallet_->Set(id, seed)) { + if (!wallet_->Set(payment_id, *raw_recovery_seed)) { BLOG(0, "Failed to set wallet"); NotifyInvalidWallet(); return; @@ -86,8 +98,7 @@ void Account::SetWallet(const std::string& id, const std::string& seed) { const WalletInfo& wallet = GetWallet(); if (wallet.WasUpdated(last_wallet_copy)) { - BLOG(1, "Successfully set wallet"); - NotifyWalletDidUpdate(wallet); + WalletDidUpdate(wallet); } if (wallet.HasChanged(last_wallet_copy)) { @@ -211,6 +222,12 @@ void Account::ProcessUnclearedTransactions() const { redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet); } +void Account::WalletDidUpdate(const WalletInfo& wallet) const { + BLOG(1, "Successfully set wallet"); + + NotifyWalletDidUpdate(wallet); +} + void Account::WalletDidChange(const WalletInfo& wallet) const { BLOG(1, "Wallet changed"); @@ -376,7 +393,7 @@ void Account::OnCaptchaRequiredToRefillUnblindedTokens( #endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) AdsClientHelper::GetInstance()->ShowScheduledCaptchaNotification( - wallet.id, captcha_id, should_show_tooltip_notification); + wallet.payment_id, captcha_id, should_show_tooltip_notification); } } // namespace ads diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/account.h b/vendor/bat-native-ads/src/bat/ads/internal/account/account.h index ca6c5dd48717..1b65cef2c339 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/account.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/account.h @@ -57,7 +57,8 @@ class Account final : public PrefManagerObserver, void AddObserver(AccountObserver* observer); void RemoveObserver(AccountObserver* observer); - void SetWallet(const std::string& id, const std::string& seed); + void SetWallet(const std::string& payment_id, + const std::string& recovery_seed); const WalletInfo& GetWallet() const; void Deposit(const std::string& creative_instance_id, @@ -82,6 +83,7 @@ class Account final : public PrefManagerObserver, void ProcessClearingCycle() const; void ProcessUnclearedTransactions() const; + void WalletDidUpdate(const WalletInfo& wallet) const; void WalletDidChange(const WalletInfo& wallet) const; void MaybeResetIssuersAndConfirmations(); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/account_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/account_unittest.cc index a7e954d9fb96..523688fc49ba 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/account_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/account_unittest.cc @@ -18,6 +18,7 @@ #include "bat/ads/internal/account/transactions/transactions.h" #include "bat/ads/internal/account/transactions/transactions_unittest_util.h" #include "bat/ads/internal/account/wallet/wallet_info.h" +#include "bat/ads/internal/account/wallet/wallet_unittest_util.h" #include "bat/ads/internal/common/unittest/unittest_base.h" #include "bat/ads/internal/common/unittest/unittest_mock_util.h" #include "bat/ads/internal/common/unittest/unittest_time_util.h" @@ -38,15 +39,6 @@ using ::testing::_; using ::testing::NiceMock; using ::testing::Return; -namespace { - -constexpr char kWalletId[] = "27a39b2f-9b2e-4eb0-bbb2-2f84447496e7"; -constexpr char kWalletSeed[] = "x5uBvgI5MTTVY6sjGv65e9EHr8v7i+UxkFB9qVc5fP0="; -constexpr char kInvalidWalletSeed[] = - "y6vCwhJ6NUUWZ7tkHw76f0FIs9w8j-VylGC0rWd6gQ1="; - -} // namespace - class BatAdsAccountTest : public AccountObserver, public UnitTestBase { protected: void SetUp() override { @@ -108,7 +100,8 @@ TEST_F(BatAdsAccountTest, SetWallet) { // Arrange // Act - account_->SetWallet(kWalletId, kWalletSeed); + account_->SetWallet(GetWalletPaymentIdForTesting(), + GetWalletRecoverySeedForTesting()); // Assert EXPECT_TRUE(wallet_did_update_); @@ -120,7 +113,8 @@ TEST_F(BatAdsAccountTest, SetInvalidWallet) { // Arrange // Act - account_->SetWallet(kWalletId, kInvalidWalletSeed); + account_->SetWallet(GetWalletPaymentIdForTesting(), + GetInvalidWalletRecoverySeedForTesting()); // Assert EXPECT_FALSE(wallet_did_update_); @@ -130,10 +124,12 @@ TEST_F(BatAdsAccountTest, SetInvalidWallet) { TEST_F(BatAdsAccountTest, ChangeWallet) { // Arrange - account_->SetWallet(kWalletId, kWalletSeed); + account_->SetWallet(GetWalletPaymentIdForTesting(), + GetWalletRecoverySeedForTesting()); // Act - account_->SetWallet("c1bf0a09-cac8-48eb-8c21-7ca6d995b0a3", kWalletSeed); + account_->SetWallet(/*payment_id*/ "c1bf0a09-cac8-48eb-8c21-7ca6d995b0a3", + GetWalletRecoverySeedForTesting()); // Assert EXPECT_TRUE(wallet_did_update_); @@ -143,17 +139,19 @@ TEST_F(BatAdsAccountTest, ChangeWallet) { TEST_F(BatAdsAccountTest, GetWallet) { // Arrange - account_->SetWallet(kWalletId, kWalletSeed); + account_->SetWallet(GetWalletPaymentIdForTesting(), + GetWalletRecoverySeedForTesting()); // Act const WalletInfo& wallet = account_->GetWallet(); // Assert WalletInfo expected_wallet; - expected_wallet.id = "27a39b2f-9b2e-4eb0-bbb2-2f84447496e7"; + expected_wallet.payment_id = "27a39b2f-9b2e-4eb0-bbb2-2f84447496e7"; + expected_wallet.public_key = "BiG/i3tfNLSeOA9ZF5rkPCGyhkc7KCRbQS3bVGMvFQ0="; expected_wallet.secret_key = - "93052310477323AAE423A84BA32C68B1AE3B66B71952F6D8A69026E33BD817980621BF8B" - "7B5F34B49E380F59179AE43C21B286473B28245B412DDB54632F150D"; + "kwUjEEdzI6rkI6hLoyxosa47ZrcZUvbYppAm4zvYF5gGIb+" + "Le180tJ44D1kXmuQ8IbKGRzsoJFtBLdtUYy8VDQ=="; EXPECT_EQ(expected_wallet, wallet); } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/user_data/conversion_user_data_builder_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/user_data/conversion_user_data_builder_unittest.cc index 442003dcdc55..d4bbd3a4d6e8 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/user_data/conversion_user_data_builder_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/user_data/conversion_user_data_builder_unittest.cc @@ -42,7 +42,7 @@ TEST_F(BatAdsConversionUserDataBuilderTest, BuildConversion) { // Act BuildConversion(kCreativeInstanceId, [](base::Value::Dict user_data) { const absl::optional message = - security::OpenEvenlopeForUserDataAndAdvertiserSecretKey( + security::OpenEnvelopeForUserDataAndAdvertiserSecretKey( user_data, kAdvertiserSecretKey); ASSERT_TRUE(message); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/user_data/rotating_hash_user_data.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/user_data/rotating_hash_user_data.cc index e31a2fe46aa6..612a38a264f8 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/user_data/rotating_hash_user_data.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/user_data/rotating_hash_user_data.cc @@ -34,7 +34,7 @@ base::Value::Dict GetRotatingHash(const std::string& creative_instance_id) { const std::string timestamp = base::NumberToString(timestamp_rounded_to_nearest_hour); - const std::string rotating_hash = base::Base64Encode(security::Sha256( + const std::string rotating_hash = base::Base64Encode(crypto::Sha256( base::StrCat({device_id, creative_instance_id, timestamp}))); user_data.Set(kRotatingHashKey, rotating_hash); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_unittest.cc index f517c2c5e0f7..4e9652476b09 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_unittest.cc @@ -91,7 +91,7 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensTest, RedeemUnblindedPaymentTokens) { OnDidRetryRedeemingUnblindedPaymentTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet); FastForwardClockToNextPendingTask(); @@ -163,7 +163,7 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensTest, OnDidRetryRedeemingUnblindedPaymentTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet); redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet); @@ -217,7 +217,7 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensTest, ScheduleNextTokenRedemption) { OnDidRetryRedeemingUnblindedPaymentTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet); FastForwardClockToNextPendingTask(); @@ -325,7 +325,7 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensTest, NoUnblindedPaymentTokens) { OnDidRetryRedeemingUnblindedPaymentTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet); FastForwardClockToNextPendingTask(); @@ -379,7 +379,7 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensTest, Retry) { EXPECT_CALL(*redeem_unblinded_payment_tokens_delegate_mock_, OnDidScheduleNextUnblindedPaymentTokensRedemption(_)); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); redeem_unblinded_payment_tokens_->MaybeRedeemAfterDelay(wallet); FastForwardClockToNextPendingTask(); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_url_request_builder.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_url_request_builder.cc index 5770679f4ce9..6f1c827fef03 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_url_request_builder.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_url_request_builder.cc @@ -122,7 +122,7 @@ RedeemUnblindedPaymentTokensUrlRequestBuilder::Build() { GURL RedeemUnblindedPaymentTokensUrlRequestBuilder::BuildUrl() const { const std::string spec = base::StringPrintf( "%s/v3/confirmation/payment/%s", server::GetNonAnonymousHost().c_str(), - wallet_.id.c_str()); + wallet_.payment_id.c_str()); return GURL(spec); } @@ -146,7 +146,7 @@ std::string RedeemUnblindedPaymentTokensUrlRequestBuilder::BuildBody( std::string RedeemUnblindedPaymentTokensUrlRequestBuilder::CreatePayload() const { base::Value::Dict payload; - payload.Set("paymentId", wallet_.id); + payload.Set("paymentId", wallet_.payment_id); std::string json; CHECK(base::JSONWriter::Write(payload, &json)); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_url_request_builder_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_url_request_builder_unittest.cc index f7359662cd6c..59b9db9d8542 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_url_request_builder_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_url_request_builder_unittest.cc @@ -10,6 +10,7 @@ #include "base/check.h" #include "bat/ads/internal/account/utility/redeem_unblinded_payment_tokens/redeem_unblinded_payment_tokens_user_data_builder.h" #include "bat/ads/internal/account/wallet/wallet_info.h" +#include "bat/ads/internal/account/wallet/wallet_unittest_util.h" #include "bat/ads/internal/common/unittest/unittest_base.h" #include "bat/ads/internal/common/unittest/unittest_mock_util.h" #include "bat/ads/internal/flags/flag_manager.h" @@ -78,12 +79,6 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensUrlRequestBuilderTest, FlagManager::GetInstance()->SetEnvironmentTypeForTesting( EnvironmentType::kStaging); - WalletInfo wallet; - wallet.id = "d4ed0af0-bfa9-464b-abd7-67b29d891b8b"; - wallet.secret_key = - "e9b1ab4f44d39eb04323411eed0b5a2ceedff01264474f86e29c707a56615650" - "33cea0085cfd551faa170c1dd7f6daaa903cdd3138d61ed5ab2845e224d58144"; - const privacy::UnblindedPaymentTokenList unblinded_payment_tokens = GetUnblindedPaymentTokens(7); @@ -91,22 +86,22 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensUrlRequestBuilderTest, unblinded_payment_tokens); // Act - user_data_builder.Build([&wallet, &unblinded_payment_tokens]( + user_data_builder.Build([&unblinded_payment_tokens]( const base::Value::Dict& user_data) { RedeemUnblindedPaymentTokensUrlRequestBuilder url_request_builder( - wallet, unblinded_payment_tokens, user_data); + GetWalletForTesting(), unblinded_payment_tokens, user_data); const mojom::UrlRequestInfoPtr url_request = url_request_builder.Build(); mojom::UrlRequestInfoPtr expected_url_request = mojom::UrlRequestInfo::New(); expected_url_request->url = GURL( - R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/payment/d4ed0af0-bfa9-464b-abd7-67b29d891b8b)"); + R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/payment/27a39b2f-9b2e-4eb0-bbb2-2f84447496e7)"); expected_url_request->headers = { R"(Via: 1.1 brave, 1.1 ads-serve.brave.com (Apache/1.1))", R"(accept: application/json)"}; expected_url_request->content = - R"({"odyssey":"guest","payload":"{\"paymentId\":\"d4ed0af0-bfa9-464b-abd7-67b29d891b8b\"}","paymentCredentials":[{"confirmationType":"view","credential":{"signature":"wQXvy7chZlrrVCe/RYIiL/siGUFYF0tCxx7M0xIOPvThR4TCBwmH9IDWQKyqQy9g2wUw5jcKszqBHEhPyidrlA==","t":"PLowz2WF2eGD5zfwZjk9p76HXBLDKMq/3EAZHeG/fE2XGQ48jyte+Ve50ZlasOuYL5mwA8CU2aFMlJrt3DDgCw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"AemGBdoUXbp25pGZJuWv6yiImtfXC4AtboJMGR1Z6nQm178ier7hLJDVCJ11HWEO1UdlAYFRrJqyuD5uUBxgug==","t":"hfrMEltWLuzbKQ02Qixh5C/DWiJbdOoaGaidKZ7Mv+cRq5fyxJqemE/MPlARPhl6NgXPHUeyaxzd6/Lk6YHlfQ=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"krVZeadk/ElvsaYiUE4Ma/hkicRDjvS8O7QVkrWl0n2zsGYyAa/hodVb1aDn8tT3CMOV/l1JZdTVSXHrSHBHGg==","t":"bbpQ1DcxfDA+ycNg9WZvIwinjO0GKnCon1UFxDLoDOLZVnKG3ufruNZi/n8dO+G2AkTiWkUKbi78xCyKsqsXnA=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"1HwlWbhUewzvEWfGlOhmEo8x4FR3w82iRan+ZyBl1h3laOiXTVHXe5EraDiUd3G6bZlLJ+x9snDXPcd4wI5tpA==","t":"OlDIXpWRR1/B+1pjPbLyc5sx0V+d7QzQb4NDGUI6F676jy8tL++u57SF4DQhvdEpBrKID+j27RLrbjsecXSjRw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"c9wbOwh7xT3Fx89HKh6D4isUU8ki9vTq+1MR81bRyPWCv0lDHYchd7Kk9EFtz3qNip4nZpSDUDDqV5Gu3ac2DA==","t":"Y579V5BUcCzAFj6qNX7YnIr+DvH0mugb/nnY5UINdjxziyDJlejJwi0kPaRGmqbVT3+B51lpErt8e66z0jTbAw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"PW8G57q6/hoj0GzBoiRPilmPyWSYrFfOpJJ9I0tLsNfNF+DNOASnBoRpUy6nGJLX1vWcJnUQGGVr9hfwBNTGfg==","t":"+MPQfSo6UcaZNWtfmbd5je9UIr+FVrCWHl6I5C1ZFD7y7bjP/yz7flTjV+l5mKulbCvsRna7++MhbBz6iC0FvQ=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"Rn9mRKy6B0Sysx6+y3scWE+ZE6EWVA/pYTp1XqOLFZH3IVVh+WnIVP/FNA7GuexDmVaq8/an8+9Gv7puKpQPWA==","t":"CRXUzo7S0X//u0RGsO534vCoIbrsXgbzLfWw8CLML0CkgMltEGxM6XwBTICl4dqqfhIcLhD0f1WFod7JpuEkjw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="}],"platform":"windows","totals":[{"ad_format":"ad_notification","view":"7"}]})"; + R"({"odyssey":"guest","payload":"{\"paymentId\":\"27a39b2f-9b2e-4eb0-bbb2-2f84447496e7\"}","paymentCredentials":[{"confirmationType":"view","credential":{"signature":"H9HPNdEVJBvc9d8RZig/Gihlrcgug/n/rRaAJzeZI20gKPCivIj9Ig8StvqMSc5GfgLrBaJDibwBghnhRhqYRQ==","t":"PLowz2WF2eGD5zfwZjk9p76HXBLDKMq/3EAZHeG/fE2XGQ48jyte+Ve50ZlasOuYL5mwA8CU2aFMlJrt3DDgCw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"mfv+HJP5K/q9ogcGwD4uqOd98sb2fx96h+QnsdtGwJ4wdZfvrukbP4whyz46Ro3gm2FIMhPWZ5wM2Hhg9OGPtg==","t":"hfrMEltWLuzbKQ02Qixh5C/DWiJbdOoaGaidKZ7Mv+cRq5fyxJqemE/MPlARPhl6NgXPHUeyaxzd6/Lk6YHlfQ=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"acj3J7aaf/rN/uleCnaGTOzNceo9m9Dz/2D1+NDIVN4MnZG2BF1hYl5qHY/VRZNh5/uhnRbqgsuPn6SXvaTXVA==","t":"bbpQ1DcxfDA+ycNg9WZvIwinjO0GKnCon1UFxDLoDOLZVnKG3ufruNZi/n8dO+G2AkTiWkUKbi78xCyKsqsXnA=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"zETc7kPZQhzNaufsUiBHiBtnwEhk3oQYUh5HZeNbTOiHvm5pAzNl/TuR/fjQcusN5EqH7e1B+ycsO5CeF5FmAw==","t":"OlDIXpWRR1/B+1pjPbLyc5sx0V+d7QzQb4NDGUI6F676jy8tL++u57SF4DQhvdEpBrKID+j27RLrbjsecXSjRw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"d1oVe9XnwRs7bNcMzxp+/q7zL5hDHLD13f4tazz79a+ImszJfVRYIcf1fxvc7d2Pstciu1i11jRYGUWIDttYVg==","t":"Y579V5BUcCzAFj6qNX7YnIr+DvH0mugb/nnY5UINdjxziyDJlejJwi0kPaRGmqbVT3+B51lpErt8e66z0jTbAw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"rQkn3xdJu/GFfkGUREqvV69lQNQWsa5a9RQIXaig7CQtlS/PVLsUvM6plG4IeboK0E6gR8Do5Gg40OK59/ZyUQ==","t":"+MPQfSo6UcaZNWtfmbd5je9UIr+FVrCWHl6I5C1ZFD7y7bjP/yz7flTjV+l5mKulbCvsRna7++MhbBz6iC0FvQ=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"GohmRsNFp8Vd47PB5MzgFvM7o/8XjkY+9gjCOJY/MAcpmLBgoS6v373A0Tvkq3T/t0z9GYM0yrs6LikJggwnmg==","t":"CRXUzo7S0X//u0RGsO534vCoIbrsXgbzLfWw8CLML0CkgMltEGxM6XwBTICl4dqqfhIcLhD0f1WFod7JpuEkjw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="}],"platform":"windows","totals":[{"ad_format":"ad_notification","view":"7"}]})"; expected_url_request->content_type = "application/json"; expected_url_request->method = mojom::UrlRequestMethodType::kPut; @@ -126,12 +121,6 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensUrlRequestBuilderTest, FlagManager::GetInstance()->SetEnvironmentTypeForTesting( EnvironmentType::kStaging); - WalletInfo wallet; - wallet.id = "d4ed0af0-bfa9-464b-abd7-67b29d891b8b"; - wallet.secret_key = - "e9b1ab4f44d39eb04323411eed0b5a2ceedff01264474f86e29c707a56615650" - "33cea0085cfd551faa170c1dd7f6daaa903cdd3138d61ed5ab2845e224d58144"; - const privacy::UnblindedPaymentTokenList unblinded_payment_tokens = GetUnblindedPaymentTokens(7); @@ -139,22 +128,22 @@ TEST_F(BatAdsRedeemUnblindedPaymentTokensUrlRequestBuilderTest, unblinded_payment_tokens); // Act - user_data_builder.Build([&wallet, &unblinded_payment_tokens]( + user_data_builder.Build([&unblinded_payment_tokens]( const base::Value::Dict& user_data) { RedeemUnblindedPaymentTokensUrlRequestBuilder url_request_builder( - wallet, unblinded_payment_tokens, user_data); + GetWalletForTesting(), unblinded_payment_tokens, user_data); const mojom::UrlRequestInfoPtr url_request = url_request_builder.Build(); mojom::UrlRequestInfoPtr expected_url_request = mojom::UrlRequestInfo::New(); expected_url_request->url = GURL( - R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/payment/d4ed0af0-bfa9-464b-abd7-67b29d891b8b)"); + R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/payment/27a39b2f-9b2e-4eb0-bbb2-2f84447496e7)"); expected_url_request->headers = { R"(Via: 1.0 brave, 1.1 ads-serve.brave.com (Apache/1.1))", R"(accept: application/json)"}; expected_url_request->content = - R"({"odyssey":"host","payload":"{\"paymentId\":\"d4ed0af0-bfa9-464b-abd7-67b29d891b8b\"}","paymentCredentials":[{"confirmationType":"view","credential":{"signature":"wQXvy7chZlrrVCe/RYIiL/siGUFYF0tCxx7M0xIOPvThR4TCBwmH9IDWQKyqQy9g2wUw5jcKszqBHEhPyidrlA==","t":"PLowz2WF2eGD5zfwZjk9p76HXBLDKMq/3EAZHeG/fE2XGQ48jyte+Ve50ZlasOuYL5mwA8CU2aFMlJrt3DDgCw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"AemGBdoUXbp25pGZJuWv6yiImtfXC4AtboJMGR1Z6nQm178ier7hLJDVCJ11HWEO1UdlAYFRrJqyuD5uUBxgug==","t":"hfrMEltWLuzbKQ02Qixh5C/DWiJbdOoaGaidKZ7Mv+cRq5fyxJqemE/MPlARPhl6NgXPHUeyaxzd6/Lk6YHlfQ=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"krVZeadk/ElvsaYiUE4Ma/hkicRDjvS8O7QVkrWl0n2zsGYyAa/hodVb1aDn8tT3CMOV/l1JZdTVSXHrSHBHGg==","t":"bbpQ1DcxfDA+ycNg9WZvIwinjO0GKnCon1UFxDLoDOLZVnKG3ufruNZi/n8dO+G2AkTiWkUKbi78xCyKsqsXnA=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"1HwlWbhUewzvEWfGlOhmEo8x4FR3w82iRan+ZyBl1h3laOiXTVHXe5EraDiUd3G6bZlLJ+x9snDXPcd4wI5tpA==","t":"OlDIXpWRR1/B+1pjPbLyc5sx0V+d7QzQb4NDGUI6F676jy8tL++u57SF4DQhvdEpBrKID+j27RLrbjsecXSjRw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"c9wbOwh7xT3Fx89HKh6D4isUU8ki9vTq+1MR81bRyPWCv0lDHYchd7Kk9EFtz3qNip4nZpSDUDDqV5Gu3ac2DA==","t":"Y579V5BUcCzAFj6qNX7YnIr+DvH0mugb/nnY5UINdjxziyDJlejJwi0kPaRGmqbVT3+B51lpErt8e66z0jTbAw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"PW8G57q6/hoj0GzBoiRPilmPyWSYrFfOpJJ9I0tLsNfNF+DNOASnBoRpUy6nGJLX1vWcJnUQGGVr9hfwBNTGfg==","t":"+MPQfSo6UcaZNWtfmbd5je9UIr+FVrCWHl6I5C1ZFD7y7bjP/yz7flTjV+l5mKulbCvsRna7++MhbBz6iC0FvQ=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"Rn9mRKy6B0Sysx6+y3scWE+ZE6EWVA/pYTp1XqOLFZH3IVVh+WnIVP/FNA7GuexDmVaq8/an8+9Gv7puKpQPWA==","t":"CRXUzo7S0X//u0RGsO534vCoIbrsXgbzLfWw8CLML0CkgMltEGxM6XwBTICl4dqqfhIcLhD0f1WFod7JpuEkjw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="}],"platform":"windows","totals":[{"ad_format":"ad_notification","view":"7"}]})"; + R"({"odyssey":"host","payload":"{\"paymentId\":\"27a39b2f-9b2e-4eb0-bbb2-2f84447496e7\"}","paymentCredentials":[{"confirmationType":"view","credential":{"signature":"H9HPNdEVJBvc9d8RZig/Gihlrcgug/n/rRaAJzeZI20gKPCivIj9Ig8StvqMSc5GfgLrBaJDibwBghnhRhqYRQ==","t":"PLowz2WF2eGD5zfwZjk9p76HXBLDKMq/3EAZHeG/fE2XGQ48jyte+Ve50ZlasOuYL5mwA8CU2aFMlJrt3DDgCw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"mfv+HJP5K/q9ogcGwD4uqOd98sb2fx96h+QnsdtGwJ4wdZfvrukbP4whyz46Ro3gm2FIMhPWZ5wM2Hhg9OGPtg==","t":"hfrMEltWLuzbKQ02Qixh5C/DWiJbdOoaGaidKZ7Mv+cRq5fyxJqemE/MPlARPhl6NgXPHUeyaxzd6/Lk6YHlfQ=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"acj3J7aaf/rN/uleCnaGTOzNceo9m9Dz/2D1+NDIVN4MnZG2BF1hYl5qHY/VRZNh5/uhnRbqgsuPn6SXvaTXVA==","t":"bbpQ1DcxfDA+ycNg9WZvIwinjO0GKnCon1UFxDLoDOLZVnKG3ufruNZi/n8dO+G2AkTiWkUKbi78xCyKsqsXnA=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"zETc7kPZQhzNaufsUiBHiBtnwEhk3oQYUh5HZeNbTOiHvm5pAzNl/TuR/fjQcusN5EqH7e1B+ycsO5CeF5FmAw==","t":"OlDIXpWRR1/B+1pjPbLyc5sx0V+d7QzQb4NDGUI6F676jy8tL++u57SF4DQhvdEpBrKID+j27RLrbjsecXSjRw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"d1oVe9XnwRs7bNcMzxp+/q7zL5hDHLD13f4tazz79a+ImszJfVRYIcf1fxvc7d2Pstciu1i11jRYGUWIDttYVg==","t":"Y579V5BUcCzAFj6qNX7YnIr+DvH0mugb/nnY5UINdjxziyDJlejJwi0kPaRGmqbVT3+B51lpErt8e66z0jTbAw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"rQkn3xdJu/GFfkGUREqvV69lQNQWsa5a9RQIXaig7CQtlS/PVLsUvM6plG4IeboK0E6gR8Do5Gg40OK59/ZyUQ==","t":"+MPQfSo6UcaZNWtfmbd5je9UIr+FVrCWHl6I5C1ZFD7y7bjP/yz7flTjV+l5mKulbCvsRna7++MhbBz6iC0FvQ=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="},{"confirmationType":"view","credential":{"signature":"GohmRsNFp8Vd47PB5MzgFvM7o/8XjkY+9gjCOJY/MAcpmLBgoS6v373A0Tvkq3T/t0z9GYM0yrs6LikJggwnmg==","t":"CRXUzo7S0X//u0RGsO534vCoIbrsXgbzLfWw8CLML0CkgMltEGxM6XwBTICl4dqqfhIcLhD0f1WFod7JpuEkjw=="},"publicKey":"RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="}],"platform":"windows","totals":[{"ad_format":"ad_notification","view":"7"}]})"; expected_url_request->content_type = "application/json"; expected_url_request->method = mojom::UrlRequestMethodType::kPut; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder.cc index 70e57a99e93d..c0b96de96bf1 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder.cc @@ -39,7 +39,7 @@ GURL GetSignedTokensUrlRequestBuilder::BuildUrl() const { const std::string spec = base::StringPrintf("%s/v3/confirmation/token/%s?nonce=%s", server::GetNonAnonymousHost().c_str(), - wallet_.id.c_str(), nonce_.c_str()); + wallet_.payment_id.c_str(), nonce_.c_str()); return GURL(spec); } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder_unittest.cc index 4e6d59ddb271..2449265aa650 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder_unittest.cc @@ -6,6 +6,7 @@ #include "bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder.h" #include "bat/ads/internal/account/wallet/wallet_info.h" +#include "bat/ads/internal/account/wallet/wallet_unittest_util.h" #include "bat/ads/internal/common/unittest/unittest_base.h" #include "bat/ads/internal/flags/flag_manager.h" #include "url/gurl.h" @@ -21,15 +22,10 @@ TEST_F(BatAdsGetSignedTokensUrlRequestBuilderTest, BuildUrl) { FlagManager::GetInstance()->SetEnvironmentTypeForTesting( EnvironmentType::kStaging); - WalletInfo wallet; - wallet.id = "d4ed0af0-bfa9-464b-abd7-67b29d891b8b"; - wallet.secret_key = - "e9b1ab4f44d39eb04323411eed0b5a2ceedff01264474f86e29c707a56615650" - "33cea0085cfd551faa170c1dd7f6daaa903cdd3138d61ed5ab2845e224d58144"; - const std::string nonce = "716c3381-66e6-46e4-962f-15d01455b5b9"; - GetSignedTokensUrlRequestBuilder url_request_builder(wallet, nonce); + GetSignedTokensUrlRequestBuilder url_request_builder(GetWalletForTesting(), + nonce); // Act mojom::UrlRequestInfoPtr const url_request = url_request_builder.Build(); @@ -37,7 +33,7 @@ TEST_F(BatAdsGetSignedTokensUrlRequestBuilderTest, BuildUrl) { // Assert mojom::UrlRequestInfoPtr expected_url_request = mojom::UrlRequestInfo::New(); expected_url_request->url = GURL( - R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/token/d4ed0af0-bfa9-464b-abd7-67b29d891b8b?nonce=716c3381-66e6-46e4-962f-15d01455b5b9)"); + R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/token/27a39b2f-9b2e-4eb0-bbb2-2f84447496e7?nonce=716c3381-66e6-46e4-962f-15d01455b5b9)"); expected_url_request->method = mojom::UrlRequestMethodType::kGet; EXPECT_EQ(expected_url_request, url_request); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/refill_unblinded_tokens.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/refill_unblinded_tokens.cc index e1b2304d94b7..0f540d4d8fbf 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/refill_unblinded_tokens.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/refill_unblinded_tokens.cc @@ -19,6 +19,7 @@ #include "bat/ads/internal/account/utility/refill_unblinded_tokens/get_signed_tokens_url_request_builder.h" #include "bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder.h" #include "bat/ads/internal/ads_client_helper.h" +#include "bat/ads/internal/common/crypto/crypto_util.h" #include "bat/ads/internal/common/logging_util.h" #include "bat/ads/internal/common/net/http/http_status_code.h" #include "bat/ads/internal/common/time/time_formatting_util.h" @@ -327,8 +328,26 @@ void RefillUnblindedTokens::OnGetSignedTokens( for (const auto& batch_dleq_proof_unblinded_token : *batch_dleq_proof_unblinded_tokens) { privacy::UnblindedTokenInfo unblinded_token; + unblinded_token.value = batch_dleq_proof_unblinded_token; + unblinded_token.public_key = public_key; + + const absl::optional unblinded_token_base64 = + unblinded_token.value.EncodeBase64(); + if (!unblinded_token_base64) { + NOTREACHED(); + continue; + } + + const absl::optional signature = + crypto::Sign(*unblinded_token_base64, wallet_.secret_key); + if (!signature) { + NOTREACHED(); + continue; + } + unblinded_token.signature = *signature; + DCHECK(IsValid(unblinded_token)); unblinded_tokens.push_back(unblinded_token); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/refill_unblinded_tokens_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/refill_unblinded_tokens_unittest.cc index 6b0daa834677..1737fef89e69 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/refill_unblinded_tokens_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/refill_unblinded_tokens_unittest.cc @@ -169,7 +169,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, RefillUnblindedTokens) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -220,7 +220,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, RefillUnblindedTokensCaptchaRequired) { EXPECT_CALL(*refill_unblinded_tokens_delegate_mock_, OnCaptchaRequiredToRefillUnblindedTokens("captcha-id")); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -260,7 +260,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, IssuersPublicKeyMismatch) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -286,7 +286,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, InvalidIssuersFormat) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -412,7 +412,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, EXPECT_CALL(*refill_unblinded_tokens_delegate_mock_, OnDidRefillUnblindedTokens()); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); FastForwardClockToNextPendingTask(); @@ -449,7 +449,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, RequestSignedTokensMissingNonce) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -554,7 +554,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, EXPECT_CALL(*refill_unblinded_tokens_delegate_mock_, OnDidRefillUnblindedTokens()); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); FastForwardClockToNextPendingTask(); @@ -599,7 +599,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, GetSignedTokensInvalidResponse) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -698,7 +698,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, GetSignedTokensMissingPublicKey) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -797,7 +797,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, GetSignedTokensMissingBatchProofDleq) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -845,7 +845,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, GetSignedTokensMissingSignedTokens) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -945,7 +945,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, GetInvalidSignedTokens) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -975,7 +975,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, DoNotRefillIfAboveTheMinimumThreshold) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert @@ -1058,7 +1058,7 @@ TEST_F(BatAdsRefillUnblindedTokensTest, RefillIfBelowTheMinimumThreshold) { OnDidRetryRefillingUnblindedTokens()) .Times(0); - const WalletInfo wallet = GetWallet(); + const WalletInfo wallet = GetWalletForTesting(); refill_unblinded_tokens_->MaybeRefill(wallet); // Assert diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder.cc index 4459449b1b3f..6d1dd2e2e328 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder.cc @@ -10,6 +10,7 @@ #include "base/base64.h" #include "base/check.h" +#include "base/containers/flat_map.h" #include "base/json/json_writer.h" #include "base/strings/stringprintf.h" #include "base/values.h" @@ -26,7 +27,7 @@ namespace { std::string BuildDigestHeaderValue(const std::string& body) { DCHECK(!body.empty()); - const std::vector body_sha256 = security::Sha256(body); + const std::vector body_sha256 = crypto::Sha256(body); const std::string body_sha256_base64 = base::Base64Encode(body_sha256); return base::StringPrintf("SHA-256=%s", body_sha256_base64.c_str()); @@ -64,7 +65,7 @@ mojom::UrlRequestInfoPtr RequestSignedTokensUrlRequestBuilder::Build() { GURL RequestSignedTokensUrlRequestBuilder::BuildUrl() const { const std::string spec = base::StringPrintf( "%s/v3/confirmation/token/%s", server::GetNonAnonymousHost().c_str(), - wallet_.id.c_str()); + wallet_.payment_id.c_str()); return GURL(spec); } @@ -98,10 +99,33 @@ std::string RequestSignedTokensUrlRequestBuilder::BuildSignatureHeaderValue( const std::string& body) const { DCHECK(!body.empty()); - const std::string digest_header_value = BuildDigestHeaderValue(body); + const base::flat_map headers = { + {"digest", BuildDigestHeaderValue(body)}}; + + std::string concatenated_header; + std::string concatenated_message; + + unsigned int index = 0; + for (const auto& header : headers) { + if (index != 0) { + concatenated_header += " "; + concatenated_message += "\n"; + } + + concatenated_header += header.first; + concatenated_message += header.first + ": " + header.second; + + index++; + } + + const absl::optional signature_base64 = + crypto::Sign(concatenated_message, wallet_.secret_key); + if (!signature_base64) { + return {}; + } - return security::Sign({{"digest", digest_header_value}}, "primary", - wallet_.secret_key); + return R"(keyId="primary",algorithm="ed25519",headers=")" + + concatenated_header + R"(",signature=")" + *signature_base64 + R"(")"; } std::string RequestSignedTokensUrlRequestBuilder::BuildBody() const { diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder_unittest.cc index 20ec5a5452a3..84800c8bca0b 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder_unittest.cc @@ -6,6 +6,7 @@ #include "bat/ads/internal/account/utility/refill_unblinded_tokens/request_signed_tokens_url_request_builder.h" #include "base/check.h" +#include "bat/ads/internal/account/wallet/wallet_unittest_util.h" #include "bat/ads/internal/common/unittest/unittest_base.h" #include "bat/ads/internal/flags/flag_manager.h" #include "bat/ads/internal/privacy/challenge_bypass_ristretto/blinded_token_util.h" @@ -71,18 +72,12 @@ TEST_F(BatAdsRequestSignedTokensUrlRequestBuilderTest, BuildUrlForRPill) { FlagManager::GetInstance()->SetEnvironmentTypeForTesting( EnvironmentType::kStaging); - WalletInfo wallet; - wallet.id = "d4ed0af0-bfa9-464b-abd7-67b29d891b8b"; - wallet.secret_key = - "e9b1ab4f44d39eb04323411eed0b5a2ceedff01264474f86e29c707a56615650" - "33cea0085cfd551faa170c1dd7f6daaa903cdd3138d61ed5ab2845e224d58144"; - const std::vector tokens = GetTokens(3); const std::vector blinded_tokens = privacy::cbr::BlindTokens(tokens); - RequestSignedTokensUrlRequestBuilder url_request_builder(wallet, - blinded_tokens); + RequestSignedTokensUrlRequestBuilder url_request_builder( + GetWalletForTesting(), blinded_tokens); // Act mojom::UrlRequestInfoPtr const url_request = url_request_builder.Build(); @@ -90,10 +85,10 @@ TEST_F(BatAdsRequestSignedTokensUrlRequestBuilderTest, BuildUrlForRPill) { // Assert mojom::UrlRequestInfoPtr expected_url_request = mojom::UrlRequestInfo::New(); expected_url_request->url = GURL( - R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/token/d4ed0af0-bfa9-464b-abd7-67b29d891b8b)"); + R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/token/27a39b2f-9b2e-4eb0-bbb2-2f84447496e7)"); expected_url_request->headers = { R"(digest: SHA-256=Sxq6H/YDThn/m2RSXsTzewSzKfAuGLh09w7m59VBYwU=)", - R"(signature: keyId="primary",algorithm="ed25519",headers="digest",signature="zImEsG3U2K2jROcUOerWMgzA+LyEoDqqYcr9svpnaEDNOYLzGn67qiz+HIFlqSjzy6Q9RPdU+h3VaFrIspsfCQ==")", + R"(signature: keyId="primary",algorithm="ed25519",headers="digest",signature="tLMjZ1f52kBqbwJy0B0On2h82978eV8tf4oK/3UJyq4mQqCu5y2q6puaxoe969ENtwSPU292PvbTIFAZZzwaCA==")", R"(content-type: application/json)", R"(Via: 1.1 brave, 1.1 ads-serve.brave.com (Apache/1.1))", R"(accept: application/json)"}; @@ -112,18 +107,12 @@ TEST_F(BatAdsRequestSignedTokensUrlRequestBuilderTest, BuildUrlForBPill) { FlagManager::GetInstance()->SetEnvironmentTypeForTesting( EnvironmentType::kStaging); - WalletInfo wallet; - wallet.id = "d4ed0af0-bfa9-464b-abd7-67b29d891b8b"; - wallet.secret_key = - "e9b1ab4f44d39eb04323411eed0b5a2ceedff01264474f86e29c707a56615650" - "33cea0085cfd551faa170c1dd7f6daaa903cdd3138d61ed5ab2845e224d58144"; - const std::vector tokens = GetTokens(3); const std::vector blinded_tokens = privacy::cbr::BlindTokens(tokens); - RequestSignedTokensUrlRequestBuilder url_request_builder(wallet, - blinded_tokens); + RequestSignedTokensUrlRequestBuilder url_request_builder( + GetWalletForTesting(), blinded_tokens); // Act mojom::UrlRequestInfoPtr const url_request = url_request_builder.Build(); @@ -131,10 +120,10 @@ TEST_F(BatAdsRequestSignedTokensUrlRequestBuilderTest, BuildUrlForBPill) { // Assert mojom::UrlRequestInfoPtr expected_url_request = mojom::UrlRequestInfo::New(); expected_url_request->url = GURL( - R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/token/d4ed0af0-bfa9-464b-abd7-67b29d891b8b)"); + R"(https://mywallet.ads.bravesoftware.com/v3/confirmation/token/27a39b2f-9b2e-4eb0-bbb2-2f84447496e7)"); expected_url_request->headers = { R"(digest: SHA-256=Sxq6H/YDThn/m2RSXsTzewSzKfAuGLh09w7m59VBYwU=)", - R"(signature: keyId="primary",algorithm="ed25519",headers="digest",signature="zImEsG3U2K2jROcUOerWMgzA+LyEoDqqYcr9svpnaEDNOYLzGn67qiz+HIFlqSjzy6Q9RPdU+h3VaFrIspsfCQ==")", + R"(signature: keyId="primary",algorithm="ed25519",headers="digest",signature="tLMjZ1f52kBqbwJy0B0On2h82978eV8tf4oK/3UJyq4mQqCu5y2q6puaxoe969ENtwSPU292PvbTIFAZZzwaCA==")", R"(content-type: application/json)", R"(Via: 1.0 brave, 1.1 ads-serve.brave.com (Apache/1.1))", R"(accept: application/json)"}; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet.cc index ee210eb29c73..106c7dae3fdd 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet.cc @@ -5,24 +5,24 @@ #include "bat/ads/internal/account/wallet/wallet.h" -#include -#include - -#include "base/strings/string_number_conversions.h" +#include "base/base64.h" #include "bat/ads/internal/common/crypto/crypto_util.h" +#include "bat/ads/internal/common/crypto/key_pair_info.h" namespace ads { -bool Wallet::Set(const std::string& id, const std::string& seed) { - const std::vector secret_key = - security::GenerateSecretKeyFromSeed(seed); - if (secret_key.empty()) { +bool Wallet::Set(const std::string& payment_id, + const std::vector& recovery_seed) { + const absl::optional key_pair = + crypto::GenerateSignKeyPairFromSeed(recovery_seed); + if (!key_pair || !key_pair->IsValid()) { return false; } WalletInfo wallet; - wallet.id = id; - wallet.secret_key = base::HexEncode(secret_key.data(), secret_key.size()); + wallet.payment_id = payment_id; + wallet.public_key = base::Base64Encode(key_pair->public_key); + wallet.secret_key = base::Base64Encode(key_pair->secret_key); if (!wallet.IsValid()) { return false; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet.h b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet.h index e9fcb0bad6a1..f910a0278b4e 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet.h @@ -6,7 +6,9 @@ #ifndef BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_ACCOUNT_WALLET_WALLET_H_ #define BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_ACCOUNT_WALLET_WALLET_H_ +#include #include +#include #include "bat/ads/internal/account/wallet/wallet_info.h" @@ -14,7 +16,8 @@ namespace ads { class Wallet final { public: - bool Set(const std::string& id, const std::string& seed); + bool Set(const std::string& payment_id, + const std::vector& recovery_seed); const WalletInfo& Get() const; private: diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_info.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_info.cc index 3060503e30ee..e0f3baf67224 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_info.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_info.cc @@ -8,7 +8,7 @@ namespace ads { bool WalletInfo::IsValid() const { - return !id.empty() && !secret_key.empty(); + return !payment_id.empty() && !public_key.empty() && !secret_key.empty(); } bool WalletInfo::WasUpdated(const WalletInfo& other) const { @@ -20,7 +20,8 @@ bool WalletInfo::HasChanged(const WalletInfo& other) const { } bool WalletInfo::operator==(const WalletInfo& other) const { - return id == other.id && secret_key == other.secret_key; + return payment_id == other.payment_id && public_key == other.public_key && + secret_key == other.secret_key; } bool WalletInfo::operator!=(const WalletInfo& other) const { diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_info.h b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_info.h index 96517c3157f5..850fac85851c 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_info.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_info.h @@ -19,7 +19,8 @@ struct WalletInfo final { bool operator==(const WalletInfo& other) const; bool operator!=(const WalletInfo& other) const; - std::string id; + std::string payment_id; + std::string public_key; std::string secret_key; }; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest.cc index dc43b8a553aa..930362ed8ce2 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest.cc @@ -5,67 +5,37 @@ #include "bat/ads/internal/account/wallet/wallet.h" -#include "bat/ads/internal/common/unittest/unittest_base.h" +#include "absl/types/optional.h" +#include "base/base64.h" +#include "bat/ads/internal/account/wallet/wallet_unittest_util.h" +#include "testing/gtest/include/gtest/gtest.h" // npm run test -- brave_unit_tests --filter=BatAds* namespace ads { -namespace { - -constexpr char kId[] = "27a39b2f-9b2e-4eb0-bbb2-2f84447496e7"; -constexpr char kValidSeed[] = "x5uBvgI5MTTVY6sjGv65e9EHr8v7i+UxkFB9qVc5fP0="; -constexpr char kInvalidSeed[] = "y6vCwhJ6NUUWZ7tkHw76f0FIs9w8j-VylGC0rWd6gQ1="; - -} // namespace - -class BatAdsWalletTest : public UnitTestBase { - protected: - bool SetWallet(const std::string& id, const std::string& seed) { - return wallet_.Set(id, seed); - } - - WalletInfo GetWallet() const { return wallet_.Get(); } - - Wallet wallet_; -}; - -TEST_F(BatAdsWalletTest, SetWallet) { +TEST(BatAdsWalletTest, SetWallet) { // Arrange + Wallet wallet; - // Act - const bool success = SetWallet(kId, kValidSeed); - - // Assert - EXPECT_TRUE(success); -} - -TEST_F(BatAdsWalletTest, SetInvalidWallet) { - // Arrange + const absl::optional> raw_recovery_seed = + base::Base64Decode(GetWalletRecoverySeedForTesting()); + ASSERT_TRUE(raw_recovery_seed); // Act - const bool success = SetWallet(kId, kInvalidSeed); - - // Assert - EXPECT_FALSE(success); -} - -TEST_F(BatAdsWalletTest, GetWallet) { - // Arrange - const bool success = SetWallet(kId, kValidSeed); + const bool success = + wallet.Set(GetWalletPaymentIdForTesting(), *raw_recovery_seed); ASSERT_TRUE(success); - // Act - const WalletInfo& wallet = GetWallet(); - // Assert WalletInfo expected_wallet; - expected_wallet.id = "27a39b2f-9b2e-4eb0-bbb2-2f84447496e7"; + expected_wallet.payment_id = "27a39b2f-9b2e-4eb0-bbb2-2f84447496e7"; + expected_wallet.public_key = "BiG/i3tfNLSeOA9ZF5rkPCGyhkc7KCRbQS3bVGMvFQ0="; expected_wallet.secret_key = - "93052310477323AAE423A84BA32C68B1AE3B66B71952F6D8A69026E33BD817980621BF8B" - "7B5F34B49E380F59179AE43C21B286473B28245B412DDB54632F150D"; + "kwUjEEdzI6rkI6hLoyxosa47ZrcZUvbYppAm4zvYF5gGIb+" + "Le180tJ44D1kXmuQ8IbKGRzsoJFtBLdtUYy8VDQ=="; - EXPECT_EQ(expected_wallet, wallet); + EXPECT_EQ(expected_wallet, wallet.Get()); } } // namespace ads diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest_util.cc index 904b600980fc..1952ae3ee50f 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest_util.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest_util.cc @@ -5,6 +5,11 @@ #include "bat/ads/internal/account/wallet/wallet_unittest_util.h" +#include + +#include "absl/types/optional.h" +#include "base/base64.h" +#include "base/check.h" #include "bat/ads/internal/account/wallet/wallet.h" #include "bat/ads/internal/account/wallet/wallet_info.h" @@ -12,22 +17,33 @@ namespace ads { namespace { -constexpr char kId[] = "27a39b2f-9b2e-4eb0-bbb2-2f84447496e7"; -constexpr char kValidSeed[] = "x5uBvgI5MTTVY6sjGv65e9EHr8v7i+UxkFB9qVc5fP0="; -constexpr char kInvalidSeed[] = "y6vCwhJ6NUUWZ7tkHw76f0FIs9w8j-VylGC0rWd6gQ1="; +constexpr char kPaymentId[] = "27a39b2f-9b2e-4eb0-bbb2-2f84447496e7"; +constexpr char kValidRecoverySeed[] = + "x5uBvgI5MTTVY6sjGv65e9EHr8v7i+UxkFB9qVc5fP0="; +constexpr char kInvalidRecoverySeed[] = + "y6vCwhJ6NUUWZ7tkHw76f0FIs9w8j-VylGC0rWd6gQ1="; } // namespace -WalletInfo GetWallet() { - Wallet wallet; - wallet.Set(kId, kValidSeed); - return wallet.Get(); +std::string GetWalletPaymentIdForTesting() { + return kPaymentId; } -WalletInfo GetInvalidWallet() { - Wallet wallet; - wallet.Set(kId, kInvalidSeed); +std::string GetWalletRecoverySeedForTesting() { + return kValidRecoverySeed; +} +std::string GetInvalidWalletRecoverySeedForTesting() { + return kInvalidRecoverySeed; +} + +WalletInfo GetWalletForTesting() { + const absl::optional> raw_recovery_seed = + base::Base64Decode(kValidRecoverySeed); + CHECK(raw_recovery_seed); + + Wallet wallet; + wallet.Set(kPaymentId, *raw_recovery_seed); return wallet.Get(); } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest_util.h b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest_util.h index 48e8f6473b47..91d486671812 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest_util.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/account/wallet/wallet_unittest_util.h @@ -6,12 +6,17 @@ #ifndef BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_ACCOUNT_WALLET_WALLET_UNITTEST_UTIL_H_ #define BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_ACCOUNT_WALLET_WALLET_UNITTEST_UTIL_H_ +#include + namespace ads { struct WalletInfo; -WalletInfo GetWallet(); -WalletInfo GetInvalidWallet(); +std::string GetWalletPaymentIdForTesting(); +std::string GetWalletRecoverySeedForTesting(); +std::string GetInvalidWalletRecoverySeedForTesting(); + +WalletInfo GetWalletForTesting(); } // namespace ads diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc index c24afce5c596..85429de0008c 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc @@ -276,8 +276,8 @@ void AdsImpl::OnDidCloseTab(const int32_t tab_id) { } void AdsImpl::OnRewardsWalletDidChange(const std::string& payment_id, - const std::string& seed) { - account_->SetWallet(payment_id, seed); + const std::string& recovery_seed) { + account_->SetWallet(payment_id, recovery_seed); } void AdsImpl::OnDidUpdateResourceComponent(const std::string& id) { @@ -520,6 +520,7 @@ void AdsImpl::OnMigrateConfirmationState(InitializeCallback callback, } ConfirmationStateManager::GetInstance()->Initialize( + account_->GetWallet(), base::BindOnce(&AdsImpl::OnLoadConfirmationState, base::Unretained(this), std::move(callback))); } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h index 4b09a3cb8504..93941653512c 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h @@ -140,7 +140,7 @@ class AdsImpl final : public Ads, void OnDidCloseTab(int32_t tab_id) override; void OnRewardsWalletDidChange(const std::string& payment_id, - const std::string& seed) override; + const std::string& recovery_seed) override; void GetStatementOfAccounts(GetStatementOfAccountsCallback callback) override; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.cc deleted file mode 100644 index d2649c9b1304..000000000000 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.cc +++ /dev/null @@ -1,31 +0,0 @@ -/* Copyright (c) 2022 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 https://mozilla.org/MPL/2.0/. */ - -#include "bat/ads/internal/common/crypto/crypto_unittest_util.h" - -#include - -#include "base/ranges/algorithm.h" -#include "tweetnacl.h" // NOLINT - -namespace ads::security { - -std::vector Decrypt(const std::vector& ciphertext, - const std::vector& nonce, - const std::vector& ephemeral_public_key, - const std::vector& secret_key) { - std::vector padded_plaintext(ciphertext.size()); - crypto_box_open(&padded_plaintext.front(), &ciphertext.front(), - ciphertext.size(), &nonce.front(), - &ephemeral_public_key.front(), &secret_key.front()); - - std::vector plaintext; - base::ranges::copy_n(padded_plaintext.cbegin() + crypto_box_ZEROBYTES, - padded_plaintext.size() - crypto_box_ZEROBYTES, - std::back_inserter(plaintext)); - return plaintext; -} - -} // namespace ads::security diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.h b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.h deleted file mode 100644 index 213e42b0ef87..000000000000 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_unittest_util.h +++ /dev/null @@ -1,21 +0,0 @@ -/* Copyright (c) 2022 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 https://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_COMMON_CRYPTO_CRYPTO_UNITTEST_UTIL_H_ -#define BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_COMMON_CRYPTO_CRYPTO_UNITTEST_UTIL_H_ - -#include -#include - -namespace ads::security { - -std::vector Decrypt(const std::vector& ciphertext, - const std::vector& nonce, - const std::vector& ephemeral_public_key, - const std::vector& secret_key); - -} // namespace ads::security - -#endif // BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_COMMON_CRYPTO_CRYPTO_UNITTEST_UTIL_H_ diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util.cc index f5b6542c3e05..6bee7c9da609 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util.cc @@ -8,15 +8,16 @@ #include #include #include +#include #include "base/base64.h" -#include "base/rand_util.h" #include "base/ranges/algorithm.h" -#include "base/strings/string_number_conversions.h" #include "bat/ads/internal/common/crypto/key_pair_info.h" +#include "crypto/random.h" +#include "third_party/boringssl/src/include/openssl/curve25519.h" #include "tweetnacl.h" // NOLINT -namespace ads::security { +namespace ads::crypto { namespace { @@ -29,41 +30,32 @@ constexpr uint8_t kHKDFSalt[] = { 246, 105, 20, 215, 5, 248, 154, 179, 191, 46, 17, 6, 72, 210, 91, 10, 169, 145, 248, 22, 147, 117, 24, 105, 12}; -std::vector GetHKDF(const std::string& secret) { - if (secret.empty()) { - return {}; - } - - std::vector raw_secret; - raw_secret.assign(secret.cbegin(), secret.cend()); +absl::optional> GetHKDF( + const std::vector& secret) { + DCHECK(!secret.empty()); std::vector derived_key(kHKDFSeedLength); - const uint8_t info[] = {0}; - - const int result = - HKDF(&derived_key.front(), kHKDFSeedLength, EVP_sha512(), - &raw_secret.front(), raw_secret.size(), kHKDFSalt, - std::size(kHKDFSalt), info, sizeof(info) / sizeof(info[0])); + constexpr uint8_t kInfo[] = {0}; - if (result == 0) { - return {}; + if (HKDF(derived_key.data(), kHKDFSeedLength, EVP_sha512(), secret.data(), + secret.size(), kHKDFSalt, std::size(kHKDFSalt), kInfo, + sizeof(kInfo) / sizeof(kInfo[0])) == 0) { + return absl::nullopt; } return derived_key; } -KeyPairInfo GenerateSignKeyPairFromSeed(const std::vector& seed) { - KeyPairInfo key_pair; - if (seed.empty()) { - return key_pair; - } +KeyPairInfo GenerateSignKeyPairFromSecret(const std::vector& secret) { + DCHECK(!secret.empty()); - std::vector secret_key = seed; + std::vector secret_key = secret; secret_key.resize(crypto_sign_SECRETKEYBYTES); std::vector public_key(crypto_sign_PUBLICKEYBYTES); - crypto_sign_keypair(&public_key.front(), &secret_key.front(), 1); + crypto_sign_keypair(public_key.data(), secret_key.data(), /*seeded*/ 1); + KeyPairInfo key_pair; key_pair.public_key = public_key; key_pair.secret_key = secret_key; @@ -72,69 +64,29 @@ KeyPairInfo GenerateSignKeyPairFromSeed(const std::vector& seed) { } // namespace -std::string Sign(const base::flat_map& headers, - const std::string& key_id, - const std::string& secret_key) { - if (headers.empty() || key_id.empty() || secret_key.empty()) { - return {}; - } - - std::string concatenated_header; - std::string concatenated_message; - - unsigned int index = 0; - for (const auto& header : headers) { - if (index != 0) { - concatenated_header += " "; - concatenated_message += "\n"; - } - - concatenated_header += header.first; - concatenated_message += header.first + ": " + header.second; - - index++; - } - - std::vector signed_message(crypto_sign_BYTES + - concatenated_message.length()); - - std::vector raw_secret_key; - if (!base::HexStringToBytes(secret_key, &raw_secret_key)) { - return {}; - } - - // Resolving the following linter/presubmit error breaks the build on Windows. - unsigned long long signed_message_length = 0; // NOLINT - crypto_sign( - &signed_message.front(), &signed_message_length, - reinterpret_cast(concatenated_message.c_str()), - concatenated_message.length(), &raw_secret_key.front()); - - std::vector signature(crypto_sign_BYTES); - base::ranges::copy_n(signed_message.cbegin(), crypto_sign_BYTES, - signature.begin()); - - return "keyId=\"" + key_id + "\",algorithm=\"" + crypto_sign_PRIMITIVE + - "\",headers=\"" + concatenated_header + "\",signature=\"" + - base::Base64Encode(signature) + "\""; +std::vector Sha256(const std::string& value) { + std::vector hash(SHA256_DIGEST_LENGTH); + SHA256(reinterpret_cast(value.data()), value.length(), + hash.data()); + return hash; } -std::vector Sha256(const std::string& value) { - if (value.empty()) { - return {}; - } +absl::optional GenerateSignKeyPairFromSeed( + const std::vector& seed) { + DCHECK(!seed.empty()); - std::vector sha256(SHA256_DIGEST_LENGTH); - SHA256(reinterpret_cast(value.c_str()), value.length(), - &sha256.front()); + const absl::optional> derived_key = GetHKDF(seed); + if (!derived_key) { + return absl::nullopt; + } - return sha256; + return GenerateSignKeyPairFromSecret(*derived_key); } KeyPairInfo GenerateBoxKeyPair() { std::vector public_key(crypto_box_PUBLICKEYBYTES); std::vector secret_key(crypto_box_SECRETKEYBYTES); - crypto_box_keypair(&public_key.front(), &secret_key.front()); + crypto_box_keypair(public_key.data(), secret_key.data()); KeyPairInfo key_pair; key_pair.public_key = public_key; @@ -143,40 +95,81 @@ KeyPairInfo GenerateBoxKeyPair() { return key_pair; } -std::vector GenerateSecretKeyFromSeed(const std::string& seed_base64) { - std::string seed; - if (!base::Base64Decode(seed_base64, &seed)) { - return {}; +std::vector GenerateRandomNonce() { + std::vector nonce(crypto_box_NONCEBYTES); + ::crypto::RandBytes(nonce); + return nonce; +} + +absl::optional Sign(const std::string& message, + const std::string& secret_key) { + const absl::optional> raw_secret_key = + base::Base64Decode(secret_key); + if (!raw_secret_key) { + return absl::nullopt; } - const std::vector derived_key = GetHKDF(seed); - const KeyPairInfo key_pair = GenerateSignKeyPairFromSeed(derived_key); - return key_pair.secret_key; + const std::vector raw_message(message.begin(), message.end()); + + std::vector raw_signature; + raw_signature.resize(ED25519_SIGNATURE_LEN); + if (ED25519_sign(raw_signature.data(), raw_message.data(), raw_message.size(), + raw_secret_key->data()) == 0) { + return absl::nullopt; + } + + return base::Base64Encode(raw_signature); } -// Because NaCL uses a 192bit nonce, there is enough entropy to ensure -// uniqueness if generated at random. -std::vector GenerateRandom192BitNonce() { - std::vector nonce(crypto_box_NONCEBYTES); - base::RandBytes(&nonce.front(), nonce.size()); +bool Verify(const std::string& message, + const std::string& public_key, + const std::string& signature) { + const absl::optional> raw_public_key = + base::Base64Decode(public_key); + if (!raw_public_key) { + return false; + } - return nonce; + const absl::optional> raw_signature = + base::Base64Decode(signature); + if (!raw_signature) { + return false; + } + + const std::vector raw_message(message.begin(), message.end()); + + return ED25519_verify(raw_message.data(), raw_message.size(), + raw_signature->data(), raw_public_key->data()) != 0; } std::vector Encrypt(const std::vector& plaintext, const std::vector& nonce, const std::vector& public_key, - const std::vector& ephemeral_secret_key) { - // API requires 32 leading zero-padded bytes + const std::vector& secret_key) { std::vector padded_plaintext = plaintext; padded_plaintext.insert(padded_plaintext.cbegin(), crypto_box_ZEROBYTES, 0); std::vector ciphertext(padded_plaintext.size()); - crypto_box(&ciphertext.front(), &padded_plaintext.front(), - padded_plaintext.size(), &nonce.front(), &public_key.front(), - &ephemeral_secret_key.front()); + crypto_box(ciphertext.data(), padded_plaintext.data(), + padded_plaintext.size(), nonce.data(), public_key.data(), + secret_key.data()); return ciphertext; } -} // namespace ads::security +std::vector Decrypt(const std::vector& ciphertext, + const std::vector& nonce, + const std::vector& public_key, + const std::vector& secret_key) { + std::vector padded_plaintext(ciphertext.size()); + crypto_box_open(padded_plaintext.data(), ciphertext.data(), ciphertext.size(), + nonce.data(), public_key.data(), secret_key.data()); + + std::vector plaintext; + base::ranges::copy_n(padded_plaintext.cbegin() + crypto_box_ZEROBYTES, + padded_plaintext.size() - crypto_box_ZEROBYTES, + std::back_inserter(plaintext)); + return plaintext; +} + +} // namespace ads::crypto diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util.h b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util.h index 5d1689686106..d1fa22e47472 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util.h @@ -10,29 +10,35 @@ #include #include -#include "base/containers/flat_map.h" +#include "absl/types/optional.h" -namespace ads::security { +namespace ads::crypto { struct KeyPairInfo; -std::string Sign(const base::flat_map& headers, - const std::string& key_id, - const std::string& secret_key); - std::vector Sha256(const std::string& value); +absl::optional GenerateSignKeyPairFromSeed( + const std::vector& seed); KeyPairInfo GenerateBoxKeyPair(); -std::vector GenerateSecretKeyFromSeed(const std::string& seed_base64); +std::vector GenerateRandomNonce(); -std::vector GenerateRandom192BitNonce(); +absl::optional Sign(const std::string& message, + const std::string& secret_key); +bool Verify(const std::string& message, + const std::string& public_key, + const std::string& signature); std::vector Encrypt(const std::vector& plaintext, const std::vector& nonce, const std::vector& public_key, - const std::vector& ephemeral_secret_key); + const std::vector& secret_key); +std::vector Decrypt(const std::vector& ciphertext, + const std::vector& nonce, + const std::vector& public_key, + const std::vector& secret_key); -} // namespace ads::security +} // namespace ads::crypto #endif // BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_COMMON_CRYPTO_CRYPTO_UTIL_H_ diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util_unittest.cc index cd18ae2d3bb4..f661bbf7ed97 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/crypto_util_unittest.cc @@ -6,214 +6,125 @@ #include "bat/ads/internal/common/crypto/crypto_util.h" #include "base/base64.h" -#include "bat/ads/internal/common/crypto/crypto_unittest_util.h" #include "bat/ads/internal/common/crypto/key_pair_info.h" #include "testing/gtest/include/gtest/gtest.h" // IWYU pragma: keep #include "tweetnacl.h" // NOLINT // npm run test -- brave_unit_tests --filter=BatAds* -namespace ads::security { +namespace ads::crypto { namespace { -constexpr size_t kCryptoBoxPublicKeyBytes = crypto_box_PUBLICKEYBYTES; -constexpr size_t kCryptoBoxSecretKeyBytes = crypto_box_SECRETKEYBYTES; +constexpr char kMessage[] = "The quick brown fox jumps over the lazy dog"; +constexpr char kPublicKey[] = "5LmgyD6OG0qcVeRgTzk3IWbzSWjemE4KpjTRtRW4eRk="; +constexpr char kSecretKey[] = + "oyd1rHNB5xHU6TzPSO/MUUfUJNHiol1ExFHMMKV/" + "7dvkuaDIPo4bSpxV5GBPOTchZvNJaN6YTgqmNNG1Fbh5GQ=="; } // namespace -TEST(BatAdsCryptoUtilTest, Sign) { - // Arrange - const base::flat_map headers = { - {"digest", "SHA-256=qj7EBzMRSsGh4Rfu8Zha6MvPB2WftfJNeF8gt7hE9AY="}}; - - const std::string key_id = "primary"; - - const std::string secret_key = - "e9b1ab4f44d39eb04323411eed0b5a2ceedff01264474f86e29c707a56615650" - "33cea0085cfd551faa170c1dd7f6daaa903cdd3138d61ed5ab2845e224d58144"; - - // Act - const std::string signature = security::Sign(headers, key_id, secret_key); - - // Assert - const std::string expected_signature = - R"(keyId="primary",)" - R"(algorithm="ed25519",)" - R"(headers="digest",)" - R"(signature="m5CxS9uqI7DbZ5UDo51bcLRP2awqcUSU8tfc4t/ysr)" - R"(H47B8OJUG1roQyi6/pjSZj9VJuj296v77c/lxBlCn2DA==")"; - - EXPECT_EQ(expected_signature, signature); -} - -TEST(BatAdsCryptoUtilTest, SignWithInvalidheaders) { +TEST(BatAdsCryptoUtilTest, Sha256) { // Arrange - const base::flat_map headers; - - const std::string key_id = "primary"; - - const std::string secret_key = - "e9b1ab4f44d39eb04323411eed0b5a2ceedff01264474f86e29c707a56615650" - "33cea0085cfd551faa170c1dd7f6daaa903cdd3138d61ed5ab2845e224d58144"; // Act - const std::string signature = security::Sign(headers, key_id, secret_key); + const std::vector sha256 = Sha256(kMessage); // Assert - const std::string expected_signature; + const std::string expected_sha256_base64 = + "16j7swfXgJRpypq8sAguT41WUeRtPNt2LQLQvzfJ5ZI="; - EXPECT_EQ(expected_signature, signature); + EXPECT_EQ(expected_sha256_base64, base::Base64Encode(sha256)); } -TEST(BatAdsCryptoUtilTest, SignWithInvalidKeyId) { +TEST(BatAdsCryptoUtilTest, Sha256WithEmptyString) { // Arrange - const base::flat_map headers = { - {"digest", "SHA-256=qj7EBzMRSsGh4Rfu8Zha6MvPB2WftfJNeF8gt7hE9AY="}}; - - const std::string key_id; - - const std::string secret_key = - "e9b1ab4f44d39eb04323411eed0b5a2ceedff01264474f86e29c707a56615650" - "33cea0085cfd551faa170c1dd7f6daaa903cdd3138d61ed5ab2845e224d58144"; + const std::string value; // Act - const std::string signature = security::Sign(headers, key_id, secret_key); + const std::vector sha256 = Sha256(value); // Assert - const std::string expected_signature; - - EXPECT_EQ(expected_signature, signature); + const std::string expected_sha256_base64 = + "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="; + EXPECT_EQ(expected_sha256_base64, base::Base64Encode(sha256)); } -TEST(BatAdsCryptoUtilTest, SignWithInvalidSecretKey) { +TEST(BatAdsCryptoUtilTest, GenerateSignKeyPairFromSeed) { // Arrange - const base::flat_map headers = { - {"digest", "SHA-256=qj7EBzMRSsGh4Rfu8Zha6MvPB2WftfJNeF8gt7hE9AY="}}; - - const std::string key_id = "primary"; - - const std::string secret_key; + const absl::optional> seed = + base::Base64Decode("x5uBvgI5MTTVY6sjGv65e9EHr8v7i+UxkFB9qVc5fP0="); + ASSERT_TRUE(seed); // Act - const std::string signature = security::Sign(headers, key_id, secret_key); + const absl::optional key_pair = + GenerateSignKeyPairFromSeed(*seed); + ASSERT_TRUE(key_pair); // Assert - const std::string expected_signature; - - EXPECT_EQ(expected_signature, signature); + ASSERT_EQ(crypto_sign_ed25519_PUBLICKEYBYTES, + static_cast(key_pair->public_key.size())); + ASSERT_EQ(crypto_sign_ed25519_SECRETKEYBYTES, + static_cast(key_pair->secret_key.size())); + EXPECT_TRUE(key_pair->IsValid()); } -TEST(BatAdsCryptoUtilTest, Sha256) { +TEST(BatAdsCryptoUtilTest, GenerateBoxKeyPair) { // Arrange - const std::string value = R"( - { - "blindedTokens" : [ - "iiafV6PGoG+Xz6QR+k1WaYllcA+w0a1jcDqhbpFbvWw=", - "8g7v9CDoZuOjnABr8SYUJmCIRHlwkFpFBB6rLfEJlz0=", - "chNIADY97/IiLfWrE/P5T3p3SQIPZAc4fKkB8/4byHE=", - "4nW47xQoQB4+uEz3i6/sbb+FDozpdiOTG53E+4RJ9kI=", - "KO9qa7ZuGosA2xjM2+t3rn7/7Oljga6Ak1fgixjtp2U=", - "tIBcIB2Xvmx0S+2jwcYrnzPvf20GTconlWDSiWHqR3g=", - "aHtan+UcZF0II/SRoYm7bK27VJWDabNKjXKSVaoPPTY=", - "6jggPJK8NL1AedlRpJSrCC3+reG2BMGqHOmIPtAsmwA=", - "7ClK9P723ff+dOZxOZ0jSonmI5AHqsQU2Cn8FVAHID4=", - "zkm+vIFM0ko74m+XhnZirCh7YUc9ucDtQTC+kwhWvzQ=", - "+uoLhdsMEg42PRYiLs0lrAiGcmsPWX2D6hxmrcLUgC8=", - "GNE2ISRb52HSPq0maJ9YXmbbkzUpo5dSNIM9I1eD+F4=", - "iBx49OAb3LWQzKko8ZeVVAkwdSKRbDHViqR6ciBICCw=", - "IBC208b0z56kzjG2Z/iTwriZfMp2cqoQgk4vyJAKJy8=", - "Vq4l6jx8vSCmvTVFMg3Wz04Xz/oomFq4QRt26vRhDWg=", - "5KIAJPFrSrVW92FJXP7WmHLc7d5a4lfTrXTRKC9rYQg=", - "/s/SELS2gTDt1Rt7XaJ54RaGLQUL85cLpKW2mBLU2HU=", - "HkJpt3NbymO56XbB2Tj4S4xyIKSjltFTjn1QdC1rLnM=", - "/CQIGwgHAX2kFmaJ+65YtAbO4eSfUvMojVxZLq/p/AE=", - "8N33oYwImtxf9rbrAQ1v8VlRD4iHDVR11yhYCKKKGFs=", - "6EjTK0lYDGwFPrtMyTjiYIPV4OK7beMBTV6qrgFCwDw=", - "5LzZynN+sxbIfQKc92V3dC82x4e99oxChk7fFNvJHmM=", - "uEW1D0SU8VU5UGPOnkrCv3I+NFNa1fNPSjDy4gjvIm0=", - "aIEvt2dBwTp1vuxNYjLaP25YdV3FjCG23NDxZG+MXxg=", - "DIhrKTcba0NNoEKQAsSb1t9R3KVrkwX8fpLlOOLcMkI=", - "vNaRbm7RPEkFvNNdLKaNhyd7gkM+kNt23G0N4sLnLhU=", - "4MXZ/1hM6+xVzyYWY14tjIxCaisfrTgAUD3LLJHSd14=", - "6hsMVd3VIjKUhHmHQRQRKr7duSiKzL36b/J+Mc4DPHM=", - "OCe1Vv0l86izNn1PHw+yLw5e37J/Ab3oVyTPgFlS4Wc=", - "hu5fi5YMxsWfmK3uTspjcjwguBDeiYMGuV+vIzC8jlg=", - "Vs+EZRjtF+xUC3sYUZsvpND8ugLPz6Yl0jCcv4HO2Co=", - "7Pxgek1VUU+93o6PWUdKgQW7IkDmLsotSEg8H7xj93U=", - "avRL8coOl6cWJxKlvY9mHfw1FWIF14JnhNdxW00fqAM=", - "Vvo4hscwrZgOIuwkgUaxzyrcGQbUS1vCWcNgjEkhfUg=", - "ChsgA1m1hmWFt3r6xQqNCZVqx/tMMzEdpy++uccB3Cs=", - "MImbGYf4TyE9WW/jx381Spk0B9boASAyehwz1om9Ong=", - "ksPN5jCF2uN8d1io+xXVJhJXZs/DpQsPsoCZl8L9EgA=", - "4AApGEJLMC3rgYgUABQp9nTXeikDmS29a2wkUOXIQXU=", - "JOcObac9kXq8eD0aIU5S5DKWiA/Ggf4tBC58KD2xtRs=", - "CBHMKoOwelZhfmupH1bH5Yo6BxDSkT8G2Jfk4xKsgyU=", - "Al/1AAI4W68MEk6+Ay0xIGjxzvlX6IdnPV9KgO1RU0c=", - "MtKvUJzIOOvOw8y+XzBbUrgyPxvE/DID2qvB3VsmVEs=", - "oIaCqLv0kIG9BDZz5u0xj0/ZQqZQMCn7gkgIHVioSFc=", - "8N1j1xiNm8dY90J9HQaeKyG861i2AN0w9nkF4cieZzw=", - "wDMa7tUhloYanmLOivcgHyjCLr/OMaKtWdqbhadEmRM=", - "bCquxc5v8J/P2pqay5fpzcLkTqSVvwdZrAbbIOF8Lhs=", - "ODPBJiCcOMv48YS9QIcD0dH4bsfD2zQVsWkwBef1ci4=", - "eA9Yt1HOkDNvDT6+kq0093d7WI/L78/Gj9nAlmSYwzE=", - "wqt3REJpnoxOCSdHcJEiOsdBWb5yQD5jaTahFz40Tkc=", - "tLdemf03DyE7OkTS8QCZS8OT0JflCVO1CmCbA8i2SXI=" - ] - } - )"; // Act - const std::vector sha256 = security::Sha256(value); - const std::string sha256_base64 = base::Base64Encode(sha256); + const KeyPairInfo key_pair = GenerateBoxKeyPair(); // Assert - const std::string expected_sha256_base64 = - "CLhef3V+rztnAAf7WyUhoyfhRbD2MXKhgtpzivvJJC0="; - - EXPECT_EQ(expected_sha256_base64, sha256_base64); + ASSERT_EQ(crypto_box_PUBLICKEYBYTES, + static_cast(key_pair.public_key.size())); + ASSERT_EQ(crypto_box_SECRETKEYBYTES, + static_cast(key_pair.secret_key.size())); + EXPECT_TRUE(key_pair.IsValid()); } -TEST(BatAdsCryptoUtilTest, Sha256WithEmptyString) { +TEST(BatAdsCryptoUtilTest, GenerateRandomNonce) { // Arrange - const std::string value; // Act - const std::vector sha256 = security::Sha256(value); + const std::vector nonce = GenerateRandomNonce(); // Assert - EXPECT_TRUE(sha256.empty()); + EXPECT_EQ(crypto_box_NONCEBYTES, static_cast(nonce.size())); } -TEST(BatAdsCryptoUtilTest, GenerateBoxKeyPair) { +TEST(BatAdsCryptoUtilTest, Sign) { // Arrange // Act - const KeyPairInfo key_pair = GenerateBoxKeyPair(); + const absl::optional signature = Sign(kMessage, kSecretKey); + ASSERT_TRUE(signature); // Assert - ASSERT_EQ(kCryptoBoxPublicKeyBytes, key_pair.public_key.size()); - ASSERT_EQ(kCryptoBoxSecretKeyBytes, key_pair.secret_key.size()); - EXPECT_TRUE(key_pair.IsValid()); + const std::string expected_signature = + "t4VwMNwX7hsAHQVXNGl3nGWj6LtCYSacEN/J0xKtXK6sQ5uBRB3m9kE6mVPHj6/" + "cv90OIdvrVcrl+eZm60FbAQ=="; + EXPECT_EQ(expected_signature, *signature); + EXPECT_TRUE(Verify(kMessage, kPublicKey, *signature)); } TEST(BatAdsCryptoUtilTest, Encrypt) { // Arrange const KeyPairInfo key_pair = GenerateBoxKeyPair(); const KeyPairInfo ephemeral_key_pair = GenerateBoxKeyPair(); - const std::vector nonce = GenerateRandom192BitNonce(); - const std::string message = "The quick brown fox jumps over the lazy dog"; + const std::vector nonce = GenerateRandomNonce(); + const std::string message = kMessage; const std::vector plaintext(message.cbegin(), message.cend()); // Act - const std::vector ciphertext = security::Encrypt( + const std::vector ciphertext = Encrypt( plaintext, nonce, key_pair.public_key, ephemeral_key_pair.secret_key); - const std::vector decrypted_plaintext = security::Decrypt( + const std::vector decrypted_plaintext = Decrypt( ciphertext, nonce, ephemeral_key_pair.public_key, key_pair.secret_key); // Assert EXPECT_EQ(plaintext, decrypted_plaintext); } -} // namespace ads::security +} // namespace ads::crypto diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/key_pair_info.cc b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/key_pair_info.cc index 1e22b14c2c5c..75c66b23d157 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/key_pair_info.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/key_pair_info.cc @@ -5,7 +5,7 @@ #include "bat/ads/internal/common/crypto/key_pair_info.h" -namespace ads::security { +namespace ads::crypto { KeyPairInfo::KeyPairInfo() = default; @@ -28,7 +28,7 @@ bool KeyPairInfo::operator!=(const KeyPairInfo& other) const { } bool KeyPairInfo::IsValid() const { - return !(public_key.empty() || secret_key.empty()); + return !public_key.empty() && !secret_key.empty(); } -} // namespace ads::security +} // namespace ads::crypto diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/key_pair_info.h b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/key_pair_info.h index cdc9c08b2872..330579f08911 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/key_pair_info.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/common/crypto/key_pair_info.h @@ -9,7 +9,7 @@ #include #include -namespace ads::security { +namespace ads::crypto { struct KeyPairInfo final { KeyPairInfo(); @@ -31,6 +31,6 @@ struct KeyPairInfo final { std::vector secret_key; }; -} // namespace ads::security +} // namespace ads::crypto #endif // BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_COMMON_CRYPTO_KEY_PAIR_INFO_H_ diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/unittest/unittest_base.cc b/vendor/bat-native-ads/src/bat/ads/internal/common/unittest/unittest_base.cc index 71170c47c194..9c8514912609 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/unittest/unittest_base.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/common/unittest/unittest_base.cc @@ -11,6 +11,7 @@ #include "base/functional/bind.h" #include "base/values.h" #include "bat/ads/database.h" +#include "bat/ads/internal/account/wallet/wallet_unittest_util.h" #include "bat/ads/internal/common/unittest/unittest_command_line_switch_util.h" #include "bat/ads/internal/common/unittest/unittest_constants.h" #include "bat/ads/internal/common/unittest/unittest_file_util.h" @@ -186,6 +187,7 @@ void UnitTestBase::Initialize() { confirmation_state_manager_ = std::make_unique(); confirmation_state_manager_->Initialize( + GetWalletForTesting(), base::BindOnce([](const bool success) { ASSERT_TRUE(success); })); covariate_manager_ = std::make_unique(); @@ -323,18 +325,14 @@ void UnitTestBase::SetUpIntegrationTest() { "initialized for integration testing"; ads_ = std::make_unique(ads_client_mock_.get()); - ads_->Initialize( - base::BindOnce(&UnitTestBase::OnAdsInitialize, base::Unretained(this))); - task_environment_.RunUntilIdle(); -} + ads_->OnRewardsWalletDidChange(GetWalletPaymentIdForTesting(), + GetWalletRecoverySeedForTesting()); -void UnitTestBase::OnAdsInitialize(const bool success) { - ASSERT_TRUE(success); + ads_->Initialize( + base::BindOnce([](const bool success) { ASSERT_TRUE(success); })); - ads_->OnRewardsWalletDidChange( - /*payment_id*/ "c387c2d8-a26d-4451-83e4-5c0c6fd942be", - /*seed*/ "5BEKM1Y7xcRSg/1q8in/+Lki2weFZQB+UMYZlRw8ql8="); + task_environment_.RunUntilIdle(); } } // namespace ads diff --git a/vendor/bat-native-ads/src/bat/ads/internal/common/unittest/unittest_base.h b/vendor/bat-native-ads/src/bat/ads/internal/common/unittest/unittest_base.h index 454476580c86..c20b425aa414 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/common/unittest/unittest_base.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/common/unittest/unittest_base.h @@ -145,8 +145,6 @@ class UnitTestBase : public testing::Test { void SetUpIntegrationTest(); - void OnAdsInitialize(bool success); - base::ScopedTempDir temp_dir_; bool is_integration_test_ = false; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/conversions/conversions_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/conversions/conversions_util.cc index 62b6acfaf687..e68076357bc8 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/conversions/conversions_util.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/conversions/conversions_util.cc @@ -64,15 +64,15 @@ absl::optional SealEnvelope( return absl::nullopt; } - const KeyPairInfo ephemeral_key_pair = GenerateBoxKeyPair(); + const crypto::KeyPairInfo ephemeral_key_pair = crypto::GenerateBoxKeyPair(); if (!ephemeral_key_pair.IsValid()) { return absl::nullopt; } - const std::vector nonce = GenerateRandom192BitNonce(); + const std::vector nonce = crypto::GenerateRandomNonce(); - const std::vector padded_ciphertext = - Encrypt(plaintext, nonce, *public_key, ephemeral_key_pair.secret_key); + const std::vector padded_ciphertext = crypto::Encrypt( + plaintext, nonce, *public_key, ephemeral_key_pair.secret_key); // The first 16 bytes of the resulting ciphertext is left as padding by the // C API and should be removed before sending out extraneously. diff --git a/vendor/bat-native-ads/src/bat/ads/internal/conversions/verifiable_conversion_envelope_unittest_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/conversions/verifiable_conversion_envelope_unittest_util.cc index 41689a6affb0..5081809ea1c1 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/conversions/verifiable_conversion_envelope_unittest_util.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/conversions/verifiable_conversion_envelope_unittest_util.cc @@ -9,7 +9,7 @@ #include #include "base/base64.h" -#include "bat/ads/internal/common/crypto/crypto_unittest_util.h" +#include "bat/ads/internal/common/crypto/crypto_util.h" #include "bat/ads/internal/conversions/verifiable_conversion_envelope_info.h" #include "tweetnacl.h" // NOLINT @@ -92,13 +92,13 @@ absl::optional OpenEnvelope( return absl::nullopt; } - const std::vector plaintext = Decrypt( + const std::vector plaintext = crypto::Decrypt( *ciphertext, *nonce, *ephemeral_public_key, *advertiser_secret_key); - return std::string(reinterpret_cast(&plaintext.front())); + return std::string(reinterpret_cast(plaintext.data())); } -absl::optional OpenEvenlopeForUserDataAndAdvertiserSecretKey( +absl::optional OpenEnvelopeForUserDataAndAdvertiserSecretKey( const base::Value::Dict& user_data, const std::string& advertiser_secret_key) { const absl::optional diff --git a/vendor/bat-native-ads/src/bat/ads/internal/conversions/verifiable_conversion_envelope_unittest_util.h b/vendor/bat-native-ads/src/bat/ads/internal/conversions/verifiable_conversion_envelope_unittest_util.h index d8541acd437d..c54b31afe2f7 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/conversions/verifiable_conversion_envelope_unittest_util.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/conversions/verifiable_conversion_envelope_unittest_util.h @@ -22,7 +22,7 @@ absl::optional OpenEnvelope( const VerifiableConversionEnvelopeInfo& verifiable_conversion_envelope, const std::string& advertiser_secret_key_base64); -absl::optional OpenEvenlopeForUserDataAndAdvertiserSecretKey( +absl::optional OpenEnvelopeForUserDataAndAdvertiserSecretKey( const base::Value::Dict& user_data, const std::string& advertiser_secret_key); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/deprecated/confirmations/confirmation_state_manager.cc b/vendor/bat-native-ads/src/bat/ads/internal/deprecated/confirmations/confirmation_state_manager.cc index da49bde41a8e..1bd880044d14 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/deprecated/confirmations/confirmation_state_manager.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/deprecated/confirmations/confirmation_state_manager.cc @@ -20,6 +20,7 @@ #include "bat/ads/internal/account/confirmations/confirmation_util.h" #include "bat/ads/internal/account/confirmations/opted_in_info.h" #include "bat/ads/internal/ads_client_helper.h" +#include "bat/ads/internal/common/crypto/crypto_util.h" #include "bat/ads/internal/common/logging_util.h" #include "bat/ads/internal/deprecated/confirmations/confirmation_state_manager_constants.h" #include "bat/ads/internal/privacy/challenge_bypass_ristretto/blinded_token.h" @@ -28,6 +29,7 @@ #include "bat/ads/internal/privacy/challenge_bypass_ristretto/unblinded_token.h" #include "bat/ads/internal/privacy/tokens/unblinded_payment_tokens/unblinded_payment_token_value_util.h" #include "bat/ads/internal/privacy/tokens/unblinded_payment_tokens/unblinded_payment_tokens.h" +#include "bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.h" #include "bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util.h" #include "bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens.h" #include "brave/components/brave_ads/common/pref_names.h" @@ -52,61 +54,6 @@ bool IsMutated(const std::string& value) { prefs::kConfirmationsHash) != GenerateHash(value); } -absl::optional GetOptedIn(const base::Value::Dict& dict) { - OptedInInfo opted_in; - - // Token - if (const std::string* const value = dict.FindString("payment_token")) { - opted_in.token = privacy::cbr::Token(*value); - } else { - return absl::nullopt; - } - - // Blinded token - if (const std::string* const value = - dict.FindString("blinded_payment_token")) { - opted_in.blinded_token = privacy::cbr::BlindedToken(*value); - } else { - return absl::nullopt; - } - - // Unblinded token - if (const base::Value::Dict* const unblinded_token = - dict.FindDict("token_info")) { - // Value - if (const std::string* const value = - unblinded_token->FindString("unblinded_token")) { - opted_in.unblinded_token.value = privacy::cbr::UnblindedToken(*value); - } else { - return absl::nullopt; - } - - // Public key - if (const std::string* const value = - unblinded_token->FindString("public_key")) { - opted_in.unblinded_token.public_key = privacy::cbr::PublicKey(*value); - } else { - return absl::nullopt; - } - } - - // User data - if (const base::Value::Dict* const value = dict.FindDict("user_data")) { - opted_in.user_data = value->Clone(); - } else { - return absl::nullopt; - } - - // Credential - if (const std::string* const value = dict.FindString("credential")) { - opted_in.credential_base64url = *value; - } else { - return absl::nullopt; - } - - return opted_in; -} - base::Value::Dict GetFailedConfirmationsAsDictionary( const ConfirmationList& confirmations) { base::Value::Dict dict; @@ -165,6 +112,10 @@ base::Value::Dict GetFailedConfirmationsAsDictionary( } unblinded_token.Set("public_key", *public_key_base64); + const absl::optional signature = + confirmation.opted_in->unblinded_token.signature; + unblinded_token.Set("signature", *signature); + confirmation_dict.Set("token_info", std::move(unblinded_token)); // User data @@ -187,99 +138,6 @@ base::Value::Dict GetFailedConfirmationsAsDictionary( return dict; } -bool GetFailedConfirmationsFromDictionary(const base::Value::Dict& dict, - ConfirmationList* confirmations) { - DCHECK(confirmations); - - // Confirmations - const base::Value::List* const failed_confirmations = - dict.FindList("failed_confirmations"); - if (!failed_confirmations) { - BLOG(0, "Failed confirmations dictionary missing failed confirmations"); - return false; - } - - ConfirmationList new_failed_confirmations; - - for (const auto& value : *failed_confirmations) { - const base::Value::Dict* const failed_confirmation_dict = value.GetIfDict(); - if (!failed_confirmation_dict) { - BLOG(0, "Confirmation should be a dictionary"); - continue; - } - - ConfirmationInfo confirmation; - - // Transaction id - if (const std::string* const value = - failed_confirmation_dict->FindString("transaction_id")) { - confirmation.transaction_id = *value; - } else { - // Migrate legacy confirmations - confirmation.transaction_id = - base::GUID::GenerateRandomV4().AsLowercaseString(); - } - - // Creative instance id - if (const std::string* const value = - failed_confirmation_dict->FindString("creative_instance_id")) { - confirmation.creative_instance_id = *value; - } else { - BLOG(0, "Missing confirmation creative instance id"); - continue; - } - - // Type - if (const std::string* const value = - failed_confirmation_dict->FindString("type")) { - confirmation.type = ConfirmationType(*value); - } else { - BLOG(0, "Missing confirmation type"); - continue; - } - - // Ad type - if (const std::string* const value = - failed_confirmation_dict->FindString("ad_type")) { - confirmation.ad_type = AdType(*value); - } else { - // Migrate legacy confirmations, this value is not used right now so safe - // to set to |kNotificationAd| - confirmation.ad_type = AdType::kNotificationAd; - } - - // Created at - if (const std::string* const value = - failed_confirmation_dict->FindString("timestamp_in_seconds")) { - double timestamp_as_double; - if (!base::StringToDouble(*value, ×tamp_as_double)) { - continue; - } - - confirmation.created_at = base::Time::FromDoubleT(timestamp_as_double); - } - - // Was created - const absl::optional was_created = - failed_confirmation_dict->FindBool("created"); - confirmation.was_created = was_created.value_or(true); - - // Opted-in - confirmation.opted_in = GetOptedIn(*failed_confirmation_dict); - - if (!IsValid(confirmation)) { - BLOG(0, "Invalid confirmation"); - continue; - } - - new_failed_confirmations.push_back(confirmation); - } - - *confirmations = new_failed_confirmations; - - return true; -} - } // namespace ConfirmationStateManager::ConfirmationStateManager() @@ -306,9 +164,14 @@ bool ConfirmationStateManager::HasInstance() { return !!g_confirmation_state_manager_instance; } -void ConfirmationStateManager::Initialize(InitializeCallback callback) { +void ConfirmationStateManager::Initialize(const WalletInfo& wallet, + InitializeCallback callback) { + DCHECK(wallet.IsValid()); + BLOG(3, "Loading confirmations state"); + wallet_ = wallet; + AdsClientHelper::GetInstance()->Load( kConfirmationStateFilename, base::BindOnce(&ConfirmationStateManager::OnLoaded, @@ -375,6 +238,176 @@ void ConfirmationStateManager::Save() { })); } +absl::optional ConfirmationStateManager::GetOptedIn( + const base::Value::Dict& dict) const { + OptedInInfo opted_in; + + // Token + if (const std::string* const value = dict.FindString("payment_token")) { + opted_in.token = privacy::cbr::Token(*value); + } else { + return absl::nullopt; + } + + // Blinded token + if (const std::string* const value = + dict.FindString("blinded_payment_token")) { + opted_in.blinded_token = privacy::cbr::BlindedToken(*value); + } else { + return absl::nullopt; + } + + // Unblinded token + if (const base::Value::Dict* const unblinded_token = + dict.FindDict("token_info")) { + // Value + if (const std::string* const value = + unblinded_token->FindString("unblinded_token")) { + opted_in.unblinded_token.value = privacy::cbr::UnblindedToken(*value); + } else { + return absl::nullopt; + } + + // Public key + if (const std::string* const value = + unblinded_token->FindString("public_key")) { + opted_in.unblinded_token.public_key = privacy::cbr::PublicKey(*value); + } else { + return absl::nullopt; + } + + // Signature + if (const std::string* const value = + unblinded_token->FindString("signature")) { + opted_in.unblinded_token.signature = *value; + } else { + const absl::optional unblinded_token_base64 = + opted_in.unblinded_token.value.EncodeBase64(); + if (!unblinded_token_base64) { + return absl::nullopt; + } + + const absl::optional signature = + crypto::Sign(*unblinded_token_base64, wallet_.secret_key); + if (!signature) { + return absl::nullopt; + } + + opted_in.unblinded_token.signature = *signature; + } + } + + // User data + if (const base::Value::Dict* const value = dict.FindDict("user_data")) { + opted_in.user_data = value->Clone(); + } else { + return absl::nullopt; + } + + // Credential + if (const std::string* const value = dict.FindString("credential")) { + opted_in.credential_base64url = *value; + } else { + return absl::nullopt; + } + + return opted_in; +} + +bool ConfirmationStateManager::GetFailedConfirmationsFromDictionary( + const base::Value::Dict& dict, + ConfirmationList* confirmations) const { + DCHECK(confirmations); + + // Confirmations + const base::Value::List* const failed_confirmations = + dict.FindList("failed_confirmations"); + if (!failed_confirmations) { + BLOG(0, "Failed confirmations dictionary missing failed confirmations"); + return false; + } + + ConfirmationList new_failed_confirmations; + + for (const auto& value : *failed_confirmations) { + const base::Value::Dict* const failed_confirmation_dict = value.GetIfDict(); + if (!failed_confirmation_dict) { + BLOG(0, "Confirmation should be a dictionary"); + continue; + } + + ConfirmationInfo confirmation; + + // Transaction id + if (const std::string* const value = + failed_confirmation_dict->FindString("transaction_id")) { + confirmation.transaction_id = *value; + } else { + // Migrate legacy confirmations + confirmation.transaction_id = + base::GUID::GenerateRandomV4().AsLowercaseString(); + } + + // Creative instance id + if (const std::string* const value = + failed_confirmation_dict->FindString("creative_instance_id")) { + confirmation.creative_instance_id = *value; + } else { + BLOG(0, "Missing confirmation creative instance id"); + continue; + } + + // Type + if (const std::string* const value = + failed_confirmation_dict->FindString("type")) { + confirmation.type = ConfirmationType(*value); + } else { + BLOG(0, "Missing confirmation type"); + continue; + } + + // Ad type + if (const std::string* const value = + failed_confirmation_dict->FindString("ad_type")) { + confirmation.ad_type = AdType(*value); + } else { + // Migrate legacy confirmations, this value is not used right now so safe + // to set to |kNotificationAd| + confirmation.ad_type = AdType::kNotificationAd; + } + + // Created at + if (const std::string* const value = + failed_confirmation_dict->FindString("timestamp_in_seconds")) { + double timestamp_as_double; + if (!base::StringToDouble(*value, ×tamp_as_double)) { + continue; + } + + confirmation.created_at = base::Time::FromDoubleT(timestamp_as_double); + } + + // Was created + const absl::optional was_created = + failed_confirmation_dict->FindBool("created"); + confirmation.was_created = was_created.value_or(true); + + // Opted-in + confirmation.opted_in = GetOptedIn(*failed_confirmation_dict); + + if (!IsValid(confirmation)) { + BLOG(0, "Invalid confirmation"); + continue; + } + + new_failed_confirmations.push_back(confirmation); + } + + *confirmations = new_failed_confirmations; + + return true; +} + const ConfirmationList& ConfirmationStateManager::GetFailedConfirmations() const { DCHECK(is_initialized_); @@ -477,8 +510,24 @@ bool ConfirmationStateManager::ParseUnblindedTokensFromDictionary( return false; } - unblinded_tokens_->SetTokens( - privacy::UnblindedTokensFromValue(*unblinded_tokens)); + privacy::UnblindedTokenList filtered_unblinded_tokens = + privacy::UnblindedTokensFromValue(*unblinded_tokens); + + const std::string public_key = wallet_.public_key; + + filtered_unblinded_tokens.erase( + base::ranges::remove_if( + filtered_unblinded_tokens, + [&public_key](const privacy::UnblindedTokenInfo& unblinded_token) { + const absl::optional unblinded_token_base64 = + unblinded_token.value.EncodeBase64(); + return !unblinded_token_base64 || + !crypto::Verify(*unblinded_token_base64, public_key, + unblinded_token.signature); + }), + filtered_unblinded_tokens.cend()); + + unblinded_tokens_->SetTokens(filtered_unblinded_tokens); return true; } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/deprecated/confirmations/confirmation_state_manager.h b/vendor/bat-native-ads/src/bat/ads/internal/deprecated/confirmations/confirmation_state_manager.h index b4a392174f20..b994e43615fe 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/deprecated/confirmations/confirmation_state_manager.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/deprecated/confirmations/confirmation_state_manager.h @@ -9,9 +9,11 @@ #include #include +#include "absl/types/optional.h" #include "base/values.h" #include "bat/ads/ads_callback.h" #include "bat/ads/internal/account/confirmations/confirmation_info.h" +#include "bat/ads/internal/account/wallet/wallet_info.h" namespace ads { @@ -38,7 +40,7 @@ class ConfirmationStateManager final { static bool HasInstance(); - void Initialize(InitializeCallback callback); + void Initialize(const WalletInfo& wallet, InitializeCallback callback); bool IsInitialized() const; void Save(); @@ -46,10 +48,14 @@ class ConfirmationStateManager final { std::string ToJson(); bool FromJson(const std::string& json); + absl::optional GetOptedIn(const base::Value::Dict& dict) const; + bool GetFailedConfirmationsFromDictionary( + const base::Value::Dict& dict, + ConfirmationList* confirmations) const; const ConfirmationList& GetFailedConfirmations() const; void AppendFailedConfirmation(const ConfirmationInfo& confirmation); bool RemoveFailedConfirmation(const ConfirmationInfo& confirmation); - void reset_failed_confirmations() { failed_confirmations_ = {}; } + void reset_failed_confirmations() { failed_confirmations_.clear(); } privacy::UnblindedTokens* GetUnblindedTokens() const { DCHECK(is_initialized_); @@ -78,6 +84,8 @@ class ConfirmationStateManager final { bool is_initialized_ = false; + WalletInfo wallet_; + ConfirmationList failed_confirmations_; std::unique_ptr unblinded_tokens_; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ml/pipeline/text_processing/embedding_processing.cc b/vendor/bat-native-ads/src/bat/ads/internal/ml/pipeline/text_processing/embedding_processing.cc index 9189bbfb43ec..d3ab2a2a72cd 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ml/pipeline/text_processing/embedding_processing.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ml/pipeline/text_processing/embedding_processing.cc @@ -99,7 +99,7 @@ TextEmbeddingInfo EmbeddingProcessing::EmbedText( } const std::string in_vocab_text = base::JoinString(in_vocab_tokens, " "); - const std::vector in_vocab_sha256 = security::Sha256(in_vocab_text); + const std::vector in_vocab_sha256 = crypto::Sha256(in_vocab_text); text_embedding.hashed_text_base64 = base::Base64Encode(in_vocab_sha256); const auto scalar = static_cast(in_vocab_tokens.size()); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.cc b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.cc index f6d27ab433b6..049865f21050 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.cc @@ -8,7 +8,8 @@ namespace ads::privacy { bool UnblindedTokenInfo::operator==(const UnblindedTokenInfo& other) const { - return public_key == other.public_key && value == other.value; + return public_key == other.public_key && value == other.value && + signature == other.signature; } bool UnblindedTokenInfo::operator!=(const UnblindedTokenInfo& other) const { diff --git a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.h b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.h index bd9ce30470df..ab39581f6dae 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.h @@ -6,6 +6,7 @@ #ifndef BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_PRIVACY_TOKENS_UNBLINDED_TOKENS_UNBLINDED_TOKEN_INFO_H_ #define BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_PRIVACY_TOKENS_UNBLINDED_TOKENS_UNBLINDED_TOKEN_INFO_H_ +#include #include #include "bat/ads/internal/privacy/challenge_bypass_ristretto/public_key.h" @@ -19,6 +20,7 @@ struct UnblindedTokenInfo final { cbr::UnblindedToken value; cbr::PublicKey public_key; + std::string signature; }; using UnblindedTokenList = std::vector; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_util.cc index f59b98078432..e7494d07a09a 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_util.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_util.cc @@ -86,7 +86,8 @@ int UnblindedTokenCount() { bool IsValid(const UnblindedTokenInfo& unblinded_token) { return unblinded_token.value.has_value() && - unblinded_token.public_key.has_value(); + unblinded_token.public_key.has_value() && + !unblinded_token.signature.empty(); } } // namespace ads::privacy diff --git a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util.cc index 3d6c575e3b7e..cf140420cd3b 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util.cc @@ -18,6 +18,7 @@ namespace { constexpr char kUnblindedTokenKey[] = "unblinded_token"; constexpr char kPublicKey[] = "public_key"; +constexpr char kSignature[] = "signature"; } // namespace @@ -41,6 +42,7 @@ base::Value::List UnblindedTokensToValue( base::Value::Dict dict; dict.Set(kUnblindedTokenKey, *unblinded_token_base64); dict.Set(kPublicKey, *public_key_base64); + dict.Set(kSignature, unblinded_token.signature); list.Append(std::move(dict)); } @@ -83,6 +85,14 @@ UnblindedTokenList UnblindedTokensFromValue(const base::Value::List& list) { continue; } + // Signature + if (const std::string* const value = dict->FindString(kSignature)) { + unblinded_token.signature = *value; + } else { + BLOG(0, "Missing unblinded token signature"); + continue; + } + unblinded_tokens.push_back(unblinded_token); } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util_unittest.cc b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util_unittest.cc index abf5df2b6888..b88d42c4fa30 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util_unittest.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_value_util_unittest.cc @@ -20,10 +20,12 @@ constexpr char kJson[] = R"( [ { "public_key": "RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk=", + "signature": "+yxJmIDobOZ5DBncIVuzjQEZfIa0+UPrSQhzA5pwEAL9qC4UW7A1H35nKAhVLehJlXnnfMVKV02StVO3fBU5CQ==", "unblinded_token": "PLowz2WF2eGD5zfwZjk9p76HXBLDKMq/3EAZHeG/fE2XGQ48jyte+Ve50ZlasOuYL5mwA8CU2aFMlJrt3DDgC3B1+VD/uyHPfa/+bwYRrpVH5YwNSDEydVx8S4r+BYVY" }, { "public_key": "RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk=", + "signature": "WeBTGGAvueivHOo33UKGTgDRw7fF/Hp9+tNZYDlUjc9CIKt/+ksh4X+mVxSMXc2E1chUWqUDME7DFFuDhasmCg==", "unblinded_token": "hfrMEltWLuzbKQ02Qixh5C/DWiJbdOoaGaidKZ7Mv+cRq5fyxJqemE/MPlARPhl6NgXPHUeyaxzd6/Lk6YHlfXbBA023DYvGMHoKm15NP/nWnZ1V3iLkgOOHZuk80Z4K" } ])"; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.cc index 6c8cf5453abc..4c1dc8595acc 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.cc @@ -6,6 +6,9 @@ #include "bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.h" #include "base/check.h" +#include "bat/ads/internal/account/wallet/wallet_info.h" +#include "bat/ads/internal/account/wallet/wallet_unittest_util.h" +#include "bat/ads/internal/common/crypto/crypto_util.h" #include "bat/ads/internal/deprecated/confirmations/confirmation_state_manager.h" #include "bat/ads/internal/privacy/challenge_bypass_ristretto/public_key.h" #include "bat/ads/internal/privacy/challenge_bypass_ristretto/unblinded_token.h" @@ -25,7 +28,8 @@ UnblindedTokenList SetUnblindedTokens(const int count) { } UnblindedTokenInfo CreateUnblindedToken( - const std::string& unblinded_token_base64) { + const std::string& unblinded_token_base64, + const WalletInfo& wallet) { UnblindedTokenInfo unblinded_token; unblinded_token.value = cbr::UnblindedToken(unblinded_token_base64); @@ -33,18 +37,24 @@ UnblindedTokenInfo CreateUnblindedToken( unblinded_token.public_key = cbr::PublicKey("RJ2i/o/pZkrH+i0aGEMY1G9FXtd7Q7gfRi3YdNRnDDk="); + const absl::optional signature = + crypto::Sign(unblinded_token_base64, wallet.secret_key); + CHECK(signature); + unblinded_token.signature = *signature; + CHECK(IsValid(unblinded_token)); return unblinded_token; } UnblindedTokenList CreateUnblindedTokens( - const std::vector& unblinded_tokens_base64) { + const std::vector& unblinded_tokens_base64, + const WalletInfo& wallet) { UnblindedTokenList unblinded_tokens; for (const auto& unblinded_token_base64 : unblinded_tokens_base64) { const UnblindedTokenInfo unblinded_token = - CreateUnblindedToken(unblinded_token_base64); + CreateUnblindedToken(unblinded_token_base64, wallet); unblinded_tokens.push_back(unblinded_token); } @@ -53,6 +63,8 @@ UnblindedTokenList CreateUnblindedTokens( } UnblindedTokenList GetUnblindedTokens(const int count) { + const WalletInfo& wallet = GetWalletForTesting(); + const std::vector unblinded_tokens_base64 = { R"(PLowz2WF2eGD5zfwZjk9p76HXBLDKMq/3EAZHeG/fE2XGQ48jyte+Ve50ZlasOuYL5mwA8CU2aFMlJrt3DDgC3B1+VD/uyHPfa/+bwYRrpVH5YwNSDEydVx8S4r+BYVY)", R"(hfrMEltWLuzbKQ02Qixh5C/DWiJbdOoaGaidKZ7Mv+cRq5fyxJqemE/MPlARPhl6NgXPHUeyaxzd6/Lk6YHlfXbBA023DYvGMHoKm15NP/nWnZ1V3iLkgOOHZuk80Z4K)", @@ -73,7 +85,7 @@ UnblindedTokenList GetUnblindedTokens(const int count) { const std::string& unblinded_token_base64 = unblinded_tokens_base64.at(i % modulo); const UnblindedTokenInfo unblinded_token = - CreateUnblindedToken(unblinded_token_base64); + CreateUnblindedToken(unblinded_token_base64, wallet); unblinded_tokens.push_back(unblinded_token); } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.h b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.h index 092a271e749f..0da828bd8e41 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_tokens_unittest_util.h @@ -11,7 +11,11 @@ #include "bat/ads/internal/privacy/tokens/unblinded_tokens/unblinded_token_info.h" -namespace ads::privacy { +namespace ads { + +struct WalletInfo; + +namespace privacy { class UnblindedTokens; @@ -20,13 +24,16 @@ UnblindedTokens* GetUnblindedTokens(); UnblindedTokenList SetUnblindedTokens(int count); UnblindedTokenInfo CreateUnblindedToken( - const std::string& unblinded_token_base64); + const std::string& unblinded_token_base64, + const WalletInfo& wallet); UnblindedTokenList CreateUnblindedTokens( - const std::vector& unblinded_tokens_base64); + const std::vector& unblinded_tokens_base64, + const WalletInfo& wallet); UnblindedTokenList GetUnblindedTokens(int count); UnblindedTokenInfo GetUnblindedToken(); -} // namespace ads::privacy +} // namespace privacy +} // namespace ads #endif // BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_PRIVACY_TOKENS_UNBLINDED_TOKENS_UNBLINDED_TOKENS_UNITTEST_UTIL_H_ diff --git a/vendor/bat-native-ads/src/bat/ads/internal/processors/contextual/text_embedding/text_embedding_html_event_unittest_util.cc b/vendor/bat-native-ads/src/bat/ads/internal/processors/contextual/text_embedding/text_embedding_html_event_unittest_util.cc index f528f9b329de..e45527d50591 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/processors/contextual/text_embedding/text_embedding_html_event_unittest_util.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/processors/contextual/text_embedding/text_embedding_html_event_unittest_util.cc @@ -15,7 +15,7 @@ ml::pipeline::TextEmbeddingInfo BuildTextEmbedding() { ml::pipeline::TextEmbeddingInfo text_embedding; text_embedding.text = "The quick brown fox jumps over the lazy dog"; text_embedding.hashed_text_base64 = - base::Base64Encode(security::Sha256(text_embedding.text)); + base::Base64Encode(crypto::Sha256(text_embedding.text)); text_embedding.locale = kDefaultLocale; text_embedding.embedding = ml::VectorData({0.0853, -0.1789, 0.4221});