Skip to content

Commit 7f28543

Browse files
Merge pull request #163 from benthecarman/bolt11-recv-ret
Return payment hash & secret in Bolt11 and offer id in Bolt12 receive responses
2 parents 6b1a799 + 1a5ce9e commit 7f28543

5 files changed

Lines changed: 43 additions & 7 deletions

File tree

e2e-tests/tests/e2e.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ 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::offers::offer::Offer;
21+
use ldk_node::lightning_invoice::Bolt11Invoice;
2022
use ldk_server_client::ldk_server_protos::api::{
2123
Bolt11ReceiveRequest, Bolt12ReceiveRequest, OnchainReceiveRequest,
2224
};
@@ -136,8 +138,14 @@ async fn test_cli_bolt11_receive() {
136138
let server = LdkServerHandle::start(&bitcoind).await;
137139

138140
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);
141+
let invoice_str = output["invoice"].as_str().unwrap();
142+
assert!(invoice_str.starts_with("lnbcrt"), "Expected lnbcrt prefix, got: {}", invoice_str);
143+
144+
let invoice: Bolt11Invoice = invoice_str.parse().unwrap();
145+
let payment_hash = sha256::Hash::from_str(output["payment_hash"].as_str().unwrap()).unwrap();
146+
assert_eq!(*invoice.payment_hash(), payment_hash);
147+
let payment_secret = <[u8; 32]>::from_hex(output["payment_secret"].as_str().unwrap()).unwrap();
148+
assert_eq!(invoice.payment_secret().0, payment_secret);
141149
}
142150

143151
#[tokio::test]
@@ -149,8 +157,12 @@ async fn test_cli_bolt12_receive() {
149157
setup_funded_channel(&bitcoind, &server_a, &server_b, 100_000).await;
150158

151159
let output = run_cli(&server_a, &["bolt12-receive", "test offer"]);
152-
let offer = output["offer"].as_str().unwrap();
153-
assert!(offer.starts_with("lno"), "Expected lno prefix, got: {}", offer);
160+
let offer_str = output["offer"].as_str().unwrap();
161+
assert!(offer_str.starts_with("lno"), "Expected lno prefix, got: {}", offer_str);
162+
163+
let offer: Offer = offer_str.parse().unwrap();
164+
let offer_id = <[u8; 32]>::from_hex(output["offer_id"].as_str().unwrap()).unwrap();
165+
assert_eq!(offer.id().0, offer_id);
154166
}
155167

156168
#[tokio::test]

ldk-server-protos/src/api.rs

Lines changed: 9 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.
@@ -389,6 +395,9 @@ pub struct Bolt12ReceiveResponse {
389395
/// to the recipient.
390396
#[prost(string, tag = "1")]
391397
pub offer: ::prost::alloc::string::String,
398+
/// The hex-encoded offer id.
399+
#[prost(string, tag = "2")]
400+
pub offer_id: ::prost::alloc::string::String,
392401
}
393402
/// Send a payment for a BOLT12 offer.
394403
/// See more:

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

Lines changed: 9 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.
@@ -322,6 +328,9 @@ message Bolt12ReceiveResponse {
322328
// With the details of the offer, the sender has all the data necessary to send a payment
323329
// to the recipient.
324330
string offer = 1;
331+
332+
// The hex-encoded offer id.
333+
string offer_id = 2;
325334
}
326335

327336
// Send a payment for a BOLT12 offer.

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
}

ldk-server/src/api/bolt12_receive.rs

Lines changed: 3 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::{Bolt12ReceiveRequest, Bolt12ReceiveResponse};
1112

1213
use crate::api::error::LdkServerError;
@@ -28,6 +29,7 @@ pub(crate) fn handle_bolt12_receive_request(
2829
.receive_variable_amount(&request.description, request.expiry_secs)?,
2930
};
3031

31-
let response = Bolt12ReceiveResponse { offer: offer.to_string() };
32+
let offer_id = offer.id().0.to_lower_hex_string();
33+
let response = Bolt12ReceiveResponse { offer: offer.to_string(), offer_id };
3234
Ok(response)
3335
}

0 commit comments

Comments
 (0)