Skip to content

Commit

Permalink
Cleanup Brave Ads tokens/wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Feb 2, 2023
1 parent c795435 commit 8d5310a
Show file tree
Hide file tree
Showing 52 changed files with 649 additions and 673 deletions.
2 changes: 0 additions & 2 deletions components/brave_ads/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions components/services/bat_ads/bat_ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion components/services/bat_ads/bat_ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions vendor/bat-native-ads/DEPS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include_rules = [
"+absl/types/optional.h",
"+bat/ads",
"+third_party/boringssl/src/include/openssl/curve25519.h",
]
2 changes: 1 addition & 1 deletion vendor/bat-native-ads/include/bat/ads/ads.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 22 additions & 5 deletions vendor/bat-native-ads/src/bat/ads/internal/account/account.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
#include "bat/ads/internal/account/account.h"

#include <utility>
#include <vector>

#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"
Expand Down Expand Up @@ -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<std::vector<uint8_t>> 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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion vendor/bat-native-ads/src/bat/ads/internal/account/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -108,7 +100,8 @@ TEST_F(BatAdsAccountTest, SetWallet) {
// Arrange

// Act
account_->SetWallet(kWalletId, kWalletSeed);
account_->SetWallet(GetWalletPaymentIdForTesting(),
GetWalletRecoverySeedForTesting());

// Assert
EXPECT_TRUE(wallet_did_update_);
Expand All @@ -120,7 +113,8 @@ TEST_F(BatAdsAccountTest, SetInvalidWallet) {
// Arrange

// Act
account_->SetWallet(kWalletId, kInvalidWalletSeed);
account_->SetWallet(GetWalletPaymentIdForTesting(),
GetInvalidWalletRecoverySeedForTesting());

// Assert
EXPECT_FALSE(wallet_did_update_);
Expand All @@ -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_);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST_F(BatAdsConversionUserDataBuilderTest, BuildConversion) {
// Act
BuildConversion(kCreativeInstanceId, [](base::Value::Dict user_data) {
const absl::optional<std::string> message =
security::OpenEvenlopeForUserDataAndAdvertiserSecretKey(
security::OpenEnvelopeForUserDataAndAdvertiserSecretKey(
user_data, kAdvertiserSecretKey);
ASSERT_TRUE(message);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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));
Expand Down
Loading

0 comments on commit 8d5310a

Please sign in to comment.