Skip to content

Commit

Permalink
Stop overwriting an existing wallet file
Browse files Browse the repository at this point in the history
I managed to permanantly lose regtest coins because of `recover-wallet`
overwriting the wallet file.
  • Loading branch information
chris-belcher committed Jan 30, 2022
1 parent 4275ca3 commit 42d977e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
27 changes: 12 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,15 @@ pub fn generate_wallet(wallet_file_name: &PathBuf) -> std::io::Result<()> {
return Ok(());
}
};

println!("input seed phrase extension (or leave blank for none): ");
let mut extension = String::new();
io::stdin().read_line(&mut extension)?;
extension = extension.trim().to_string();

let mnemonic =
mnemonic::Mnemonic::new_random(bitcoin_wallet::account::MasterKeyEntropy::Sufficient)
.unwrap();

println!("Write down this seed phrase =\n{}", mnemonic.to_string());

if !extension.trim().is_empty() {
println!("And this extension =\n\"{}\"", extension);
}

println!(
"\nThis seed phrase is NOT enough to backup all coins in your wallet\n\
The teleport wallet file is needed to backup swapcoins"
);

Wallet::save_new_wallet_file(&wallet_file_name, mnemonic.to_string(), extension).unwrap();
Wallet::save_new_wallet_file(&wallet_file_name, mnemonic.to_string(), extension.clone())
.unwrap();

let w = match Wallet::load_wallet_from_file(&wallet_file_name, WalletSyncAddressAmount::Normal)
{
Expand All @@ -117,6 +104,16 @@ pub fn generate_wallet(wallet_file_name: &PathBuf) -> std::io::Result<()> {
&Vec::<_>::new(),
)
.unwrap();

println!("Write down this seed phrase =\n{}", mnemonic.to_string());
if !extension.trim().is_empty() {
println!("And this extension =\n\"{}\"", extension);
}
println!(
"\nThis seed phrase is NOT enough to backup all coins in your wallet\n\
The teleport wallet file is needed to backup swapcoins"
);

Ok(())
}

Expand Down
7 changes: 5 additions & 2 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// makers will only ever sync this way, but one day takers may sync in other
// ways too such as a lightweight wallet method

use std::fs::File;
use std::fs::{File, OpenOptions};
use std::io;
use std::io::Read;
use std::path::Path;
Expand Down Expand Up @@ -484,7 +484,10 @@ impl Wallet {
outgoing_swapcoins: Vec::new(),
prevout_to_contract_map: HashMap::<OutPoint, Script>::new(),
};
let wallet_file = File::create(wallet_file_name)?;
let wallet_file = OpenOptions::new()
.write(true)
.create_new(true)
.open(wallet_file_name)?;
serde_json::to_writer(wallet_file, &wallet_file_data).map_err(|e| io::Error::from(e))?;
Ok(())
}
Expand Down

0 comments on commit 42d977e

Please sign in to comment.