mirror of
https://github.com/mosip/inji-wallet.git
synced 2026-01-09 21:48:04 -05:00
INJIMOB-898 Remove Metro warnings (#1329)
* [INJIMOB-898] remove telemetry dependency from crypto utils Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com> * [INJIMOB-898] remove vcMetadata dependency from utils file Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com> * [INJIMOB-898] move common props key to constants Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com> * [INJIMOB-898] remove vcUtils dependency from vcVerification Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] remove profileIcon dependency from svg Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] remove vcItemFeild dependecy from vcUtils Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] remove vc dependecy from esignetVcItem and existing vcItem Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] remove storage dependency from api.ts Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] remove store dependency from storage Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] remove commom props dependency from api.ts Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] remove main.ts dependency from scanLayout Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] move VcItemConatinerProfileImage to different component Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] throw error when error is ENOENT and spell check Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] rename types to routeTypes and edit imports Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> * [INJIMOB-898] move to idle state on tamperedVCs state Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> --------- Signed-off-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> Signed-off-by: PoojaBabusing <115976560+PoojaBabusing@users.noreply.github.com> Co-authored-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com> Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {MMKVLoader} from 'react-native-mmkv-storage';
|
||||
import getAllConfigurations from './commonprops/commonProps';
|
||||
import getAllConfigurations from './api';
|
||||
import {
|
||||
getFreeDiskStorageOldSync,
|
||||
getFreeDiskStorageSync,
|
||||
@@ -13,12 +13,12 @@ import {
|
||||
isHardwareKeystoreExists,
|
||||
} from './cryptoutil/cryptoUtil';
|
||||
import {VCMetadata} from './VCMetadata';
|
||||
import {ENOENT, removeTamperedVcMetaData} from '../machines/store';
|
||||
import {
|
||||
androidVersion,
|
||||
isAndroid,
|
||||
MY_VCS_STORE_KEY,
|
||||
SETTINGS_STORE_KEY,
|
||||
ENOENT,
|
||||
} from './constants';
|
||||
import FileStorage, {
|
||||
getFilePath,
|
||||
@@ -35,14 +35,6 @@ import {DocumentDirectoryPath, ReadDirItem} from 'react-native-fs';
|
||||
|
||||
export const MMKV = new MMKVLoader().initialize();
|
||||
|
||||
export const API_CACHED_STORAGE_KEYS = {
|
||||
fetchIssuers: 'CACHE_FETCH_ISSUERS',
|
||||
fetchIssuerConfig: (issuerId: string) =>
|
||||
`CACHE_FETCH_ISSUER_CONFIG_${issuerId}`,
|
||||
fetchIssuerWellknownConfig: (issuerId: string) =>
|
||||
`CACHE_FETCH_ISSUER_WELLKNOWN_CONFIG_${issuerId}`,
|
||||
};
|
||||
|
||||
async function generateHmac(
|
||||
encryptionKey: string,
|
||||
data: string,
|
||||
@@ -223,7 +215,6 @@ class Storage {
|
||||
);
|
||||
|
||||
if (isDownloaded && error.message.includes(ENOENT)) {
|
||||
removeTamperedVcMetaData(key, encryptionKey);
|
||||
sendErrorEvent(
|
||||
getErrorEventData(
|
||||
TelemetryConstants.FlowType.fetchData,
|
||||
@@ -238,11 +229,11 @@ class Storage {
|
||||
getErrorEventData(
|
||||
TelemetryConstants.FlowType.fetchData,
|
||||
TelemetryConstants.ErrorId.dataRetrieval,
|
||||
'Error Occurred while retriving from Storage',
|
||||
'Error Occurred while retrieving from Storage',
|
||||
),
|
||||
);
|
||||
|
||||
console.log('Error Occurred while retriving from Storage.', error);
|
||||
console.log('Error Occurred while retrieving from Storage.', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
@@ -253,98 +244,133 @@ class Storage {
|
||||
* @param cutOffTimestamp the timestamp of the VC backup start time to current time
|
||||
*/
|
||||
private static async unloadVCs(encryptionKey: any, cutOffTimestamp: number) {
|
||||
// 1. Find the VCs in the inji directory which have the said timestamp
|
||||
const file: ReadDirItem[] = await fileStorage.getAllFilesInDirectory(
|
||||
`${DocumentDirectoryPath}/inji/VC/`,
|
||||
);
|
||||
const isGreaterThanEq = function (fName: string, ts: number): boolean {
|
||||
fName = fName.split('.')[0];
|
||||
const curr = fName.split('_')[1];
|
||||
return parseInt(curr) >= ts;
|
||||
};
|
||||
for (let i = 0; i < file.length; i++) {
|
||||
const f = file[i];
|
||||
if (isGreaterThanEq(f.name, cutOffTimestamp)) {
|
||||
await fileStorage.removeItem(f.path);
|
||||
}
|
||||
}
|
||||
// TODO: should this be done via the Store state machine to avoid popups?
|
||||
// 3. Remove the keys from MMKV which have the same timestamp
|
||||
let myVCsEnc = await MMKV.getItem(MY_VCS_STORE_KEY, encryptionKey);
|
||||
if (myVCsEnc !== null) {
|
||||
let mmkvVCs = await decryptJson(encryptionKey, myVCsEnc);
|
||||
let vcList: VCMetadata[] = JSON.parse(mmkvVCs);
|
||||
let newVCList: VCMetadata[] = [];
|
||||
vcList.forEach(d => {
|
||||
if (d.timestamp && parseInt(d.timestamp) < cutOffTimestamp) {
|
||||
newVCList.push(d);
|
||||
}
|
||||
});
|
||||
const finalVC = await encryptJson(
|
||||
encryptionKey,
|
||||
JSON.stringify(newVCList),
|
||||
try {
|
||||
// 1. Find the VCs in the inji directory which have the said timestamp
|
||||
const file: ReadDirItem[] = await fileStorage.getAllFilesInDirectory(
|
||||
`${DocumentDirectoryPath}/inji/VC/`,
|
||||
);
|
||||
const isGreaterThanEq = function (fName: string, ts: number): boolean {
|
||||
fName = fName.split('.')[0];
|
||||
const curr = fName.split('_')[1];
|
||||
return parseInt(curr) >= ts;
|
||||
};
|
||||
for (let i = 0; i < file.length; i++) {
|
||||
const f = file[i];
|
||||
if (isGreaterThanEq(f.name, cutOffTimestamp)) {
|
||||
await fileStorage.removeItem(f.path);
|
||||
}
|
||||
}
|
||||
// TODO: should this be done via the Store state machine to avoid popups?
|
||||
// 3. Remove the keys from MMKV which have the same timestamp
|
||||
let myVCsEnc = await MMKV.getItem(MY_VCS_STORE_KEY, encryptionKey);
|
||||
if (myVCsEnc !== null) {
|
||||
let mmkvVCs = await decryptJson(encryptionKey, myVCsEnc);
|
||||
let vcList: VCMetadata[] = JSON.parse(mmkvVCs);
|
||||
let newVCList: VCMetadata[] = [];
|
||||
vcList.forEach(d => {
|
||||
if (d.timestamp && parseInt(d.timestamp) < cutOffTimestamp) {
|
||||
newVCList.push(d);
|
||||
}
|
||||
});
|
||||
const finalVC = await encryptJson(
|
||||
encryptionKey,
|
||||
JSON.stringify(newVCList),
|
||||
);
|
||||
await MMKV.setItem(MY_VCS_STORE_KEY, finalVC);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('error while unloadVcs:', e);
|
||||
sendErrorEvent(
|
||||
getErrorEventData(
|
||||
TelemetryConstants.FlowType.dataRestore,
|
||||
TelemetryConstants.ErrorId.failure,
|
||||
e.message,
|
||||
),
|
||||
);
|
||||
await MMKV.setItem(MY_VCS_STORE_KEY, finalVC);
|
||||
}
|
||||
}
|
||||
|
||||
private static async loadVCs(completeBackupData: {}, encryptionKey: any) {
|
||||
const allVCs = completeBackupData['VC_Records'];
|
||||
const allVCKeys = Object.keys(allVCs);
|
||||
const dataFromDB = completeBackupData['dataFromDB'];
|
||||
// 0. Check for VC presense in the store
|
||||
// 1. store the VCs and the HMAC
|
||||
allVCKeys.forEach(async key => {
|
||||
let vc = allVCs[key];
|
||||
const ts = Date.now();
|
||||
const prevUnixTimeStamp = vc.vcMetadata.timestamp;
|
||||
vc.vcMetadata.timestamp = ts;
|
||||
dataFromDB.myVCs.forEach(myVcMetadata => {
|
||||
if (
|
||||
myVcMetadata.requestId === vc.vcMetadata.requestId &&
|
||||
myVcMetadata.timestamp === prevUnixTimeStamp
|
||||
) {
|
||||
myVcMetadata.timestamp = ts;
|
||||
}
|
||||
try {
|
||||
const allVCs = completeBackupData['VC_Records'];
|
||||
const allVCKeys = Object.keys(allVCs);
|
||||
const dataFromDB = completeBackupData['dataFromDB'];
|
||||
// 0. Check for VC presense in the store
|
||||
// 1. store the VCs and the HMAC
|
||||
allVCKeys.forEach(async key => {
|
||||
let vc = allVCs[key];
|
||||
const ts = Date.now();
|
||||
const prevUnixTimeStamp = vc.vcMetadata.timestamp;
|
||||
vc.vcMetadata.timestamp = ts;
|
||||
dataFromDB.myVCs.forEach(myVcMetadata => {
|
||||
if (
|
||||
myVcMetadata.requestId === vc.vcMetadata.requestId &&
|
||||
myVcMetadata.timestamp === prevUnixTimeStamp
|
||||
) {
|
||||
myVcMetadata.timestamp = ts;
|
||||
}
|
||||
});
|
||||
const updatedVcKey = new VCMetadata(vc.vcMetadata).getVcKey();
|
||||
const encryptedVC = await encryptJson(
|
||||
encryptionKey,
|
||||
JSON.stringify(vc),
|
||||
);
|
||||
const tmp = VCMetadata.fromVC(key);
|
||||
// Save the VC to disk
|
||||
await this.setItem(updatedVcKey, encryptedVC, encryptionKey);
|
||||
});
|
||||
const updatedVcKey = new VCMetadata(vc.vcMetadata).getVcKey();
|
||||
const encryptedVC = await encryptJson(encryptionKey, JSON.stringify(vc));
|
||||
const tmp = VCMetadata.fromVC(key);
|
||||
// Save the VC to disk
|
||||
await this.setItem(updatedVcKey, encryptedVC, encryptionKey);
|
||||
});
|
||||
// 2. Update myVCsKey
|
||||
const dataFromMyVCKey = dataFromDB[MY_VCS_STORE_KEY];
|
||||
const encryptedMyVCKeyFromMMKV = await MMKV.getItem(MY_VCS_STORE_KEY);
|
||||
let newDataForMyVCKey: VCMetadata[] = [];
|
||||
if (encryptedMyVCKeyFromMMKV != null) {
|
||||
const myVCKeyFromMMKV = await decryptJson(
|
||||
// 2. Update myVCsKey
|
||||
const dataFromMyVCKey = dataFromDB[MY_VCS_STORE_KEY];
|
||||
const encryptedMyVCKeyFromMMKV = await MMKV.getItem(MY_VCS_STORE_KEY);
|
||||
let newDataForMyVCKey: VCMetadata[] = [];
|
||||
if (encryptedMyVCKeyFromMMKV != null) {
|
||||
const myVCKeyFromMMKV = await decryptJson(
|
||||
encryptionKey,
|
||||
encryptedMyVCKeyFromMMKV,
|
||||
);
|
||||
newDataForMyVCKey = [
|
||||
...JSON.parse(myVCKeyFromMMKV),
|
||||
...dataFromMyVCKey,
|
||||
];
|
||||
} else {
|
||||
newDataForMyVCKey = dataFromMyVCKey;
|
||||
}
|
||||
const encryptedDataForMyVCKey = await encryptJson(
|
||||
encryptionKey,
|
||||
encryptedMyVCKeyFromMMKV,
|
||||
JSON.stringify(newDataForMyVCKey),
|
||||
);
|
||||
await this.setItem(
|
||||
MY_VCS_STORE_KEY,
|
||||
encryptedDataForMyVCKey,
|
||||
encryptionKey,
|
||||
);
|
||||
await fileStorage.removeItemIfExist(`${DocumentDirectoryPath}/.prev`);
|
||||
return dataFromDB;
|
||||
} catch (e) {
|
||||
console.error('error while loading Vcs:', e);
|
||||
sendErrorEvent(
|
||||
getErrorEventData(
|
||||
TelemetryConstants.FlowType.dataBackup,
|
||||
TelemetryConstants.ErrorId.failure,
|
||||
e.message,
|
||||
),
|
||||
);
|
||||
newDataForMyVCKey = [...JSON.parse(myVCKeyFromMMKV), ...dataFromMyVCKey];
|
||||
} else {
|
||||
newDataForMyVCKey = dataFromMyVCKey;
|
||||
}
|
||||
const encryptedDataForMyVCKey = await encryptJson(
|
||||
encryptionKey,
|
||||
JSON.stringify(newDataForMyVCKey),
|
||||
);
|
||||
await this.setItem(
|
||||
MY_VCS_STORE_KEY,
|
||||
encryptedDataForMyVCKey,
|
||||
encryptionKey,
|
||||
);
|
||||
await fileStorage.removeItemIfExist(`${DocumentDirectoryPath}/.prev`);
|
||||
return dataFromDB;
|
||||
}
|
||||
|
||||
private static async isVCAlreadyDownloaded(
|
||||
key: string,
|
||||
encryptionKey: string,
|
||||
) {
|
||||
const storedHMACofCurrentVC = await this.readHmacForVC(key, encryptionKey);
|
||||
return storedHMACofCurrentVC !== null;
|
||||
try {
|
||||
const storedHMACofCurrentVC = await this.readHmacForVC(
|
||||
key,
|
||||
encryptionKey,
|
||||
);
|
||||
return storedHMACofCurrentVC !== null;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private static async isCorruptedVC(
|
||||
@@ -353,31 +379,44 @@ class Storage {
|
||||
data: string,
|
||||
) {
|
||||
// TODO: INJI-612 refactor
|
||||
const storedHMACofCurrentVC = await this.readHmacForDataCorruptionCheck(
|
||||
key,
|
||||
encryptionKey,
|
||||
);
|
||||
const HMACofVC = await generateHmac(encryptionKey, data);
|
||||
return HMACofVC !== storedHMACofCurrentVC;
|
||||
try {
|
||||
const storedHMACofCurrentVC = await this.readHmacForDataCorruptionCheck(
|
||||
key,
|
||||
encryptionKey,
|
||||
);
|
||||
const HMACofVC = await generateHmac(encryptionKey, data);
|
||||
return HMACofVC !== storedHMACofCurrentVC;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private static async readHmacForVC(key: string, encryptionKey: string) {
|
||||
const encryptedHMACofCurrentVC = await MMKV.getItem(key);
|
||||
if (encryptedHMACofCurrentVC) {
|
||||
return decryptJson(encryptionKey, encryptedHMACofCurrentVC);
|
||||
try {
|
||||
const encryptedHMACofCurrentVC = await MMKV.getItem(key);
|
||||
if (encryptedHMACofCurrentVC) {
|
||||
return decryptJson(encryptionKey, encryptedHMACofCurrentVC);
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
console.log('error while reading Hmac for VC ', e);
|
||||
throw e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static async readHmacForDataCorruptionCheck(
|
||||
key: string,
|
||||
encryptionKey: string,
|
||||
) {
|
||||
const encryptedHMACofCurrentVC = await MMKV.getItem(key);
|
||||
if (encryptedHMACofCurrentVC) {
|
||||
return decryptJson(encryptionKey, encryptedHMACofCurrentVC);
|
||||
try {
|
||||
const encryptedHMACofCurrentVC = await MMKV.getItem(key);
|
||||
if (encryptedHMACofCurrentVC) {
|
||||
return decryptJson(encryptionKey, encryptedHMACofCurrentVC);
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static async readVCFromFile(key: string) {
|
||||
|
||||
Reference in New Issue
Block a user