Skip to content

Commit 6d11111

Browse files
Merge pull request #2756 from arik-so/arik/trampoline/2023-11-outbound
Serialize Trampoline payloads in outbound onions.
2 parents 650caa0 + f15c538 commit 6d11111

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

lightning/src/ln/features.rs

+11
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
//! for more info).
6767
//! - `Keysend` - send funds to a node without an invoice
6868
//! (see the [`Keysend` feature assignment proposal](https://github.com/lightning/bolts/issues/605#issuecomment-606679798) for more information).
69+
//! - `Trampoline` - supports receiving and forwarding Trampoline payments
70+
//! (see the [`Trampoline` feature proposal](https://github.com/lightning/bolts/pull/836) for more information).
6971
//!
7072
//! LDK knows about the following features, but does not support them:
7173
//! - `AnchorsNonzeroFeeHtlcTx` - the initial version of anchor outputs, which was later found to be
@@ -152,6 +154,8 @@ mod sealed {
152154
ChannelType | SCIDPrivacy,
153155
// Byte 6
154156
ZeroConf,
157+
// Byte 7
158+
Trampoline,
155159
]);
156160
define_context!(NodeContext, [
157161
// Byte 0
@@ -168,6 +172,8 @@ mod sealed {
168172
ChannelType | SCIDPrivacy,
169173
// Byte 6
170174
ZeroConf | Keysend,
175+
// Byte 7
176+
Trampoline,
171177
]);
172178
define_context!(ChannelContext, []);
173179
define_context!(Bolt11InvoiceContext, [
@@ -185,6 +191,8 @@ mod sealed {
185191
,
186192
// Byte 6
187193
PaymentMetadata,
194+
// Byte 7
195+
Trampoline,
188196
]);
189197
define_context!(OfferContext, []);
190198
define_context!(InvoiceRequestContext, []);
@@ -420,6 +428,9 @@ mod sealed {
420428
define_feature!(55, Keysend, [NodeContext],
421429
"Feature flags for keysend payments.", set_keysend_optional, set_keysend_required,
422430
supports_keysend, requires_keysend);
431+
define_feature!(57, Trampoline, [InitContext, NodeContext, Bolt11InvoiceContext],
432+
"Feature flags for Trampoline routing.", set_trampoline_routing_optional, set_trampoline_routing_required,
433+
supports_trampoline_routing, requires_trampoline_routing);
423434
// Note: update the module-level docs when a new feature bit is added!
424435

425436
#[cfg(test)]

lightning/src/ln/msgs.rs

+125-3
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ mod fuzzy_internal_msgs {
16661666
use crate::prelude::*;
16671667
use crate::ln::{PaymentPreimage, PaymentSecret};
16681668
use crate::ln::features::BlindedHopFeatures;
1669-
use super::FinalOnionHopData;
1669+
use super::{FinalOnionHopData, TrampolineOnionPacket};
16701670

16711671
// These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
16721672
// them from untrusted input):
@@ -1711,6 +1711,13 @@ mod fuzzy_internal_msgs {
17111711
amt_to_forward: u64,
17121712
outgoing_cltv_value: u32,
17131713
},
1714+
#[allow(unused)]
1715+
TrampolineEntrypoint {
1716+
amt_to_forward: u64,
1717+
outgoing_cltv_value: u32,
1718+
multipath_trampoline_data: Option<FinalOnionHopData>,
1719+
trampoline_packet: TrampolineOnionPacket,
1720+
},
17141721
Receive {
17151722
payment_data: Option<FinalOnionHopData>,
17161723
payment_metadata: Option<Vec<u8>>,
@@ -1779,6 +1786,52 @@ impl fmt::Debug for OnionPacket {
17791786
}
17801787
}
17811788

1789+
/// BOLT 4 onion packet including hop data for the next peer.
1790+
#[derive(Clone, Hash, PartialEq, Eq)]
1791+
pub struct TrampolineOnionPacket {
1792+
/// Bolt 04 version number
1793+
pub version: u8,
1794+
/// A random sepc256k1 point, used to build the ECDH shared secret to decrypt hop_data
1795+
pub public_key: PublicKey,
1796+
/// Encrypted payload for the next hop
1797+
//
1798+
// Unlike the onion packets used for payments, Trampoline onion packets have to be shorter than
1799+
// 1300 bytes. The expected default is 650 bytes.
1800+
// TODO: if 650 ends up being the most common size, optimize this to be:
1801+
// enum { ThirteenHundred([u8; 650]), VarLen(Vec<u8>) }
1802+
pub hop_data: Vec<u8>,
1803+
/// HMAC to verify the integrity of hop_data
1804+
pub hmac: [u8; 32],
1805+
}
1806+
1807+
impl onion_utils::Packet for TrampolineOnionPacket {
1808+
type Data = Vec<u8>;
1809+
fn new(public_key: PublicKey, hop_data: Vec<u8>, hmac: [u8; 32]) -> Self {
1810+
Self {
1811+
version: 0,
1812+
public_key,
1813+
hop_data,
1814+
hmac,
1815+
}
1816+
}
1817+
}
1818+
1819+
impl Writeable for TrampolineOnionPacket {
1820+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1821+
self.version.write(w)?;
1822+
self.public_key.write(w)?;
1823+
w.write_all(&self.hop_data)?;
1824+
self.hmac.write(w)?;
1825+
Ok(())
1826+
}
1827+
}
1828+
1829+
impl Debug for TrampolineOnionPacket {
1830+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1831+
f.write_fmt(format_args!("TrampolineOnionPacket version {} with hmac {:?}", self.version, &self.hmac[..]))
1832+
}
1833+
}
1834+
17821835
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
17831836
pub(crate) struct OnionErrorPacket {
17841837
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
@@ -2492,6 +2545,17 @@ impl Writeable for OutboundOnionPayload {
24922545
(6, short_channel_id, required)
24932546
});
24942547
},
2548+
Self::TrampolineEntrypoint {
2549+
amt_to_forward, outgoing_cltv_value, ref multipath_trampoline_data,
2550+
ref trampoline_packet
2551+
} => {
2552+
_encode_varint_length_prefixed_tlv!(w, {
2553+
(2, HighZeroBytesDroppedBigSize(*amt_to_forward), required),
2554+
(4, HighZeroBytesDroppedBigSize(*outgoing_cltv_value), required),
2555+
(8, multipath_trampoline_data, option),
2556+
(20, trampoline_packet, required)
2557+
});
2558+
},
24952559
Self::Receive {
24962560
ref payment_data, ref payment_metadata, ref keysend_preimage, sender_intended_htlc_amt_msat,
24972561
cltv_expiry_height, ref custom_tlvs,
@@ -3059,10 +3123,10 @@ mod tests {
30593123
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
30603124
use crate::ln::ChannelId;
30613125
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
3062-
use crate::ln::msgs::{self, FinalOnionHopData, OnionErrorPacket, CommonOpenChannelFields, CommonAcceptChannelFields};
3126+
use crate::ln::msgs::{self, FinalOnionHopData, OnionErrorPacket, CommonOpenChannelFields, CommonAcceptChannelFields, TrampolineOnionPacket};
30633127
use crate::ln::msgs::SocketAddress;
30643128
use crate::routing::gossip::{NodeAlias, NodeId};
3065-
use crate::util::ser::{Writeable, Readable, ReadableArgs, Hostname, TransactionU16LenLimited};
3129+
use crate::util::ser::{BigSize, Hostname, Readable, ReadableArgs, TransactionU16LenLimited, Writeable};
30663130
use crate::util::test_utils;
30673131

30683132
use bitcoin::hashes::hex::FromHex;
@@ -4354,6 +4418,64 @@ mod tests {
43544418
} else { panic!(); }
43554419
}
43564420

4421+
#[test]
4422+
fn encoding_final_onion_hop_data_with_trampoline_packet() {
4423+
let secp_ctx = Secp256k1::new();
4424+
let (_private_key, public_key) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101", secp_ctx);
4425+
4426+
let compressed_public_key = public_key.serialize();
4427+
assert_eq!(compressed_public_key.len(), 33);
4428+
4429+
let trampoline_packet = TrampolineOnionPacket {
4430+
version: 0,
4431+
public_key,
4432+
hop_data: vec![1; 650], // this should be the standard encoded length
4433+
hmac: [2; 32],
4434+
};
4435+
let encoded_trampoline_packet = trampoline_packet.encode();
4436+
assert_eq!(encoded_trampoline_packet.len(), 716);
4437+
4438+
let msg = msgs::OutboundOnionPayload::TrampolineEntrypoint {
4439+
multipath_trampoline_data: None,
4440+
amt_to_forward: 0x0badf00d01020304,
4441+
outgoing_cltv_value: 0xffffffff,
4442+
trampoline_packet,
4443+
};
4444+
let encoded_payload = msg.encode();
4445+
4446+
let trampoline_type_bytes = &encoded_payload[19..=19];
4447+
let mut trampoline_type_cursor = Cursor::new(trampoline_type_bytes);
4448+
let trampoline_type_big_size: BigSize = Readable::read(&mut trampoline_type_cursor).unwrap();
4449+
assert_eq!(trampoline_type_big_size.0, 20);
4450+
4451+
let trampoline_length_bytes = &encoded_payload[20..=22];
4452+
let mut trampoline_length_cursor = Cursor::new(trampoline_length_bytes);
4453+
let trampoline_length_big_size: BigSize = Readable::read(&mut trampoline_length_cursor).unwrap();
4454+
assert_eq!(trampoline_length_big_size.0, encoded_trampoline_packet.len() as u64);
4455+
}
4456+
4457+
#[test]
4458+
fn encoding_final_onion_hop_data_with_eclair_trampoline_packet() {
4459+
let public_key = PublicKey::from_slice(&<Vec<u8>>::from_hex("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()).unwrap();
4460+
let hop_data = <Vec<u8>>::from_hex("cff34152f3a36e52ca94e74927203a560392b9cc7ce3c45809c6be52166c24a595716880f95f178bf5b30ca63141f74db6e92795c6130877cfdac3d4bd3087ee73c65d627ddd709112a848cc99e303f3706509aa43ba7c8a88cba175fccf9a8f5016ef06d3b935dbb15196d7ce16dc1a7157845566901d7b2197e52cab4ce487014b14816e5805f9fcacb4f8f88b8ff176f1b94f6ce6b00bc43221130c17d20ef629db7c5f7eafaa166578c720619561dd14b3277db557ec7dcdb793771aef0f2f667cfdbeae3ac8d331c5994779dffb31e5fc0dbdedc0c592ca6d21c18e47fe3528d6975c19517d7e2ea8c5391cf17d0fe30c80913ed887234ccb48808f7ef9425bcd815c3b586210979e3bb286ef2851bf9ce04e28c40a203df98fd648d2f1936fd2f1def0e77eecb277229b4b682322371c0a1dbfcd723a991993df8cc1f2696b84b055b40a1792a29f710295a18fbd351b0f3ff34cd13941131b8278ba79303c89117120eea691738a9954908195143b039dbeed98f26a92585f3d15cf742c953799d3272e0545e9b744be9d3b4c").unwrap();
4461+
let hmac_vector = <Vec<u8>>::from_hex("bb079bfc4b35190eee9f59a1d7b41ba2f773179f322dafb4b1af900c289ebd6c").unwrap();
4462+
let mut hmac = [0; 32];
4463+
hmac.copy_from_slice(&hmac_vector);
4464+
4465+
let compressed_public_key = public_key.serialize();
4466+
assert_eq!(compressed_public_key.len(), 33);
4467+
4468+
let trampoline_packet = TrampolineOnionPacket {
4469+
version: 0,
4470+
public_key,
4471+
hop_data,
4472+
hmac,
4473+
};
4474+
let encoded_trampoline_packet = trampoline_packet.encode();
4475+
let expected_eclair_trampoline_packet = <Vec<u8>>::from_hex("0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619cff34152f3a36e52ca94e74927203a560392b9cc7ce3c45809c6be52166c24a595716880f95f178bf5b30ca63141f74db6e92795c6130877cfdac3d4bd3087ee73c65d627ddd709112a848cc99e303f3706509aa43ba7c8a88cba175fccf9a8f5016ef06d3b935dbb15196d7ce16dc1a7157845566901d7b2197e52cab4ce487014b14816e5805f9fcacb4f8f88b8ff176f1b94f6ce6b00bc43221130c17d20ef629db7c5f7eafaa166578c720619561dd14b3277db557ec7dcdb793771aef0f2f667cfdbeae3ac8d331c5994779dffb31e5fc0dbdedc0c592ca6d21c18e47fe3528d6975c19517d7e2ea8c5391cf17d0fe30c80913ed887234ccb48808f7ef9425bcd815c3b586210979e3bb286ef2851bf9ce04e28c40a203df98fd648d2f1936fd2f1def0e77eecb277229b4b682322371c0a1dbfcd723a991993df8cc1f2696b84b055b40a1792a29f710295a18fbd351b0f3ff34cd13941131b8278ba79303c89117120eea691738a9954908195143b039dbeed98f26a92585f3d15cf742c953799d3272e0545e9b744be9d3b4cbb079bfc4b35190eee9f59a1d7b41ba2f773179f322dafb4b1af900c289ebd6c").unwrap();
4476+
assert_eq!(encoded_trampoline_packet, expected_eclair_trampoline_packet);
4477+
}
4478+
43574479
#[test]
43584480
fn query_channel_range_end_blocknum() {
43594481
let tests: Vec<(u32, u32, u32)> = vec![

0 commit comments

Comments
 (0)