Skip to content

Commit 1a29ef4

Browse files
authored
Surface outbound payment events via next_event() (#34)
Previously, PaymentSuccessful events from LDK were silently ACKed and discarded by next_event()'s catch-all arm. This meant callers using the event-polling model (as opposed to the blocking wait_for_payment_outcome path) could never learn when an outbound payment completed or obtain its preimage. This is a prerequisite for making agent-wallet payments non-blocking: instead of calling payWhileRunning with a 30-second timeout that blocks the Node.js thread and risks losing the preimage on timeout, the agent-wallet will fire-and-forget the payment and pick up the result through the event loop. Add a Sent variant (discriminant 3) to PaymentEventType and map Event::PaymentSuccessful to it in next_event(), carrying the payment_id and preimage. Also extend PaymentEvent with payment_id and preimage fields, and expose the previously-discarded payment_id on PaymentFailed events so callers can correlate failures to specific outbound payments. All new fields are Option and set to None on Claimable/Received events, so this is backwards-compatible for existing consumers that only match on the first three variants.
1 parent 207542d commit 1a29ef4

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ export interface PaymentEvent {
5353
amountMsat?: number
5454
reason?: string
5555
payerNote?: string
56+
/** Opaque payment identifier. Present for Sent and Failed (outbound) events. */
57+
paymentId?: string
58+
/** Payment preimage (proof of payment). Present for Sent events. */
59+
preimage?: string
5660
}
5761
export const enum PaymentEventType {
5862
Claimable = 0,
5963
Received = 1,
6064
Failed = 2,
65+
Sent = 3
6166
}
6267
export interface NodeChannel {
6368
channelId: string

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,18 @@ pub struct PaymentEvent {
307307
pub amount_msat: Option<i64>,
308308
pub reason: Option<String>,
309309
pub payer_note: Option<String>,
310+
/// Opaque payment identifier. Present for Sent and Failed (outbound) events.
311+
pub payment_id: Option<String>,
312+
/// Payment preimage (proof of payment). Present for Sent events.
313+
pub preimage: Option<String>,
310314
}
311315

312316
#[napi]
313317
pub enum PaymentEventType {
314318
Claimable,
315319
Received,
316320
Failed,
321+
Sent,
317322
}
318323

319324
#[napi(object)]
@@ -463,6 +468,8 @@ impl MdkNode {
463468
amount_msat: Some(*claimable_amount_msat as i64),
464469
reason: None,
465470
payer_note: None,
471+
payment_id: None,
472+
preimage: None,
466473
}),
467474
Event::PaymentReceived {
468475
payment_id,
@@ -490,9 +497,12 @@ impl MdkNode {
490497
amount_msat: Some(*amount_msat as i64),
491498
reason: None,
492499
payer_note,
500+
payment_id: None,
501+
preimage: None,
493502
})
494503
}
495504
Event::PaymentFailed {
505+
payment_id: event_pid,
496506
payment_hash,
497507
reason,
498508
..
@@ -502,6 +512,22 @@ impl MdkNode {
502512
amount_msat: None,
503513
reason: reason.map(|r| format!("{r:?}")),
504514
payer_note: None,
515+
payment_id: event_pid.map(|id| bytes_to_hex(&id.0)),
516+
preimage: None,
517+
}),
518+
Event::PaymentSuccessful {
519+
payment_id: event_pid,
520+
payment_hash,
521+
payment_preimage,
522+
..
523+
} => Some(PaymentEvent {
524+
event_type: PaymentEventType::Sent,
525+
payment_hash: bytes_to_hex(&payment_hash.0),
526+
amount_msat: None,
527+
reason: None,
528+
payer_note: None,
529+
payment_id: event_pid.map(|id| bytes_to_hex(&id.0)),
530+
preimage: payment_preimage.map(|p| bytes_to_hex(&p.0)),
505531
}),
506532
_ => None,
507533
};

0 commit comments

Comments
 (0)