Skip to content

Commit

Permalink
feat(rust): Improve ockam identity show with json
Browse files Browse the repository at this point in the history
Improve `ockam identity show` with `--output json` and `--full` to
show the same information as the plain option with the same
readability.

Closes build-trust#6130
  • Loading branch information
kriogenia committed Oct 6, 2023
1 parent 9fcae94 commit 69bd1ea
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
75 changes: 69 additions & 6 deletions implementations/rust/ockam/ockam_command/src/identity/show.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::fmt::Display;

use crate::identity::{get_identity_name, initialize_identity_if_default};
use crate::output::{EncodeFormat, IdentifierDisplay, IdentityDisplay};
use crate::output::{EncodeFormat, IdentifierDisplay, Output, VerifyingPublicKeyDisplay};
use crate::util::node_rpc;
use crate::{docs, CommandGlobalOpts};
use clap::Args;
use miette::IntoDiagnostic;
use ockam::identity::{Identity, Vault};
use ockam::identity::verified_change::VerifiedChange;
use ockam::identity::{Identifier, Identity, Vault};
use ockam_api::cli_state::traits::{StateDirTrait, StateItemTrait};
use ockam_node::Context;
use serde::Serialize;

const LONG_ABOUT: &str = include_str!("./static/show/long_about.txt");
const PREVIEW_TAG: &str = include_str!("../static/preview_tag.txt");
Expand Down Expand Up @@ -62,16 +66,16 @@ impl ShowCommand {
if Some(EncodeFormat::Hex) == cmd.encoding {
opts.println(&hex::encode(change_history.export().into_diagnostic()?))?;
} else {
let identity = Identity::import_from_change_history(
let identity: ShowIdentity = Identity::import_from_change_history(
Some(&identifier),
change_history,
Vault::create_verifying_vault(),
)
.await
.into_diagnostic()?;
.into_diagnostic()?
.into();

let identity_display = IdentityDisplay(identity);
opts.println(&identity_display)?;
opts.println(&identity)?;
}
} else {
let identifier_display = IdentifierDisplay(identifier);
Expand All @@ -80,3 +84,62 @@ impl ShowCommand {
Ok(())
}
}

#[derive(Serialize)]
struct ShowIdentity {
identifier: Identifier,
changes: Vec<Change>,
}

impl From<Identity> for ShowIdentity {
fn from(value: Identity) -> Self {
Self {
identifier: value.identifier().to_owned(),
changes: value.changes().iter().cloned().map(Change::from).collect(),
}
}
}

impl Display for ShowIdentity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Identifier: {}", self.identifier)?;
for (i_num, change) in self.changes.iter().enumerate() {
writeln!(f, " Change[{}]:", i_num)?;
writeln!(f, " identifier: {}", change.identifier)?;
writeln!(
f,
" primary_public_key: {}",
change.primary_public_key
)?;
writeln!(
f,
" revoke_all_purpose_keys: {}",
change.revoke_all_purpose_keys
)?;
}
Ok(())
}
}

impl Output for ShowIdentity {
fn output(&self) -> crate::error::Result<String> {
Ok(self.to_string())
}
}

#[derive(Serialize)]
struct Change {
pub identifier: String,
pub primary_public_key: VerifyingPublicKeyDisplay,
pub revoke_all_purpose_keys: bool,
}

impl From<VerifiedChange> for Change {
fn from(value: VerifiedChange) -> Self {
Self {
identifier: hex::encode(value.change_hash()),
primary_public_key: VerifyingPublicKeyDisplay(value.primary_public_key().to_owned()),
revoke_all_purpose_keys: value.data().revoke_all_purpose_keys,
}
}
}
16 changes: 16 additions & 0 deletions implementations/rust/ockam/ockam_command/src/output/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,22 @@ impl fmt::Display for VerifyingPublicKeyDisplay {
}
}

impl Serialize for VerifyingPublicKeyDisplay {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&match &self.0 {
VerifyingPublicKey::EdDSACurve25519(value) => {
format!("EdDSACurve25519: {}", hex::encode(value.0))
}
VerifyingPublicKey::ECDSASHA256CurveP256(value) => {
format!("ECDSASHA256CurveP256: {}", hex::encode(value.0))
}
})
}
}

impl fmt::Display for IdentityDisplay {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln!(f, "Identifier: {}", self.0.identifier())?;
Expand Down

0 comments on commit 69bd1ea

Please sign in to comment.