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 Id::try_from to return a u32 and remove redundant logic in nut13::derive_path_from_keyset_id #452

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm hesitant to put in these u64 conversion and have the u32 one that does the module as defined in the nut. I think it maybe best to change the current TryFrom<Id> for u64 to TryFrom<Id> for u32 as you've done but to leave the u64 conversion out. Converting to and from u64 can be done outside cdk if thats needed for something as cdk should be kept to whats defined in the nuts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see now I broke some other stuff with this change to u32. It's definitely good to isolate this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't merge, nut13 unit test incoming...

}
}

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_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