Skip to content

Commit

Permalink
Merge pull request #29 from yanganto/version-checks
Browse files Browse the repository at this point in the history
Fix version displays of PSBT
  • Loading branch information
dr-orlovsky committed Jun 8, 2024
2 parents f4b1861 + 2e3c1d1 commit e649b0a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ categories = ["cryptography::cryptocurrencies"]
authors = ["Dr Maxim Orlovsky <orlovsky@lnp-bp.org>"]
homepage = "https://lnp-bp.org"
repository = "https://github.com/BP-WG/bp-wallet"
rust-version = "1.70.0" # Due to mnemonic dependency of bp-seals crate
rust-version = "1.75.0" # Due to descriptors
edition = "2021"
license = "Apache-2.0"

Expand Down
4 changes: 2 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ coverage:
status:
project:
default:
target: 75%
target: 0%
threshold: 1%
branches:
- master
patch:
default:
target: 60%
target: 0%
threshold: 1%
only_pulls: true
91 changes: 82 additions & 9 deletions psbt/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,7 @@ mod display_from_str {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let ver = match (f.width(), f.sign_aware_zero_pad()) {
(None, true) => PsbtVer::V0,
(Some(0), _) => PsbtVer::V0,
(Some(2), _) => PsbtVer::V2,
(Some(ver), _) => PsbtVer::try_from(ver).map_err(|_| fmt::Error)?,
_ => self.version,
};
write!(f, "{}", Base64Display::new(&self.serialize(ver), &BASE64_STANDARD))
Expand All @@ -547,14 +546,10 @@ mod display_from_str {

impl LowerHex for Psbt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut ver = match f.width().unwrap_or(0) {
0 => PsbtVer::V0,
2 => PsbtVer::V2,
_ => return Err(fmt::Error),
let ver = match f.width() {
Some(ver) => PsbtVer::try_from(ver).map_err(|_| fmt::Error)?,
None => self.version,
};
if f.alternate() {
ver = PsbtVer::V2;
}
f.write_str(&self.to_base16_ver(ver))
}
}
Expand Down Expand Up @@ -963,3 +958,81 @@ impl ModifiableFlags {
self.inputs_modifiable | self.outputs_modifiable | self.sighash_single
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn psbt_formats() {
let v0_psbt = Psbt {
version: PsbtVer::V0,
tx_version: TxVer::default(),
fallback_locktime: None,
inputs: Vec::new(),
outputs: Vec::new(),
xpubs: IndexMap::new(),
tx_modifiable: None,
proprietary: IndexMap::new(),
unknown: IndexMap::new(),
};
let v2_psbt = Psbt {
version: PsbtVer::V2,
tx_version: TxVer::default(),
fallback_locktime: None,
inputs: Vec::new(),
outputs: Vec::new(),
xpubs: IndexMap::new(),
tx_modifiable: None,
proprietary: IndexMap::new(),
unknown: IndexMap::new(),
};

assert_eq!(format!("{v0_psbt}"), "cHNidP8BAAoCAAAAAAAAAAAAAfsEAAAAAAA=");
assert_eq!(format!("{v2_psbt}"), "cHNidP8BAgQCAAAAAQQBAAEFAQAB+wQCAAAAAA==");

assert_eq!(format!("{v0_psbt:#}"), "cHNidP8BAAoCAAAAAAAAAAAAAfsEAAAAAAA=");
assert_eq!(format!("{v2_psbt:#}"), "cHNidP8BAgQCAAAAAQQBAAEFAQAB+wQCAAAAAA==");

assert_eq!(format!("{v0_psbt:x}"), "70736274ff01000a0200000000000000000001fb040000000000");
assert_eq!(
format!("{v2_psbt:x}"),
"70736274ff01020402000000010401000105010001fb040200000000"
);

assert_eq!(format!("{v0_psbt:#x}"), "70736274ff01000a0200000000000000000001fb040000000000");
assert_eq!(
format!("{v2_psbt:#x}"),
"70736274ff01020402000000010401000105010001fb040200000000"
);

// format with a specific version
assert_eq!(format!("{v2_psbt:00}"), "cHNidP8BAAoCAAAAAAAAAAAAAfsEAAAAAAA=");
assert_eq!(format!("{v0_psbt:02}"), "cHNidP8BAgQCAAAAAQQBAAEFAQAB+wQCAAAAAA==");

assert_eq!(format!("{v2_psbt:0}"), "cHNidP8BAAoCAAAAAAAAAAAAAfsEAAAAAAA=");
assert_eq!(format!("{v0_psbt:2}"), "cHNidP8BAgQCAAAAAQQBAAEFAQAB+wQCAAAAAA==");

assert_eq!(format!("{v2_psbt:#0}"), "cHNidP8BAAoCAAAAAAAAAAAAAfsEAAAAAAA=");
assert_eq!(format!("{v0_psbt:#2}"), "cHNidP8BAgQCAAAAAQQBAAEFAQAB+wQCAAAAAA==");

// error formats
let result = std::panic::catch_unwind(|| format!("{v0_psbt:01}"));
assert!(result.is_err(), "Should fail on unsupported psbt version");

let result = std::panic::catch_unwind(|| format!("{v0_psbt:1}"));
assert!(result.is_err(), "Should fail on unsupported psbt version");

let result = std::panic::catch_unwind(|| format!("{v0_psbt:#01}"));
assert!(result.is_err(), "Should fail on unsupported psbt version");

let result = std::panic::catch_unwind(|| format!("{v0_psbt:#1}"));
assert!(result.is_err(), "Should fail on unsupported psbt version");

let result = std::panic::catch_unwind(|| format!("{v0_psbt:#1x}"));
assert!(result.is_err(), "Should fail on unsupported psbt version");

let result = std::panic::catch_unwind(|| format!("{v0_psbt:#01x}"));
assert!(result.is_err(), "Should fail on unsupported psbt version");
}
}
24 changes: 24 additions & 0 deletions psbt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,27 @@ impl PsbtVer {
}
}
}

impl TryFrom<usize> for PsbtVer {
type Error = PsbtUnsupportedVer;

fn try_from(value: usize) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::V0),
2 => Ok(Self::V2),
_ => Err(PsbtUnsupportedVer(value as u32)),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn psbt_try_from_numbers() {
assert_eq!(PsbtVer::try_from(0), Ok(PsbtVer::V0));
assert_eq!(PsbtVer::try_from(2), Ok(PsbtVer::V2));
assert_eq!(PsbtVer::try_from(1), Err(PsbtUnsupportedVer(1)));
}
}

0 comments on commit e649b0a

Please sign in to comment.