From d869becebaa966e288b51a992e84dc3790e5aef2 Mon Sep 17 00:00:00 2001 From: Ashraf Fouda Date: Tue, 19 Dec 2023 14:46:12 +0200 Subject: [PATCH] generate seed the same way as subkey (#183) Signed-off-by: Ashraf Fouda --- Cargo.lock | 18 +++++++++++++++++- Cargo.toml | 3 ++- src/peer/e2e/mod.rs | 17 +++++++---------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 112d13d..f9116da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -577,6 +577,15 @@ dependencies = [ "serde", ] +[[package]] +name = "bip39" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93f2635620bf0b9d4576eb7bb9a38a55df78bd1205d26fa994b25911a69f212f" +dependencies = [ + "bitcoin_hashes", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -592,6 +601,12 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" +[[package]] +name = "bitcoin_hashes" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90064b8dee6815a6470d60bad07bbbaee885c0e12d04177138fa3291a01b7bc4" + [[package]] name = "bitflags" version = "1.3.2" @@ -3814,6 +3829,7 @@ dependencies = [ "async-trait", "base64 0.13.1", "bb8-redis", + "bip39", "clap", "futures", "futures-util", @@ -3845,10 +3861,10 @@ dependencies = [ "serde_json", "sha2 0.10.8", "simple_logger", + "substrate-bip39", "subxt", "tfchain-client", "thiserror", - "tiny-bip39", "tokio", "tokio-retry", "tokio-stream", diff --git a/Cargo.toml b/Cargo.toml index 6597c09..41cb188 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,8 @@ tfchain-client = { git="https://github.com/threefoldtech/tfchain.git", version=" reqwest = "0.11" # for e2e -tiny-bip39 = "1.0.0" +bip39 = { version = "2.0.0", default-features = false } +substrate-bip39 = { version = "0.4.4"} secp256k1 = "0.26" aes-gcm = { version = "0.10", features=["aes", "alloc"] } sha2 = "0.10" diff --git a/src/peer/e2e/mod.rs b/src/peer/e2e/mod.rs index 8bcd30d..b21c94c 100644 --- a/src/peer/e2e/mod.rs +++ b/src/peer/e2e/mod.rs @@ -13,6 +13,8 @@ pub const NONCE_KEY_SIZE: usize = 12; pub enum Error { #[error("invalid bip39 phrase")] InvalidPhrase, + #[error("invalid entropy")] + InvalidEntropy, #[error("invalid seed")] InvalidSeed, #[error("invalid cipher data")] @@ -32,17 +34,12 @@ impl FromStr for Pair { let kp: KeyPair = match s.strip_prefix("0x") { None => { // no prefix, we assume this is a bip39 Mnemonic - let mnemonic = Mnemonic::from_phrase(s, Language::English) + let mnemonic = Mnemonic::parse_in_normalized(Language::English, s) .map_err(|_| Error::InvalidPhrase)?; - - let seed = bip39::Seed::new(&mnemonic, ""); - // note: the secpk seed is only 32 bytes, so we take the first 32 bytes - // from the seed. - if seed.as_bytes().len() < 32 { - return Err(Error::InvalidPhrase); - } - - KeyPair::from_seckey_slice(&secp, &seed.as_bytes()[..32])? + let (entropy, entropy_len) = mnemonic.to_entropy_array(); + let seed = substrate_bip39::seed_from_entropy(&entropy[0..entropy_len], "") + .map_err(|_| Error::InvalidEntropy)?; + KeyPair::from_seckey_slice(&secp, &seed[..32])? } Some(h) => { let seed = hex::decode(h).map_err(|_| Error::InvalidSeed)?;