1010
1111import React , { useEffect , useState } from 'react' ;
1212import {
13- Alert ,
1413 Button ,
1514 SafeAreaView ,
1615 ScrollView ,
@@ -22,9 +21,12 @@ import Clipboard from '@react-native-clipboard/clipboard';
2221import lnd , {
2322 ENetworks ,
2423 LndConf ,
25- lnrpc ,
2624 TCurrentLndState ,
27- } from 'react-native-lightning' ;
25+ } from '@synonymdev/react-native-lightning' ;
26+
27+ import { emoji } from './src/helpers' ;
28+ import PSBT from './src/PSBT' ;
29+ import Options from './src/Options' ;
2830
2931declare const global : { HermesInternal : null | { } } ;
3032
@@ -33,6 +35,8 @@ const testNodePubkey =
3335 '034ecfd567a64f06742ac300a2985676abc0b1dc6345904a08bb52d5418e685f79' ;
3436const testNodeAddress = '35.240.72.95:9735' ;
3537
38+ const network = ENetworks . testnet ;
39+
3640const App = ( ) => {
3741 const [ message , setMessage ] = useState ( '' ) ;
3842 const [ lndState , setLndState ] = useState < TCurrentLndState > ( {
@@ -44,11 +48,9 @@ const App = () => {
4448 const [ walletExists , setWalletExists ] = useState ( false ) ;
4549 const [ seed , setSeed ] = useState < string [ ] > ( [ ] ) ;
4650
47- const emoji = ( s : boolean ) => ( s ? '✅' : '❌' ) ;
48-
4951 useEffect ( ( ) => {
5052 ( async ( ) => {
51- const walletExistsRes = await lnd . walletExists ( ENetworks . testnet ) ;
53+ const walletExistsRes = await lnd . walletExists ( network ) ;
5254 if ( walletExistsRes . isOk ( ) ) {
5355 setWalletExists ( walletExistsRes . value ) ;
5456 }
@@ -80,8 +82,8 @@ const App = () => {
8082 return console . error ( res . error ) ;
8183 }
8284
83- const backupBytes = res . value . multiChanBackup . multiChanBackup ;
84- console . log ( `Backup required (${ backupBytes . length } bytes)` ) ;
85+ const backupBytes = res . value . multiChanBackup ? .multiChanBackup ;
86+ console . log ( `Backup required (${ backupBytes ? .length } bytes)` ) ;
8587 } ,
8688 ( ) => { } ,
8789 ) ;
@@ -133,7 +135,7 @@ const App = () => {
133135 setMessage ( 'Starting LND...' ) ;
134136
135137 const customFields = { } ;
136- const lndConf = new LndConf ( ENetworks . testnet , customFields ) ;
138+ const lndConf = new LndConf ( network , customFields ) ;
137139 const res = await lnd . start ( lndConf ) ;
138140
139141 if ( res . isErr ( ) ) {
@@ -201,236 +203,11 @@ const App = () => {
201203
202204 { lndState . grpcReady ? (
203205 < >
204- < Button
205- title = { 'Get info' }
206- onPress = { async ( ) => {
207- const res = await lnd . getInfo ( ) ;
208-
209- if ( res . isErr ( ) ) {
210- console . error ( res . error ) ;
211- return ;
212- }
213-
214- const {
215- identityPubkey,
216- syncedToChain,
217- blockHeight,
218- numPeers,
219- numActiveChannels,
220- version,
221- alias,
222- } = res . value ;
223- let info = `Pubkey: ${ identityPubkey } \n\n` ;
224- info += `Alias: ${ alias } \n\n` ;
225- info += `Synced: ${ emoji ( syncedToChain ) } \n\n` ;
226- info += `BlockHeight: ${ blockHeight } \n\n` ;
227- info += `NumPeers: ${ numPeers } \n\n` ;
228- info += `NumActiveChannels: ${ numActiveChannels } \n\n` ;
229- info += `Version: ${ version } ` ;
230-
231- setMessage ( info ) ;
232- } }
233- />
234- < Button
235- title = { 'Get on chain balance' }
236- onPress = { async ( ) => {
237- const res = await lnd . getWalletBalance ( ) ;
238-
239- if ( res . isErr ( ) ) {
240- console . error ( res . error ) ;
241- return ;
242- }
243-
244- setMessage (
245- `Total balance: ${ res . value . totalBalance } \nConfirmed balance: ${ res . value . confirmedBalance } \nUnconfirmed balance: ${ res . value . unconfirmedBalance } \n` ,
246- ) ;
247- } }
248- />
249- < Button
250- title = { 'Get channel balance' }
251- onPress = { async ( ) => {
252- const res = await lnd . getChannelBalance ( ) ;
253-
254- if ( res . isErr ( ) ) {
255- console . error ( res . error ) ;
256- return ;
257- }
258-
259- setMessage (
260- `Pending open balance: ${ res . value . pendingOpenBalance } \nLocal balance: ${ res . value . localBalance ?. sat } \nRemote balance: ${ res . value ?. remoteBalance . sat } \n` ,
261- ) ;
262- } }
263- />
264- < Button
265- title = { 'Copy address to clipboard' }
266- onPress = { async ( ) => {
267- const res = await lnd . getAddress ( ) ;
268-
269- if ( res . isErr ( ) ) {
270- console . error ( res . error ) ;
271- return ;
272- }
273-
274- Clipboard . setString ( res . value . address ) ;
275- setMessage ( `Copied: ${ res . value . address } ` ) ;
276- } }
277- />
278- < Button
279- title = { 'Open channel' }
280- onPress = { async ( ) => {
281- setMessage ( 'Connecting to peer...' ) ;
282-
283- const connectRes = await lnd . connectPeer (
284- testNodePubkey ,
285- testNodeAddress ,
286- ) ;
287-
288- if (
289- connectRes . isErr ( ) &&
290- connectRes . error . message . indexOf (
291- 'already connected to peer' ,
292- ) < 0
293- ) {
294- console . error ( connectRes . error ) ;
295- return ;
296- }
297-
298- setMessage ( 'Opening channel...' ) ;
299-
300- const balanceRes = await lnd . getWalletBalance ( ) ;
301- if ( balanceRes . isErr ( ) ) {
302- setMessage ( balanceRes . error . message ) ;
303- return ;
304- }
305-
306- let value = Number ( balanceRes . value . confirmedBalance ) ;
307- const maxChannel = 0.16 * 100000000 ;
308- if ( value > maxChannel ) {
309- value = maxChannel ;
310- }
311-
312- value -= 50000 ;
313-
314- setMessage ( 'Opening channel...' ) ;
315-
316- const openRes = await lnd . openChannel ( value , testNodePubkey ) ;
317- if ( openRes . isErr ( ) ) {
318- setMessage ( openRes . error . message ) ;
319- return ;
320- }
321- } }
322- />
323-
324- < Button
325- title = { 'List channels' }
326- onPress = { async ( ) => {
327- const res = await lnd . listChannels ( ) ;
328-
329- if ( res . isErr ( ) ) {
330- console . error ( res . error ) ;
331- return ;
332- }
333-
334- let list = '' ;
335- res . value . channels . forEach ( ( channel : lnrpc . IChannel ) => {
336- list += `Remote pubkey: ${ channel . remotePubkey } \n` ;
337- list += `Capacity: ${ channel . capacity } \n` ;
338- list += `Local balance: ${ channel . localBalance } \n` ;
339- list += `Remote balance: ${ channel . remoteBalance } \n` ;
340- list += `Active: ${ emoji ( channel . active || false ) } \n` ;
341-
342- list += '\n\n' ;
343- } ) ;
344-
345- setMessage ( list || 'No channels' ) ;
346- } }
347- />
348-
349- < Button
350- title = { 'Paste invoice from clipboard' }
351- onPress = { async ( ) => {
352- const invoice = await Clipboard . getString ( ) ;
353- if ( ! invoice ) {
354- setMessage ( 'Clipboard empty' ) ;
355- }
356-
357- const decodeRes = await lnd . decodeInvoice ( invoice ) ;
358-
359- if ( decodeRes . isErr ( ) ) {
360- setMessage ( decodeRes . error . message ) ;
361- return ;
362- }
363-
364- const { numSatoshis, description} = decodeRes . value ;
365-
366- Alert . alert (
367- `Pay ${ numSatoshis } sats?` ,
368- description ,
369- [
370- {
371- text : 'Cancel' ,
372- onPress : ( ) : void => { } ,
373- style : 'cancel' ,
374- } ,
375- {
376- text : 'Pay' ,
377- onPress : async ( ) : Promise < void > => {
378- setMessage ( 'Paying...' ) ;
379- const payRes = await lnd . payInvoice ( invoice ) ;
380- if ( payRes . isErr ( ) ) {
381- setMessage ( payRes . error . message ) ;
382- return ;
383- }
384-
385- setMessage ( payRes . value . paymentError || 'Paid' ) ;
386- } ,
387- } ,
388- ] ,
389- { cancelable : true } ,
390- ) ;
391- } }
392- />
393-
394- < Button
395- title = { 'Create invoice and copy to clipboard' }
396- onPress = { async ( ) => {
397- const res = await lnd . createInvoice (
398- 5000 ,
399- `Test invoice ${ new Date ( ) . getTime ( ) } ` ,
400- ) ;
401-
402- if ( res . isErr ( ) ) {
403- console . error ( res . error ) ;
404- return ;
405- }
406-
407- Clipboard . setString ( res . value . paymentRequest ) ;
408- setMessage ( res . value . paymentRequest ) ;
409- } }
410- />
411-
412- < Button
413- title = { 'Export backup and verify it' }
414- onPress = { async ( ) => {
415- const res = await lnd . exportAllChannelBackups ( ) ;
416-
417- if ( res . isErr ( ) ) {
418- console . error ( res . error ) ;
419- return ;
420- }
421-
422- const verifyRes = await lnd . verifyMultiChannelBackup (
423- res . value . multiChanBackup . multiChanBackup ,
424- ) ;
425-
426- if ( verifyRes . isErr ( ) ) {
427- setMessage ( verifyRes . error . message ) ;
428- return ;
429- }
430-
431- setMessage ( `Backup verification: ${ emoji ( verifyRes . value ) } ` ) ;
432- } }
206+ < Options
207+ nodePubKey = { testNodePubkey }
208+ nodeAddress = { testNodeAddress }
433209 />
210+ < PSBT nodePubKey = { testNodePubkey } />
434211 </ >
435212 ) : null }
436213 </ ScrollView >
0 commit comments