Skip to content

Commit

Permalink
test + fix key parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Oct 21, 2024
1 parent 7722945 commit fd100c9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
20 changes: 17 additions & 3 deletions crates/xdid-method-key/src/keys/p256.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use jose_jwk::Jwk;
use p256::{elliptic_curve::sec1::ToEncodedPoint, pkcs8::DecodePublicKey, SecretKey};
use p256::{
elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint},
SecretKey,
};
use rand::rngs::OsRng;

use super::{KeyParser, Multicodec, PublicKey, WithMulticodec};
Expand Down Expand Up @@ -43,7 +46,8 @@ pub struct P256KeyParser;

impl KeyParser for P256KeyParser {
fn parse(&self, public_key: Vec<u8>) -> Box<dyn PublicKey> {
let key = p256::PublicKey::from_public_key_der(&public_key).unwrap();
let point = p256::EncodedPoint::from_bytes(public_key).unwrap();
let key = p256::PublicKey::from_encoded_point(&point).unwrap();
Box::new(P256PublicKey(key))
}
}
Expand All @@ -64,14 +68,15 @@ impl Multicodec for P256Codec {

#[cfg(test)]
mod tests {
use crate::DidKey;
use crate::{parser::DidKeyParser, DidKey};

use super::*;

#[test]
fn test_display() {
let pair = P256KeyPair::generate().unwrap();
let did = DidKey::new(pair.to_public()).to_did();

let did_str = did.to_string();
println!("{}", did_str);
assert!(did_str.starts_with("did:key:zDn"));
Expand All @@ -82,4 +87,13 @@ mod tests {
let pair = P256KeyPair::generate().unwrap();
let _ = pair.to_public().to_jwk();
}

#[test]
fn test_parse() {
let pair = P256KeyPair::generate().unwrap();
let did = DidKey::new(pair.to_public()).to_did();

let parser = DidKeyParser::default();
let _ = parser.parse(&did).unwrap();
}
}
20 changes: 17 additions & 3 deletions crates/xdid-method-key/src/keys/p384.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use jose_jwk::Jwk;
use p384::{elliptic_curve::sec1::ToEncodedPoint, pkcs8::DecodePublicKey, SecretKey};
use p384::{
elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint},
SecretKey,
};
use rand::rngs::OsRng;

use super::{KeyParser, Multicodec, PublicKey, WithMulticodec};
Expand Down Expand Up @@ -43,7 +46,8 @@ pub struct P384KeyParser;

impl KeyParser for P384KeyParser {
fn parse(&self, public_key: Vec<u8>) -> Box<dyn PublicKey> {
let key = p384::PublicKey::from_public_key_der(&public_key).unwrap();
let point = p384::EncodedPoint::from_bytes(public_key).unwrap();
let key = p384::PublicKey::from_encoded_point(&point).unwrap();
Box::new(P384PublicKey(key))
}
}
Expand All @@ -64,14 +68,15 @@ impl Multicodec for P384Codec {

#[cfg(test)]
mod tests {
use crate::DidKey;
use crate::{parser::DidKeyParser, DidKey};

use super::*;

#[test]
fn test_display() {
let pair = P384KeyPair::generate().unwrap();
let did = DidKey::new(pair.to_public()).to_did();

let did_str = did.to_string();
println!("{}", did_str);
assert!(did_str.starts_with("did:key:z82"));
Expand All @@ -82,4 +87,13 @@ mod tests {
let pair = P384KeyPair::generate().unwrap();
let _ = pair.to_public().to_jwk();
}

#[test]
fn test_parse() {
let pair = P384KeyPair::generate().unwrap();
let did = DidKey::new(pair.to_public()).to_did();

let parser = DidKeyParser::default();
let _ = parser.parse(&did).unwrap();
}
}
20 changes: 17 additions & 3 deletions crates/xdid-method-key/src/keys/p521.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use jose_jwk::Jwk;
use p521::{elliptic_curve::sec1::ToEncodedPoint, pkcs8::DecodePublicKey, SecretKey};
use p521::{
elliptic_curve::sec1::{FromEncodedPoint, ToEncodedPoint},
SecretKey,
};
use rand::rngs::OsRng;

use super::{KeyParser, Multicodec, PublicKey, WithMulticodec};
Expand Down Expand Up @@ -43,7 +46,8 @@ pub struct P521KeyParser;

impl KeyParser for P521KeyParser {
fn parse(&self, public_key: Vec<u8>) -> Box<dyn PublicKey> {
let key = p521::PublicKey::from_public_key_der(&public_key).unwrap();
let point = p521::EncodedPoint::from_bytes(public_key).unwrap();
let key = p521::PublicKey::from_encoded_point(&point).unwrap();
Box::new(P521PublicKey(key))
}
}
Expand All @@ -64,14 +68,15 @@ impl Multicodec for P521Codec {

#[cfg(test)]
mod tests {
use crate::DidKey;
use crate::{parser::DidKeyParser, DidKey};

use super::*;

#[test]
fn test_display() {
let pair = P521KeyPair::generate().unwrap();
let did = DidKey::new(pair.to_public()).to_did();

let did_str = did.to_string();
println!("{}", did_str);
assert!(did_str.starts_with("did:key:z2J9"));
Expand All @@ -82,4 +87,13 @@ mod tests {
let pair = P521KeyPair::generate().unwrap();
let _ = pair.to_public().to_jwk();
}

#[test]
fn test_parse() {
let pair = P521KeyPair::generate().unwrap();
let did = DidKey::new(pair.to_public()).to_did();

let parser = DidKeyParser::default();
let _ = parser.parse(&did).unwrap();
}
}
9 changes: 7 additions & 2 deletions crates/xdid-method-key/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ impl Default for DidKeyParser {
Box::new(crate::keys::ed25519::Ed25519KeyParser),
#[cfg(feature = "p256")]
Box::new(crate::keys::p256::P256KeyParser),
#[cfg(feature = "p384")]
Box::new(crate::keys::p384::P384KeyParser),
#[cfg(feature = "p521")]
Box::new(crate::keys::p521::P521KeyParser),
];

Self { parsers }
Expand All @@ -27,9 +31,10 @@ impl DidKeyParser {
debug_assert_eq!(base, Base::Base58Btc);

for parser in self.parsers.iter() {
if inner.starts_with(&parser.codec().code()) {
let code = parser.codec().code();
if let Some(bytes) = inner.strip_prefix(code.as_slice()) {
return Ok(DidKey {
key: parser.parse(inner),
key: parser.parse(bytes.to_vec()),
});
}
}
Expand Down

0 comments on commit fd100c9

Please sign in to comment.