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