Skip to content

Commit

Permalink
Add signing key to test init for two resolvable test DIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreenbury committed Sep 4, 2023
1 parent 87e2ea1 commit e6816b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
15 changes: 8 additions & 7 deletions trustchain-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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/");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 23 additions & 10 deletions trustchain-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ pub fn type_of<T>(_: &T) -> String {
std::any::type_name::<T>().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<dyn std::error::Error>> {
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();
Expand All @@ -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/<DID_SUFFIX>/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();
});
}

Expand Down
16 changes: 3 additions & 13 deletions trustchain-ion/src/attestor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e6816b8

Please sign in to comment.