Merge pull request #6239 from BlueWallet/realmtmp

FIX: Change default directory for cache data
This commit is contained in:
GLaDOS 2024-04-12 21:02:36 +00:00 committed by GitHub
commit c988c192b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 3 deletions

View File

@ -31,6 +31,7 @@ import { randomBytes } from './class/rng';
import { TWallet, Transaction } from './class/wallets/types';
import presentAlert from './components/Alert';
import prompt from './helpers/prompt';
import RNFS from 'react-native-fs';
import loc from './loc';
let usedBucketNum: boolean | number = false;
@ -233,10 +234,12 @@ export class AppStorage {
* Database file is deterministically derived from encryption key.
*/
async getRealm() {
const cacheFolderPath = RNFS.CachesDirectoryPath; // Path to cache folder
const password = this.hashIt(this.cachedPassword || 'fyegjitkyf[eqjnc.lf');
const buf = Buffer.from(this.hashIt(password) + this.hashIt(password), 'hex');
const encryptionKey = Int8Array.from(buf);
const path = this.hashIt(this.hashIt(password)) + '-wallettransactions.realm';
const fileName = this.hashIt(this.hashIt(password)) + '-wallettransactions.realm';
const path = `${cacheFolderPath}/${fileName}`; // Use cache folder path
const schema = [
{
@ -265,6 +268,7 @@ export class AppStorage {
* @returns {Promise<Realm>}
*/
async openRealmKeyValue(): Promise<Realm> {
const cacheFolderPath = RNFS.CachesDirectoryPath; // Path to cache folder
const service = 'realm_encryption_key';
let password;
const credentials = await Keychain.getGenericPassword({ service });
@ -278,7 +282,7 @@ export class AppStorage {
const buf = Buffer.from(password, 'hex');
const encryptionKey = Int8Array.from(buf);
const path = 'keyvalue.realm';
const path = `${cacheFolderPath}/keyvalue.realm`; // Use cache folder path
const schema = [
{
@ -320,6 +324,7 @@ export class AppStorage {
* @returns {Promise.<boolean>}
*/
async loadFromDisk(password?: string): Promise<boolean> {
await this.moveRealmFilesToCacheDirectory();
let data = await this.getItemWithFallbackToRealm('data');
if (password) {
data = this.decryptData(data, password);
@ -893,6 +898,34 @@ export class AppStorage {
path,
});
}
async moveRealmFilesToCacheDirectory() {
const documentPath = RNFS.DocumentDirectoryPath; // Path to documentPath folder
const cachePath = RNFS.CachesDirectoryPath; // Path to cachePath folder
try {
if (!(await RNFS.exists(documentPath))) return; // If the documentPath directory does not exist, return (nothing to move)
const files = await RNFS.readDir(documentPath); // Read all files in documentPath directory
if (Array.isArray(files) && files.length === 0) return; // If there are no files, return (nothing to move)
const appRealmFiles = files.filter(
file => file.name.endsWith('.realm') || file.name.endsWith('.realm.lock') || file.name.includes('.realm.management'),
);
for (const file of appRealmFiles) {
const filePath = `${documentPath}/${file.name}`;
const newFilePath = `${cachePath}/${file.name}`;
const fileExists = await RNFS.exists(filePath); // Check if the file exists
if (fileExists) {
await RNFS.moveFile(filePath, newFilePath); // Move the file if it exists
console.log(`Moved Realm file: ${filePath} to ${newFilePath}`);
} else {
console.log(`File does not exist: ${filePath}`);
}
}
} catch (error) {
console.error('Error moving Realm files:', error);
throw new Error(`Error moving Realm files: ${(error as Error).message}`);
}
}
}
const BlueApp = new AppStorage();

View File

@ -8,6 +8,7 @@ import { LegacyWallet, SegwitBech32Wallet, SegwitP2SHWallet, TaprootWallet } fro
import presentAlert from '../components/Alert';
import loc from '../loc';
import { reloadAllTimelines } from '../components/WidgetCommunication';
import RNFS from 'react-native-fs';
const ElectrumClient = require('electrum-client');
const net = require('net');
@ -108,10 +109,11 @@ let _realm: Realm | undefined;
async function _getRealm() {
if (_realm) return _realm;
const cacheFolderPath = RNFS.CachesDirectoryPath; // Path to cache folder
const password = bitcoin.crypto.sha256(Buffer.from('fyegjitkyf[eqjnc.lf')).toString('hex');
const buf = Buffer.from(password + password, 'hex');
const encryptionKey = Int8Array.from(buf);
const path = 'electrumcache.realm';
const path = `${cacheFolderPath}/electrumcache.realm`; // Use cache folder path
const schema = [
{
@ -130,6 +132,7 @@ async function _getRealm() {
path,
encryptionKey,
});
return _realm;
}