centichain_keypair/
lib.rs1use bip39::Mnemonic;
2use sp_core::{
3 ed25519::{self, Public, Signature},
4 Pair,
5};
6
7 pub struct CentichainKey;
8
9impl CentichainKey {
10 pub fn generate() -> (String, Public) {
16 let entropy = rand::random::<[u8; 32]>();
17 let mnemonic = Mnemonic::from_entropy_in(bip39::Language::English, &entropy)
18 .expect("Valid entropy should generate valid mnemonic");
19 let keypair = ed25519::Pair::from_phrase(&mnemonic.to_string(), None)
20 .expect("Valid mnemonic should generate valid keypair");
21 (mnemonic.to_string(), keypair.0.public())
22 }
23
24 pub fn check_phrase<'a>(seed_phrase: &String) -> Result<Public, &'a str> {
27 let keypair = ed25519::Pair::from_phrase(seed_phrase, None);
28 match keypair {
29 Ok(pair) => Ok(pair.0.public()),
30 Err(_) => Err("Your seed phrase is wrong!"),
31 }
32 }
33
34 pub fn signing<'a>(seed_phrase: &String, message: &String) -> Result<Signature, &'a str> {
37 let keypair = ed25519::Pair::from_phrase(&seed_phrase, None);
38 match keypair {
39 Ok(pair) => Ok(pair.0.sign(message.as_bytes())),
40 Err(_) => Err("Your seed phrase is wrong!"),
41 }
42 }
43}