pop-common 0.14.0

Library that provides a collection of essential utilities and shared functionality for pop.
Documentation
// SPDX-License-Identifier: GPL-3.0

use crate::errors::Error;
use subxt_signer::{SecretUri, sr25519::Keypair};

/// Create a keypair from a secret URI.
///
/// # Arguments
/// `suri` - Secret URI string used to generate the `Keypair`.
pub fn create_signer(suri: &str) -> Result<Keypair, Error> {
	let uri = <SecretUri as std::str::FromStr>::from_str(suri)
		.map_err(|e| Error::ParseSecretURI(format!("{}", e)))?;
	let keypair = Keypair::from_uri(&uri).map_err(|e| Error::KeyPairCreation(format!("{}", e)))?;
	Ok(keypair)
}

#[cfg(test)]
mod tests {
	use super::*;
	use anyhow::Result;

	#[test]
	fn create_signer_works() -> Result<(), Error> {
		let keypair = create_signer("//Alice")?;
		assert_eq!(
			keypair.public_key().to_account_id().to_string(),
			"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" //Alice account
		);
		Ok(())
	}

	#[test]
	fn create_signer_fails_wrong_key() -> Result<(), Error> {
		assert!(matches!(create_signer("11111"), Err(Error::KeyPairCreation(..))));
		Ok(())
	}
}