Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from tiny-bip39 to bip39 crate #2084

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 8 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion substrate/client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ chrono = "0.4.27"
clap = { version = "4.4.6", features = ["derive", "string", "wrap_help"] }
fdlimit = "0.2.1"
futures = "0.3.21"
itertools = "0.10.3"
libp2p-identity = { version = "0.1.3", features = ["peerid", "ed25519"]}
log = "0.4.17"
names = { version = "0.13.0", default-features = false }
Expand All @@ -28,7 +29,7 @@ rpassword = "7.0.0"
serde = "1.0.188"
serde_json = "1.0.107"
thiserror = "1.0.48"
tiny-bip39 = "1.0.0"
bip39 = "2.0.0"
tokio = { version = "1.22.0", features = ["signal", "rt-multi-thread", "parking_lot"] }
sc-client-api = { path = "../api" }
sc-client-db = { path = "../db", default-features = false}
Expand Down
23 changes: 13 additions & 10 deletions substrate/client/cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ use crate::{
utils::print_from_uri, with_crypto_scheme, CryptoSchemeFlag, Error, KeystoreParams,
NetworkSchemeFlag, OutputTypeFlag,
};
use bip39::{Language, Mnemonic, MnemonicType};
use bip39::Mnemonic;
use clap::Parser;
use itertools::Itertools;

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

let phrase = mnemonic.word_iter().join(" ");

with_crypto_scheme!(
self.crypto_scheme.scheme,
print_from_uri(mnemonic.phrase(), password, self.network_scheme.network, output)
print_from_uri(&phrase, password, self.network_scheme.network, output)
);
Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions substrate/primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down Expand Up @@ -76,6 +77,8 @@ default = [ "std" ]
std = [
"array-bytes",
"bandersnatch_vrfs/getrandom",
"bip39/rand",
"bip39/std",
"blake2/std",
"bounded-collections/std",
"bs58/std",
Expand All @@ -88,6 +91,7 @@ std = [
"hash-db/std",
"hash256-std-hasher/std",
"impl-serde/std",
"itertools",
"lazy_static",
"libsecp256k1/std",
"log/std",
Expand All @@ -114,7 +118,6 @@ std = [
"ss58-registry/std",
"substrate-bip39",
"thiserror",
"tiny-bip39",
"tracing",
"w3f-bls?/std",
"zeroize/alloc",
Expand Down
16 changes: 10 additions & 6 deletions substrate/primitives/core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand All @@ -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();
Expand Down