Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit 21b374d

Browse files
committed
Swift channel manager claim funds
1 parent 829f5c8 commit 21b374d

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

ios/Ldk.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ @interface RCT_EXTERN_MODULE(Ldk, NSObject)
8282
reject:(RCTPromiseRejectBlock)reject)
8383
RCT_EXTERN_METHOD(processPendingHtlcForwards:(RCTPromiseResolveBlock)resolve
8484
reject:(RCTPromiseRejectBlock)reject)
85+
RCT_EXTERN_METHOD(claimFunds:(NSString *)paymentPreimage
86+
resolve:(RCTPromiseResolveBlock)resolve
87+
reject:(RCTPromiseRejectBlock)reject)
8588
@end
8689

8790
//MARK: Events

ios/Ldk.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum LdkErrors: String {
4949
case invoice_payment_fail = "invoice_payment_fail"
5050
case init_ldk_currency = "init_ldk_currency"
5151
case invoice_create_failed = "invoice_create_failed"
52+
case claim_funds_failed = "claim_funds_failed"
5253
}
5354

5455
enum LdkCallbackResponses: String {
@@ -68,6 +69,7 @@ enum LdkCallbackResponses: String {
6869
case tx_set_confirmed = "tx_set_confirmed"
6970
case tx_set_unconfirmed = "tx_set_unconfirmed"
7071
case process_pending_htlc_forwards_success = "process_pending_htlc_forwards_success"
72+
case claim_funds_success = "claim_funds_success"
7173
}
7274

7375
@objc(Ldk)
@@ -480,6 +482,20 @@ class Ldk: NSObject {
480482
handleResolve(resolve, .process_pending_htlc_forwards_success)
481483
}
482484

485+
@objc
486+
func claimFunds(_ paymentPreimage: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
487+
guard let channelManager = channelManager else {
488+
return handleReject(reject, .init_channel_manager)
489+
}
490+
491+
let res = channelManager.claim_funds(payment_preimage: String(paymentPreimage).hexaBytes)
492+
if res == false {
493+
handleReject(reject, .claim_funds_failed)
494+
}
495+
496+
handleResolve(resolve, .claim_funds_success)
497+
}
498+
483499
//MARK: Fetch methods
484500
@objc
485501
func version(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {

src/ldk.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,20 @@ class LDK {
323323
}
324324
}
325325

326+
/**
327+
* https://docs.rs/lightning/latest/lightning/ln/channelmanager/struct.ChannelManager.html#method.claim_funds
328+
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
329+
* @param paymentPreimage
330+
*/
331+
async claimFunds(paymentPreimage: string): Promise<Result<string>> {
332+
try {
333+
const res = await NativeLDK.claimFunds(paymentPreimage);
334+
return ok(res);
335+
} catch (e) {
336+
return err(e);
337+
}
338+
}
339+
326340
/**
327341
* Pays a bolt11 payment request
328342
* @param paymentRequest

src/lightning-manager.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,12 @@ class LightningManager {
335335
private onChannelManagerPaymentReceived(
336336
res: TChannelManagerPaymentReceived,
337337
): void {
338-
//TODO call channelManager.claim_funds(payment_preimage: paymentPreimage) if spontaneous_payment_preimage is not a blank string as
339-
//https://docs.rs/lightning/latest/lightning/util/events/enum.PaymentPurpose.html#variant.SpontaneousPayment
340-
//If not a spontaneous payment then nothing to do but notify user invoice was paid
341-
console.log(`onChannelManagerPaymentReceived: ${JSON.stringify(res)}`); //TODO
338+
if (res.spontaneous_payment_preimage) {
339+
//https://docs.rs/lightning/latest/lightning/util/events/enum.PaymentPurpose.html#variant.SpontaneousPayment
340+
ldk.claimFunds(res.spontaneous_payment_preimage).catch(console.error);
341+
} else {
342+
ldk.claimFunds(res.payment_preimage).catch(console.error);
343+
}
342344
}
343345

344346
private onChannelManagerPaymentSent(res: TChannelManagerPaymentSent): void {

0 commit comments

Comments
 (0)