Skip to content

Commit

Permalink
Add Custom TLVs for payment::ReceiveTlvs
Browse files Browse the repository at this point in the history
- Building on the previous commit, this update allows users to
  include their own custom TLVs within the reply path of a sent
  onion message.
- With this, users can attach custom data to the message, which
  will be returned in the response, providing more flexibility for
  custom use cases.
  • Loading branch information
shaavan committed Nov 5, 2024
1 parent 5f7054d commit 2668794
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
12 changes: 11 additions & 1 deletion lightning/src/blinded_path/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ pub struct ReceiveTlvs {
pub payment_constraints: PaymentConstraints,
/// Context for the receiver of this payment.
pub payment_context: PaymentContext,
/// Custom Tlvs
pub custom_tlvs: Vec<u8>,
}

/// Data to construct a [`BlindedHop`] for sending a payment over.
Expand Down Expand Up @@ -404,7 +406,8 @@ impl Writeable for ReceiveTlvs {
encode_tlv_stream!(w, {
(12, self.payment_constraints, required),
(65536, self.payment_secret, required),
(65537, self.payment_context, required)
(65537, self.payment_context, required),
(65539, self.custom_tlvs, (default_value, Vec::new())),
});
Ok(())
}
Expand Down Expand Up @@ -432,6 +435,7 @@ impl Readable for BlindedPaymentTlvs {
(14, features, (option, encoding: (BlindedHopFeatures, WithoutLength))),
(65536, payment_secret, option),
(65537, payment_context, (default_value, PaymentContext::unknown())),
(65539, custom_tlvs, (default_value, Vec::new()))
});
let _padding: Option<utils::Padding> = _padding;

Expand All @@ -452,6 +456,7 @@ impl Readable for BlindedPaymentTlvs {
payment_secret: payment_secret.ok_or(DecodeError::InvalidValue)?,
payment_constraints: payment_constraints.0.unwrap(),
payment_context: payment_context.0.unwrap(),
custom_tlvs: custom_tlvs.0.unwrap(),
}))
}
}
Expand Down Expand Up @@ -683,6 +688,7 @@ mod tests {
htlc_minimum_msat: 1,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let htlc_maximum_msat = 100_000;
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, 12).unwrap();
Expand All @@ -702,6 +708,7 @@ mod tests {
htlc_minimum_msat: 1,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs, 4242, TEST_FINAL_CLTV as u16).unwrap();
assert_eq!(blinded_payinfo.fee_base_msat, 0);
Expand Down Expand Up @@ -758,6 +765,7 @@ mod tests {
htlc_minimum_msat: 3,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let htlc_maximum_msat = 100_000;
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, TEST_FINAL_CLTV as u16).unwrap();
Expand Down Expand Up @@ -811,6 +819,7 @@ mod tests {
htlc_minimum_msat: 1,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let htlc_minimum_msat = 3798;
assert!(super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_minimum_msat - 1, TEST_FINAL_CLTV as u16).is_err());
Expand Down Expand Up @@ -868,6 +877,7 @@ mod tests {
htlc_minimum_msat: 1,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new()
};

let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, 10_000, TEST_FINAL_CLTV as u16).unwrap();
Expand Down
4 changes: 4 additions & 0 deletions lightning/src/ln/blinded_payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fn blinded_payment_path(
intro_node_min_htlc_opt.unwrap_or_else(|| channel_upds.last().unwrap().htlc_minimum_msat),
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let mut secp_ctx = Secp256k1::new();
BlindedPaymentPath::new(
Expand Down Expand Up @@ -120,6 +121,7 @@ fn do_one_hop_blinded_path(success: bool) {
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let mut secp_ctx = Secp256k1::new();
let blinded_path = BlindedPaymentPath::new(
Expand Down Expand Up @@ -164,6 +166,7 @@ fn mpp_to_one_hop_blinded_path() {
htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let blinded_path = BlindedPaymentPath::new(
&[], nodes[3].node.get_our_node_id(), payee_tlvs, u64::MAX, TEST_FINAL_CLTV as u16,
Expand Down Expand Up @@ -1310,6 +1313,7 @@ fn custom_tlvs_to_blinded_path() {
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let mut secp_ctx = Secp256k1::new();
let blinded_path = BlindedPaymentPath::new(
Expand Down
7 changes: 4 additions & 3 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9445,7 +9445,7 @@ where
Ok((payment_hash, payment_secret)) => {
let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
let payment_paths = self.create_blinded_payment_paths(
amount_msats, payment_secret, payment_context
amount_msats, payment_secret, payment_context, None
)
.map_err(|_| Bolt12SemanticError::MissingPaths)?;

Expand Down Expand Up @@ -9685,7 +9685,7 @@ where
/// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
/// [`Router::create_blinded_payment_paths`].
fn create_blinded_payment_paths(
&self, amount_msats: u64, payment_secret: PaymentSecret, payment_context: PaymentContext
&self, amount_msats: u64, payment_secret: PaymentSecret, payment_context: PaymentContext, custom_tlvs: Option<Vec<u8>>
) -> Result<Vec<BlindedPaymentPath>, ()> {
let secp_ctx = &self.secp_ctx;

Expand All @@ -9700,6 +9700,7 @@ where
htlc_minimum_msat: 1,
},
payment_context,
custom_tlvs: custom_tlvs.unwrap_or_default()
};
self.router.create_blinded_payment_paths(
payee_node_id, first_hops, payee_tlvs, amount_msats, secp_ctx
Expand Down Expand Up @@ -11190,7 +11191,7 @@ where
invoice_request: invoice_request.fields(),
});
let payment_paths = match self.create_blinded_payment_paths(
amount_msats, payment_secret, payment_context
amount_msats, payment_secret, payment_context, custom_tlvs.clone()
) {
Ok(payment_paths) => payment_paths,
Err(()) => {
Expand Down
1 change: 1 addition & 0 deletions lightning/src/ln/max_payment_path_len_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ fn one_hop_blinded_path_with_custom_tlv() {
htlc_minimum_msat: chan_upd_1_2.htlc_minimum_msat,
},
payment_context: PaymentContext::unknown(),
custom_tlvs: Vec::new(),
};
let mut secp_ctx = Secp256k1::new();
let blinded_path = BlindedPaymentPath::new(
Expand Down
5 changes: 3 additions & 2 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2909,8 +2909,9 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, NS)> for InboundOnionPayload wh
next_blinding_override,
})
},
// Note: The custom tlvs in the receive tlvs is not used here.
ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Receive(ReceiveTlvs {
payment_secret, payment_constraints, payment_context
payment_secret, payment_constraints, payment_context, custom_tlvs: user_custom_tlvs
})} => {
if total_msat.unwrap_or(0) > MAX_VALUE_MSAT { return Err(DecodeError::InvalidValue) }
Ok(Self::BlindedReceive {
Expand All @@ -2923,7 +2924,7 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, NS)> for InboundOnionPayload wh
intro_node_blinding_point,
keysend_preimage,
sender_custom_tlvs,
user_custom_tlvs: Vec::new(),
user_custom_tlvs,
})
},
}
Expand Down

0 comments on commit 2668794

Please sign in to comment.