Skip to content

Commit a3e02ee

Browse files
committed
chore: code cleanup
1 parent af0eb40 commit a3e02ee

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ enum class LdkCallbackResponses {
123123
peer_already_connected,
124124
peer_currently_connecting,
125125
chain_sync_success,
126-
invoice_payment_success,
127126
tx_set_confirmed,
128127
tx_set_unconfirmed,
129128
process_pending_htlc_forwards_success,
@@ -427,7 +426,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
427426
var channelManagerSerialized: ByteArray? = null
428427
val channelManagerFile = File(accountStoragePath + "/" + LdkFileNames.ChannelManager.fileName)
429428
if (channelManagerFile.exists()) {
430-
channelManagerSerialized = channelManagerFile.readBytes()
429+
channelManagerSerialized = channelManagerFile.readBytes()
431430
}
432431

433432
//Scorer setup
@@ -558,11 +557,11 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
558557
LdkEventEmitter.send(EventTypes.channel_manager_restarted, "")
559558
LdkEventEmitter.send(EventTypes.native_log, "LDK restarted successfully")
560559
handleResolve(promise, LdkCallbackResponses.ldk_restart)
561-
},
560+
},
562561
{ reject ->
563562
LdkEventEmitter.send(EventTypes.native_log, "Error restarting LDK. Error: $reject")
564563
handleReject(promise, LdkErrors.unknown_error)
565-
})
564+
})
566565

567566
initChannelManager(
568567
currentNetwork,
@@ -687,7 +686,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
687686
if (currentlyConnectingPeers.contains(pubKey)) {
688687
return handleResolve(promise, LdkCallbackResponses.peer_currently_connecting)
689688
}
690-
689+
691690
try {
692691
currentlyConnectingPeers.add(pubKey)
693692
peerHandler!!.connect(pubKey.hexa(), InetSocketAddress(address, port.toInt()), timeout.toInt())
@@ -927,24 +926,32 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
927926

928927
@ReactMethod
929928
fun pay(paymentRequest: String, amountSats: Double, timeoutSeconds: Double, promise: Promise) {
930-
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)
929+
val (paymentId, error) = handlePayment(paymentRequest, amountSats, timeoutSeconds)
930+
if (error != null) {
931+
return handleReject(promise, error)
932+
}
933+
return promise.resolve(paymentId)
934+
}
935+
936+
private fun handlePayment(paymentRequest: String, amountSats: Double, timeoutSeconds: Double): Pair<String?, LdkErrors?> {
937+
channelManager ?: return Pair(null, LdkErrors.init_channel_manager)
931938

932939
val invoiceParse = Bolt11Invoice.from_str(paymentRequest)
933940
if (!invoiceParse.is_ok) {
934-
return handleReject(promise, LdkErrors.decode_invoice_fail)
941+
return Pair(null, LdkErrors.decode_invoice_fail)
935942
}
936943
val invoice = (invoiceParse as Result_Bolt11InvoiceParseOrSemanticErrorZ_OK).res
937944

938945
val isZeroValueInvoice = invoice.amount_milli_satoshis() is Option_u64Z.None
939946

940947
//If it's a zero invoice and we don't have an amount then don't proceed
941948
if (isZeroValueInvoice && amountSats == 0.0) {
942-
return handleReject(promise, LdkErrors.invoice_payment_fail_must_specify_amount)
949+
return Pair(null, LdkErrors.invoice_payment_fail_must_specify_amount)
943950
}
944951

945952
//Amount was set but not allowed to set own amount
946953
if (amountSats > 0 && !isZeroValueInvoice) {
947-
return handleReject(promise, LdkErrors.invoice_payment_fail_must_not_specify_amount)
954+
return Pair(null, LdkErrors.invoice_payment_fail_must_not_specify_amount)
948955
}
949956

950957
val paymentId = invoice.payment_hash()
@@ -953,7 +960,7 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
953960
UtilMethods.payment_parameters_from_invoice(invoice)
954961

955962
if (!detailsRes.is_ok) {
956-
return handleReject(promise, LdkErrors.invoice_payment_fail_invoice)
963+
return Pair(null, LdkErrors.invoice_payment_fail_invoice)
957964
}
958965

959966
val sendDetails = detailsRes as Result_C3Tuple_ThirtyTwoBytesRecipientOnionFieldsRouteParametersZNoneZ_OK
@@ -974,26 +981,23 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
974981
"state" to "pending"
975982
))
976983

977-
return handleResolve(promise, LdkCallbackResponses.invoice_payment_success)
984+
return Pair(paymentId.hexEncodedString(), null)
978985
}
979986

980987
val error = res as? Result_NoneRetryableSendFailureZ_Err
981-
?: return handleReject(promise, LdkErrors.invoice_payment_fail_unknown)
988+
?: return Pair(null, LdkErrors.invoice_payment_fail_unknown)
982989

983-
when (error.err) {
990+
return when (error.err) {
984991
RetryableSendFailure.LDKRetryableSendFailure_DuplicatePayment -> {
985-
handleReject(promise, LdkErrors.invoice_payment_fail_duplicate_payment)
992+
Pair(null, LdkErrors.invoice_payment_fail_duplicate_payment)
986993
}
987-
988994
RetryableSendFailure.LDKRetryableSendFailure_PaymentExpired -> {
989-
handleReject(promise, LdkErrors.invoice_payment_fail_payment_expired)
995+
Pair(null, LdkErrors.invoice_payment_fail_payment_expired)
990996
}
991-
992997
RetryableSendFailure.LDKRetryableSendFailure_RouteNotFound -> {
993-
handleReject(promise, LdkErrors.invoice_payment_fail_route_not_found)
998+
Pair(null, LdkErrors.invoice_payment_fail_route_not_found)
994999
}
995-
996-
else -> handleReject(promise, LdkErrors.invoice_payment_fail_unknown)
1000+
else -> Pair(null, LdkErrors.invoice_payment_fail_unknown)
9971001
}
9981002
}
9991003

@@ -1409,10 +1413,10 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
14091413

14101414
keysManager?.inner?.as_NodeSigner()
14111415
?.get_node_id(Recipient.LDKRecipient_Node)?.let { pubKeyRes ->
1412-
if (pubKeyRes.is_ok) {
1413-
logDump.add("NodeID: ${(pubKeyRes as Result_PublicKeyNoneZ_OK).res.hexEncodedString()}")
1416+
if (pubKeyRes.is_ok) {
1417+
logDump.add("NodeID: ${(pubKeyRes as Result_PublicKeyNoneZ_OK).res.hexEncodedString()}")
1418+
}
14141419
}
1415-
}
14161420

14171421
channelManager?.list_channels()?.forEach { channel ->
14181422
logDump.add("Open channel:")

lib/ios/Ldk.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ enum LdkCallbackResponses: String {
9494
case peer_already_connected
9595
case peer_currently_connecting
9696
case chain_sync_success
97-
case invoice_payment_success
9897
case tx_set_confirmed
9998
case tx_set_unconfirmed
10099
case process_pending_htlc_forwards_success
@@ -1128,7 +1127,7 @@ class Ldk: NSObject {
11281127
return handleReject(reject, orginalError)
11291128
}
11301129
}
1131-
1130+
11321131
@objc
11331132
func pay(_ paymentRequest: NSString, amountSats: NSInteger, timeoutSeconds: NSInteger, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
11341133
let (paymentId, error) = handlePayment(paymentRequest: paymentRequest, amountSats: amountSats, timeoutSeconds: timeoutSeconds)
@@ -1201,10 +1200,6 @@ class Ldk: NSObject {
12011200
case .PaymentExpired:
12021201
return (nil, .invoice_payment_fail_payment_expired)
12031202
case .RouteNotFound:
1204-
//Delete scorer
1205-
//Delete graph
1206-
//Download and update graph again
1207-
//Retry payment
12081203
return (nil, .invoice_payment_fail_route_not_found)
12091204
@unknown default:
12101205
return (nil, .invoice_payment_fail_unknown)

0 commit comments

Comments
 (0)