1+ package com.reactnativeldk
2+ import com.facebook.react.bridge.Arguments
3+ import com.facebook.react.bridge.Promise
4+ import com.facebook.react.bridge.WritableArray
5+ import com.facebook.react.bridge.WritableMap
6+ import org.ldk.structs.*
7+
8+ fun handleResolve (promise : Promise , res : LdkCallbackResponses ) {
9+ LdkEventEmitter .send(EventTypes .swift_log, " Success: ${res} " )
10+ promise.resolve(res.toString());
11+ }
12+
13+ fun handleReject (promise : Promise , ldkError : LdkErrors , error : Error ? = null) {
14+ if (error != = null ) {
15+ LdkEventEmitter .send(EventTypes .swift_log, " Error: ${ldkError} . Message: ${error.toString()} " )
16+ promise.reject(ldkError.toString(), error);
17+ } else {
18+ LdkEventEmitter .send(EventTypes .swift_log, " Error: ${ldkError} " )
19+ promise.reject(ldkError.toString(), ldkError.toString())
20+ }
21+ }
22+
23+ fun ByteArray.hexEncodedString (): String {
24+ return joinToString(" " ) { " %02x" .format(it) }
25+ }
26+
27+ fun String.hexa (): ByteArray {
28+ check(length % 2 == 0 ) { " Must have an even length" }
29+ return chunked(2 )
30+ .map { it.toInt(16 ).toByte() }
31+ .toByteArray()
32+ }
33+
34+ val Invoice .asJson: WritableMap
35+ get() {
36+ val result = Arguments .createMap()
37+ val signedInv = into_signed_raw()
38+ val rawInvoice = signedInv.raw_invoice()
39+
40+ result.putInt(" amount_milli_satoshis" , (amount_milli_satoshis() as Option_u64Z .Some ).some.toInt())
41+ result.putString(" description" , rawInvoice.description()?.into_inner())
42+ result.putBoolean(" check_signature" , signedInv.check_signature())
43+ result.putBoolean(" is_expired" , is_expired)
44+ result.putInt(" duration_since_epoch" , duration_since_epoch().toInt())
45+ result.putInt(" expiry_time" , expiry_time().toInt())
46+ result.putInt(" min_final_cltv_expiry" , min_final_cltv_expiry().toInt())
47+ result.putHexString(" payee_pub_key" , rawInvoice.payee_pub_key()?._a )
48+ result.putHexString(" recover_payee_pub_key" , recover_payee_pub_key())
49+ result.putHexString(" payment_hash" , payment_hash())
50+ result.putHexString(" payment_secret" , payment_secret())
51+ result.putInt(" timestamp" , timestamp().toInt())
52+ result.putHexString(" features" , features()?.write())
53+ result.putInt(" currency" , currency().ordinal)
54+ result.putString(" to_str" , signedInv.to_str())
55+
56+ return result
57+ }
58+
59+ val ChannelDetails .asJson: WritableMap
60+ get() {
61+ val result = Arguments .createMap()
62+
63+ result.putHexString(" channel_id" , _channel_id )
64+ result.putBoolean(" is_public" , _is_public )
65+ result.putBoolean(" is_usable" , _is_usable )
66+ result.putBoolean(" is_outbound" , _is_outbound )
67+ result.putInt(" balance_msat" , _balance_msat .toInt())
68+ result.putHexString(" counterparty" , _counterparty .write())
69+ result.putHexString(" funding_txo" , _funding_txo ?.write())
70+ result.putHexString(" channel_type" , _channel_type ?.write())
71+ result.putInt(" user_channel_id" , _user_channel_id .toInt())
72+ result.putInt(" get_confirmations_required" , (_confirmations_required as Option_u32Z .Some ).some)
73+ (_short_channel_id as ? Option_u64Z .Some )?.some?.toInt()
74+ ?.let { result.putInt(" short_channel_id" , it) } // Optional number
75+ result.putBoolean(" is_funding_locked" , _is_funding_locked )
76+ (_inbound_scid_alias as ? Option_u64Z .Some )?.some?.toInt()
77+ ?.let { result.putInt(" inbound_scid_alias" , it) }
78+ (_inbound_scid_alias as ? Option_u64Z .Some )?.some?.toInt()
79+ ?.let { result.putInt(" inbound_payment_scid" , it) }
80+ result.putInt(" inbound_capacity_msat" , _inbound_capacity_msat .toInt())
81+ result.putInt(" outbound_capacity_msat" , _outbound_capacity_msat .toInt())
82+ result.putInt(" channel_value_satoshis" , _channel_value_satoshis .toInt())
83+ (_force_close_spend_delay as ? Option_u16Z .Some )?.some?.toInt()
84+ ?.let { result.putInt(" force_close_spend_delay" , it) }
85+ result.putInt(" unspendable_punishment_reserve" , (_unspendable_punishment_reserve as Option_u64Z .Some ).some.toInt())
86+
87+ return result
88+ }
89+
90+ val RouteHop .asJson: WritableMap
91+ get() {
92+ val hop = Arguments .createMap()
93+ hop.putHexString(" pubkey" , _pubkey )
94+ hop.putInt(" fee_msat" , _fee_msat .toInt())
95+ return hop
96+ }
97+
98+ fun WritableMap.putHexString (key : String , bytes : ByteArray? ) {
99+ if (bytes != null ) {
100+ putString(key, bytes.hexEncodedString())
101+ } else {
102+ putString(key, null )
103+ }
104+ }
105+
106+ fun WritableArray.pushHexString (bytes : ByteArray ) {
107+ pushString(bytes.hexEncodedString())
108+ }
0 commit comments