From e6816b898686711cd5ece18869ee005c7c25e956 Mon Sep 17 00:00:00 2001 From: Sam Greenbury Date: Mon, 4 Sep 2023 17:32:41 +0100 Subject: [PATCH] Add signing key to test init for two resolvable test DIDs --- trustchain-api/src/api.rs | 15 ++++++++------- trustchain-core/src/utils.rs | 33 +++++++++++++++++++++++---------- trustchain-ion/src/attestor.rs | 16 +++------------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/trustchain-api/src/api.rs b/trustchain-api/src/api.rs index 4835a950..0a57cb0c 100644 --- a/trustchain-api/src/api.rs +++ b/trustchain-api/src/api.rs @@ -210,6 +210,7 @@ mod tests { use ssi::ldp::now_ms; use ssi::one_or_many::OneOrMany; use ssi::vc::{Credential, CredentialOrJWT, Presentation, VCDateTime}; + use trustchain_core::utils::init; use trustchain_core::vc::CredentialError; use trustchain_core::vp::PresentationError; use trustchain_core::{holder::Holder, issuer::Issuer}; @@ -248,7 +249,8 @@ mod tests { #[ignore = "requires a running Sidetree node listening on http://localhost:3000"] #[tokio::test] async fn test_verify_credential() { - let issuer_did = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; + init(); + let issuer_did = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; // root+1 let issuer = IONAttestor::new(issuer_did); let mut vc_with_proof = signed_credential(issuer).await; let resolver = get_ion_resolver("http://localhost:3000/"); @@ -283,10 +285,9 @@ mod tests { #[ignore = "requires a running Sidetree node listening on http://localhost:3000"] #[tokio::test] async fn test_verify_presentation() { - // root+1 - let issuer_did = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; - // root+2 - let holder_did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q"; + init(); + let issuer_did = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; // root+1 + let holder_did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q"; // root+2 let issuer = IONAttestor::new(issuer_did); let holder = IONAttestor::new(holder_did); @@ -332,8 +333,8 @@ mod tests { #[tokio::test] // No signature from holder in presentation (unauthenticated) async fn test_verify_presentation_unauthenticated() { - // root+1 - let issuer_did = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; + init(); + let issuer_did = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; // root+1 let issuer = IONAttestor::new(issuer_did); let vc_with_proof = signed_credential(issuer).await; diff --git a/trustchain-core/src/utils.rs b/trustchain-core/src/utils.rs index 1f6efa9f..91b0153e 100644 --- a/trustchain-core/src/utils.rs +++ b/trustchain-core/src/utils.rs @@ -12,6 +12,20 @@ pub fn type_of(_: &T) -> String { std::any::type_name::().to_string() } +/// Writes a given signing key for a given DID suffix to the key manager during test init only. +fn write_signing_key( + did_suffix: &str, + signing_key: &str, +) -> Result<(), Box> { + let path = Path::new(&std::env::var(TRUSTCHAIN_DATA)?) + .join("key_manager") + .join(did_suffix); + std::fs::create_dir_all(&path)?; + let path = path.join("signing_key.json"); + std::fs::write(path.clone(), signing_key)?; + Ok(()) +} + /// Set-up tempdir and use as env var for `TRUSTCHAIN_DATA`. // https://stackoverflow.com/questions/58006033/how-to-run-setup-code-before-any-tests-run-in-rust static INIT: Once = Once::new(); @@ -20,16 +34,15 @@ pub fn init() { // initialization code here let tempdir = tempfile::tempdir().unwrap(); std::env::set_var(TRUSTCHAIN_DATA, Path::new(tempdir.as_ref().as_os_str())); - - // TODO: write the required key_manager path in TRUSTCHAIN_DATA with: root, root-plus-1, - // root-plus-2 signing keys in "signing_key.json" files - // DID suffixes: - // root: EiCClfEdkTv_aM3UnBBhlOV89LlGhpQAbfeZLFdFxVFkEg - // root-plus-1: EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A - // root-plus-2: EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q - // - // Path for each DID's signing key file: - // env!("TRUSTCHAIN_DATA")/key_manager//signing_key.json + // Manually drop here so additional writes in the init call are not removed + drop(tempdir); + // Include test signing keys for two resolvable DIDs + let root_plus_1_did_suffix = "EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; + let root_plus_2_did_suffix = "EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q"; + let root_plus_1_signing_key: &str = r#"{"kty":"EC","crv":"secp256k1","x":"aApKobPO8H8wOv-oGT8K3Na-8l-B1AE3uBZrWGT6FJU","y":"dspEqltAtlTKJ7cVRP_gMMknyDPqUw-JHlpwS2mFuh0","d":"HbjLQf4tnwJR6861-91oGpERu8vmxDpW8ZroDCkmFvY"}"#; + let root_plus_2_signing_key: &str = r#"{"kty":"EC","crv":"secp256k1","x":"0nnR-pz2EZGfb7E1qfuHhnDR824HhBioxz4E-EBMnM4","y":"rWqDVJ3h16RT1N-Us7H7xRxvbC0UlMMQQgxmXOXd4bY","d":"bJnhIQgj0eQoRXIw5Xna6LErnili2ajMstoJLI21HiQ"}"#; + write_signing_key(root_plus_1_did_suffix, root_plus_1_signing_key).unwrap(); + write_signing_key(root_plus_2_did_suffix, root_plus_2_signing_key).unwrap(); }); } diff --git a/trustchain-ion/src/attestor.rs b/trustchain-ion/src/attestor.rs index b2c217e8..32cbf438 100644 --- a/trustchain-ion/src/attestor.rs +++ b/trustchain-ion/src/attestor.rs @@ -358,22 +358,12 @@ mod tests { #[ignore = "requires a running Sidetree node listening on http://localhost:3000"] #[tokio::test] async fn test_attest_presentation() { - // Note: removed tmp directory overwrite for TRUSTCHAIN_DATA, to have access to - // the signing keys in .trustchain - // init(); + init(); let resolver = get_ion_resolver("http://localhost:3000/"); - // root+1 - let issuer_did = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; - // root+2 - let holder_did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q"; - + let issuer_did = "did:ion:test:EiBVpjUxXeSRJpvj2TewlX9zNF3GKMCKWwGmKBZqF6pk_A"; // root+1 + let holder_did = "did:ion:test:EiAtHHKFJWAk5AsM3tgCut3OiBY4ekHTf66AAjoysXL65Q"; // root+2 let issuer = IONAttestor::new(issuer_did); let holder = IONAttestor::new(holder_did); - // let target = IONAttestor::try_from(AttestorData::new( - // did.to_string(), - // serde_json::from_str(TEST_SIGNING_KEYS).unwrap(), - // )) - // .unwrap(); let vc = serde_json::from_str(TEST_CREDENTIAL).unwrap(); let vc_with_proof = issuer.sign(&vc, None, None, &resolver).await.unwrap();