Repository navigation
Migrate URI parsing to bitcoin_uri 0.2 - #1975
caarloshenriq wants to merge 4 commits into
Conversation
991add4 to
ad753c5
Compare
Coverage Report for CI Build 44314772033Coverage increased (+0.1%) to 87.842%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
f3306ff to
c81da26
Compare
|
Merged 0.2.0 which is now on crates.io, so this can be taken out of draft TODO:
After this merges, the next thing to do is reading |
c81da26 to
89fa5b6
Compare
2299db4 to
6ece25d
Compare
xstoicunicornx
left a comment
There was a problem hiding this comment.
Interested in hearing your thoughts on the set_amount comment, since I'm not at all confident that my feedback is the most appropriate way to handle this. I think I am pretty confident that we would want a way to retain the req- prefixes when appropriate when constructing one uri from another, but could be convinced that it's fine to accept this regression for now and clean it up in a follow up.
| /// This rebuilds the URI 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(&mut self, amount: Amount) { |
There was a problem hiding this comment.
Parsing req-pj=… and then calling set_amount prints pj=…. The same happens to req-pjos. This seems to be because the Uri is completely rebuilt piecemeal rather than from the original uri.
I almost think bitcoin_uri should add UriBuilder::from(Uri), or an into_builder(), that seeds the builder with the parsed entries. Alternatively, we could just add a set_amount type helper to bitcoin_uri. Honestly both seem to be nice to have if we expect this to be a universal parser leveraged for things other than just payjoin, however the set_amount helper would probably be the easiest for this PR.
There was a problem hiding this comment.
Dropping req-pj is worse than dropping lightning, since it turns a required payjoin into an optional one, so I fixed that here: set_amount now keeps the req- prefix on amount, pj and pjos when the parsed URI had it. Didn't want to block this PR on a bitcoin_uri release for into_builder.
Other params like lightning are still dropped, which is the documented behavior from #2050. Receivers that want them in the URI should build it with UriBuilder once step 2 there lands, and into_builder looks like a good fit for that
| pub(crate) struct PayjoinExtrasAdapter(pub(crate) PayjoinExtras); | ||
| /// The extension parameters this crate handles, so `req-pj` and `req-pjos` | ||
| /// are accepted while any other `req-` parameter rejects the URI. | ||
| const UNDERSTOOD_PARAMS: &[&str] = &["pj", "pjos"]; |
There was a problem hiding this comment.
The bots are suggesting that we should include "amount" here, so that we can accept "req-amount" param. Also maybe add a test for it. They note that this is not a regression introduced by this PR.
There was a problem hiding this comment.
Added amount, since amount() already reads req-amount through the base key and bitcoin_uri's docs say a caller that honors req-amount should list it. Added a test for it, plus req-label in the rejected cases so it's clear only amount was opened up
| } | ||
| } | ||
|
|
||
| impl From<PjParseError> for UriParseError { |
There was a problem hiding this comment.
I don't know if we need this to be public? Since PjParseError and UriParseError are both re-exported this impl would travel with them.
There was a problem hiding this comment.
Agreed, replaced it with a pub(super) UriParseError::from_payjoin_params like the other constructors, so nothing leaks into the public API.
bitcoin_uri 0.2 is a plain BIP 321 tokenizer and no longer takes a deserialization hook for extension params, so the payjoin adapter traits go away. Uri and PjUri now wrap an owned bitcoin_uri::Uri and read pj/pjos with get_all, keeping the payjoin rules (duplicates, pjos without pj, endpoint checks) in this crate. bitcoin_uri 0.2 reports req- parameters instead of rejecting them, so the BIP 321 rule that a URI with a required parameter we cannot handle MUST be rejected is enforced here. req-pj and req-pjos are understood. UriBuilder::build needs a network, but a checked address does not expose its own, so network_of probes for one the address is valid for. check_pj_supported rebuilds the inner URI so Display keeps the canonical param order (standard params, then pjos, then pj) whatever the input order was. The public API and the emitted URI are unchanged, so senders, receivers and their tests are untouched.
Pin how the payjoin Uri consumes bitcoin_uri's BIP 321 tokenizer: unknown req- parameters are rejected while req-pj and req-pjos are understood, keys match case-insensitively, a URI needs a body address, and every network survives a print and re-parse. Display is checked to be canonical: all 120 orderings of the same five parameters print identically, and printing is a fixpoint after one round.
The integration tests hand the sender the receiver's own printed URI. A wallet shows it differently in a QR code: upper-case scheme, keys and bech32 address, a lightning fallback, pj marked as required and the parameters reordered. Pay such a URI over v1 on regtest to check that bitcoin_uri 0.2 parsing holds up through a full payjoin
check_pj_supported rebuilt the URI from the parameters payjoin reads, so printing a sender's PjUri dropped any other parameter, such as a lightning or sp fallback, and reordered the rest. Keep the parsed bitcoin_uri::Uri instead, so a PjUri prints exactly what it parsed. set_amount still rebuilds the URI, so document that it drops those parameters on a parsed URI. Replacing it is left to a follow-up. Tests that pinned the canonical output now check that the input is printed unchanged.
6ece25d to
d8f4b71
Compare
xstoicunicornx
left a comment
There was a problem hiding this comment.
utACK d8f4b71
This is a straightforward change to upgrade bitcoin_uri dependency to v0.2 which enables more universal bip321 token parsing.
There are two follow ups that I don't think should hold up merging this:
- Create an
into_builder()api forbitcoin_urito allow for rebuilding aUrifrom an existing one while retainingreq-params appropriately. I believe this is also tracked as step 2 from #2050. - Update
set_amount()to leverage the newbitcoin_uriapi
@caarloshenriq let me know if either of these 2 follow ups need to be adjusted.
Moves payjoin onto bitcoin_uri 0.2. The 0.2 parser is a plain BIP 321 tokenizer, so the adapter traits we had for 0.1 are gone and pj/pjos handling now lives in this crate.
What changes:
UriandPjUriwrap an ownedbitcoin_uri::Uriand read pj/pjos off it. Duplicate params, pjos without pj and the endpoint checks are enforced here.check_pj_supportedrebuilds the URI in canonical order (standard params, then pjos, then pj), so the printed URI is the same as before whatever the input order.The public API and the emitted URIs don't change, so send/receive code and their tests are untouched.
Tests: a bip321 module covering the req- rules, case-insensitive keys, all 120 orderings of five params printing the same, printing being a fixpoint, and every network round-tripping. Plus an integration test that pays, over v1 on regtest, a URI shaped the way a wallet shows it in a QR code (upper case, a lightning fallback, req-pj, reordered params).
Reviewer: @DanGould
Disclosure: co-authored by Claude
Pull Request Checklist
Please confirm the following before requesting review: