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: offer credential workflow v1 #113

Merged
merged 4 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ members = [
"cloudagent-python",
"agent",
"cli",
"workflow"
]
2 changes: 1 addition & 1 deletion agent/src/modules/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub trait ConnectionModule {
) -> Result<Connection>;
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct ConnectionCreateInvitationOptions {
pub auto_accept: bool,
pub qr: bool,
Expand Down
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ path = "src/main.rs"
[dependencies]
cloudagent-python = {path = "../cloudagent-python"}
agent = {path = "../agent"}
workflow = {path = "../workflow"}
base64 = "0.13.0"
clap = {version = "3.1.0", features = ["derive"]}
clipboard = "0.5.0"
Expand Down
2 changes: 2 additions & 0 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use clap::{Parser, Subcommand};

use crate::help_strings::HelpStrings;

use crate::modules::workflow::WorkflowOptions;
use crate::modules::{
configuration::ConfigurationOptions, connections::ConnectionOptions,
credential_definition::CredentialDefinitionOptions, credentials::CredentialOptions,
Expand Down Expand Up @@ -48,4 +49,5 @@ pub enum Commands {
Message(MessageOptions),
Credentials(CredentialOptions),
Configuration(ConfigurationOptions),
Workflow(WorkflowOptions),
}
2 changes: 1 addition & 1 deletion cli/src/modules/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub async fn parse_connection_args(
let query_parameters = split_url
.get(1)
.ok_or(Error::InvalidAgentInvitation)?
.split("&")
.split('&')
.map(|u| u.to_owned())
.collect::<Vec<String>>();

Expand Down
4 changes: 2 additions & 2 deletions cli/src/modules/features.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use agent::modules::features::FeaturesModule;
use clap::Args;
use log::{debug, info};
use log::debug;

use crate::error::Result;
use crate::help_strings::HelpStrings;
Expand All @@ -19,7 +19,7 @@ pub async fn parse_features_args(agent: impl FeaturesModule) -> Result<()> {
loader.stop();
debug!("{}", pretty_stringify_obj(&features));
features.disclose.protocols.iter().for_each(|p| {
info!("{}", p.pid);
println!("{}", p.pid);
});
})
}
1 change: 1 addition & 0 deletions cli/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod credentials;
pub mod features;
pub mod message;
pub mod schema;
pub mod workflow;
60 changes: 60 additions & 0 deletions cli/src/modules/workflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use agent::modules::connections::ConnectionModule;
use agent::modules::credential_definition::CredentialDefinitionModule;
use agent::modules::credentials::CredentialsModule;
use agent::modules::schema::SchemaModule;
use clap::{Args, Subcommand};
use colored::*;
use std::collections::HashMap;
use workflow::workflows::credential_offer::CredentialOfferWorkflow;

use crate::error::Result;
use crate::utils::loader::{Loader, LoaderVariant};

#[derive(Args)]
pub struct WorkflowOptions {
#[clap(subcommand)]
pub commands: WorkflowSubcommands,
}

#[derive(Subcommand, Debug)]
pub enum WorkflowSubcommands {
CredentialOffer {
#[clap(long, short)]
connection_id: String,
},
}

pub async fn parse_workflow_args(
options: &WorkflowOptions,
agent: impl ConnectionModule + CredentialsModule + SchemaModule + CredentialDefinitionModule,
) -> Result<()> {
let loader = Loader::start(LoaderVariant::default());

match &options.commands {
WorkflowSubcommands::CredentialOffer { connection_id } => {
// Mock credential
let mut attributes: HashMap<String, String> = HashMap::new();
attributes.insert(String::from("Name"), String::from("Joyce Brown"));
attributes.insert(String::from("Date Of Birth"), String::from("19890321"));
attributes.insert(String::from("Street"), String::from("Main Road 207"));
attributes.insert(String::from("City"), String::from("New York"));
attributes.insert(String::from("Bank"), String::from("qBank New York"));
attributes.insert(
String::from("Card Number"),
String::from("4537-6696-0666-0146"),
);
attributes.insert(String::from("Security Code"), String::from("063"));
attributes.insert(String::from("Valid Until"), String::from("20251212"));

let options = CredentialOfferWorkflow {
connection_id: connection_id.to_string(),
attributes,
};

options.execute(agent).await?;
println!("{} executed workflow", "Successfully".green());
loader.stop();
Ok(())
}
}
}
7 changes: 6 additions & 1 deletion cli/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::modules::configuration::parse_configuration_args;
use crate::modules::credential_definition::parse_credential_definition_args;
use crate::modules::credentials::parse_credentials_args;
use crate::modules::message::parse_message_args;
use crate::modules::workflow::parse_workflow_args;
use crate::modules::{
connections::parse_connection_args, features::parse_features_args, schema::parse_schema_args,
};
Expand Down Expand Up @@ -59,14 +60,18 @@ pub async fn register() -> Result<()> {
Commands::Connections(options) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
// TODO: refactor cli.copy
parse_connection_args(options, agent).await
}
Commands::Credentials(options) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
parse_credentials_args(&options.commands, agent).await
}
Commands::Workflow(options) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
parse_workflow_args(options, agent).await
}
}?;

debug!("{} executed command", "Successfully".green());
Expand Down
1 change: 1 addition & 0 deletions cli/src/utils/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn init(level: LevelFilter, should_copy: bool) {
.set_max_level(LevelFilter::Debug)
.add_filter_allow(String::from("aries_cli"))
.add_filter_allow(String::from("cloudagent_"))
.add_filter_allow(String::from("workflow"))
.build(),
TerminalMode::default(),
ColorChoice::Never,
Expand Down
12 changes: 12 additions & 0 deletions workflow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "workflow"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cloudagent-python = {path = "../cloudagent-python"}
agent = {path = "../agent"}
async-trait = "0.1.51"
log = "0.4.14"
18 changes: 18 additions & 0 deletions workflow/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::fmt::{Display, Formatter};

#[derive(Debug)]
pub enum Error {
ConnectionNotReady,
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::ConnectionNotReady => write!(f, "Connection is not in state active"),
}
}
}
2 changes: 2 additions & 0 deletions workflow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod error;
pub mod workflows;
68 changes: 68 additions & 0 deletions workflow/src/workflows/credential_offer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::error::{Error, Result};
use agent::modules::{
connections::ConnectionModule,
credential_definition::CredentialDefinitionModule,
credentials::{CredentialsModule, CredentialsOfferOptions},
schema::{SchemaCreateOptions, SchemaModule},
};
use log::trace;
use std::collections::HashMap;

/// Credential offer workflow which offers an prebuilt credential to a connection
pub struct CredentialOfferWorkflow {
pub connection_id: String,
pub attributes: HashMap<String, String>,
}

impl CredentialOfferWorkflow {
pub async fn execute(
&self,
agent: impl ConnectionModule + CredentialsModule + SchemaModule + CredentialDefinitionModule,
) -> Result<()> {
trace!("Starting workflow CredentialOfferWorkflow");
trace!("{}", self.connection_id);
trace!("{:#?}", self.attributes);

let attribute_keys: Vec<String> = self.attributes.keys().map(|e| e.to_owned()).collect();
let attribute_values: Vec<String> =
self.attributes.values().map(|e| e.to_owned()).collect();

// Check if it as a valid connection
println!("Fetching the connection...");
let connection = ConnectionModule::get_by_id(&agent, self.connection_id.to_owned()).await?;
if connection.state != "active" {
return Err(Error::ConnectionNotReady.into());
}

// Create or fetch the schema
println!("Registering the schema...");
let schema = SchemaModule::create(
&agent,
SchemaCreateOptions {
name: String::from("full-credential-offer-workflow"),
attributes: attribute_keys.to_owned(),
version: String::from("1.0"),
},
)
.await?;

println!("Registering the credential definition...");
// Create or fetch the credential definition
let credential_definition =
CredentialDefinitionModule::create(&agent, schema.schema_id).await?;

println!("Offering the credential...");
let credential_offer_response = agent
.send_offer(CredentialsOfferOptions {
keys: attribute_keys,
values: attribute_values,
connection_id: self.connection_id.to_owned(),
cred_def_id: credential_definition.credential_definition_id,
})
.await?;

trace!("Workflow completed and offered a credential");
trace!("{:#?}", credential_offer_response);
Ok(())
}
}
1 change: 1 addition & 0 deletions workflow/src/workflows/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod credential_offer;