Skip to content

Commit 6d50e31

Browse files
committed
Return payment hash and secret in Bolt11ReceiveResponse
Before you would create an invoice and then need to decode it to get the payment hash/secret to then match it to a PaymentReceived event. Now we'll include those in the Bolt11ReceiveResponse to make it easier.
1 parent 6b1a799 commit 6d50e31

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

e2e-tests/tests/e2e.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use e2e_tests::{
1414
find_available_port, mine_and_sync, run_cli, run_cli_raw, setup_funded_channel,
1515
wait_for_onchain_balance, LdkServerHandle, RabbitMqEventConsumer, TestBitcoind,
1616
};
17-
use hex_conservative::DisplayHex;
17+
use hex_conservative::{DisplayHex, FromHex};
1818
use ldk_node::bitcoin::hashes::{sha256, Hash};
1919
use ldk_node::lightning::ln::msgs::SocketAddress;
20+
use ldk_node::lightning_invoice::Bolt11Invoice;
2021
use ldk_server_client::ldk_server_protos::api::{
2122
Bolt11ReceiveRequest, Bolt12ReceiveRequest, OnchainReceiveRequest,
2223
};
@@ -136,8 +137,14 @@ async fn test_cli_bolt11_receive() {
136137
let server = LdkServerHandle::start(&bitcoind).await;
137138

138139
let output = run_cli(&server, &["bolt11-receive", "50000sat", "-d", "test"]);
139-
let invoice = output["invoice"].as_str().unwrap();
140-
assert!(invoice.starts_with("lnbcrt"), "Expected lnbcrt prefix, got: {}", invoice);
140+
let invoice_str = output["invoice"].as_str().unwrap();
141+
assert!(invoice_str.starts_with("lnbcrt"), "Expected lnbcrt prefix, got: {}", invoice_str);
142+
143+
let invoice: Bolt11Invoice = invoice_str.parse().unwrap();
144+
let payment_hash = sha256::Hash::from_str(output["payment_hash"].as_str().unwrap()).unwrap();
145+
assert_eq!(*invoice.payment_hash(), payment_hash);
146+
let payment_secret = <[u8; 32]>::from_hex(output["payment_secret"].as_str().unwrap()).unwrap();
147+
assert_eq!(invoice.payment_secret().0, payment_secret);
141148
}
142149

143150
#[tokio::test]

ldk-server-protos/src/api.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ pub struct Bolt11ReceiveResponse {
173173
/// to the recipient.
174174
#[prost(string, tag = "1")]
175175
pub invoice: ::prost::alloc::string::String,
176+
/// The hex-encoded 32-byte payment hash.
177+
#[prost(string, tag = "2")]
178+
pub payment_hash: ::prost::alloc::string::String,
179+
/// The hex-encoded 32-byte payment secret.
180+
#[prost(string, tag = "3")]
181+
pub payment_secret: ::prost::alloc::string::String,
176182
}
177183
/// Return a BOLT11 payable invoice for a given payment hash.
178184
/// The inbound payment will NOT be automatically claimed upon arrival.

ldk-server-protos/src/proto/api.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ message Bolt11ReceiveResponse {
146146
// With the details of the invoice, the sender has all the data necessary to send a payment
147147
// to the recipient.
148148
string invoice = 1;
149+
150+
// The hex-encoded 32-byte payment hash.
151+
string payment_hash = 2;
152+
153+
// The hex-encoded 32-byte payment secret.
154+
string payment_secret = 3;
149155
}
150156

151157
// Return a BOLT11 payable invoice for a given payment hash.

ldk-server/src/api/bolt11_receive.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10+
use hex::DisplayHex;
1011
use ldk_server_protos::api::{Bolt11ReceiveRequest, Bolt11ReceiveResponse};
1112

1213
use crate::api::error::LdkServerError;
@@ -27,6 +28,9 @@ pub(crate) fn handle_bolt11_receive_request(
2728
.receive_variable_amount(&description, request.expiry_secs)?,
2829
};
2930

30-
let response = Bolt11ReceiveResponse { invoice: invoice.to_string() };
31+
let payment_hash = invoice.payment_hash().0.to_lower_hex_string();
32+
let payment_secret = invoice.payment_secret().0.to_lower_hex_string();
33+
let response =
34+
Bolt11ReceiveResponse { invoice: invoice.to_string(), payment_hash, payment_secret };
3135
Ok(response)
3236
}

0 commit comments

Comments
 (0)