Skip to content

Commit

Permalink
generate seed the same way as subkey
Browse files Browse the repository at this point in the history
Signed-off-by: Ashraf Fouda <[email protected]>
  • Loading branch information
ashraffouda committed Dec 17, 2023
1 parent 15b8be8 commit 02452cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
18 changes: 17 additions & 1 deletion 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 7 additions & 10 deletions src/peer/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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)?;
Expand Down

0 comments on commit 02452cf

Please sign in to comment.