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: Expose from_public_key functionality #44

Merged
Changes from all commits
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
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ fn decode_raw(raw: &[u8]) -> Result<Vec<u8>> {
}

/// Returns the prefix byte and the underlying public key bytes
fn from_public_key(source: &str) -> Result<(u8, [u8; 32])> {
/// NOTE: This is considered an advanced use case, it's generally recommended to stick with [`KeyPair::from_public_key`] instead.
pub fn from_public_key(source: &str) -> Result<(u8, [u8; 32])> {
if source.len() != ENCODED_PUBKEY_LENGTH {
let l = source.len();
return Err(err!(InvalidKeyLength, "Bad key length: {}", l));
Expand Down Expand Up @@ -453,6 +454,17 @@ mod tests {
assert_eq!(decoded_bytes, input_bytes);
}

#[test]
fn validate_from_public_key() {
let input_bytes = generate_seed_rand();
let public_key = encode(&KeyPairType::User, input_bytes.as_slice());

let (prefix, decoded_bytes) = from_public_key(&public_key).unwrap();

assert_eq!(prefix, PREFIX_BYTE_USER);
assert_eq!(decoded_bytes, input_bytes);
}

#[test]
fn seed_encode_decode_round_trip() {
let pair = KeyPair::new_user();
Expand Down
Loading