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

feat: added general util functions with unit tests #3

Merged
merged 8 commits into from
Feb 21, 2025
Merged

Conversation

lucad70
Copy link
Collaborator

@lucad70 lucad70 commented Feb 21, 2025

Implemented these basic functions for a Nostr and Segwit usage on interface

External:
convert_npub_to_public_key(npub: String) -> String (Uses bitcoin::key::PublicKey)
check_npub(npub: String) -> bool

Internal:
convert_nsec_to_secret_key (nsec: String) -> SecretKey (Uses bitcoin::key::SecretKey)

@lucad70 lucad70 changed the title Added general util functions with unit tests feat: added general util functions with unit tests Feb 21, 2025
Copy link
Owner

@storopoli storopoli left a comment

Choose a reason for hiding this comment

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

Minor docstring suggestions.
We need to properly test convert_nsec_to_secret_key.

Comment on lines 42 to 62
#[test]
fn test_convert_days_to_blocks() {
assert_eq!(convert_days_to_blocks(1), 144);
assert_eq!(convert_days_to_blocks(2), 288);
assert_eq!(convert_days_to_blocks(3), 432);
}

#[test]
fn test_check_npub() {
let npub = "npub1tv7hxxwtw4gcz4n6fpduads7lsmynh5pjedgfhvdctnulrz9rsksjx28xe";
assert!(check_npub(npub.to_string()));
}

#[test]
fn test_nsec_to_hex() {
let nsec = "nsec1ezmlpxvhhjnqt9wf60tmshkye7xlwsf37dl0qlmrjuxeq7p3zahs2tukgx";
let expected_hex = "c8b7f09997bca60595c9d3d7b85ec4cf8df74131f37ef07f63970d907831176f";

let hex = convert_nsec_to_hex(nsec.to_string());
assert_eq!(expected_hex, hex);
}
Copy link
Owner

Choose a reason for hiding this comment

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

We are missing a test of the convert_nsec_to_secret_key. This is important to check if the conversion is successful and also should test if the SecretKey generated is valid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The function already uses the conversion before converting to hex, so the '''SecretKey::from_slice(&data)''' already does the check for a valid secret key.

@storopoli storopoli merged commit c65ad8c into main Feb 21, 2025
3 checks passed
@storopoli storopoli deleted the util-fn branch February 21, 2025 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants