@@ -37,6 +37,8 @@ use lightning::events::{Event as LdkEvent, PaymentFailureReason};
37
37
use lightning:: impl_writeable_tlv_based_enum;
38
38
use lightning:: ln:: channelmanager:: PaymentId ;
39
39
use lightning:: ln:: types:: ChannelId ;
40
+ use lightning:: offers:: static_invoice:: StaticInvoice ;
41
+ use lightning:: onion_message:: messenger:: Responder ;
40
42
use lightning:: routing:: gossip:: NodeId ;
41
43
use lightning:: util:: config:: {
42
44
ChannelConfigOverrides , ChannelConfigUpdate , ChannelHandshakeConfigUpdate ,
@@ -56,7 +58,7 @@ use rand::{thread_rng, Rng};
56
58
57
59
use core:: future:: Future ;
58
60
use core:: task:: { Poll , Waker } ;
59
- use std:: collections:: VecDeque ;
61
+ use std:: collections:: { HashMap , VecDeque } ;
60
62
use std:: ops:: Deref ;
61
63
use std:: sync:: { Arc , Condvar , Mutex } ;
62
64
@@ -458,6 +460,32 @@ where
458
460
runtime : Arc < Runtime > ,
459
461
logger : L ,
460
462
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
+ }
461
489
}
462
490
463
491
impl < L : Deref + Clone + Sync + Send + ' static > EventHandler < L >
@@ -487,6 +515,7 @@ where
487
515
logger,
488
516
runtime,
489
517
config,
518
+ static_invoice_store : StaticInvoiceStore :: new ( ) ,
490
519
}
491
520
}
492
521
@@ -1494,11 +1523,34 @@ where
1494
1523
LdkEvent :: OnionMessagePeerConnected { .. } => {
1495
1524
debug_assert ! ( false , "We currently don't support onion message interception, so this event should never be emitted." ) ;
1496
1525
} ,
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) ;
1499
1540
} ,
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
+ }
1502
1554
} ,
1503
1555
LdkEvent :: FundingTransactionReadyForSigning { .. } => {
1504
1556
debug_assert ! ( false , "We currently don't support interactive-tx, so this event should never be emitted." ) ;
0 commit comments