Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use crate::{gdev, indexer::*, Args, Client};
use anyhow::{anyhow, Result};
use sp_core::crypto::AccountId32;
use std::str::FromStr;
pub async fn get_identity(
client: Client,
mut account_id: Option<AccountId32>,
mut identity_id: Option<u32>,
mut username: Option<String>,
args: &Args,
) -> Result<()> {
let parent_hash = client
.storage()
.fetch(&gdev::storage().system().parent_hash(), None)
.await?
.unwrap();
let gql_client = reqwest::Client::builder()
.user_agent("gcli/0.1.0")
.build()?;
let indexer = if args.no_indexer {
None
} else {
Some(Indexer {
gql_client,
gql_url: &args.indexer,
})
};
if let Some(account_id) = &account_id {
identity_id = client
.storage()
.fetch(
&gdev::storage().identity().identity_index_of(account_id),
Some(parent_hash),
)
.await?;
} else if let Some(identity_id) = &identity_id {
account_id = client
.storage()
.fetch(
&gdev::storage().identity().identities(identity_id),
Some(parent_hash),
)
.await?
.map(|idty| idty.owner_key);
} else if let Some(username) = &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? {
let some_account_id = AccountId32::from_str(&pubkey).map_err(|e| anyhow!(e))?;
identity_id = client
.storage()
.fetch(
&gdev::storage()
.identity()
.identity_index_of(&some_account_id),
Some(parent_hash),
)
.await?;
account_id = Some(some_account_id);
}
} else {
return Err(anyhow!("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());
Ok(())
}