From 12c712278eba929ac1daf0523b297b122723d492 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Fri, 20 Sep 2019 20:36:00 -0700 Subject: [PATCH] Fix bip39 --- Cargo.toml | 2 +- src/lightwallet/mod.rs | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d1e694b..09b8906 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ byteorder = "1" rand = "0.5.6" json = "0.12.0" shellwords = "1.0.0" -bip39 = "0.6.0-beta.1" +tiny-bip39 = "0.6.2" clap = "2.33" secp256k1 = "=0.15.0" sha2 = "0.8.0" diff --git a/src/lightwallet/mod.rs b/src/lightwallet/mod.rs index 0bd0445..09a86cc 100644 --- a/src/lightwallet/mod.rs +++ b/src/lightwallet/mod.rs @@ -151,6 +151,7 @@ impl LightWallet { pub fn new(seed_phrase: Option, config: &LightClientConfig) -> io::Result { use rand::{FromEntropy, ChaChaRng, Rng}; + // This is the source entropy that corresponds to the 24-word seed phrase let mut seed_bytes = [0u8; 32]; if seed_phrase.is_none() { @@ -159,12 +160,15 @@ impl LightWallet { system_rng.fill(&mut seed_bytes); } else { seed_bytes.copy_from_slice(&Mnemonic::from_phrase(seed_phrase.expect("should have a seed phrase"), - Language::English).unwrap().entropy()); + Language::English).unwrap().entropy()); } + // The seed bytes is the raw entropy. To pass it to HD wallet generation, + // we need to get the 64 byte bip39 entropy + let bip39_seed = bip39::Seed::new(&Mnemonic::from_entropy(&seed_bytes, Language::English).unwrap(), ""); + // TODO: This only reads one key for now - println!("{}", hex::encode(seed_bytes)); - let ext_t_key = ExtendedPrivKey::with_seed(&seed_bytes).unwrap(); + let ext_t_key = ExtendedPrivKey::with_seed(&bip39_seed.as_bytes()).unwrap(); let tpk = ext_t_key .derive_private_key(KeyIndex::hardened_from_normalize_index(44).unwrap()).unwrap() .derive_private_key(KeyIndex::hardened_from_normalize_index(1).unwrap()).unwrap() // TODO: Cointype @@ -381,8 +385,7 @@ impl LightWallet { // Encode into t address let mut hash160 = ripemd160::Ripemd160::new(); hash160.input(Sha256::digest(&pk.serialize()[..].to_vec())); - - // TODO: The taddr version prefix needs to be different for testnet and mainnet + hash160.result().to_base58check(&self.config.base58_pubkey_address(), &[]) }