diff --git a/src/jwk.rs b/src/jwk.rs index 131c901..a64be72 100644 --- a/src/jwk.rs +++ b/src/jwk.rs @@ -1,6 +1,6 @@ use std::collections::BTreeMap; -use crate::{err, from_public_key, from_seed, KeyPair, KeyPairType, Result}; +use crate::{decode_seed, err, from_public_key, KeyPair, KeyPairType, Result}; use data_encoding::BASE64URL_NOPAD; use ed25519_dalek::{SigningKey, VerifyingKey}; use serde::{Deserialize, Serialize}; @@ -42,7 +42,7 @@ pub struct JsonWebKey { impl JsonWebKey { pub fn from_seed(source: &str) -> Result { - let (prefix, seed) = from_seed(source)?; + let (prefix, seed) = decode_seed(source)?; let sk = SigningKey::from_bytes(&seed); let kp_type = &KeyPairType::from(prefix); let public_key = BASE64URL_NOPAD.encode(sk.verifying_key().as_bytes()); diff --git a/src/lib.rs b/src/lib.rs index b48c9a6..dc19ec6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -308,7 +308,7 @@ impl KeyPair { /// Attempts to produce a full key pair from the given encoded seed string pub fn from_seed(source: &str) -> Result { - let (ty, seed) = from_seed(source)?; + let (ty, seed) = decode_seed(source)?; let signing_key = SigningKey::from_bytes(&seed); @@ -364,8 +364,9 @@ fn from_public_key(source: &str) -> Result<(u8, [u8; 32])> { Ok((prefix, public_key)) } -/// Returns the type and the seed -fn from_seed(source: &str) -> Result<(u8, [u8; 32])> { +/// Attempts to decode the provided base32 encoded string into a valid prefix byte and the private key seed bytes. +/// NOTE: This is considered an advanced use case, it's generally recommended to stick with [`KeyPair::from_seed`] instead. +pub fn decode_seed(source: &str) -> Result<(u8, [u8; 32])> { if source.len() != ENCODED_SEED_LENGTH { let l = source.len(); return Err(err!(InvalidKeyLength, "Bad seed length: {}", l)); @@ -441,6 +442,17 @@ mod tests { use super::*; use crate::error::ErrorKind; + #[test] + fn validate_decode_seed() { + let input_bytes = generate_seed_rand(); + let seed = encode_seed(&KeyPairType::User, input_bytes.as_slice()); + + let (prefix, decoded_bytes) = decode_seed(&seed).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(); diff --git a/src/xkeys.rs b/src/xkeys.rs index cea42c0..8c9b370 100644 --- a/src/xkeys.rs +++ b/src/xkeys.rs @@ -1,6 +1,6 @@ use crate::{ - decode_raw, encode, encode_prefix, encode_seed, err, from_seed, KeyPairType, PREFIX_BYTE_CURVE, - PREFIX_BYTE_PRIVATE, + decode_raw, decode_seed, encode, encode_prefix, encode_seed, err, KeyPairType, + PREFIX_BYTE_CURVE, PREFIX_BYTE_PRIVATE, }; use super::Result; @@ -86,7 +86,7 @@ impl XKey { /// Attempts to produce a full xkey pair from the given encoded seed string pub fn from_seed(source: &str) -> Result { - let (ty, seed) = from_seed(source)?; + let (ty, seed) = decode_seed(source)?; if ty != PREFIX_BYTE_CURVE { return Err(err!(