Skip to content

Commit d86f651

Browse files
benthecarmanclaude
andcommitted
Error when quantity set without amount in bolt12_receive
Previously, setting quantity without amount_msat silently produced a variable-amount offer with no quantity constraint. Now both the server and CLI reject this misconfiguration with a clear error message. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a2fdf9c commit d86f651

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

ldk-server-cli/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,11 @@ async fn main() {
780780
);
781781
},
782782
Commands::Bolt12Receive { description, amount, expiry_secs, quantity } => {
783+
if quantity.is_some() && amount.is_none() {
784+
handle_error_msg(
785+
"quantity can only be set for fixed-amount offers (amount must be provided)",
786+
);
787+
}
783788
let amount_msat = amount.map(|a| a.to_msat());
784789
handle_response_result::<_, Bolt12ReceiveResponse>(
785790
client

ldk-server/src/api/bolt12_receive.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,29 @@ use hex::DisplayHex;
1111
use ldk_server_protos::api::{Bolt12ReceiveRequest, Bolt12ReceiveResponse};
1212

1313
use crate::api::error::LdkServerError;
14+
use crate::api::error::LdkServerErrorCode::InvalidRequestError;
1415
use crate::service::Context;
1516

1617
pub(crate) fn handle_bolt12_receive_request(
1718
context: Context, request: Bolt12ReceiveRequest,
1819
) -> Result<Bolt12ReceiveResponse, LdkServerError> {
19-
let offer = match request.amount_msat {
20-
Some(amount_msat) => context.node.bolt12_payment().receive(
21-
amount_msat,
22-
&request.description,
23-
request.expiry_secs,
24-
request.quantity,
25-
)?,
26-
None => context
27-
.node
28-
.bolt12_payment()
29-
.receive_variable_amount(&request.description, request.expiry_secs)?,
30-
};
20+
let offer =
21+
match (request.amount_msat, request.quantity) {
22+
(Some(amount_msat), quantity) => context.node.bolt12_payment().receive(
23+
amount_msat,
24+
&request.description,
25+
request.expiry_secs,
26+
quantity,
27+
)?,
28+
(None, Some(_)) => return Err(LdkServerError::new(
29+
InvalidRequestError,
30+
"quantity can only be set for fixed-amount offers (amount_msat must be provided)",
31+
)),
32+
(None, None) => context
33+
.node
34+
.bolt12_payment()
35+
.receive_variable_amount(&request.description, request.expiry_secs)?,
36+
};
3137

3238
let offer_id = offer.id().0.to_lower_hex_string();
3339
let response = Bolt12ReceiveResponse { offer: offer.to_string(), offer_id };

0 commit comments

Comments
 (0)