Skip to content

Commit

Permalink
Add ability to pass in additional-properties in vc create
Browse files Browse the repository at this point in the history
  • Loading branch information
w3irdrobot committed Oct 19, 2024
1 parent 6f6c336 commit 97e8c7d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions crates/web5_cli/src/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use web5::{
CredentialSubject, Issuer, VerifiableCredential, VerifiableCredentialCreateOptions,
},
dids::{bearer_did::BearerDid, portable_did::PortableDid},
json::{FromJson, ToJson},
json::{FromJson, JsonObject, ToJson},
};

#[derive(Subcommand, Debug)]
Expand All @@ -19,16 +19,19 @@ pub enum Commands {
///
/// Examples:
///
/// Create a VC with an issuer and return the minified form
/// Create a VC with an issuer and two additional properties and return the minified form
///
/// $ web5 vc create did:dht:36xw3konj1pdd93axsn9p3a58a83uatcgx1nsjud97d91dtr56ry \
/// --issuer did:dht:36xw3konj1pdd93axsn9p3a58a83uatcgx1nsjud97d91dtr56ry \
/// --additional-property "role:engineer" \
// --additional-property "clearance:level5" \
/// --no-indent
///
/// Create a VC with a portable DID and return escaped JSON
/// Create a VC with a portable DID and one additional property and return escaped JSON
///
/// $ web5 vc create did:dht:36xw3konj1pdd93axsn9p3a58a83uatcgx1nsjud97d91dtr56ry \
/// --portable-did '{"uri": ... }' \
/// --additional-property "role:engineer" \
/// --json-escape
#[command(verbatim_doc_comment)]
Create {
Expand All @@ -52,6 +55,10 @@ pub enum Commands {
/// If true, output JSON will be escaped
#[arg(long)]
json_escape: bool,
/// An additional property to add to the credentials subject of the VC. The
/// key and value are separated by a colon.
#[arg(long = "additional-property")]
additional_properties: Option<Vec<String>>,
},
Verify {
vc_jwt: String,
Expand All @@ -63,13 +70,14 @@ pub enum Commands {
}

impl Commands {
pub async fn command(&self) {
pub async fn command(self) {
match self {
Commands::Create {
credential_subject_id,
portable_did,
issuer,
expiration_date,
additional_properties,
no_indent,
json_escape,
} => {
Expand All @@ -92,12 +100,22 @@ impl Commands {
}
},
};
let additional_properties = additional_properties.map(|vs| {
vs.into_iter().fold(JsonObject::new(), |mut acc, cur| {
let split = cur.split(':').map(String::from).collect::<Vec<_>>();
// clap will make sure we are getting a string. since we will always split a string
// and get two strings, the insert should never fail since the passed in value will
// always be a string and therefore always be able to successfully become a JSON value
acc.insert(&split[0], &split[1]).unwrap();
acc
})
});

let vc = VerifiableCredential::create(
issuer,
CredentialSubject {
id: credential_subject_id.to_string(),
..Default::default()
additional_properties,
},
Some(VerifiableCredentialCreateOptions {
expiration_date,
Expand All @@ -112,7 +130,7 @@ impl Commands {
false => serde_json::to_string_pretty(&vc).unwrap(),
};

if *json_escape {
if json_escape {
output_str = output_str.replace('"', "\\\"");
}

Expand All @@ -128,7 +146,7 @@ impl Commands {
vc_jwt,
no_indent,
json_escape,
} => match VerifiableCredential::from_vc_jwt(vc_jwt, true).await {
} => match VerifiableCredential::from_vc_jwt(&vc_jwt, true).await {
Err(e) => {
println!("\n❌ Verfication failed\n");
println!("{:?} {}", e, e);
Expand All @@ -141,7 +159,7 @@ impl Commands {
false => serde_json::to_string_pretty(&vc).unwrap(),
};

if *json_escape {
if json_escape {
output_str = output_str.replace('"', "\\\"");
}

Expand Down

0 comments on commit 97e8c7d

Please sign in to comment.