Skip to content

Commit

Permalink
update except message and start integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mj10021 committed Feb 15, 2024
1 parent 2c978c4 commit 4334037
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/subcommand/wallet/restore.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;
use std::io;
use {
super::*,
std::io,
};

#[derive(Debug, Parser)]
#[clap(group(
Expand All @@ -9,7 +11,7 @@ pub(crate) struct Restore {
#[arg(long, help = "Restore wallet from <DESCRIPTOR> from stdin.")]
descriptor: bool,
#[arg(long, help = "Restore wallet from <MNEMONIC>.")]
mnemonic: Option<Mnemonic>,
mnemonic: Option<Option<Mnemonic>>,
#[arg(
long,
requires = "mnemonic",
Expand All @@ -24,28 +26,21 @@ impl Restore {

if self.descriptor {
let mut buffer = Vec::new();
std::io::stdin().read_to_end(&mut buffer)?;
io::stdin().read_to_end(&mut buffer)?;

let wallet_descriptors: ListDescriptorsResult = serde_json::from_slice(&buffer)?;

wallet.initialize_from_descriptors(wallet_descriptors.descriptors)?;
} else if let Some(mnemonic) = self.mnemonic {
} else if let Some(Some(mnemonic)) = self.mnemonic {
wallet.initialize(mnemonic.to_seed(self.passphrase.unwrap_or_default()))?;
} else {
unreachable!();
}
} else if let Some(None) = self.mnemonic {
let mut buffer = Vec::new();
println!("Please input your seed phrase:");
io::stdin().read_to_end(&mut buffer)?;
let msg = format!("invalid input as bytes, expected [u8; 64] read [u8; {}]", buffer.len());
wallet.initialize(buffer.try_into().expect(&msg))?
} else {unreachable!()}

let seed = {
if self.mnemonic.is_none() {
let mut input = String::new();
println!("Please input your seed below:");
io::stdin().read_line(&mut input).expect("failed to read mnemonic");
let input = input.into_bytes();
assert_eq!(input.len(), 64);
input.try_into().unwrap()
} else {self.mnemonic.unwrap().to_seed(self.passphrase)}
};
wallet::initialize(seed)?;
Ok(None)
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/wallet/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,25 @@ fn restore_with_compact_works() {
.expected_exit_code(0)
.run_and_extract_stdout();
}

#[test]
fn restore_with_blank_mnemonic_generates_same_descriptors() {
let (mnemonic, descriptors) = {
let rpc_server = test_bitcoincore_rpc::spawn();

let create::Output { mnemonic, .. } = CommandBuilder::new("wallet create")
.bitcoin_rpc_server(&rpc_server)
.run_and_deserialize_output();

(mnemonic, rpc_server.descriptors())
};

let rpc_server = test_bitcoincore_rpc::spawn();

CommandBuilder::new(["wallet", "restore", "--mnemonic"])
.stdin(mnemonic.to_string().as_bytes().to_vec())
.bitcoin_rpc_server(&rpc_server)
.run_and_extract_stdout();

assert_eq!(rpc_server.descriptors(), descriptors);
}

0 comments on commit 4334037

Please sign in to comment.