Skip to content
Snippets Groups Projects
Select Git revision
  • bfb85c07289b4a9196ffe98702f6418a93582a4d
  • master default protected
  • elois-ci-refactor protected
  • gtest
  • hugo/gtest
  • json-output
  • nostr
  • 48-error-base-58-requirement-is-violated
  • no-rename
  • hugo/tx-comments
  • poka/dev
  • hugo/dev
  • tuxmain/mail
  • test-gtest
  • 0.4.3-gtest-RC1
  • 0.4.3-RC2
  • 0.4.3-RC1
  • 0.4.2
  • 0.4.1
  • 0.4.0
  • 0.3.0
  • 0.2.17
  • 0.2.16
  • 0.2.15
  • 0.2.14
  • 0.2.13
  • 0.2.12
  • 0.2.10
  • 0.2.9
  • 0.2.8
  • 0.2.7
  • 0.2.6
  • 0.2.5
33 results

cesium.rs

Blame
    • Hugo Trentesaux's avatar
      bfb85c07
      add Dockerfile (!31) · bfb85c07
      Hugo Trentesaux authored
      * working dockerfile
      
      * wip dockerfile
      
      TODO make duniter-polkadot-sdk repo lighter to avoid long download time in docker build
      
      (cherry picked from commit 5f4b716f)
      
      * update subxt and polkadot sdk
      bfb85c07
      History
      add Dockerfile (!31)
      Hugo Trentesaux authored
      * working dockerfile
      
      * wip dockerfile
      
      TODO make duniter-polkadot-sdk repo lighter to avoid long download time in docker build
      
      (cherry picked from commit 5f4b716f)
      
      * update subxt and polkadot sdk
    cesium.rs 2.05 KiB
    use crate::*;
    use bs58;
    
    /// define cesium subcommands
    #[derive(Clone, Default, Debug, clap::Parser)]
    pub enum Subcommand {
    	// Nothing
    	#[default]
    	#[clap(hide = true)]
    	Nothing,
    	/// Analyse a base58 pubkey and gives it in all its form
    	Pubkey { pubkey: String },
    	/// Prompt for cesium input
    	Prompt,
    }
    
    /// handle blockchain commands
    pub async fn handle_command(_data: Data, command: Subcommand) -> Result<(), GcliError> {
    	match command {
    		Subcommand::Nothing => {}
    		Subcommand::Pubkey { pubkey } => {
    			let raw_pubkey = bs58::decode(pubkey).into_vec().unwrap();
    			let raw_pubkey: [u8; 32] = if raw_pubkey.len() > 32 {
    				return Err(GcliError::Input("invalid pubkey size".to_string()));
    			} else {
    				[vec![0; 32 - raw_pubkey.len()], raw_pubkey]
    					.concat()
    					.try_into()
    					.unwrap()
    			};
    			println!("Pubkey (hex): 0x{}", hex::encode(raw_pubkey));
    			let address: AccountId = sp_core::ed25519::Public::from_raw(raw_pubkey).into();
    			println!("Address (SS58): {}", address);
    		}
    		Subcommand::Prompt => {
    			let keypair = prompt_secret_cesium();
    			println!("Pubkey: {}", bs58::encode(keypair.pkey).into_string());
    			let address: AccountId = keypair.pkey.into();
    			println!("Address: {}", address);
    		}
    	}
    	Ok(())
    }
    
    pub struct CesiumSigner<T: subxt::Config> {
    	account_id: T::AccountId,
    	keypair: nacl::sign::Keypair,
    }
    impl<T> CesiumSigner<T>
    where
    	T: subxt::Config,
    	T::AccountId: From<[u8; 32]>,
    {
    	pub fn new(keypair: nacl::sign::Keypair) -> Self {
    		Self {
    			account_id: T::AccountId::from(keypair.pkey),
    			keypair,
    		}
    	}
    }
    impl<T> subxt::tx::Signer<T> for CesiumSigner<T>
    where
    	T: subxt::Config,
    	T::Address: From<T::AccountId>,
    	T::Signature: From<sp_core::ed25519::Signature>,
    {
    	fn account_id(&self) -> T::AccountId {
    		self.account_id.clone()
    	}
    
    	fn address(&self) -> T::Address {
    		self.account_id.clone().into()
    	}
    
    	fn sign(&self, payload: &[u8]) -> T::Signature {
    		sp_core::ed25519::Signature::from_raw(
    			nacl::sign::signature(payload, &self.keypair.skey)
    				.unwrap()
    				.try_into()
    				.expect("could not read signature"),
    		)
    		.into()
    	}
    }