diff --git a/crates/cdk/src/nuts/nut02.rs b/crates/cdk/src/nuts/nut02.rs index 0de24c98..5d63c56b 100644 --- a/crates/cdk/src/nuts/nut02.rs +++ b/crates/cdk/src/nuts/nut02.rs @@ -112,16 +112,25 @@ impl Id { id: bytes[1..].try_into()?, }) } + + /// [`Id`] as bytes + pub fn as_bytes(&self) -> [u8; Self::BYTELEN + 1] { + let mut bytes = [0u8; Self::BYTELEN + 1]; + bytes[0] = self.version.to_byte(); + bytes[1..].copy_from_slice(&self.id); + bytes + } } -impl TryFrom for u64 { - type Error = Error; - fn try_from(value: Id) -> Result { - let hex_bytes: [u8; 8] = value.to_bytes().try_into().map_err(|_| Error::Length)?; +// Used to generate a compressed unique identifier as part of the NUT13 spec +// This is a one-way function +impl From for u32 { + fn from(value: Id) -> Self { + let hex_bytes: [u8; 8] = value.as_bytes(); let int = u64::from_be_bytes(hex_bytes); - Ok(int % (2_u64.pow(31) - 1)) + (int % (2_u64.pow(31) - 1)) as u32 } } @@ -490,10 +499,17 @@ mod test { fn test_to_int() { let id = Id::from_str("009a1f293253e41e").unwrap(); - let id_int = u64::try_from(id).unwrap(); + let id_int = u32::from(id); assert_eq!(864559728, id_int) } + #[test] + fn test_id_from_invalid_byte_length() { + let three_bytes = [0x01, 0x02, 0x03]; + let result = Id::from_bytes(&three_bytes); + assert!(result.is_err(), "Expected an invalid byte length error"); + } + #[test] fn test_keyset_bytes() { let id = Id::from_str("009a1f293253e41e").unwrap(); diff --git a/crates/cdk/src/nuts/nut13.rs b/crates/cdk/src/nuts/nut13.rs index 4383ba13..84913197 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 = (u64::try_from(id)? % (2u64.pow(31) - 1)) as u32; + let index = u32::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}" + ); + } + } }