Skip to content

Commit 05ab48d

Browse files
Move phantom route hint selection into its own function
1 parent af68d4f commit 05ab48d

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

lightning-invoice/src/utils.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,33 @@ where
200200
invoice = invoice.amount_milli_satoshis(amt);
201201
}
202202

203+
for route_hint in select_phantom_hints(amt_msat, phantom_route_hints, logger) {
204+
invoice = invoice.private_route(route_hint);
205+
}
206+
207+
let raw_invoice = match invoice.build_raw() {
208+
Ok(inv) => inv,
209+
Err(e) => return Err(SignOrCreationError::CreationError(e))
210+
};
211+
let hrp_str = raw_invoice.hrp.to_string();
212+
let hrp_bytes = hrp_str.as_bytes();
213+
let data_without_signature = raw_invoice.data.to_base32();
214+
let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode));
215+
match signed_raw_invoice {
216+
Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
217+
Err(e) => Err(SignOrCreationError::SignError(e))
218+
}
219+
}
220+
221+
/// Utility to select route hints for phantom invoices.
222+
/// See [`PhantomKeysManager`] for more information on phantom node payments.
223+
fn select_phantom_hints<L: Deref>(amt_msat: Option<u64>, phantom_route_hints: Vec<PhantomRouteHints>,
224+
logger: L) -> Vec<RouteHint>
225+
where
226+
L::Target: Logger,
227+
{
228+
let mut phantom_hints : Vec<RouteHint> = Vec::new();
229+
203230
for PhantomRouteHints { channels, phantom_scid, real_node_pubkey } in phantom_route_hints {
204231
log_trace!(logger, "Generating phantom route hints for node {}",
205232
log_pubkey!(real_node_pubkey));
@@ -223,22 +250,12 @@ where
223250
cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
224251
htlc_minimum_msat: None,
225252
htlc_maximum_msat: None,});
226-
invoice = invoice.private_route(route_hint.clone());
253+
254+
phantom_hints.push(route_hint.clone());
227255
}
228256
}
229257

230-
let raw_invoice = match invoice.build_raw() {
231-
Ok(inv) => inv,
232-
Err(e) => return Err(SignOrCreationError::CreationError(e))
233-
};
234-
let hrp_str = raw_invoice.hrp.to_string();
235-
let hrp_bytes = hrp_str.as_bytes();
236-
let data_without_signature = raw_invoice.data.to_base32();
237-
let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode));
238-
match signed_raw_invoice {
239-
Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
240-
Err(e) => Err(SignOrCreationError::SignError(e))
241-
}
258+
phantom_hints
242259
}
243260

244261
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)