Skip to content

Commit

Permalink
fix: convert Id::TryFrom to Id::From
Browse files Browse the repository at this point in the history
  • Loading branch information
vnprc committed Nov 14, 2024
1 parent f3ae4f4 commit 507cadb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions crates/cdk/src/nuts/nut02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,24 @@ 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<Id> for u32 {
type Error = Error;
fn try_from(value: Id) -> Result<Self, Self::Error> {
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
impl From<Id> for u32 {
fn from(value: Id) -> Self {
let hex_bytes: [u8; 8] = value.as_bytes();

let int = u64::from_be_bytes(hex_bytes);

let result = (int % (2_u64.pow(31) - 1)) as u32;
Ok(result)
(int % (2_u64.pow(31) - 1)) as u32
}
}

Expand Down Expand Up @@ -491,7 +498,7 @@ mod test {
fn test_to_int() {
let id = Id::from_str("009a1f293253e41e").unwrap();

let id_int = u32::try_from(id).unwrap();
let id_int = u32::from(id);
assert_eq!(864559728, id_int)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/cdk/src/nuts/nut13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl PreMintSecrets {
}

fn derive_path_from_keyset_id(id: Id) -> Result<DerivationPath, Error> {
let index = u32::try_from(id)?;
let index = u32::from(id);

let keyset_child_number = ChildNumber::from_hardened_idx(index)?;
Ok(DerivationPath::from(vec![
Expand Down

0 comments on commit 507cadb

Please sign in to comment.