Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,816 changes: 1,548 additions & 1,268 deletions Cargo-minimal.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,9 @@ dependencies = [

[[package]]
name = "bitcoin_uri"
version = "0.1.0"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e0a228e083d1702f83389b0ac71eb70078dc8d7fcbb6cde864d1cbca145f5cc"
checksum = "f4cbc30414f96a38793368163a52490cc9e62c2f21958b6b27b403c4dee95402"
dependencies = [
"bitcoin",
"percent-encoding-rfc3986",
Expand Down
7 changes: 7 additions & 0 deletions payjoin-ffi/changelog.d/bitcoin-uri-0-2-bip321.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- `Uri.parse` follows BIP 321: keys match case-insensitively, `req-pj` and
`req-pjos` are accepted, and a URI with any other unknown `req-` parameter
is rejected. Some URIs that parsed before, such as one with a repeated
`amount`, are now rejected; see the payjoin changelog for the full list.
- `PjUri.as_string` returns the URI it was parsed from unchanged, keeping
parameters payjoin does not read, such as `lightning` or `sp`.
`PjUri.set_amount_sats` still rebuilds the URI and drops them.
4 changes: 4 additions & 0 deletions payjoin-ffi/src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl PjUri {
pub fn amount_sats(&self) -> Option<u64> { self.0.amount().map(|e| e.to_sat()) }

/// Sets the amount in sats and returns a new PjUri
///
/// The URI is rebuilt from its address, amount, label, message and payjoin
/// parameters, so on a parsed URI any other parameter (`lightning`, `sp`,
/// ...) is dropped.
pub fn set_amount_sats(&self, amount_sats: u64) -> Result<Self, FfiValidationError> {
let mut uri = self.0.clone();
let amount = validate_amount_sat(amount_sats)?;
Expand Down
2 changes: 1 addition & 1 deletion payjoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ arbitrary = { version = "1.0.1", optional = true }
bhttp = { version = "0.6.1", optional = true }
bitcoin = { version = "0.32.102", features = ["base64"] }
bitcoin-units = "0.1.101"
bitcoin_uri = { version = "0.1.0", optional = true }
bitcoin_uri = { version = "0.2.0", optional = true }
hkdf = { version = "0.12.3", optional = true }
hpke = { package = "bitcoin-hpke", version = "0.20.0", optional = true }
http = { version = "1.3.1", optional = true }
Expand Down
34 changes: 34 additions & 0 deletions payjoin/changelog.d/bitcoin-uri-0-2-bip321.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### Dependencies

- Upgrade `bitcoin_uri` from 0.1 to 0.2, which parses BIP 321 URIs. The `Uri`
and `PjUri` API is unchanged.

### Bug Fixes

- Accept URIs that mark the payjoin parameters as required with `req-pj` or
`req-pjos`. BIP 321 lets a URI require any parameter the payer must
understand, and payjoin understands both, but they were rejected as unknown
required parameters. Any other unknown `req-` parameter is still rejected.
- Match URI keys case-insensitively, as BIP 321 requires. Upper-case keys used
to be ignored, so a URI with `PJ=` did not request payjoin and one with
`AMOUNT=` parsed without an amount. Both are read now.
- A `PjUri` parsed from a string prints that string unchanged. It used to be
rebuilt from the parameters payjoin reads, which dropped any other parameter
such as `lightning` or `sp`. `PjUri::set_amount` still rebuilds the URI and
drops them.

### Stricter URI Parsing

- Some URIs that parsed before are now rejected under BIP 321's rules. Code
that relied on them parsing could see this as a breaking change, though it
is unlikely that anyone depends on it:
- A repeated `amount`, `label`, `message` or `pop` is an error, including
the same key in another case (`amount=1&AMOUNT=1`). The last value used to
win, and a key in another case was ignored.
- `pj` or `pjos` repeated in another case (`pj=...&PJ=...`) is an error, as
repeating them in the same case already was.
- An address parameter (`bc`, `tb` or `bcrt`) whose value is not a segwit
address of the network its key names, such as `bc=tb1...`, is an error.
These parameters used to be ignored.
- `PjUri::set_amount` now leaves out a `label` or `message` that is not valid
UTF-8, where it used to keep the raw value.
49 changes: 37 additions & 12 deletions payjoin/src/core/uri/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub struct PjParseError(pub(super) InternalPjParseError);

/// Error parsing a BIP21 URI into a payjoin [`Uri`](super::Uri).
///
/// This wraps the underlying `bitcoin_uri` parse error so that a breaking change
/// in that crate does not force a breaking change in this crate's public API.
/// This wraps the underlying `bitcoin_uri` errors so that a breaking change in
/// that crate does not force a breaking change in this crate's public API.
#[derive(Debug)]
pub struct UriParseError(InternalUriParseError);

Expand All @@ -14,22 +14,35 @@ enum InternalUriParseError {
///
/// The foreign error is held in a private variant so that it does not appear
/// in the public API, while preserving the `source()` chain.
Bip21(bitcoin_uri::de::UriError),
Bip21(bitcoin_uri::Error),
/// The address does not belong to the requested network.
Network(bitcoin_uri::NetworkError),
/// The URI carries no on-chain address, which payjoin requires.
MissingAddress,
/// The URI has a `req-` parameter this crate does not understand, so
/// BIP 321 requires rejecting it. Holds the full key, prefix included.
UnsatisfiedRequirement(String),
/// The payjoin parameters are invalid.
PayjoinParams(PjParseError),
}

impl UriParseError {
/// Erases the foreign `bitcoin_uri` parse error into this opaque type.
/// Erases a `bitcoin_uri` parse error into this opaque type.
///
/// This is an inherent constructor rather than a `From` impl so that
/// These are inherent constructors rather than `From` impls so that
/// `bitcoin_uri` types stay out of the public API.
pub(super) fn from_bip21_error(value: bitcoin_uri::de::Error<PjParseError>) -> Self {
match value {
bitcoin_uri::de::Error::Uri(e) => UriParseError(InternalUriParseError::Bip21(e)),
bitcoin_uri::de::Error::Extras(e) =>
UriParseError(InternalUriParseError::PayjoinParams(e)),
}
pub(super) fn from_bip21(e: bitcoin_uri::Error) -> Self {
UriParseError(InternalUriParseError::Bip21(e))
}

pub(super) fn from_network(e: bitcoin_uri::NetworkError) -> Self {
UriParseError(InternalUriParseError::Network(e))
}

pub(super) fn missing_address() -> Self { UriParseError(InternalUriParseError::MissingAddress) }

pub(super) fn unsatisfied_requirement(key: String) -> Self {
UriParseError(InternalUriParseError::UnsatisfiedRequirement(key))
}

/// The payjoin parameter parse error, if parsing failed because the payjoin
Expand All @@ -38,15 +51,24 @@ impl UriParseError {
pub(crate) fn payjoin_params(&self) -> Option<&PjParseError> {
match &self.0 {
InternalUriParseError::PayjoinParams(e) => Some(e),
InternalUriParseError::Bip21(_) => None,
_ => None,
}
}
}

impl From<PjParseError> for UriParseError {
fn from(e: PjParseError) -> Self { UriParseError(InternalUriParseError::PayjoinParams(e)) }
}

impl std::fmt::Display for UriParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
InternalUriParseError::Bip21(e) => write!(f, "Invalid BIP21 URI: {e}"),
InternalUriParseError::Network(e) => write!(f, "Invalid network: {e}"),
InternalUriParseError::MissingAddress => write!(f, "BIP21 URI has no on-chain address"),
InternalUriParseError::UnsatisfiedRequirement(key) => {
write!(f, "BIP21 URI requires unsupported parameter '{key}'")
}
InternalUriParseError::PayjoinParams(e) => write!(f, "Invalid payjoin parameters: {e}"),
}
}
Expand All @@ -56,6 +78,9 @@ impl std::error::Error for UriParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.0 {
InternalUriParseError::Bip21(e) => Some(e),
InternalUriParseError::Network(e) => Some(e),
InternalUriParseError::MissingAddress => None,
InternalUriParseError::UnsatisfiedRequirement(_) => None,
InternalUriParseError::PayjoinParams(e) => Some(e),
}
}
Expand Down
Loading