99 */
1010import React , { useEffect , useState } from 'react' ;
1111import {
12+ Alert ,
1213 Button ,
1314 SafeAreaView ,
1415 ScrollView ,
1516 StatusBar ,
1617 StyleSheet ,
1718 Text ,
1819} from 'react-native' ;
19- import ldk from '@synonymdev/react-native-ldk' ;
20+ import lm from '@synonymdev/react-native-ldk' ;
21+ import ldk from '@synonymdev/react-native-ldk/dist/ldk' ; //For direct access to LDK functions
22+
2023const testNodePubkey =
2124 '034ecfd567a64f06742ac300a2985676abc0b1dc6345904a08bb52d5418e685f79' ;
22- const testNodeAddress = '35.240.72.95:9735' ;
25+ const testNodeAddress = '35.240.72.95' ;
26+ const testNodePort = 9735 ;
2327
2428const App = ( ) => {
2529 const [ message , setMessage ] = useState ( '' ) ;
2630
27- const startLdk = async ( ) : Promise < void > => {
28- setMessage ( 'Starting LDK...' ) ;
29- try {
30- const res = await ldk . startChainMonitor ( ) ;
31-
32- if ( res . isErr ( ) ) {
33- setMessage ( res . error . message ) ;
34- console . error ( res . error ) ;
35- return ;
36- }
37-
38- setMessage ( JSON . stringify ( res . value ) ) ;
39- } catch ( e ) {
40- setMessage ( e . toString ( ) ) ;
41- }
42- } ;
43-
4431 return (
4532 < >
4633 < StatusBar barStyle = "dark-content" />
@@ -51,55 +38,202 @@ const App = () => {
5138 style = { styles . scrollView } >
5239 < Text style = { styles . message } > { message } </ Text >
5340
54- < Button title = { 'Start' } onPress = { startLdk } />
41+ < Button
42+ title = { 'Start' }
43+ onPress = { async ( ) => {
44+ setMessage ( 'Starting LDK...' ) ;
45+ try {
46+ const res = await lm . start ( ) ;
47+
48+ if ( res . isErr ( ) ) {
49+ setMessage ( res . error . message ) ;
50+ return ;
51+ }
52+
53+ setMessage ( JSON . stringify ( res . value ) ) ;
54+ } catch ( e ) {
55+ setMessage ( e . toString ( ) ) ;
56+ }
57+ } }
58+ />
5559
5660 < Button
57- title = { 'Version ' }
61+ title = { 'Add peer ' }
5862 onPress = { async ( ) => {
59- const res = await ldk . version ( ) ;
60- alert ( JSON . stringify ( res ) ) ;
63+ setMessage ( 'Adding peer...' ) ;
64+ try {
65+ const res = await ldk . addPeer ( {
66+ pubKey :
67+ '02b96088e17dd53a4297d0769e8166a64c25ff47b143cbd6615723cc2a49efbb65' ,
68+ address : '127.0.0.1' ,
69+ port : 9837 ,
70+ } ) ;
71+
72+ if ( res . isErr ( ) ) {
73+ setMessage ( res . error . message ) ;
74+ return ;
75+ }
76+
77+ setMessage ( JSON . stringify ( res . value ) ) ;
78+ } catch ( e ) {
79+ setMessage ( e . toString ( ) ) ;
80+ }
81+ } }
82+ />
83+
84+ < Button
85+ title = { 'List peers' }
86+ onPress = { async ( ) => {
87+ try {
88+ const res = await ldk . listPeers ( ) ;
89+
90+ if ( res . isErr ( ) ) {
91+ setMessage ( res . error . message ) ;
92+ return ;
93+ }
94+
95+ setMessage ( JSON . stringify ( res . value ) ) ;
96+ } catch ( e ) {
97+ setMessage ( e . toString ( ) ) ;
98+ }
99+ } }
100+ />
101+
102+ < Button
103+ title = { 'List channels' }
104+ onPress = { async ( ) => {
105+ try {
106+ const res = await ldk . listChannels ( ) ;
107+
108+ if ( res . isErr ( ) ) {
109+ setMessage ( res . error . message ) ;
110+ return ;
111+ }
112+
113+ let msg = '' ;
114+ res . value . forEach ( ( channel ) => {
115+ Object . keys ( channel ) . forEach ( ( key ) => {
116+ // @ts -ignore
117+ msg += `${ key } : ${ channel [ key ] } \n` ;
118+ } ) ;
119+ } ) ;
120+
121+ setMessage ( msg ) ;
122+ } catch ( e ) {
123+ setMessage ( e . toString ( ) ) ;
124+ }
61125 } }
62126 />
63127
64128 < Button
65- title = { 'initFeeEstimator ' }
129+ title = { 'List usable channels ' }
66130 onPress = { async ( ) => {
67- const res = await ldk . initFeeEstimator ( {
68- high : 1000 ,
69- normal : 500 ,
70- low : 100 ,
71- } ) ;
72- alert ( JSON . stringify ( res ) ) ;
131+ try {
132+ const res = await ldk . listUsableChannels ( ) ;
133+
134+ if ( res . isErr ( ) ) {
135+ setMessage ( res . error . message ) ;
136+ return ;
137+ }
138+
139+ let msg = '' ;
140+ res . value . forEach ( ( channel ) => {
141+ Object . keys ( channel ) . forEach ( ( key ) => {
142+ // @ts -ignore
143+ msg += `${ key } : ${ channel [ key ] } \n` ;
144+ } ) ;
145+ } ) ;
146+
147+ setMessage ( msg ) ;
148+ } catch ( e ) {
149+ setMessage ( e . toString ( ) ) ;
150+ }
73151 } }
74152 />
75153
76154 < Button
77- title = { 'initLogger ' }
155+ title = { 'Pay invoice ' }
78156 onPress = { async ( ) => {
79- const res = await ldk . initLogger ( ) ;
80- alert ( JSON . stringify ( res ) ) ;
157+ const paymentRequest =
158+ 'lnbcrt100u1p3gw4lmpp5g8nn2m856gwmc2rxlsgacw9746tyrzz5nh8svq6s6usmqqwdkn9qdp92phkcctjypykuan0d93k2grxdaezqcmpwfhkcxqyjw5qcqp2sp5csqwsteyyfeg6l0pfa6x6cwh88aklqzy0g7s5ndrg55h5fanwajq9qy9qsqtvfuzpl3w00tfvfgwt0waxsl9fe4z6eu4cwttulx5c5p5fl442vynngr9w0un3janvcr68a3vtlyn3js0j332wzdry5wneat6f2365gp6xstn6' ;
159+ const res = await ldk . decode ( { paymentRequest} ) ;
160+ if ( res . isErr ( ) ) {
161+ return setMessage ( res . error . message ) ;
162+ }
163+
164+ const { recover_payee_pub_key, amount_milli_satoshis} = res . value ;
165+
166+ Alert . alert (
167+ `Pay ${ amount_milli_satoshis ?? 0 } ` ,
168+ `To pubkey: ${ recover_payee_pub_key } ` ,
169+ [
170+ {
171+ text : 'Cancel' ,
172+ onPress : ( ) => console . log ( 'Cancel Pressed' ) ,
173+ style : 'cancel' ,
174+ } ,
175+ {
176+ text : 'Pay' ,
177+ onPress : async ( ) => {
178+ const pay = await ldk . pay ( { paymentRequest} ) ;
179+ if ( pay . isErr ( ) ) {
180+ return setMessage ( pay . error . message ) ;
181+ }
182+
183+ setMessage ( pay . value ) ;
184+ } ,
185+ } ,
186+ ] ,
187+ ) ;
81188 } }
82189 />
83190
84191 < Button
85- title = { 'setLogLevel ' }
192+ title = { 'Create invoice ' }
86193 onPress = { async ( ) => {
87- const res = await ldk . setLogLevel ( 2 , true ) ;
88- alert ( JSON . stringify ( res ) ) ;
194+ try {
195+ const res = await ldk . createPaymentRequest ( {
196+ amountSats : 1234 ,
197+ description : 'paymeplz' ,
198+ } ) ;
199+
200+ if ( res . isErr ( ) ) {
201+ setMessage ( res . error . message ) ;
202+ return ;
203+ }
204+
205+ const { to_str} = res . value ;
206+
207+ setMessage ( to_str ) ;
208+ } catch ( e ) {
209+ setMessage ( e . toString ( ) ) ;
210+ }
89211 } }
90212 />
91213
92214 < Button
93- title = { 'updateFees ' }
215+ title = { 'Get info ' }
94216 onPress = { async ( ) => {
95- const res = await ldk . updateFees ( {
96- high : 1000 ,
97- normal : 500 ,
98- low : 100 ,
99- } ) ;
100- if ( res . isOk ( ) ) {
101- alert ( res . value ) ;
217+ const nodeIdRes = await ldk . nodeId ( ) ;
218+ if ( nodeIdRes . isErr ( ) ) {
219+ return setMessage ( nodeIdRes . error . message ) ;
102220 }
221+
222+ console . log ( nodeIdRes . value ) ;
223+
224+ setMessage ( `Node ID: ${ nodeIdRes . value } ` ) ;
225+ } }
226+ />
227+
228+ < Button
229+ title = { 'Show version' }
230+ onPress = { async ( ) => {
231+ const res = await ldk . version ( ) ;
232+ if ( res . isErr ( ) ) {
233+ return setMessage ( res . error . message ) ;
234+ }
235+
236+ setMessage ( res . value . ldk ) ;
103237 } }
104238 />
105239 </ ScrollView >
@@ -119,6 +253,7 @@ const styles = StyleSheet.create({
119253 message : {
120254 margin : 10 ,
121255 textAlign : 'center' ,
256+ color : 'green' ,
122257 } ,
123258} ) ;
124259
0 commit comments