1+ use crate :: storage:: get_invoice_by_hash;
12use crate :: utils:: {
23 convert_from_fedimint_invoice, convert_to_fedimint_invoice, fetch_with_timeout, now, spawn,
34} ;
@@ -506,7 +507,7 @@ impl<S: MutinyStorage> FederationClient<S> {
506507
507508 let desc = Description :: new ( String :: new ( ) ) . expect ( "empty string is valid" ) ;
508509 let gateway = self . gateway . read ( ) . await ;
509- let ( id, invoice, _ ) = lightning_module
510+ let ( id, invoice, preimage ) = lightning_module
510511 . create_bolt11_invoice (
511512 Amount :: from_sats ( amount) ,
512513 Bolt11InvoiceDescription :: Direct ( & desc) ,
@@ -523,6 +524,7 @@ impl<S: MutinyStorage> FederationClient<S> {
523524 let mut stored_payment: MutinyInvoice = invoice. clone ( ) . into ( ) ;
524525 stored_payment. inbound = inbound;
525526 stored_payment. labels = labels;
527+ stored_payment. preimage = Some ( preimage. to_lower_hex_string ( ) ) ;
526528
527529 log_trace ! ( self . logger, "Persisting payment" ) ;
528530 let hash = stored_payment. payment_hash . into_32 ( ) ;
@@ -640,7 +642,7 @@ impl<S: MutinyStorage> FederationClient<S> {
640642 stored_payment. labels = labels;
641643 stored_payment. status = HTLCStatus :: InFlight ;
642644 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 ( ) ) ;
644646 persist_payment_info ( & self . storage , & hash, & payment_info, inbound) ?;
645647
646648 // Subscribe and process outcome based on payment type
@@ -651,8 +653,7 @@ impl<S: MutinyStorage> FederationClient<S> {
651653 let o = process_ln_outcome (
652654 o,
653655 process_pay_state_internal,
654- invoice. clone ( ) ,
655- inbound,
656+ stored_payment,
656657 Some ( DEFAULT_PAYMENT_TIMEOUT * 1_000 ) ,
657658 self . stop . clone ( ) ,
658659 Arc :: clone ( & self . logger ) ,
@@ -669,8 +670,7 @@ impl<S: MutinyStorage> FederationClient<S> {
669670 let o = process_ln_outcome (
670671 o,
671672 process_pay_state_ln,
672- invoice. clone ( ) ,
673- inbound,
673+ stored_payment,
674674 Some ( DEFAULT_PAYMENT_TIMEOUT * 1_000 ) ,
675675 self . stop . clone ( ) ,
676676 Arc :: clone ( & self . logger ) ,
@@ -1151,15 +1151,23 @@ async fn process_operation_until_timeout<S: MutinyStorage>(
11511151 let updated_invoice = match lightning_meta. variant {
11521152 LightningOperationMetaVariant :: Pay ( pay_meta) => {
11531153 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 {
11561165 match lightning_module. subscribe_ln_pay ( operation_id) . await {
11571166 Ok ( o) => Some (
11581167 process_ln_outcome (
11591168 o,
11601169 process_pay_state_ln,
11611170 invoice,
1162- false ,
11631171 timeout,
11641172 stop,
11651173 logger. clone ( ) ,
@@ -1170,7 +1178,7 @@ async fn process_operation_until_timeout<S: MutinyStorage>(
11701178 log_error ! ( logger, "Error trying to process stream outcome: {e}" ) ;
11711179
11721180 // return the latest status of the invoice even if it fails
1173- Some ( invoice. into ( ) )
1181+ Some ( invoice)
11741182 }
11751183 }
11761184 } else {
@@ -1179,15 +1187,23 @@ async fn process_operation_until_timeout<S: MutinyStorage>(
11791187 }
11801188 LightningOperationMetaVariant :: Receive { invoice, .. } => {
11811189 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 {
11841201 match lightning_module. subscribe_ln_receive ( operation_id) . await {
11851202 Ok ( o) => Some (
11861203 process_ln_outcome (
11871204 o,
11881205 process_receive_state,
11891206 invoice,
1190- true ,
11911207 timeout,
11921208 stop,
11931209 logger. clone ( ) ,
@@ -1198,7 +1214,7 @@ async fn process_operation_until_timeout<S: MutinyStorage>(
11981214 log_error ! ( logger, "Error trying to process stream outcome: {e}" ) ;
11991215
12001216 // return the latest status of the invoice even if it fails
1201- Some ( invoice. into ( ) )
1217+ Some ( invoice)
12021218 }
12031219 }
12041220 } else {
@@ -1330,8 +1346,7 @@ fn process_receive_state(receive_state: LnReceiveState, invoice: &mut MutinyInvo
13301346async fn process_ln_outcome < U , F > (
13311347 stream_or_outcome : UpdateStreamOrOutcome < U > ,
13321348 process_fn : F ,
1333- invoice : Bolt11Invoice ,
1334- inbound : bool ,
1349+ mut invoice : MutinyInvoice ,
13351350 timeout : Option < u64 > ,
13361351 stop : Arc < AtomicBool > ,
13371352 logger : Arc < MutinyLogger > ,
@@ -1347,9 +1362,6 @@ where
13471362 + ' static ,
13481363 F : Fn ( U , & mut MutinyInvoice ) ,
13491364{
1350- let mut invoice: MutinyInvoice = invoice. into ( ) ;
1351- invoice. inbound = inbound;
1352-
13531365 match stream_or_outcome {
13541366 UpdateStreamOrOutcome :: Outcome ( outcome) => {
13551367 invoice. status = outcome. into ( ) ;
0 commit comments