Skip to content

Commit

Permalink
feat: Expose from_public_key functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Joonas Bergius <[email protected]>
  • Loading branch information
joonas committed Jul 18, 2024
1 parent 7e52c00 commit 1ece246
Showing 1 changed file with 13 additions and 1 deletion.
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

0 comments on commit 1ece246

Please sign in to comment.