Skip to content

Commit 2212575

Browse files
committed
feat: ios create pending channel
1 parent 885ed8a commit 2212575

File tree

8 files changed

+325
-220
lines changed

8 files changed

+325
-220
lines changed

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ PODS:
316316
- React-jsinspector (0.72.4)
317317
- React-logger (0.72.4):
318318
- glog
319-
- react-native-ldk (0.0.148):
319+
- react-native-ldk (0.0.150):
320320
- React
321321
- react-native-randombytes (3.6.1):
322322
- React-Core
@@ -621,7 +621,7 @@ SPEC CHECKSUMS:
621621
React-jsiexecutor: c7f826e40fa9cab5d37cab6130b1af237332b594
622622
React-jsinspector: aaed4cf551c4a1c98092436518c2d267b13a673f
623623
React-logger: da1ebe05ae06eb6db4b162202faeafac4b435e77
624-
react-native-ldk: fda4d4381d40401bdc5c3a9965937d19b232ed08
624+
react-native-ldk: 2b19de9eb94dcfd46f3f2a7191502292b75a5d7a
625625
react-native-randombytes: 421f1c7d48c0af8dbcd471b0324393ebf8fe7846
626626
react-native-tcp-socket: c1b7297619616b4c9caae6889bcb0aba78086989
627627
React-NativeModulesApple: edb5ace14f73f4969df6e7b1f3e41bef0012740f

lib/ios/Classes/LdkChannelManagerPersister.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LdkChannelManagerPersister: Persister, ExtendedChannelManagerPersister {
2525
guard let fundingGeneration = event.getValueAsFundingGenerationReady() else {
2626
return handleEventError(event)
2727
}
28-
28+
2929
LdkEventEmitter.shared.send(
3030
withEvent: .channel_manager_funding_generation_ready,
3131
body: [

lib/ios/Ldk.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ @interface RCT_EXTERN_MODULE(Ldk, NSObject)
8686
force:(BOOL *)force
8787
resolve:(RCTPromiseResolveBlock)resolve
8888
reject:(RCTPromiseRejectBlock)reject)
89+
RCT_EXTERN_METHOD(createChannel:(NSString *)counterPartyNodeId
90+
channelValueSats:(NSInteger *)channelValueSats
91+
pushSats:(NSInteger *)pushSats
92+
resolve:(RCTPromiseResolveBlock)resolve
93+
reject:(RCTPromiseRejectBlock)reject)
8994
RCT_EXTERN_METHOD(forceCloseAllChannels:(BOOL *)broadcastLatestTx
9095
resolve:(RCTPromiseResolveBlock)resolve
9196
reject:(RCTPromiseRejectBlock)reject)

lib/ios/Ldk.swift

Lines changed: 245 additions & 205 deletions
Large diffs are not rendered by default.

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@synonymdev/react-native-ldk",
33
"title": "React Native LDK",
4-
"version": "0.0.148",
4+
"version": "0.0.150",
55
"description": "React Native wrapper for LDK",
66
"main": "./dist/index.js",
77
"types": "./dist/index.d.ts",

lib/src/ldk.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
TInitKeysManager,
4040
TSpendRecoveredForceCloseOutputsReq,
4141
TChannelMonitor,
42+
TCreateChannelReq,
4243
} from './utils/types';
4344
import { extractPaymentRequest } from './utils/helpers';
4445

@@ -480,6 +481,25 @@ class LDK {
480481
}
481482
}
482483

484+
/**
485+
* Start create channel process and returns channel ID that can be used when funding the channel.
486+
* https://docs.rs/lightning/latest/lightning/ln/channelmanager/struct.ChannelManager.html#method.create_channel
487+
* @param counterPartyNodeId
488+
* @param channelValueSats
489+
* @param pushSats
490+
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
491+
*/
492+
async createChannel({counterPartyNodeId, channelValueSats, pushSats}: TCreateChannelReq): Promise<Result<string>> {
493+
try {
494+
const res = await NativeLDK.createChannel(counterPartyNodeId, channelValueSats, pushSats);
495+
this.writeDebugToLog('createChannel');
496+
return ok(res);
497+
} catch (e) {
498+
this.writeErrorToLog('createChannel', e);
499+
return err(e);
500+
}
501+
}
502+
483503
/**
484504
* Force close all channels
485505
* @param broadcastLatestTx

lib/src/lightning-manager.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
TLspLogPayload,
5959
TLspLogEvent,
6060
TChannelMonitor,
61+
TCreateChannelReq,
6162
} from './utils/types';
6263
import {
6364
appendPath,
@@ -170,10 +171,10 @@ class LightningManager {
170171
this.onBroadcastTransaction.bind(this),
171172
);
172173
//Channel manager handle events:
173-
ldk.onEvent(
174-
EEventTypes.channel_manager_funding_generation_ready,
175-
this.onChannelManagerFundingGenerationReady.bind(this),
176-
);
174+
// ldk.onEvent(
175+
// EEventTypes.channel_manager_funding_generation_ready,
176+
// this.onChannelManagerFundingGenerationReady.bind(this),
177+
// );
177178
ldk.onEvent(
178179
EEventTypes.channel_manager_payment_claimable,
179180
this.onChannelManagerPaymentClaimable.bind(this),
@@ -2050,13 +2051,13 @@ class LightningManager {
20502051
//LDK channel manager events
20512052
//All events and their values: https://docs.rs/lightning/latest/lightning/util/events/enum.Event.html
20522053
//Sample node for examples on how to handle events: https://github.com/lightningdevkit/ldk-sample/blob/c0a722430b8fbcb30310d64487a32aae839da3e8/src/main.rs#L600
2053-
private onChannelManagerFundingGenerationReady(
2054-
res: TChannelManagerFundingGenerationReady,
2055-
): void {
2056-
console.log(
2057-
`onChannelManagerFundingGenerationReady: ${JSON.stringify(res)}`,
2058-
); //TODO
2059-
}
2054+
// private onChannelManagerFundingGenerationReady(
2055+
// res: TChannelManagerFundingGenerationReady,
2056+
// ): void {
2057+
// console.log(
2058+
// `onChannelManagerFundingGenerationReady: ${JSON.stringify(res)}`,
2059+
// ); //TODO
2060+
// }
20602061

20612062
private async onChannelManagerPaymentClaimable(
20622063
res: TChannelManagerClaim,
@@ -2437,6 +2438,39 @@ class LightningManager {
24372438
remotePersist: true,
24382439
});
24392440
}
2441+
2442+
/**
2443+
* Creates a new channel with the provided peer and then listens for the channel manager funding generation ready event.
2444+
* Once event is triggered, those details can be used to create the funding transaction.
2445+
*/
2446+
async createChannel(req: TCreateChannelReq): Promise<Result<TChannelManagerFundingGenerationReady>> {
2447+
const res = await ldk.createChannel(req);
2448+
if (res.isErr()) {
2449+
return err(res.error);
2450+
}
2451+
2452+
return new Promise((resolve, reject) => {
2453+
// Channel funding ready event should be instant but if it fails and we don't get the event, we should reject.
2454+
const timeout = setTimeout(() => {
2455+
reject(err(new Error("Event not triggered within 5 seconds")));
2456+
}, 5000);
2457+
2458+
// Listen for the event for the channel funding details
2459+
ldk.onEvent(EEventTypes.channel_manager_funding_generation_ready, (eventRes: TChannelManagerFundingGenerationReady) => {
2460+
clearTimeout(timeout);
2461+
resolve(ok(eventRes));
2462+
});
2463+
2464+
ldk.onEvent(EEventTypes.channel_manager_channel_closed, (eventRes: TChannelManagerChannelClosed) => {
2465+
if (eventRes.channel_id === res.value) {
2466+
clearTimeout(timeout);
2467+
reject(err("Channel closed before funding"));
2468+
}
2469+
});
2470+
});
2471+
}
2472+
2473+
//TODO: fund channel
24402474
}
24412475

24422476
export default new LightningManager();

lib/src/utils/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ export type TCloseChannelReq = {
281281
force?: boolean;
282282
};
283283

284+
export type TCreateChannelReq = {
285+
counterPartyNodeId: string;
286+
channelValueSats: number;
287+
pushSats: number;
288+
};
289+
284290
export type TAcceptChannelReq = {
285291
temporaryChannelId: string;
286292
counterPartyNodeId: string;

0 commit comments

Comments
 (0)