Skip to content

Commit 37b1a65

Browse files
committed
Kotlin list all/usable channels
1 parent 34846cd commit 37b1a65

File tree

4 files changed

+110
-70
lines changed

4 files changed

+110
-70
lines changed

android/src/main/java/com/reactnativeldk/Helpers.kt

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package com.reactnativeldk
22
import com.facebook.react.bridge.Arguments
33
import com.facebook.react.bridge.Promise
44
import com.facebook.react.bridge.WritableMap
5-
import org.ldk.structs.Invoice
6-
import org.ldk.structs.Option_u64Z
5+
import org.ldk.structs.*
76

87
fun handleResolve(promise: Promise, res: LdkCallbackResponses) {
98
LdkEventEmitter.send(EventTypes.swift_log, "Success: ${res}")
@@ -30,26 +29,59 @@ fun String.hexa(): ByteArray {
3029
.map { it.toInt(16).toByte() }
3130
.toByteArray()
3231
}
33-
fun Invoice.json(): WritableMap {
34-
val result = Arguments.createMap()
35-
val signedInv = this.into_signed_raw()
36-
val rawInvoice = signedInv.raw_invoice()
37-
38-
result.putDouble("amount_milli_satoshis", (this.amount_milli_satoshis() as Option_u64Z.Some).some.toDouble())
39-
result.putString("description", rawInvoice.description()?.into_inner())
40-
result.putBoolean("check_signature", signedInv.check_signature())
41-
result.putBoolean("is_expired", this.is_expired)
42-
result.putInt("duration_since_epoch", this.duration_since_epoch().toInt())
43-
result.putInt("expiry_time", this.expiry_time().toInt())
44-
result.putInt("min_final_cltv_expiry", this.min_final_cltv_expiry().toInt())
45-
result.putString("payee_pub_key", rawInvoice.payee_pub_key()?._a?.hexEncodedString())
46-
result.putString("recover_payee_pub_key", this.recover_payee_pub_key().hexEncodedString())
47-
result.putString("payment_hash", this.payment_hash().hexEncodedString())
48-
result.putString("payment_secret", this.payment_secret().hexEncodedString())
49-
result.putInt("timestamp", this.timestamp().toInt())
50-
result.putString("features", this.features()?.write()?.hexEncodedString())
51-
result.putInt("currency", this.currency().ordinal)
52-
result.putString("to_str", signedInv.to_str())
53-
54-
return result
55-
}
32+
33+
val Invoice.asJson: WritableMap
34+
get() {
35+
val result = Arguments.createMap()
36+
val signedInv = this.into_signed_raw()
37+
val rawInvoice = signedInv.raw_invoice()
38+
39+
result.putDouble("amount_milli_satoshis", (this.amount_milli_satoshis() as Option_u64Z.Some).some.toDouble())
40+
result.putString("description", rawInvoice.description()?.into_inner())
41+
result.putBoolean("check_signature", signedInv.check_signature())
42+
result.putBoolean("is_expired", this.is_expired)
43+
result.putInt("duration_since_epoch", this.duration_since_epoch().toInt())
44+
result.putInt("expiry_time", this.expiry_time().toInt())
45+
result.putInt("min_final_cltv_expiry", this.min_final_cltv_expiry().toInt())
46+
result.putString("payee_pub_key", rawInvoice.payee_pub_key()?._a?.hexEncodedString())
47+
result.putString("recover_payee_pub_key", this.recover_payee_pub_key().hexEncodedString())
48+
result.putString("payment_hash", this.payment_hash().hexEncodedString())
49+
result.putString("payment_secret", this.payment_secret().hexEncodedString())
50+
result.putInt("timestamp", this.timestamp().toInt())
51+
result.putString("features", this.features()?.write()?.hexEncodedString())
52+
result.putInt("currency", this.currency().ordinal)
53+
result.putString("to_str", signedInv.to_str())
54+
55+
return result
56+
}
57+
58+
val ChannelDetails.asJson: WritableMap
59+
get() {
60+
val result = Arguments.createMap()
61+
62+
result.putString("channel_id", this._channel_id.hexEncodedString())
63+
result.putBoolean("is_public", this._is_public)
64+
result.putBoolean("is_usable", this._is_usable)
65+
result.putBoolean("is_outbound", this._is_outbound)
66+
result.putInt("balance_msat", this._balance_msat.toInt())
67+
result.putString("counterparty", this._counterparty.write().hexEncodedString())
68+
result.putString("funding_txo", if (this._funding_txo != null) this._funding_txo!!.write().hexEncodedString() else null)
69+
result.putString("channel_type", if (this._channel_type != null) this._channel_type!!.write().hexEncodedString() else null)
70+
result.putInt("user_channel_id", this._user_channel_id.toInt())
71+
result.putInt("get_confirmations_required", (this._confirmations_required as Option_u32Z.Some).some)
72+
(this._short_channel_id as? Option_u64Z.Some)?.some?.toInt()
73+
?.let { result.putInt("short_channel_id", it) } //Optional number
74+
result.putBoolean("is_funding_locked", this._is_funding_locked)
75+
(this._inbound_scid_alias as? Option_u64Z.Some)?.some?.toInt()
76+
?.let { result.putInt("inbound_scid_alias", it) }
77+
(this._inbound_scid_alias as? Option_u64Z.Some)?.some?.toInt()
78+
?.let { result.putInt("inbound_payment_scid", it) }
79+
result.putInt("inbound_capacity_msat", this._inbound_capacity_msat.toInt())
80+
result.putInt("outbound_capacity_msat", this._outbound_capacity_msat.toInt())
81+
result.putInt("channel_value_satoshis", this._channel_value_satoshis.toInt())
82+
(this._force_close_spend_delay as? Option_u16Z.Some)?.some?.toInt()
83+
?.let { result.putInt("force_close_spend_delay", it) }
84+
result.putInt("unspendable_punishment_reserve", (this._unspendable_punishment_reserve as Option_u64Z.Some).some.toInt())
85+
86+
return result
87+
}

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
346346

347347
val parsedInvoice = parsed as Result_InvoiceParseOrSemanticErrorZ_OK
348348

349-
promise.resolve(parsedInvoice.res.json())
349+
promise.resolve(parsedInvoice.res.asJson)
350350
}
351351

352352
@ReactMethod
@@ -385,7 +385,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
385385
);
386386

387387
if (res.is_ok) {
388-
return promise.resolve((res as Result_InvoiceSignOrCreationErrorZ_OK).res.json())
388+
return promise.resolve((res as Result_InvoiceSignOrCreationErrorZ_OK).res.asJson)
389389
}
390390

391391
val error = res as Result_InvoiceSignOrCreationErrorZ
@@ -424,14 +424,22 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
424424

425425
@ReactMethod
426426
fun listChannels(promise: Promise) {
427-
//TODO
428-
promise.resolve("[]")
427+
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)
428+
429+
val list = Arguments.createArray()
430+
channelManager!!.list_channels().iterator().forEach { list.pushMap(it.asJson) }
431+
432+
promise.resolve(list)
429433
}
430434

431435
@ReactMethod
432436
fun listUsableChannels(promise: Promise) {
433-
//TODO
434-
promise.resolve("[]")
437+
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)
438+
439+
val list = Arguments.createArray()
440+
channelManager!!.list_usable_channels().iterator().forEach { list.pushMap(it.asJson) }
441+
442+
promise.resolve(list)
435443
}
436444
}
437445

ios/Helpers.swift

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,47 @@ func handleReject(_ reject: RCTPromiseRejectBlock, _ ldkError: LdkErrors, _ erro
2727
extension Invoice {
2828
var asJson: Any {
2929
return [
30-
"amount_milli_satoshis": self.amount_milli_satoshis().getValue() as Any,
31-
"description": self.into_signed_raw().raw_invoice().description(),
32-
"check_signature": self.check_signature().isOk(),
33-
"is_expired": self.is_expired(),
34-
"duration_since_epoch": self.duration_since_epoch(),
35-
"expiry_time": self.expiry_time(),
36-
"min_final_cltv_expiry": self.min_final_cltv_expiry(),
37-
"payee_pub_key": Data(self.payee_pub_key()).hexEncodedString(),
38-
"recover_payee_pub_key": Data(self.recover_payee_pub_key()).hexEncodedString(),
39-
"payment_hash": Data(self.payment_hash()).hexEncodedString(),
40-
"payment_secret": Data(self.payment_secret()).hexEncodedString(),
41-
"timestamp": self.timestamp(),
42-
"features": Data(self.features().write()).hexEncodedString(),
43-
"currency": self.currency().rawValue,
44-
"to_str": self.to_str()
30+
"amount_milli_satoshis": amount_milli_satoshis().getValue() as Any,
31+
"description": into_signed_raw().raw_invoice().description(),
32+
"check_signature": check_signature().isOk(),
33+
"is_expired": is_expired(),
34+
"duration_since_epoch": duration_since_epoch(),
35+
"expiry_time": expiry_time(),
36+
"min_final_cltv_expiry": min_final_cltv_expiry(),
37+
"payee_pub_key": Data(payee_pub_key()).hexEncodedString(),
38+
"recover_payee_pub_key": Data(recover_payee_pub_key()).hexEncodedString(),
39+
"payment_hash": Data(payment_hash()).hexEncodedString(),
40+
"payment_secret": Data(payment_secret()).hexEncodedString(),
41+
"timestamp": timestamp(),
42+
"features": Data(features().write()).hexEncodedString(),
43+
"currency": currency().rawValue,
44+
"to_str": to_str()
4545
]
4646
}
4747
}
4848

4949
extension ChannelDetails {
5050
var asJson: Any {
5151
return [
52-
"channel_id": Data(self.get_channel_id()).hexEncodedString(),
53-
"is_public": self.get_is_public(),
54-
"is_usable": self.get_is_usable(),
55-
"is_outbound": self.get_is_outbound(),
56-
"balance_msat": self.get_balance_msat(),
57-
"counterparty": Data(self.get_counterparty().write()).hexEncodedString(),
58-
"funding_txo": Data(self.get_funding_txo()?.write() ?? []).hexEncodedString(),
59-
"channel_type": Data(self.get_channel_type().write()).hexEncodedString(),
60-
"user_channel_id": self.get_user_channel_id(), //Number
61-
"confirmations_required": self.get_confirmations_required().getValue() as Any, // Optional number
62-
"short_channel_id": self.get_short_channel_id().getValue() as Any, //Optional number
63-
"is_funding_locked": self.get_is_funding_locked(), //Bool
64-
"inbound_scid_alias": self.get_inbound_scid_alias().getValue() as Any, //Optional number
65-
"get_inbound_payment_scid": self.get_inbound_payment_scid().getValue() as Any, //Optional number,
66-
"inbound_capacity_msat": self.get_inbound_capacity_msat(),
67-
"channel_value_satoshis": self.get_channel_value_satoshis(),
68-
"outbound_capacity_msat": self.get_outbound_capacity_msat(),
69-
"force_close_spend_delay": self.get_force_close_spend_delay().getValue() as Any, //Optional number
70-
"unspendable_punishment_reserve": self.get_unspendable_punishment_reserve().getValue() as Any //Optional number
52+
"channel_id": Data(get_channel_id()).hexEncodedString(),
53+
"is_public": get_is_public(),
54+
"is_usable": get_is_usable(),
55+
"is_outbound": get_is_outbound(),
56+
"balance_msat": get_balance_msat(),
57+
"counterparty": Data(get_counterparty().write()).hexEncodedString(),
58+
"funding_txo": Data(get_funding_txo()?.write() ?? []).hexEncodedString(),
59+
"channel_type": Data(get_channel_type().write()).hexEncodedString(),
60+
"user_channel_id": get_user_channel_id(), //Number
61+
"confirmations_required": get_confirmations_required().getValue() as Any, // Optional number
62+
"short_channel_id": get_short_channel_id().getValue() as Any, //Optional number
63+
"is_funding_locked": get_is_funding_locked(), //Bool
64+
"inbound_scid_alias": get_inbound_scid_alias().getValue() as Any, //Optional number
65+
"inbound_payment_scid": get_inbound_payment_scid().getValue() as Any, //Optional number,
66+
"inbound_capacity_msat": get_inbound_capacity_msat(),
67+
"outbound_capacity_msat": get_outbound_capacity_msat(),
68+
"channel_value_satoshis": get_channel_value_satoshis(),
69+
"force_close_spend_delay": get_force_close_spend_delay().getValue() as Any, //Optional number
70+
"unspendable_punishment_reserve": get_unspendable_punishment_reserve().getValue() as Any //Optional number
7171
]
7272
}
7373
}

src/utils/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,24 @@ export type TChannel = {
113113
is_outbound: boolean;
114114
balance_msat: number;
115115
counterparty: string;
116-
funding_txo: string;
117-
channel_type: string;
116+
funding_txo?: string;
117+
channel_type?: string;
118118
user_channel_id: number;
119119
confirmations_required?: number;
120120
short_channel_id?: number;
121121
is_funding_locked: boolean;
122122
inbound_scid_alias?: number;
123-
get_inbound_payment_scid?: number;
123+
inbound_payment_scid?: number;
124124
inbound_capacity_msat: number;
125-
channel_value_satoshis: number;
126125
outbound_capacity_msat: number;
126+
channel_value_satoshis: number;
127127
force_close_spend_delay?: number;
128128
unspendable_punishment_reserve?: number;
129129
};
130130

131131
export type TInvoice = {
132132
amount_milli_satoshis?: number;
133-
description: string,
133+
description?: string,
134134
check_signature: boolean;
135135
is_expired: boolean;
136136
duration_since_epoch: number;

0 commit comments

Comments
 (0)