Skip to content

Commit

Permalink
feat: ios bearer token auth for restoring backups
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonvdb committed Sep 28, 2023
1 parent 467fcba commit 9d9de90
Show file tree
Hide file tree
Showing 9 changed files with 412 additions and 219 deletions.
6 changes: 3 additions & 3 deletions backup-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ fastify.route({

const bearer = crypto.randomBytes(32).toString('hex');

//Valid for 30min, should only be used for doing a restore
const expires = Date.now() + 30 * 60 * 1000;
//Valid for 5min, should only be used for doing a restore
const expires = Date.now() + 5 * 60 * 1000;
users.set(bearer, {pubkey, expires});

return {bearer, expires};
Expand Down Expand Up @@ -278,7 +278,7 @@ fastify.route({
const {pubkey} = users.get(bearerToken);

const list = await storage.list({pubkey, network});
const channelMonitorList = storage.list({pubkey, network, subdir: 'channel_monitors'});
const channelMonitorList = await storage.list({pubkey, network, subdir: 'channel_monitors'});

const allFiles = {
list,
Expand Down
20 changes: 16 additions & 4 deletions example/Dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
syncLdk,
getAddressBalance,
updateHeader,
getBackupServerDetails,
} from './ldk';
import { connectToElectrum, subscribeToHeader } from './electrum';
import ldk from '@synonymdev/react-native-ldk/dist/ldk';
Expand All @@ -30,7 +29,7 @@ import lm, {
TChannelManagerPaymentPathSuccessful,
TChannelUpdate,
} from '@synonymdev/react-native-ldk';
import { peers } from './utils/constants';
import { backupServerDetails, peers } from './utils/constants';
import {
createNewAccount,
getAccount,
Expand Down Expand Up @@ -640,10 +639,9 @@ const Dev = (): ReactElement => {
await ldk.stop();

const account = await getAccount();
const serverDetails = await getBackupServerDetails();
const restoreRes = await lm.restoreFromRemoteServer({
account,
serverDetails,
serverDetails: backupServerDetails,
overwrite: true,
});

Expand All @@ -655,6 +653,20 @@ const Dev = (): ReactElement => {
setMessage('Successfully restored wallet');
}}
/>

<Button
title={'Backup self check'}
onPress={async (): Promise<void> => {
setMessage('Checking...');
const backupCheckRes = await ldk.backupSelfCheck();
if (backupCheckRes.isErr()) {
console.error('Backup check failed', backupCheckRes.error);
setMessage(backupCheckRes.error.message);
}
setMessage('Backup server check passed ✅');
}}
/>

<Button
title={'Restart node'}
onPress={async (): Promise<void> => {
Expand Down
20 changes: 6 additions & 14 deletions example/ldk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import lm, {
defaultUserConfig,
TAccount,
TAccountBackup,
TBackupServerDetails,
THeader,
TTransactionData,
TTransactionPosition,
} from '@synonymdev/react-native-ldk';
import ldk from '@synonymdev/react-native-ldk/dist/ldk';
import { backupServer, peers, selectedNetwork } from '../utils/constants';
import {
backupServerDetails,
peers,
selectedNetwork,
} from '../utils/constants';
import {
getAccount,
getAddress,
Expand Down Expand Up @@ -89,17 +92,6 @@ export const syncLdk = async (): Promise<Result<string>> => {
return syncResponse;
};

export const getBackupServerDetails =
async (): Promise<TBackupServerDetails> => {
const backupServerDetails: TBackupServerDetails = {
url: backupServer,
token:
'c14813666b79958e2ba830172bf3f4bca78c4782e5c8d3933237728cec2df8643fe0f6d540e6e92030148dcda578e70f8e93338f1f004ac38ea2b673d7f4224c',
};

return backupServerDetails;
};

/**
* Used to spin-up LDK services.
* In order, this method:
Expand Down Expand Up @@ -149,7 +141,7 @@ export const setupLdk = async (
manually_accept_inbound_channels: true,
},
trustedZeroConfPeers: [peers.lnd.pubKey],
backupServerDetails: await getBackupServerDetails(),
backupServerDetails,
});

if (lmStart.isErr()) {
Expand Down
Loading

0 comments on commit 9d9de90

Please sign in to comment.