Skip to content
Snippets Groups Projects
cesium.rs 1.6 KiB
Newer Older
use crate::*;
use bs58;

/// define cesium subcommands
#[derive(Clone, Default, Debug, clap::Parser)]
pub enum Subcommand {
	// Nothing
	#[default]
	#[clap(hide = true)]
	Nothing,
	/// Generate key pair with old style unsafe scrypt
	Pubkey { id: String, pass: String },
	/// Prompt
	Prompt,
}

/// handle blockchain commands
pub async fn handle_command(_data: Data, command: Subcommand) -> Result<(), GcliError> {
	match command {
		Subcommand::Nothing => {}
		Subcommand::Pubkey { id, pass } => {
			let keypair = pair_from_cesium(id, pass);
			println!(
				"Pubkey: {}",
				bs58::encode(keypair.pkey).into_string()
			);
		}
		Subcommand::Prompt => {
			let keypair = prompt_secret_cesium();
			println!(
				"Pubkey: {}",
				bs58::encode(keypair.pkey).into_string()
			);
		}
	}
	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(
			nacl::sign::signature(payload, &self.keypair.skey)
				.unwrap()
				.try_into()
				.expect("could not read signature"),
		)
		.into()
	}
}