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