Skip to content
Snippets Groups Projects
Verified Commit 45cce8b9 authored by Pascal Engélibert's avatar Pascal Engélibert :bicyclist:
Browse files

feat: identity command

parent 15b907eb
No related branches found
No related tags found
No related merge requests found
[package] [package]
authors = ['librelois <c@elo.tf>'] authors = ["librelois <c@elo.tf>", "tuxmain <tuxmain@zettascript.org>"]
edition = "2021" edition = "2021"
license = 'AGPL-3.0' license = "AGPL-3.0"
name = "gcli" name = "gcli"
repository = 'https://git.duniter.org/clients/rust/gcli-v2s' repository = "https://git.duniter.org/clients/rust/gcli-v2s"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
......
...@@ -18,7 +18,7 @@ Send 10 ĞD from Alice to Ferdie: ...@@ -18,7 +18,7 @@ Send 10 ĞD from Alice to Ferdie:
List certifications and session keys that will expire within one month: List certifications and session keys that will expire within one month:
cargo run -- --url wss://gdev.librelois.fr:443/ws expire --blocks 432000 cargo run -- --url wss://gdev.p2p.legal:443/ws expire --blocks 432000
### Smith ### Smith
...@@ -35,6 +35,6 @@ Now the command `duniter-rpc` will open an SSH session and a bridge to your RPC ...@@ -35,6 +35,6 @@ Now the command `duniter-rpc` will open an SSH session and a bridge to your RPC
When your node is ready to forge blocks, rotate keys and go online: When your node is ready to forge blocks, rotate keys and go online:
```bash ```bash
gcli update-keys gcli --secret "my secret phrase" update-keys
gcli --secret "my secret phrase" go-online gcli --secret "my secret phrase" go-online
``` ```
query IdentityNameByPubkey($pubkey: String!) { query IdentityNameByPubkey($pubkey: String!) {
identity_by_pk(id: $pubkey) { identity(where: {pubkey: {_eq: $pubkey}}) {
name name
} }
} }
query IdentityPubkeyByName($name: String!) {
identity_by_pk(name: $name) {
pubkey
}
}
source diff could not be displayed: it is too large. Options to address this: view the blob.
...@@ -8,11 +8,11 @@ use std::collections::{hash_map, HashMap}; ...@@ -8,11 +8,11 @@ use std::collections::{hash_map, HashMap};
pub struct IdentityCache<'a> { pub struct IdentityCache<'a> {
client: &'a Client, client: &'a Client,
identities: HashMap<u32, String>, identities: HashMap<u32, String>,
indexer: Option<(&'a reqwest::Client, &'a str)>, indexer: Option<Indexer<'a>>,
} }
impl<'a> IdentityCache<'a> { impl<'a> IdentityCache<'a> {
pub fn new(client: &'a Client, indexer: Option<(&'a reqwest::Client, &'a str)>) -> Self { pub fn new(client: &'a Client, indexer: Option<Indexer<'a>>) -> Self {
Self { Self {
client, client,
identities: HashMap::new(), identities: HashMap::new(),
...@@ -40,35 +40,18 @@ impl<'a> IdentityCache<'a> { ...@@ -40,35 +40,18 @@ impl<'a> IdentityCache<'a> {
.ok_or_else(|| anyhow!("Identity {} not found", identity_id))? .ok_or_else(|| anyhow!("Identity {} not found", identity_id))?
.owner_key .owner_key
.to_string(); .to_string();
if let Some((gql_client, gql_url)) = self.indexer { format!(
if let Ok(resp) = post_graphql::<IdentityNameByPubkey, _>( "“ {} ”",
gql_client, if let Some(indexer) = &self.indexer {
gql_url, if let Ok(Some(username)) = indexer.username_by_pubkey(&pubkey).await {
identity_name_by_pubkey::Variables { username
pubkey: pubkey.clone(),
},
)
.await
{
if let Some(data) = resp.data {
if let Some(identity) = data.identity_by_pk {
if let Some(name) = identity.name {
format!("“ {name} ”")
} else {
pubkey
}
} else {
pubkey
}
} else { } else {
pubkey pubkey
} }
} else { } else {
pubkey pubkey
} }
} else { )
pubkey
}
}) })
.clone(), .clone(),
}) })
......
pub mod expire; pub mod expire;
pub mod identity;
pub mod net_test; pub mod net_test;
pub mod oneshot; pub mod oneshot;
pub mod revocation; pub mod revocation;
......
use crate::{cache, gdev, Args, Client}; use crate::{cache, gdev, indexer::*, Args, Client};
use anyhow::Result; use anyhow::Result;
use futures::join; use futures::join;
...@@ -40,7 +40,10 @@ pub async fn monitor_expirations( ...@@ -40,7 +40,10 @@ pub async fn monitor_expirations(
if args.no_indexer { if args.no_indexer {
None None
} else { } else {
Some((&gql_client, &args.indexer)) Some(Indexer {
gql_client,
gql_url: &args.indexer,
})
}, },
); );
......
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(())
}
pub use graphql_client::{reqwest::post_graphql, GraphQLQuery}; pub use graphql_client::{reqwest::post_graphql, GraphQLQuery};
use anyhow::Result;
#[derive(GraphQLQuery)] #[derive(GraphQLQuery)]
#[graphql( #[graphql(
schema_path = "res/indexer-schema.graphql", schema_path = "res/indexer-schema.json",
query_path = "res/indexer-queries.graphql" query_path = "res/indexer-queries.graphql"
)] )]
pub struct IdentityNameByPubkey; pub struct IdentityNameByPubkey;
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "res/indexer-schema.json",
query_path = "res/indexer-queries.graphql"
)]
pub struct IdentityPubkeyByName;
pub struct Indexer<'a> {
pub gql_client: reqwest::Client,
pub gql_url: &'a str,
}
impl<'a> Indexer<'a> {
pub async fn username_by_pubkey(&self, pubkey: &str) -> Result<Option<String>> {
Ok(post_graphql::<IdentityNameByPubkey, _>(
&self.gql_client,
self.gql_url,
identity_name_by_pubkey::Variables {
pubkey: pubkey.to_string(),
},
)
.await?
.data
.and_then(|data| data.identity.into_iter().next().map(|idty| idty.name)))
}
pub async fn pubkey_by_username(&self, username: &str) -> Result<Option<String>> {
Ok(post_graphql::<IdentityPubkeyByName, _>(
&self.gql_client,
self.gql_url,
identity_pubkey_by_name::Variables {
name: username.to_string(),
},
)
.await?
.data
.and_then(|data| data.identity_by_pk.map(|idty| idty.pubkey)))
}
}
...@@ -57,7 +57,11 @@ pub struct Args { ...@@ -57,7 +57,11 @@ pub struct Args {
pub subcommand: Subcommand, pub subcommand: Subcommand,
/// Indexer URL /// Indexer URL
#[clap(short, long, default_value = "https://idx.gdev.cgeek.fr/v1/graphql")] #[clap(
short,
long,
default_value = "https://gdev-indexer.p2p.legal/v1/graphql"
)]
indexer: String, indexer: String,
/// Do not use indexer /// Do not use indexer
#[clap(long)] #[clap(long)]
...@@ -103,6 +107,15 @@ pub enum Subcommand { ...@@ -103,6 +107,15 @@ pub enum Subcommand {
#[clap(short, long, default_value_t = 100)] #[clap(short, long, default_value_t = 100)]
sessions: u32, sessions: u32,
}, },
/// Fetch identity
Identity {
#[clap(short = 'p', long = "pubkey")]
account_id: Option<sp_core::crypto::AccountId32>,
#[clap(short = 'i', long = "identity")]
identity_id: Option<u32>,
#[clap(short = 'u', long = "username")]
username: Option<String>,
},
/// Generate a revocation document for the provided account /// Generate a revocation document for the provided account
GenRevocDoc, GenRevocDoc,
GoOffline, GoOffline,
...@@ -235,6 +248,20 @@ async fn main() -> Result<()> { ...@@ -235,6 +248,20 @@ async fn main() -> Result<()> {
Subcommand::Expire { blocks, sessions } => { Subcommand::Expire { blocks, sessions } => {
commands::expire::monitor_expirations(client, blocks, sessions, &args).await? commands::expire::monitor_expirations(client, blocks, sessions, &args).await?
} }
Subcommand::Identity {
ref account_id,
identity_id,
ref username,
} => {
commands::identity::get_identity(
client,
account_id.clone(),
identity_id,
username.clone(),
&args,
)
.await?
}
Subcommand::GenRevocDoc => { Subcommand::GenRevocDoc => {
commands::revocation::gen_revoc_doc( commands::revocation::gen_revoc_doc(
&client, &client,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment