From b37c70dc921f4fff2a6b84fce49b06415b22fe62 Mon Sep 17 00:00:00 2001 From: AlexeyBarabash Date: Fri, 15 Apr 2022 21:57:33 +0300 Subject: [PATCH] Introduced TimeLimitedWords::GetWordsV2Epoch() date --- components/brave_sync/time_limited_words.cc | 26 ++++++++++++++----- components/brave_sync/time_limited_words.h | 2 ++ .../brave_sync/time_limited_words_unittest.cc | 19 +++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/components/brave_sync/time_limited_words.cc b/components/brave_sync/time_limited_words.cc index e860d0289a30..67ad35f733a4 100644 --- a/components/brave_sync/time_limited_words.cc +++ b/components/brave_sync/time_limited_words.cc @@ -16,7 +16,6 @@ #include "base/strings/string_util.h" #include "base/time/time.h" #include "brave/components/brave_sync/crypto/crypto.h" -#include "brave/components/brave_sync/qr_code_validator.h" #include "brave/vendor/bip39wally-core-native/include/wally_bip39.h" #include "brave/vendor/bip39wally-core-native/src/wordlist.h" @@ -27,12 +26,15 @@ namespace { // TODO(alexeybarabash): subject to change static constexpr char kWordsv1SunsetDate[] = "Wed, 1 Jun 2022 00:00:00 GMT"; +static constexpr char kWordsv2Epoch[] = "Fri, 15 Apr 2022 00:00:00 GMT"; + } // namespace using base::Time; using base::TimeDelta; Time TimeLimitedWords::words_v1_sunset_day_; +Time TimeLimitedWords::words_v2_epoch_; std::string TimeLimitedWords::GetWordByIndex(size_t index) { DCHECK_EQ(BIP39_WORDLIST_LEN, 2048); @@ -79,6 +81,17 @@ Time TimeLimitedWords::GetWordsV1SunsetDay() { return words_v1_sunset_day_; } +Time TimeLimitedWords::GetWordsV2Epoch() { + if (words_v2_epoch_.is_null()) { + bool convert_result = Time::FromUTCString(kWordsv2Epoch, &words_v2_epoch_); + CHECK(convert_result); + } + + CHECK(!words_v2_epoch_.is_null()); + + return words_v2_epoch_; +} + int TimeLimitedWords::GetRoundedDaysDiff(const Time& time1, const Time& time2) { TimeDelta delta = time2 - time1; @@ -94,15 +107,15 @@ std::string TimeLimitedWords::GenerateForNow(const std::string& pure_words) { std::string TimeLimitedWords::GenerateForDate(const std::string& pure_words, const Time& not_after) { - int days_since_qrv2_epoh = - GetRoundedDaysDiff(QrCodeDataValidator::GetQRv1SunsetDay(), not_after); + int days_since_words_v2_epoch = + GetRoundedDaysDiff(GetWordsV2Epoch(), not_after); - if (days_since_qrv2_epoh < 0) { + if (days_since_words_v2_epoch < 0) { // Something goes bad, requested |not_after| is even before sync v2 epoch return std::string(); } - std::string last_word = GetWordByIndex(days_since_qrv2_epoh); + std::string last_word = GetWordByIndex(days_since_words_v2_epoch); std::string time_limited_code = pure_words + " " + last_word; return time_limited_code; @@ -143,8 +156,7 @@ WordsValidationResult TimeLimitedWords::Validate( base::span(words.begin(), kPureWordsCount), " "); if (crypto::IsPassphraseValid(recombined_pure_words)) { int days_actual = - GetRoundedDaysDiff(QrCodeDataValidator::GetQRv1SunsetDay(), now) % - BIP39_WORDLIST_LEN; + GetRoundedDaysDiff(GetWordsV2Epoch(), now) % BIP39_WORDLIST_LEN; int days_encoded = GetIndexByWord(words[kWordsV2Count - 1]); DCHECK(days_encoded < BIP39_WORDLIST_LEN); diff --git a/components/brave_sync/time_limited_words.h b/components/brave_sync/time_limited_words.h index 328a7737b186..cafca393ad51 100644 --- a/components/brave_sync/time_limited_words.h +++ b/components/brave_sync/time_limited_words.h @@ -37,6 +37,7 @@ class TimeLimitedWords { std::string* pure_words); static base::Time GetWordsV1SunsetDay(); + static base::Time GetWordsV2Epoch(); private: FRIEND_TEST_ALL_PREFIXES(TimeLimitedWordsTest, GenerateForDate); @@ -54,6 +55,7 @@ class TimeLimitedWords { static int GetIndexByWord(const std::string& word); static base::Time words_v1_sunset_day_; + static base::Time words_v2_epoch_; }; } // namespace brave_sync diff --git a/components/brave_sync/time_limited_words_unittest.cc b/components/brave_sync/time_limited_words_unittest.cc index 0f74dd1aae96..c1f302293de8 100644 --- a/components/brave_sync/time_limited_words_unittest.cc +++ b/components/brave_sync/time_limited_words_unittest.cc @@ -10,7 +10,6 @@ #include "base/logging.h" #include "base/test/gtest_util.h" #include "base/time/time_override.h" -#include "brave/components/brave_sync/qr_code_validator.h" #include "testing/gtest/include/gtest/gtest.h" using base::subtle::ScopedTimeClockOverrides; @@ -39,7 +38,7 @@ std::unique_ptr OverrideWithTimeNow( } // namespace TEST(TimeLimitedWordsTest, GetRoundedDaysDiff) { - const base::Time time1 = QrCodeDataValidator::GetQRv1SunsetDay(); + const base::Time time1 = TimeLimitedWords::GetWordsV2Epoch(); base::Time time2 = time1 + base::Hours(11); EXPECT_EQ(TimeLimitedWords::GetRoundedDaysDiff(time1, time2), 0); @@ -65,14 +64,14 @@ TEST(TimeLimitedWordsTest, GetWordByIndex) { TEST(TimeLimitedWordsTest, GenerateForDate) { EXPECT_EQ(std::string(kValidSyncCode) + " abandon", TimeLimitedWords::GenerateForDate( - kValidSyncCode, QrCodeDataValidator::GetQRv1SunsetDay())); - EXPECT_EQ(std::string(kValidSyncCode) + " ability", - TimeLimitedWords::GenerateForDate( - kValidSyncCode, - QrCodeDataValidator::GetQRv1SunsetDay() + base::Days(1))); + kValidSyncCode, TimeLimitedWords::GetWordsV2Epoch())); + EXPECT_EQ( + std::string(kValidSyncCode) + " ability", + TimeLimitedWords::GenerateForDate( + kValidSyncCode, TimeLimitedWords::GetWordsV2Epoch() + base::Days(1))); EXPECT_EQ("", TimeLimitedWords::GenerateForDate( kValidSyncCode, - QrCodeDataValidator::GetQRv1SunsetDay() - base::Days(1))); + TimeLimitedWords::GetWordsV2Epoch() - base::Days(1))); } TEST(TimeLimitedWordsTest, Validate) { @@ -108,7 +107,7 @@ TEST(TimeLimitedWordsTest, Validate) { } const base::Time anchorDayForWordsV2 = - QrCodeDataValidator::GetQRv1SunsetDay() + base::Days(20); + TimeLimitedWords::GetWordsV2Epoch() + base::Days(20); const std::string valid25thAnchoredWord = TimeLimitedWords::GetWordByIndex(20); const std::string valid25thAnchoredWords = @@ -171,7 +170,7 @@ TEST(TimeLimitedWordsTest, Validate) { kValidSyncCode + std::string(" ") + validModulo2048Word; auto time_override = OverrideWithTimeNow( - QrCodeDataValidator::GetQRv1SunsetDay() + base::Days(2048)); + TimeLimitedWords::GetWordsV2Epoch() + base::Days(2048)); result = TimeLimitedWords::Validate(validModulo2048Words, &pure_words); EXPECT_EQ(result, WordsValidationResult::kValid); EXPECT_EQ(pure_words, kValidSyncCode);