From b17921b95ea81d66ad4c497962efc36b55cc4723 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Wed, 25 Mar 2026 18:10:12 +0100 Subject: [PATCH 1/2] feat: add BOLT12 payer proof support Integrated payer proof support for outbound BOLT12 payments, including persisted invoice context handling and UniFFI exposure. AI-assisted-by: Codex --- Cargo.toml | 48 ++++++------ bindings/ldk_node.udl | 3 + src/builder.rs | 48 ++++++++---- src/config.rs | 1 + src/error.rs | 11 +++ src/event.rs | 114 +++++++++++++++++++++++++---- src/ffi/types.rs | 87 ++++++++++++++++++++++ src/io/mod.rs | 4 + src/io/utils.rs | 76 +++++++++++++++++++ src/lib.rs | 9 ++- src/payment/bolt12.rs | 121 ++++++++++++++++++++++++++++++- src/payment/mod.rs | 3 +- src/payment/payer_proof_store.rs | 108 +++++++++++++++++++++++++++ src/types.rs | 2 + 14 files changed, 576 insertions(+), 59 deletions(-) create mode 100644 src/payment/payer_proof_store.rs diff --git a/Cargo.toml b/Cargo.toml index a9354cbad..f99d9baa7 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,17 +39,17 @@ default = [] #lightning-liquidity = { version = "0.2.0", features = ["std"] } #lightning-macros = { version = "0.2.0" } -lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953", features = ["std"] } -lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953" } -lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953", features = ["std"] } -lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953" } -lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953", features = ["tokio"] } -lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953" } -lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953" } -lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953", features = ["rest-client", "rpc-client", "tokio"] } -lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953", features = ["esplora-async-https", "time", "electrum-rustls-ring"] } -lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953", features = ["std"] } -lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953" } +lightning = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3", features = ["std"] } +lightning-types = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-invoice = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3", features = ["std"] } +lightning-net-tokio = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-persister = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3", features = ["tokio"] } +lightning-background-processor = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-rapid-gossip-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-block-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3", features = ["rest-client", "rpc-client", "tokio"] } +lightning-transaction-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3", features = ["esplora-async-https", "time", "electrum-rustls-ring"] } +lightning-liquidity = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3", features = ["std"] } +lightning-macros = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } bdk_chain = { version = "0.23.0", default-features = false, features = ["std"] } bdk_esplora = { version = "0.22.0", default-features = false, features = ["async-https-rustls", "tokio"]} @@ -85,7 +85,7 @@ bitcoin-payment-instructions = { git = "https://github.com/tnull/bitcoin-payment winapi = { version = "0.3", features = ["winbase"] } [dev-dependencies] -lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "49912057895ddfbd69d503de67c80d5576c09953", features = ["std", "_test_utils"] } +lightning = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3", features = ["std", "_test_utils"] } rand = { version = "0.9.2", default-features = false, features = ["std", "thread_rng", "os_rng"] } proptest = "1.0.0" regex = "1.5.6" @@ -171,15 +171,15 @@ harness = false #vss-client-ng = { path = "../vss-client" } #vss-client-ng = { git = "https://github.com/lightningdevkit/vss-client", branch = "main" } # -#[patch."https://github.com/lightningdevkit/rust-lightning"] -#lightning = { path = "../rust-lightning/lightning" } -#lightning-types = { path = "../rust-lightning/lightning-types" } -#lightning-invoice = { path = "../rust-lightning/lightning-invoice" } -#lightning-net-tokio = { path = "../rust-lightning/lightning-net-tokio" } -#lightning-persister = { path = "../rust-lightning/lightning-persister" } -#lightning-background-processor = { path = "../rust-lightning/lightning-background-processor" } -#lightning-rapid-gossip-sync = { path = "../rust-lightning/lightning-rapid-gossip-sync" } -#lightning-block-sync = { path = "../rust-lightning/lightning-block-sync" } -#lightning-transaction-sync = { path = "../rust-lightning/lightning-transaction-sync" } -#lightning-liquidity = { path = "../rust-lightning/lightning-liquidity" } -#lightning-macros = { path = "../rust-lightning/lightning-macros" } +[patch."https://github.com/lightningdevkit/rust-lightning"] +lightning = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-types = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-invoice = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-net-tokio = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-persister = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-background-processor = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-rapid-gossip-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-block-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-transaction-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-liquidity = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } +lightning-macros = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "96f1d193b75cf9534def6cc2b3e65f1aaea5d4e3" } diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 014993690..29ed4df94 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -185,6 +185,8 @@ enum NodeError { "FeerateEstimationUpdateTimeout", "WalletOperationFailed", "WalletOperationTimeout", + "PayerProofCreationFailed", + "PayerProofUnavailable", "OnchainTxSigningFailed", "TxSyncFailed", "TxSyncTimeout", @@ -225,6 +227,7 @@ enum NodeError { "LnurlAuthFailed", "LnurlAuthTimeout", "InvalidLnurl", + "InvalidPayerProof", }; typedef dictionary NodeStatus; diff --git a/src/builder.rs b/src/builder.rs index 806c676b3..532a217ce 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -56,12 +56,14 @@ use crate::gossip::GossipSource; use crate::io::sqlite_store::SqliteStore; use crate::io::utils::{ read_event_queue, read_external_pathfinding_scores_from_cache, read_network_graph, - read_node_metrics, read_output_sweeper, read_payments, read_peer_info, read_pending_payments, - read_scorer, write_node_metrics, + read_node_metrics, read_output_sweeper, read_payer_proof_contexts, read_payments, + read_peer_info, read_pending_payments, read_scorer, write_node_metrics, }; use crate::io::vss_store::VssStoreBuilder; use crate::io::{ - self, PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE, PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE, + self, PAYER_PROOF_CONTEXT_PERSISTENCE_PRIMARY_NAMESPACE, + PAYER_PROOF_CONTEXT_PERSISTENCE_SECONDARY_NAMESPACE, + PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE, PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE, PENDING_PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE, PENDING_PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE, }; @@ -77,8 +79,8 @@ use crate::runtime::{Runtime, RuntimeSpawner}; use crate::tx_broadcaster::TransactionBroadcaster; use crate::types::{ AsyncPersister, ChainMonitor, ChannelManager, DynStore, DynStoreWrapper, GossipSync, Graph, - KeysManager, MessageRouter, OnionMessenger, PaymentStore, PeerManager, PendingPaymentStore, - Persister, SyncAndAsyncKVStore, + KeysManager, MessageRouter, OnionMessenger, PayerProofContextStore, PaymentStore, PeerManager, + PendingPaymentStore, Persister, SyncAndAsyncKVStore, }; use crate::wallet::persist::KVStoreWalletPersister; use crate::wallet::Wallet; @@ -1260,14 +1262,19 @@ fn build_with_store_internal( let kv_store_ref = Arc::clone(&kv_store); let logger_ref = Arc::clone(&logger); - let (payment_store_res, node_metris_res, pending_payment_store_res) = - runtime.block_on(async move { - tokio::join!( - read_payments(&*kv_store_ref, Arc::clone(&logger_ref)), - read_node_metrics(&*kv_store_ref, Arc::clone(&logger_ref)), - read_pending_payments(&*kv_store_ref, Arc::clone(&logger_ref)) - ) - }); + let ( + payment_store_res, + node_metris_res, + pending_payment_store_res, + payer_proof_context_store_res, + ) = runtime.block_on(async move { + tokio::join!( + read_payments(&*kv_store_ref, Arc::clone(&logger_ref)), + read_node_metrics(&*kv_store_ref, Arc::clone(&logger_ref)), + read_pending_payments(&*kv_store_ref, Arc::clone(&logger_ref)), + read_payer_proof_contexts(&*kv_store_ref, Arc::clone(&logger_ref)) + ) + }); // Initialize the status fields. let node_metrics = match node_metris_res { @@ -1296,6 +1303,20 @@ fn build_with_store_internal( }, }; + let payer_proof_context_store = match payer_proof_context_store_res { + Ok(contexts) => Arc::new(PayerProofContextStore::new( + contexts, + PAYER_PROOF_CONTEXT_PERSISTENCE_PRIMARY_NAMESPACE.to_string(), + PAYER_PROOF_CONTEXT_PERSISTENCE_SECONDARY_NAMESPACE.to_string(), + Arc::clone(&kv_store), + Arc::clone(&logger), + )), + Err(e) => { + log_error!(logger, "Failed to read payer proof contexts from store: {}", e); + return Err(BuildError::ReadFailed); + }, + }; + let (chain_source, chain_tip_opt) = match chain_data_source_config { Some(ChainDataSourceConfig::Esplora { server_url, headers, sync_config }) => { let sync_config = sync_config.unwrap_or(EsploraSyncConfig::default()); @@ -1992,6 +2013,7 @@ fn build_with_store_internal( scorer, peer_store, payment_store, + payer_proof_context_store, lnurl_auth, is_running, node_metrics, diff --git a/src/config.rs b/src/config.rs index 71e4d2314..8300c4882 100644 --- a/src/config.rs +++ b/src/config.rs @@ -342,6 +342,7 @@ pub(crate) fn default_user_config(config: &Config) -> UserConfig { user_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = config.anchor_channels_config.is_some(); user_config.reject_inbound_splices = false; + user_config.manually_handle_bolt12_invoices = true; if may_announce_channel(config).is_err() { user_config.accept_forwards_to_priv_channels = false; diff --git a/src/error.rs b/src/error.rs index d07212b00..1fdef8360 100644 --- a/src/error.rs +++ b/src/error.rs @@ -57,6 +57,10 @@ pub enum Error { WalletOperationFailed, /// A wallet operation timed out. WalletOperationTimeout, + /// Creating a payer proof failed. + PayerProofCreationFailed, + /// A payer proof is unavailable for the requested payment. + PayerProofUnavailable, /// A signing operation for transaction failed. OnchainTxSigningFailed, /// A transaction sync operation failed. @@ -137,6 +141,8 @@ pub enum Error { LnurlAuthTimeout, /// The provided lnurl is invalid. InvalidLnurl, + /// The provided payer proof is invalid. + InvalidPayerProof, } impl fmt::Display for Error { @@ -168,6 +174,10 @@ impl fmt::Display for Error { }, Self::WalletOperationFailed => write!(f, "Failed to conduct wallet operation."), Self::WalletOperationTimeout => write!(f, "A wallet operation timed out."), + Self::PayerProofCreationFailed => write!(f, "Failed to create payer proof."), + Self::PayerProofUnavailable => { + write!(f, "A payer proof is unavailable for the requested payment.") + }, Self::OnchainTxSigningFailed => write!(f, "Failed to sign given transaction."), Self::TxSyncFailed => write!(f, "Failed to sync transactions."), Self::TxSyncTimeout => write!(f, "Syncing transactions timed out."), @@ -222,6 +232,7 @@ impl fmt::Display for Error { Self::LnurlAuthFailed => write!(f, "LNURL-auth authentication failed."), Self::LnurlAuthTimeout => write!(f, "LNURL-auth authentication timed out."), Self::InvalidLnurl => write!(f, "The provided lnurl is invalid."), + Self::InvalidPayerProof => write!(f, "The provided payer proof is invalid."), } } } diff --git a/src/event.rs b/src/event.rs index ccee8e50b..09fc93c80 100644 --- a/src/event.rs +++ b/src/event.rs @@ -14,6 +14,7 @@ use std::sync::{Arc, Mutex}; use bitcoin::blockdata::locktime::absolute::LockTime; use bitcoin::secp256k1::PublicKey; use bitcoin::{Amount, OutPoint}; +use lightning::blinded_path::message::OffersContext; use lightning::events::bump_transaction::BumpTransactionEvent; #[cfg(not(feature = "uniffi"))] use lightning::events::PaidBolt12Invoice; @@ -23,7 +24,13 @@ use lightning::events::{ }; use lightning::impl_writeable_tlv_based_enum; use lightning::ln::channelmanager::PaymentId; +use lightning::ln::outbound_payment::Bolt12PaymentError; use lightning::ln::types::ChannelId; +use lightning::offers::invoice::Bolt12Invoice; +use lightning::offers::invoice_error::InvoiceError; +use lightning::offers::parse::Bolt12SemanticError; +use lightning::onion_message::messenger::Responder; +use lightning::onion_message::offers::OffersMessage; use lightning::routing::gossip::NodeId; use lightning::sign::EntropySource; use lightning::util::config::{ @@ -49,12 +56,14 @@ use crate::liquidity::LiquiditySource; use crate::logger::{log_debug, log_error, log_info, log_trace, LdkLogger, Logger}; use crate::payment::asynchronous::om_mailbox::OnionMessageMailbox; use crate::payment::asynchronous::static_invoice_store::StaticInvoiceStore; +use crate::payment::payer_proof_store::PayerProofContext; use crate::payment::store::{ PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus, }; use crate::runtime::Runtime; use crate::types::{ - CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PaymentStore, Sweeper, Wallet, + CustomTlvRecord, DynStore, KeysManager, OnionMessenger, PayerProofContextStore, PaymentStore, + Sweeper, Wallet, }; use crate::{ hex_utils, BumpTransactionEventHandler, ChannelManager, Error, Graph, PeerInfo, PeerStore, @@ -507,6 +516,7 @@ where network_graph: Arc, liquidity_source: Option>>>, payment_store: Arc, + payer_proof_context_store: Arc, peer_store: Arc>, keys_manager: Arc, runtime: Arc, @@ -527,10 +537,11 @@ where channel_manager: Arc, connection_manager: Arc>, output_sweeper: Arc, network_graph: Arc, liquidity_source: Option>>>, - payment_store: Arc, peer_store: Arc>, - keys_manager: Arc, static_invoice_store: Option, - onion_messenger: Arc, om_mailbox: Option>, - runtime: Arc, logger: L, config: Arc, + payment_store: Arc, payer_proof_context_store: Arc, + peer_store: Arc>, keys_manager: Arc, + static_invoice_store: Option, onion_messenger: Arc, + om_mailbox: Option>, runtime: Arc, logger: L, + config: Arc, ) -> Self { Self { event_queue, @@ -542,6 +553,7 @@ where network_graph, liquidity_source, payment_store, + payer_proof_context_store, peer_store, keys_manager, logger, @@ -553,6 +565,66 @@ where } } + fn persist_payer_proof_context( + &self, payment_id: PaymentId, invoice: &Bolt12Invoice, context: Option<&OffersContext>, + ) -> Result<(), ReplayEvent> { + if let Some(context) = + PayerProofContext::from_invoice_received(payment_id, invoice, context) + { + match self.payer_proof_context_store.insert_or_update(context) { + Ok(_) => {}, + Err(e) => { + log_error!( + self.logger, + "Failed to persist payer proof context for {}: {}", + payment_id, + e + ); + return Err(ReplayEvent()); + }, + } + } + + Ok(()) + } + + fn remove_payer_proof_context(&self, payment_id: &PaymentId) -> Result<(), ReplayEvent> { + self.payer_proof_context_store.remove(payment_id).map_err(|e| { + log_error!( + self.logger, + "Failed to remove payer proof context for {}: {}", + payment_id, + e + ); + ReplayEvent() + }) + } + + fn respond_to_invoice_error(&self, error: Bolt12PaymentError, responder: Option) { + let error = match error { + Bolt12PaymentError::UnknownRequiredFeatures => { + InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures) + }, + Bolt12PaymentError::SendingFailed(e) => InvoiceError::from_string(format!("{:?}", e)), + Bolt12PaymentError::BlindedPathCreationFailed => InvoiceError::from_string( + "Failed to create a blinded path back to ourselves".to_string(), + ), + Bolt12PaymentError::UnexpectedInvoice | Bolt12PaymentError::DuplicateInvoice => return, + }; + + let Some(responder) = responder else { + log_trace!(self.logger, "No reply path available for invoice_error response"); + return; + }; + + if let Err(e) = self + .onion_messenger + .handle_onion_message_response(OffersMessage::InvoiceError(error), responder.respond()) + { + log_error!(self.logger, "Failed to send invoice_error response: {:?}", e); + } + } + pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> { match event { LdkEvent::FundingGenerationReady { @@ -1121,6 +1193,7 @@ where return Err(ReplayEvent()); }, }; + self.remove_payer_proof_context(&payment_id)?; let event = Event::PaymentFailed { payment_id: Some(payment_id), payment_hash, reason }; @@ -1542,20 +1615,14 @@ where }; }, LdkEvent::DiscardFunding { channel_id, funding_info } => { - if let FundingInfo::Contribution { inputs: _, outputs } = funding_info { + if let FundingInfo::Tx { transaction } = funding_info { log_info!( self.logger, "Reclaiming unused addresses from channel {} funding", channel_id, ); - let tx = bitcoin::Transaction { - version: bitcoin::transaction::Version::TWO, - lock_time: bitcoin::absolute::LockTime::ZERO, - input: vec![], - output: outputs, - }; - if let Err(e) = self.wallet.cancel_tx(&tx) { + if let Err(e) = self.wallet.cancel_tx(&transaction) { log_error!(self.logger, "Failed reclaiming unused addresses: {}", e); return Err(ReplayEvent()); } @@ -1579,8 +1646,25 @@ where .await; } }, - LdkEvent::InvoiceReceived { .. } => { - debug_assert!(false, "We currently don't handle BOLT12 invoices manually, so this event should never be emitted."); + LdkEvent::InvoiceReceived { payment_id, invoice, context, responder } => { + self.persist_payer_proof_context(payment_id, &invoice, context.as_ref())?; + + match self + .channel_manager + .send_payment_for_bolt12_invoice(&invoice, context.as_ref()) + { + Ok(()) => {}, + Err(e) => { + log_error!( + self.logger, + "Failed to initiate payment for BOLT12 invoice {}: {:?}", + payment_id, + e + ); + self.remove_payer_proof_context(&payment_id)?; + self.respond_to_invoice_error(e, responder); + }, + } }, LdkEvent::ConnectionNeeded { node_id, addresses } => { let spawn_logger = self.logger.clone(); diff --git a/src/ffi/types.rs b/src/ffi/types.rs index 5a1420882..70796bd5a 100644 --- a/src/ffi/types.rs +++ b/src/ffi/types.rs @@ -31,6 +31,7 @@ pub use lightning::ln::types::ChannelId; use lightning::offers::invoice::Bolt12Invoice as LdkBolt12Invoice; pub use lightning::offers::offer::OfferId; use lightning::offers::offer::{Amount as LdkAmount, Offer as LdkOffer}; +use lightning::offers::payer_proof::PayerProof as LdkPayerProof; use lightning::offers::refund::Refund as LdkRefund; use lightning::offers::static_invoice::StaticInvoice as LdkStaticInvoice; use lightning::onion_message::dns_resolution::HumanReadableName as LdkHumanReadableName; @@ -875,6 +876,92 @@ impl Readable for PaidBolt12Invoice { } } +/// A cryptographic proof that a BOLT12 invoice was paid by this node. +#[derive(Debug, Clone, uniffi::Object)] +#[uniffi::export(Debug, Display)] +pub struct PayerProof { + pub(crate) inner: LdkPayerProof, +} + +#[uniffi::export] +impl PayerProof { + #[uniffi::constructor] + pub fn from_bytes(proof_bytes: Vec) -> Result { + let inner = LdkPayerProof::try_from(proof_bytes).map_err(|_| Error::InvalidPayerProof)?; + Ok(Self { inner }) + } + + /// The payment preimage proving the payment completed. + pub fn preimage(&self) -> PaymentPreimage { + self.inner.preimage() + } + + /// The payment hash committed to by the invoice and proven by the preimage. + pub fn payment_hash(&self) -> PaymentHash { + self.inner.payment_hash() + } + + /// The public key of the payer that authorized the payment. + pub fn payer_id(&self) -> PublicKey { + self.inner.payer_id() + } + + /// The issuer signing public key committed to by the invoice. + pub fn issuer_signing_pubkey(&self) -> PublicKey { + self.inner.issuer_signing_pubkey() + } + + /// The invoice signature bytes. + pub fn invoice_signature(&self) -> Vec { + self.inner.invoice_signature().as_ref().to_vec() + } + + /// The payer signature bytes. + pub fn payer_signature(&self) -> Vec { + self.inner.payer_signature().as_ref().to_vec() + } + + /// The optional note attached to the proof. + pub fn payer_note(&self) -> Option { + self.inner.payer_note().map(|value| value.to_string()) + } + + /// The Merkle root committed to by the proof. + pub fn merkle_root(&self) -> Vec { + self.inner.merkle_root().to_byte_array().to_vec() + } + + /// The raw TLV bytes of the proof. + pub fn bytes(&self) -> Vec { + self.inner.bytes().to_vec() + } + + /// The bech32-encoded string form of the proof. + pub fn as_string(&self) -> String { + self.inner.to_string() + } +} + +impl From for PayerProof { + fn from(inner: LdkPayerProof) -> Self { + Self { inner } + } +} + +impl Deref for PayerProof { + type Target = LdkPayerProof; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl std::fmt::Display for PayerProof { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.inner) + } +} + uniffi::custom_type!(OfferId, String, { remote, try_lift: |val| { diff --git a/src/io/mod.rs b/src/io/mod.rs index e080d39f7..d5966d61f 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -82,3 +82,7 @@ pub(crate) const STATIC_INVOICE_STORE_PRIMARY_NAMESPACE: &str = "static_invoices /// The pending payment information will be persisted under this prefix. pub(crate) const PENDING_PAYMENT_INFO_PERSISTENCE_PRIMARY_NAMESPACE: &str = "pending_payments"; pub(crate) const PENDING_PAYMENT_INFO_PERSISTENCE_SECONDARY_NAMESPACE: &str = ""; + +/// The payer proof context will be persisted under this prefix. +pub(crate) const PAYER_PROOF_CONTEXT_PERSISTENCE_PRIMARY_NAMESPACE: &str = "payer_proof_contexts"; +pub(crate) const PAYER_PROOF_CONTEXT_PERSISTENCE_SECONDARY_NAMESPACE: &str = ""; diff --git a/src/io/utils.rs b/src/io/utils.rs index eef71ec0b..2915f1d57 100644 --- a/src/io/utils.rs +++ b/src/io/utils.rs @@ -42,8 +42,11 @@ use crate::config::WALLET_KEYS_SEED_LEN; use crate::fee_estimator::OnchainFeeEstimator; use crate::io::{ NODE_METRICS_KEY, NODE_METRICS_PRIMARY_NAMESPACE, NODE_METRICS_SECONDARY_NAMESPACE, + PAYER_PROOF_CONTEXT_PERSISTENCE_PRIMARY_NAMESPACE, + PAYER_PROOF_CONTEXT_PERSISTENCE_SECONDARY_NAMESPACE, }; use crate::logger::{log_error, LdkLogger, Logger}; +use crate::payment::payer_proof_store::PayerProofContext; use crate::payment::PendingPaymentDetails; use crate::peer_store::PeerStore; use crate::types::{Broadcaster, DynStore, KeysManager, Sweeper}; @@ -298,6 +301,79 @@ where Ok(res) } +/// Read previously persisted payer proof contexts from the store. +pub(crate) async fn read_payer_proof_contexts( + kv_store: &DynStore, logger: L, +) -> Result, std::io::Error> +where + L::Target: LdkLogger, +{ + let mut res = Vec::new(); + + let mut stored_keys = KVStore::list( + &*kv_store, + PAYER_PROOF_CONTEXT_PERSISTENCE_PRIMARY_NAMESPACE, + PAYER_PROOF_CONTEXT_PERSISTENCE_SECONDARY_NAMESPACE, + ) + .await?; + + const BATCH_SIZE: usize = 50; + + let mut set = tokio::task::JoinSet::new(); + + while set.len() < BATCH_SIZE && !stored_keys.is_empty() { + if let Some(next_key) = stored_keys.pop() { + let fut = KVStore::read( + &*kv_store, + PAYER_PROOF_CONTEXT_PERSISTENCE_PRIMARY_NAMESPACE, + PAYER_PROOF_CONTEXT_PERSISTENCE_SECONDARY_NAMESPACE, + &next_key, + ); + set.spawn(fut); + debug_assert!(set.len() <= BATCH_SIZE); + } + } + + while let Some(read_res) = set.join_next().await { + let reader = read_res + .map_err(|e| { + log_error!(logger, "Failed to read PayerProofContext: {}", e); + set.abort_all(); + e + })? + .map_err(|e| { + log_error!(logger, "Failed to read PayerProofContext: {}", e); + set.abort_all(); + e + })?; + + if let Some(next_key) = stored_keys.pop() { + let fut = KVStore::read( + &*kv_store, + PAYER_PROOF_CONTEXT_PERSISTENCE_PRIMARY_NAMESPACE, + PAYER_PROOF_CONTEXT_PERSISTENCE_SECONDARY_NAMESPACE, + &next_key, + ); + set.spawn(fut); + debug_assert!(set.len() <= BATCH_SIZE); + } + + let context = PayerProofContext::read(&mut &*reader).map_err(|e| { + log_error!(logger, "Failed to deserialize PayerProofContext: {}", e); + std::io::Error::new( + std::io::ErrorKind::InvalidData, + "Failed to deserialize PayerProofContext", + ) + })?; + res.push(context); + } + + debug_assert!(set.is_empty()); + debug_assert!(stored_keys.is_empty()); + + Ok(res) +} + /// Read `OutputSweeper` state from the store. pub(crate) async fn read_output_sweeper( broadcaster: Arc, fee_estimator: Arc, diff --git a/src/lib.rs b/src/lib.rs index 2e02e996c..237f77b23 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -174,8 +174,8 @@ use runtime::Runtime; pub use tokio; use types::{ Broadcaster, BumpTransactionEventHandler, ChainMonitor, ChannelManager, DynStore, Graph, - HRNResolver, KeysManager, OnionMessenger, PaymentStore, PeerManager, Router, Scorer, Sweeper, - Wallet, + HRNResolver, KeysManager, OnionMessenger, PayerProofContextStore, PaymentStore, PeerManager, + Router, Scorer, Sweeper, Wallet, }; pub use types::{ChannelDetails, CustomTlvRecord, PeerDetails, SyncAndAsyncKVStore, UserChannelId}; pub use vss_client; @@ -233,6 +233,7 @@ pub struct Node { scorer: Arc>, peer_store: Arc>>, payment_store: Arc, + payer_proof_context_store: Arc, lnurl_auth: Arc, is_running: Arc>, node_metrics: Arc>, @@ -585,6 +586,7 @@ impl Node { Arc::clone(&self.network_graph), self.liquidity_source.clone(), Arc::clone(&self.payment_store), + Arc::clone(&self.payer_proof_context_store), Arc::clone(&self.peer_store), Arc::clone(&self.keys_manager), static_invoice_store, @@ -904,6 +906,7 @@ impl Node { Arc::clone(&self.channel_manager), Arc::clone(&self.keys_manager), Arc::clone(&self.payment_store), + Arc::clone(&self.payer_proof_context_store), Arc::clone(&self.config), Arc::clone(&self.is_running), Arc::clone(&self.logger), @@ -920,6 +923,7 @@ impl Node { Arc::clone(&self.channel_manager), Arc::clone(&self.keys_manager), Arc::clone(&self.payment_store), + Arc::clone(&self.payer_proof_context_store), Arc::clone(&self.config), Arc::clone(&self.is_running), Arc::clone(&self.logger), @@ -1776,6 +1780,7 @@ impl Node { /// Remove the payment with the given id from the store. pub fn remove_payment(&self, payment_id: &PaymentId) -> Result<(), Error> { + self.payer_proof_context_store.remove(&payment_id)?; self.payment_store.remove(&payment_id) } diff --git a/src/payment/bolt12.rs b/src/payment/bolt12.rs index 980e20696..59631db8e 100644 --- a/src/payment/bolt12.rs +++ b/src/payment/bolt12.rs @@ -18,8 +18,10 @@ use lightning::ln::channelmanager::{OptionalOfferPaymentParams, PaymentId}; use lightning::ln::outbound_payment::Retry; use lightning::offers::offer::{Amount, Offer as LdkOffer, OfferFromHrn, Quantity}; use lightning::offers::parse::Bolt12SemanticError; +#[cfg(not(feature = "uniffi"))] +use lightning::offers::payer_proof::PayerProof as LdkPayerProof; use lightning::routing::router::RouteParametersConfig; -use lightning::sign::EntropySource; +use lightning::sign::{EntropySource, NodeSigner}; #[cfg(feature = "uniffi")] use lightning::util::ser::{Readable, Writeable}; use lightning_types::string::UntrustedString; @@ -28,8 +30,9 @@ use crate::config::{AsyncPaymentsRole, Config, LDK_PAYMENT_RETRY_TIMEOUT}; use crate::error::Error; use crate::ffi::{maybe_deref, maybe_wrap}; use crate::logger::{log_error, log_info, LdkLogger, Logger}; +use crate::payment::payer_proof_store::PayerProofContext; use crate::payment::store::{PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus}; -use crate::types::{ChannelManager, KeysManager, PaymentStore}; +use crate::types::{ChannelManager, KeysManager, PayerProofContextStore, PaymentStore}; #[cfg(not(feature = "uniffi"))] type Bolt12Invoice = lightning::offers::invoice::Bolt12Invoice; @@ -51,6 +54,11 @@ type HumanReadableName = lightning::onion_message::dns_resolution::HumanReadable #[cfg(feature = "uniffi")] type HumanReadableName = Arc; +#[cfg(not(feature = "uniffi"))] +type PayerProof = LdkPayerProof; +#[cfg(feature = "uniffi")] +type PayerProof = Arc; + /// A payment handler allowing to create and pay [BOLT 12] offers and refunds. /// /// Should be retrieved by calling [`Node::bolt12_payment`]. @@ -62,22 +70,43 @@ pub struct Bolt12Payment { channel_manager: Arc, keys_manager: Arc, payment_store: Arc, + payer_proof_context_store: Arc, config: Arc, is_running: Arc>, logger: Arc, async_payments_role: Option, } +/// Options controlling which optional fields are disclosed in a BOLT12 payer proof. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "uniffi", derive(uniffi::Record))] +pub struct PayerProofOptions { + /// An optional note attached to the payer proof itself. + pub note: Option, + /// Whether to include the offer description in the proof. + pub include_offer_description: bool, + /// Whether to include the offer issuer in the proof. + pub include_offer_issuer: bool, + /// Whether to include the invoice amount in the proof. + pub include_invoice_amount: bool, + /// Whether to include the invoice creation timestamp in the proof. + pub include_invoice_created_at: bool, + /// Additional TLV types to include in the selective disclosure set. + pub extra_tlv_types: Vec, +} + impl Bolt12Payment { pub(crate) fn new( channel_manager: Arc, keys_manager: Arc, - payment_store: Arc, config: Arc, is_running: Arc>, - logger: Arc, async_payments_role: Option, + payment_store: Arc, payer_proof_context_store: Arc, + config: Arc, is_running: Arc>, logger: Arc, + async_payments_role: Option, ) -> Self { Self { channel_manager, keys_manager, payment_store, + payer_proof_context_store, config, is_running, logger, @@ -245,6 +274,21 @@ impl Bolt12Payment { .blinded_paths_for_async_recipient(recipient_id, None) .or(Err(Error::InvalidBlindedPaths)) } + + fn payer_proof_context( + &self, payment_id: &PaymentId, + ) -> Result<(PaymentDetails, PayerProofContext), Error> { + let payment = self.payment_store.get(payment_id).ok_or(Error::PayerProofUnavailable)?; + if payment.direction != PaymentDirection::Outbound + || payment.status != PaymentStatus::Succeeded + { + return Err(Error::PayerProofUnavailable); + } + + let context = + self.payer_proof_context_store.get(payment_id).ok_or(Error::PayerProofUnavailable)?; + Ok((payment, context)) + } } #[cfg_attr(feature = "uniffi", uniffi::export)] @@ -383,6 +427,75 @@ impl Bolt12Payment { Ok(payment_id) } + /// Create a payer proof for a previously succeeded outbound BOLT12 payment. + /// + /// This requires a standard BOLT12 invoice response that carried payer proof context. + /// Payments that completed via static invoices do not support payer proofs. + pub fn create_payer_proof( + &self, payment_id: &PaymentId, options: Option, + ) -> Result { + let (payment, context) = self.payer_proof_context(payment_id)?; + let preimage = match payment.kind { + PaymentKind::Bolt12Offer { preimage: Some(preimage), .. } + | PaymentKind::Bolt12Refund { preimage: Some(preimage), .. } => preimage, + _ => return Err(Error::PayerProofUnavailable), + }; + + let options = options.unwrap_or_default(); + let mut builder = context.invoice.payer_proof_builder(preimage).map_err(|e| { + log_error!( + self.logger, + "Failed to initialize payer proof builder for {}: {:?}", + payment_id, + e + ); + Error::PayerProofCreationFailed + })?; + + for tlv_type in options.extra_tlv_types { + builder = builder.include_type(tlv_type).map_err(|e| { + log_error!( + self.logger, + "Failed to include TLV {} in payer proof for {}: {:?}", + tlv_type, + payment_id, + e + ); + Error::PayerProofCreationFailed + })?; + } + + if options.include_offer_description { + builder = builder.include_offer_description(); + } + if options.include_offer_issuer { + builder = builder.include_offer_issuer(); + } + if options.include_invoice_amount { + builder = builder.include_invoice_amount(); + } + if options.include_invoice_created_at { + builder = builder.include_invoice_created_at(); + } + + let expanded_key = self.keys_manager.get_expanded_key(); + let secp_ctx = bitcoin::secp256k1::Secp256k1::new(); + let proof = builder + .build_with_derived_key( + &expanded_key, + context.nonce, + *payment_id, + options.note.as_deref(), + &secp_ctx, + ) + .map_err(|e| { + log_error!(self.logger, "Failed to build payer proof for {}: {:?}", payment_id, e); + Error::PayerProofCreationFailed + })?; + + Ok(maybe_wrap(proof)) + } + /// Returns a payable offer that can be used to request and receive a payment of the amount /// given. pub fn receive( diff --git a/src/payment/mod.rs b/src/payment/mod.rs index 42b5aff3b..86a3f1154 100644 --- a/src/payment/mod.rs +++ b/src/payment/mod.rs @@ -11,13 +11,14 @@ pub(crate) mod asynchronous; mod bolt11; mod bolt12; mod onchain; +pub(crate) mod payer_proof_store; pub(crate) mod pending_payment_store; mod spontaneous; pub(crate) mod store; mod unified; pub use bolt11::Bolt11Payment; -pub use bolt12::Bolt12Payment; +pub use bolt12::{Bolt12Payment, PayerProofOptions}; pub use onchain::OnchainPayment; pub use pending_payment_store::PendingPaymentDetails; pub use spontaneous::SpontaneousPayment; diff --git a/src/payment/payer_proof_store.rs b/src/payment/payer_proof_store.rs new file mode 100644 index 000000000..d50131386 --- /dev/null +++ b/src/payment/payer_proof_store.rs @@ -0,0 +1,108 @@ +// This file is Copyright its original authors, visible in version control history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license , at your option. You may not use this file except in +// accordance with one or both of these licenses. + +use lightning::blinded_path::message::OffersContext; +use lightning::impl_writeable_tlv_based; +use lightning::ln::channelmanager::PaymentId; +use lightning::offers::invoice::Bolt12Invoice; +use lightning::offers::nonce::Nonce; + +use crate::data_store::{StorableObject, StorableObjectUpdate}; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct PayerProofContext { + pub payment_id: PaymentId, + pub invoice: Bolt12Invoice, + pub nonce: Nonce, +} + +impl PayerProofContext { + pub(crate) fn from_invoice_received( + payment_id: PaymentId, invoice: &Bolt12Invoice, context: Option<&OffersContext>, + ) -> Option { + match context { + Some(OffersContext::OutboundPaymentForOffer { + payment_id: context_payment_id, + nonce, + }) + | Some(OffersContext::OutboundPaymentForRefund { + payment_id: context_payment_id, + nonce, + }) if *context_payment_id == payment_id => { + Some(Self { payment_id, invoice: invoice.clone(), nonce: *nonce }) + }, + _ => None, + } + } +} + +impl_writeable_tlv_based!(PayerProofContext, { + (0, payment_id, required), + (2, invoice, required), + (4, nonce, required), +}); + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct PayerProofContextUpdate { + pub payment_id: PaymentId, + pub invoice: Option, + pub nonce: Option, +} + +impl From<&PayerProofContext> for PayerProofContextUpdate { + fn from(value: &PayerProofContext) -> Self { + Self { + payment_id: value.payment_id, + invoice: Some(value.invoice.clone()), + nonce: Some(value.nonce), + } + } +} + +impl StorableObject for PayerProofContext { + type Id = PaymentId; + type Update = PayerProofContextUpdate; + + fn id(&self) -> Self::Id { + self.payment_id + } + + fn update(&mut self, update: Self::Update) -> bool { + debug_assert_eq!( + self.payment_id, update.payment_id, + "We should only ever override payer proof context for the same payment id" + ); + + let mut updated = false; + + if let Some(invoice) = update.invoice { + if self.invoice != invoice { + self.invoice = invoice; + updated = true; + } + } + + if let Some(nonce) = update.nonce { + if self.nonce != nonce { + self.nonce = nonce; + updated = true; + } + } + + updated + } + + fn to_update(&self) -> Self::Update { + self.into() + } +} + +impl StorableObjectUpdate for PayerProofContextUpdate { + fn id(&self) -> ::Id { + self.payment_id + } +} diff --git a/src/types.rs b/src/types.rs index a54763313..b798d1db8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -39,6 +39,7 @@ use crate::data_store::DataStore; use crate::fee_estimator::OnchainFeeEstimator; use crate::logger::Logger; use crate::message_handler::NodeCustomMessageHandler; +use crate::payment::payer_proof_store::PayerProofContext; use crate::payment::{PaymentDetails, PendingPaymentDetails}; use crate::runtime::RuntimeSpawner; @@ -319,6 +320,7 @@ pub(crate) type BumpTransactionEventHandler = >; pub(crate) type PaymentStore = DataStore>; +pub(crate) type PayerProofContextStore = DataStore>; /// A local, potentially user-provided, identifier of a channel. /// From 18e0c72ad75b503c8eb5c05383d636f13b389b2c Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Wed, 25 Mar 2026 18:23:03 +0100 Subject: [PATCH 2/2] fix: rebase payer proof branch onto current main Adjust liquidity, payment forwarding, splicing, and binding surfaces to stay compatible with the payer proof rust-lightning fork on top of the current upstream mainline. AI-assisted-by: Codex --- Cargo.toml | 1 + bindings/ldk_node.udl | 1 - src/builder.rs | 5 +++ src/event.rs | 81 +++++++++++++++++++------------------------ src/lib.rs | 22 +++--------- src/liquidity.rs | 11 +++++- src/types.rs | 1 + 7 files changed, 57 insertions(+), 65 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f99d9baa7..3626e6d9f 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -125,6 +125,7 @@ check-cfg = [ "cfg(cln_test)", "cfg(lnd_test)", "cfg(cycle_tests)", + "cfg(lsps1_service)", ] [[bench]] diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 29ed4df94..9a64b49f6 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -303,7 +303,6 @@ dictionary LSPS1ChannelInfo { [Remote] enum LSPS1PaymentState { "ExpectPayment", - "Hold", "Paid", "Refunded", }; diff --git a/src/builder.rs b/src/builder.rs index 532a217ce..7d8c0a8e7 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1817,6 +1817,11 @@ fn build_with_store_internal( Arc::clone(&wallet), Arc::clone(&channel_manager), Arc::clone(&keys_manager), + Arc::clone(&chain_source), + ChainParameters { + network: config.network.into(), + best_block: channel_manager.current_best_block(), + }, Arc::clone(&tx_broadcaster), Arc::clone(&kv_store), Arc::clone(&config), diff --git a/src/event.rs b/src/event.rs index 09fc93c80..f0e11758b 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1379,8 +1379,12 @@ where } }, LdkEvent::PaymentForwarded { - prev_htlcs, - next_htlcs, + prev_channel_id, + next_channel_id, + prev_user_channel_id, + next_user_channel_id, + prev_node_id, + next_node_id, total_fee_earned_msat, skimmed_fee_msat, claim_from_onchain_tx, @@ -1391,10 +1395,11 @@ where let nodes = read_only_network_graph.nodes(); let channels = self.channel_manager.list_channels(); - let node_str = |channel_id: &ChannelId| { - channels - .iter() - .find(|c| c.channel_id == *channel_id) + let node_str = |channel_id: &Option| { + channel_id + .and_then(|channel_id| { + channels.iter().find(|c| c.channel_id == channel_id) + }) .and_then(|channel| { nodes.get(&NodeId::from_pubkey(&channel.counterparty.node_id)) }) @@ -1406,30 +1411,28 @@ where }) }) }; - let from_prev_str: String = prev_htlcs - .iter() - .map(|htlc| { - format!("with {} on {}", node_str(&htlc.channel_id), htlc.channel_id) - }) - .collect::>() - .join(", "); - - let to_next_str: String = next_htlcs - .iter() - .map(|htlc| { - format!("with {} on {}", node_str(&htlc.channel_id), htlc.channel_id) - }) - .collect::>() - .join(", "); + let channel_str = |channel_id: &Option| { + channel_id + .map(|channel_id| format!(" with channel {}", channel_id)) + .unwrap_or_default() + }; + let from_prev_str = format!( + " from {}{}", + node_str(&prev_channel_id), + channel_str(&prev_channel_id) + ); + let to_next_str = format!( + " to {}{}", + node_str(&next_channel_id), + channel_str(&next_channel_id) + ); let fee_earned = total_fee_earned_msat.unwrap_or(0); if claim_from_onchain_tx { log_info!( self.logger, - "Forwarded payment with {} inbound HTLC(s) ({}) and {} outbound HTLC(s) ({}) of {}msat, earning {}msat in fees from claiming onchain.", - prev_htlcs.len(), + "Forwarded payment{}{} of {}msat, earning {}msat in fees from claiming onchain.", from_prev_str, - next_htlcs.len(), to_next_str, outbound_amount_forwarded_msat.unwrap_or(0), fee_earned, @@ -1437,10 +1440,8 @@ where } else { log_info!( self.logger, - "Forwarded payment with {} inbound HTLC(s) ({}) and {} outbound HTLC(s) ({}) of {}msat, earning {}msat in fees.", - prev_htlcs.len(), + "Forwarded payment{}{} of {}msat, earning {}msat in fees.", from_prev_str, - next_htlcs.len(), to_next_str, outbound_amount_forwarded_msat.unwrap_or(0), fee_earned, @@ -1448,32 +1449,20 @@ where } } - // We only allow multiple HTLCs in/out for trampoline forwards, which have not yet - // been fully implemented in LDK, so we do not lose any information by just - // reporting the first HTLC in each vec. - debug_assert_eq!(prev_htlcs.len(), 1, "unexpected number of prev_htlcs"); - debug_assert_eq!(next_htlcs.len(), 1, "unexpected number of next_htlcs"); - let prev_htlc = prev_htlcs - .first() - .expect("we expect at least one prev_htlc for PaymentForwarded"); - let next_htlc = next_htlcs - .first() - .expect("we expect at least one next_htlc for PaymentForwarded"); - if let Some(liquidity_source) = self.liquidity_source.as_ref() { let skimmed_fee_msat = skimmed_fee_msat.unwrap_or(0); liquidity_source - .handle_payment_forwarded(Some(next_htlc.channel_id), skimmed_fee_msat) + .handle_payment_forwarded(next_channel_id, skimmed_fee_msat) .await; } let event = Event::PaymentForwarded { - prev_channel_id: prev_htlc.channel_id, - next_channel_id: next_htlc.channel_id, - prev_user_channel_id: prev_htlc.user_channel_id.map(UserChannelId), - next_user_channel_id: next_htlc.user_channel_id.map(UserChannelId), - prev_node_id: prev_htlc.node_id, - next_node_id: next_htlc.node_id, + prev_channel_id: prev_channel_id.expect("prev_channel_id expected for events generated by LDK versions greater than 0.0.107."), + next_channel_id: next_channel_id.expect("next_channel_id expected for events generated by LDK versions greater than 0.0.107."), + prev_user_channel_id: prev_user_channel_id.map(UserChannelId), + next_user_channel_id: next_user_channel_id.map(UserChannelId), + prev_node_id, + next_node_id, total_fee_earned_msat, skimmed_fee_msat, claim_from_onchain_tx, diff --git a/src/lib.rs b/src/lib.rs index 237f77b23..c092cdd82 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,11 +118,11 @@ pub use balance::{BalanceDetails, LightningBalance, PendingSweepBalance}; pub use bip39; pub use bitcoin; use bitcoin::secp256k1::PublicKey; +#[cfg(not(feature = "uniffi"))] +use bitcoin::Address; +use bitcoin::Amount; #[cfg(feature = "uniffi")] pub use bitcoin::FeeRate; -#[cfg(not(feature = "uniffi"))] -use bitcoin::FeeRate; -use bitcoin::{Address, Amount}; #[cfg(feature = "uniffi")] pub use builder::ArcedNodeBuilder as Builder; pub use builder::BuildError; @@ -1413,7 +1413,6 @@ impl Node { { let min_feerate = self.fee_estimator.estimate_fee_rate(ConfirmationTarget::ChannelFunding); - let max_feerate = FeeRate::from_sat_per_kwu(min_feerate.to_sat_per_kwu() * 3 / 2); let splice_amount_sats = match splice_amount_sats { FundingAmount::Exact { amount_sats } => amount_sats, @@ -1473,12 +1472,7 @@ impl Node { let funding_template = self .channel_manager - .splice_channel( - &channel_details.channel_id, - &counterparty_node_id, - min_feerate, - max_feerate, - ) + .splice_channel(&channel_details.channel_id, &counterparty_node_id, min_feerate) .map_err(|e| { log_error!(self.logger, "Failed to splice channel: {:?}", e); Error::ChannelSplicingFailed @@ -1585,16 +1579,10 @@ impl Node { let min_feerate = self.fee_estimator.estimate_fee_rate(ConfirmationTarget::ChannelFunding); - let max_feerate = FeeRate::from_sat_per_kwu(min_feerate.to_sat_per_kwu() * 3 / 2); let funding_template = self .channel_manager - .splice_channel( - &channel_details.channel_id, - &counterparty_node_id, - min_feerate, - max_feerate, - ) + .splice_channel(&channel_details.channel_id, &counterparty_node_id, min_feerate) .map_err(|e| { log_error!(self.logger, "Failed to splice channel: {:?}", e); Error::ChannelSplicingFailed diff --git a/src/liquidity.rs b/src/liquidity.rs index 485da941c..d09376142 100644 --- a/src/liquidity.rs +++ b/src/liquidity.rs @@ -16,7 +16,7 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1}; use bitcoin::Transaction; use chrono::Utc; use lightning::events::HTLCHandlingFailureType; -use lightning::ln::channelmanager::{InterceptId, MIN_FINAL_CLTV_EXPIRY_DELTA}; +use lightning::ln::channelmanager::{ChainParameters, InterceptId, MIN_FINAL_CLTV_EXPIRY_DELTA}; use lightning::ln::msgs::SocketAddress; use lightning::ln::types::ChannelId; use lightning::routing::router::{RouteHint, RouteHintHop}; @@ -39,6 +39,7 @@ use lightning_types::payment::PaymentHash; use tokio::sync::oneshot; use crate::builder::BuildError; +use crate::chain::ChainSource; use crate::connection::ConnectionManager; use crate::logger::{log_debug, log_error, log_info, LdkLogger, Logger}; use crate::runtime::Runtime; @@ -154,6 +155,8 @@ where wallet: Arc, channel_manager: Arc, keys_manager: Arc, + chain_source: Arc, + chain_params: ChainParameters, tx_broadcaster: Arc, kv_store: Arc, config: Arc, @@ -166,6 +169,7 @@ where { pub(crate) fn new( wallet: Arc, channel_manager: Arc, keys_manager: Arc, + chain_source: Arc, chain_params: ChainParameters, tx_broadcaster: Arc, kv_store: Arc, config: Arc, logger: L, ) -> Self { let lsps1_client = None; @@ -178,6 +182,8 @@ where wallet, channel_manager, keys_manager, + chain_source, + chain_params, tx_broadcaster, kv_store, config, @@ -236,6 +242,7 @@ where let lsps5_service_config = None; let advertise_service = s.service_config.advertise_service; LiquidityServiceConfig { + #[cfg(lsps1_service)] lsps1_service_config: None, lsps2_service_config, lsps5_service_config, @@ -257,6 +264,8 @@ where Arc::clone(&self.keys_manager), Arc::clone(&self.keys_manager), Arc::clone(&self.channel_manager), + Some(Arc::clone(&self.chain_source)), + Some(self.chain_params), Arc::clone(&self.kv_store), Arc::clone(&self.tx_broadcaster), liquidity_service_config, diff --git a/src/types.rs b/src/types.rs index b798d1db8..e0f3b58aa 100644 --- a/src/types.rs +++ b/src/types.rs @@ -232,6 +232,7 @@ pub(crate) type LiquidityManager = lightning_liquidity::LiquidityManager< Arc, Arc, Arc, + Arc, Arc, DefaultTimeProvider, Arc,