Skip to content
Snippets Groups Projects
Commit 8eda68de authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

wip refac smith cert

parent 79bf0446
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !5. Comments created here will be created in the context of that merge request.
use crate::*;
use crate::indexer::*; use crate::indexer::*;
use crate::*;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use std::collections::{hash_map, HashMap}; use std::collections::{hash_map, HashMap};
......
use crate::*;
use crate::indexer::*; use crate::indexer::*;
use crate::*;
use anyhow::Result; use anyhow::Result;
use sp_core::{sr25519::Pair, H256}; use sp_core::{sr25519::Pair, H256};
......
use crate::*;
use crate::indexer::*; use crate::indexer::*;
use crate::*;
use anyhow::Result; use anyhow::Result;
use futures::join; use futures::join;
...@@ -102,7 +102,9 @@ pub async fn monitor_expirations( ...@@ -102,7 +102,9 @@ pub async fn monitor_expirations(
let mut smith_certs_iter = client let mut smith_certs_iter = client
.storage() .storage()
.iter( .iter(
runtime::storage().smith_cert().storage_certs_removable_on(0), runtime::storage()
.smith_cert()
.storage_certs_removable_on(0),
10, 10,
Some(parent_hash), Some(parent_hash),
) )
...@@ -163,7 +165,9 @@ pub async fn monitor_expirations( ...@@ -163,7 +165,9 @@ pub async fn monitor_expirations(
let mut smith_membership_iter = client let mut smith_membership_iter = client
.storage() .storage()
.iter( .iter(
runtime::storage().smith_membership().memberships_expire_on(0), runtime::storage()
.smith_membership()
.memberships_expire_on(0),
10, 10,
Some(parent_hash), Some(parent_hash),
) )
......
use crate::*;
use crate::indexer::*; use crate::indexer::*;
use crate::*;
use crate::runtime::runtime_types::common_runtime::entities::IdtyData; use crate::runtime::runtime_types::common_runtime::entities::IdtyData;
use crate::runtime::runtime_types::pallet_identity::types::*; use crate::runtime::runtime_types::pallet_identity::types::*;
...@@ -101,7 +101,11 @@ pub async fn get_identity_by_index( ...@@ -101,7 +101,11 @@ pub async fn get_identity_by_index(
.await?) .await?)
} }
pub async fn create_identity(pair: Pair, client: Client, target: AccountId32) -> Result<TxProgress, subxt::Error> { pub async fn create_identity(
pair: Pair,
client: Client,
target: AccountId32,
) -> Result<TxProgress, subxt::Error> {
Ok(client Ok(client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
...@@ -112,7 +116,11 @@ pub async fn create_identity(pair: Pair, client: Client, target: AccountId32) -> ...@@ -112,7 +116,11 @@ pub async fn create_identity(pair: Pair, client: Client, target: AccountId32) ->
.await?) .await?)
} }
pub async fn confirm_identity(pair: Pair, client: Client, name: String) -> Result<TxProgress, subxt::Error> { pub async fn confirm_identity(
pair: Pair,
client: Client,
name: String,
) -> Result<TxProgress, subxt::Error> {
Ok(client Ok(client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
......
...@@ -50,8 +50,7 @@ pub async fn repart( ...@@ -50,8 +50,7 @@ pub async fn repart(
pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Result<()> { pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Result<()> {
let mut nonce = 0; let mut nonce = 0;
let mut pairs = let mut pairs = Vec::<(PairSigner<Runtime, Pair>, AccountId32)>::with_capacity(actual_repart);
Vec::<(PairSigner<Runtime, Pair>, AccountId32)>::with_capacity(actual_repart);
for i in 0..actual_repart { for i in 0..actual_repart {
let pair_i = pair let pair_i = pair
.derive(std::iter::once(DeriveJunction::hard::<u32>(i as u32)), None) .derive(std::iter::once(DeriveJunction::hard::<u32>(i as u32)), None)
......
...@@ -45,7 +45,9 @@ pub async fn consume_oneshot_account( ...@@ -45,7 +45,9 @@ pub async fn consume_oneshot_account(
dest.into(), dest.into(),
) )
} else { } else {
runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(dest.into()) runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
dest.into(),
)
}, },
), ),
&PairSigner::new(pair), &PairSigner::new(pair),
...@@ -111,7 +113,9 @@ pub async fn oneshot_account_balance(client: Client, account: AccountId32) -> Re ...@@ -111,7 +113,9 @@ pub async fn oneshot_account_balance(client: Client, account: AccountId32) -> Re
client client
.storage() .storage()
.fetch( .fetch(
&runtime::storage().oneshot_account().oneshot_accounts(&account), &runtime::storage()
.oneshot_account()
.oneshot_accounts(&account),
None None
) )
.await? .await?
......
...@@ -124,7 +124,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> { ...@@ -124,7 +124,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
let incoming_authorities = client let incoming_authorities = client
.storage() .storage()
.fetch( .fetch(
&runtime::storage().authority_members().incoming_authorities(), &runtime::storage()
.authority_members()
.incoming_authorities(),
Some(parent_hash), Some(parent_hash),
) )
.await? .await?
...@@ -144,7 +146,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> { ...@@ -144,7 +146,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
let outgoing_authorities = client let outgoing_authorities = client
.storage() .storage()
.fetch( .fetch(
&runtime::storage().authority_members().outgoing_authorities(), &runtime::storage()
.authority_members()
.outgoing_authorities(),
Some(parent_hash), Some(parent_hash),
) )
.await? .await?
...@@ -164,37 +168,8 @@ pub async fn online(client: Client, args: &Args) -> Result<()> { ...@@ -164,37 +168,8 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
Ok(()) Ok(())
} }
/// emit a new smith cert from signer's identity to target identity
pub async fn emit_cert(args: Args, receiver: u32) -> Result<()> {
// issuer key
let pair = get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap();
// connect to client
let client = Client::from_url(&args.url).await.unwrap();
// get issuer index
let issuer = commands::identity::get_idty_index_by_account_id(
client.clone(),
&AccountId32::from(pair.public()),
)
.await?
.ok_or(anyhow!("can not certify if not member"))?;
// submit and track certification
cert(client, pair, issuer, receiver).await?;
Ok(())
}
/// submit a certification and track progress /// submit a certification and track progress
async fn cert(client: Client, pair: Pair, issuer: u32, receiver: u32) -> Result<()> { pub async fn cert(client: Client, pair: Pair, issuer: u32, receiver: u32) -> Result<()> {
let mut progress = client let mut progress = client
.tx() .tx()
.sign_and_submit_then_watch( .sign_and_submit_then_watch(
......
...@@ -14,11 +14,12 @@ pub enum NeededKeys { ...@@ -14,11 +14,12 @@ pub enum NeededKeys {
Secret, Secret,
} }
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
pub enum SecretFormat { pub enum SecretFormat {
/// Raw 32B seed /// Raw 32B seed
Seed, Seed,
/// Substrate secret key or BIP39 mnemonic (optionally followed by derivation path) /// Substrate secret key or BIP39 mnemonic (optionally followed by derivation path)
#[default]
Substrate, Substrate,
} }
......
...@@ -52,7 +52,7 @@ impl From<u64> for Tip { ...@@ -52,7 +52,7 @@ impl From<u64> for Tip {
} }
} }
#[derive(Clone, Parser, Debug)] #[derive(Clone, Parser, Debug, Default)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
pub struct Args { pub struct Args {
#[clap(subcommand)] #[clap(subcommand)]
...@@ -81,11 +81,13 @@ pub struct Args { ...@@ -81,11 +81,13 @@ pub struct Args {
/// Data of current command /// Data of current command
/// can also include fetched information /// can also include fetched information
#[derive(Default)]
pub struct Data { pub struct Data {
pub args: Args, pub args: Args,
pub client: Option<Client>, pub client: Option<Client>,
pub address: Option<AccountId>, pub address: Option<AccountId>,
pub keypair: Option<Pair>, pub keypair: Option<Pair>,
pub idty_index: Option<u32>,
} }
// implement helper functions for Data // implement helper functions for Data
...@@ -94,12 +96,11 @@ impl Data { ...@@ -94,12 +96,11 @@ impl Data {
pub fn new(args: Args) -> Self { pub fn new(args: Args) -> Self {
Self { Self {
args, args,
client: None, ..Default::default()
address: None,
keypair: None,
} }
} }
// --- getters --- // --- getters ---
// the "unwrap" should not fail if data is well prepared
pub fn client(&self) -> Client { pub fn client(&self) -> Client {
self.client.clone().unwrap() self.client.clone().unwrap()
} }
...@@ -109,7 +110,11 @@ impl Data { ...@@ -109,7 +110,11 @@ impl Data {
pub fn keypair(&self) -> Pair { pub fn keypair(&self) -> Pair {
self.keypair.clone().unwrap() self.keypair.clone().unwrap()
} }
pub fn idty_index(&self) -> u32 {
self.idty_index.clone().unwrap()
}
// --- mutators --- // --- mutators ---
/// force an address if needed
pub fn build_address(mut self) -> Self { pub fn build_address(mut self) -> Self {
self.address = Some( self.address = Some(
get_keys( get_keys(
...@@ -124,24 +129,38 @@ impl Data { ...@@ -124,24 +129,38 @@ impl Data {
); );
self self
} }
/// force a keypair if needed
pub fn build_keypair(mut self) -> Self { pub fn build_keypair(mut self) -> Self {
self.keypair = Some( let (address, keypair) = get_keys(
get_keys(
self.args.secret_format, self.args.secret_format,
&self.args.address, &self.args.address,
&self.args.secret, &self.args.secret,
NeededKeys::Secret, NeededKeys::Secret,
) )
.expect("needed") .expect("needed");
.1 self.address = address;
.expect("needed"), self.keypair = keypair;
);
self self
} }
/// build a client from url
// TODO get client from a pre-defined list
pub async fn build_client(mut self) -> Self { pub async fn build_client(mut self) -> Self {
self.client = Some(Client::from_url(&self.args.url).await.expect("needed")); self.client = Some(Client::from_url(&self.args.url).await.expect("needed"));
self self
} }
/// get issuer index
/// needs address and client first
pub async fn fetch_idty_index(mut self) -> Result<Self, anyhow::Error> {
self.idty_index = Some(
commands::identity::get_idty_index_by_account_id(
self.client().clone(),
&self.address(),
)
.await?
.ok_or(anyhow::anyhow!("needs to be member to use this command"))?,
);
Ok(self)
}
} }
/// track progress of transaction on the network /// track progress of transaction on the network
...@@ -181,8 +200,11 @@ impl From<anyhow::Error> for GcliError { ...@@ -181,8 +200,11 @@ impl From<anyhow::Error> for GcliError {
} }
} }
#[derive(Clone, Debug, clap::Subcommand)] #[derive(Clone, Debug, clap::Subcommand, Default)]
pub enum Subcommand { pub enum Subcommand {
// TODO flodef
#[default]
GetBalance,
/// Create and certify an identity /// Create and certify an identity
/// ///
/// Caller must be member, and the target account must exist. /// Caller must be member, and the target account must exist.
...@@ -301,11 +323,11 @@ async fn main() -> Result<(), GcliError> { ...@@ -301,11 +323,11 @@ async fn main() -> Result<(), GcliError> {
let mut data = Data::new(args.clone()); let mut data = Data::new(args.clone());
match args.subcommand { match args.subcommand {
Subcommand::GetBalance => {}
Subcommand::CreateIdentity { target } => { Subcommand::CreateIdentity { target } => {
data = data.build_client().await.build_keypair(); data = data.build_client().await.build_keypair();
let progress = let progress =
commands::identity::create_identity(data.keypair(), data.client(), target) commands::identity::create_identity(data.keypair(), data.client(), target).await?;
.await?;
track_progress(progress).await? track_progress(progress).await?
} }
Subcommand::ConfirmIdentity { name } => { Subcommand::ConfirmIdentity { name } => {
...@@ -495,7 +517,15 @@ async fn main() -> Result<(), GcliError> { ...@@ -495,7 +517,15 @@ async fn main() -> Result<(), GcliError> {
) )
.await? .await?
} }
Subcommand::SmithCert { to } => commands::smith::emit_cert(args, to).await?, Subcommand::SmithCert { to } => {
data = data
.build_client()
.await
.build_keypair()
.fetch_idty_index()
.await?;
commands::smith::cert(data.client(), data.keypair(), data.idty_index(), to).await?
}
Subcommand::TechMembers => { Subcommand::TechMembers => {
commands::collective::technical_committee_members( commands::collective::technical_committee_members(
Client::from_url(&args.url).await.unwrap(), Client::from_url(&args.url).await.unwrap(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment