diff --git a/Cargo.lock b/Cargo.lock index eecb7e1..fef2920 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -658,6 +658,7 @@ name = "psbt" version = "0.11.0" dependencies = [ "amplify", + "base64", "bp-std", "chrono", "indexmap 2.0.2", diff --git a/psbt/Cargo.toml b/psbt/Cargo.toml index b043646..0ed31f3 100644 --- a/psbt/Cargo.toml +++ b/psbt/Cargo.toml @@ -16,6 +16,7 @@ license = { workspace = true } amplify = { workspace = true } bp-std = { path = "../std" } indexmap = { workspace = true } +base64 = "0.21.4" chrono = "0.4.31" serde_crate = { workspace = true, optional = true } diff --git a/psbt/src/coders.rs b/psbt/src/coders.rs index bb05c27..f3b0fde 100644 --- a/psbt/src/coders.rs +++ b/psbt/src/coders.rs @@ -20,10 +20,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::io; -use std::io::{Cursor, Read, Write}; +use std::fmt::{self, Display, Formatter}; +use std::io::{self, Cursor, Read, Write}; use amplify::{IoError, Wrapper}; +use base64::Engine; use bp::{ ComprPubkey, ConsensusEncode, Idx, KeyOrigin, LegacyPubkey, LockTime, RedeemScript, Sats, ScriptBytes, ScriptPubkey, SeqNo, SigScript, Tx, TxOut, TxVer, Txid, UncomprPubkey, Vout, @@ -35,6 +36,22 @@ use crate::{ ModifiableFlags, Output, OutputKey, Psbt, PsbtVer, SighashType, }; +impl Display for Psbt { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let engine = base64::engine::GeneralPurpose::new( + &base64::alphabet::STANDARD, + base64::engine::GeneralPurposeConfig::new(), + ); + let ver = match f.width().unwrap_or(0) { + 0 => PsbtVer::V0, + 2 => PsbtVer::V2, + _ => return Err(fmt::Error), + }; + engine.encode(self.serialize(ver)); + Ok(()) + } +} + #[derive(Clone, Debug, Display, Error, From)] #[display(inner)] pub enum DecodeError {