From 072e1d1b86c2161c262b1e5600f402de914c7d17 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Wed, 27 Jan 2021 21:19:04 +0100 Subject: [PATCH] PSBT Display & FromStr using Base64 serialization --- src/util/psbt/error.rs | 2 +- src/util/psbt/mod.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/util/psbt/error.rs b/src/util/psbt/error.rs index b00878fe..4c63d158 100644 --- a/src/util/psbt/error.rs +++ b/src/util/psbt/error.rs @@ -100,7 +100,7 @@ impl fmt::Display for Error { write!(f, "Preimage {:?} does not match {:?} hash {:?}", preimage, hash_type, hash ) } Error::MergeConflict(ref s) => { write!(f, "Merge conflict: {}", s) } - Error::ConsensusEncoding => f.write_str("bitcoin consensus encoding error"), + Error::ConsensusEncoding => f.write_str("bitcoin consensus or BIP-174 encoding error"), } } } diff --git a/src/util/psbt/mod.rs b/src/util/psbt/mod.rs index d54f523d..252fe22c 100644 --- a/src/util/psbt/mod.rs +++ b/src/util/psbt/mod.rs @@ -91,6 +91,29 @@ impl PartiallySignedTransaction { } } +#[cfg(feature = "base64")] +mod _base64encoding { + use super::{PartiallySignedTransaction, Error}; + use std::fmt::{Display, Formatter, self}; + use std::str::FromStr; + use consensus::encode; + use ::base64::display::Base64Display; + + impl Display for PartiallySignedTransaction { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", Base64Display::with_config(&encode::serialize(self), ::base64::STANDARD)) + } + } + + impl FromStr for PartiallySignedTransaction { + type Err = Error; + + fn from_str(s: &str) -> Result { + Ok(encode::deserialize(&::base64::decode(s).map_err(|_| Error::ConsensusEncoding)?)?) + } + } +} + impl Encodable for PartiallySignedTransaction { fn consensus_encode( &self,