Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
Simplify key generation for tests and debugging
Alexis Sellier committed 3 years ago
commit 66d1f99a1eada49d1b043aab7b9535af193a34a3
parent 03fd69f3231c5ff6e62384b3b8e2079e0231ad25
2 files changed +19 -2
modified radicle-crypto/src/lib.rs
@@ -374,6 +374,23 @@ impl sqlite::Bindable for &PublicKey {
    }
}

+
pub mod keypair {
+
    use super::*;
+
    use std::env;
+

+
    /// Generate a new keypair using OS randomness.
+
    pub fn generate() -> KeyPair {
+
        #[cfg(debug_assertions)]
+
        if env::var("RAD_DEBUG").is_ok() {
+
            // Generate a test keypair that is always the same.
+
            // This is useful for debugging and testing, since the
+
            // public key is known in advance.
+
            return KeyPair::from_seed(Seed::new([0xff; 32]));
+
        }
+
        KeyPair::generate()
+
    }
+
}
+

#[cfg(test)]
mod tests {
    use crate::PublicKey;
modified radicle-crypto/src/ssh/keystore.rs
@@ -5,7 +5,7 @@ use std::{fs, io};
use thiserror::Error;
use zeroize::Zeroizing;

-
use crate::{KeyPair, PublicKey, SecretKey, Signature, Signer, SignerError};
+
use crate::{keypair, PublicKey, SecretKey, Signature, Signer, SignerError};

/// A secret key passphrase.
pub type Passphrase = Zeroizing<String>;
@@ -47,7 +47,7 @@ impl Keystore {
    /// The `comment` is associated with the private key.
    /// The `passphrase` is used to encrypt the private key.
    pub fn init(&self, comment: &str, passphrase: &str) -> Result<PublicKey, Error> {
-
        let pair = KeyPair::generate();
+
        let pair = keypair::generate();
        let ssh_pair = ssh_key::private::Ed25519Keypair::from_bytes(&pair)?;
        let ssh_pair = ssh_key::private::KeypairData::Ed25519(ssh_pair);
        let secret = ssh_key::PrivateKey::new(ssh_pair, comment)?;