11import Keychain from 'react-native-keychain' ;
2- import { TAccount , TAvailableNetworks } from '@synonymdev/react-native-ldk' ;
3- import { getItem , setItem } from '../ldk' ;
2+ import {
3+ EEventTypes ,
4+ TAccount ,
5+ TAvailableNetworks ,
6+ } from '@synonymdev/react-native-ldk' ;
7+ import { backupAccount , getItem , importAccount , setItem } from '../ldk' ;
48import { EAccount } from './types' ;
59import { err , ok , Result } from './result' ;
610import { randomBytes } from 'react-native-randombytes' ;
@@ -10,6 +14,8 @@ import RNFS from 'react-native-fs';
1014import * as bip32 from 'bip32' ;
1115import * as bip39 from 'bip39' ;
1216import { ENetworks } from '@synonymdev/react-native-ldk/dist/utils/types' ;
17+ import ldk from '@synonymdev/react-native-ldk/dist/ldk' ;
18+ import Clipboard from '@react-native-clipboard/clipboard' ;
1319
1420/**
1521 * Use Keychain to save LDK name & seed.
@@ -205,3 +211,78 @@ export const ldkNetwork = (network: TAvailableNetworks): ENetworks => {
205211 return ENetworks . mainnet ;
206212 }
207213} ;
214+
215+ export const simulateStaleRestore = async (
216+ onUpdate : ( string ) => void ,
217+ ) : Promise < void > = > {
218+ const channels = await ldk . listChannels ( ) ;
219+ if ( channels . isErr ( ) ) {
220+ throw channels . error ;
221+ }
222+ if ( channels . value . filter ( ( c ) => c . is_usable ) . length === 0 ) {
223+ throw new Error ( 'No usable channels. Open a channel first.' ) ;
224+ }
225+
226+ onUpdate ( 'Backing up...' ) ;
227+ const backupResponse = await backupAccount ( ) ;
228+ if ( backupResponse . isErr ( ) ) {
229+ throw backupResponse . error ;
230+ }
231+
232+ const timeoutSeconds = 30 ;
233+ const invoice = await ldk . createPaymentRequest ( {
234+ amountSats : 12 ,
235+ description : 'crash test' ,
236+ expiryDeltaSeconds : timeoutSeconds ,
237+ } ) ;
238+ if ( invoice . isErr ( ) ) {
239+ throw invoice . error ;
240+ }
241+
242+ let paymentClaimed = false ;
243+ let paymentSubscription = ldk . onEvent (
244+ EEventTypes . channel_manager_payment_claimed ,
245+ ( ) => ( paymentClaimed = true ) ,
246+ ) ;
247+
248+ Clipboard . setString ( invoice . value . to_str ) ;
249+
250+ //Keep checking if we got the payment
251+ for ( let i = 0 ; i < timeoutSeconds ; i ++ ) {
252+ onUpdate (
253+ `Please pay invoice in clipboard to continue (${ timeoutSeconds - i } )...` ,
254+ ) ;
255+
256+ if ( paymentClaimed ) {
257+ onUpdate ( 'Payment claimed! Testing stale restore...' ) ;
258+ break ;
259+ }
260+
261+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
262+ }
263+
264+ paymentSubscription . remove ( ) ;
265+
266+ if ( ! paymentClaimed ) {
267+ throw new Error ( 'No payment claimed. Timeout out.' ) ;
268+ }
269+
270+ onUpdate ( 'Importing stale backup and force closing all channels...' ) ;
271+
272+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2500 ) ) ;
273+
274+ await ldk . stop ( ) ;
275+ const forceCloseAllChannels = true ; //To test the crash restore set to false
276+ const importResponse = await importAccount (
277+ backupResponse . value ,
278+ forceCloseAllChannels ,
279+ ) ;
280+ if ( importResponse . isErr ( ) ) {
281+ throw importResponse . error ;
282+ }
283+
284+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2500 ) ) ;
285+ onUpdate (
286+ "If this didn't crash and you can see your claimable balance, you're good!" ,
287+ ) ;
288+ } ;
0 commit comments