-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only allow mnemonic from stdin (#3023)
- Loading branch information
Showing
3 changed files
with
78 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,41 @@ | ||
use super::*; | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(group( | ||
ArgGroup::new("source").required(true).args(&["descriptor", "mnemonic"])) | ||
)] | ||
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>, | ||
#[arg( | ||
long, | ||
requires = "mnemonic", | ||
help = "Use <PASSPHRASE> when deriving wallet" | ||
)] | ||
#[clap(value_enum, long, help = "Restore wallet from <SOURCE> on stdin.")] | ||
from: Source, | ||
#[arg(long, help = "Use <PASSPHRASE> when deriving wallet")] | ||
pub(crate) passphrase: Option<String>, | ||
} | ||
|
||
#[derive(clap::ValueEnum, Debug, Clone)] | ||
enum Source { | ||
Descriptor, | ||
Mnemonic, | ||
} | ||
|
||
impl Restore { | ||
pub(crate) fn run(self, wallet: Wallet) -> SubcommandResult { | ||
ensure!(!wallet.exists()?, "wallet `{}` already exists", wallet.name); | ||
|
||
if self.descriptor { | ||
let mut buffer = Vec::new(); | ||
std::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 { | ||
wallet.initialize(mnemonic.to_seed(self.passphrase.unwrap_or_default()))?; | ||
} else { | ||
unreachable!(); | ||
let mut buffer = String::new(); | ||
io::stdin().read_to_string(&mut buffer)?; | ||
|
||
match self.from { | ||
Source::Descriptor => { | ||
ensure!( | ||
self.passphrase.is_none(), | ||
"descriptor does not take a passphrase" | ||
); | ||
let wallet_descriptors: ListDescriptorsResult = serde_json::from_str(&buffer)?; | ||
wallet.initialize_from_descriptors(wallet_descriptors.descriptors)?; | ||
} | ||
Source::Mnemonic => { | ||
let mnemonic = Mnemonic::from_str(&buffer)?; | ||
wallet.initialize(mnemonic.to_seed(self.passphrase.unwrap_or_default()))?; | ||
} | ||
} | ||
|
||
Ok(None) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn descriptor_and_mnemonic_conflict() { | ||
assert_regex_match!( | ||
Arguments::try_parse_from([ | ||
"ord", | ||
"wallet", | ||
"restore", | ||
"--descriptor", | ||
"--mnemonic", | ||
"oil oil oil oil oil oil oil oil oil oil oil oil" | ||
]) | ||
.unwrap_err() | ||
.to_string(), | ||
".*--descriptor.*cannot be used with.*--mnemonic.*" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters