@@ -15,11 +15,23 @@ use crate::types::DynStore;
15
15
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
16
16
use bitcoin:: hashes:: Hash ;
17
17
18
- use lightning:: { offers:: static_invoice:: StaticInvoice , util:: ser:: Writeable } ;
18
+ use lightning:: blinded_path:: message:: BlindedMessagePath ;
19
+ use lightning:: impl_writeable_tlv_based;
20
+ use lightning:: { offers:: static_invoice:: StaticInvoice , util:: ser:: Readable , util:: ser:: Writeable } ;
19
21
20
22
use std:: sync:: { Arc , Mutex } ;
21
23
use std:: time:: Duration ;
22
24
25
+ struct PersistedStaticInvoice {
26
+ invoice : StaticInvoice ,
27
+ request_path : BlindedMessagePath ,
28
+ }
29
+
30
+ impl_writeable_tlv_based ! ( PersistedStaticInvoice , {
31
+ ( 0 , invoice, required) ,
32
+ ( 2 , request_path, required)
33
+ } ) ;
34
+
23
35
pub ( crate ) struct StaticInvoiceStore {
24
36
kv_store : Arc < DynStore > ,
25
37
request_rate_limiter : Mutex < RateLimiter > ,
@@ -60,20 +72,24 @@ impl StaticInvoiceStore {
60
72
61
73
pub ( crate ) async fn handle_static_invoice_requested (
62
74
& self , recipient_id : & [ u8 ] , invoice_slot : u16 ,
63
- ) -> Result < Option < StaticInvoice > , lightning:: io:: Error > {
75
+ ) -> Result < Option < ( StaticInvoice , BlindedMessagePath ) > , lightning:: io:: Error > {
64
76
Self :: check_rate_limit ( & self . request_rate_limiter , & recipient_id) ?;
65
77
66
78
let ( secondary_namespace, key) = Self :: get_storage_location ( invoice_slot, recipient_id) ;
67
79
68
80
self . kv_store
69
81
. read ( STATIC_INVOICE_STORE_PRIMARY_NAMESPACE , & secondary_namespace, & key)
70
82
. and_then ( |data| {
71
- data. try_into ( ) . map ( Some ) . map_err ( |e| {
72
- lightning:: io:: Error :: new (
73
- lightning:: io:: ErrorKind :: InvalidData ,
74
- format ! ( "Failed to parse static invoice: {:?}" , e) ,
75
- )
76
- } )
83
+ PersistedStaticInvoice :: read ( & mut & * data)
84
+ . map ( |persisted_invoice| {
85
+ Some ( ( persisted_invoice. invoice , persisted_invoice. request_path ) )
86
+ } )
87
+ . map_err ( |e| {
88
+ lightning:: io:: Error :: new (
89
+ lightning:: io:: ErrorKind :: InvalidData ,
90
+ format ! ( "Failed to parse static invoice: {:?}" , e) ,
91
+ )
92
+ } )
77
93
} )
78
94
. or_else (
79
95
|e| {
@@ -87,14 +103,18 @@ impl StaticInvoiceStore {
87
103
}
88
104
89
105
pub ( crate ) async fn handle_persist_static_invoice (
90
- & self , invoice : StaticInvoice , invoice_slot : u16 , recipient_id : Vec < u8 > ,
106
+ & self , invoice : StaticInvoice , invoice_request_path : BlindedMessagePath , invoice_slot : u16 ,
107
+ recipient_id : Vec < u8 > ,
91
108
) -> Result < ( ) , lightning:: io:: Error > {
92
109
Self :: check_rate_limit ( & self . persist_rate_limiter , & recipient_id) ?;
93
110
94
111
let ( secondary_namespace, key) = Self :: get_storage_location ( invoice_slot, & recipient_id) ;
95
112
113
+ let persisted_invoice =
114
+ PersistedStaticInvoice { invoice, request_path : invoice_request_path } ;
115
+
96
116
let mut buf = Vec :: new ( ) ;
97
- invoice . write ( & mut buf) ?;
117
+ persisted_invoice . write ( & mut buf) ?;
98
118
99
119
// Static invoices will be persisted at "static_invoices/<sha256(recipient_id)>/<invoice_slot>".
100
120
//
@@ -144,15 +164,21 @@ mod tests {
144
164
145
165
let static_invoice = invoice ( ) ;
146
166
let recipient_id = vec ! [ 1 , 1 , 1 ] ;
167
+ let invoice_request_path = blinded_path ( ) ;
147
168
assert ! ( static_invoice_store
148
- . handle_persist_static_invoice( static_invoice. clone( ) , 0 , recipient_id. clone( ) )
169
+ . handle_persist_static_invoice(
170
+ static_invoice. clone( ) ,
171
+ invoice_request_path. clone( ) ,
172
+ 0 ,
173
+ recipient_id. clone( )
174
+ )
149
175
. await
150
176
. is_ok( ) ) ;
151
177
152
178
let requested_invoice =
153
179
static_invoice_store. handle_static_invoice_requested ( & recipient_id, 0 ) . await . unwrap ( ) ;
154
180
155
- assert_eq ! ( requested_invoice. unwrap( ) , static_invoice) ;
181
+ assert_eq ! ( requested_invoice. unwrap( ) , ( static_invoice, invoice_request_path ) ) ;
156
182
157
183
assert ! ( static_invoice_store
158
184
. handle_static_invoice_requested( & recipient_id, 1 )
0 commit comments