Skip to content

Commit 7f2d806

Browse files
Add experimental TLV fields for invoice requests: invreq_contact_secret, invreq_payer_offer, invreq_payer_bip_353_name
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
1 parent 08c2236 commit 7f2d806

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

lightning/src/offers/invoice.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ type FullInvoiceTlvStreamRef<'a> = (
15461546
InvoiceTlvStreamRef<'a>,
15471547
SignatureTlvStreamRef<'a>,
15481548
ExperimentalOfferTlvStreamRef,
1549-
ExperimentalInvoiceRequestTlvStreamRef,
1549+
ExperimentalInvoiceRequestTlvStreamRef<'a>,
15501550
ExperimentalInvoiceTlvStreamRef,
15511551
);
15521552

@@ -1590,7 +1590,7 @@ type PartialInvoiceTlvStreamRef<'a> = (
15901590
InvoiceRequestTlvStreamRef<'a>,
15911591
InvoiceTlvStreamRef<'a>,
15921592
ExperimentalOfferTlvStreamRef,
1593-
ExperimentalInvoiceRequestTlvStreamRef,
1593+
ExperimentalInvoiceRequestTlvStreamRef<'a>,
15941594
ExperimentalInvoiceTlvStreamRef,
15951595
);
15961596

@@ -2014,7 +2014,12 @@ mod tests {
20142014
},
20152015
SignatureTlvStreamRef { signature: Some(&invoice.signature()) },
20162016
ExperimentalOfferTlvStreamRef { experimental_foo: None },
2017-
ExperimentalInvoiceRequestTlvStreamRef { experimental_bar: None },
2017+
ExperimentalInvoiceRequestTlvStreamRef {
2018+
experimental_bar: None,
2019+
invreq_contact_secret: None,
2020+
invreq_payer_offer: None,
2021+
invreq_payer_bip_353_name: None,
2022+
},
20182023
ExperimentalInvoiceTlvStreamRef { experimental_baz: None },
20192024
),
20202025
);
@@ -2117,7 +2122,12 @@ mod tests {
21172122
},
21182123
SignatureTlvStreamRef { signature: Some(&invoice.signature()) },
21192124
ExperimentalOfferTlvStreamRef { experimental_foo: None },
2120-
ExperimentalInvoiceRequestTlvStreamRef { experimental_bar: None },
2125+
ExperimentalInvoiceRequestTlvStreamRef {
2126+
experimental_bar: None,
2127+
invreq_contact_secret: None,
2128+
invreq_payer_offer: None,
2129+
invreq_payer_bip_353_name: None,
2130+
},
21212131
ExperimentalInvoiceTlvStreamRef { experimental_baz: None },
21222132
),
21232133
);

lightning/src/offers/invoice_request.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
use crate::blinded_path::message::BlindedMessagePath;
6969
use crate::blinded_path::payment::BlindedPaymentPath;
7070
use crate::io;
71+
use crate::io::Read;
7172
use crate::ln::channelmanager::PaymentId;
7273
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
7374
use crate::ln::msgs::DecodeError;
@@ -500,7 +501,11 @@ impl UnsignedInvoiceRequest {
500501

501502
invoice_request_tlv_stream.write(&mut bytes).unwrap();
502503

503-
const EXPERIMENTAL_TLV_ALLOCATION_SIZE: usize = 0;
504+
// Allocate sufficient capacity for experimental TLV fields to avoid reallocations.
505+
// The new fields (invreq_contact_secret: ~48 bytes, invreq_payer_offer: ~116 bytes,
506+
// invreq_payer_bip_353_name: ~116 bytes) total ~280 bytes, with 600 providing headroom
507+
// for future experimental fields and variable-length data.
508+
const EXPERIMENTAL_TLV_ALLOCATION_SIZE: usize = 600;
504509
let mut experimental_bytes = Vec::with_capacity(EXPERIMENTAL_TLV_ALLOCATION_SIZE);
505510

506511
let experimental_tlv_stream =
@@ -1225,6 +1230,9 @@ impl InvoiceRequestContentsWithoutPayerSigningPubkey {
12251230
};
12261231

12271232
let experimental_invoice_request = ExperimentalInvoiceRequestTlvStreamRef {
1233+
invreq_contact_secret: None,
1234+
invreq_payer_offer: None,
1235+
invreq_payer_bip_353_name: None,
12281236
#[cfg(test)]
12291237
experimental_bar: self.experimental_bar,
12301238
};
@@ -1288,21 +1296,47 @@ tlv_stream!(InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef<'a>, INVOICE_REQ
12881296
pub(super) const EXPERIMENTAL_INVOICE_REQUEST_TYPES: core::ops::Range<u64> =
12891297
2_000_000_000..3_000_000_000;
12901298

1299+
/// A contact secret used in experimental TLV fields.
1300+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
1301+
pub struct ContactSecret {
1302+
contents: [u8; 32],
1303+
}
1304+
1305+
impl Readable for ContactSecret {
1306+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1307+
let mut buf = [0u8; 32];
1308+
r.read_exact(&mut buf)?;
1309+
Ok(ContactSecret { contents: buf })
1310+
}
1311+
}
1312+
1313+
impl Writeable for ContactSecret {
1314+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1315+
w.write_all(&self.contents)
1316+
}
1317+
}
1318+
12911319
#[cfg(not(test))]
12921320
tlv_stream!(
12931321
ExperimentalInvoiceRequestTlvStream,
1294-
ExperimentalInvoiceRequestTlvStreamRef,
1322+
ExperimentalInvoiceRequestTlvStreamRef<'a>,
12951323
EXPERIMENTAL_INVOICE_REQUEST_TYPES,
12961324
{
1325+
(2_000_001_729, invreq_contact_secret: (Vec<u8>, WithoutLength)),
1326+
(2_000_001_731, invreq_payer_offer: (Vec<u8>, WithoutLength)),
1327+
(2_000_001_733, invreq_payer_bip_353_name: (Vec<u8>, WithoutLength)),
12971328
// When adding experimental TLVs, update EXPERIMENTAL_TLV_ALLOCATION_SIZE accordingly in
12981329
// UnsignedInvoiceRequest::new to avoid unnecessary allocations.
12991330
}
13001331
);
13011332

13021333
#[cfg(test)]
13031334
tlv_stream!(
1304-
ExperimentalInvoiceRequestTlvStream, ExperimentalInvoiceRequestTlvStreamRef,
1335+
ExperimentalInvoiceRequestTlvStream, ExperimentalInvoiceRequestTlvStreamRef<'a>,
13051336
EXPERIMENTAL_INVOICE_REQUEST_TYPES, {
1337+
(2_000_001_729, invreq_contact_secret: (Vec<u8>, WithoutLength)),
1338+
(2_000_001_731, invreq_payer_offer: (Vec<u8>, WithoutLength)),
1339+
(2_000_001_733, invreq_payer_bip_353_name: (Vec<u8>, WithoutLength)),
13061340
(2_999_999_999, experimental_bar: (u64, HighZeroBytesDroppedBigSize)),
13071341
}
13081342
);
@@ -1322,7 +1356,7 @@ type FullInvoiceRequestTlvStreamRef<'a> = (
13221356
InvoiceRequestTlvStreamRef<'a>,
13231357
SignatureTlvStreamRef<'a>,
13241358
ExperimentalOfferTlvStreamRef,
1325-
ExperimentalInvoiceRequestTlvStreamRef,
1359+
ExperimentalInvoiceRequestTlvStreamRef<'a>,
13261360
);
13271361

13281362
impl CursorReadable for FullInvoiceRequestTlvStream {
@@ -1358,7 +1392,7 @@ type PartialInvoiceRequestTlvStreamRef<'a> = (
13581392
OfferTlvStreamRef<'a>,
13591393
InvoiceRequestTlvStreamRef<'a>,
13601394
ExperimentalOfferTlvStreamRef,
1361-
ExperimentalInvoiceRequestTlvStreamRef,
1395+
ExperimentalInvoiceRequestTlvStreamRef<'a>,
13621396
);
13631397

13641398
impl TryFrom<Vec<u8>> for UnsignedInvoiceRequest {
@@ -1437,6 +1471,9 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
14371471
},
14381472
experimental_offer_tlv_stream,
14391473
ExperimentalInvoiceRequestTlvStream {
1474+
invreq_contact_secret: _,
1475+
invreq_payer_offer: _,
1476+
invreq_payer_bip_353_name: _,
14401477
#[cfg(test)]
14411478
experimental_bar,
14421479
},
@@ -1660,7 +1697,12 @@ mod tests {
16601697
},
16611698
SignatureTlvStreamRef { signature: Some(&invoice_request.signature()) },
16621699
ExperimentalOfferTlvStreamRef { experimental_foo: None },
1663-
ExperimentalInvoiceRequestTlvStreamRef { experimental_bar: None },
1700+
ExperimentalInvoiceRequestTlvStreamRef {
1701+
invreq_contact_secret: None,
1702+
invreq_payer_offer: None,
1703+
invreq_payer_bip_353_name: None,
1704+
experimental_bar: None,
1705+
},
16641706
),
16651707
);
16661708

lightning/src/offers/refund.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,9 @@ impl RefundContents {
816816
};
817817

818818
let experimental_invoice_request = ExperimentalInvoiceRequestTlvStreamRef {
819+
invreq_contact_secret: None,
820+
invreq_payer_offer: None,
821+
invreq_payer_bip_353_name: None,
819822
#[cfg(test)]
820823
experimental_bar: self.experimental_bar,
821824
};
@@ -861,7 +864,7 @@ type RefundTlvStreamRef<'a> = (
861864
OfferTlvStreamRef<'a>,
862865
InvoiceRequestTlvStreamRef<'a>,
863866
ExperimentalOfferTlvStreamRef,
864-
ExperimentalInvoiceRequestTlvStreamRef,
867+
ExperimentalInvoiceRequestTlvStreamRef<'a>,
865868
);
866869

867870
impl CursorReadable for RefundTlvStream {
@@ -934,6 +937,9 @@ impl TryFrom<RefundTlvStream> for RefundContents {
934937
experimental_foo,
935938
},
936939
ExperimentalInvoiceRequestTlvStream {
940+
invreq_contact_secret: _,
941+
invreq_payer_offer: _,
942+
invreq_payer_bip_353_name: _,
937943
#[cfg(test)]
938944
experimental_bar,
939945
},
@@ -1120,7 +1126,12 @@ mod tests {
11201126
offer_from_hrn: None,
11211127
},
11221128
ExperimentalOfferTlvStreamRef { experimental_foo: None },
1123-
ExperimentalInvoiceRequestTlvStreamRef { experimental_bar: None },
1129+
ExperimentalInvoiceRequestTlvStreamRef {
1130+
invreq_contact_secret: None,
1131+
invreq_payer_offer: None,
1132+
invreq_payer_bip_353_name: None,
1133+
experimental_bar: None,
1134+
},
11241135
),
11251136
);
11261137

0 commit comments

Comments
 (0)