Skip to content

Commit

Permalink
Move base64 stuff to v0
Browse files Browse the repository at this point in the history
  • Loading branch information
tcharding committed Nov 15, 2023
1 parent c2d6201 commit 895cb44
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 62 deletions.
62 changes: 0 additions & 62 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,68 +58,6 @@ pub use crate::{
sighash_type::PsbtSighashType,
};

#[cfg(feature = "base64")]
mod display_from_str {
use core::fmt::{self, Display, Formatter};
use core::str::FromStr;

use bitcoin::base64::display::Base64Display;
use bitcoin::base64::prelude::{Engine as _, BASE64_STANDARD};

use crate::error::write_err;
use crate::Error;

/// Error encountered during PSBT decoding from Base64 string.
#[derive(Debug)]
#[non_exhaustive]
pub enum PsbtParseError {
/// Error in internal PSBT data structure.
PsbtEncoding(Error),
/// Error in PSBT Base64 encoding.
Base64Encoding(bitcoin::base64::DecodeError),
}

impl Display for PsbtParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use self::PsbtParseError::*;

match *self {
PsbtEncoding(ref e) => write_err!(f, "error in internal PSBT data structure"; e),
Base64Encoding(ref e) => write_err!(f, "error in PSBT base64 encoding"; e),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for PsbtParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::PsbtParseError::*;

match self {
PsbtEncoding(e) => Some(e),
Base64Encoding(e) => Some(e),
}
}
}

impl Display for crate::v0::Psbt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", Base64Display::new(&self.serialize(), &BASE64_STANDARD))
}
}

impl FromStr for crate::v0::Psbt {
type Err = PsbtParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let data = BASE64_STANDARD.decode(s).map_err(PsbtParseError::Base64Encoding)?;
crate::v0::Psbt::deserialize(&data).map_err(PsbtParseError::PsbtEncoding)
}
}
}
#[cfg(feature = "base64")]
pub use self::display_from_str::PsbtParseError;

#[rustfmt::skip]
mod prelude {
#[cfg(all(not(feature = "std"), not(test)))]
Expand Down
64 changes: 64 additions & 0 deletions src/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ use crate::v0::error::{ExtractTxError, IndexOutOfBoundsError, SignError};
use crate::v0::map::{Global, Input, Map, Output};
use crate::Error;

#[rustfmt::skip] // Keep public re-exports separate.
#[cfg(feature = "base64")]
pub use self::display_from_str::PsbtParseError;

/// A Partially Signed Transaction.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -532,6 +536,66 @@ impl Psbt {
}
}

/// If the "base64" feature is enabled we implement `Display` and `FromStr` using base64 encoding.
#[cfg(feature = "base64")]
mod display_from_str {
use core::fmt::{self, Display, Formatter};
use core::str::FromStr;

use bitcoin::base64::display::Base64Display;
use bitcoin::base64::prelude::{Engine as _, BASE64_STANDARD};

use super::*;

impl Display for Psbt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", Base64Display::new(&self.serialize(), &BASE64_STANDARD))
}
}

impl FromStr for Psbt {
type Err = PsbtParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let data = BASE64_STANDARD.decode(s).map_err(PsbtParseError::Base64Encoding)?;
Psbt::deserialize(&data).map_err(PsbtParseError::PsbtEncoding)
}
}

/// Error encountered during PSBT decoding from Base64 string.
#[derive(Debug)]
#[non_exhaustive]
pub enum PsbtParseError {
/// Error in internal PSBT data structure.
PsbtEncoding(Error),
/// Error in PSBT Base64 encoding.
Base64Encoding(bitcoin::base64::DecodeError),
}

impl Display for PsbtParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use self::PsbtParseError::*;

match *self {
PsbtEncoding(ref e) => write_err!(f, "error in internal PSBT data structure"; e),
Base64Encoding(ref e) => write_err!(f, "error in PSBT base64 encoding"; e),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for PsbtParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::PsbtParseError::*;

match self {
PsbtEncoding(e) => Some(e),
Base64Encoding(e) => Some(e),
}
}
}
}

/// Data required to call [`GetKey`] to get the private key to sign an input.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
Expand Down

0 comments on commit 895cb44

Please sign in to comment.