forked from bitcoinvault/GoldWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalletMigrate.js
More file actions
112 lines (105 loc) · 4.14 KB
/
walletMigrate.js
File metadata and controls
112 lines (105 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import AsyncStorage from '@react-native-community/async-storage';
import RNFS from 'react-native-fs';
import RNSecureKeyStore, { ACCESSIBLE } from 'react-native-secure-key-store';
export default class WalletMigrate {
static expoDataDirectory =
RNFS.DocumentDirectoryPath + '/ExponentExperienceData/%40overtorment%2Fbluewallet/RCTAsyncLocalStorage';
constructor(onComplete) {
this.onComplete = onComplete;
}
// 0: Let's start!
async start() {
return this.migrateDataFromExpo();
}
// 1: Migrate Document directory from Expo
async migrateDataFromExpo() {
const expoDirectoryExists = await RNFS.exists(RNFS.DocumentDirectoryPath + '/ExponentExperienceData');
if (!expoDirectoryExists) {
console.log('Expo data was previously migrated. Exiting migration...');
await this.migrateDataToSecureKeystore();
return;
}
try {
await RNFS.unlink(RNFS.DocumentDirectoryPath + '/RCTAsyncLocalStorage_V1');
console.log('/RCTAsyncLocalStorage_V1 has been deleted. Continuing...');
} catch (error) {
console.log(error);
console.log('/RCTAsyncLocalStorage_V1 does not exist. Continuing...');
}
try {
await RNFS.copyFile(WalletMigrate.expoDataDirectory, RNFS.DocumentDirectoryPath + '/RCTAsyncLocalStorage_V1');
} catch (error) {
console.log(
'An error was encountered when trying to copy Expo data to /RCTAsyncLocalStorage_V1. Exiting migration...',
);
console.log(error);
}
try {
await RNFS.unlink(RNFS.DocumentDirectoryPath + '/RCTAsyncLocalStorage_V1/.DS_Store');
} catch (error) {
console.log('An error was encountered when trying to delete .DS_Store. Continuing migration...');
console.log(error);
}
const files = await RNFS.readDir(WalletMigrate.expoDataDirectory);
for (const file of files) {
try {
if (file.isFile()) {
if (file.name === 'manifest.json') {
const manifestFile = await RNFS.readFile(file.path);
const manifestFileParsed = JSON.parse(manifestFile);
if (manifestFileParsed.hasOwnProperty('data')) {
if (typeof manifestFileParsed.data === 'string') {
await AsyncStorage.setItem('data', manifestFileParsed.data);
}
}
if (manifestFileParsed.hasOwnProperty('data_encrypted')) {
if (typeof manifestFileParsed.data_encrypted === 'string') {
await AsyncStorage.setItem('data_encrypted', manifestFileParsed.data_encrypted);
}
}
} else if (file.name !== 'manifest.json') {
const manifestFile = await RNFS.readFile(file.path);
const manifestFileParsed = JSON.parse(manifestFile);
if (typeof manifestFileParsed === 'object')
await AsyncStorage.setItem('data', JSON.stringify(manifestFileParsed));
}
}
} catch (error) {
console.log(error);
}
}
try {
await RNFS.unlink(RNFS.DocumentDirectoryPath + '/ExponentExperienceData');
console.log('Deleted /ExponentExperienceData.');
} catch (error) {
console.log('An error was encountered when trying to delete /ExponentExperienceData. Exiting migration...');
console.log(error);
}
await this.migrateDataToSecureKeystore();
}
// 2: Migrate Data from AsyncStorage to RNSecureKeyStore
async migrateDataToSecureKeystore() {
try {
const data = await AsyncStorage.getItem('data');
if (data) {
const isEncrypted = (await AsyncStorage.getItem('data_encrypted')) || '';
await RNSecureKeyStore.set('data', data, {
accessible: ACCESSIBLE.WHEN_UNLOCKED,
});
await RNSecureKeyStore.set('data_encrypted', isEncrypted, {
accessible: ACCESSIBLE.WHEN_UNLOCKED,
});
await AsyncStorage.removeItem('data');
await AsyncStorage.removeItem('data_encrypted');
}
} catch (_e) {
console.log('Nothing to migrate from AsyncStorage.');
}
this.migrationComplete();
}
// 3: We're done!
migrationComplete() {
console.log('Migration was successful. Exiting migration...');
this.onComplete();
}
}