Skip to content

Commit

Permalink
psbt: impl Display as Base64 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 3, 2023
1 parent 4c9f066 commit 5ff1962
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions psbt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
21 changes: 19 additions & 2 deletions psbt/src/coders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 5ff1962

Please sign in to comment.