Skip to content

Commit

Permalink
CLI prints DID after create operation
Browse files Browse the repository at this point in the history
  • Loading branch information
thobson88 committed Jul 6, 2024
1 parent 739e47d commit 4367fa5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
15 changes: 13 additions & 2 deletions trustchain-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ use trustchain_cli::config::cli_config;
use trustchain_core::{
vc::{CredentialError, DataCredentialError},
verifier::Verifier,
JSON_FILE_EXTENSION,
};
use trustchain_ion::{
attest::attest_operation,
create::{create_operation, create_operation_mnemonic},
trustchain_resolver,
verifier::TrustchainVerifier,
CREATE_OPERATION_FILENAME_PREFIX,
};

fn cli() -> Command {
Expand Down Expand Up @@ -131,20 +133,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if mnemonic && file_path.is_some() {
panic!("Please use only one of '--file_path' and '--mnemonic'.")
}
let filename: String;
if !mnemonic {
// Read doc state from file path
let doc_state = if let Some(file_path) = file_path {
Some(serde_json::from_reader(File::open(file_path)?)?)
} else {
None
};
create_operation(doc_state, verbose)?;
filename = create_operation(doc_state, verbose)?;
} else {
let mut mnemonic = String::new();
println!("Enter a mnemonic:");
std::io::stdin().read_line(&mut mnemonic).unwrap();
create_operation_mnemonic(&mnemonic, None)?;
filename = create_operation_mnemonic(&mnemonic, None)?;
}
println!(
"Created new DID: {}",
filename
.strip_prefix(CREATE_OPERATION_FILENAME_PREFIX)
.unwrap_or_else(|| &filename)
.strip_suffix(JSON_FILE_EXTENSION)
.unwrap_or_else(|| &filename)
);
}
Some(("attest", sub_matches)) => {
let did = sub_matches.get_one::<String>("did").unwrap();
Expand Down
11 changes: 6 additions & 5 deletions trustchain-core/src/key_manager.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Key management API with default implementations.
use crate::TRUSTCHAIN_DATA;
use crate::{JSON_FILE_EXTENSION, TRUSTCHAIN_DATA};
use serde_json::{from_str, to_string_pretty as to_json};
use ssi::jwk::JWK;
use ssi::one_or_many::OneOrMany;
use std::fmt::format;

Check warning on line 6 in trustchain-core/src/key_manager.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `std::fmt::format`

warning: unused import: `std::fmt::format` --> trustchain-core/src/key_manager.rs:6:5 | 6 | use std::fmt::format; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -151,10 +152,10 @@ pub trait KeyManager {
) -> Result<PathBuf, KeyManagerError> {
// Get the stem for the corresponding key type
let file_name = match key_type {
KeyType::UpdateKey => "update_key.json",
KeyType::NextUpdateKey => "next_update_key.json",
KeyType::RecoveryKey => "recovery_key.json",
KeyType::SigningKey => "signing_key.json",
KeyType::UpdateKey => format!("update_key{}", JSON_FILE_EXTENSION),
KeyType::NextUpdateKey => format!("next_update_key{}", JSON_FILE_EXTENSION),
KeyType::RecoveryKey => format!("recovery_key{}", JSON_FILE_EXTENSION),
KeyType::SigningKey => format!("signing_key{}", JSON_FILE_EXTENSION),
};

// Get environment for TRUSTCHAIN_DATA
Expand Down
2 changes: 2 additions & 0 deletions trustchain-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ pub const TRUSTCHAIN_PROOF_SERVICE_ID_VALUE: &str = "trustchain-controller-proof

/// The value of the type for the service containing a Trustchain controller proof within a DID document.
pub const TRUSTCHAIN_PROOF_SERVICE_TYPE_VALUE: &str = "TrustchainProofService";

pub const JSON_FILE_EXTENSION: &str = ".json";
8 changes: 6 additions & 2 deletions trustchain-ion/src/attest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! ION operation for DID attestation.
use crate::ion::IONTest as ION;
use crate::ATTEST_OPERATION_FILENAME_PREFIX;
use did_ion::sidetree::DIDStatePatch;
use did_ion::sidetree::PublicKeyJwk;
use did_ion::sidetree::{DIDSuffix, Operation, Sidetree};
Expand All @@ -10,6 +11,7 @@ use trustchain_core::key_manager::{ControllerKeyManager, KeyType};
use trustchain_core::resolver::TrustchainResolver;
use trustchain_core::subject::Subject;
use trustchain_core::utils::get_operations_path;
use trustchain_core::JSON_FILE_EXTENSION;
use trustchain_core::TRUSTCHAIN_PROOF_SERVICE_ID_VALUE;

use crate::controller::IONController;
Expand Down Expand Up @@ -103,8 +105,10 @@ pub async fn attest_operation(
// operation_manager.save(operation, OperationType::Update)?;
let path = get_operations_path()?;
let path = path.join(format!(
"attest_operation_{}.json",
controller.controlled_did_suffix()
"{}{}{}",
ATTEST_OPERATION_FILENAME_PREFIX,
controller.controlled_did_suffix(),
JSON_FILE_EXTENSION
));
std::fs::write(path, to_json(&operation).unwrap())?;

Expand Down
13 changes: 10 additions & 3 deletions trustchain-ion/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::attestor::{AttestorData, IONAttestor};
use crate::controller::{ControllerData, IONController};
use crate::ion::IONTest as ION;
use crate::mnemonic::IONKeys;
use crate::CREATE_OPERATION_FILENAME_PREFIX;
use bip39::Mnemonic;
use did_ion::sidetree::{CreateOperation, DIDStatePatch};
use did_ion::sidetree::{DocumentState, PublicKeyEntry, PublicKeyJwk};
Expand All @@ -13,6 +14,7 @@ use ssi::one_or_many::OneOrMany;
use std::convert::TryFrom;
use trustchain_core::controller::Controller;
use trustchain_core::utils::{generate_key, get_operations_path};
use trustchain_core::JSON_FILE_EXTENSION;

/// Collection of methods to return DID information from an operation.
pub trait OperationDID {
Expand Down Expand Up @@ -73,8 +75,10 @@ fn write_create_operation(
// Write create operation to push to ION server
let path = get_operations_path().unwrap();
let filename = format!(
"create_operation_{}.json",
controller.controlled_did_suffix()
"{}{}{}",
CREATE_OPERATION_FILENAME_PREFIX,
controller.controlled_did_suffix(),
JSON_FILE_EXTENSION
);
std::fs::write(
path.join(&filename),
Expand Down Expand Up @@ -275,7 +279,10 @@ mod test {

// Try to read outputted create operations and check they deserialize
let path = get_operations_path()?;
let pattern = path.join("create_operation_*.json");
let pattern = path.join(format!(
"{}*{}",
CREATE_OPERATION_FILENAME_PREFIX, JSON_FILE_EXTENSION
));
let pattern = pattern.into_os_string().into_string().unwrap();
let paths = glob(pattern.as_str())?;

Expand Down
3 changes: 3 additions & 0 deletions trustchain-ion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ pub enum TrustchainBitcoinError {

// DID
pub const CONTROLLER_KEY: &str = "controller";
pub const CREATE_OPERATION_FILENAME_PREFIX: &str = "create_operation_";
pub const ATTEST_OPERATION_FILENAME_PREFIX: &str = "attest_operation_";
// pub const UPDATE_OPERATION_FILENAME_PREFIX: &str = "update_operation_";

// ION
pub const ION_METHOD: &str = "ion";
Expand Down

0 comments on commit 4367fa5

Please sign in to comment.