|
| 1 | +import { Alert, Platform } from 'react-native'; |
| 2 | +import RNFS from 'react-native-fs'; |
| 3 | +import Share from 'react-native-share'; |
| 4 | +import RNRestart from 'react-native-restart'; |
| 5 | +import { localeString } from './LocaleUtils'; |
| 6 | + |
| 7 | +import Storage from '../storage'; |
| 8 | + |
| 9 | +export const CHANNEL_MIGRATION_ACTIVE = 'channel_migration_active'; |
| 10 | + |
| 11 | +export const getChannelDbPath = async ( |
| 12 | + lndDir: string, |
| 13 | + isTestnet: boolean, |
| 14 | + isSqlite: boolean |
| 15 | +) => { |
| 16 | + const network = isTestnet ? 'testnet' : 'mainnet'; |
| 17 | + const rootPath = Platform.select({ |
| 18 | + android: RNFS.DocumentDirectoryPath, |
| 19 | + ios: `${RNFS.LibraryDirectoryPath}/Application Support` |
| 20 | + }); |
| 21 | + const basePath = `${rootPath}/${lndDir}/data/graph/${network}`; |
| 22 | + const fileName = isSqlite ? 'channel.sqlite' : 'channel.db'; |
| 23 | + const fullPath = `${basePath}/${fileName}`; |
| 24 | + |
| 25 | + if (await RNFS.exists(fullPath)) { |
| 26 | + return { path: fullPath, type: isSqlite ? 'sqlite' : 'bolt' }; |
| 27 | + } |
| 28 | + return null; |
| 29 | +}; |
| 30 | + |
| 31 | +export const exportChannelDb = async ( |
| 32 | + lndDir: string | any, |
| 33 | + isTestnet: boolean, |
| 34 | + isSqlite: boolean, |
| 35 | + setLoading?: (loading: boolean) => void |
| 36 | +) => { |
| 37 | + try { |
| 38 | + if (setLoading) setLoading(true); |
| 39 | + |
| 40 | + const dbPath = await getChannelDbPath(lndDir, isTestnet, isSqlite); |
| 41 | + |
| 42 | + if (!dbPath) { |
| 43 | + Alert.alert( |
| 44 | + localeString('general.error'), |
| 45 | + localeString('views.Tools.migration.databaseNotFound') |
| 46 | + ); |
| 47 | + if (setLoading) setLoading(false); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const { path, type } = dbPath; |
| 52 | + const extension = type === 'sqlite' ? 'sqlite' : 'db'; |
| 53 | + const backupFileName = `zeus-channels-${ |
| 54 | + isTestnet ? 'testnet' : 'mainnet' |
| 55 | + }-${Date.now()}.${extension}`; |
| 56 | + const stagingPath = `${RNFS.DocumentDirectoryPath}/${backupFileName}`; |
| 57 | + |
| 58 | + if (await RNFS.exists(stagingPath)) { |
| 59 | + await RNFS.unlink(stagingPath); |
| 60 | + } |
| 61 | + |
| 62 | + await RNFS.copyFile(path, stagingPath); |
| 63 | + |
| 64 | + if (setLoading) setLoading(false); |
| 65 | + |
| 66 | + console.log('Opening Share Sheet...'); |
| 67 | + |
| 68 | + const shareResult = await Share.open({ |
| 69 | + title: localeString('views.Tools.migration.export.title'), |
| 70 | + url: `file://${stagingPath}`, |
| 71 | + type: 'application/octet-stream', |
| 72 | + filename: backupFileName, |
| 73 | + failOnCancel: false |
| 74 | + }); |
| 75 | + |
| 76 | + const isDismissed = |
| 77 | + shareResult.dismissedAction || shareResult.success === false; |
| 78 | + |
| 79 | + if (isDismissed) { |
| 80 | + await RNFS.unlink(stagingPath); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + console.log(' Export successful. Locking wallet...'); |
| 85 | + |
| 86 | + await Storage.setItem( |
| 87 | + CHANNEL_MIGRATION_ACTIVE, |
| 88 | + JSON.stringify({ migrationStatus: true, lndDir: lndDir }) |
| 89 | + ); |
| 90 | + await RNFS.unlink(stagingPath); |
| 91 | + |
| 92 | + Alert.alert( |
| 93 | + localeString('views.Tools.migration.export.success'), |
| 94 | + localeString('views.Tools.migration.export.success.text'), |
| 95 | + [ |
| 96 | + { |
| 97 | + text: localeString('views.Wallet.restart'), |
| 98 | + onPress: () => RNRestart.Restart() |
| 99 | + } |
| 100 | + ], |
| 101 | + { cancelable: false } |
| 102 | + ); |
| 103 | + } catch (error) { |
| 104 | + console.error('Export Failed:', error); |
| 105 | + if (setLoading) setLoading(false); |
| 106 | + Alert.alert(localeString('general.error')); |
| 107 | + RNRestart.Restart(); |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +export const restoreChannels = async ( |
| 112 | + sourceUri: string, |
| 113 | + fileName: string, |
| 114 | + lndDir: string, |
| 115 | + isTestnet: boolean, |
| 116 | + setLoading?: (loading: boolean) => void |
| 117 | +) => { |
| 118 | + try { |
| 119 | + if (setLoading) setLoading(true); |
| 120 | + |
| 121 | + console.log(`Restoring from: ${sourceUri}`); |
| 122 | + |
| 123 | + const isSqliteBackup = fileName.toLowerCase().endsWith('.sqlite'); |
| 124 | + const dbName = isSqliteBackup ? 'channel.sqlite' : 'channel.db'; |
| 125 | + |
| 126 | + const rootPath = Platform.select({ |
| 127 | + android: RNFS.DocumentDirectoryPath, |
| 128 | + ios: `${RNFS.LibraryDirectoryPath}/Application Support` |
| 129 | + }); |
| 130 | + |
| 131 | + const destFolder = `${rootPath}/${lndDir}/data/graph/${ |
| 132 | + isTestnet ? 'testnet' : 'mainnet' |
| 133 | + }`; |
| 134 | + const destPath = `${destFolder}/${dbName}`; |
| 135 | + |
| 136 | + console.log(`Target Destination: ${destPath}`); |
| 137 | + |
| 138 | + if (await RNFS.exists(destPath)) { |
| 139 | + await RNFS.unlink(destPath); |
| 140 | + } |
| 141 | + await RNFS.copyFile(sourceUri, destPath); |
| 142 | + |
| 143 | + if (setLoading) setLoading(false); |
| 144 | + |
| 145 | + Alert.alert( |
| 146 | + localeString('views.Tools.nodeConfigExportImport.importSuccess'), |
| 147 | + `${localeString( |
| 148 | + 'views.Tools.migration.import.success.text1' |
| 149 | + )}\n\n` + |
| 150 | + `ⓘ ${localeString( |
| 151 | + 'views.Tools.migration.import.success.text2' |
| 152 | + )}`, |
| 153 | + [ |
| 154 | + { |
| 155 | + text: localeString('views.Wallet.restart'), |
| 156 | + onPress: () => RNRestart.Restart() |
| 157 | + } |
| 158 | + ], |
| 159 | + { cancelable: false } |
| 160 | + ); |
| 161 | + } catch (error) { |
| 162 | + console.error('Import Failed:', error); |
| 163 | + if (setLoading) setLoading(false); |
| 164 | + Alert.alert(localeString('general.error')); |
| 165 | + } |
| 166 | +}; |
0 commit comments