unified_uri
A Rust library for parsing unified Bitcoin URIs that support both Lightning Network invoices and Payjoin parameters, based on the BIP21 URI specification.
Overview
This crate extends the BIP21 URI standard to support unified QR codes that contain both on-chain Bitcoin addresses and Lightning Network payment information, as well as Payjoin parameters. This enables a single QR code to work with both on-chain and Lightning wallets, eliminating the need for separate payment interfaces.
Features
- Lightning Network Support: Parse BOLT11 invoices from BIP21 URIs using the
lightningparameter - Payjoin Integration: Support for payjoin endpoints (
pj) and output substitution control (pjos) - Backwards Compatible: Works with standard BIP21 URIs (on-chain only)
- Security: Validates payjoin endpoints to ensure they use secure protocols (HTTPS or .onion domains)
Usage
Add this to your Cargo.toml:
[]
= "0.1"
Basic Example
use UnifiedUri;
use FromStr;
Building URIs
use UnifiedUriBuilder;
use ;
use FromStr;
Parsing Different URI Types
use UnifiedUri;
// On-chain only (standard BIP21)
let onchain_uri = "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd";
// With Lightning invoice
let lightning_uri = "bitcoin:BC1QYLH3U67J673H6Y6ALV70M0PL2YZ53TZHVXGG7U?lightning=LNBC10U1P3PJ257PP5YZTKWJCZ5FTL5LAXKAV23ZMZEKAW37ZK6KMV80PK4XAEV5QHTZ7QDPDWD3XGER9WD5KWM36YPRX7U3QD36KUCMGYP282ETNV3SHJCQZPGXQYZ5VQSP5USYC4LK9CHSFP53KVCNVQ456GANH60D89REYKDNGSMTJ6YW3NHVQ9QYYSSQJCEWM5CJWZ4A6RFJX77C490YCED6PEMK0UPKXHY89CMM7SCT66K8GNEANWYKZGDRWRFJE69H9U5U0W57RRCSYSAS7GADWMZXC8C6T0SPJAZUP6";
// With Payjoin parameters
let payjoin_uri = "bitcoin:BC1QYLH3U67J673H6Y6ALV70M0PL2YZ53TZHVXGG7U?pj=https://payjoin.example.com/payjoin&pjos=1";
// All three can be parsed with UnifiedUri
let unified_uri = from_str?;
BIP21 Parameters Supported
| Parameter | Type | Description |
|---|---|---|
lightning |
String | BOLT11 Lightning invoice |
pj |
String | Payjoin endpoint URL |
pjos |
String | Payjoin output substitution (0 = enabled, 1 = disabled) |
Unified QR Codes
This crate enables the creation and parsing of unified QR codes as described in the Unified QR Code specification. These QR codes allow a single payment request to work with:
- On-chain only wallets: Ignore Lightning and Payjoin parameters
- Lightning wallets: Use the Lightning invoice when present
- Payjoin-compatible wallets: Use the Payjoin endpoint for enhanced privacy
API Reference
UnifiedUri<'a>
The main type for parsing unified BIP21 URIs. This is a type alias for Uri<'a, NetworkUnchecked, UnifiedExtras>.
UnifiedExtras
Contains the extra parameters parsed from the URI:
lightning: Option<Bolt11Invoice>- Lightning invoice if presentpj: Option<Url>- Payjoin endpoint URL if presentpjos: Option<bool>- Payjoin output substitution setting
Methods
disable_output_substitution() -> bool- Returnstrueif payjoin output substitution should be disabled
UnifiedUriBuilder
Builder for creating unified BIP21 URI strings with optional Lightning and Payjoin parameters.
Methods
new(address: Address) -> Self- Create a new builder instance with required Bitcoin addressamount(self, amount: Amount) -> Self- Set the payment amount in satoshislabel<S: Into<String>>(self, label: S) -> Self- Set the payment labelmessage<S: Into<String>>(self, message: S) -> Self- Set the payment messagelightning_invoice(self, invoice: Bolt11Invoice) -> Self- Set the Lightning invoice from Bolt11Invoice structlightning<S: Into<String>>(self, invoice: S) -> Self- Set the Lightning invoice from stringpayjoin_url<S: Into<String>>(self, url: S) -> Self- Set the Payjoin endpoint URL from stringpayjoin(self, url: Url) -> Self- Set the Payjoin endpoint URL from Url structdisable_output_substitution(self, disable: bool) -> Self- Set whether to disable output substitutionbuild(self) -> String- Build the final URI string
Security Considerations
- Payjoin endpoints are validated to ensure they use secure protocols (HTTPS or .onion domains)
- Malformed Lightning invoices will result in parsing errors
- Multiple parameters of the same type are not allowed and will cause errors
Dependencies
bip21- BIP21 URI parsingbitcoin- Bitcoin address handlinglightning-invoice- BOLT11 invoice parsingurl- URL validation
References
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.