Skip to content

Commit

Permalink
psbt: docs for Display and LowerHex improved, also support "1" version
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 3, 2024
1 parent c3d2b8f commit fec9ffb
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions psbt/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,25 +541,39 @@ mod display_from_str {
/// - by default, it uses version specified in the PSBT itself;
/// - if zero `{:0}` is given and no width (`{:0}`) or a zero width (`{:00}`) is provided, than
/// the PSBT is encoded as V0 even if the structure itself uses V2;
/// - if a width equal to one is given like in `{:1}`, than zero flag is ignored (so `{:01}`
/// also works that way) and PSBT is encoded as V0 even if the structure itself uses V2;
/// - if a width equal to two is given like in `{:2}`, than zero flag is ignored (so `{:02}`
/// also works that way) and PSBT is encoded as V2 even if the structure itself uses V1;
/// - all other flags has no effect on the display.
impl Display for Psbt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let ver = match (f.width(), f.sign_aware_zero_pad()) {
(None, true) => PsbtVer::V0,
(Some(1), _) => PsbtVer::V0,
(Some(ver), _) => PsbtVer::try_from(ver).map_err(|_| fmt::Error)?,
_ => self.version,
};
write!(f, "{}", Base64Display::new(&self.serialize(ver), &BASE64_STANDARD))
}
}

/// PSBT is formatted like hex-encoded string. The selection of the version if the following:
/// - by default, it uses version specified in the PSBT itself;
/// - if zero `{:0}` is given and no width (`{:0}`) or a zero width (`{:00}`) is provided, than
/// the PSBT is encoded as V0 even if the structure itself uses V2;
/// - if a width equal to one is given like in `{:1}`, than zero flag is ignored (so `{:01}`
/// also works that way) and PSBT is encoded as V0 even if the structure itself uses V2;
/// - if a width equal to two is given like in `{:2}`, than zero flag is ignored (so `{:02}`
/// also works that way) and PSBT is encoded as V2 even if the structure itself uses V1;
/// - all other flags has no effect on the display.
impl LowerHex for Psbt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let ver = match f.width() {
Some(ver) => PsbtVer::try_from(ver).map_err(|_| fmt::Error)?,
None => self.version,
let ver = match (f.width(), f.sign_aware_zero_pad()) {
(None, true) => PsbtVer::V0,
(Some(1), _) => PsbtVer::V0,
(Some(ver), _) => PsbtVer::try_from(ver).map_err(|_| fmt::Error)?,
_ => self.version,
};
f.write_str(&self.to_base16_ver(ver))
}
Expand Down

0 comments on commit fec9ffb

Please sign in to comment.