Repository navigation
0.2: owned Uri<V> BIP 321 parser with optional body address #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a48512f
Declare MSRV 1.63 and require bitcoin 0.32.102
DanGould 1105b33
Add internal Params tokenizer and value decoder
DanGould 7f1fef7
Switch Uri to an owned model
DanGould 8cd0f98
Make the URI body optional
DanGould d9250f1
Validate networks for the body and HRP params
DanGould b833956
Report unsatisfied req- parameters
DanGould cc2506f
Make Display byte-exact for a parsed URI
DanGould abbc441
Add UriBuilder
DanGould 680704b
Test BIP 321's example URIs
DanGould File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| //! Reading extension parameters off a parsed URI. | ||
| //! | ||
| //! `bitcoin_uri` is a tokenizer: it parses the BIP 321 shape and hands back | ||
| //! every parameter still percent-encoded, without knowing what any of them | ||
| //! mean. An extension (payjoin's `pj`, a silent-payment `sp`, a BOLT 11 | ||
| //! `lightning` invoice, anything else) reads whatever it needs straight off | ||
| //! the parsed [`Uri`] with [`Uri::get`]/[`Uri::get_all`] and decodes it with | ||
| //! [`ParamEntry::decoded_value`]. This example walks a body-less URI that | ||
| //! carries three unrelated extensions at once. | ||
| //! | ||
| //! Run with: `cargo run --example extension_params` | ||
|
|
||
| use bitcoin_uri::Uri; | ||
|
|
||
| fn main() { | ||
| // A body-less URI (BIP 321 allows one): no on-chain address, just three | ||
| // extension parameters, one of them marked `req-`. | ||
| let input = "bitcoin:?sp=sp1qsilentpaymentaddress\ | ||
| &lightning=lnbc420bogusinvoice\ | ||
| &req-pj=https://example.com/pay%3Ftoken%3Dabc"; | ||
|
|
||
| let uri: Uri<_> = input.parse().expect("valid BIP 321 URI"); | ||
|
|
||
| // No body, so this is an extension-only payment request. | ||
| assert!(uri.body().is_none()); | ||
|
|
||
| // A silent-payment wallet reads `sp` without the crate knowing what it is. | ||
| if let Some(entry) = uri.get("sp") { | ||
| let address = entry.decoded_value().expect("sp has a value").expect("valid UTF-8"); | ||
| println!("sp: {address}"); | ||
| } | ||
|
|
||
| // `lightning` carries a BOLT 11 invoice (BOLT 12 offers use `lno`). The | ||
| // value comes back in the case it arrived in, which is uppercase if it | ||
| // came from a QR code. | ||
| if let Some(entry) = uri.get("lightning") { | ||
| let invoice = entry.decoded_value().expect("lightning has a value").expect("valid UTF-8"); | ||
| println!("lightning: {invoice}"); | ||
| } | ||
|
|
||
| // `get` matches by base key, so it finds `req-pj` when asked for `pj`. | ||
| // `decoded_value` applies RFC 3986 percent-decoding on demand, turning | ||
| // `%3F` back into `?` and `%3D` into `=`. | ||
| if let Some(entry) = uri.get("pj") { | ||
| let endpoint = entry.decoded_value().expect("pj has a value").expect("valid UTF-8"); | ||
| println!("pj endpoint: {endpoint} (required: {})", entry.is_required()); | ||
| } | ||
|
|
||
| // A consumer declares the extensions it understands; anything `req-` it | ||
| // does not understand means it MUST reject the URI. Here it handles `pj` | ||
| // and `sp` but not, say, a hypothetical `req-foo`. | ||
| let understood = ["pj", "sp", "lightning"]; | ||
| let unsatisfied = uri.unsatisfied_requirements(&understood).count(); | ||
| assert_eq!(unsatisfied, 0, "every required extension is understood"); | ||
| println!("all {} parameters understood; safe to act on", uri.params().len()); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to include
--all-featuresas we don't default to includingstdas a default feature.