mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-01 14:47:55 +00:00
Crypto Pair trait refactory (#13657)
* Crypto pair refactory * Remove unused method * Apply review suggestions * Remove leftovers * Associated type is not really required * Fix after refactory * Fix benchmark-ui test --------- Co-authored-by: Anton <anton.kalyaev@gmail.com>
This commit is contained in:
@@ -29,11 +29,9 @@ use crate::crypto::{
|
||||
};
|
||||
#[cfg(feature = "full_crypto")]
|
||||
use crate::{
|
||||
crypto::{DeriveJunction, Pair as TraitPair, SecretStringError},
|
||||
crypto::{DeriveError, DeriveJunction, Pair as TraitPair, SecretStringError},
|
||||
hashing::blake2_256,
|
||||
};
|
||||
#[cfg(feature = "std")]
|
||||
use bip39::{Language, Mnemonic, MnemonicType};
|
||||
#[cfg(all(feature = "full_crypto", not(feature = "std")))]
|
||||
use secp256k1::Secp256k1;
|
||||
#[cfg(feature = "std")]
|
||||
@@ -367,13 +365,6 @@ fn derive_hard_junction(secret_seed: &Seed, cc: &[u8; 32]) -> Seed {
|
||||
("Secp256k1HDKD", secret_seed, cc).using_encoded(sp_core_hashing::blake2_256)
|
||||
}
|
||||
|
||||
/// An error when deriving a key.
|
||||
#[cfg(feature = "full_crypto")]
|
||||
pub enum DeriveError {
|
||||
/// A soft key was found in the path (and is unsupported).
|
||||
SoftKeyInPath,
|
||||
}
|
||||
|
||||
/// A key pair.
|
||||
#[cfg(feature = "full_crypto")]
|
||||
#[derive(Clone)]
|
||||
@@ -387,44 +378,6 @@ impl TraitPair for Pair {
|
||||
type Public = Public;
|
||||
type Seed = Seed;
|
||||
type Signature = Signature;
|
||||
type DeriveError = DeriveError;
|
||||
|
||||
/// Generate new secure (random) key pair and provide the recovery phrase.
|
||||
///
|
||||
/// You can recover the same key later with `from_phrase`.
|
||||
#[cfg(feature = "std")]
|
||||
fn generate_with_phrase(password: Option<&str>) -> (Pair, String, Seed) {
|
||||
let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English);
|
||||
let phrase = mnemonic.phrase();
|
||||
let (pair, seed) = Self::from_phrase(phrase, password)
|
||||
.expect("All phrases generated by Mnemonic are valid; qed");
|
||||
(pair, phrase.to_owned(), seed)
|
||||
}
|
||||
|
||||
/// Generate key pair from given recovery phrase and password.
|
||||
#[cfg(feature = "std")]
|
||||
fn from_phrase(
|
||||
phrase: &str,
|
||||
password: Option<&str>,
|
||||
) -> Result<(Pair, Seed), SecretStringError> {
|
||||
let big_seed = substrate_bip39::seed_from_entropy(
|
||||
Mnemonic::from_phrase(phrase, Language::English)
|
||||
.map_err(|_| SecretStringError::InvalidPhrase)?
|
||||
.entropy(),
|
||||
password.unwrap_or(""),
|
||||
)
|
||||
.map_err(|_| SecretStringError::InvalidSeed)?;
|
||||
let mut seed = Seed::default();
|
||||
seed.copy_from_slice(&big_seed[0..32]);
|
||||
Self::from_seed_slice(&big_seed[0..32]).map(|x| (x, seed))
|
||||
}
|
||||
|
||||
/// Make a new key pair from secret seed material.
|
||||
///
|
||||
/// You should never need to use this; generate(), generate_with_phrase
|
||||
fn from_seed(seed: &Seed) -> Pair {
|
||||
Self::from_seed_slice(&seed[..]).expect("seed has valid length; qed")
|
||||
}
|
||||
|
||||
/// Make a new key pair from secret seed material. The slice must be 32 bytes long or it
|
||||
/// will return `None`.
|
||||
|
||||
Reference in New Issue
Block a user