Skip to content

Commit c130dc7

Browse files
committed
Kotlin and Swift more detailed payment failure messages
Code cleanup
1 parent 37b1a65 commit c130dc7

File tree

4 files changed

+101
-49
lines changed

4 files changed

+101
-49
lines changed

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ fun String.hexa(): ByteArray {
3333
val Invoice.asJson: WritableMap
3434
get() {
3535
val result = Arguments.createMap()
36-
val signedInv = this.into_signed_raw()
36+
val signedInv = into_signed_raw()
3737
val rawInvoice = signedInv.raw_invoice()
3838

39-
result.putDouble("amount_milli_satoshis", (this.amount_milli_satoshis() as Option_u64Z.Some).some.toDouble())
39+
result.putDouble("amount_milli_satoshis", (amount_milli_satoshis() as Option_u64Z.Some).some.toDouble())
4040
result.putString("description", rawInvoice.description()?.into_inner())
4141
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())
42+
result.putBoolean("is_expired", is_expired)
43+
result.putInt("duration_since_epoch", duration_since_epoch().toInt())
44+
result.putInt("expiry_time", expiry_time().toInt())
45+
result.putInt("min_final_cltv_expiry", min_final_cltv_expiry().toInt())
4646
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)
47+
result.putString("recover_payee_pub_key", recover_payee_pub_key().hexEncodedString())
48+
result.putString("payment_hash", payment_hash().hexEncodedString())
49+
result.putString("payment_secret", payment_secret().hexEncodedString())
50+
result.putInt("timestamp", timestamp().toInt())
51+
result.putString("features", features()?.write()?.hexEncodedString())
52+
result.putInt("currency", currency().ordinal)
5353
result.putString("to_str", signedInv.to_str())
5454

5555
return result
@@ -59,29 +59,29 @@ val ChannelDetails.asJson: WritableMap
5959
get() {
6060
val result = Arguments.createMap()
6161

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()
62+
result.putString("channel_id", _channel_id.hexEncodedString())
63+
result.putBoolean("is_public", _is_public)
64+
result.putBoolean("is_usable", _is_usable)
65+
result.putBoolean("is_outbound", _is_outbound)
66+
result.putInt("balance_msat", _balance_msat.toInt())
67+
result.putString("counterparty", _counterparty.write().hexEncodedString())
68+
result.putString("funding_txo", if (_funding_txo != null) _funding_txo!!.write().hexEncodedString() else null)
69+
result.putString("channel_type", if (_channel_type != null) _channel_type!!.write().hexEncodedString() else null)
70+
result.putInt("user_channel_id", _user_channel_id.toInt())
71+
result.putInt("get_confirmations_required", (_confirmations_required as Option_u32Z.Some).some)
72+
(_short_channel_id as? Option_u64Z.Some)?.some?.toInt()
7373
?.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()
74+
result.putBoolean("is_funding_locked", _is_funding_locked)
75+
(_inbound_scid_alias as? Option_u64Z.Some)?.some?.toInt()
7676
?.let { result.putInt("inbound_scid_alias", it) }
77-
(this._inbound_scid_alias as? Option_u64Z.Some)?.some?.toInt()
77+
(_inbound_scid_alias as? Option_u64Z.Some)?.some?.toInt()
7878
?.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()
79+
result.putInt("inbound_capacity_msat", _inbound_capacity_msat.toInt())
80+
result.putInt("outbound_capacity_msat", _outbound_capacity_msat.toInt())
81+
result.putInt("channel_value_satoshis", _channel_value_satoshis.toInt())
82+
(_force_close_spend_delay as? Option_u16Z.Some)?.some?.toInt()
8383
?.let { result.putInt("force_close_spend_delay", it) }
84-
result.putInt("unspendable_punishment_reserve", (this._unspendable_punishment_reserve as Option_u64Z.Some).some.toInt())
84+
result.putInt("unspendable_punishment_reserve", (_unspendable_punishment_reserve as Option_u64Z.Some).some.toInt())
8585

8686
return result
8787
}

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ enum class LdkErrors {
5757
init_channel_manager,
5858
decode_invoice_fail,
5959
init_invoice_payer,
60-
invoice_payment_fail,
60+
invoice_payment_fail_unknown,
61+
invoice_payment_fail_invoice,
62+
invoice_payment_fail_routing,
63+
invoice_payment_fail_sending,
64+
invoice_payment_fail_retry_safe,
65+
invoice_payment_fail_parameter_error,
66+
invoice_payment_fail_partial,
67+
invoice_payment_fail_path_parameter_error,
6168
init_ldk_currency,
6269
invoice_create_failed
6370
}
@@ -361,13 +368,47 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
361368
val res = invoicePayer!!.pay_invoice((invoice as Result_InvoiceParseOrSemanticErrorZ_OK).res)
362369

363370
if (res.is_ok) {
364-
handleResolve(promise, LdkCallbackResponses.invoice_payment_success)
371+
return handleResolve(promise, LdkCallbackResponses.invoice_payment_success)
365372
}
366373

367-
val error = res as Result_PaymentIdPaymentErrorZ.Result_PaymentIdPaymentErrorZ_Err // ?: return return handleReject(promise, LdkErrors.invoice_payment_fail)
374+
val error = res as? Result_PaymentIdPaymentErrorZ.Result_PaymentIdPaymentErrorZ_Err // ?: return return handleReject(promise, LdkErrors.invoice_payment_fail)
368375

369-
//TODO check payment fail options like in swift
370-
handleReject(promise, LdkErrors.invoice_payment_fail, Error(error.err.toString()))
376+
val invoiceError = error?.err as? PaymentError.Invoice
377+
if (invoiceError != null) {
378+
return handleReject(promise, LdkErrors.invoice_payment_fail_invoice, Error(invoiceError.invoice))
379+
}
380+
381+
val routingError = error?.err as? PaymentError.Routing
382+
if (routingError != null) {
383+
return handleReject(promise, LdkErrors.invoice_payment_fail_routing, Error(routingError.routing._err))
384+
}
385+
386+
val sendingError = error?.err as? PaymentError.Sending
387+
if (sendingError != null) {
388+
val paymentAllFailedRetrySafe = sendingError.sending as? PaymentSendFailure.AllFailedRetrySafe
389+
if (paymentAllFailedRetrySafe != null) {
390+
return handleReject(promise, LdkErrors.invoice_payment_fail_retry_safe, Error(paymentAllFailedRetrySafe.all_failed_retry_safe.map { it.toString() }.toString()))
391+
}
392+
393+
val paymentParameterError = sendingError.sending as? PaymentSendFailure.ParameterError
394+
if (paymentParameterError != null) {
395+
return handleReject(promise, LdkErrors.invoice_payment_fail_parameter_error, Error(paymentParameterError.parameter_error.toString()))
396+
}
397+
398+
val paymentPartialFailure = sendingError.sending as? PaymentSendFailure.PartialFailure
399+
if (paymentPartialFailure != null) {
400+
return handleReject(promise, LdkErrors.invoice_payment_fail_partial, Error(paymentPartialFailure.toString()))
401+
}
402+
403+
val paymentPathParameterError = sendingError.sending as? PaymentSendFailure.PathParameterError
404+
if (paymentPathParameterError != null) {
405+
return handleReject(promise, LdkErrors.invoice_payment_fail_path_parameter_error, Error(paymentPartialFailure.toString()))
406+
}
407+
408+
return handleReject(promise, LdkErrors.invoice_payment_fail_sending, Error("PaymentError.Sending"))
409+
}
410+
411+
return handleReject(promise, LdkErrors.invoice_payment_fail_unknown)
371412
}
372413

373414
@ReactMethod

ios/Helpers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension Data {
8080

8181
func hexEncodedString(options: HexEncodingOptions = []) -> String {
8282
let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx"
83-
return self.map { String(format: format, $0) }.joined()
83+
return map { String(format: format, $0) }.joined()
8484
}
8585
}
8686

@@ -89,8 +89,8 @@ extension StringProtocol {
8989
var hexaBytes: [UInt8] { .init(hexa) }
9090
private var hexa: UnfoldSequence<UInt8, Index> {
9191
sequence(state: startIndex) { startIndex in
92-
guard startIndex < self.endIndex else { return nil }
93-
let endIndex = self.index(startIndex, offsetBy: 2, limitedBy: self.endIndex) ?? self.endIndex
92+
guard startIndex < endIndex else { return nil }
93+
let endIndex = index(startIndex, offsetBy: 2, limitedBy: endIndex) ?? endIndex
9494
defer { startIndex = endIndex }
9595
return UInt8(self[startIndex..<endIndex], radix: 16)
9696
}

ios/Ldk.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ enum LdkErrors: String {
4444
case init_channel_manager = "init_channel_manager"
4545
case decode_invoice_fail = "decode_invoice_fail"
4646
case init_invoice_payer = "init_invoice_payer"
47-
case invoice_payment_fail = "invoice_payment_fail"
47+
case invoice_payment_fail_unknown = "invoice_payment_fail_unknown"
48+
case invoice_payment_fail_invoice = "invoice_payment_fail_invoice"
49+
case invoice_payment_fail_routing = "invoice_payment_fail_routing"
50+
case invoice_payment_fail_sending = "invoice_payment_fail_sending"
51+
case invoice_payment_fail_retry_safe = "invoice_payment_fail_retry_safe"
52+
case invoice_payment_fail_parameter_error = "invoice_payment_fail_parameter_error"
53+
case invoice_payment_fail_partial = "invoice_payment_fail_partial"
54+
case invoice_payment_fail_path_parameter_error = "invoice_payment_fail_path_parameter_error"
4855
case init_ldk_currency = "init_ldk_currency"
4956
case invoice_create_failed = "invoice_create_failed"
5057
case claim_funds_failed = "claim_funds_failed"
@@ -392,33 +399,37 @@ class Ldk: NSObject {
392399
}
393400

394401
guard let error = res.getError() else {
395-
return handleReject(reject, .invoice_payment_fail)
402+
return handleReject(reject, .invoice_payment_fail_unknown)
396403
}
397404

398405
switch error.getValueType() {
399406
case .Invoice:
400-
return handleReject(reject, .invoice_payment_fail, nil, error.getValueAsInvoice())
407+
return handleReject(reject, .invoice_payment_fail_invoice, nil, error.getValueAsInvoice())
401408
case .Routing:
402-
return handleReject(reject, .invoice_payment_fail, nil, error.getValueAsRouting()?.get_err())
409+
return handleReject(reject, .invoice_payment_fail_routing, nil, error.getValueAsRouting()?.get_err())
403410
case .Sending:
404411
//Multiple sending errors
405412
guard let sendingError = error.getValueAsSending() else {
406-
return handleReject(reject, .invoice_payment_fail)
413+
return handleReject(reject, .invoice_payment_fail_sending)
407414
}
408415

409416
switch sendingError.getValueType() {
410417
case .AllFailedRetrySafe:
411-
return handleReject(reject, .invoice_payment_fail, nil, sendingError.getValueAsAllFailedRetrySafe().map { $0.description } )
418+
return handleReject(reject, .invoice_payment_fail_retry_safe, nil, sendingError.getValueAsAllFailedRetrySafe().map { $0.description } )
412419
case .ParameterError:
413-
return handleReject(reject, .invoice_payment_fail, nil, sendingError.getValueAsParameterError().debugDescription)
420+
return handleReject(reject, .invoice_payment_fail_parameter_error, nil, sendingError.getValueAsParameterError().debugDescription)
414421
case .PartialFailure:
415-
return handleReject(reject, .invoice_payment_fail, nil, sendingError.getValueAsPartialFailure().debugDescription)
422+
return handleReject(reject, .invoice_payment_fail_partial, nil, sendingError.getValueAsPartialFailure().debugDescription)
423+
case .PathParameterError:
424+
return handleReject(reject, .invoice_payment_fail_path_parameter_error, nil, sendingError.getValueAsPartialFailure().debugDescription)
416425
default:
417-
return handleReject(reject, .invoice_payment_fail)
426+
return handleReject(reject, .invoice_payment_fail_sending)
418427
}
419428
default:
420-
return handleReject(reject, .invoice_payment_fail, nil, res.getError().debugDescription)
429+
return handleReject(reject, .invoice_payment_fail_sending, nil, res.getError().debugDescription)
421430
}
431+
432+
return handleReject(reject, .invoice_payment_fail_unknown, nil)
422433
}
423434

424435
@objc

0 commit comments

Comments
 (0)