Skip to content

Commit

Permalink
test: derive_path_from_keyset_id
Browse files Browse the repository at this point in the history
  • Loading branch information
vnprc committed Nov 13, 2024
1 parent 2cc8618 commit f3ae4f4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion crates/cdk/src/nuts/nut13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ impl PreMintSecrets {
}

fn derive_path_from_keyset_id(id: Id) -> Result<DerivationPath, Error> {
let index = u32::try_from(id)? % (2u32.pow(31) - 1);
let index = u32::try_from(id)?;

let keyset_child_number = ChildNumber::from_hardened_idx(index)?;
Ok(DerivationPath::from(vec![
ChildNumber::from_hardened_idx(129372)?,
Expand All @@ -184,6 +185,7 @@ mod tests {
use std::str::FromStr;

use bip39::Mnemonic;
use bitcoin::bip32::DerivationPath;
use bitcoin::Network;

use super::*;
Expand Down Expand Up @@ -232,4 +234,23 @@ mod tests {
assert_eq!(r, SecretKey::from_hex(test_r).unwrap())
}
}

#[test]
fn test_derive_path_from_keyset_id() {
let test_cases = [
("009a1f293253e41e", "m/129372'/0'/864559728'"),
("0000000000000000", "m/129372'/0'/0'"),
("00ffffffffffffff", "m/129372'/0'/33554431'"),
];

for (id_hex, expected_path) in test_cases {
let id = Id::from_str(id_hex).unwrap();
let path = derive_path_from_keyset_id(id).unwrap();
assert_eq!(
DerivationPath::from_str(expected_path).unwrap(),
path,
"Path derivation failed for ID {id_hex}"
);
}
}
}

0 comments on commit f3ae4f4

Please sign in to comment.