Skip to content

Commit

Permalink
feat: android list all channel monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonvdb committed Mar 22, 2024
1 parent c4ac2c1 commit 0332c9b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 14 deletions.
43 changes: 31 additions & 12 deletions lib/android/src/main/java/com/reactnativeldk/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ val RouteHop.asJson: WritableMap
return hop
}


fun ChannelMonitor.asJson(channelId: String): WritableMap {
val result = Arguments.createMap()
result.putString("channel_id", channelId)
result.putHexString("funding_txo", _funding_txo._b)
result.putHexString("counterparty_node_id", _counterparty_node_id)

val balances = Arguments.createArray()
_claimable_balances.iterator().forEach { balance ->
balances.pushMap(balance.asJson)
}
result.putArray("claimable_balances", balances)
return result
}

fun WritableMap.putHexString(key: String, bytes: ByteArray?) {
if (bytes != null) {
putString(key, bytes.hexEncodedString())
Expand Down Expand Up @@ -437,56 +452,60 @@ fun UserConfig.mergeWithMap(map: ReadableMap): UserConfig {
return this
}

fun ChainMonitor.getClaimableBalancesAsJson(ignoredChannels: Array<ChannelDetails>): WritableArray {
val result = Arguments.createArray()

get_claimable_balances(ignoredChannels).iterator().forEach { balance ->
val Balance.asJson: WritableMap
get() {
val map = Arguments.createMap()
//Defaults if all castings for balance fail
map.putInt("amount_satoshis", 0)
map.putString("type", "Unknown")

(balance as? Balance.ClaimableAwaitingConfirmations)?.let { claimableAwaitingConfirmations ->
(this as? Balance.ClaimableAwaitingConfirmations)?.let { claimableAwaitingConfirmations ->
map.putInt("amount_satoshis", claimableAwaitingConfirmations.amount_satoshis.toInt())
map.putInt("confirmation_height", claimableAwaitingConfirmations.confirmation_height)
map.putString("type", "ClaimableAwaitingConfirmations")
}

(balance as? Balance.ClaimableOnChannelClose)?.let { claimableOnChannelClose ->
(this as? Balance.ClaimableOnChannelClose)?.let { claimableOnChannelClose ->
map.putInt("amount_satoshis", claimableOnChannelClose.amount_satoshis.toInt())
map.putString("type", "ClaimableOnChannelClose")
}

(balance as? Balance.ContentiousClaimable)?.let { contentiousClaimable ->
(this as? Balance.ContentiousClaimable)?.let { contentiousClaimable ->
map.putInt("amount_satoshis", contentiousClaimable.amount_satoshis.toInt())
map.putInt("timeout_height", contentiousClaimable.timeout_height)
map.putString("type", "ContentiousClaimable")
}

(balance as? Balance.CounterpartyRevokedOutputClaimable)?.let { counterpartyRevokedOutputClaimable ->
(this as? Balance.CounterpartyRevokedOutputClaimable)?.let { counterpartyRevokedOutputClaimable ->
map.putInt("amount_satoshis", counterpartyRevokedOutputClaimable.amount_satoshis.toInt())
map.putString("type", "CounterpartyRevokedOutputClaimable")
}

(balance as? Balance.MaybePreimageClaimableHTLC)?.let { maybePreimageClaimableHTLC ->
(this as? Balance.MaybePreimageClaimableHTLC)?.let { maybePreimageClaimableHTLC ->
map.putInt("amount_satoshis", maybePreimageClaimableHTLC.amount_satoshis.toInt())
map.putInt("expiry_height", maybePreimageClaimableHTLC.expiry_height)
map.putString("type", "MaybePreimageClaimableHTLC")
}

(balance as? Balance.MaybeTimeoutClaimableHTLC)?.let { maybeTimeoutClaimableHTLC ->
(this as? Balance.MaybeTimeoutClaimableHTLC)?.let { maybeTimeoutClaimableHTLC ->
map.putInt("amount_satoshis", maybeTimeoutClaimableHTLC.amount_satoshis.toInt())
map.putInt("claimable_height", maybeTimeoutClaimableHTLC.claimable_height)
map.putString("type", "MaybeTimeoutClaimableHTLC")
}

result.pushMap(map)
return map
}

fun ChainMonitor.getClaimableBalancesAsJson(ignoredChannels: Array<ChannelDetails>): WritableArray {
val result = Arguments.createArray()

get_claimable_balances(ignoredChannels).iterator().forEach { balance ->
result.pushMap(balance.asJson)
}

return result
}


/// Helper for returning real network and currency as a tuple from a string
fun getNetwork(chain: String): Pair<Network, Currency> {
return when (chain) {
Expand Down
36 changes: 34 additions & 2 deletions lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,40 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
promise.resolve(list)
}

@ReactMethod
fun listChannelMonitors(ignoreOpenChannels: Boolean, promise: Promise) {
val channelManager = channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)
val keysManager = keysManager ?: return handleReject(promise, LdkErrors.init_keys_manager)
if (channelStoragePath == "") {
return handleReject(promise, LdkErrors.init_storage_path)
}

val ignoredChannels = if (ignoreOpenChannels)
channelManager.list_channels().map { it._channel_id.hexEncodedString() }.toTypedArray() else
arrayOf()

val channelFiles = File(channelStoragePath).listFiles()

val result = Arguments.createArray()
for (channelFile in channelFiles) {
val channelId = channelFile.nameWithoutExtension

//Ignore open channels
if (ignoredChannels.contains(channelId)) {
continue
}

val channelMonitor = UtilMethods.C2Tuple_ThirtyTwoBytesChannelMonitorZ_read(channelFile.readBytes(), keysManager!!.inner.as_EntropySource(), SignerProvider.new_impl(keysManager!!.signerProvider))

if (channelMonitor.is_ok) {
val channelMonitorResult = (channelMonitor as Result_C2Tuple_ThirtyTwoBytesChannelMonitorZDecodeErrorZ_OK)
result.pushMap(channelMonitorResult.res._b.asJson(channelMonitorResult.res._a.hexEncodedString()))
}
}

promise.resolve(result)
}

@ReactMethod
fun networkGraphListNodeIds(promise: Promise) {
val graph = networkGraph?.read_only() ?: return handleReject(promise, LdkErrors.init_network_graph)
Expand Down Expand Up @@ -1007,8 +1041,6 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
channelManager.list_channels() else
arrayOf<ChannelDetails>()



promise.resolve(chainMonitor.getClaimableBalancesAsJson(ignoredChannels))
}

Expand Down

0 comments on commit 0332c9b

Please sign in to comment.