Skip to content

Commit

Permalink
move signing to Signer trait
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Oct 24, 2024
1 parent 1baf7ef commit 2bafea1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
4 changes: 3 additions & 1 deletion crates/xdid-method-key/src/keys/ed25519.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use jose_jwk::Jwk;
use ring::{rand::SystemRandom, signature::KeyPair};

use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, WithMulticodec};
use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, Signer, WithMulticodec};

pub struct Ed25519KeyPair {
pair: ring::signature::Ed25519KeyPair,
Expand All @@ -24,7 +24,9 @@ impl DidKeyPair for Ed25519KeyPair {
fn secret_bytes(&self) -> Box<[u8]> {
todo!();
}
}

impl Signer for Ed25519KeyPair {
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, SignError> {
Ok(self.pair.sign(message).as_ref().to_vec())
}
Expand Down
8 changes: 5 additions & 3 deletions crates/xdid-method-key/src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ pub mod p384;
#[cfg(feature = "p521")]
pub mod p521;

pub trait DidKeyPair {
pub trait Signer {
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, SignError>;
}

pub trait DidKeyPair: Signer {
/// Generate a new pair of keys.
fn generate() -> Self;

Expand All @@ -24,8 +28,6 @@ pub trait DidKeyPair {
fn public_bytes(&self) -> Box<[u8]>;
/// Raw secret key bytes.
fn secret_bytes(&self) -> Box<[u8]>;

fn sign(&self, message: &[u8]) -> Result<Vec<u8>, SignError>;
}

#[derive(Error, Debug)]
Expand Down
4 changes: 3 additions & 1 deletion crates/xdid-method-key/src/keys/p256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ring::{
signature::{EcdsaKeyPair, ECDSA_P256_SHA256_ASN1_SIGNING},
};

use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, WithMulticodec};
use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, Signer, WithMulticodec};

pub struct P256KeyPair(SecretKey);

Expand All @@ -29,7 +29,9 @@ impl DidKeyPair for P256KeyPair {
fn secret_bytes(&self) -> Box<[u8]> {
self.0.to_bytes().to_vec().into()
}
}

impl Signer for P256KeyPair {
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, SignError> {
let rng = SystemRandom::new();

Expand Down
4 changes: 3 additions & 1 deletion crates/xdid-method-key/src/keys/p384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ring::{
signature::{EcdsaKeyPair, ECDSA_P384_SHA384_ASN1_SIGNING},
};

use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, WithMulticodec};
use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, Signer, WithMulticodec};

pub struct P384KeyPair {
secret: SecretKey,
Expand All @@ -31,7 +31,9 @@ impl DidKeyPair for P384KeyPair {
fn secret_bytes(&self) -> Box<[u8]> {
self.secret.to_bytes().to_vec().into()
}
}

impl Signer for P384KeyPair {
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, SignError> {
let rng = SystemRandom::new();

Expand Down
4 changes: 3 additions & 1 deletion crates/xdid-method-key/src/keys/p521.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ring::{
signature::{EcdsaKeyPair, ECDSA_P384_SHA384_ASN1_SIGNING},
};

use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, WithMulticodec};
use super::{DidKeyPair, KeyParser, Multicodec, PublicKey, SignError, Signer, WithMulticodec};

pub struct P521KeyPair {
secret: SecretKey,
Expand All @@ -31,7 +31,9 @@ impl DidKeyPair for P521KeyPair {
fn secret_bytes(&self) -> Box<[u8]> {
self.secret.to_bytes().to_vec().into()
}
}

impl Signer for P521KeyPair {
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, SignError> {
let rng = SystemRandom::new();

Expand Down

0 comments on commit 2bafea1

Please sign in to comment.