Skip to content

Commit

Permalink
add KeyPair trait
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Oct 22, 2024
1 parent 7019c24 commit b0a89ef
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 58 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ repository = "https://github.com/unavi-xyz/xdid"
license = "MIT OR Apache-2.0"
keywords = ["did"]

[workspace.metadata.release]
shared-version = true
tag-name = "v{{version}}"

[workspace.dependencies]
jose-jwk = "0.1.2"
serde_json = "1.0.132"
thiserror = "1.0.64"

[workspace.metadata.release]
shared-version = true
tag-name = "v{{version}}"
4 changes: 2 additions & 2 deletions crates/xdid-method-key/src/keys/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl Ed25519KeyPair {
pub struct Ed25519PublicKey(Vec<u8>);

impl PublicKey for Ed25519PublicKey {
fn public_key(&self) -> Vec<u8> {
self.0.clone()
fn bytes(&self) -> Box<[u8]> {
self.0.clone().into()
}

fn to_jwk(&self) -> Jwk {
Expand Down
30 changes: 19 additions & 11 deletions crates/xdid-method-key/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ pub mod p384;
#[cfg(feature = "p521")]
pub mod p521;

pub trait Multicodec {
fn code_u64(&self) -> u64;
fn code(&self) -> Vec<u8> {
let mut buffer = unsigned_varint::encode::u64_buffer();
unsigned_varint::encode::u64(self.code_u64(), &mut buffer).to_vec()
}
}
pub trait KeyPair {
fn generate() -> Self;

pub trait WithMulticodec {
fn codec(&self) -> Box<dyn Multicodec>;
fn public(&self) -> impl PublicKey;
fn public_bytes(&self) -> Box<[u8]>;
fn secret_bytes(&self) -> Box<[u8]>;
}

pub trait PublicKey: WithMulticodec {
fn public_key(&self) -> Vec<u8>;
fn bytes(&self) -> Box<[u8]>;
fn to_jwk(&self) -> Jwk;
fn to_did(&self) -> Did {
let bytes = self.public_key();
let bytes = self.bytes();
let code = self.codec().code();

let mut inner = Vec::with_capacity(code.len() + bytes.len());
Expand All @@ -45,6 +41,18 @@ pub trait PublicKey: WithMulticodec {
}
}

pub trait Multicodec {
fn code_u64(&self) -> u64;
fn code(&self) -> Vec<u8> {
let mut buffer = unsigned_varint::encode::u64_buffer();
unsigned_varint::encode::u64(self.code_u64(), &mut buffer).to_vec()
}
}

pub trait WithMulticodec {
fn codec(&self) -> Box<dyn Multicodec>;
}

pub trait KeyParser: WithMulticodec {
fn parse(&self, public_key: Vec<u8>) -> Box<dyn PublicKey>;
}
30 changes: 18 additions & 12 deletions crates/xdid-method-key/src/keys/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@ use p256::{
};
use rand::rngs::OsRng;

use super::{KeyParser, Multicodec, PublicKey, WithMulticodec};
use super::{KeyPair, KeyParser, Multicodec, PublicKey, WithMulticodec};

pub struct P256KeyPair {
secret: SecretKey,
}

impl P256KeyPair {
pub fn generate() -> Result<Self, ring::error::Unspecified> {
impl KeyPair for P256KeyPair {
fn generate() -> Self {
let mut rng = OsRng;
let secret = SecretKey::random(&mut rng);
Ok(Self { secret })
Self { secret }
}

pub fn public(&self) -> P256PublicKey {
fn public(&self) -> impl PublicKey {
P256PublicKey(self.secret.public_key())
}
fn public_bytes(&self) -> Box<[u8]> {
self.secret.public_key().to_sec1_bytes()
}
fn secret_bytes(&self) -> Box<[u8]> {
self.secret.to_bytes().to_vec().into()
}
}

pub struct P256PublicKey(p256::PublicKey);
struct P256PublicKey(p256::PublicKey);

impl PublicKey for P256PublicKey {
fn public_key(&self) -> Vec<u8> {
self.0.to_encoded_point(true).as_bytes().to_vec()
fn bytes(&self) -> Box<[u8]> {
self.0.to_encoded_point(true).as_bytes().into()
}

fn to_jwk(&self) -> Jwk {
Expand All @@ -42,7 +48,7 @@ impl WithMulticodec for P256PublicKey {
}
}

pub struct P256KeyParser;
pub(crate) struct P256KeyParser;

impl KeyParser for P256KeyParser {
fn parse(&self, public_key: Vec<u8>) -> Box<dyn PublicKey> {
Expand Down Expand Up @@ -74,7 +80,7 @@ mod tests {

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

let did_str = did.to_string();
Expand All @@ -84,13 +90,13 @@ mod tests {

#[test]
fn test_jwk() {
let pair = P256KeyPair::generate().unwrap();
let pair = P256KeyPair::generate();
let _ = pair.public().to_jwk();
}

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

let parser = DidKeyParser::default();
Expand Down
30 changes: 18 additions & 12 deletions crates/xdid-method-key/src/keys/p384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@ use p384::{
};
use rand::rngs::OsRng;

use super::{KeyParser, Multicodec, PublicKey, WithMulticodec};
use super::{KeyPair, KeyParser, Multicodec, PublicKey, WithMulticodec};

pub struct P384KeyPair {
secret: SecretKey,
}

impl P384KeyPair {
pub fn generate() -> Result<Self, ring::error::Unspecified> {
impl KeyPair for P384KeyPair {
fn generate() -> Self {
let mut rng = OsRng;
let secret = SecretKey::random(&mut rng);
Ok(Self { secret })
Self { secret }
}

pub fn public(&self) -> P384PublicKey {
fn public(&self) -> impl PublicKey {
P384PublicKey(self.secret.public_key())
}
fn public_bytes(&self) -> Box<[u8]> {
self.secret.public_key().to_sec1_bytes()
}
fn secret_bytes(&self) -> Box<[u8]> {
self.secret.to_bytes().to_vec().into()
}
}

pub struct P384PublicKey(p384::PublicKey);
struct P384PublicKey(p384::PublicKey);

impl PublicKey for P384PublicKey {
fn public_key(&self) -> Vec<u8> {
self.0.to_encoded_point(true).as_bytes().to_vec()
fn bytes(&self) -> Box<[u8]> {
self.0.to_encoded_point(true).as_bytes().into()
}

fn to_jwk(&self) -> Jwk {
Expand All @@ -42,7 +48,7 @@ impl WithMulticodec for P384PublicKey {
}
}

pub struct P384KeyParser;
pub(crate) struct P384KeyParser;

impl KeyParser for P384KeyParser {
fn parse(&self, public_key: Vec<u8>) -> Box<dyn PublicKey> {
Expand Down Expand Up @@ -74,7 +80,7 @@ mod tests {

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

let did_str = did.to_string();
Expand All @@ -84,13 +90,13 @@ mod tests {

#[test]
fn test_jwk() {
let pair = P384KeyPair::generate().unwrap();
let pair = P384KeyPair::generate();
let _ = pair.public().to_jwk();
}

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

let parser = DidKeyParser::default();
Expand Down
30 changes: 18 additions & 12 deletions crates/xdid-method-key/src/keys/p521.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@ use p521::{
};
use rand::rngs::OsRng;

use super::{KeyParser, Multicodec, PublicKey, WithMulticodec};
use super::{KeyPair, KeyParser, Multicodec, PublicKey, WithMulticodec};

pub struct P521KeyPair {
secret: SecretKey,
}

impl P521KeyPair {
pub fn generate() -> Result<Self, ring::error::Unspecified> {
impl KeyPair for P521KeyPair {
fn generate() -> Self {
let mut rng = OsRng;
let secret = SecretKey::random(&mut rng);
Ok(Self { secret })
Self { secret }
}

pub fn public(&self) -> P521PublicKey {
fn public(&self) -> impl PublicKey {
P521PublicKey(self.secret.public_key())
}
fn public_bytes(&self) -> Box<[u8]> {
self.secret.public_key().to_sec1_bytes()
}
fn secret_bytes(&self) -> Box<[u8]> {
self.secret.to_bytes().to_vec().into()
}
}

pub struct P521PublicKey(p521::PublicKey);
struct P521PublicKey(p521::PublicKey);

impl PublicKey for P521PublicKey {
fn public_key(&self) -> Vec<u8> {
self.0.to_encoded_point(true).as_bytes().to_vec()
fn bytes(&self) -> Box<[u8]> {
self.0.to_encoded_point(true).as_bytes().into()
}

fn to_jwk(&self) -> Jwk {
Expand All @@ -42,7 +48,7 @@ impl WithMulticodec for P521PublicKey {
}
}

pub struct P521KeyParser;
pub(crate) struct P521KeyParser;

impl KeyParser for P521KeyParser {
fn parse(&self, public_key: Vec<u8>) -> Box<dyn PublicKey> {
Expand Down Expand Up @@ -74,7 +80,7 @@ mod tests {

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

let did_str = did.to_string();
Expand All @@ -84,13 +90,13 @@ mod tests {

#[test]
fn test_jwk() {
let pair = P521KeyPair::generate().unwrap();
let pair = P521KeyPair::generate();
let _ = pair.public().to_jwk();
}

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

let parser = DidKeyParser::default();
Expand Down
4 changes: 2 additions & 2 deletions crates/xdid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
//! ## Example
//!
//! ```
//! use xdid::{resolver::DidResolver, methods::key::{p256::P256KeyPair, PublicKey}};
//! use xdid::{resolver::DidResolver, methods::key::{p256::P256KeyPair, KeyPair, PublicKey}};
//!
//! #[tokio::main]
//! async fn main() {
//! // Generate a new did:key.
//! let keys = P256KeyPair::generate().unwrap();
//! let keys = P256KeyPair::generate();
//! let did = keys.public().to_did();
//!
//! assert!(did.to_string().starts_with("did:key:zDn"));
Expand Down
5 changes: 2 additions & 3 deletions crates/xdid/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ impl DidResolver {

#[cfg(test)]
mod tests {
use xdid_method_key::{p256::P256KeyPair, PublicKey};
use xdid_method_key::{p256::P256KeyPair, KeyPair, PublicKey};

use super::*;

#[tokio::test]
async fn test_resolve_did_key() {
let did = P256KeyPair::generate().unwrap().public().to_did();

let did = P256KeyPair::generate().public().to_did();
let resolver = DidResolver::default();
let document = resolver.resolve(&did).await.unwrap();
assert_eq!(document.id, did);
Expand Down

0 comments on commit b0a89ef

Please sign in to comment.