Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update existing Id::try_from to return a u32 and impl new ones to convert to and from u64 #447

Closed
wants to merge 9 commits into from
43 changes: 40 additions & 3 deletions crates/cdk/src/nuts/nut02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,33 @@ impl Id {
}
}

impl TryFrom<Id> for u64 {
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)?;

let int = u64::from_be_bytes(hex_bytes);

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

impl TryFrom<u64> for Id {
type Error = Error;
fn try_from(value: u64) -> Result<Self, Self::Error> {
let bytes = value.to_be_bytes();
Self::from_bytes(&bytes)
}
}

impl TryFrom<Id> for u64 {
type Error = Error;

fn try_from(value: Id) -> Result<Self, Self::Error> {
let bytes = value.to_bytes();
let byte_array: [u8; 8] = bytes.try_into().map_err(|_| Error::Length)?;
Ok(u64::from_be_bytes(byte_array))
}
}

Expand Down Expand Up @@ -490,10 +509,28 @@ 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::try_from(id).unwrap();
assert_eq!(864559728, id_int)
}

#[test]
fn test_u64_to_id_and_back_conversion() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function name is innaccurate and too long. Change it to something like this:

Suggested change
fn test_u64_to_id_and_back_conversion() {
fn test_to_u64_and_back() {

let id = Id::from_str("009a1f293253e41e").unwrap();

let id_long = u64::try_from(id).unwrap();
assert_eq!(43381408211919902, id_long);

let new_id = Id::try_from(id_long).unwrap();
assert_eq!(id, new_id);
}

#[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();
Expand Down
Loading