Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INJIMOB-2571]: refactor storage class #1746

Merged
merged 7 commits into from
Dec 20, 2024
4 changes: 2 additions & 2 deletions machines/backupAndRestore/backupRestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fileStorage, {
getBackupFilePath,
unZipAndRemoveFile,
} from '../../shared/fileStorage';
import Storage from '../../shared/storage';
import Storage, { isMinimumLimitReached } from '../../shared/storage';
import {StoreEvents} from '../store';
import Cloud from '../../shared/CloudBackupAndRestoreUtils';
import {TelemetryConstants} from '../../shared/telemetry/TelemetryConstants';
Expand Down Expand Up @@ -289,7 +289,7 @@ export const backupRestoreMachine = model.createMachine(
checkInternet: async () => await NetInfo.fetch(),

checkStorageAvailability: () => async () => {
return await Storage.isMinimumLimitReached('minStorageRequired');
return await isMinimumLimitReached('minStorageRequired');
},

downloadLatestBackup: () => async () => {
Expand Down
6 changes: 2 additions & 4 deletions machines/bleShare/request/requestMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {VcMetaEvents} from '../../VerifiableCredential/VCMetaMachine/VCMetaMachi
import {subscribe} from '../../../shared/openIdBLE/verifierEventHandler';
import {VerifierDataEvent} from '../../../shared/tuvali/types/events';
import {BLEError} from '../types';
import Storage from '../../../shared/storage';
import Storage, { isMinimumLimitReached } from '../../../shared/storage';
import {VCMetadata} from '../../../shared/VCMetadata';
import {
getEndEventData,
Expand Down Expand Up @@ -885,9 +885,7 @@ export const requestMachine =
},

checkStorageAvailability: () => async () => {
return Promise.resolve(
Storage.isMinimumLimitReached('minStorageRequired'),
);
return Promise.resolve(isMinimumLimitReached('minStorageRequired'));
},
},

Expand Down
6 changes: 2 additions & 4 deletions machines/bleShare/scan/scanServices.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isLocationEnabled} from 'react-native-device-info';
import Storage from '../../../shared/storage';
import Storage, { isMinimumLimitReached } from '../../../shared/storage';
import BluetoothStateManager from 'react-native-bluetooth-state-manager';
import {
check,
Expand Down Expand Up @@ -188,9 +188,7 @@ export const ScanServices = (model: any) => {
},

checkStorageAvailability: () => async () => {
return Promise.resolve(
Storage.isMinimumLimitReached('minStorageRequiredForAuditEntry'),
);
return Promise.resolve(isMinimumLimitReached('minStorageRequiredForAuditEntry'));
},
};
};
41 changes: 19 additions & 22 deletions machines/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Storage, {MMKV} from '../shared/storage';
import binaryToBase64 from 'react-native/Libraries/Utilities/binaryToBase64';
import {
EventFrom,
Receiver,
Expand Down Expand Up @@ -36,6 +35,7 @@ import {
} from '../shared/telemetry/TelemetryUtils';
import {Buffer} from 'buffer';
import {VC} from './VerifiableCredential/VCMetaMachine/vc';
import { isVCStorageInitialised } from '../shared/fileStorage';

export const keyinvalidatedString =
'Key Invalidated due to biometric enrollment';
Expand Down Expand Up @@ -276,16 +276,17 @@ export const storeMachine =
BIOMETRIC_CANCELLED: {
actions: [
send(
(_, event) => model.events.BIOMETRIC_CANCELLED(event.requester),
(_, event) =>
model.events.BIOMETRIC_CANCELLED(event.requester),
{
to: (_, event) => event.requester,
},
),
sendUpdate(),
sendParent('BIOMETRIC_CANCELLED')
sendParent('BIOMETRIC_CANCELLED'),
],
target: 'checkFreshInstall',
}
},
},
},
},
Expand All @@ -303,10 +304,10 @@ export const storeMachine =
},
BIOMETRIC_CANCELLED: {
actions: [sendParent('BIOMETRIC_CANCELLED')],
target: 'checkFreshInstall'
target: 'checkFreshInstall',
},
},
},
},
{
actions: {
notifyParent: sendParent(model.events.READY()),
Expand Down Expand Up @@ -334,11 +335,10 @@ export const storeMachine =
return;
},
checkFreshInstall: () => async callback => {
try{
return await getItem('auth', null, '');
}
catch(e){
if(e instanceof BiometricCancellationError){
try {
return await getItem('auth', null, '');
} catch (e) {
if (e instanceof BiometricCancellationError) {
callback(model.events.BIOMETRIC_CANCELLED());
} else {
callback(model.events.STORE_ERROR(e));
Expand All @@ -363,7 +363,7 @@ export const storeMachine =
base64EncodedString,
);
} catch (e) {
if(e instanceof BiometricCancellationError){
if (e instanceof BiometricCancellationError) {
callback(model.events.BIOMETRIC_CANCELLED(event.requester));
}
sendErrorEvent(getErrorEventData('ENCRYPTION', '', e));
Expand Down Expand Up @@ -394,7 +394,7 @@ export const storeMachine =
},

checkStorageInitialisedOrNot: () => async callback => {
const isDirectoryExist = await Storage.isVCStorageInitialised();
const isDirectoryExist = await isVCStorageInitialised();
if (!isDirectoryExist) {
callback(model.events.READY());
} else {
Expand Down Expand Up @@ -422,7 +422,7 @@ export const storeMachine =
break;
}
case 'EXPORT': {
response = await exportData(context.encryptionKey);
response = await backupAndExportData(context.encryptionKey);
break;
}
case 'GET_VCS_DATA': {
Expand All @@ -431,10 +431,7 @@ export const storeMachine =
}
case 'RESTORE_BACKUP': {
// the backup data is in plain text
response = await loadBackupData(
event.data,
context.encryptionKey,
);
await restoreBackedUpData(event.data, context.encryptionKey);
break;
}
case 'SET': {
Expand Down Expand Up @@ -622,12 +619,12 @@ export async function setItem(
}
}

export async function exportData(encryptionKey: string) {
return Storage.exportData(encryptionKey);
export async function backupAndExportData(encryptionKey: string) {
return Storage.backupData(encryptionKey);
}

export async function loadBackupData(data, encryptionKey) {
await Storage.loadBackupData(data, encryptionKey);
export async function restoreBackedUpData(data, encryptionKey) {
await Storage.restoreBackedUpData(data, encryptionKey);
}

export async function fetchAllWellknownConfig(encryptionKey: string) {
Expand Down
142 changes: 53 additions & 89 deletions machines/store.typegen.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,54 @@
// This file was automatically generated. Edits will be overwritten

export interface Typegen0 {
'@@xstate/typegen': true;
internalEvents: {
'done.invoke._store': {
type: 'done.invoke._store';
data: unknown;
__tip: 'See the XState TS docs to learn how to strongly type this.';
};
'done.invoke.store.checkFreshInstall:invocation[0]': {
type: 'done.invoke.store.checkFreshInstall:invocation[0]';
data: unknown;
__tip: 'See the XState TS docs to learn how to strongly type this.';
};
'done.invoke.store.resettingStorage:invocation[0]': {
type: 'done.invoke.store.resettingStorage:invocation[0]';
data: unknown;
__tip: 'See the XState TS docs to learn how to strongly type this.';
};
'error.platform._store': {type: 'error.platform._store'; data: unknown};
'xstate.init': {type: 'xstate.init'};
};
invokeSrcNameMap: {
checkFreshInstall: 'done.invoke.store.checkFreshInstall:invocation[0]';
checkStorageInitialisedOrNot: 'done.invoke.store.checkStorageInitialisation:invocation[0]';
clear: 'done.invoke.store.resettingStorage:invocation[0]';
clearKeys: 'done.invoke.store.clearIosKeys:invocation[0]';
generateEncryptionKey: 'done.invoke.store.generatingEncryptionKey:invocation[0]';
getEncryptionKey: 'done.invoke.store.gettingEncryptionKey:invocation[0]';
hasEncryptionKey: 'done.invoke.store.checkEncryptionKey:invocation[0]';
store: 'done.invoke._store';
};
missingImplementations: {
actions: never;
delays: never;
guards: never;
services: never;
};
eventsCausingActions: {
forwardStoreRequest:
| 'APPEND'
| 'CLEAR'
| 'EXPORT'
| 'FETCH_ALL_WELLKNOWN_CONFIG'
| 'GET'
| 'GET_VCS_DATA'
| 'PREPEND'
| 'REMOVE'
| 'REMOVE_ITEMS'
| 'REMOVE_VC_METADATA'
| 'RESTORE_BACKUP'
| 'SET'
| 'UPDATE';
notifyParent:
| 'KEY_RECEIVED'
| 'READY'
| 'done.invoke.store.resettingStorage:invocation[0]';
setEncryptionKey: 'KEY_RECEIVED';
};
eventsCausingDelays: {};
eventsCausingGuards: {
hasData: 'done.invoke.store.checkFreshInstall:invocation[0]';
isCustomSecureKeystore: 'KEY_RECEIVED';
};
eventsCausingServices: {
checkFreshInstall: 'xstate.init';
checkStorageInitialisedOrNot: 'ERROR';
clear: 'KEY_RECEIVED';
clearKeys: 'done.invoke.store.checkFreshInstall:invocation[0]';
generateEncryptionKey: 'ERROR' | 'IGNORE' | 'READY';
getEncryptionKey: 'TRY_AGAIN';
hasEncryptionKey: never;
store:
| 'KEY_RECEIVED'
| 'READY'
| 'done.invoke.store.resettingStorage:invocation[0]';
};
matchesStates:
| 'checkEncryptionKey'
| 'checkFreshInstall'
| 'checkStorageInitialisation'
| 'clearIosKeys'
| 'failedReadingKey'
| 'generatingEncryptionKey'
| 'gettingEncryptionKey'
| 'ready'
| 'resettingStorage';
tags: never;
}
// This file was automatically generated. Edits will be overwritten

export interface Typegen0 {
'@@xstate/typegen': true;
internalEvents: {
"done.invoke._store": { type: "done.invoke._store"; data: unknown; __tip: "See the XState TS docs to learn how to strongly type this." };
"done.invoke.store.checkFreshInstall:invocation[0]": { type: "done.invoke.store.checkFreshInstall:invocation[0]"; data: unknown; __tip: "See the XState TS docs to learn how to strongly type this." };
"done.invoke.store.resettingStorage:invocation[0]": { type: "done.invoke.store.resettingStorage:invocation[0]"; data: unknown; __tip: "See the XState TS docs to learn how to strongly type this." };
"error.platform._store": { type: "error.platform._store"; data: unknown };
"xstate.init": { type: "xstate.init" };
};
invokeSrcNameMap: {
"checkFreshInstall": "done.invoke.store.checkFreshInstall:invocation[0]";
"checkStorageInitialisedOrNot": "done.invoke.store.checkStorageInitialisation:invocation[0]";
"clear": "done.invoke.store.resettingStorage:invocation[0]";
"clearKeys": "done.invoke.store.clearIosKeys:invocation[0]";
"generateEncryptionKey": "done.invoke.store.generatingEncryptionKey:invocation[0]";
"getEncryptionKey": "done.invoke.store.gettingEncryptionKey:invocation[0]";
"hasEncryptionKey": "done.invoke.store.checkEncryptionKey:invocation[0]";
"store": "done.invoke._store";
};
missingImplementations: {
actions: never;
delays: never;
guards: never;
services: never;
};
eventsCausingActions: {
"forwardStoreRequest": "APPEND" | "CLEAR" | "EXPORT" | "FETCH_ALL_WELLKNOWN_CONFIG" | "GET" | "GET_VCS_DATA" | "PREPEND" | "REMOVE" | "REMOVE_ITEMS" | "REMOVE_VC_METADATA" | "RESTORE_BACKUP" | "SET" | "UPDATE";
"notifyParent": "KEY_RECEIVED" | "READY" | "done.invoke.store.resettingStorage:invocation[0]";
"setEncryptionKey": "KEY_RECEIVED";
};
eventsCausingDelays: {

};
eventsCausingGuards: {
"hasData": "done.invoke.store.checkFreshInstall:invocation[0]";
"isCustomSecureKeystore": "KEY_RECEIVED";
};
eventsCausingServices: {
"checkFreshInstall": "BIOMETRIC_CANCELLED" | "xstate.init";
"checkStorageInitialisedOrNot": "ERROR";
"clear": "KEY_RECEIVED";
"clearKeys": "done.invoke.store.checkFreshInstall:invocation[0]";
"generateEncryptionKey": "ERROR" | "IGNORE" | "READY";
"getEncryptionKey": "TRY_AGAIN";
"hasEncryptionKey": never;
"store": "KEY_RECEIVED" | "READY" | "done.invoke.store.resettingStorage:invocation[0]";
};
matchesStates: "checkEncryptionKey" | "checkFreshInstall" | "checkStorageInitialisation" | "clearIosKeys" | "failedReadingKey" | "generatingEncryptionKey" | "gettingEncryptionKey" | "ready" | "resettingStorage";
tags: never;
}

4 changes: 2 additions & 2 deletions screens/Home/HomeScreenMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ReceivedVcsTabMachine,
} from './ReceivedVcsTabMachine';
import {IssuersMachine} from '../../machines/Issuers/IssuersMachine';
import Storage from '../../shared/storage';
import Storage, { isMinimumLimitReached } from '../../shared/storage';
import {VCItemMachine} from '../../machines/VerifiableCredential/VCItemMachine/VCItemMachine';

const model = createModel(
Expand Down Expand Up @@ -173,7 +173,7 @@ export const HomeScreenMachine = model.createMachine(
services: {
checkStorageAvailability: () => async () => {
return Promise.resolve(
Storage.isMinimumLimitReached('minStorageRequired'),
isMinimumLimitReached('minStorageRequired'),
);
},
},
Expand Down
Loading
Loading