mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 06:51:07 +00:00
Make subkey support Sr25519 crypto (#1933)
* Make subkey support Sr25519 crypto. * Rebuild runtime. * Build and rejig locks. * Fix grumbles * Derivations * Introduce tests
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use rand::{rngs::OsRng, RngCore};
|
||||
use substrate_primitives::ed25519::Pair;
|
||||
use super::Crypto;
|
||||
|
||||
fn good_waypoint(done: u64) -> u64 {
|
||||
match done {
|
||||
@@ -38,9 +38,9 @@ fn next_seed(mut seed: [u8; 32]) -> [u8; 32] {
|
||||
|
||||
/// A structure used to carry both Pair and seed.
|
||||
/// This should usually NOT been used. If unsure, use Pair.
|
||||
pub struct KeyPair {
|
||||
pub pair: Pair,
|
||||
pub seed: [u8; 32],
|
||||
pub(super) struct KeyPair<C: Crypto> {
|
||||
pub pair: C::Pair,
|
||||
pub seed: C::Seed,
|
||||
pub score: usize,
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ fn calculate_score(_desired: &str, key: &str) -> usize {
|
||||
0
|
||||
}
|
||||
|
||||
pub fn generate_key(desired: &str) -> Result<KeyPair, &str> {
|
||||
pub(super) fn generate_key<C: Crypto<Seed=[u8; 32]>>(desired: &str) -> Result<KeyPair<C>, &str> {
|
||||
if desired.is_empty() {
|
||||
return Err("Pattern must not be empty");
|
||||
}
|
||||
@@ -77,8 +77,8 @@ pub fn generate_key(desired: &str) -> Result<KeyPair, &str> {
|
||||
OsRng::new().unwrap().fill_bytes(&mut seed[..]);
|
||||
}
|
||||
|
||||
let p = Pair::from_seed(&seed);
|
||||
let ss58 = p.public().to_ss58check();
|
||||
let p = C::pair_from_seed(&seed);
|
||||
let ss58 = C::ss58_from_pair(&p);
|
||||
let score = calculate_score(&desired, &ss58);
|
||||
if score > best || desired.len() < 2 {
|
||||
best = score;
|
||||
@@ -104,12 +104,13 @@ pub fn generate_key(desired: &str) -> Result<KeyPair, &str> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::super::OriginalEd25519;
|
||||
#[cfg(feature = "bench")]
|
||||
use test::Bencher;
|
||||
|
||||
#[test]
|
||||
fn test_generation_with_single_char() {
|
||||
assert!(generate_key("j").unwrap().pair.public().to_ss58check().contains("j"));
|
||||
assert!(generate_key::<OriginalEd25519>("j").unwrap().pair.public().to_ss58check().contains("j"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user