From 42d977ed40211a19b8f11fc1778db4226cbdd189 Mon Sep 17 00:00:00 2001 From: chris-belcher Date: Sun, 30 Jan 2022 18:17:12 +0000 Subject: [PATCH] Stop overwriting an existing wallet file I managed to permanantly lose regtest coins because of `recover-wallet` overwriting the wallet file. --- src/lib.rs | 27 ++++++++++++--------------- src/wallet_sync.rs | 7 +++++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2d05fb6a..97a32065 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) { @@ -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(()) } diff --git a/src/wallet_sync.rs b/src/wallet_sync.rs index b6a06e3d..c7e516bd 100644 --- a/src/wallet_sync.rs +++ b/src/wallet_sync.rs @@ -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; @@ -484,7 +484,10 @@ impl Wallet { outgoing_swapcoins: Vec::new(), prevout_to_contract_map: HashMap::::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(()) }