Skip to content
This repository was archived by the owner on Feb 9, 2026. It is now read-only.

Commit d995d42

Browse files
committed
chore: remove old backup logic from example app and add deprecated warnings to old backup and restore functions
fix: lint
1 parent 3ec65c6 commit d995d42

6 files changed

Lines changed: 45 additions & 205 deletions

File tree

example/Dev.tsx

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@ import {
1212
View,
1313
} from 'react-native';
1414
import Clipboard from '@react-native-clipboard/clipboard';
15-
import {
16-
backupAccount,
17-
getAddressBalance,
18-
importAccount,
19-
setupLdk,
20-
syncLdk,
21-
updateHeader,
22-
} from './ldk';
15+
import { getAddressBalance, setupLdk, syncLdk, updateHeader } from './ldk';
2316
import { connectToElectrum, subscribeToHeader } from './electrum';
2417
import ldk from '@synonymdev/react-native-ldk/dist/ldk';
2518
import lm, {
@@ -31,20 +24,14 @@ import lm, {
3124
TChannelUpdate,
3225
} from '@synonymdev/react-native-ldk';
3326
import { backupServerDetails, peers } from './utils/constants';
34-
import {
35-
createNewAccount,
36-
getAccount,
37-
getAddress,
38-
simulateStaleRestore,
39-
} from './utils/helpers';
27+
import { createNewAccount, getAccount, getAddress } from './utils/helpers';
4028
import RNFS from 'react-native-fs';
4129

4230
let logSubscription: EmitterSubscription | undefined;
4331
let paymentSubscription: EmitterSubscription | undefined;
4432
let onChannelSubscription: EmitterSubscription | undefined;
4533
let paymentFailedSubscription: EmitterSubscription | undefined;
4634
let paymentPathSuccess: EmitterSubscription | undefined;
47-
let backupSubscriptionId: string | undefined;
4835

4936
const Dev = (): ReactElement => {
5037
const [message, setMessage] = useState('...');
@@ -144,25 +131,12 @@ const Dev = (): ReactElement => {
144131
);
145132
}
146133

147-
if (!backupSubscriptionId) {
148-
backupSubscriptionId = lm.subscribeToBackups((backupRes) => {
149-
if (backupRes.isErr()) {
150-
return alert('Backup required but failed to export account');
151-
}
152-
153-
console.log(
154-
`Backup updated for account ${backupRes.value.account.name}`,
155-
);
156-
});
157-
}
158-
159134
return (): void => {
160135
logSubscription && logSubscription.remove();
161136
paymentSubscription && paymentSubscription.remove();
162137
paymentFailedSubscription && paymentFailedSubscription.remove();
163138
paymentPathSuccess && paymentPathSuccess.remove();
164139
onChannelSubscription && onChannelSubscription.remove();
165-
backupSubscriptionId && lm.unsubscribeFromBackups(backupSubscriptionId);
166140
};
167141
}, []);
168142

@@ -589,50 +563,6 @@ const Dev = (): ReactElement => {
589563
}}
590564
/>
591565

592-
<Button
593-
title={'Backup Current Account'}
594-
onPress={async (): Promise<void> => {
595-
const backupResponse = await backupAccount();
596-
if (backupResponse.isErr()) {
597-
setMessage(backupResponse.error.message);
598-
return;
599-
}
600-
console.log(backupResponse.value);
601-
Clipboard.setString(JSON.stringify(backupResponse.value));
602-
setMessage(
603-
`Backup of the following account copied to clipboard:\n${JSON.stringify(
604-
backupResponse.value.account,
605-
)}`,
606-
);
607-
}}
608-
/>
609-
<Button
610-
title={'Import Account From Clipboard'}
611-
onPress={async (): Promise<void> => {
612-
setMessage('Importing Account...');
613-
const clipboardBackup = await Clipboard.getString();
614-
const importResponse = await importAccount(clipboardBackup);
615-
if (importResponse.isErr()) {
616-
setMessage(importResponse.error.message);
617-
return;
618-
}
619-
const accountData = JSON.stringify(importResponse.value);
620-
setMessage(
621-
`Successfully imported the following account: ${accountData}`,
622-
);
623-
}}
624-
/>
625-
<Button
626-
title={'Simulate stale backup restore'}
627-
onPress={async (): Promise<void> => {
628-
try {
629-
await simulateStaleRestore((msg) => setMessage(msg));
630-
} catch (e) {
631-
setMessage(e.message);
632-
return;
633-
}
634-
}}
635-
/>
636566
<Button
637567
title={'Restore backup from server'}
638568
onPress={async (): Promise<void> => {

example/ldk/index.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import {
1111
import lm, {
1212
DefaultTransactionDataShape,
1313
defaultUserConfig,
14-
TAccount,
15-
TAccountBackup,
1614
THeader,
1715
TTransactionData,
1816
TTransactionPosition,
@@ -28,9 +26,7 @@ import {
2826
getAddress,
2927
getNetwork,
3028
ldkNetwork,
31-
setAccount,
3229
} from '../utils/helpers';
33-
import { EAccount } from '../utils/types';
3430
import * as bitcoin from 'bitcoinjs-lib';
3531

3632
/**
@@ -324,42 +320,3 @@ export const broadcastTransaction = async (rawTx: string): Promise<string> => {
324320
return '';
325321
}
326322
};
327-
328-
/**
329-
* Used to backup a given account.
330-
* @param {TAccount} [account]
331-
* @returns {Promise<Result<string>>}
332-
*/
333-
export const backupAccount = async (
334-
account?: TAccount,
335-
): Promise<Result<TAccountBackup>> => {
336-
if (!account) {
337-
account = await getAccount();
338-
}
339-
return await lm.backupAccount({
340-
account,
341-
});
342-
};
343-
344-
/**
345-
* Used to import an account using the backup JSON string or TAccountBackup object.
346-
* @param {string | TAccountBackup} backup
347-
* @returns {Promise<Result<TAccount>>}
348-
*/
349-
export const importAccount = async (
350-
backup: string | TAccountBackup,
351-
forceCloseAllChannels = false,
352-
): Promise<Result<TAccount>> => {
353-
const importResponse = await lm.importAccount({
354-
backup,
355-
overwrite: true,
356-
});
357-
if (importResponse.isErr()) {
358-
return err(importResponse.error.message);
359-
}
360-
await setAccount(importResponse.value);
361-
await setItem(EAccount.currentAccountKey, importResponse.value.name);
362-
await setupLdk(forceCloseAllChannels);
363-
await syncLdk();
364-
return ok(importResponse.value);
365-
};

example/tests/lnd.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,15 @@ describe('LND', function () {
559559
// - force close channel from LDK
560560
// - check everything is ok
561561

562-
let fees = { highPriority: 3, normal: 2, background: 1, mempoolMinimum: 1 };
562+
let fees = {
563+
nonAnchorChannelFee: 5,
564+
anchorChannelFee: 5,
565+
maxAllowedNonAnchorChannelRemoteFee: 5,
566+
channelCloseMinimum: 5,
567+
minAllowedAnchorChannelRemoteFee: 5,
568+
minAllowedNonAnchorChannelRemoteFee: 5,
569+
onChainSweep: 5,
570+
};
563571

564572
const lmStart = await lm.start({
565573
...profile.getStartParams(),
@@ -656,7 +664,15 @@ describe('LND', function () {
656664
);
657665

658666
// set height fees and restart LDK so it catches up
659-
fees = { highPriority: 30, normal: 20, background: 10, mempoolMinimum: 1 };
667+
fees = {
668+
nonAnchorChannelFee: 30,
669+
anchorChannelFee: 30,
670+
maxAllowedNonAnchorChannelRemoteFee: 30,
671+
channelCloseMinimum: 5,
672+
minAllowedAnchorChannelRemoteFee: 5,
673+
minAllowedNonAnchorChannelRemoteFee: 5,
674+
onChainSweep: 30,
675+
};
660676
const syncRes0 = await lm.syncLdk();
661677
await lm.setFees();
662678
if (syncRes0.isErr()) {

example/tests/unit.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ describe('Unit', function () {
5959
getScriptPubKeyHistory: async () => [],
6060
getFees: () => {
6161
return Promise.resolve({
62-
highPriority: 10,
63-
normal: 5,
64-
background: 2,
65-
mempoolMinimum: 1,
62+
nonAnchorChannelFee: 5,
63+
anchorChannelFee: 5,
64+
maxAllowedNonAnchorChannelRemoteFee: 5,
65+
channelCloseMinimum: 5,
66+
minAllowedAnchorChannelRemoteFee: 5,
67+
minAllowedNonAnchorChannelRemoteFee: 5,
68+
onChainSweep: 5,
6669
});
6770
},
6871
getTransactionData: async () => ({

example/utils/helpers.ts

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import Keychain from 'react-native-keychain';
2-
import {
3-
EEventTypes,
4-
TAccount,
5-
TAvailableNetworks,
6-
} from '@synonymdev/react-native-ldk';
7-
import { backupAccount, getItem, importAccount, setItem } from '../ldk';
2+
import { TAccount, TAvailableNetworks } from '@synonymdev/react-native-ldk';
3+
import { getItem, setItem } from '../ldk';
84
import { EAccount } from './types';
95
import { err, ok, Result } from './result';
106
import { randomBytes } from 'react-native-randombytes';
@@ -15,8 +11,6 @@ import * as bip32 from 'bip32';
1511
import * as bip39 from 'bip39';
1612
import { ENetworks } from '@synonymdev/react-native-ldk/dist/utils/types';
1713
import networks from '@synonymdev/react-native-ldk/dist/utils/networks';
18-
import ldk from '@synonymdev/react-native-ldk/dist/ldk';
19-
import Clipboard from '@react-native-clipboard/clipboard';
2014

2115
/**
2216
* Use Keychain to save LDK name & seed.
@@ -216,78 +210,3 @@ export const ldkNetwork = (network: TAvailableNetworks): ENetworks => {
216210
return ENetworks.signet;
217211
}
218212
};
219-
220-
export const simulateStaleRestore = async (
221-
onUpdate: (string) => void,
222-
): Promise<void> => {
223-
const channels = await ldk.listChannels();
224-
if (channels.isErr()) {
225-
throw channels.error;
226-
}
227-
if (channels.value.filter((c) => c.is_usable).length === 0) {
228-
throw new Error('No usable channels. Open a channel first.');
229-
}
230-
231-
onUpdate('Backing up...');
232-
const backupResponse = await backupAccount();
233-
if (backupResponse.isErr()) {
234-
throw backupResponse.error;
235-
}
236-
237-
const timeoutSeconds = 30;
238-
const invoice = await ldk.createPaymentRequest({
239-
amountSats: 12,
240-
description: 'crash test',
241-
expiryDeltaSeconds: timeoutSeconds,
242-
});
243-
if (invoice.isErr()) {
244-
throw invoice.error;
245-
}
246-
247-
let paymentClaimed = false;
248-
let paymentSubscription = ldk.onEvent(
249-
EEventTypes.channel_manager_payment_claimed,
250-
() => (paymentClaimed = true),
251-
);
252-
253-
Clipboard.setString(invoice.value.to_str);
254-
255-
//Keep checking if we got the payment
256-
for (let i = 0; i < timeoutSeconds; i++) {
257-
onUpdate(
258-
`Please pay invoice in clipboard to continue (${timeoutSeconds - i})...`,
259-
);
260-
261-
if (paymentClaimed) {
262-
onUpdate('Payment claimed! Testing stale restore...');
263-
break;
264-
}
265-
266-
await new Promise((resolve) => setTimeout(resolve, 1000));
267-
}
268-
269-
paymentSubscription.remove();
270-
271-
if (!paymentClaimed) {
272-
throw new Error('No payment claimed. Timeout out.');
273-
}
274-
275-
onUpdate('Importing stale backup and force closing all channels...');
276-
277-
await new Promise((resolve) => setTimeout(resolve, 2500));
278-
279-
await ldk.stop();
280-
const forceCloseAllChannels = true; //To test the crash restore set to false
281-
const importResponse = await importAccount(
282-
backupResponse.value,
283-
forceCloseAllChannels,
284-
);
285-
if (importResponse.isErr()) {
286-
throw importResponse.error;
287-
}
288-
289-
await new Promise((resolve) => setTimeout(resolve, 2500));
290-
onUpdate(
291-
"If this didn't crash and you can see your claimable balance, you're good!",
292-
);
293-
};

lib/src/lightning-manager.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ class LightningManager {
148148
private pendingStartPromises: Array<(result: Result<string>) => void> = [];
149149
private previousAccountName: string = '';
150150

151+
private backupsServerSetup = false;
152+
151153
constructor() {
152154
// Step 0: Subscribe to all events
153155
ldk.onEvent(EEventTypes.native_log, (line) => {
@@ -447,6 +449,7 @@ class LightningManager {
447449
details: backupServerDetails,
448450
}),
449451
);
452+
this.backupsServerSetup = true;
450453
}
451454

452455
// Check for any errors before starting channel manager.
@@ -984,7 +987,7 @@ class LightningManager {
984987
overwrite?: boolean;
985988
}): Promise<Result<TAccount>> => {
986989
console.warn(
987-
'This method is deprecated and should only be used to import accounts that were backed up with a previous async method.',
990+
'importAccount() is deprecated and should only be used to import accounts that were backed up with a previous async method.',
988991
);
989992
if (!this.baseStoragePath) {
990993
return err(
@@ -1148,6 +1151,9 @@ class LightningManager {
11481151
account: TAccount;
11491152
includeTransactionHistory?: boolean;
11501153
}): Promise<Result<TAccountBackup>> => {
1154+
console.warn(
1155+
'backupAccount() is deprecated and will be removed in future versions. Use remote backup server instead.',
1156+
);
11511157
if (!this.baseStoragePath) {
11521158
return err(
11531159
'baseStoragePath required for wallet persistence. Call setBaseStoragePath(path) first.',
@@ -1350,6 +1356,9 @@ class LightningManager {
13501356
subscribeToBackups(
13511357
callback: (backup: Result<TAccountBackup>) => void,
13521358
): string {
1359+
console.warn(
1360+
'subscribeToBackups() is deprecated, use backup server instead.',
1361+
);
13531362
this.backupSubscriptionsId++;
13541363
const id = `${this.backupSubscriptionsId}`;
13551364
this.backupSubscriptions[id] = callback;
@@ -1361,6 +1370,9 @@ class LightningManager {
13611370
* @param id
13621371
*/
13631372
unsubscribeFromBackups(id: string): void {
1373+
console.warn(
1374+
'unsubscribeFromBackups() is deprecated, use backup server instead.',
1375+
);
13641376
if (this.backupSubscriptions[id]) {
13651377
delete this.backupSubscriptions[id];
13661378
}
@@ -1372,6 +1384,9 @@ class LightningManager {
13721384
* @returns {Promise<void>}
13731385
*/
13741386
private async onLdkBackupEvent(): Promise<void> {
1387+
if (this.backupsServerSetup) {
1388+
return;
1389+
}
13751390
if (this.backupSubscriptionsDebounceTimer) {
13761391
clearTimeout(this.backupSubscriptionsDebounceTimer);
13771392
}

0 commit comments

Comments
 (0)