Skip to content

Commit

Permalink
Merge pull request #261 from synonymdev/ldk-123
Browse files Browse the repository at this point in the history
LDK 123
  • Loading branch information
Jasonvdb authored Aug 22, 2024
2 parents b3718c7 + 36be24a commit 885ed8a
Show file tree
Hide file tree
Showing 38 changed files with 61,588 additions and 43,713 deletions.
Binary file modified lib/android/libs/LDK-release.aar
Binary file not shown.
Binary file modified lib/android/libs/ldk-java-javadoc.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions lib/android/src/main/java/com/reactnativeldk/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ val ChannelDetails.asJson: WritableMap
get() {
val result = Arguments.createMap()

result.putHexString("channel_id", _channel_id)
result.putHexString("channel_id", _channel_id._a)
result.putBoolean("is_public", _is_public)
result.putBoolean("is_usable", _is_usable)
result.putBoolean("is_channel_ready", _is_channel_ready)
Expand Down Expand Up @@ -205,7 +205,7 @@ fun ChannelMonitor.asJson(channelId: String): WritableMap {
val result = Arguments.createMap()
result.putString("channel_id", channelId)
result.putInt("funding_txo_index", _funding_txo._a._index.toInt())
result.putHexString("funding_txo_txid", _funding_txo._a.to_channel_id().reversedArray())
result.putHexString("funding_txo_txid", _funding_txo._a._txid.reversedArray())
result.putHexString("counterparty_node_id", _counterparty_node_id)

val balances = Arguments.createArray()
Expand Down
27 changes: 14 additions & 13 deletions lib/android/src/main/java/com/reactnativeldk/LdkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -622,14 +622,14 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod

LdkEventEmitter.send(EventTypes.native_log, "Checking for dropped peers")

val currentlyConnected = peerManager!!._peer_node_ids
val currentlyConnected = peerManager!!.list_peers().map { it._counterparty_node_id.hexEncodedString() }

addedPeers.forEach { peer ->
val pubKey = peer["pubKey"] as String
val address = peer["address"] as String
val port = peer["port"] as Double

if (currentlyConnected.map { it._a.hexEncodedString() }.contains(pubKey)) {
if (currentlyConnected.contains(pubKey)) {
return
}

Expand All @@ -654,8 +654,8 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
peerHandler ?: return handleReject(promise, LdkErrors.init_peer_handler)

//If peer is already connected don't add again
val currentList = peerManager!!._peer_node_ids
if (currentList.map { it._a.hexEncodedString() }.contains(pubKey)) {
val currentList = peerManager!!.list_peers().map { it._counterparty_node_id.hexEncodedString() }
if (currentList.contains(pubKey)) {
return handleResolve(promise, LdkCallbackResponses.peer_already_connected)
}

Expand Down Expand Up @@ -724,7 +724,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
fun acceptChannel(temporaryChannelId: String, counterPartyNodeId: String, trustedPeer0Conf: Boolean, promise: Promise) {
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)

val temporaryChannelId = temporaryChannelId.hexa()
val temporaryChannelId = ChannelId.of(temporaryChannelId.hexa())
val counterPartyNodeId = counterPartyNodeId.hexa()
val userChannelIdBytes = ByteArray(16)
Random().nextBytes(userChannelIdBytes)
Expand Down Expand Up @@ -755,7 +755,8 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
fun closeChannel(channelId: String, counterpartyNodeId: String, force: Boolean, promise: Promise) {
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)

val res = if (force) channelManager!!.force_close_broadcasting_latest_txn(channelId.hexa(), counterpartyNodeId.hexa()) else channelManager!!.close_channel(channelId.hexa(), counterpartyNodeId.hexa())
val channelIdObj = ChannelId.of(channelId.hexa())
val res = if (force) channelManager!!.force_close_broadcasting_latest_txn(channelIdObj, counterpartyNodeId.hexa()) else channelManager!!.close_channel(channelIdObj, counterpartyNodeId.hexa())
if (!res.is_ok) {
return handleReject(promise, LdkErrors.channel_close_fail)
}
Expand Down Expand Up @@ -981,9 +982,9 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
peerManager ?: return handleReject(promise, LdkErrors.init_peer_manager)

val res = Arguments.createArray()
val list = peerManager!!._peer_node_ids
val list = peerManager!!.list_peers()
list.iterator().forEach {
res.pushString(it._a.hexEncodedString())
res.pushString(it._counterparty_node_id.hexEncodedString())
}

promise.resolve(res)
Expand Down Expand Up @@ -1034,7 +1035,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
}

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

val channelFiles = File(channelStoragePath).listFiles()
Expand Down Expand Up @@ -1241,7 +1242,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
return handleReject(promise, LdkErrors.init_keys_manager)
}

val openChannelIds = channelManager!!.list_channels().map { it._channel_id.hexEncodedString() }
val openChannelIds = channelManager!!.list_channels().map { it._channel_id._a.hexEncodedString() }

//Get list of files in this path
val channelFiles = File(channelStoragePath).listFiles()
Expand Down Expand Up @@ -1271,7 +1272,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
}

val res = if (useInner) {
keysManager!!.inner.spend_spendable_outputs(
keysManager!!.inner.as_OutputSpender().spend_spendable_outputs(
descriptors,
emptyArray(),
changeDestinationScript.hexa(),
Expand Down Expand Up @@ -1354,10 +1355,10 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
logDump.add("RGS last sync time unavailable.")
}

peerManager?._peer_node_ids?.let { peers ->
peerManager?.list_peers()?.let { peers ->
if (peers.isNotEmpty()) {
peers.forEach { peer ->
logDump.add("Connected peer: ${peer._a.hexEncodedString()}")
logDump.add("Connected peer: ${peer._counterparty_node_id.hexEncodedString()}")
}
} else {
logDump.add("No connected peers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CustomKeysManager(

LdkEventEmitter.send(EventTypes.native_log, "Spending ${onlyNonStatic.count()} non static outputs")

return inner.spend_spendable_outputs(
return inner.as_OutputSpender().spend_spendable_outputs(
onlyNonStatic,
outputs,
changeDestinationScript,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {
override fun handle_event(event: Event) {
(event as? Event.FundingGenerationReady)?.let { fundingGenerationReady ->
val body = Arguments.createMap()
body.putHexString("temp_channel_id", fundingGenerationReady.temporary_channel_id)
body.putHexString("temp_channel_id", fundingGenerationReady.temporary_channel_id._a)
body.putHexString("output_script", fundingGenerationReady.output_script)
body.putString("user_channel_id", fundingGenerationReady.user_channel_id.leBytes.hexEncodedString())
body.putInt("value_satoshis", fundingGenerationReady.channel_value_satoshis.toInt())
Expand All @@ -29,7 +29,7 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {
val body = Arguments.createMap()
body.putHexString("payment_hash", paymentClaimable.payment_hash)
body.putInt("amount_sat", paymentClaimable.amount_msat.toInt() / 1000)
(paymentClaimable.purpose as? PaymentPurpose.InvoicePayment)?.let {
(paymentClaimable.purpose as? PaymentPurpose.Bolt11InvoicePayment)?.let {
body.putHexString("payment_preimage", (it.payment_preimage as Option_ThirtyTwoBytesZ.Some).some)
body.putHexString("payment_secret", it.payment_secret)
}
Expand Down Expand Up @@ -60,7 +60,7 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {
(event as? Event.OpenChannelRequest)?.let { openChannelRequest ->
//Use if we ever manually accept inbound channels. Setting in initConfig.
val body = Arguments.createMap()
body.putHexString("temp_channel_id", openChannelRequest.temporary_channel_id)
body.putHexString("temp_channel_id", openChannelRequest.temporary_channel_id._a)
body.putHexString("counterparty_node_id", openChannelRequest.counterparty_node_id)
body.putInt("push_sat", openChannelRequest.push_msat.toInt() / 1000)
body.putInt("funding_satoshis", openChannelRequest.funding_satoshis.toInt())
Expand Down Expand Up @@ -143,10 +143,9 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {
(event as? Event.ChannelClosed)?.let { channelClosed ->
val body = Arguments.createMap()
body.putString("user_channel_id", channelClosed.user_channel_id.leBytes.hexEncodedString())
body.putHexString("channel_id", channelClosed.channel_id)
body.putHexString("channel_id", channelClosed.channel_id._a)
val reasonString = when (channelClosed.reason) {
is ClosureReason.CommitmentTxConfirmed -> "CommitmentTxConfirmed"
is ClosureReason.CooperativeClosure -> "CooperativeClosure"
is ClosureReason.CounterpartyCoopClosedUnfundedChannel -> "CounterpartyCoopClosedUnfundedChannel"
is ClosureReason.CounterpartyForceClosed -> "CounterpartyForceClosed"
is ClosureReason.DisconnectedPeer -> "DisconnectedPeer"
Expand All @@ -155,6 +154,10 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {
is ClosureReason.HolderForceClosed -> "HolderForceClosed"
is ClosureReason.OutdatedChannelManager -> "OutdatedChannelManager"
is ClosureReason.ProcessingError -> "ProcessingError"
is ClosureReason.CounterpartyInitiatedCooperativeClosure -> "CounterpartyInitiatedCooperativeClosure"
is ClosureReason.LegacyCooperativeClosure -> "LegacyCooperativeClosure"
is ClosureReason.LocallyInitiatedCooperativeClosure -> "LocallyInitiatedCooperativeClosure"
is ClosureReason.HTLCsTimedOut -> "HTLCsTimedOut"
else -> "Unknown"
}
body.putString("reason", reasonString)
Expand All @@ -164,7 +167,7 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {

(event as? Event.DiscardFunding)?.let { discardFunding ->
val body = Arguments.createMap()
body.putHexString("channel_id", discardFunding.channel_id)
body.putHexString("channel_id", discardFunding.channel_id._a)
body.putHexString("tx", discardFunding.transaction)
return LdkEventEmitter.send(EventTypes.channel_manager_discard_funding, body)
}
Expand All @@ -173,7 +176,7 @@ class LdkChannelManagerPersister: ChannelManagerConstructor.EventHandler {
val body = Arguments.createMap()
body.putHexString("payment_hash", paymentClaimed.payment_hash)
body.putInt("amount_sat", paymentClaimed.amount_msat.toInt() / 1000)
(paymentClaimed.purpose as? PaymentPurpose.InvoicePayment)?.let {
(paymentClaimed.purpose as? PaymentPurpose.Bolt11InvoicePayment)?.let {
body.putHexString("payment_preimage", (it.payment_preimage as Option_ThirtyTwoBytesZ.Some).some)
body.putHexString("payment_secret", it.payment_secret)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ import org.ldk.structs.Persist.PersistInterface
import java.io.File

class LdkPersister {
fun handleChannel(id: OutPoint, data: ChannelMonitor, update_id: MonitorUpdateId): ChannelMonitorUpdateStatus {
fun handleChannel(channelFundingOutpoint: OutPoint, data: ChannelMonitor, update_id: MonitorUpdateId): ChannelMonitorUpdateStatus {
val channelId = data.channel_id()._a.hexEncodedString()
val body = Arguments.createMap()
body.putHexString("channel_id", id.to_channel_id())
body.putString("channel_id", channelId)
body.putHexString("counterparty_node_id", data._counterparty_node_id)

try {
if (LdkModule.channelStoragePath == "") {
throw Exception("Channel storage path not set")
}

val channelId = id.to_channel_id().hexEncodedString()

val file = File(LdkModule.channelStoragePath + "/" + channelId + ".bin")

val isNew = !file.exists()
Expand All @@ -34,24 +33,24 @@ class LdkPersister {

BackupClient.addToPersistQueue(BackupClient.Label.CHANNEL_MONITOR(channelId=channelId), data.write()) { error ->
if (error != null) {
LdkEventEmitter.send(EventTypes.native_log, "Failed to persist channel (${id.to_channel_id().hexEncodedString()}) to remote backup: $error")
LdkEventEmitter.send(EventTypes.native_log, "Failed to persist channel (${channelId}) to remote backup: $error")
return@addToPersistQueue
}

try {
file.writeBytes(data.write())
} catch (e: Exception) {
//If this fails we can't do much but LDK will retry on startup
LdkEventEmitter.send(EventTypes.native_log, "Failed to locally persist channel (${id.to_channel_id().hexEncodedString()}) to disk")
LdkEventEmitter.send(EventTypes.native_log, "Failed to locally persist channel (${channelId}) to disk")
return@addToPersistQueue
}

//Update chain monitor with successful persist
val res = LdkModule.chainMonitor?.channel_monitor_updated(id, update_id)
val res = LdkModule.chainMonitor?.channel_monitor_updated(channelFundingOutpoint, update_id)
if (res == null || !res.is_ok) {
LdkEventEmitter.send(EventTypes.native_log, "Failed to update chain monitor with persisted channel (${id.to_channel_id().hexEncodedString()})")
LdkEventEmitter.send(EventTypes.native_log, "Failed to update chain monitor with persisted channel (${channelId})")
} else {
LdkEventEmitter.send(EventTypes.native_log, "Persisted channel (${id.to_channel_id().hexEncodedString()}) to disk")
LdkEventEmitter.send(EventTypes.native_log, "Persisted channel (${channelId}) to disk")
if (isNew) {
LdkEventEmitter.send(EventTypes.new_channel, body)
}
Expand All @@ -65,12 +64,16 @@ class LdkPersister {
}

var persister = Persist.new_impl(object : PersistInterface {
override fun persist_new_channel(id: OutPoint, data: ChannelMonitor, update_id: MonitorUpdateId): ChannelMonitorUpdateStatus {
return handleChannel(id, data, update_id)
override fun persist_new_channel(channelFundingOutpoint: OutPoint, data: ChannelMonitor, update_id: MonitorUpdateId): ChannelMonitorUpdateStatus {
return handleChannel(channelFundingOutpoint, data, update_id)
}

override fun update_persisted_channel(channelFundingOutpoint: OutPoint, update: ChannelMonitorUpdate?, data: ChannelMonitor, update_id: MonitorUpdateId): ChannelMonitorUpdateStatus {
return handleChannel(channelFundingOutpoint, data, update_id)
}

override fun update_persisted_channel(id: OutPoint, update: ChannelMonitorUpdate?, data: ChannelMonitor, update_id: MonitorUpdateId): ChannelMonitorUpdateStatus {
return handleChannel(id, data, update_id)
override fun archive_persisted_channel(p0: OutPoint?) {
TODO("Not yet implemented")
}
})
}
2 changes: 1 addition & 1 deletion lib/ios/Classes/CustomKeysManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CustomKeysManager {

LdkEventEmitter.shared.send(withEvent: .native_log, body: "Spending \(onlyNonStatic.count) non static outputs")

let res = self.inner.spendSpendableOutputs(
let res = self.inner.asOutputSpender().spendSpendableOutputs(
descriptors: onlyNonStatic,
outputs: outputs,
changeDestinationScript: changeDestinationScript,
Expand Down
Loading

0 comments on commit 885ed8a

Please sign in to comment.