Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/164/json output #240

Closed
wants to merge 12 commits into from
Closed
2 changes: 1 addition & 1 deletion crates/afj-rest/src/cloudagent/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use siera_agent::modules::connection::{
};

/// Create invitation response
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
/// Invitation url
Expand Down
4 changes: 2 additions & 2 deletions crates/agent/src/modules/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct ConnectionGetAllOptions {
}

/// Create invitation response
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Invitation {
/// Invitation url
#[serde(alias = "invitationUrl")]
Expand All @@ -46,7 +46,7 @@ pub struct Invitation {
}

/// A single connection structure
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Connection {
/// The connection id used for further functionality
#[serde(alias = "connection_id")]
Expand Down
3 changes: 3 additions & 0 deletions crates/agent/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ pub mod schema;
/// Multitenancy module for a generic cloudagent
pub mod multitenancy;

/// wallet module for a generic cloudagent
pub mod wallet;

/// webhook module for a generic cloudagent
pub mod webhook;
108 changes: 108 additions & 0 deletions crates/agent/src/modules/wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use crate::error::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};

/// Options that are supplied when querying a wallet for DIDs
#[derive(Debug, Deserialize, Serialize)]
pub struct Did {
/// The DID of interest
pub did: Option<String>,

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

// TODO: enum
/// DID method to query for. e.g. sov to only fetch indy/sov DIDs Available values : key, sov
pub method: Option<String>,

// TODO: enum
/// Whether DID is current public DID, posted to ledger but current public DID, or local to the wallet
/// Available values : public, posted, wallet_only
pub posture: Option<String>,

/// The verification key of interest
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
pub key_type: String,
}

/// Options that are supplied when querying a wallet for DIDs
#[derive(Debug, Deserialize, Serialize)]
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
pub options: KeyType,
}

/// Options that are supplied when querying a wallet for DIDs
#[derive(Debug, Deserialize, Serialize)]
pub struct DidEndpoint {
/// The DID of interest
pub did: String,

/// The endpoint url
pub endpoint: String,
}

/// Options that are supplied when querying a wallet for DIDs
#[derive(Debug, Deserialize, Serialize)]
pub struct SetDidEndpointOptions {
/// The DID of interest
pub did: String,

/// The endpoint url
pub endpoint: String,

///The endpoint type eg. 'Endpoint'
pub endpoint_type: String,
}

/// Generic cloudagent basic message module
#[async_trait]
pub trait WalletModule {
/// Query a wallet for DIDs
async fn get_wallet_dids(&self, options: Did) -> Result<Vec<Did>>;

/// Create a local DID
async fn create_local_did(&self, options: CreateLocalDidOptions) -> Result<Did>;

/// Rotate key pair
async fn rotate_keypair(&self, did: String) -> Result<()>;

/// Fetch public did
async fn fetch_public_did(&self) -> Result<Did>;

/// Assign the current public DID
async fn assign_public_did(&self, did: String) -> Result<Did>;

/// Query DID endpoint of wallet
async fn fetch_did_endpoint(&self, did: String) -> Result<DidEndpoint>;

/// Set DID endpoint of wallet
async fn set_did_endpoint(&self, options: SetDidEndpointOptions) -> Result<()>;
}
12 changes: 10 additions & 2 deletions crates/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::modules::{
basic_message::BasicMessageOptions, configuration::ConfigurationOptions,
connection::ConnectionOptions, credential::CredentialOptions,
credential_definition::CredentialDefinitionOptions, feature::FeaturesOptions, oob::OobOptions,
proof::ProofOptions, schema::SchemaOptions, webhook::WebhookOptions,
proof::ProofOptions, schema::SchemaOptions, wallet::WalletOptions, webhook::WebhookOptions,
};

/// Main command with options, flags and subcommands
Expand Down Expand Up @@ -39,9 +39,13 @@ pub struct Cli {
pub copy: bool,

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

/// Whether the output should be json
#[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 Down Expand Up @@ -97,6 +101,9 @@ pub enum Commands {

/// Multitenancy subcommands
Multitenancy(MultitenancyOptions),

/// Wallet subcommands
Wallet(WalletOptions),
}

impl From<Commands> for String {
Expand All @@ -114,6 +121,7 @@ impl From<Commands> for String {
Commands::Configuration(_) => "Configuration",
Commands::Proof(_) => "Proof",
Commands::Multitenancy(_) => "Multitenancy",
Commands::Wallet(_) => "Wallet",
};

Self::from(s)
Expand Down
37 changes: 37 additions & 0 deletions crates/cli/src/help_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ pub enum HelpStrings {
AutomationCreateCredentialDefinitionName,
AutomationCreateCredentialDefinitionAttributes,
AutomationCreateCredentialDefinitionVersion,

// Wallet
Wallet,
WalletCreate,
WalletCreateMethod,
WalletCreateOptions,
WalletEndpoint,
WalletEndpointType,
WalletFetchDidEndpoint,
WalletGetPublic,
WalletList,
WalletListDid,
WalletListKeyType,
WalletListMethod,
WalletListPosture,
WalletListVerkey,
WalletRotateKeypair,
WalletSetEndpoint,
WalletSetPublic,
}

impl From<HelpStrings> for Option<&str> {
Expand Down Expand Up @@ -251,6 +270,24 @@ impl HelpStrings {
Self::MultitenancyCreate => "Create a new sub agent",
Self::MultitenancyRemove => "Remove a sub agent",
Self::MultitenancyRemoveWalletId => "Remove the wallet by id of a sub agent",

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::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::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",
Self::WalletRotateKeypair => "Rotate the keypair for a DID",
Self::WalletSetEndpoint => "E.g. 'Endpoint'",
Self::WalletSetPublic => "Set the public DID of the wallet",
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions crates/cli/src/modules/basic_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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, LogLevel};

/// Basic Message options and flags
#[derive(Args)]
Expand All @@ -22,13 +23,20 @@ pub async fn parse_basic_message_args(
options: &BasicMessageOptions,
agent: impl BasicMessageModule + Send + Sync,
) -> Result<()> {
let loader = Loader::start(&LoaderVariant::default());
let log_level = &::siera_logger::STATE.read().unwrap().level;
let loader: Option<Loader> = match log_level {
LogLevel::Json => None,
_ => Loader::start(&LoaderVariant::default()).into(),
};
let send_options = SendBasicMessageOptions {
connection_id: options.connection_id.clone(),
message: options.message.clone(),
};
agent.send_message(send_options).await.map(|_| {
loader.stop();
log!("Successfully sent message")
agent.send_message(send_options).await.map(|response| {
if let Some(l) = loader {
l.stop()
}
log!("Successfully sent message");
log_json!("{}", pretty_stringify_obj(response));
})
}
24 changes: 19 additions & 5 deletions crates/cli/src/modules/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use siera_agent::modules::connection::{
ConnectionCreateInvitationOptions, ConnectionGetAllOptions, ConnectionModule,
ConnectionReceiveInvitationOptions,
};
use siera_logger::{copy, pretty_stringify_obj};
use siera_logger::{copy, pretty_stringify_obj, LogLevel};
use std::str;

/// Connection options and flags
Expand Down Expand Up @@ -97,7 +97,11 @@ pub async fn parse_connection_args(
options: &ConnectionOptions,
agent: impl ConnectionModule + Send + Sync,
) -> Result<()> {
let loader = Loader::start(&LoaderVariant::default());
let log_level = &::siera_logger::STATE.read().unwrap().level;
let loader: Option<Loader> = match log_level {
LogLevel::Json => None,
_ => Loader::start(&LoaderVariant::default()).into(),
};

match &options.commands {
ConnectionSubcommands::Invite {
Expand All @@ -115,7 +119,9 @@ pub async fn parse_connection_args(
toolbox: *toolbox,
};
agent.create_invitation(options).await.map(|response| {
loader.stop();
if let Some(l) = loader {
l.stop()
}
log_info!("Created invite with connection id:");
log!("{}", response.id);
if *qr {
Expand All @@ -126,6 +132,7 @@ pub async fn parse_connection_args(
log!("{}", &response.invitation_url);
}
copy!("{}", response.invitation_url);
log_json!("{}", pretty_stringify_obj(response));
})
}
ConnectionSubcommands::Receive { url } => {
Expand All @@ -136,6 +143,7 @@ pub async fn parse_connection_args(
.map(|connection| {
log_debug!("{}", pretty_stringify_obj(&connection));
log_info!("Fetched connection id:");
log_json!("{}", pretty_stringify_obj(&connection));
log!("{}", connection.id);
})
}
Expand All @@ -150,8 +158,11 @@ pub async fn parse_connection_args(
their_did,
} => match id {
Some(i) => agent.get_by_id(i.clone()).await.map(|connection| {
loader.stop();
if let Some(l) = loader {
l.stop()
}
copy!("{}", pretty_stringify_obj(&connection));
log_json!("{}", pretty_stringify_obj(&connection));
log!("{}", pretty_stringify_obj(connection));
}),
None => {
Expand All @@ -169,8 +180,11 @@ pub async fn parse_connection_args(
their_role: their_role.as_deref().map(std::string::ToString::to_string),
};
agent.get_all(options).await.map(|connections| {
loader.stop();
if let Some(l) = loader {
l.stop()
}
copy!("{}", pretty_stringify_obj(&connections));
log_json!("{}", pretty_stringify_obj(&connections));
log!("{}", pretty_stringify_obj(connections));
})
}
Expand Down
13 changes: 10 additions & 3 deletions crates/cli/src/modules/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::help_strings::HelpStrings;
use crate::utils::loader::{Loader, LoaderVariant};
use clap::{Args, Subcommand};
use siera_agent::modules::credential::{CredentialModule, CredentialOfferOptions};
use siera_logger::pretty_stringify_obj;
use siera_logger::{pretty_stringify_obj, LogLevel};

/// Credential options and flags
#[derive(Args)]
Expand Down Expand Up @@ -45,7 +45,11 @@ pub async fn parse_credentials_args(
commands: &CredentialSubcommands,
agent: impl CredentialModule + Send + Sync,
) -> Result<()> {
let loader = Loader::start(&LoaderVariant::default());
let log_level = &::siera_logger::STATE.read().unwrap().level;
let loader: Option<Loader> = match log_level {
LogLevel::Json => None,
_ => Loader::start(&LoaderVariant::default()).into(),
};
match commands {
CredentialSubcommands::Offer {
connection_id,
Expand All @@ -64,8 +68,11 @@ pub async fn parse_credentials_args(
values: value.iter().map(std::string::ToString::to_string).collect(),
};
agent.send_offer(options).await.map(|cred| {
loader.stop();
if let Some(l) = loader {
l.stop()
}
log_debug!("{}", pretty_stringify_obj(&cred));
log_json!("{}", pretty_stringify_obj(&cred));
log_info!("Successefully offered a credential. Credential exchange id: ",);
log!("{}", cred.credential_exchange_id);
})
Expand Down
Loading