Skip to content

Commit a69da4a

Browse files
Switch from tiny-bip39 to bip39 crate (#2084)
Switch from: https://crates.io/crates/tiny-bip39 to: https://crates.io/crates/bip39 Required for: #2044
1 parent 30f3ad2 commit a69da4a

File tree

5 files changed

+38
-49
lines changed

5 files changed

+38
-49
lines changed

Cargo.lock

Lines changed: 8 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

substrate/client/cli/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ chrono = "0.4.27"
1818
clap = { version = "4.4.6", features = ["derive", "string", "wrap_help"] }
1919
fdlimit = "0.2.1"
2020
futures = "0.3.21"
21+
itertools = "0.10.3"
2122
libp2p-identity = { version = "0.1.3", features = ["peerid", "ed25519"]}
2223
log = "0.4.17"
2324
names = { version = "0.13.0", default-features = false }
@@ -28,7 +29,7 @@ rpassword = "7.0.0"
2829
serde = "1.0.188"
2930
serde_json = "1.0.107"
3031
thiserror = "1.0.48"
31-
tiny-bip39 = "1.0.0"
32+
bip39 = "2.0.0"
3233
tokio = { version = "1.22.0", features = ["signal", "rt-multi-thread", "parking_lot"] }
3334
sc-client-api = { path = "../api" }
3435
sc-client-db = { path = "../db", default-features = false}

substrate/client/cli/src/commands/generate.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use crate::{
2020
utils::print_from_uri, with_crypto_scheme, CryptoSchemeFlag, Error, KeystoreParams,
2121
NetworkSchemeFlag, OutputTypeFlag,
2222
};
23-
use bip39::{Language, Mnemonic, MnemonicType};
23+
use bip39::Mnemonic;
2424
use clap::Parser;
25+
use itertools::Itertools;
2526

2627
/// The `generate` command
2728
#[derive(Debug, Clone, Parser)]
@@ -52,20 +53,22 @@ impl GenerateCmd {
5253
/// Run the command
5354
pub fn run(&self) -> Result<(), Error> {
5455
let words = match self.words {
55-
Some(words) => MnemonicType::for_word_count(words).map_err(|_| {
56-
Error::Input(
57-
"Invalid number of words given for phrase: must be 12/15/18/21/24".into(),
58-
)
59-
})?,
60-
None => MnemonicType::Words12,
61-
};
62-
let mnemonic = Mnemonic::new(words, Language::English);
56+
Some(words_count) if [12, 15, 18, 21, 24].contains(&words_count) => Ok(words_count),
57+
Some(_) => Err(Error::Input(
58+
"Invalid number of words given for phrase: must be 12/15/18/21/24".into(),
59+
)),
60+
None => Ok(12),
61+
}?;
62+
let mnemonic = Mnemonic::generate(words)
63+
.map_err(|e| Error::Input(format!("Mnemonic generation failed: {e}").into()))?;
6364
let password = self.keystore_params.read_password()?;
6465
let output = self.output_scheme.output_type;
6566

67+
let phrase = mnemonic.word_iter().join(" ");
68+
6669
with_crypto_scheme!(
6770
self.crypto_scheme.scheme,
68-
print_from_uri(mnemonic.phrase(), password, self.network_scheme.network, output)
71+
print_from_uri(&phrase, password, self.network_scheme.network, output)
6972
);
7073
Ok(())
7174
}

substrate/primitives/core/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ hash256-std-hasher = { version = "0.15.2", default-features = false }
2525
bs58 = { version = "0.5.0", default-features = false, optional = true }
2626
rand = { version = "0.8.5", features = ["small_rng"], optional = true }
2727
substrate-bip39 = { version = "0.4.4", optional = true }
28-
tiny-bip39 = { version = "1.0.0", optional = true }
28+
bip39 = { version = "2.0.0", default-features = false }
2929
regex = { version = "1.6.0", optional = true }
3030
zeroize = { version = "1.4.3", default-features = false }
3131
secrecy = { version = "0.8.0", default-features = false }
@@ -42,6 +42,7 @@ thiserror = { version = "1.0.48", optional = true }
4242
tracing = { version = "0.1.29", optional = true }
4343
bitflags = "1.3"
4444
paste = "1.0.7"
45+
itertools = { version = "0.10.3", optional = true }
4546

4647
# full crypto
4748
array-bytes = { version = "6.1", optional = true }
@@ -76,6 +77,8 @@ default = [ "std" ]
7677
std = [
7778
"array-bytes",
7879
"bandersnatch_vrfs/getrandom",
80+
"bip39/rand",
81+
"bip39/std",
7982
"blake2/std",
8083
"bounded-collections/std",
8184
"bs58/std",
@@ -88,6 +91,7 @@ std = [
8891
"hash-db/std",
8992
"hash256-std-hasher/std",
9093
"impl-serde/std",
94+
"itertools",
9195
"lazy_static",
9296
"libsecp256k1/std",
9397
"log/std",
@@ -114,7 +118,6 @@ std = [
114118
"ss58-registry/std",
115119
"substrate-bip39",
116120
"thiserror",
117-
"tiny-bip39",
118121
"tracing",
119122
"w3f-bls?/std",
120123
"zeroize/alloc",

substrate/primitives/core/src/crypto.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
2020
use crate::{ed25519, sr25519};
2121
#[cfg(feature = "std")]
22-
use bip39::{Language, Mnemonic, MnemonicType};
22+
use bip39::{Language, Mnemonic};
2323
use codec::{Decode, Encode, MaxEncodedLen};
2424
#[cfg(feature = "std")]
25+
use itertools::Itertools;
26+
#[cfg(feature = "std")]
2527
use rand::{rngs::OsRng, RngCore};
2628
#[cfg(feature = "std")]
2729
use regex::Regex;
@@ -870,9 +872,9 @@ pub trait Pair: CryptoType + Sized {
870872
/// the key from the current session.
871873
#[cfg(feature = "std")]
872874
fn generate_with_phrase(password: Option<&str>) -> (Self, String, Self::Seed) {
873-
let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English);
874-
let phrase = mnemonic.phrase();
875-
let (pair, seed) = Self::from_phrase(phrase, password)
875+
let mnemonic = Mnemonic::generate(12).expect("Mnemonic generation always works; qed");
876+
let phrase = mnemonic.word_iter().join(" ");
877+
let (pair, seed) = Self::from_phrase(&phrase, password)
876878
.expect("All phrases generated by Mnemonic are valid; qed");
877879
(pair, phrase.to_owned(), seed)
878880
}
@@ -883,10 +885,12 @@ pub trait Pair: CryptoType + Sized {
883885
phrase: &str,
884886
password: Option<&str>,
885887
) -> Result<(Self, Self::Seed), SecretStringError> {
886-
let mnemonic = Mnemonic::from_phrase(phrase, Language::English)
888+
let mnemonic = Mnemonic::parse_in(Language::English, phrase)
887889
.map_err(|_| SecretStringError::InvalidPhrase)?;
890+
891+
let (entropy, entropy_len) = mnemonic.to_entropy_array();
888892
let big_seed =
889-
substrate_bip39::seed_from_entropy(mnemonic.entropy(), password.unwrap_or(""))
893+
substrate_bip39::seed_from_entropy(&entropy[0..entropy_len], password.unwrap_or(""))
890894
.map_err(|_| SecretStringError::InvalidSeed)?;
891895
let mut seed = Self::Seed::default();
892896
let seed_slice = seed.as_mut();

0 commit comments

Comments
 (0)