@@ -37,6 +37,8 @@ use lightning::events::{Event as LdkEvent, PaymentFailureReason};
3737use lightning:: impl_writeable_tlv_based_enum;
3838use lightning:: ln:: channelmanager:: PaymentId ;
3939use lightning:: ln:: types:: ChannelId ;
40+ use lightning:: offers:: static_invoice:: StaticInvoice ;
41+ use lightning:: onion_message:: messenger:: Responder ;
4042use lightning:: routing:: gossip:: NodeId ;
4143use lightning:: util:: config:: {
4244 ChannelConfigOverrides , ChannelConfigUpdate , ChannelHandshakeConfigUpdate ,
@@ -56,7 +58,7 @@ use rand::{thread_rng, Rng};
5658
5759use core:: future:: Future ;
5860use core:: task:: { Poll , Waker } ;
59- use std:: collections:: VecDeque ;
61+ use std:: collections:: { HashMap , VecDeque } ;
6062use std:: ops:: Deref ;
6163use std:: sync:: { Arc , Condvar , Mutex } ;
6264
@@ -458,6 +460,32 @@ where
458460 runtime : Arc < Runtime > ,
459461 logger : L ,
460462 config : Arc < Config > ,
463+ static_invoice_store : StaticInvoiceStore ,
464+ }
465+
466+ struct StaticInvoiceStore {
467+ pub static_invoices : Mutex < HashMap < ( Vec < u8 > , u16 ) , StaticInvoice > > ,
468+ }
469+
470+ impl StaticInvoiceStore {
471+ fn new ( ) -> Self {
472+ Self { static_invoices : Mutex :: new ( HashMap :: new ( ) ) }
473+ }
474+
475+ fn handle_static_invoice_requested (
476+ & self , recipient_id : Vec < u8 > , invoice_slot : u16 ,
477+ ) -> Option < StaticInvoice > {
478+ let map = self . static_invoices . lock ( ) . unwrap ( ) ;
479+
480+ map. get ( & ( recipient_id. clone ( ) , invoice_slot) ) . cloned ( )
481+ }
482+
483+ fn handle_persist_static_invoice (
484+ & self , invoice : StaticInvoice , invoice_slot : u16 , recipient_id : Vec < u8 > ,
485+ ) {
486+ let mut map = self . static_invoices . lock ( ) . unwrap ( ) ;
487+ map. insert ( ( recipient_id, invoice_slot) , invoice) ;
488+ }
461489}
462490
463491impl < L : Deref + Clone + Sync + Send + ' static > EventHandler < L >
@@ -487,6 +515,7 @@ where
487515 logger,
488516 runtime,
489517 config,
518+ static_invoice_store : StaticInvoiceStore :: new ( ) ,
490519 }
491520 }
492521
@@ -1494,11 +1523,34 @@ where
14941523 LdkEvent :: OnionMessagePeerConnected { .. } => {
14951524 debug_assert ! ( false , "We currently don't support onion message interception, so this event should never be emitted." ) ;
14961525 } ,
1497- LdkEvent :: PersistStaticInvoice { .. } => {
1498- debug_assert ! ( false , "We currently don't support static invoice persistence, so this event should never be emitted." ) ;
1526+
1527+ LdkEvent :: PersistStaticInvoice {
1528+ invoice,
1529+ invoice_slot,
1530+ recipient_id,
1531+ invoice_persisted_path,
1532+ } => {
1533+ self . static_invoice_store . handle_persist_static_invoice (
1534+ invoice,
1535+ invoice_slot,
1536+ recipient_id,
1537+ ) ;
1538+
1539+ self . channel_manager . static_invoice_persisted ( invoice_persisted_path) ;
14991540 } ,
1500- LdkEvent :: StaticInvoiceRequested { .. } => {
1501- debug_assert ! ( false , "We currently don't support static invoice persistence, so this event should never be emitted." ) ;
1541+ LdkEvent :: StaticInvoiceRequested { recipient_id, invoice_slot, reply_path } => {
1542+ let invoice = self
1543+ . static_invoice_store
1544+ . handle_static_invoice_requested ( recipient_id, invoice_slot) ;
1545+
1546+ if let Some ( invoice) = invoice {
1547+ match self . channel_manager . send_static_invoice ( invoice, reply_path) {
1548+ Ok ( ( ) ) => { } ,
1549+ Err ( _) => {
1550+ log_error ! ( self . logger, "Failed to send static invoice" ) ;
1551+ } ,
1552+ }
1553+ }
15021554 } ,
15031555 LdkEvent :: FundingTransactionReadyForSigning { .. } => {
15041556 debug_assert ! ( false , "We currently don't support interactive-tx, so this event should never be emitted." ) ;
0 commit comments