Skip to content

Commit

Permalink
fix: use of branch seed in key manager (#3751)
Browse files Browse the repository at this point in the history
Description
---
The branch seed was not used (even if it was mentioned in the comment).

Motivation and Context
---
Problem with doubling the tokens in collectibles because of the pub keys for asset and tokens were the same.

How Has This Been Tested?
---
Manually (fixed the double problem in collectibles)
Added new cargo test for the use of the branch seed.
  • Loading branch information
Cifko committed Jan 26, 2022
1 parent 9725f5f commit ec92919
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion base_layer/key_manager/src/key_manager.rs
Expand Up @@ -77,7 +77,12 @@ where

/// Derive a new private key from master key: derived_key=SHA256(master_key||branch_seed||index)
pub fn derive_key(&self, key_index: u64) -> Result<DerivedKey<K>, ByteArrayError> {
let concatenated = format!("{}{}", self.seed.entropy().to_vec().to_hex(), key_index);
let concatenated = format!(
"{}{}{}",
self.seed.entropy().to_vec().to_hex(),
self.branch_seed,
key_index
);
match K::from_bytes(D::digest(&concatenated.into_bytes()).as_slice()) {
Ok(k) => Ok(DerivedKey { k, key_index }),
Err(e) => Err(e),
Expand Down Expand Up @@ -166,4 +171,14 @@ mod test {
assert_eq!(next_key1.key_index, desired_key_index1);
assert_eq!(next_key2.key_index, desired_key_index2);
}

#[test]
fn test_use_of_branch_seed() {
let x = CipherSeed::new();
let mut km1 = KeyManager::<RistrettoSecretKey, Sha256>::from(x.clone(), "some".to_string(), 0);
let mut km2 = KeyManager::<RistrettoSecretKey, Sha256>::from(x, "other".to_string(), 0);
let next_key1 = km1.next_key().unwrap();
let next_key2 = km2.next_key().unwrap();
assert_ne!(next_key1.k, next_key2.k);
}
}

0 comments on commit ec92919

Please sign in to comment.