1
+ use crate :: storage:: get_invoice_by_hash;
1
2
use crate :: utils:: {
2
3
convert_from_fedimint_invoice, convert_to_fedimint_invoice, fetch_with_timeout, now, spawn,
3
4
} ;
@@ -506,7 +507,7 @@ impl<S: MutinyStorage> FederationClient<S> {
506
507
507
508
let desc = Description :: new ( String :: new ( ) ) . expect ( "empty string is valid" ) ;
508
509
let gateway = self . gateway . read ( ) . await ;
509
- let ( id, invoice, _ ) = lightning_module
510
+ let ( id, invoice, preimage ) = lightning_module
510
511
. create_bolt11_invoice (
511
512
Amount :: from_sats ( amount) ,
512
513
Bolt11InvoiceDescription :: Direct ( & desc) ,
@@ -523,6 +524,7 @@ impl<S: MutinyStorage> FederationClient<S> {
523
524
let mut stored_payment: MutinyInvoice = invoice. clone ( ) . into ( ) ;
524
525
stored_payment. inbound = inbound;
525
526
stored_payment. labels = labels;
527
+ stored_payment. preimage = Some ( preimage. to_lower_hex_string ( ) ) ;
526
528
527
529
log_trace ! ( self . logger, "Persisting payment" ) ;
528
530
let hash = stored_payment. payment_hash . into_32 ( ) ;
@@ -640,7 +642,7 @@ impl<S: MutinyStorage> FederationClient<S> {
640
642
stored_payment. labels = labels;
641
643
stored_payment. status = HTLCStatus :: InFlight ;
642
644
let hash = stored_payment. payment_hash . into_32 ( ) ;
643
- let payment_info = PaymentInfo :: from ( stored_payment) ;
645
+ let payment_info = PaymentInfo :: from ( stored_payment. clone ( ) ) ;
644
646
persist_payment_info ( & self . storage , & hash, & payment_info, inbound) ?;
645
647
646
648
// Subscribe and process outcome based on payment type
@@ -651,8 +653,7 @@ impl<S: MutinyStorage> FederationClient<S> {
651
653
let o = process_ln_outcome (
652
654
o,
653
655
process_pay_state_internal,
654
- invoice. clone ( ) ,
655
- inbound,
656
+ stored_payment,
656
657
Some ( DEFAULT_PAYMENT_TIMEOUT * 1_000 ) ,
657
658
self . stop . clone ( ) ,
658
659
Arc :: clone ( & self . logger ) ,
@@ -669,8 +670,7 @@ impl<S: MutinyStorage> FederationClient<S> {
669
670
let o = process_ln_outcome (
670
671
o,
671
672
process_pay_state_ln,
672
- invoice. clone ( ) ,
673
- inbound,
673
+ stored_payment,
674
674
Some ( DEFAULT_PAYMENT_TIMEOUT * 1_000 ) ,
675
675
self . stop . clone ( ) ,
676
676
Arc :: clone ( & self . logger ) ,
@@ -1151,15 +1151,23 @@ async fn process_operation_until_timeout<S: MutinyStorage>(
1151
1151
let updated_invoice = match lightning_meta. variant {
1152
1152
LightningOperationMetaVariant :: Pay ( pay_meta) => {
1153
1153
let hash = pay_meta. invoice . payment_hash ( ) . into_inner ( ) ;
1154
- let invoice = convert_from_fedimint_invoice ( & pay_meta. invoice ) ;
1155
- if invoice. payment_hash ( ) . into_32 ( ) == hash {
1154
+ let bolt11 = convert_from_fedimint_invoice ( & pay_meta. invoice ) ;
1155
+ let invoice = match get_invoice_by_hash ( bolt11. payment_hash ( ) , & storage, & logger) {
1156
+ Ok ( invoice) => invoice,
1157
+ Err ( _) => {
1158
+ // if we can't find the invoice, we should just create MutinyInvoice from the bolt11
1159
+ let mut invoice: MutinyInvoice = bolt11. into ( ) ;
1160
+ invoice. inbound = false ;
1161
+ invoice
1162
+ }
1163
+ } ;
1164
+ if invoice. payment_hash . into_32 ( ) == hash {
1156
1165
match lightning_module. subscribe_ln_pay ( operation_id) . await {
1157
1166
Ok ( o) => Some (
1158
1167
process_ln_outcome (
1159
1168
o,
1160
1169
process_pay_state_ln,
1161
1170
invoice,
1162
- false ,
1163
1171
timeout,
1164
1172
stop,
1165
1173
logger. clone ( ) ,
@@ -1170,7 +1178,7 @@ async fn process_operation_until_timeout<S: MutinyStorage>(
1170
1178
log_error ! ( logger, "Error trying to process stream outcome: {e}" ) ;
1171
1179
1172
1180
// return the latest status of the invoice even if it fails
1173
- Some ( invoice. into ( ) )
1181
+ Some ( invoice)
1174
1182
}
1175
1183
}
1176
1184
} else {
@@ -1179,15 +1187,23 @@ async fn process_operation_until_timeout<S: MutinyStorage>(
1179
1187
}
1180
1188
LightningOperationMetaVariant :: Receive { invoice, .. } => {
1181
1189
let hash = invoice. payment_hash ( ) . into_inner ( ) ;
1182
- let invoice = convert_from_fedimint_invoice ( & invoice) ;
1183
- if invoice. payment_hash ( ) . into_32 ( ) == hash {
1190
+ let bolt11 = convert_from_fedimint_invoice ( & invoice) ;
1191
+ let invoice = match get_invoice_by_hash ( bolt11. payment_hash ( ) , & storage, & logger) {
1192
+ Ok ( invoice) => invoice,
1193
+ Err ( _) => {
1194
+ // if we can't find the invoice, we should just create MutinyInvoice from the bolt11
1195
+ let mut invoice: MutinyInvoice = bolt11. into ( ) ;
1196
+ invoice. inbound = true ;
1197
+ invoice
1198
+ }
1199
+ } ;
1200
+ if invoice. payment_hash . into_32 ( ) == hash {
1184
1201
match lightning_module. subscribe_ln_receive ( operation_id) . await {
1185
1202
Ok ( o) => Some (
1186
1203
process_ln_outcome (
1187
1204
o,
1188
1205
process_receive_state,
1189
1206
invoice,
1190
- true ,
1191
1207
timeout,
1192
1208
stop,
1193
1209
logger. clone ( ) ,
@@ -1198,7 +1214,7 @@ async fn process_operation_until_timeout<S: MutinyStorage>(
1198
1214
log_error ! ( logger, "Error trying to process stream outcome: {e}" ) ;
1199
1215
1200
1216
// return the latest status of the invoice even if it fails
1201
- Some ( invoice. into ( ) )
1217
+ Some ( invoice)
1202
1218
}
1203
1219
}
1204
1220
} else {
@@ -1330,8 +1346,7 @@ fn process_receive_state(receive_state: LnReceiveState, invoice: &mut MutinyInvo
1330
1346
async fn process_ln_outcome < U , F > (
1331
1347
stream_or_outcome : UpdateStreamOrOutcome < U > ,
1332
1348
process_fn : F ,
1333
- invoice : Bolt11Invoice ,
1334
- inbound : bool ,
1349
+ mut invoice : MutinyInvoice ,
1335
1350
timeout : Option < u64 > ,
1336
1351
stop : Arc < AtomicBool > ,
1337
1352
logger : Arc < MutinyLogger > ,
@@ -1347,9 +1362,6 @@ where
1347
1362
+ ' static ,
1348
1363
F : Fn ( U , & mut MutinyInvoice ) ,
1349
1364
{
1350
- let mut invoice: MutinyInvoice = invoice. into ( ) ;
1351
- invoice. inbound = inbound;
1352
-
1353
1365
match stream_or_outcome {
1354
1366
UpdateStreamOrOutcome :: Outcome ( outcome) => {
1355
1367
invoice. status = outcome. into ( ) ;
0 commit comments