Skip to content

Commit f1aba79

Browse files
Add phantom shared secret to HTLCPreviousHopData
This also fixes a bug where we were failing back phantom payments with the wrong scid, causing them to never actually be failed backwards (L3022 in channelmanager.rs) This new field will be used in upcoming commit(s) to encrypt phantom payment failure packets.
1 parent bafd141 commit f1aba79

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ pub(crate) struct HTLCPreviousHopData {
420420
short_channel_id: u64,
421421
htlc_id: u64,
422422
incoming_packet_shared_secret: [u8; 32],
423+
phantom_shared_secret: Option<[u8; 32]>,
423424

424425
// This field is consumed by `claim_funds_from_hop()` when updating a force-closed backwards
425426
// channel with a preimage provided by the forward channel.
@@ -3014,17 +3015,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30143015
routing, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value },
30153016
prev_funding_outpoint } => {
30163017
macro_rules! fail_forward {
3017-
($msg: expr, $err_code: expr, $err_data: expr) => {
3018+
($msg: expr, $err_code: expr, $err_data: expr, $phantom_ss: expr) => {
30183019
{
30193020
log_info!(self.logger, "Failed to accept/forward incoming HTLC: {}", $msg);
30203021
let htlc_source = HTLCSource::PreviousHopData(HTLCPreviousHopData {
3021-
short_channel_id: short_chan_id,
3022+
short_channel_id: prev_short_channel_id,
30223023
outpoint: prev_funding_outpoint,
30233024
htlc_id: prev_htlc_id,
30243025
incoming_packet_shared_secret: incoming_shared_secret,
3026+
phantom_shared_secret: $phantom_ss,
30253027
});
30263028
failed_forwards.push((htlc_source, payment_hash,
3027-
HTLCFailReason::Reason { failure_code: $err_code, data: $err_data }
3029+
HTLCFailReason::Reason { failure_code: $err_code, data: $err_data }
30283030
));
30293031
continue;
30303032
}
@@ -3041,26 +3043,26 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30413043
let next_hop = match onion_utils::decode_next_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
30423044
Ok(res) => res,
30433045
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
3044-
fail_forward!(err_msg, err_code, Vec::new());
3046+
fail_forward!(err_msg, err_code, Vec::new(), None);
30453047
},
30463048
Err(onion_utils::OnionDecodeErr::Relay { err_msg, err_code }) => {
3047-
fail_forward!(err_msg, err_code, Vec::new());
3049+
fail_forward!(err_msg, err_code, Vec::new(), Some(phantom_shared_secret));
30483050
},
30493051
};
30503052
match next_hop {
30513053
onion_utils::Hop::Receive(hop_data) => {
30523054
match self.construct_recv_pending_htlc_info(hop_data, incoming_shared_secret, payment_hash, amt_to_forward, outgoing_cltv_value, Some(phantom_shared_secret)) {
30533055
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, vec![(info, prev_htlc_id)])),
3054-
Err(ReceiveError { err_code, err_data, msg }) => fail_forward!(msg, err_code, err_data)
3056+
Err(ReceiveError { err_code, err_data, msg }) => fail_forward!(msg, err_code, err_data, Some(phantom_shared_secret))
30553057
}
30563058
},
30573059
_ => panic!(),
30583060
}
30593061
} else {
3060-
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new());
3062+
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new(), None);
30613063
}
30623064
} else {
3063-
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new());
3065+
fail_forward!(format!("Unknown short channel id {} for forward HTLC", short_chan_id), 0x4000 | 10, Vec::new(), None);
30643066
}
30653067
},
30663068
HTLCForwardInfo::FailHTLC { .. } => {
@@ -3093,6 +3095,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30933095
outpoint: prev_funding_outpoint,
30943096
htlc_id: prev_htlc_id,
30953097
incoming_packet_shared_secret: incoming_shared_secret,
3098+
// Phantom payments are only PendingHTLCRouting::Receive.
3099+
phantom_shared_secret: None,
30963100
});
30973101
match chan.get_mut().send_htlc(amt_to_forward, payment_hash, outgoing_cltv_value, htlc_source.clone(), onion_packet, &self.logger) {
30983102
Err(e) => {
@@ -3224,6 +3228,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32243228
outpoint: prev_funding_outpoint,
32253229
htlc_id: prev_htlc_id,
32263230
incoming_packet_shared_secret: incoming_shared_secret,
3231+
phantom_shared_secret,
32273232
},
32283233
value: amt_to_forward,
32293234
cltv_expiry,
@@ -3241,6 +3246,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32413246
outpoint: prev_funding_outpoint,
32423247
htlc_id: $htlc.prev_hop.htlc_id,
32433248
incoming_packet_shared_secret: $htlc.prev_hop.incoming_packet_shared_secret,
3249+
phantom_shared_secret,
32443250
}), payment_hash,
32453251
HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: htlc_msat_height_data }
32463252
));
@@ -6073,6 +6079,7 @@ impl_writeable_tlv_based_enum!(PendingHTLCStatus, ;
60736079

60746080
impl_writeable_tlv_based!(HTLCPreviousHopData, {
60756081
(0, short_channel_id, required),
6082+
(1, phantom_shared_secret, option),
60766083
(2, outpoint, required),
60776084
(4, htlc_id, required),
60786085
(6, incoming_packet_shared_secret, required)

0 commit comments

Comments
 (0)