PSBT Display & FromStr using Base64 serialization

This commit is contained in:
Dr Maxim Orlovsky 2021-01-27 21:19:04 +01:00
parent 2414c5b0a9
commit 072e1d1b86
No known key found for this signature in database
GPG Key ID: FFC0250947E5C6F7
2 changed files with 24 additions and 1 deletions

View File

@ -100,7 +100,7 @@ impl fmt::Display for Error {
write!(f, "Preimage {:?} does not match {:?} hash {:?}", preimage, hash_type, hash ) write!(f, "Preimage {:?} does not match {:?} hash {:?}", preimage, hash_type, hash )
} }
Error::MergeConflict(ref s) => { write!(f, "Merge conflict: {}", s) } 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"),
} }
} }
} }

View File

@ -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<Self, Self::Err> {
Ok(encode::deserialize(&::base64::decode(s).map_err(|_| Error::ConsensusEncoding)?)?)
}
}
}
impl Encodable for PartiallySignedTransaction { impl Encodable for PartiallySignedTransaction {
fn consensus_encode<S: io::Write>( fn consensus_encode<S: io::Write>(
&self, &self,