use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
use hex::FromHex;
use schnorrkel::{
derive::{ChainCode, Derivation},
ExpansionMode, MiniSecretKey,
};
use secrecy::ExposeSecret;
const SEED_LENGTH: usize = schnorrkel::keys::MINI_SECRET_KEY_LENGTH;
const SIGNING_CTX: &[u8] = b"substrate";
pub type Seed = [u8; SEED_LENGTH];
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Signature(pub [u8; 64]);
impl AsRef<[u8]> for Signature {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
pub struct PublicKey(pub [u8; 32]);
impl AsRef<[u8]> for PublicKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Debug, Clone)]
pub struct Keypair(schnorrkel::Keypair);
impl Keypair {
pub fn from_uri(uri: &SecretUri) -> Result<Self, Error> {
let SecretUri {
junctions,
phrase,
password,
} = uri;
let key = if let Some(hex_str) = phrase.expose_secret().strip_prefix("0x") {
let seed = Seed::from_hex(hex_str)?;
Self::from_seed(seed)?
} else {
let phrase = bip39::Mnemonic::parse(phrase.expose_secret().as_str())?;
let pass_str = password.as_ref().map(|p| p.expose_secret().as_str());
Self::from_phrase(&phrase, pass_str)?
};
Ok(key.derive(junctions.iter().copied()))
}
pub fn from_phrase(mnemonic: &bip39::Mnemonic, password: Option<&str>) -> Result<Self, Error> {
let big_seed = seed_from_entropy(&mnemonic.to_entropy(), password.unwrap_or(""))
.ok_or(Error::InvalidSeed)?;
let seed: Seed = big_seed[..SEED_LENGTH]
.try_into()
.expect("should be valid Seed");
Self::from_seed(seed)
}
pub fn from_seed(seed: Seed) -> Result<Self, Error> {
let keypair = MiniSecretKey::from_bytes(&seed)
.map_err(|_| Error::InvalidSeed)?
.expand_to_keypair(ExpansionMode::Ed25519);
Ok(Keypair(keypair))
}
pub fn derive<Js: IntoIterator<Item = DeriveJunction>>(&self, junctions: Js) -> Self {
let init = self.0.secret.clone();
let result = junctions.into_iter().fold(init, |acc, j| match j {
DeriveJunction::Soft(cc) => acc.derived_key_simple(ChainCode(cc), []).0,
DeriveJunction::Hard(cc) => {
let seed = acc.hard_derive_mini_secret_key(Some(ChainCode(cc)), b"").0;
seed.expand(ExpansionMode::Ed25519)
}
});
Self(result.into())
}
pub fn public_key(&self) -> PublicKey {
PublicKey(self.0.public.to_bytes())
}
pub fn sign(&self, message: &[u8]) -> Signature {
let context = schnorrkel::signing_context(SIGNING_CTX);
let signature = self.0.sign(context.bytes(message));
Signature(signature.to_bytes())
}
}
pub fn verify<M: AsRef<[u8]>>(sig: &Signature, message: M, pubkey: &PublicKey) -> bool {
let Ok(signature) = schnorrkel::Signature::from_bytes(&sig.0) else {
return false;
};
let Ok(public) = schnorrkel::PublicKey::from_bytes(&pubkey.0) else {
return false;
};
public
.verify_simple(SIGNING_CTX, message.as_ref(), &signature)
.is_ok()
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Invalid seed (was it the wrong length?)")]
InvalidSeed,
#[error("Cannot parse phrase: {0}")]
Phrase(#[from] bip39::Error),
#[error("Cannot parse hex string: {0}")]
Hex(#[from] hex::FromHexError),
}
pub mod dev {
use super::*;
use std::str::FromStr;
once_static_cloned! {
pub fn alice() -> Keypair {
Keypair::from_uri(&SecretUri::from_str("//Alice").unwrap()).unwrap()
}
pub fn bob() -> Keypair {
Keypair::from_uri(&SecretUri::from_str("//Bob").unwrap()).unwrap()
}
pub fn charlie() -> Keypair {
Keypair::from_uri(&SecretUri::from_str("//Charlie").unwrap()).unwrap()
}
pub fn dave() -> Keypair {
Keypair::from_uri(&SecretUri::from_str("//Dave").unwrap()).unwrap()
}
pub fn eve() -> Keypair {
Keypair::from_uri(&SecretUri::from_str("//Eve").unwrap()).unwrap()
}
pub fn ferdie() -> Keypair {
Keypair::from_uri(&SecretUri::from_str("//Ferdie").unwrap()).unwrap()
}
pub fn one() -> Keypair {
Keypair::from_uri(&SecretUri::from_str("//One").unwrap()).unwrap()
}
pub fn two() -> Keypair {
Keypair::from_uri(&SecretUri::from_str("//Two").unwrap()).unwrap()
}
}
}
#[cfg(feature = "subxt")]
#[cfg_attr(docsrs, doc(cfg(feature = "subxt")))]
mod subxt_compat {
use super::*;
use subxt::config::Config;
use subxt::tx::Signer as SignerT;
use subxt::utils::{AccountId32, MultiAddress, MultiSignature};
impl From<Signature> for MultiSignature {
fn from(value: Signature) -> Self {
MultiSignature::Sr25519(value.0)
}
}
impl From<PublicKey> for AccountId32 {
fn from(value: PublicKey) -> Self {
value.to_account_id()
}
}
impl<T> From<PublicKey> for MultiAddress<AccountId32, T> {
fn from(value: PublicKey) -> Self {
value.to_address()
}
}
impl PublicKey {
pub fn to_account_id(self) -> AccountId32 {
AccountId32(self.0)
}
pub fn to_address<T>(self) -> MultiAddress<AccountId32, T> {
MultiAddress::Id(self.to_account_id())
}
}
impl<T: Config> SignerT<T> for Keypair
where
T::AccountId: From<PublicKey>,
T::Address: From<PublicKey>,
T::Signature: From<Signature>,
{
fn account_id(&self) -> T::AccountId {
self.public_key().into()
}
fn address(&self) -> T::Address {
self.public_key().into()
}
fn sign(&self, signer_payload: &[u8]) -> T::Signature {
self.sign(signer_payload).into()
}
}
}
#[cfg(test)]
mod test {
use std::str::FromStr;
use super::*;
use sp_core::crypto::Pair as _;
use sp_core::sr25519::Pair as SpPair;
#[test]
fn check_from_phrase_matches() {
for _ in 0..20 {
let (sp_pair, phrase, _seed) = SpPair::generate_with_phrase(None);
let phrase = bip39::Mnemonic::parse(phrase).expect("valid phrase expected");
let pair = Keypair::from_phrase(&phrase, None).expect("should be valid");
assert_eq!(sp_pair.public().0, pair.public_key().0);
}
}
#[test]
fn check_from_phrase_with_password_matches() {
for _ in 0..20 {
let (sp_pair, phrase, _seed) = SpPair::generate_with_phrase(Some("Testing"));
let phrase = bip39::Mnemonic::parse(phrase).expect("valid phrase expected");
let pair = Keypair::from_phrase(&phrase, Some("Testing")).expect("should be valid");
assert_eq!(sp_pair.public().0, pair.public_key().0);
}
}
#[test]
fn check_from_secret_uri_matches() {
let uri_paths = [
"/foo",
"//bar",
"/1",
"/0001",
"//1",
"//0001",
"//foo//bar/wibble",
"//foo//001/wibble",
];
for i in 0..2 {
for path in &uri_paths {
let password = format!("Testing{i}");
let (_sp_pair, phrase, _seed) = SpPair::generate_with_phrase(Some(&password));
let uri = format!("{phrase}{path}///{password}");
let sp_pair = SpPair::from_string(&uri, None).expect("should be valid");
let uri = SecretUri::from_str(&uri).expect("should be valid secret URI");
let pair = Keypair::from_uri(&uri).expect("should be valid");
assert_eq!(sp_pair.public().0, pair.public_key().0);
}
}
}
#[test]
fn check_dev_accounts_match() {
use sp_keyring::sr25519::Keyring::*;
assert_eq!(dev::alice().public_key().0, Alice.public().0);
assert_eq!(dev::bob().public_key().0, Bob.public().0);
assert_eq!(dev::charlie().public_key().0, Charlie.public().0);
assert_eq!(dev::dave().public_key().0, Dave.public().0);
assert_eq!(dev::eve().public_key().0, Eve.public().0);
assert_eq!(dev::ferdie().public_key().0, Ferdie.public().0);
assert_eq!(dev::one().public_key().0, One.public().0);
assert_eq!(dev::two().public_key().0, Two.public().0);
}
#[test]
fn check_signing_and_verifying_matches() {
use sp_core::sr25519::Signature as SpSignature;
for _ in 0..20 {
let (sp_pair, phrase, _seed) = SpPair::generate_with_phrase(Some("Testing"));
let phrase = bip39::Mnemonic::parse(phrase).expect("valid phrase expected");
let pair = Keypair::from_phrase(&phrase, Some("Testing")).expect("should be valid");
let message = b"Hello world";
let sp_sig = sp_pair.sign(message).0;
let sig = pair.sign(message).0;
assert!(SpPair::verify(
&SpSignature(sig),
message,
&sp_pair.public()
));
assert!(verify(&Signature(sp_sig), message, &pair.public_key()));
}
}
#[test]
fn check_hex_uris() {
let uri_str =
"0x1122334455667788112233445566778811223344556677881122334455667788///SomePassword";
let uri = SecretUri::from_str(uri_str).expect("should be valid");
let pair = Keypair::from_uri(&uri).expect("should be valid");
let sp_pair = SpPair::from_string(uri_str, None).expect("should be valid");
assert_eq!(pair.public_key().0, sp_pair.public().0);
}
}