From efbea86fe5df8d1ba1204556130c33aa180c822d Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Wed, 10 Jul 2019 03:53:38 -0700 Subject: [PATCH] Move the constructor for key pairs out of the trait. --- src/functions.rs | 4 ---- src/keys.rs | 38 +++++++++++++++++++------------------- src/lib.rs | 2 +- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index d46e3612..699b2711 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -77,8 +77,6 @@ static REFERENCE_HASH_FIELDS_TO_REMOVE: &[&str] = &["age_ts", "signatures", "uns /// A homeserver signs JSON with a key pair: /// /// ```rust -/// use ruma_signatures::KeyPair as _; -/// /// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI"; /// const PRIVATE_KEY: &str = "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA0"; /// @@ -297,8 +295,6 @@ pub fn reference_hash(value: &Value) -> Result { /// # Examples /// /// ```rust -/// use ruma_signatures::KeyPair as _; -/// /// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI"; /// const PRIVATE_KEY: &str = "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA0"; /// diff --git a/src/keys.rs b/src/keys.rs index a8e1c833..0fcd374d 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -9,22 +9,6 @@ use crate::{signatures::Signature, Algorithm, Error}; /// A cryptographic key pair for digitally signing data. 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; - /// Signs a JSON object. /// /// # Parameters @@ -46,8 +30,22 @@ pub struct Ed25519KeyPair { version: String, } -impl KeyPair for Ed25519KeyPair { - fn new(public_key: &[u8], private_key: &[u8], version: String) -> Result { +impl Ed25519KeyPair { + /// 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 { if let Err(error) = RingEd25519KeyPair::from_seed_and_public_key( Input::from(private_key), Input::from(public_key), @@ -61,9 +59,11 @@ impl KeyPair for Ed25519KeyPair { version, }) } +} +impl KeyPair for Ed25519KeyPair { 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( Input::from(&self.private_key), Input::from(&self.public_key), diff --git a/src/lib.rs b/src/lib.rs index 6343bd15..3bb26d6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,7 +183,7 @@ mod test { use super::{ 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 = "K8280/U9SSy9IVtjBuVeLr+HpOB4BQFWbg+UZaADMtTdGYI7Geitb76LTrr5QV/7Xg4ahLwYGYZzuHGZKM5ZAQ";