Skip to content

Commit

Permalink
chore: refactor and code review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Moriarty <[email protected]>
  • Loading branch information
Moriarty committed Dec 15, 2022
1 parent 1f2e1ba commit 2e48934
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 86 deletions.
24 changes: 4 additions & 20 deletions crates/agent/src/modules/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Did {
pub did: Option<String>,

// TODO: enum
/// The key type to query for eg. ed25519, bls1238g2
/// The key type to query for eg. ed25519, bls12381g2
pub key_type: Option<String>,

// TODO: enum
Expand All @@ -25,27 +25,11 @@ pub struct Did {
pub verkey: Option<String>,
}

/// Response from the cloudagent when requesting info about dids
/// of a wallet
#[derive(Debug, Deserialize, Serialize)]
pub struct DidList {
/// List of all the ids of every schema that the cloudagent has registered
pub results: Vec<Did>,
}

/// Response from the cloudagent when requesting info about dids
/// of a wallet
#[derive(Debug, Deserialize, Serialize)]
pub struct DidResult {
/// Single definition information about a DID of a wallet
pub result: Did,
}

/// Key type in a JSON format k,v pair
#[derive(Debug, Deserialize, Serialize)]
pub struct KeyType {
// TODO: enum
/// The key type to query for eg. ed25519, bls1238g2
/// The key type to query for eg. ed25519, bls12381g2
pub key_type: String,
}

Expand All @@ -55,7 +39,7 @@ pub struct CreateLocalDidOptions {
/// DID method to query for. e.g. sov to only fetch indy/sov DIDs Available values : key, sov
pub method: String,

/// The key type to query for eg. ed25519, bls1238g2
/// The key type to query for eg. ed25519, bls12381g2
pub options: KeyType,
}

Expand All @@ -66,7 +50,7 @@ pub struct DidEndpoint {
pub did: String,

/// The endpoint url
pub endpoint: String,
pub endpoint: Option<String>,
}

/// Options that are supplied when querying a wallet for DIDs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use siera_agent::modules::credential_definition::CredentialDefinitionModule;
use siera_agent::modules::schema::{SchemaCreateOptions, SchemaModule};

use colored::Colorize;
use siera_logger::pretty_stringify_obj;

use crate::error::Result;

Expand Down Expand Up @@ -52,20 +51,19 @@ impl<'a> CreateCredentialDefinition<'a> {
..CredentialDefinitionCreateOptions::default()
};

log_info!("{} the credential definition...", "Registering".cyan());
log!("{} the credential definition...", "Registering".cyan());
// Create or fetch the credential definition
let credential_definition = CredentialDefinitionModule::create(agent, options).await?;

log_info!(
log!(
"{} credential definition with id {}",
"Created".green(),
String::from(&credential_definition.credential_definition_id).green()
);
log!(
"{}",
pretty_stringify_obj(&credential_definition.credential_definition_id)
copy!("{}", &credential_definition.credential_definition_id);
log_json!(
{"credential_definition_id": &credential_definition.credential_definition_id}
);
copy!("{}", credential_definition.credential_definition_id);
Ok(credential_definition)
}
}
7 changes: 3 additions & 4 deletions crates/automations/src/automations/credential_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use siera_agent::modules::{
credential_definition::CredentialDefinitionModule,
schema::SchemaModule,
};
use siera_logger::pretty_stringify_obj;
use std::collections::HashMap;

/// Credential offer Automation which offers an prebuilt credential to a connection
Expand Down Expand Up @@ -52,7 +51,7 @@ impl CredentialOfferAutomation {
let attribute_values: Vec<String> = self.attributes.values().cloned().collect();

// Check if it as a valid connection
log_info!("{} the connection...", "Fetching".cyan());
log!("{} the connection...", "Fetching".cyan());
let connection = ConnectionModule::get_by_id(&agent, self.connection_id.clone()).await?;
if connection.state != "active" && connection.state != "response" {
return Err(Error::ConnectionNotReady.into());
Expand All @@ -66,7 +65,7 @@ impl CredentialOfferAutomation {

let credential_definition = create_credential_definition.execute(&agent).await?;

log_info!("{} the credential...", "Offering".cyan());
log!("{} the credential...", "Offering".cyan());
let credential_offer_response = agent
.send_offer(CredentialOfferOptions {
keys: attribute_keys.iter().map(|x| String::from(*x)).collect(),
Expand All @@ -78,7 +77,7 @@ impl CredentialOfferAutomation {

log_trace!("Automation completed and offered a credential");
log_trace!("{:#?}", credential_offer_response);
log!("{}", pretty_stringify_obj(credential_offer_response));
log_json!(credential_offer_response);
Ok(())
}
}
6 changes: 5 additions & 1 deletion crates/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub struct Cli {
#[clap(long, short, help = HelpStrings::Quiet, conflicts_with = "verbose", conflicts_with = "json")]
pub quiet: bool,

/// Whether the output should be quiet
#[clap(long, short = 'j', help = HelpStrings::Quiet, conflicts_with = "verbose", conflicts_with = "quiet")]
pub json: bool,

/// Which config path to use instead of the default one
#[clap(long, short = 'o', help = HelpStrings::Config)]
pub config: Option<PathBuf>,
Expand All @@ -51,7 +55,7 @@ pub struct Cli {
pub environment: String,

/// Whether more verbose output should be printed
#[clap(long, short='v', help = HelpStrings::Verbose, parse(from_occurrences), conflicts_with = "quiet")]
#[clap(long, short='v', help = HelpStrings::Verbose, parse(from_occurrences), conflicts_with = "quiet", conflicts_with = "json")]
pub verbose: usize,

/// The main cli subcommands
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/help_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ impl HelpStrings {
Self::Wallet => "Interacts with a wallet",
Self::WalletCreate => "Create a local DID",
Self::WalletCreateMethod => "The did method. One of 'key' or 'sov'",
Self::WalletCreateOptions => "Key types are e.g. ed25519, bls1238g2",
Self::WalletCreateOptions => "Key types are e.g. ed25519, bls12381g2",
Self::WalletEndpoint => "The endpoint url",
Self::WalletEndpointType => "The endpoint type",
Self::WalletFetchDidEndpoint => "Get the endpoint information associated with a DID",
Self::WalletGetPublic => "Get the public DID of the wallet",
Self::WalletList => "Query for DID associated with a wallet",
Self::WalletListDid => "A DID to query for",
Self::WalletListKeyType => "Key types are e.g. ed25519, bls1238g2",
Self::WalletListKeyType => "Key types are e.g. ed25519, bls12381g2",
Self::WalletListMethod => "DID method to query for. e.g. sov to only fetch indy/sov DIDs Available values : key, sov",
Self::WalletListPosture => "Whether DID is current public DID, posted to ledger but current public DID, or local to the wallet. Available values : public, posted, wallet_only",
Self::WalletListVerkey => "The verification key of interest",
Expand Down
7 changes: 3 additions & 4 deletions crates/cli/src/modules/basic_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::help_strings::HelpStrings;
use crate::utils::loader::{Loader, LoaderVariant};
use clap::Args;
use siera_agent::modules::basic_message::{BasicMessageModule, SendBasicMessageOptions};
use siera_logger::pretty_stringify_obj;

/// Basic Message options and flags
#[derive(Args)]
Expand All @@ -28,9 +27,9 @@ pub async fn parse_basic_message_args(
connection_id: options.connection_id.clone(),
message: options.message.clone(),
};
agent.send_message(send_options).await.map(|response| {
agent.send_message(send_options).await.map(|_| {
loader.stop();
log_info!("Successfully sent message");
log!("{}", pretty_stringify_obj(response));
log!("Successfully sent message");
log_json!({"status": "success"});
})
}
14 changes: 9 additions & 5 deletions crates/cli/src/modules/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ pub async fn parse_connection_args(
agent.create_invitation(options).await.map(|response| {
loader.stop();
log_info!("Created invite with connection id:");
log_info!("{}", response.id);
log!("{}", response.id);
if *qr {
log_info!("Scan this QR code to accept the invitation:\n");
print_qr_code(&response.invitation_url).unwrap();
} else {
log_info!("Another agent can use this URL to accept your invitation:");
log!("{}", pretty_stringify_obj(&response.invitation_url));
log!("{}", &response.invitation_url);
log_json!({ "invitation_url": &response.invitation_url });
}
copy!("{}", response.invitation_url);
})
Expand All @@ -136,7 +137,8 @@ pub async fn parse_connection_args(
.map(|connection| {
log_debug!("{}", pretty_stringify_obj(&connection));
log_info!("Fetched connection id:");
log!("{}", pretty_stringify_obj(connection.id));
log!("{}", connection.id);
log_json!({ "connection_id": connection.id });
})
}
ConnectionSubcommands::List {
Expand All @@ -152,7 +154,8 @@ pub async fn parse_connection_args(
Some(i) => agent.get_by_id(i.clone()).await.map(|connection| {
loader.stop();
copy!("{}", pretty_stringify_obj(&connection));
log!("{}", pretty_stringify_obj(connection));
log!("{}", pretty_stringify_obj(&connection));
log_json!({ "connection": connection })
}),
None => {
let options = ConnectionGetAllOptions {
Expand All @@ -171,7 +174,8 @@ pub async fn parse_connection_args(
agent.get_all(options).await.map(|connections| {
loader.stop();
copy!("{}", pretty_stringify_obj(&connections));
log!("{}", pretty_stringify_obj(connections));
log!("{}", pretty_stringify_obj(&connections));
log_json!({ "connections": connections })
})
}
},
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/src/modules/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ pub async fn parse_credentials_args(
loader.stop();
log_debug!("{}", pretty_stringify_obj(&cred));
log_info!("Successfully offered a credential. Credential exchange id: ",);
log!("{}", pretty_stringify_obj(cred.credential_exchange_id));
copy!("{}", cred.credential_exchange_id);
log_json!({ "credential_exchange_id": cred.credential_exchange_id });
})
}
}
Expand Down
15 changes: 9 additions & 6 deletions crates/cli/src/modules/credential_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ pub async fn parse_credential_definition_args(
loader.stop();
copy!("{}", cred_def.credential_definition_id);
log_info!("Created credential definition with id:");
log!(
"{}",
pretty_stringify_obj(cred_def.credential_definition_id)
);
log!("{}", cred_def.credential_definition_id);
log_json!({ "credential_definition_id": cred_def.credential_definition_id })
})
}
CredentialDefinitionSubcommands::List { id } => match id {
Expand All @@ -92,13 +90,18 @@ pub async fn parse_credential_definition_args(
});
log_debug!("{}", pretty_stringify_obj(&cred_def));
copy!("{}", pretty_stringify_obj(&loggable));
log!("{}", pretty_stringify_obj(loggable));
log!("{}", pretty_stringify_obj(&loggable));
log_json!({ "credential_definition": loggable })
}),

None => agent.get_all().await.map(|cred_defs| {
loader.stop();
cred_defs
.credential_definition_ids
.iter()
.for_each(|x| log!("{}", x));
log_info!("Successfully fetched credential definition IDs",);
log!("{}", pretty_stringify_obj(cred_defs));
log_json!({ "credential_definitions": cred_defs });
}),
},
}
Expand Down
5 changes: 4 additions & 1 deletion crates/cli/src/modules/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub async fn parse_features_args(agent: impl FeatureModule + Send + Sync) -> Res
agent.discover_features().await.map(|features| {
loader.stop();
log_debug!("{}", pretty_stringify_obj(&features));
log!("{}", pretty_stringify_obj(&features.disclose.protocols));
features.disclose.protocols.iter().for_each(|p| {
log!("{}", p.pid);
});
log_json!({ "features": features.disclose.protocols });
})
}
8 changes: 5 additions & 3 deletions crates/cli/src/modules/multitenancy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ pub async fn parse_multitenancy_args(
match &options.commands {
MultitenancySubcommands::Create {} => agent.create().await.map(|response| {
loader.stop();
log!("{}", pretty_stringify_obj(response));
copy!("{}", response.wallet_id);
log!("{}", pretty_stringify_obj(&response));
log_json!(response);
}),
MultitenancySubcommands::Remove { wallet_id } => {
agent.remove(wallet_id.clone()).await?;
loader.stop();
log_info!("Successfully removed wallet with id: {}", wallet_id);
log!("{}", pretty_stringify_obj(wallet_id));
log!("Successfully removed wallet with id: {}", &wallet_id);
log_json!({ "wallet_id": wallet_id });
Ok(())
}
}
Expand Down
6 changes: 4 additions & 2 deletions crates/cli/src/modules/oob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ pub async fn parse_oob_args(
print_qr_code(&response.invitation_url).unwrap();
} else {
log_info!("Another agent can use this URL to accept your invitation:\n");
log!("{}", pretty_stringify_obj(&response.invitation_url));
log!("{}", &response.invitation_url);
log_json!({ "invitation_url": response.invitation_url })
}
copy!("{}", response.invitation_url);
})
Expand All @@ -98,7 +99,8 @@ pub async fn parse_oob_args(
.map(|connection| {
log_debug!("{}", pretty_stringify_obj(&connection));
log_info!("Fetched connection id:");
log!("{}", pretty_stringify_obj(connection.connection_id));
log!("{}", &connection.connection_id);
log_json!({ "connection_id": connection.connection_id})
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/src/modules/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub async fn parse_proof_args(
.map(|proof| {
log_debug!("{}", pretty_stringify_obj(&proof));
log_info!("Successfully requested a proof. proof exchange id: ",);
log!("{}", pretty_stringify_obj(&proof.presentation_exchange_id));
log!("{}", &proof.presentation_exchange_id);
log_json!({ "presentation_exchange_id": &proof.presentation_exchange_id });
copy!("{}", &proof.presentation_exchange_id);
})?;
loader.stop();
Expand Down
6 changes: 4 additions & 2 deletions crates/cli/src/modules/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ pub async fn parse_schema_args(
.into_iter()
.for_each(|name| log_info!("- {}", name));
log_info!("Schema id:");
log!("{}", pretty_stringify_obj(&schema.id));
log!("{}", &schema.id);
log_json!({ "schema_id": &schema.id });
copy!("{}", schema.id);
})
}
Expand All @@ -83,8 +84,9 @@ pub async fn parse_schema_args(
}),
None => agent.get_all().await.map(|schemas| {
loader.stop();
schemas.schema_ids.iter().for_each(|x| log!("{}", x));
log_info!("Successfully fetched schema IDs");
log!("{}", pretty_stringify_obj(schemas.schema_ids))
log_json!({ "schema_ids": schemas.schema_ids })
}),
},
}
Expand Down
Loading

0 comments on commit 2e48934

Please sign in to comment.