From f3ae4f4862ef42196fa24324750655175763cd7d Mon Sep 17 00:00:00 2001 From: vnprc Date: Wed, 13 Nov 2024 11:59:48 -0500 Subject: [PATCH] test: derive_path_from_keyset_id --- crates/cdk/src/nuts/nut13.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/cdk/src/nuts/nut13.rs b/crates/cdk/src/nuts/nut13.rs index 9cef31ca..df7706b7 100644 --- a/crates/cdk/src/nuts/nut13.rs +++ b/crates/cdk/src/nuts/nut13.rs @@ -170,7 +170,8 @@ impl PreMintSecrets { } fn derive_path_from_keyset_id(id: Id) -> Result { - 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)?, @@ -184,6 +185,7 @@ mod tests { use std::str::FromStr; use bip39::Mnemonic; + use bitcoin::bip32::DerivationPath; use bitcoin::Network; use super::*; @@ -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}" + ); + } + } }