diff --git a/lib/src/commands.rs b/lib/src/commands.rs index 7c57b7c..f529753 100644 --- a/lib/src/commands.rs +++ b/lib/src/commands.rs @@ -463,19 +463,7 @@ impl Command for FixBip39BugCommand { fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { use crate::lightwallet::bugs::BugBip39Derivation; - let r = if BugBip39Derivation::has_bug(&lightclient.wallet.read().unwrap()) { - object!{ - "has_bug" => true, - "fixed" => false, - } - } else { - object!{ - "has_bug" => false, - "fixed" => false, - } - }; - - r.pretty(2) + BugBip39Derivation::fix_bug(lightclient) } } diff --git a/lib/src/lightwallet/bugs.rs b/lib/src/lightwallet/bugs.rs index 52dc82f..0f6028d 100644 --- a/lib/src/lightwallet/bugs.rs +++ b/lib/src/lightwallet/bugs.rs @@ -1,12 +1,16 @@ use super::LightWallet; +use crate::lightclient::LightClient; +use json::object; use bip39::{Mnemonic, Language}; pub struct BugBip39Derivation {} impl BugBip39Derivation { - pub fn has_bug(wallet: &LightWallet) -> bool { + pub fn has_bug(client: &LightClient) -> bool { + let wallet = client.wallet.read().unwrap(); + if wallet.zaddress.read().unwrap().len() <= 1 { return false; } @@ -37,4 +41,43 @@ impl BugBip39Derivation { false } + + pub fn fix_bug(client: &LightClient) -> String { + if !BugBip39Derivation::has_bug(client) { + let r = object!{ + "has_bug" => false + }; + + return r.pretty(2); + } + + // TODO: Tranfer money + + // regen addresses + let wallet = client.wallet.read().unwrap(); + let num_zaddrs = wallet.zaddress.read().unwrap().len(); + let num_taddrs = wallet.taddresses.read().unwrap().len(); + + wallet.extsks.write().unwrap().truncate(1); + wallet.extfvks.write().unwrap().truncate(1); + wallet.zaddress.write().unwrap().truncate(1); + + wallet.tkeys.write().unwrap().truncate(1); + wallet.taddresses.write().unwrap().truncate(1); + + for _ in 1..num_zaddrs { + wallet.add_zaddr(); + } + + for _ in 1..num_taddrs { + wallet.add_taddr(); + } + + let r = object!{ + "has_bug" => true, + "fixed" => true, + }; + + return r.pretty(2); + } } \ No newline at end of file