Skip to content

Commit

Permalink
Add export genesis secret key for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpraynaud committed Jul 24, 2023
1 parent 9f1f880 commit f1bd152
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
13 changes: 4 additions & 9 deletions mithril-aggregator/src/tools/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ mod tests {

use mithril_common::{
certificate_chain::MithrilCertificateVerifier,
crypto_helper::{key_encode_hex, ProtocolClerk, ProtocolGenesisSigner},
crypto_helper::{ProtocolClerk, ProtocolGenesisSigner},
store::adapter::MemoryAdapter,
test_utils::{fake_data, MithrilFixtureBuilder},
};
Expand Down Expand Up @@ -254,15 +254,10 @@ mod tests {
let genesis_signer = ProtocolGenesisSigner::create_deterministic_genesis_signer();
let (genesis_tools, certificate_store, genesis_verifier, certificate_verifier) =
build_tools(&genesis_signer);
let mut genesis_secret_key_file = File::create(&genesis_secret_key_path).unwrap();
genesis_secret_key_file
.write_all(
key_encode_hex(genesis_signer.secret_key.as_bytes())
.unwrap()
.as_bytes(),
)
.unwrap();

genesis_signer
.export_to_file(&genesis_secret_key_path)
.expect("exporting the secret key should not fail");
genesis_tools
.export_payload_to_sign(&payload_path)
.expect("export_payload_to_sign should not fail");
Expand Down
22 changes: 20 additions & 2 deletions mithril-common/src/crypto_helper/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use crate::StdResult;
use ed25519_dalek::{ExpandedSecretKey, SignatureError};
use rand_chacha_dalek_compat::rand_core::{self, CryptoRng, RngCore, SeedableRng};
use rand_chacha_dalek_compat::ChaCha20Rng;
use serde::{Deserialize, Serialize};
use std::{fs::File, io::Write, path::Path};
use thiserror::Error;

use super::{ProtocolGenesisSecretKey, ProtocolGenesisSignature, ProtocolGenesisVerificationKey};
use super::{
key_encode_hex, ProtocolGenesisSecretKey, ProtocolGenesisSignature,
ProtocolGenesisVerificationKey,
};

#[derive(Error, Debug)]
/// [ProtocolGenesisSigner] and [ProtocolGenesisVerifier] related errors.
Expand All @@ -19,7 +24,7 @@ pub enum ProtocolGenesisError {
#[derive(Debug, Serialize, Deserialize)]
pub struct ProtocolGenesisSigner {
/// Protocol Genesis secret key
pub secret_key: ProtocolGenesisSecretKey,
pub(crate) secret_key: ProtocolGenesisSecretKey,
}

impl ProtocolGenesisSigner {
Expand Down Expand Up @@ -76,6 +81,19 @@ impl ProtocolGenesisSigner {
let verification_key = self.create_verification_key(&expanded_secret_key);
expanded_secret_key.sign(message, &verification_key)
}

/// Export the secret key from the genesis verifier to a file. TEST ONLY
#[doc(hidden)]
pub fn export_to_file(&self, secret_key_path: &Path) -> StdResult<()> {
let mut genesis_secret_key_file = File::create(secret_key_path)?;
genesis_secret_key_file.write_all(
key_encode_hex(self.secret_key.as_bytes())
.unwrap()
.as_bytes(),
)?;

Ok(())
}
}

/// A protocol Genesis Verifier that is responsible for verifying the
Expand Down

0 comments on commit f1bd152

Please sign in to comment.