Newer
Older
use crate::commands::revocation::get_revoc_doc;
use crate::runtime::runtime_types::common_runtime::entities::IdtyData;
use crate::runtime::runtime_types::pallet_identity::types::*;
use crate::runtime::runtime_types::sp_core::sr25519::Signature;
use crate::runtime::runtime_types::sp_runtime::MultiSignature;
use sp_core::{crypto::AccountId32, sr25519::Pair};
use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner};
client: Client,
mut account_id: Option<AccountId32>,
mut identity_id: Option<u32>,
mut username: Option<String>,
args: &Args,
let indexer = if args.no_indexer {
None
} else {
Some(Indexer {
gql_client: reqwest::Client::builder()
.user_agent("gcli/0.1.0")
.build()?,
// fetch missing information
match (&account_id, identity_id, &username) {
(None, Some(identity_id), None) => {
account_id = get_identity_by_index(client, identity_id)
.await?
.map(|idty| idty.owner_key);
(Some(account_id), None, None) => {
identity_id = get_idty_index_by_account_id(client, account_id).await?;
}
(None, None, Some(username)) => {
let indexer = indexer.as_ref().ok_or(anyhow!(
"Cannot fetch identity from username without indexer."
))?;
if let Some(pubkey) = indexer.pubkey_by_username(username).await? {
// convert string to accountid
let fetched_account_id = AccountId32::from_str(&pubkey).map_err(|e| anyhow!(e))?;
// in the future, also ask indexer the identity index
identity_id = get_idty_index_by_account_id(client, &fetched_account_id).await?;
account_id = Some(fetched_account_id);
} else {
return Err(anyhow!("no identity found for this username"));
}
}
_ => {
return Err(anyhow!(
"One and only one argument is needed to fetch the identity."
));
}
};
println!(
"Account id: {}",
account_id
.as_ref()
.map_or(String::new(), AccountId32::to_string)
);
println!(
"Identity id: {}",
identity_id.map_or(String::new(), |identity_id| format!("{identity_id}"))
);
if let (Some(indexer), Some(account_id), None) = (&indexer, &account_id, &username) {
username = indexer.username_by_pubkey(&account_id.to_string()).await?;
}
println!("Username: {}", username.unwrap_or_default());
pub async fn get_idty_index_by_account_id(
client: Client,
account_id: &AccountId32,
) -> Result<Option<u32>> {
Ok(client
.storage()
.fetch(
None,
)
.await?)
}
pub async fn get_identity_by_index(
client: Client,
idty_index: u32,
) -> Result<Option<IdtyValue<u32, AccountId32, IdtyData>>> {
Ok(client
.storage()
.fetch(&runtime::storage().identity().identities(idty_index), None)
pub async fn create_identity(
pair: Pair,
client: Client,
target: AccountId32,
) -> Result<TxProgress, subxt::Error> {
.tx()
.sign_and_submit_then_watch(
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
pub async fn confirm_identity(
pair: Pair,
client: Client,
name: String,
) -> Result<TxProgress, subxt::Error> {
.tx()
.sign_and_submit_then_watch(
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
.await
}
// TODO : use Result<TxProgress, subxt::Error> instead of Result<()>
pub async fn revoke_identity(pair: Pair, client: Client, account_id: AccountId32) -> Result<()> {
let (idty_index, signature) = get_revoc_doc(&client, &pair).await?;
// Transform signature to MultiSignature
// TODO: this is a hack, we should be able to use the signature directly
let signature = Signature(signature.0);
let multisign = MultiSignature::Sr25519(signature);
client
.tx()
.sign_and_submit_then_watch(
&runtime::tx()
.identity()
.revoke_identity(idty_index, account_id, multisign),
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
.await?
.wait_for_in_block()
.await?
.wait_for_success()
.await?;
Ok(())