Move the constructor for key pairs out of the trait.
This commit is contained in:
parent
f1d9eba728
commit
efbea86fe5
@ -77,8 +77,6 @@ static REFERENCE_HASH_FIELDS_TO_REMOVE: &[&str] = &["age_ts", "signatures", "uns
|
|||||||
/// A homeserver signs JSON with a key pair:
|
/// A homeserver signs JSON with a key pair:
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use ruma_signatures::KeyPair as _;
|
|
||||||
///
|
|
||||||
/// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI";
|
/// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI";
|
||||||
/// const PRIVATE_KEY: &str = "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA0";
|
/// const PRIVATE_KEY: &str = "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA0";
|
||||||
///
|
///
|
||||||
@ -297,8 +295,6 @@ pub fn reference_hash(value: &Value) -> Result<String, Error> {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use ruma_signatures::KeyPair as _;
|
|
||||||
///
|
|
||||||
/// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI";
|
/// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI";
|
||||||
/// const PRIVATE_KEY: &str = "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA0";
|
/// const PRIVATE_KEY: &str = "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA0";
|
||||||
///
|
///
|
||||||
|
38
src/keys.rs
38
src/keys.rs
@ -9,22 +9,6 @@ use crate::{signatures::Signature, Algorithm, Error};
|
|||||||
|
|
||||||
/// A cryptographic key pair for digitally signing data.
|
/// A cryptographic key pair for digitally signing data.
|
||||||
pub trait KeyPair: Sized {
|
pub trait KeyPair: Sized {
|
||||||
/// Initializes a new key pair.
|
|
||||||
///
|
|
||||||
/// # Parameters
|
|
||||||
///
|
|
||||||
/// * public_key: The public key of the key pair.
|
|
||||||
/// * private_key: The private key of the key pair.
|
|
||||||
/// * version: The "version" of the key used for this signature.
|
|
||||||
/// Versions are used as an identifier to distinguish signatures generated from different keys
|
|
||||||
/// but using the same algorithm on the same homeserver.
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Returns an error if the public and private keys provided are invalid for the implementing
|
|
||||||
/// algorithm.
|
|
||||||
fn new(public_key: &[u8], private_key: &[u8], version: String) -> Result<Self, Error>;
|
|
||||||
|
|
||||||
/// Signs a JSON object.
|
/// Signs a JSON object.
|
||||||
///
|
///
|
||||||
/// # Parameters
|
/// # Parameters
|
||||||
@ -46,8 +30,22 @@ pub struct Ed25519KeyPair {
|
|||||||
version: String,
|
version: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeyPair for Ed25519KeyPair {
|
impl Ed25519KeyPair {
|
||||||
fn new(public_key: &[u8], private_key: &[u8], version: String) -> Result<Self, Error> {
|
/// Initializes a new key pair.
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
///
|
||||||
|
/// * public_key: The public key of the key pair.
|
||||||
|
/// * private_key: The private key of the key pair.
|
||||||
|
/// * version: The "version" of the key used for this signature.
|
||||||
|
/// Versions are used as an identifier to distinguish signatures generated from different keys
|
||||||
|
/// but using the same algorithm on the same homeserver.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns an error if the public and private keys provided are invalid for the implementing
|
||||||
|
/// algorithm.
|
||||||
|
pub fn new(public_key: &[u8], private_key: &[u8], version: String) -> Result<Self, Error> {
|
||||||
if let Err(error) = RingEd25519KeyPair::from_seed_and_public_key(
|
if let Err(error) = RingEd25519KeyPair::from_seed_and_public_key(
|
||||||
Input::from(private_key),
|
Input::from(private_key),
|
||||||
Input::from(public_key),
|
Input::from(public_key),
|
||||||
@ -61,9 +59,11 @@ impl KeyPair for Ed25519KeyPair {
|
|||||||
version,
|
version,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl KeyPair for Ed25519KeyPair {
|
||||||
fn sign(&self, message: &[u8]) -> Signature {
|
fn sign(&self, message: &[u8]) -> Signature {
|
||||||
// Okay to unwrap because we verified the input in the `new`.
|
// Okay to unwrap because we verified the input in `new`.
|
||||||
let ring_key_pair = RingEd25519KeyPair::from_seed_and_public_key(
|
let ring_key_pair = RingEd25519KeyPair::from_seed_and_public_key(
|
||||||
Input::from(&self.private_key),
|
Input::from(&self.private_key),
|
||||||
Input::from(&self.public_key),
|
Input::from(&self.public_key),
|
||||||
|
@ -183,7 +183,7 @@ mod test {
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
hash_and_sign_event, sign_json, to_canonical_json, verify_event, verify_json,
|
hash_and_sign_event, sign_json, to_canonical_json, verify_event, verify_json,
|
||||||
Ed25519KeyPair, Ed25519Verifier, KeyPair, Signature,
|
Ed25519KeyPair, Ed25519Verifier, Signature,
|
||||||
};
|
};
|
||||||
const EMPTY_JSON_SIGNATURE: &str =
|
const EMPTY_JSON_SIGNATURE: &str =
|
||||||
"K8280/U9SSy9IVtjBuVeLr+HpOB4BQFWbg+UZaADMtTdGYI7Geitb76LTrr5QV/7Xg4ahLwYGYZzuHGZKM5ZAQ";
|
"K8280/U9SSy9IVtjBuVeLr+HpOB4BQFWbg+UZaADMtTdGYI7Geitb76LTrr5QV/7Xg4ahLwYGYZzuHGZKM5ZAQ";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user