Skip to content

Commit

Permalink
fix: add more recent zero point for wallet birthday (see issue #4176) (
Browse files Browse the repository at this point in the history
…#4275)

Description
--- 
Add logic to tackle issue #4176, which aims at improving birthday representation for Cipher Seed generation.

Motivation and Context
--- 
The goal of this PR is to resolve issue #4176.  The current version is supposed to allow a more recent genesis for wallet birthdays (it actually depends on the genesis block of the Tari network, we wish to work with). Moreover, it updates the version. Moreover, if the birthday does not fit within the `u16` type range, we update to a new version. 

How Has This Been Tested?
--- 
Add simple tests to check if logic is correct.
  • Loading branch information
jorgeantonio21 authored Aug 2, 2022
1 parent 1cabd70 commit 815c478
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions base_layer/key_manager/src/cipher_seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
use std::{
convert::TryFrom,
mem::size_of,
time::{SystemTime, UNIX_EPOCH},
ops::Add,
time::{Duration, SystemTime, UNIX_EPOCH},
};

use argon2::{
Expand Down Expand Up @@ -54,6 +55,8 @@ use crate::{
};

const CIPHER_SEED_VERSION: u8 = 0u8;
// seconds elapsed from unix epoch until '2022-01-01' == 60 * 60 * 24 * 365 * 52
pub const BIRTHDAY_GENESIS_FROM_UNIX_EPOCH: u64 = 1639872000;
pub const DEFAULT_CIPHER_SEED_PASSPHRASE: &str = "TARI_CIPHER_SEED";
const ARGON2_SALT_BYTES: usize = 16;
pub const CIPHER_SEED_BIRTHDAY_BYTES: usize = 2;
Expand Down Expand Up @@ -119,7 +122,12 @@ impl CipherSeed {
#[cfg(not(target_arch = "wasm32"))]
pub fn new() -> Self {
const SECONDS_PER_DAY: u64 = 24 * 60 * 60;
let days = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() / SECONDS_PER_DAY;
let birthday_genesis_date = UNIX_EPOCH.add(Duration::from_secs(BIRTHDAY_GENESIS_FROM_UNIX_EPOCH));
let days = SystemTime::now()
.duration_since(birthday_genesis_date)
.unwrap()
.as_secs() /
SECONDS_PER_DAY;
let birthday = u16::try_from(days).unwrap_or(0u16);
CipherSeed::new_with_birthday(birthday)
}
Expand Down

0 comments on commit 815c478

Please sign in to comment.