pop_common/
signer.rs

1// SPDX-License-Identifier: GPL-3.0
2
3use crate::errors::Error;
4use subxt_signer::{sr25519::Keypair, SecretUri};
5
6/// Create a keypair from a secret URI.
7///
8/// # Arguments
9/// `suri` - Secret URI string used to generate the `Keypair`.
10pub fn create_signer(suri: &str) -> Result<Keypair, Error> {
11	let uri = <SecretUri as std::str::FromStr>::from_str(suri)
12		.map_err(|e| Error::ParseSecretURI(format!("{}", e)))?;
13	let keypair = Keypair::from_uri(&uri).map_err(|e| Error::KeyPairCreation(format!("{}", e)))?;
14	Ok(keypair)
15}
16
17#[cfg(test)]
18mod tests {
19	use super::*;
20	use anyhow::Result;
21
22	#[test]
23	fn create_signer_works() -> Result<(), Error> {
24		let keypair = create_signer("//Alice")?;
25		assert_eq!(
26			keypair.public_key().to_account_id().to_string(),
27			"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" //Alice account
28		);
29		Ok(())
30	}
31
32	#[test]
33	fn create_signer_fails_wrong_key() -> Result<(), Error> {
34		assert!(matches!(create_signer("11111"), Err(Error::KeyPairCreation(..))));
35		Ok(())
36	}
37}