Newer
Older
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;
type SessionKeys = [u8; 128];
/// decode byte array into runtime session keys
// TODO find a way to avoid doing this manually by importing session keys trait implementation
fn session_keys_decode(session_keys: SessionKeys) -> RuntimeSessionKeys {
RuntimeSessionKeys {
grandpa: runtime::runtime_types::sp_consensus_grandpa::app::Public(
babe: runtime::runtime_types::sp_consensus_babe::app::Public(
im_online: runtime::runtime_types::pallet_im_online::sr25519::app_sr25519::Public(
authority_discovery: runtime::runtime_types::sp_authority_discovery::app::Public(
/// define smith subcommands
#[derive(Clone, Default, Debug, clap::Parser)]
pub enum Subcommand {
/// go online
GoOnline,
/// go offline
GoOffline,
/// Rotate and set session keys
UpdateKeys,
/// Set session keys
SetSessionKeys { session_keys: String },
/// List upcoming expirations that require an action
ShowExpire {
/// Show certs that expire within less than this number of blocks
#[clap(short, long, default_value_t = 100800)]
blocks: u32,
/// Show authorities that should rotate keys within less than this number of sessions
#[clap(short, long, default_value_t = 100)]
sessions: u32,
},
/// List online authorities
ShowOnline,
Nicolas80
committed
Invite {
#[clap(value_name = "USERNAME")]
target: String,
},
/// Accept invitation
Accept,
/// Certify smith
#[clap(alias = "cert")]
Nicolas80
committed
Certify {
#[clap(value_name = "USERNAME")]
target: String,
},
pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliError> {
let mut data = data.build_client().await?.build_indexer().await?;
match command {
Subcommand::GoOnline => {
Subcommand::SetSessionKeys { session_keys } => {
let session_keys = session_keys_decode(
hex::decode(session_keys)
.expect("wrong hexadecimal")
.try_into()
.expect("wrong format"),
); // decode session keys from hex string
set_session_keys(&data, session_keys).await?;
}
Subcommand::ShowExpire { blocks, sessions } => {
commands::expire::monitor_expirations(&data, blocks, sessions).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 } => {
let target = try_get_idty_index_by_name(&data, &target).await?;
certify_smith(&data, target).await?
}
pub async fn rotate_keys(data: &Data) -> Result<SessionKeys, GcliError> {
data.legacy_rpc_methods()
.await
.author_rotate_keys()
"Please make sure you are connected to your validator node with the unsafe RPC \
API enabled {e}"
.map_err(|e| GcliError::Duniter(format!("Session keys have wrong length: {:?}", e)))
session_keys: RuntimeSessionKeys,
) -> Result<TxProgress, subxt::Error> {
submit_call::<StaticPayload<runtime::authority_members::calls::types::SetSessionKeys>>(
data,
&runtime::tx()
.authority_members()
.set_session_keys(session_keys),
)
.await
// use runtime::runtime_types::sp_consensus_grandpa::app::Public
pub async fn update_session_keys(data: &Data) -> Result<(), GcliError> {
// manual session key conversion
let session_keys = session_keys_decode(session_keys);
let progress = set_session_keys(data, session_keys).await?;
if data.args.no_wait {
return Ok(());
}
let _ = track_progress(progress).await?; // TODO
Ok(())
pub async fn go_online(data: &Data) -> Result<(), GcliError> {
if data
.client()
.at_latest()
.await?
.fetch(&runtime::storage().session().next_keys(data.address()))
return Err(GcliError::Logic(
"This account has not set session keys!".to_string(),
));
submit_call_and_look_event::<
runtime::authority_members::events::MemberGoOnline,
StaticPayload<runtime::authority_members::calls::types::GoOnline>,
>(data, &runtime::tx().authority_members().go_online())
pub async fn go_offline(data: &Data) -> Result<(), subxt::Error> {
submit_call_and_look_event::<
runtime::authority_members::events::MemberGoOffline,
StaticPayload<runtime::authority_members::calls::types::GoOffline>,
>(data, &runtime::tx().authority_members().go_offline())
.await
pub async fn online(data: &Data) -> Result<(), subxt::Error> {
let online_authorities = client
.at_latest()
.await?
.fetch(&runtime::storage().authority_members().online_authorities())
.await?
.unwrap_or_default();
let incoming_authorities = client
.storage()
&runtime::storage()
.authority_members()
.incoming_authorities(),
)
.await?
.unwrap_or_default();
let outgoing_authorities = client
.storage()
&runtime::storage()
.authority_members()
.outgoing_authorities(),
if let Some(indexer) = &data.indexer {
let mut names = HashMap::<IdtyId, String>::new();
indexer
.names_by_indexes(&online_authorities)
.await
.into_iter()
.for_each(|e| {
names.insert(e.0, e.1);
});
println!("Online:");
println!(
"{}",
online_authorities
.iter()
.map(|n| n.to_string())
.unwrap_or(format!("{} <no name found>", i)))
.collect::<Vec<String>>()
.join(", ")
);
println!("Incoming:");
"{}",
incoming_authorities
.iter()
.map(|n| n.to_string())
.unwrap_or(format!("{} <no name found>", i)))
.collect::<Vec<String>>()
println!("Outgoing:");
println!(
"{}",
outgoing_authorities
.iter()
.map(|n| n.to_string())
.unwrap_or(format!("{} <no name found>", i)))
.collect::<Vec<String>>()
.join(", ")
);
} else {
println!("Online:");
println!("{online_authorities:?}");
println!("Incoming:");
println!("{incoming_authorities:?}");
println!("Outgoing:");
println!("{outgoing_authorities:?}");
/// invite identity to join smith
pub async fn invite_smith(data: &Data, target: IdtyId) -> Result<(), subxt::Error> {
submit_call_and_look_event::<
runtime::smith_members::events::InvitationSent,
StaticPayload<runtime::smith_members::calls::types::InviteSmith>,
>(data, &runtime::tx().smith_members().invite_smith(target))
.await
}
/// accept invitation
pub async fn accept_invitation(data: &Data) -> Result<(), subxt::Error> {
submit_call_and_look_event::<
runtime::smith_members::events::InvitationAccepted,
StaticPayload<runtime::smith_members::calls::types::AcceptInvitation>,
>(data, &runtime::tx().smith_members().accept_invitation())
.await
}
/// invite identity to join smith
pub async fn certify_smith(data: &Data, target: IdtyId) -> Result<(), subxt::Error> {
submit_call_and_look_event::<
runtime::smith_members::events::SmithCertAdded,
StaticPayload<runtime::smith_members::calls::types::CertifySmith>,
>(data, &runtime::tx().smith_members().certify_smith(target))
.await