Skip to content

Commit

Permalink
feat: Expose decode_seed functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Joonas Bergius <[email protected]>
  • Loading branch information
joonas committed Jun 18, 2024
1 parent 26d192c commit 43404b8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/jwk.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct JsonWebKey {

impl JsonWebKey {
pub fn from_seed(source: &str) -> Result<Self> {
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());
Expand Down
18 changes: 15 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyPair> {
let (ty, seed) = from_seed(source)?;
let (ty, seed) = decode_seed(source)?;

let signing_key = SigningKey::from_bytes(&seed);

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/xkeys.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Self> {
let (ty, seed) = from_seed(source)?;
let (ty, seed) = decode_seed(source)?;

if ty != PREFIX_BYTE_CURVE {
return Err(err!(
Expand Down

0 comments on commit 43404b8

Please sign in to comment.