diff --git a/Cargo.toml b/Cargo.toml index 8a85c6574..89fc93a1e 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 = "dcf0c203e166da2348bef12b2e5eff4a250cdec7", features = ["std"] } -lightning-types = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7" } -lightning-invoice = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7", features = ["std"] } -lightning-net-tokio = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7" } -lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7", features = ["tokio"] } -lightning-background-processor = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7" } -lightning-rapid-gossip-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7" } -lightning-block-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7", features = ["rest-client", "rpc-client", "tokio"] } -lightning-transaction-sync = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7", features = ["esplora-async-https", "time", "electrum-rustls-ring"] } -lightning-liquidity = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7", features = ["std"] } -lightning-macros = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7" } +lightning = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085", features = ["std"] } +lightning-types = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-invoice = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085", features = ["std"] } +lightning-net-tokio = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-persister = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085", features = ["tokio"] } +lightning-background-processor = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-rapid-gossip-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-block-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085", features = ["rest-client", "rpc-client", "tokio"] } +lightning-transaction-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085", features = ["esplora-async-https", "time", "electrum-rustls-ring"] } +lightning-liquidity = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085", features = ["std"] } +lightning-macros = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } 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/joostjager/bitcoin-pa winapi = { version = "0.3", features = ["winbase"] } [dev-dependencies] -lightning = { git = "https://github.com/lightningdevkit/rust-lightning", rev = "dcf0c203e166da2348bef12b2e5eff4a250cdec7", features = ["std", "_test_utils"] } +lightning = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085", 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" @@ -172,15 +172,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 = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-types = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-invoice = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-net-tokio = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-persister = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-background-processor = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-rapid-gossip-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-block-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-transaction-sync = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-liquidity = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } +lightning-macros = { git = "https://github.com/vincenzopalazzo/rust-lightning", rev = "c8363950d753f954b47d2bf4aaff07928c86b085" } 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 cd8cc184f..0ecc6e686 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -77,8 +77,8 @@ use crate::runtime::{Runtime, RuntimeSpawner}; use crate::tx_broadcaster::TransactionBroadcaster; use crate::types::{ AsyncPersister, ChainMonitor, ChannelManager, DynStore, DynStoreRef, DynStoreWrapper, - GossipSync, Graph, KeysManager, MessageRouter, OnionMessenger, PaymentStore, PeerManager, - PendingPaymentStore, SyncAndAsyncKVStore, + GossipSync, Graph, KeysManager, MessageRouter, OnionMessenger, PayerProofContextStore, + PaymentStore, PeerManager, PendingPaymentStore, SyncAndAsyncKVStore, }; use crate::wallet::persist::KVStoreWalletPersister; use crate::wallet::Wallet; @@ -1265,7 +1265,7 @@ fn build_with_store_internal( 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_pending_payments(&*kv_store_ref, Arc::clone(&logger_ref)), ) }); @@ -1296,6 +1296,8 @@ fn build_with_store_internal( }, }; + let payer_proof_context_store = Arc::new(PayerProofContextStore::new()); + 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()); @@ -1987,6 +1989,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/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 f06d701bc..2b5307ded 100644 --- a/src/event.rs +++ b/src/event.rs @@ -15,8 +15,6 @@ use bitcoin::blockdata::locktime::absolute::LockTime; use bitcoin::secp256k1::PublicKey; use bitcoin::{Amount, OutPoint}; use lightning::events::bump_transaction::BumpTransactionEvent; -#[cfg(not(feature = "uniffi"))] -use lightning::events::PaidBolt12Invoice; use lightning::events::{ ClosureReason, Event as LdkEvent, FundingInfo, PaymentFailureReason, PaymentPurpose, ReplayEvent, @@ -24,6 +22,9 @@ use lightning::events::{ use lightning::impl_writeable_tlv_based_enum; use lightning::ln::channelmanager::PaymentId; use lightning::ln::types::ChannelId; +#[cfg(not(feature = "uniffi"))] +use lightning::offers::payer_proof::Bolt12InvoiceType; +use lightning::offers::payer_proof::PaidBolt12Invoice; use lightning::routing::gossip::NodeId; use lightning::sign::EntropySource; use lightning::util::config::{ @@ -40,7 +41,7 @@ use crate::connection::ConnectionManager; use crate::data_store::DataStoreUpdateResult; use crate::fee_estimator::ConfirmationTarget; #[cfg(feature = "uniffi")] -use crate::ffi::PaidBolt12Invoice; +use crate::ffi::PaidBolt12Invoice as FfiPaidBolt12Invoice; use crate::io::{ EVENT_QUEUE_PERSISTENCE_KEY, EVENT_QUEUE_PERSISTENCE_PRIMARY_NAMESPACE, EVENT_QUEUE_PERSISTENCE_SECONDARY_NAMESPACE, @@ -54,13 +55,19 @@ use crate::payment::store::{ }; 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, UserChannelId, }; +#[cfg(not(feature = "uniffi"))] +type Bolt12InvoiceInfo = Bolt12InvoiceType; +#[cfg(feature = "uniffi")] +type Bolt12InvoiceInfo = FfiPaidBolt12Invoice; + /// An event emitted by [`Node`], which should be handled by the user. /// /// [`Node`]: [`crate::Node`] @@ -83,17 +90,16 @@ pub enum Event { payment_preimage: Option, /// The total fee which was spent at intermediate hops in this payment. fee_paid_msat: Option, - /// The BOLT12 invoice that was paid. + /// The BOLT12 invoice type that was paid. /// /// This is useful for proof of payment. A third party can verify that the payment was made /// by checking that the `payment_hash` in the invoice matches `sha256(payment_preimage)`. /// /// Will be `None` for non-BOLT12 payments. /// - /// Note that static invoices (indicated by [`PaidBolt12Invoice::StaticInvoice`], used for - /// async payments) do not support proof of payment as the payment hash is not derived - /// from a preimage known only to the recipient. - bolt12_invoice: Option, + /// Note that static invoices (via [`Bolt12InvoiceType::StaticInvoice`], used for + /// async payments) do not support payer proofs. + bolt12_invoice: Option, }, /// A sent payment has failed. PaymentFailed { @@ -507,6 +513,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 +534,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 +550,7 @@ where network_graph, liquidity_source, payment_store, + payer_proof_context_store, peer_store, keys_manager, logger, @@ -553,6 +562,16 @@ where } } + fn persist_payer_proof_context( + &self, payment_id: PaymentId, paid_invoice: &Option, + ) { + if let Some(paid_invoice) = paid_invoice { + if paid_invoice.bolt12_invoice().is_some() { + self.payer_proof_context_store.insert_or_update(payment_id, paid_invoice.clone()); + } + } + } + pub async fn handle_event(&self, event: LdkEvent) -> Result<(), ReplayEvent> { match event { LdkEvent::FundingGenerationReady { @@ -860,6 +879,7 @@ where offer_id, payer_note, quantity, + bolt12_invoice: None, }; let payment = PaymentDetails::new( @@ -1074,11 +1094,28 @@ where return Ok(()); }; + self.persist_payer_proof_context(payment_id, &bolt12_invoice); + + #[cfg(not(feature = "uniffi"))] + let bolt12_invoice_info: Option = bolt12_invoice + .as_ref() + .and_then(|p| { + p.bolt12_invoice().map(|i| Bolt12InvoiceType::Bolt12Invoice(i.clone())) + }) + .or_else(|| { + bolt12_invoice.as_ref().and_then(|p| { + p.static_invoice().map(|i| Bolt12InvoiceType::StaticInvoice(i.clone())) + }) + }); + #[cfg(feature = "uniffi")] + let bolt12_invoice_info: Option = bolt12_invoice.map(|p| p.into()); + let update = PaymentDetailsUpdate { hash: Some(Some(payment_hash)), preimage: Some(Some(payment_preimage)), fee_paid_msat: Some(fee_paid_msat), status: Some(PaymentStatus::Succeeded), + bolt12_invoice: Some(bolt12_invoice_info.clone()), ..PaymentDetailsUpdate::new(payment_id) }; @@ -1110,7 +1147,7 @@ where payment_hash, payment_preimage: Some(payment_preimage), fee_paid_msat, - bolt12_invoice: bolt12_invoice.map(Into::into), + bolt12_invoice: bolt12_invoice_info, }; match self.event_queue.add_event(event).await { @@ -1562,20 +1599,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()); } diff --git a/src/ffi/types.rs b/src/ffi/types.rs index 5a1420882..9b2b7ecba 100644 --- a/src/ffi/types.rs +++ b/src/ffi/types.rs @@ -23,7 +23,6 @@ use bitcoin::hashes::Hash; use bitcoin::secp256k1::PublicKey; pub use bitcoin::{Address, BlockHash, Network, OutPoint, ScriptBuf, Txid}; pub use lightning::chain::channelmonitor::BalanceSource; -use lightning::events::PaidBolt12Invoice as LdkPaidBolt12Invoice; pub use lightning::events::{ClosureReason, PaymentFailureReason}; use lightning::ln::channelmanager::PaymentId; use lightning::ln::msgs::DecodeError; @@ -31,6 +30,10 @@ 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::{ + Bolt12InvoiceType as LdkBolt12InvoiceType, PaidBolt12Invoice as LdkPaidBolt12Invoice, + 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; @@ -836,42 +839,127 @@ pub enum PaidBolt12Invoice { impl From for PaidBolt12Invoice { fn from(ldk: LdkPaidBolt12Invoice) -> Self { - match ldk { - LdkPaidBolt12Invoice::Bolt12Invoice(invoice) => { - PaidBolt12Invoice::Bolt12(Arc::new(Bolt12Invoice::from(invoice))) - }, - LdkPaidBolt12Invoice::StaticInvoice(invoice) => { - PaidBolt12Invoice::Static(Arc::new(StaticInvoice::from(invoice))) - }, + if let Some(invoice) = ldk.bolt12_invoice() { + PaidBolt12Invoice::Bolt12(Arc::new(Bolt12Invoice::from(invoice.clone()))) + } else if let Some(invoice) = ldk.static_invoice() { + PaidBolt12Invoice::Static(Arc::new(StaticInvoice::from(invoice.clone()))) + } else { + panic!("PaidBolt12Invoice must contain either a Bolt12Invoice or StaticInvoice") } } } -impl From for LdkPaidBolt12Invoice { - fn from(wrapper: PaidBolt12Invoice) -> Self { - match wrapper { +impl Writeable for PaidBolt12Invoice { + fn write(&self, w: &mut W) -> Result<(), lightning::io::Error> { + let invoice_type = match self { PaidBolt12Invoice::Bolt12(invoice) => { - LdkPaidBolt12Invoice::Bolt12Invoice(invoice.inner.clone()) + LdkBolt12InvoiceType::Bolt12Invoice(invoice.inner.clone()) }, PaidBolt12Invoice::Static(invoice) => { - LdkPaidBolt12Invoice::StaticInvoice(invoice.inner.clone()) + LdkBolt12InvoiceType::StaticInvoice(invoice.inner.clone()) }, - } + }; + invoice_type.write(w) } } -impl Writeable for PaidBolt12Invoice { - fn write(&self, w: &mut W) -> Result<(), lightning::io::Error> { - // TODO: Find way to avoid cloning invoice data. - let ldk_type: LdkPaidBolt12Invoice = self.clone().into(); - ldk_type.write(w) +impl Readable for PaidBolt12Invoice { + fn read(r: &mut R) -> Result { + let invoice_type = LdkBolt12InvoiceType::read(r)?; + Ok(match invoice_type { + LdkBolt12InvoiceType::Bolt12Invoice(invoice) => { + PaidBolt12Invoice::Bolt12(Arc::new(Bolt12Invoice::from(invoice))) + }, + LdkBolt12InvoiceType::StaticInvoice(invoice) => { + PaidBolt12Invoice::Static(Arc::new(StaticInvoice::from(invoice))) + }, + }) } } -impl Readable for PaidBolt12Invoice { - fn read(r: &mut R) -> Result { - let ldk_type = LdkPaidBolt12Invoice::read(r)?; - Ok(ldk_type.into()) +/// 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) } } diff --git a/src/lib.rs b/src/lib.rs index 2ac4697e8..8696511f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -173,8 +173,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; @@ -232,6 +232,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..594675baf 100644 --- a/src/payment/bolt12.rs +++ b/src/payment/bolt12.rs @@ -18,8 +18,13 @@ 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::Bolt12InvoiceType; +use lightning::offers::payer_proof::PaidBolt12Invoice as LdkPaidBolt12Invoice; +#[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,7 +33,10 @@ 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::store::{PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus}; +use crate::payment::payer_proof_store::PayerProofContextStore; +use crate::payment::store::{ + Bolt12InvoiceInfo, PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus, +}; use crate::types::{ChannelManager, KeysManager, PaymentStore}; #[cfg(not(feature = "uniffi"))] @@ -51,6 +59,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 +75,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, @@ -154,6 +188,7 @@ impl Bolt12Payment { offer_id: offer.id(), payer_note: payer_note.map(UntrustedString), quantity, + bolt12_invoice: None, }; let payment = PaymentDetails::new( payment_id, @@ -179,6 +214,7 @@ impl Bolt12Payment { offer_id: offer.id(), payer_note: payer_note.map(UntrustedString), quantity, + bolt12_invoice: None, }; let payment = PaymentDetails::new( payment_id, @@ -245,6 +281,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, LdkPaidBolt12Invoice), 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 paid_invoice = + self.payer_proof_context_store.get(payment_id).ok_or(Error::PayerProofUnavailable)?; + Ok((payment, paid_invoice)) + } } #[cfg_attr(feature = "uniffi", uniffi::export)] @@ -314,6 +365,7 @@ impl Bolt12Payment { offer_id: offer.id(), payer_note: payer_note.map(UntrustedString), quantity, + bolt12_invoice: None, }; let payment = PaymentDetails::new( payment_id, @@ -339,6 +391,7 @@ impl Bolt12Payment { offer_id: offer.id(), payer_note: payer_note.map(UntrustedString), quantity, + bolt12_invoice: None, }; let payment = PaymentDetails::new( payment_id, @@ -383,6 +436,70 @@ 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, paid_invoice) = 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 expanded_key = self.keys_manager.get_expanded_key(); + let secp_ctx = bitcoin::secp256k1::Secp256k1::new(); + + let mut builder = paid_invoice + .prove_payer_derived(&expanded_key, *payment_id, &secp_ctx) + .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 proof = builder.build_and_sign(options.note).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( @@ -438,12 +555,21 @@ impl Bolt12Payment { let payment_hash = invoice.payment_hash(); let payment_id = PaymentId(payment_hash.0); + #[cfg(not(feature = "uniffi"))] + let bolt12_invoice_info: Option = + Some(Bolt12InvoiceType::Bolt12Invoice(invoice.clone())); + #[cfg(feature = "uniffi")] + let bolt12_invoice_info: Option = Some(crate::ffi::PaidBolt12Invoice::Bolt12( + std::sync::Arc::new(crate::ffi::Bolt12Invoice::from(invoice.clone())), + )); + let kind = PaymentKind::Bolt12Refund { hash: Some(payment_hash), preimage: None, secret: None, payer_note: refund.payer_note().map(|note| UntrustedString(note.0.to_string())), quantity: refund.quantity(), + bolt12_invoice: bolt12_invoice_info, }; let payment = PaymentDetails::new( @@ -514,6 +640,7 @@ impl Bolt12Payment { secret: None, payer_note: payer_note.map(|note| UntrustedString(note)), quantity, + bolt12_invoice: None, }; let payment = PaymentDetails::new( payment_id, 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..8c43d84b4 --- /dev/null +++ b/src/payment/payer_proof_store.rs @@ -0,0 +1,45 @@ +// 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 std::collections::HashMap; +use std::sync::Mutex; + +use lightning::ln::channelmanager::PaymentId; +use lightning::offers::payer_proof::PaidBolt12Invoice; + +/// In-memory store for payer proof contexts, keyed by payment ID. +/// +/// Stores the [`PaidBolt12Invoice`] received in [`Event::PaymentSent`] so that +/// payer proofs can be constructed later via [`Bolt12Payment::create_payer_proof`]. +/// +/// Note: This store is not persisted to disk. Payer proof contexts are lost on +/// restart. This is a known limitation of the current upstream LDK API where +/// [`PaidBolt12Invoice`] cannot be serialized and reconstructed externally. +/// +/// [`Event::PaymentSent`]: lightning::events::Event::PaymentSent +/// [`Bolt12Payment::create_payer_proof`]: crate::payment::bolt12::Bolt12Payment::create_payer_proof +pub(crate) struct PayerProofContextStore { + entries: Mutex>, +} + +impl PayerProofContextStore { + pub fn new() -> Self { + Self { entries: Mutex::new(HashMap::new()) } + } + + pub fn insert_or_update(&self, payment_id: PaymentId, paid_invoice: PaidBolt12Invoice) { + self.entries.lock().unwrap().insert(payment_id, paid_invoice); + } + + pub fn get(&self, payment_id: &PaymentId) -> Option { + self.entries.lock().unwrap().get(payment_id).cloned() + } + + pub fn remove(&self, payment_id: &PaymentId) { + self.entries.lock().unwrap().remove(payment_id); + } +} diff --git a/src/payment/store.rs b/src/payment/store.rs index 0e2de9815..9179b07dd 100644 --- a/src/payment/store.rs +++ b/src/payment/store.rs @@ -11,6 +11,8 @@ use bitcoin::{BlockHash, Txid}; use lightning::ln::channelmanager::PaymentId; use lightning::ln::msgs::DecodeError; use lightning::offers::offer::OfferId; +#[cfg(not(feature = "uniffi"))] +use lightning::offers::payer_proof::Bolt12InvoiceType; use lightning::util::ser::{Readable, Writeable}; use lightning::{ _init_and_read_len_prefixed_tlv_fields, impl_writeable_tlv_based, @@ -20,8 +22,15 @@ use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret}; use lightning_types::string::UntrustedString; use crate::data_store::{StorableObject, StorableObjectId, StorableObjectUpdate}; +#[cfg(feature = "uniffi")] +use crate::ffi::PaidBolt12Invoice; use crate::hex_utils; +#[cfg(not(feature = "uniffi"))] +pub(crate) type Bolt12InvoiceInfo = Bolt12InvoiceType; +#[cfg(feature = "uniffi")] +pub(crate) type Bolt12InvoiceInfo = PaidBolt12Invoice; + /// Represents a payment. #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "uniffi", derive(uniffi::Record))] @@ -267,6 +276,18 @@ impl StorableObject for PaymentDetails { update_if_necessary!(self.fee_paid_msat, fee_paid_msat_opt); } + if let Some(ref bolt12_invoice_opt) = update.bolt12_invoice { + match self.kind { + PaymentKind::Bolt12Offer { ref mut bolt12_invoice, .. } => { + update_if_necessary!(*bolt12_invoice, bolt12_invoice_opt.clone()); + }, + PaymentKind::Bolt12Refund { ref mut bolt12_invoice, .. } => { + update_if_necessary!(*bolt12_invoice, bolt12_invoice_opt.clone()); + }, + _ => {}, + } + } + if let Some(skimmed_fee_msat) = update.counterparty_skimmed_fee_msat { match self.kind { PaymentKind::Bolt11Jit { ref mut counterparty_skimmed_fee_msat, .. } => { @@ -428,6 +449,8 @@ pub enum PaymentKind { /// /// This will always be `None` for payments serialized with version `v0.3.0`. quantity: Option, + /// The BOLT12 invoice associated with the payment, once available. + bolt12_invoice: Option, }, /// A [BOLT 12] 'refund' payment, i.e., a payment for a [`Refund`]. /// @@ -448,6 +471,8 @@ pub enum PaymentKind { /// /// This will always be `None` for payments serialized with version `v0.3.0`. quantity: Option, + /// The BOLT12 invoice associated with the payment, once available. + bolt12_invoice: Option, }, /// A spontaneous ("keysend") payment. Spontaneous { @@ -482,6 +507,7 @@ impl_writeable_tlv_based_enum!(PaymentKind, (3, quantity, option), (4, secret, option), (6, offer_id, required), + (8, bolt12_invoice, option), }, (8, Spontaneous) => { (0, hash, required), @@ -493,6 +519,7 @@ impl_writeable_tlv_based_enum!(PaymentKind, (2, preimage, option), (3, quantity, option), (4, secret, option), + (6, bolt12_invoice, option), } ); @@ -555,6 +582,7 @@ pub(crate) struct PaymentDetailsUpdate { pub direction: Option, pub status: Option, pub confirmation_status: Option, + pub bolt12_invoice: Option>, pub txid: Option, } @@ -571,6 +599,7 @@ impl PaymentDetailsUpdate { direction: None, status: None, confirmation_status: None, + bolt12_invoice: None, txid: None, } } @@ -578,13 +607,21 @@ impl PaymentDetailsUpdate { impl From<&PaymentDetails> for PaymentDetailsUpdate { fn from(value: &PaymentDetails) -> Self { - let (hash, preimage, secret) = match value.kind { - PaymentKind::Bolt11 { hash, preimage, secret, .. } => (Some(hash), preimage, secret), - PaymentKind::Bolt11Jit { hash, preimage, secret, .. } => (Some(hash), preimage, secret), - PaymentKind::Bolt12Offer { hash, preimage, secret, .. } => (hash, preimage, secret), - PaymentKind::Bolt12Refund { hash, preimage, secret, .. } => (hash, preimage, secret), - PaymentKind::Spontaneous { hash, preimage, .. } => (Some(hash), preimage, None), - _ => (None, None, None), + let (hash, preimage, secret, bolt12_invoice) = match &value.kind { + PaymentKind::Bolt11 { hash, preimage, secret, .. } => { + (Some(*hash), *preimage, *secret, None) + }, + PaymentKind::Bolt11Jit { hash, preimage, secret, .. } => { + (Some(*hash), *preimage, *secret, None) + }, + PaymentKind::Bolt12Offer { hash, preimage, secret, bolt12_invoice, .. } => { + (*hash, *preimage, *secret, Some(bolt12_invoice.clone())) + }, + PaymentKind::Bolt12Refund { hash, preimage, secret, bolt12_invoice, .. } => { + (*hash, *preimage, *secret, Some(bolt12_invoice.clone())) + }, + PaymentKind::Spontaneous { hash, preimage, .. } => (Some(*hash), *preimage, None, None), + _ => (None, None, None, None), }; let (confirmation_status, txid) = match &value.kind { @@ -592,9 +629,9 @@ impl From<&PaymentDetails> for PaymentDetailsUpdate { _ => (None, None), }; - let counterparty_skimmed_fee_msat = match value.kind { + let counterparty_skimmed_fee_msat = match &value.kind { PaymentKind::Bolt11Jit { counterparty_skimmed_fee_msat, .. } => { - Some(counterparty_skimmed_fee_msat) + Some(*counterparty_skimmed_fee_msat) }, _ => None, }; @@ -610,6 +647,7 @@ impl From<&PaymentDetails> for PaymentDetailsUpdate { direction: Some(value.direction), status: Some(value.status), confirmation_status, + bolt12_invoice, txid, } } diff --git a/src/types.rs b/src/types.rs index dae315ae0..b92116cea 100644 --- a/src/types.rs +++ b/src/types.rs @@ -37,6 +37,7 @@ use crate::data_store::DataStore; use crate::fee_estimator::OnchainFeeEstimator; use crate::logger::Logger; use crate::message_handler::NodeCustomMessageHandler; +pub(crate) use crate::payment::payer_proof_store::PayerProofContextStore; use crate::payment::{PaymentDetails, PendingPaymentDetails}; use crate::runtime::RuntimeSpawner; diff --git a/tests/integration_tests_rust.rs b/tests/integration_tests_rust.rs index 413b2d44a..0e20020ff 100644 --- a/tests/integration_tests_rust.rs +++ b/tests/integration_tests_rust.rs @@ -1115,12 +1115,14 @@ async fn simple_bolt12_send_receive() { offer_id, quantity: ref qty, payer_note: ref note, + bolt12_invoice: ref invoice, } => { assert!(hash.is_some()); assert!(preimage.is_some()); assert_eq!(offer_id, offer.id()); assert_eq!(&expected_quantity, qty); assert_eq!(expected_payer_note.unwrap(), note.clone().unwrap().0); + assert!(invoice.is_some()); // TODO: We should eventually set and assert the secret sender-side, too, but the BOLT12 // API currently doesn't allow to do that. }, @@ -1182,12 +1184,14 @@ async fn simple_bolt12_send_receive() { offer_id, quantity: ref qty, payer_note: ref note, + bolt12_invoice: ref invoice, } => { assert!(hash.is_some()); assert!(preimage.is_some()); assert_eq!(offer_id, offer.id()); assert_eq!(&expected_quantity, qty); assert_eq!(expected_payer_note.unwrap(), note.clone().unwrap().0); + assert!(invoice.is_some()); // TODO: We should eventually set and assert the secret sender-side, too, but the BOLT12 // API currently doesn't allow to do that. hash.unwrap() @@ -1255,11 +1259,13 @@ async fn simple_bolt12_send_receive() { secret: _, quantity: ref qty, payer_note: ref note, + bolt12_invoice: ref invoice, } => { assert!(hash.is_some()); assert!(preimage.is_some()); assert_eq!(&expected_quantity, qty); - assert_eq!(expected_payer_note.unwrap(), note.clone().unwrap().0) + assert_eq!(expected_payer_note.unwrap(), note.clone().unwrap().0); + assert!(invoice.is_some()); // TODO: We should eventually set and assert the secret sender-side, too, but the BOLT12 // API currently doesn't allow to do that. },