Skip to content

Commit

Permalink
btc-cold: improve PSBT error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 3, 2023
1 parent 37a5678 commit 5d2bb54
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/bin/btc-cold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,8 @@ impl Args {
let secp = Secp256k1::new();

let data = fs::read(psbt_path)?;
let mut psbt = consensus::encode::deserialize::<PartiallySignedTransaction>(&data)?;
let mut psbt = consensus::encode::deserialize::<PartiallySignedTransaction>(&data)
.map_err(Error::psbt_from_consensus)?;

psbt.finalize_mut(&secp).map_err(VecDisplay::from)?;

Expand Down Expand Up @@ -742,21 +743,21 @@ impl Args {
fn inspect(&self, path: Option<&PathBuf>) -> Result<(), Error> {
let psbt = if let Some(path) = path {
let data = fs::read(path)?;
Psbt::deserialize(&data)?
Psbt::deserialize(&data).map_err(Error::psbt_from_consensus)?
} else {
eprint!("Type in Base58 encoded PSBT and press enter: ");
stdout().flush()?;
let stdin = stdin();
let psbt58 = stdin.lock().lines().next().expect("no PSBT data")?;
Psbt::from_str(psbt58.trim())?
let psbt64 = stdin.lock().lines().next().expect("no PSBT data")?;
Psbt::from_str(psbt64.trim()).map_err(Error::psbt_from_consensus)?
};
println!("\n{}", serde_yaml::to_string(&psbt)?);
Ok(())
}

fn convert(&self, path: &Path) -> Result<(), Error> {
let data = fs::read(path)?;
let psbt = Psbt::deserialize(&data)?;
let psbt = Psbt::deserialize(&data).map_err(Error::psbt_from_consensus)?;
println!("\n{}\n", psbt);
Ok(())
}
Expand Down Expand Up @@ -927,7 +928,7 @@ pub enum Error {
Io(IoError),

#[from]
PsbtEncoding(consensus::encode::Error),
PsbtEncoding(psbt::Error),

#[from]
Miniscript(miniscript::Error),
Expand Down Expand Up @@ -981,6 +982,15 @@ pub enum Error {
PsbtProprietaryKey(ProprietaryKeyError),
}

impl Error {
pub fn psbt_from_consensus(e: consensus::encode::Error) -> Error {
let consensus::encode::Error::Psbt(e) = e else {
unreachable!()
};
Error::PsbtEncoding(e)
}
}

// TODO: Move to amplify crate
#[derive(Debug, From)]
pub struct VecDisplay<T, const PREFIX: bool, const PREFIX_CHAR: char, const JOIN_CHAR: char>(
Expand Down

0 comments on commit 5d2bb54

Please sign in to comment.