Skip to content

Commit 34846cd

Browse files
committed
Koltin create payment request
Kotlin create invoice fix
1 parent 33f417e commit 34846cd

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ 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
56
import org.ldk.structs.Option_u64Z
6-
import org.ldk.structs.Result_InvoiceParseOrSemanticErrorZ.Result_InvoiceParseOrSemanticErrorZ_OK
77

88
fun handleResolve(promise: Promise, res: LdkCallbackResponses) {
99
LdkEventEmitter.send(EventTypes.swift_log, "Success: ${res}")
@@ -30,27 +30,26 @@ fun String.hexa(): ByteArray {
3030
.map { it.toInt(16).toByte() }
3131
.toByteArray()
3232
}
33-
fun Result_InvoiceParseOrSemanticErrorZ_OK.json(): WritableMap {
33+
fun Invoice.json(): WritableMap {
3434
val result = Arguments.createMap()
35-
val inv = this.res
36-
val signedInv = inv.into_signed_raw()
35+
val signedInv = this.into_signed_raw()
3736
val rawInvoice = signedInv.raw_invoice()
3837

39-
result.putDouble("amount_milli_satoshis", (this.res.amount_milli_satoshis() as Option_u64Z.Some).some.toDouble())
38+
result.putDouble("amount_milli_satoshis", (this.amount_milli_satoshis() as Option_u64Z.Some).some.toDouble())
4039
result.putString("description", rawInvoice.description()?.into_inner())
4140
result.putBoolean("check_signature", signedInv.check_signature())
42-
result.putBoolean("is_expired", inv.is_expired)
43-
result.putInt("duration_since_epoch", inv.duration_since_epoch().toInt())
44-
result.putInt("expiry_time", inv.expiry_time().toInt())
45-
result.putInt("min_final_cltv_expiry", inv.min_final_cltv_expiry().toInt())
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())
4645
result.putString("payee_pub_key", rawInvoice.payee_pub_key()?._a?.hexEncodedString())
47-
result.putString("recover_payee_pub_key", inv.recover_payee_pub_key().hexEncodedString())
48-
result.putString("payment_hash", inv.payment_hash().hexEncodedString())
49-
result.putString("payment_secret", inv.payment_secret().hexEncodedString())
50-
result.putInt("timestamp", inv.timestamp().toInt())
51-
result.putString("features", inv.features()?.write()?.hexEncodedString())
52-
result.putInt("currency", inv.currency().ordinal)
53-
result.putString("features", signedInv.to_str())
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())
5453

5554
return result
5655
}

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

Lines changed: 19 additions & 3 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.json())
349+
promise.resolve(parsedInvoice.res.json())
350350
}
351351

352352
@ReactMethod
@@ -372,8 +372,24 @@ class LdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
372372

373373
@ReactMethod
374374
fun createPaymentRequest(amountSats: Double, description: String, promise: Promise) {
375-
//TODO
376-
handleResolve(promise, LdkCallbackResponses.invoice_payment_success)
375+
channelManager ?: return handleReject(promise, LdkErrors.init_channel_manager)
376+
keysManager ?: return handleReject(promise, LdkErrors.init_keys_manager)
377+
ldkCurrency ?: return handleReject(promise, LdkErrors.init_ldk_currency)
378+
379+
val res = UtilMethods.create_invoice_from_channelmanager(
380+
channelManager,
381+
keysManager!!.as_KeysInterface(),
382+
Currency.LDKCurrency_Bitcoin,
383+
Option_u64Z.some(amountSats.toLong()),
384+
description
385+
);
386+
387+
if (res.is_ok) {
388+
return promise.resolve((res as Result_InvoiceSignOrCreationErrorZ_OK).res.json())
389+
}
390+
391+
val error = res as Result_InvoiceSignOrCreationErrorZ
392+
return handleReject(promise, LdkErrors.invoice_create_failed, Error(error.toString()))
377393
}
378394

379395
//MARK: Fetch methods

example/App.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,6 @@ const App = () => {
231231
setMessage(res.value.ldk);
232232
}}
233233
/>
234-
235-
<Button
236-
title={'Test'}
237-
onPress={async () => {
238-
const res = await ldk.test();
239-
if (res.isErr()) {
240-
return setMessage(res.error.message);
241-
}
242-
243-
setMessage(res.value.ldk);
244-
}}
245-
/>
246234
</ScrollView>
247235
</SafeAreaView>
248236
</>

0 commit comments

Comments
 (0)