@@ -1526,6 +1526,101 @@ where
1526
1526
/// # }
1527
1527
/// ```
1528
1528
///
1529
+ /// ## BOLT 12 Offers
1530
+ ///
1531
+ /// The [`offers`] module is useful for creating BOLT 12 offers. An [`Offer`] is a precursor to a
1532
+ /// [`Bolt12Invoice`], which must first be requested by the payer. The interchange of these messages
1533
+ /// as defined in the specification is handled by [`ChannelManager`] and its implementation of
1534
+ /// [`OffersMessageHandler`]. However, this only works with an [`Offer`] created using a builder
1535
+ /// returned by [`create_offer_builder`]. With this approach, BOLT 12 offers and invoices are
1536
+ /// stateless just as BOLT 11 invoices are.
1537
+ ///
1538
+ /// ```
1539
+ /// # use lightning::events::{Event, EventsProvider, PaymentPurpose};
1540
+ /// # use lightning::ln::channelmanager::AChannelManager;
1541
+ /// # use lightning::offers::parse::Bolt12SemanticError;
1542
+ /// #
1543
+ /// # fn example<T: AChannelManager>(channel_manager: T) -> Result<(), Bolt12SemanticError> {
1544
+ /// # let channel_manager = channel_manager.get_cm();
1545
+ /// let offer = channel_manager
1546
+ /// .create_offer_builder("coffee".to_string())?
1547
+ /// .amount_msats(10_000_000)
1548
+ /// .build()?;
1549
+ /// let bech32_offer = offer.to_string();
1550
+ ///
1551
+ /// // On the event processing thread
1552
+ /// channel_manager.process_pending_events(&|event| match event {
1553
+ /// Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
1554
+ /// PaymentPurpose::InvoicePayment { payment_preimage: Some(payment_preimage), .. } => {
1555
+ /// println!("Claiming payment {}", payment_hash);
1556
+ /// channel_manager.claim_funds(payment_preimage);
1557
+ /// },
1558
+ /// PaymentPurpose::InvoicePayment { payment_preimage: None, .. } => {
1559
+ /// println!("Unknown payment hash: {}", payment_hash);
1560
+ /// },
1561
+ /// // ...
1562
+ /// # _ => {},
1563
+ /// },
1564
+ /// Event::PaymentClaimed { payment_hash, amount_msat, .. } => {
1565
+ /// println!("Claimed {} msats", amount_msat);
1566
+ /// },
1567
+ /// // ...
1568
+ /// # _ => {},
1569
+ /// });
1570
+ /// # Ok(())
1571
+ /// # }
1572
+ /// ```
1573
+ ///
1574
+ /// Use [`pay_for_offer`] to initiated payment, which sends an [`InvoiceRequest`] for an [`Offer`]
1575
+ /// and pays the [`Bolt12Invoice`] response. In addition to success and failure events,
1576
+ /// [`ChannelManager`] may also generate an [`Event::InvoiceRequestFailed`].
1577
+ ///
1578
+ /// ```
1579
+ /// # use lightning::events::{Event, EventsProvider};
1580
+ /// # use lightning::ln::channelmanager::{AChannelManager, PaymentId, RecentPaymentDetails, Retry};
1581
+ /// # use lightning::offers::offer::Offer;
1582
+ /// #
1583
+ /// # fn example<T: AChannelManager>(
1584
+ /// # channel_manager: T, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
1585
+ /// # payer_note: Option<String>, retry: Retry, max_total_routing_fee_msat: Option<u64>
1586
+ /// # ) {
1587
+ /// # let channel_manager = channel_manager.get_cm();
1588
+ /// let payment_id = PaymentId([42; 32]);
1589
+ /// match channel_manager.pay_for_offer(
1590
+ /// offer, quantity, amount_msats, payer_note, payment_id, retry, max_total_routing_fee_msat
1591
+ /// ) {
1592
+ /// Ok(()) => println!("Requesting invoice for offer"),
1593
+ /// Err(e) => println!("Unable to request invoice for offer: {:?}", e),
1594
+ /// }
1595
+ ///
1596
+ /// // First the payment will be waiting on an invoice
1597
+ /// let expected_payment_id = payment_id;
1598
+ /// assert!(
1599
+ /// channel_manager.list_recent_payments().iter().find(|details| matches!(
1600
+ /// details,
1601
+ /// RecentPaymentDetails::AwaitingInvoice { payment_id: expected_payment_id }
1602
+ /// )).is_some()
1603
+ /// );
1604
+ ///
1605
+ /// // Once the invoice is received, a payment will be sent
1606
+ /// assert!(
1607
+ /// channel_manager.list_recent_payments().iter().find(|details| matches!(
1608
+ /// details,
1609
+ /// RecentPaymentDetails::Pending { payment_id: expected_payment_id, .. }
1610
+ /// )).is_some()
1611
+ /// );
1612
+ ///
1613
+ /// // On the event processing thread
1614
+ /// channel_manager.process_pending_events(&|event| match event {
1615
+ /// Event::PaymentSent { payment_id: Some(payment_id), .. } => println!("Paid {}", payment_id),
1616
+ /// Event::PaymentFailed { payment_id, .. } => println!("Failed paying {}", payment_id),
1617
+ /// Event::InvoiceRequestFailed { payment_id, .. } => println!("Failed paying {}", payment_id),
1618
+ /// // ...
1619
+ /// # _ => {},
1620
+ /// });
1621
+ /// # }
1622
+ /// ```
1623
+ ///
1529
1624
/// # Persistence
1530
1625
///
1531
1626
/// Implements [`Writeable`] to write out all channel state to disk. Implies [`peer_disconnected`] for
@@ -1601,6 +1696,10 @@ where
1601
1696
/// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
1602
1697
/// [`claim_funds`]: Self::claim_funds
1603
1698
/// [`send_payment`]: Self::send_payment
1699
+ /// [`offers`]: crate::offers
1700
+ /// [`create_offer_builder`]: Self::create_offer_builder
1701
+ /// [`pay_for_offer`]: Self::pay_for_offer
1702
+ /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
1604
1703
/// [`peer_disconnected`]: msgs::ChannelMessageHandler::peer_disconnected
1605
1704
/// [`funding_created`]: msgs::FundingCreated
1606
1705
/// [`funding_transaction_generated`]: Self::funding_transaction_generated
0 commit comments