Switch from tiny-bip39 to bip39 crate (#2084)

Switch from: 
https://crates.io/crates/tiny-bip39
to:
https://crates.io/crates/bip39

Required for: https://github.com/paritytech/polkadot-sdk/pull/2044
This commit is contained in:
Michal Kucharczyk
2023-10-30 17:03:30 +01:00
committed by GitHub
parent 30f3ad2eef
commit a69da4a85f
5 changed files with 38 additions and 49 deletions
+5 -2
View File
@@ -25,7 +25,7 @@ hash256-std-hasher = { version = "0.15.2", default-features = false }
bs58 = { version = "0.5.0", default-features = false, optional = true }
rand = { version = "0.8.5", features = ["small_rng"], optional = true }
substrate-bip39 = { version = "0.4.4", optional = true }
tiny-bip39 = { version = "1.0.0", optional = true }
bip39 = { version = "2.0.0", default-features = false }
regex = { version = "1.6.0", optional = true }
zeroize = { version = "1.4.3", default-features = false }
secrecy = { version = "0.8.0", default-features = false }
@@ -42,6 +42,7 @@ thiserror = { version = "1.0.48", optional = true }
tracing = { version = "0.1.29", optional = true }
bitflags = "1.3"
paste = "1.0.7"
itertools = { version = "0.10.3", optional = true }
# full crypto
array-bytes = { version = "6.1", optional = true }
@@ -76,6 +77,8 @@ default = [ "std" ]
std = [
"array-bytes",
"bandersnatch_vrfs/getrandom",
"bip39/rand",
"bip39/std",
"blake2/std",
"bounded-collections/std",
"bs58/std",
@@ -88,6 +91,7 @@ std = [
"hash-db/std",
"hash256-std-hasher/std",
"impl-serde/std",
"itertools",
"lazy_static",
"libsecp256k1/std",
"log/std",
@@ -114,7 +118,6 @@ std = [
"ss58-registry/std",
"substrate-bip39",
"thiserror",
"tiny-bip39",
"tracing",
"w3f-bls?/std",
"zeroize/alloc",
+10 -6
View File
@@ -19,9 +19,11 @@
use crate::{ed25519, sr25519};
#[cfg(feature = "std")]
use bip39::{Language, Mnemonic, MnemonicType};
use bip39::{Language, Mnemonic};
use codec::{Decode, Encode, MaxEncodedLen};
#[cfg(feature = "std")]
use itertools::Itertools;
#[cfg(feature = "std")]
use rand::{rngs::OsRng, RngCore};
#[cfg(feature = "std")]
use regex::Regex;
@@ -870,9 +872,9 @@ pub trait Pair: CryptoType + Sized {
/// the key from the current session.
#[cfg(feature = "std")]
fn generate_with_phrase(password: Option<&str>) -> (Self, String, Self::Seed) {
let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English);
let phrase = mnemonic.phrase();
let (pair, seed) = Self::from_phrase(phrase, password)
let mnemonic = Mnemonic::generate(12).expect("Mnemonic generation always works; qed");
let phrase = mnemonic.word_iter().join(" ");
let (pair, seed) = Self::from_phrase(&phrase, password)
.expect("All phrases generated by Mnemonic are valid; qed");
(pair, phrase.to_owned(), seed)
}
@@ -883,10 +885,12 @@ pub trait Pair: CryptoType + Sized {
phrase: &str,
password: Option<&str>,
) -> Result<(Self, Self::Seed), SecretStringError> {
let mnemonic = Mnemonic::from_phrase(phrase, Language::English)
let mnemonic = Mnemonic::parse_in(Language::English, phrase)
.map_err(|_| SecretStringError::InvalidPhrase)?;
let (entropy, entropy_len) = mnemonic.to_entropy_array();
let big_seed =
substrate_bip39::seed_from_entropy(mnemonic.entropy(), password.unwrap_or(""))
substrate_bip39::seed_from_entropy(&entropy[0..entropy_len], password.unwrap_or(""))
.map_err(|_| SecretStringError::InvalidSeed)?;
let mut seed = Self::Seed::default();
let seed_slice = seed.as_mut();