Skip to content

Commit 31e22d3

Browse files
committed
feat: android open and fund channels
1 parent d1ee703 commit 31e22d3

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

example/utils/helpers.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,4 @@ export const ldkNetwork = (network: TAvailableNetworks): ENetworks => {
239239
case 'bitcoinSignet':
240240
return ENetworks.signet;
241241
}
242-
};
243-
244-
245-
export const scriptToAddress = (outputScript: string): string => {
246-
if (!outputScript.startsWith('0020')) {
247-
throw new Error('Invalid output script, currently example app only supports P2WSH');
248-
}
249-
250-
outputScript = outputScript.slice(4);
251-
252-
const scriptPubKey = Buffer.from(outputScript, 'hex');
253-
const network = getNetwork(selectedNetwork);
254-
const address = bitcoin.payments.p2wsh({ hash: scriptPubKey, network }).address;
255-
256-
return address ?? "";
257-
}
242+
};

lib/android/src/main/java/com/reactnativeldk/LdkModule.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ enum class LdkErrors {
8686
invoice_create_failed,
8787
init_scorer_failed,
8888
channel_close_fail,
89+
start_create_channel_fail,
90+
fund_channel_fail,
8991
channel_accept_fail,
9092
spend_outputs_fail,
9193
failed_signing_request,
@@ -126,6 +128,8 @@ enum class LdkCallbackResponses {
126128
ldk_restart,
127129
accept_channel_success,
128130
close_channel_success,
131+
start_create_channel_fail,
132+
fund_channel_success,
129133
file_write_success,
130134
backup_client_setup_success,
131135
backup_restore_success,
@@ -764,6 +768,54 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
764768
handleResolve(promise, LdkCallbackResponses.close_channel_success)
765769
}
766770

771+
@ReactMethod
772+
fun createChannel(counterPartyNodeId: String, channelValueSats: Double, pushSats: Double, promise: Promise) {
773+
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)
774+
keysManager ?: return handleReject(promise, LdkErrors.init_keys_manager)
775+
776+
val theirNetworkKey = counterPartyNodeId.hexa()
777+
val channelValueSatoshis = channelValueSats.toLong()
778+
val pushMsat = pushSats.toLong() * 1000
779+
val userChannelIdBytes = ByteArray(16)
780+
Random().nextBytes(userChannelIdBytes)
781+
val userChannelId = UInt128(userChannelIdBytes)
782+
783+
val tempChannelId = ChannelId.temporary_from_entropy_source(keysManager!!.inner.as_EntropySource())
784+
785+
val res = channelManager!!.create_channel(
786+
theirNetworkKey,
787+
channelValueSatoshis,
788+
pushMsat,
789+
userChannelId,
790+
tempChannelId,
791+
UserConfig.with_default()
792+
)
793+
794+
if (!res.is_ok) {
795+
return handleReject(promise, LdkErrors.start_create_channel_fail)
796+
}
797+
798+
handleResolve(promise, LdkCallbackResponses.start_create_channel_fail)
799+
}
800+
801+
@ReactMethod
802+
fun fundChannel(temporaryChannelId: String, counterPartyNodeId: String, fundingTransaction: String, promise: Promise) {
803+
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)
804+
805+
val res = channelManager!!.funding_transaction_generated(
806+
ChannelId.of(temporaryChannelId.hexa()),
807+
counterPartyNodeId.hexa(),
808+
fundingTransaction.hexa()
809+
)
810+
811+
if (res.is_ok) {
812+
handleResolve(promise, LdkCallbackResponses.fund_channel_success)
813+
return
814+
}
815+
816+
handleReject(promise, LdkErrors.fund_channel_fail)
817+
}
818+
767819
@ReactMethod
768820
fun forceCloseAllChannels(broadcastLatestTx: Boolean, promise: Promise) {
769821
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)

lib/ios/Ldk.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,16 +917,19 @@ class Ldk: NSObject {
917917
let channelValueSatoshis = UInt64(channelValueSats)
918918
let pushMsat = UInt64(pushSats) * 1000
919919

920-
guard let channelId = Bindings.ChannelId.initWithTemporaryFromEntropySource(entropySource: keysManager.inner.asEntropySource()).getA() else {
921-
return handleReject(reject, .start_create_channel_fail)
920+
var userChannelId = Data(count: 32)
921+
userChannelId.withUnsafeMutableBytes { mutableBytes in
922+
arc4random_buf(mutableBytes.baseAddress, 32)
922923
}
923924

925+
let temporaryChannelId = Bindings.ChannelId.initWithTemporaryFromEntropySource(entropySource: keysManager.inner.asEntropySource())
926+
924927
let res = channelManager.createChannel(
925928
theirNetworkKey: theirNetworkKey,
926929
channelValueSatoshis: channelValueSatoshis,
927930
pushMsat: pushMsat,
928-
userChannelId: channelId,
929-
temporaryChannelId: .initWithTemporaryFromEntropySource(entropySource: keysManager.inner.asEntropySource()),
931+
userChannelId: [UInt8](userChannelId),
932+
temporaryChannelId: temporaryChannelId,
930933
overrideConfig: .initWithDefault()
931934
)
932935

0 commit comments

Comments
 (0)