-
Notifications
You must be signed in to change notification settings - Fork 51
feat: allow setting custom preimage in spontaneous-send #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,9 @@ | |
| use std::str::FromStr; | ||
| use std::sync::Arc; | ||
|
|
||
| use hex::FromHex; | ||
| use ldk_node::bitcoin::secp256k1::PublicKey; | ||
| use ldk_node::lightning_types::payment::PaymentPreimage; | ||
| use ldk_server_grpc::api::{SpontaneousSendRequest, SpontaneousSendResponse}; | ||
|
|
||
| use crate::api::error::LdkServerError; | ||
|
|
@@ -27,19 +29,48 @@ pub(crate) async fn handle_spontaneous_send_request( | |
|
|
||
| let route_parameters = build_route_parameters_config_from_proto(request.route_parameters)?; | ||
|
|
||
| let payment_id = if request.custom_tlvs.is_empty() { | ||
| context.node.spontaneous_payment().send(request.amount_msat, node_id, route_parameters)? | ||
| } else { | ||
| let custom_tlvs: Vec<_> = | ||
| request.custom_tlvs.iter().map(proto_to_node_custom_tlv).collect(); | ||
| context.node.spontaneous_payment().send_with_custom_tlvs( | ||
| let preimage = request | ||
| .preimage | ||
| .map(|p| { | ||
| <[u8; 32]>::from_hex(&p).map(PaymentPreimage).map_err(|_| { | ||
| LdkServerError::new( | ||
| InvalidRequestError, | ||
| "Invalid preimage, must be a 32-byte hex string.".to_string(), | ||
| ) | ||
| }) | ||
| }) | ||
| .transpose()?; | ||
|
|
||
| let custom_tlvs: Vec<_> = request.custom_tlvs.iter().map(proto_to_node_custom_tlv).collect(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No strong opinion here, especially as I believe there's really no runtime cost in iterating and mapping over an empty vector. But since we're creating the vector and immediately checking if it's empty in the I'm wondering if we could do something like this instead? |
||
|
|
||
| let payment_id = match (preimage, custom_tlvs.is_empty()) { | ||
| (None, true) => context.node.spontaneous_payment().send( | ||
| request.amount_msat, | ||
| node_id, | ||
| route_parameters, | ||
| )?, | ||
| (None, false) => context.node.spontaneous_payment().send_with_custom_tlvs( | ||
| request.amount_msat, | ||
| node_id, | ||
| route_parameters, | ||
| custom_tlvs, | ||
| )? | ||
| )?, | ||
| (Some(preimage), true) => context.node.spontaneous_payment().send_with_preimage( | ||
| request.amount_msat, | ||
| node_id, | ||
| preimage, | ||
| route_parameters, | ||
| )?, | ||
| (Some(preimage), false) => { | ||
| context.node.spontaneous_payment().send_with_preimage_and_custom_tlvs( | ||
| request.amount_msat, | ||
| node_id, | ||
| custom_tlvs, | ||
| preimage, | ||
| route_parameters, | ||
| )? | ||
| }, | ||
| }; | ||
|
|
||
| let response = SpontaneousSendResponse { payment_id: payment_id.to_string() }; | ||
| Ok(response) | ||
| Ok(SpontaneousSendResponse { payment_id: payment_id.to_string() }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we have a new line between the imports and const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@f3r10 Looks like you missed this in your latest push.