From d8ba05d3e70f5389714d86b81c7d7599aa96d3fe Mon Sep 17 00:00:00 2001
From: tuxmain <tuxmain@zettascript.org>
Date: Mon, 27 Mar 2023 12:44:14 +0200
Subject: [PATCH] feat: create-identity, confirm-identity

---
 src/commands/expire.rs   |  7 +++++--
 src/commands/identity.rs | 29 ++++++++++++++++++++++++++-
 src/main.rs              | 42 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/src/commands/expire.rs b/src/commands/expire.rs
index 207bfbf..d051ea8 100644
--- a/src/commands/expire.rs
+++ b/src/commands/expire.rs
@@ -29,8 +29,8 @@ pub async fn monitor_expirations(
 			.storage()
 			.fetch(&addr_current_session, Some(parent_hash),)
 	);
-	let current_block = current_block?.unwrap();
-	let current_session = current_session?.unwrap();
+	let current_block = current_block?.unwrap_or_default();
+	let current_session = current_session?.unwrap_or_default();
 
 	let end_block = current_block + blocks;
 	let end_session = current_session + sessions;
@@ -152,6 +152,9 @@ pub async fn monitor_expirations(
 	while let Some((k, v)) = basic_membership_iter.next().await? {
 		let block_number = u32::from_le_bytes(k.as_ref()[40..44].try_into().unwrap());
 		if block_number < end_block {
+			if block_number < current_block {
+				dbg!((block_number, current_block));
+			}
 			basic_memberships.insert(block_number - current_block, v);
 		}
 	}
diff --git a/src/commands/identity.rs b/src/commands/identity.rs
index 0fa2755..6cde145 100644
--- a/src/commands/identity.rs
+++ b/src/commands/identity.rs
@@ -1,8 +1,9 @@
 use crate::{gdev, indexer::*, Args, Client};
 
 use anyhow::{anyhow, Result};
-use sp_core::crypto::AccountId32;
+use sp_core::{crypto::AccountId32, sr25519::Pair};
 use std::str::FromStr;
+use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner};
 
 pub async fn get_identity(
 	client: Client,
@@ -87,3 +88,29 @@ pub async fn get_identity(
 
 	Ok(())
 }
+
+pub async fn create_identity(pair: Pair, client: Client, target: AccountId32) -> Result<()> {
+	client
+		.tx()
+		.sign_and_submit_then_watch(
+			&gdev::tx().identity().create_identity(target),
+			&PairSigner::new(pair),
+			BaseExtrinsicParamsBuilder::new(),
+		)
+		.await?;
+
+	Ok(())
+}
+
+pub async fn confirm_identity(pair: Pair, client: Client, name: String) -> Result<()> {
+	client
+		.tx()
+		.sign_and_submit_then_watch(
+			&gdev::tx().identity().confirm_identity(name),
+			&PairSigner::new(pair),
+			BaseExtrinsicParamsBuilder::new(),
+		)
+		.await?;
+
+	Ok(())
+}
diff --git a/src/main.rs b/src/main.rs
index 5ce099e..7b451af 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -82,6 +82,18 @@ pub struct Args {
 
 #[derive(Debug, clap::Subcommand)]
 pub enum Subcommand {
+	/// Confirm an identity
+	///
+	/// To be called by the certified not-yet-member account, to become member.
+	ConfirmIdentity {
+		name: String,
+	},
+	/// Create and certify an identity
+	///
+	/// Caller must be member, and the target account must exist.
+	CreateIdentity {
+		target: sp_core::crypto::AccountId32,
+	},
 	CreateOneshot {
 		balance: u64,
 		dest: sp_core::crypto::AccountId32,
@@ -196,6 +208,36 @@ async fn main() -> Result<()> {
 	}*/
 
 	match args.subcommand {
+		Subcommand::ConfirmIdentity { name } => {
+			commands::identity::confirm_identity(
+				get_keys(
+					args.secret_format,
+					&args.address,
+					&args.secret,
+					NeededKeys::Secret,
+				)?
+				.1
+				.unwrap(),
+				Client::from_url(&args.url).await.unwrap(),
+				name,
+			)
+			.await?
+		}
+		Subcommand::CreateIdentity { target } => {
+			commands::identity::create_identity(
+				get_keys(
+					args.secret_format,
+					&args.address,
+					&args.secret,
+					NeededKeys::Secret,
+				)?
+				.1
+				.unwrap(),
+				Client::from_url(&args.url).await.unwrap(),
+				target,
+			)
+			.await?
+		}
 		Subcommand::CreateOneshot { balance, dest } => {
 			commands::oneshot::create_oneshot_account(
 				get_keys(
-- 
GitLab