Skip to content

Commit

Permalink
Introduced TimeLimitedWords::GetWordsV2Epoch() date
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed May 13, 2022
1 parent bbed331 commit b37c70d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
26 changes: 19 additions & 7 deletions components/brave_sync/time_limited_words.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

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

Expand All @@ -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;
Expand Down Expand Up @@ -143,8 +156,7 @@ WordsValidationResult TimeLimitedWords::Validate(
base::span<std::string>(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);
Expand Down
2 changes: 2 additions & 0 deletions components/brave_sync/time_limited_words.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
19 changes: 9 additions & 10 deletions components/brave_sync/time_limited_words_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,7 +38,7 @@ std::unique_ptr<ScopedTimeClockOverrides> 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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b37c70d

Please sign in to comment.