From 7e4a4a0e7a18c13e165f31f35fc7e175f3461d15 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo.trentesaux@lilo.org>
Date: Wed, 14 Feb 2024 18:02:16 +0100
Subject: [PATCH] add renew cert and change arg to name
 (clients/rust/gcli-v2s!21)

* revert use try_get_idty_index_by_name

review txels

* add renew cert and change arg to name
---
 src/commands/certification.rs | 25 +++++++++++++++----------
 src/commands/identity.rs      | 33 +++++++++++++++++++++++++++------
 src/commands/smith.rs         | 16 ++++++++++++----
 3 files changed, 54 insertions(+), 20 deletions(-)

diff --git a/src/commands/certification.rs b/src/commands/certification.rs
index dd8b981..2ccd8e7 100644
--- a/src/commands/certification.rs
+++ b/src/commands/certification.rs
@@ -1,14 +1,19 @@
 use crate::*;
 
 /// submit a certification and track progress
-pub async fn certify(data: &Data, receiver: IdtyId) -> Result<(), anyhow::Error> {
-	let progress = submit_call(data, &runtime::tx().certification().add_cert(receiver)).await?;
-	if data.args.no_wait {
-		return Ok(());
-	}
-	let events = track_progress(progress).await?;
-	// look for the expected event
-	look_event::<runtime::certification::events::CertAdded>(data, &events)?;
-	look_event::<runtime::certification::events::CertRenewed>(data, &events)?;
-	Ok(())
+pub async fn certify(data: &Data, target: IdtyId) -> Result<(), subxt::Error> {
+	submit_call_and_look_event::<
+		runtime::certification::events::CertAdded,
+		Payload<runtime::certification::calls::types::AddCert>,
+	>(data, &runtime::tx().certification().add_cert(target))
+	.await
+}
+
+/// renew certification
+pub async fn renew(data: &Data, target: IdtyId) -> Result<(), subxt::Error> {
+	submit_call_and_look_event::<
+		runtime::certification::events::CertRenewed,
+		Payload<runtime::certification::calls::types::RenewCert>,
+	>(data, &runtime::tx().certification().renew_cert(target))
+	.await
 }
diff --git a/src/commands/identity.rs b/src/commands/identity.rs
index 6da04b9..448c87b 100644
--- a/src/commands/identity.rs
+++ b/src/commands/identity.rs
@@ -37,9 +37,13 @@ pub enum Subcommand {
 	/// make sure that it's ok otherwise currency is slashed
 	RequestDistanceEvaluation,
 	/// Request distance evaluation for unvalidated identity
-	RequestDistanceEvaluationFor { target: IdtyId },
+	RequestDistanceEvaluationFor { target: String },
 	/// Certify an identity
-	Certify { target: IdtyId },
+	#[clap(alias = "cert")]
+	Certify { target: String },
+	/// Renew a certification
+	#[clap(alias = "renew")]
+	RenewCert { target: String },
 	/// Revoke an identity immediately
 	Revoke,
 	/// Generate a revocation document for the provided account
@@ -95,13 +99,24 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
 			commands::distance::request_distance_evaluation(&data).await?;
 		}
 		Subcommand::RequestDistanceEvaluationFor { target } => {
+			let target = try_get_idty_index_by_name(&data, &target).await?;
 			commands::distance::request_distance_evaluation_for(&data, target).await?;
 		}
 		Subcommand::Certify { target } => {
-			data = data.fetch_idty_index().await?;
-			// TODO fetch target username / key / index
-			// and ask user to confirm certification
-			commands::certification::certify(&data, target).await?;
+			let targetid = try_get_idty_index_by_name(&data, &target).await?;
+			// ask user to confirm certification
+			if let Ok(true) = inquire::Confirm::new(&format!(
+				"Are you sure you want to certify {target} ({targetid})?"
+			))
+			.with_default(false)
+			.prompt()
+			{
+				commands::certification::certify(&data, targetid).await?;
+			};
+		}
+		Subcommand::RenewCert { target } => {
+			let target = try_get_idty_index_by_name(&data, &target).await?;
+			commands::certification::renew(&data, target).await?;
 		}
 		Subcommand::Revoke => {
 			data = data.fetch_idty_index().await?;
@@ -374,6 +389,12 @@ pub async fn get_idty_index_by_name(
 		.await
 }
 
+pub async fn try_get_idty_index_by_name(data: &Data, name: &str) -> Result<IdtyId, GcliError> {
+	get_idty_index_by_name(data.client(), name)
+		.await?
+		.ok_or_else(|| GcliError::Input(format!("no identity with name {name}")))
+}
+
 /// get identityt value by index
 pub async fn get_identity_by_index(
 	client: &Client,
diff --git a/src/commands/smith.rs b/src/commands/smith.rs
index 163ad3d..ad2d1cd 100644
--- a/src/commands/smith.rs
+++ b/src/commands/smith.rs
@@ -1,5 +1,6 @@
 use crate::*;
 
+use commands::identity::try_get_idty_index_by_name;
 #[cfg(feature = "gdev")]
 use runtime::runtime_types::gdev_runtime::opaque::SessionKeys as RuntimeSessionKeys;
 use std::ops::Deref;
@@ -57,11 +58,12 @@ pub enum Subcommand {
 	/// List online authorities
 	ShowOnline,
 	/// Invite identity to become smith
-	Invite { target: IdtyId },
+	Invite { target: String },
 	/// Accept invitation
 	Accept,
 	/// Certify smith
-	Certify { target: IdtyId },
+	#[clap(alias = "cert")]
+	Certify { target: String },
 }
 
 /// handle smith commands
@@ -91,9 +93,15 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
 			commands::expire::monitor_expirations(&data, blocks, sessions).await?
 		}
 		Subcommand::ShowOnline => online(&data).await?,
-		Subcommand::Invite { target } => invite_smith(&data, target).await?,
+		Subcommand::Invite { target } => {
+			let target = try_get_idty_index_by_name(&data, &target).await?;
+			invite_smith(&data, target).await?
+		}
 		Subcommand::Accept => accept_invitation(&data).await?,
-		Subcommand::Certify { target } => certify_smith(&data, target).await?,
+		Subcommand::Certify { target } => {
+			let target = try_get_idty_index_by_name(&data, &target).await?;
+			certify_smith(&data, target).await?
+		}
 	};
 
 	Ok(())
-- 
GitLab