Skip to content

Commit bafd141

Browse files
Add phantom shared secret to PendingHTLCRouting::Receive
This will be used in upcoming commit(s) to encrypt phantom payment failure packets.
1 parent 1f6700c commit bafd141

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ enum PendingHTLCRouting {
366366
Receive {
367367
payment_data: msgs::FinalOnionHopData,
368368
incoming_cltv_expiry: u32, // Used to track when we should expire pending HTLCs that go unclaimed
369+
phantom_shared_secret: Option<[u8; 32]>,
369370
},
370371
ReceiveKeysend {
371372
payment_preimage: PaymentPreimage,
@@ -2072,7 +2073,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20722073
}
20732074

20742075
fn construct_recv_pending_htlc_info(&self, hop_data: msgs::OnionHopData, shared_secret: [u8; 32],
2075-
payment_hash: PaymentHash, amt_msat: u64, cltv_expiry: u32) -> Result<PendingHTLCInfo, ReceiveError>
2076+
payment_hash: PaymentHash, amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>) -> Result<PendingHTLCInfo, ReceiveError>
20762077
{
20772078
// final_incorrect_cltv_expiry
20782079
if hop_data.outgoing_cltv_value != cltv_expiry {
@@ -2129,6 +2130,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21292130
PendingHTLCRouting::Receive {
21302131
payment_data: data,
21312132
incoming_cltv_expiry: hop_data.outgoing_cltv_value,
2133+
phantom_shared_secret,
21322134
}
21332135
} else if let Some(payment_preimage) = keysend_preimage {
21342136
// We need to check that the sender knows the keysend preimage before processing this
@@ -2232,7 +2234,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22322234
let pending_forward_info = match next_hop {
22332235
onion_utils::Hop::Receive(next_hop_data) => {
22342236
// OUR PAYMENT!
2235-
match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry) {
2237+
match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry, None) {
22362238
Ok(info) => {
22372239
// Note that we could obviously respond immediately with an update_fulfill_htlc
22382240
// message, however that would leak that we are the recipient of this payment, so
@@ -3031,12 +3033,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30313033
if let PendingHTLCRouting::Forward { onion_packet, .. } = routing {
30323034
let phantom_secret_res = self.keys_manager.get_node_secret(Recipient::PhantomNode);
30333035
if phantom_secret_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id) {
3034-
let shared_secret = {
3036+
let phantom_shared_secret = {
30353037
let mut arr = [0; 32];
30363038
arr.copy_from_slice(&SharedSecret::new(&onion_packet.public_key.unwrap(), &phantom_secret_res.unwrap())[..]);
30373039
arr
30383040
};
3039-
let next_hop = match onion_utils::decode_next_hop(shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
3041+
let next_hop = match onion_utils::decode_next_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
30403042
Ok(res) => res,
30413043
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
30423044
fail_forward!(err_msg, err_code, Vec::new());
@@ -3047,7 +3049,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30473049
};
30483050
match next_hop {
30493051
onion_utils::Hop::Receive(hop_data) => {
3050-
match self.construct_recv_pending_htlc_info(hop_data, shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value) {
3052+
match self.construct_recv_pending_htlc_info(hop_data, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value, Some(phantom_shared_secret)) {
30513053
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, vec![(info, prev_htlc_id)])),
30523054
Err(ReceiveError { err_code, err_data, msg }) => fail_forward!(msg, err_code, err_data)
30533055
}
@@ -3207,11 +3209,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32073209
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
32083210
routing, incoming_shared_secret, payment_hash, amt_to_forward, .. },
32093211
prev_funding_outpoint } => {
3210-
let (cltv_expiry, onion_payload) = match routing {
3211-
PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry } =>
3212-
(incoming_cltv_expiry, OnionPayload::Invoice(payment_data)),
3212+
let (cltv_expiry, onion_payload, phantom_shared_secret) = match routing {
3213+
PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry, phantom_shared_secret } =>
3214+
(incoming_cltv_expiry, OnionPayload::Invoice(payment_data), phantom_shared_secret),
32133215
PendingHTLCRouting::ReceiveKeysend { payment_preimage, incoming_cltv_expiry } =>
3214-
(incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage)),
3216+
(incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage), None),
32153217
_ => {
32163218
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
32173219
}
@@ -5979,6 +5981,7 @@ impl_writeable_tlv_based_enum!(PendingHTLCRouting,
59795981
},
59805982
(1, Receive) => {
59815983
(0, payment_data, required),
5984+
(1, phantom_shared_secret, option),
59825985
(2, incoming_cltv_expiry, required),
59835986
},
59845987
(2, ReceiveKeysend) => {

0 commit comments

Comments
 (0)