@@ -8594,6 +8594,7 @@ impl<
85948594 let verify_res = inbound_payment::verify(
85958595 payment_hash,
85968596 &payment_data,
8597+ onion_fields.payment_metadata.as_deref(),
85978598 self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
85988599 &self.inbound_payment_key,
85998600 &self.logger,
@@ -14255,7 +14256,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1425514256 ) -> Result<Bolt11Invoice, SignOrCreationError<()>> {
1425614257 let Bolt11InvoiceParameters {
1425714258 amount_msats, description, invoice_expiry_delta_secs, min_final_cltv_expiry_delta,
14258- payment_hash,
14259+ payment_hash, payment_metadata,
1425914260 } = params;
1426014261
1426114262 let currency =
@@ -14288,6 +14289,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1428814289 payment_hash, amount_msats,
1428914290 invoice_expiry_delta_secs.unwrap_or(DEFAULT_EXPIRY_TIME as u32),
1429014291 min_final_cltv_expiry_delta,
14292+ payment_metadata.as_deref(),
1429114293 )
1429214294 .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
1429314295 (payment_hash, payment_secret)
@@ -14297,6 +14299,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1429714299 .create_inbound_payment(
1429814300 amount_msats, invoice_expiry_delta_secs.unwrap_or(DEFAULT_EXPIRY_TIME as u32),
1429914301 min_final_cltv_expiry_delta,
14302+ payment_metadata.as_deref(),
1430014303 )
1430114304 .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?
1430214305 },
@@ -14335,7 +14338,11 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1433514338 invoice = invoice.private_route(hint);
1433614339 }
1433714340
14338- let raw_invoice = invoice.build_raw().map_err(|e| SignOrCreationError::CreationError(e))?;
14341+ let raw_invoice = if let Some(payment_metadata) = payment_metadata {
14342+ invoice.payment_metadata(payment_metadata).build_raw()
14343+ } else {
14344+ invoice.build_raw()
14345+ }.map_err(|e| SignOrCreationError::CreationError(e))?;
1433914346 let signature = self.node_signer.sign_invoice(&raw_invoice, Recipient::Node);
1434014347
1434114348 raw_invoice
@@ -14414,6 +14421,14 @@ pub struct Bolt11InvoiceParameters {
1441414421 /// involving another protocol where the payment hash is also involved outside the scope of
1441514422 /// lightning.
1441614423 pub payment_hash: Option<PaymentHash>,
14424+
14425+ /// The `payment_metadata` to include in the invoice. This is provided back to us in the payment
14426+ /// onion by the sender, available as [`RecipientOnionFields::payment_metadata`] via
14427+ /// [`Event::PaymentClaimable::onion_fields`].
14428+ ///
14429+ /// Note that because it is exposed to the sender in the invoice you should consider encrypting
14430+ /// it. It is committed to, however, so cannot be modified by the sender.
14431+ pub payment_metadata: Option<Vec<u8>>,
1441714432}
1441814433
1441914434impl Default for Bolt11InvoiceParameters {
@@ -14424,6 +14439,7 @@ impl Default for Bolt11InvoiceParameters {
1442414439 invoice_expiry_delta_secs: None,
1442514440 min_final_cltv_expiry_delta: None,
1442614441 payment_hash: None,
14442+ payment_metadata: None,
1442714443 }
1442814444 }
1442914445}
@@ -14915,7 +14931,7 @@ impl<
1491514931 refund,
1491614932 self.list_usable_channels(),
1491714933 |amount_msats, relative_expiry| {
14918- self.create_inbound_payment(Some(amount_msats), relative_expiry, None)
14934+ self.create_inbound_payment(Some(amount_msats), relative_expiry, None, None )
1491914935 .map_err(|()| Bolt12SemanticError::InvalidAmount)
1492014936 },
1492114937 )?;
@@ -14958,7 +14974,7 @@ impl<
1495814974 /// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
1495914975 pub fn create_inbound_payment(
1496014976 &self, min_value_msat: Option<u64>, invoice_expiry_delta_secs: u32,
14961- min_final_cltv_expiry_delta: Option<u16>,
14977+ min_final_cltv_expiry_delta: Option<u16>, payment_metadata: Option<&[u8]>,
1496214978 ) -> Result<(PaymentHash, PaymentSecret), ()> {
1496314979 inbound_payment::create(
1496414980 &self.inbound_payment_key,
@@ -14967,6 +14983,7 @@ impl<
1496714983 &self.entropy_source,
1496814984 self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
1496914985 min_final_cltv_expiry_delta,
14986+ payment_metadata,
1497014987 )
1497114988 }
1497214989
@@ -14986,6 +15003,9 @@ impl<
1498615003 /// before a [`PaymentClaimable`] event will be generated, ensuring that we do not provide the
1498715004 /// sender "proof-of-payment" unless they have paid the required amount.
1498815005 ///
15006+ /// The returned secret commits to the `payment_metadata` and thus the invoice's metadata must
15007+ /// match what is provided here.
15008+ ///
1498915009 /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
1499015010 /// in excess of the current time. This should roughly match the expiry time set in the invoice.
1499115011 /// After this many seconds, we will remove the inbound payment, resulting in any attempts to
@@ -15019,6 +15039,7 @@ impl<
1501915039 pub fn create_inbound_payment_for_hash(
1502015040 &self, payment_hash: PaymentHash, min_value_msat: Option<u64>,
1502115041 invoice_expiry_delta_secs: u32, min_final_cltv_expiry: Option<u16>,
15042+ payment_metadata: Option<&[u8]>,
1502215043 ) -> Result<PaymentSecret, ()> {
1502315044 inbound_payment::create_from_hash(
1502415045 &self.inbound_payment_key,
@@ -15027,18 +15048,25 @@ impl<
1502715048 invoice_expiry_delta_secs,
1502815049 self.highest_seen_timestamp.load(Ordering::Acquire) as u64,
1502915050 min_final_cltv_expiry,
15051+ payment_metadata,
1503015052 )
1503115053 }
1503215054
15033- /// Gets an LDK-generated payment preimage from a payment hash and payment secret that were
15055+ /// Gets an LDK-generated payment preimage from a payment hash, metadata and secret that were
1503415056 /// previously returned from [`create_inbound_payment`].
1503515057 ///
1503615058 /// [`create_inbound_payment`]: Self::create_inbound_payment
1503715059 pub fn get_payment_preimage(
1503815060 &self, payment_hash: PaymentHash, payment_secret: PaymentSecret,
15061+ payment_metadata: Option<&[u8]>,
1503915062 ) -> Result<PaymentPreimage, APIError> {
1504015063 let expanded_key = &self.inbound_payment_key;
15041- inbound_payment::get_payment_preimage(payment_hash, payment_secret, expanded_key)
15064+ inbound_payment::get_payment_preimage(
15065+ payment_hash,
15066+ payment_secret,
15067+ payment_metadata,
15068+ expanded_key,
15069+ )
1504215070 }
1504315071
1504415072 /// [`BlindedMessagePath`]s for an async recipient to communicate with this node and interactively
@@ -17107,7 +17135,8 @@ impl<
1710717135 self.create_inbound_payment(
1710817136 Some(amount_msats),
1710917137 relative_expiry,
17110- None
17138+ None,
17139+ None,
1711117140 ).map_err(|_| Bolt12SemanticError::InvalidAmount)
1711217141 };
1711317142
@@ -21319,15 +21348,15 @@ mod tests {
2131921348 // payment verification fails as expected.
2132021349 let mut bad_payment_hash = payment_hash.clone();
2132121350 bad_payment_hash.0[0] += 1;
21322- match inbound_payment::verify(bad_payment_hash, &payment_data, nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger) {
21351+ match inbound_payment::verify(bad_payment_hash, &payment_data, None, nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger) {
2132321352 Ok(_) => panic!("Unexpected ok"),
2132421353 Err(()) => {
2132521354 nodes[0].logger.assert_log_contains("lightning::ln::inbound_payment", "Failing HTLC with user-generated payment_hash", 1);
2132621355 }
2132721356 }
2132821357
2132921358 // Check that using the original payment hash succeeds.
21330- assert!(inbound_payment::verify(payment_hash, &payment_data, nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger).is_ok());
21359+ assert!(inbound_payment::verify(payment_hash, &payment_data, None, nodes[0].node.highest_seen_timestamp.load(Ordering::Acquire) as u64, &nodes[0].node.inbound_payment_key, &nodes[0].logger).is_ok());
2133121360 }
2133221361
2133321362 fn check_not_connected_to_peer_error<T>(
@@ -22000,7 +22029,7 @@ pub mod bench {
2200022029 payment_preimage.0[0..8].copy_from_slice(&payment_count.to_le_bytes());
2200122030 payment_count += 1;
2200222031 let payment_hash = PaymentHash(Sha256::hash(&payment_preimage.0[..]).to_byte_array());
22003- let payment_secret = $node_b.create_inbound_payment_for_hash(payment_hash, None, 7200, None).unwrap();
22032+ let payment_secret = $node_b.create_inbound_payment_for_hash(payment_hash, None, 7200, None, None ).unwrap();
2200422033
2200522034 $node_a.send_payment(payment_hash, RecipientOnionFields::secret_only(payment_secret, 10_000),
2200622035 PaymentId(payment_hash.0),
0 commit comments