diff --git a/machines/store.ts b/machines/store.ts index 4a99821a80..edfec4164d 100644 --- a/machines/store.ts +++ b/machines/store.ts @@ -594,8 +594,13 @@ export async function removeItem( await removeVCMetaData(MY_VCS_STORE_KEY, key, encryptionKey); } else if (key === MY_VCS_STORE_KEY) { const data = await Storage.getItem(key, encryptionKey); - const decryptedData = await decryptJson(encryptionKey, data); - const list = JSON.parse(decryptedData) as Object[]; + let list: Object[] = []; + + if (data !== null) { + const decryptedData = await decryptJson(encryptionKey, data); + list = JSON.parse(decryptedData) as Object[]; + } + const newList = list.filter((vcMetadataObject: Object) => { return new VCMetadata(vcMetadataObject).getVcKey() !== value; }); @@ -616,8 +621,13 @@ export async function removeVCMetaData( ) { try { const data = await Storage.getItem(key, encryptionKey); - const decryptedData = await decryptJson(encryptionKey, data); - const list = JSON.parse(decryptedData) as Object[]; + let list: Object[] = []; + + if (data != null) { + const decryptedData = await decryptJson(encryptionKey, data); + list = JSON.parse(decryptedData) as Object[]; + } + const newList = list.filter((vcMetadataObject: Object) => { return new VCMetadata(vcMetadataObject).getVcKey() !== vcKey; }); @@ -648,8 +658,13 @@ export async function removeItems( ) { try { const data = await Storage.getItem(key, encryptionKey); - const decryptedData = await decryptJson(encryptionKey, data); - const list = JSON.parse(decryptedData) as Object[]; + let list: Object[] = []; + + if (data !== null) { + const decryptedData = await decryptJson(encryptionKey, data); + list = JSON.parse(decryptedData) as Object[]; + } + const newList = list.filter( (vcMetadataObject: Object) => !values.includes(new VCMetadata(vcMetadataObject).getVcKey()), diff --git a/shared/cryptoutil/cryptoUtil.ts b/shared/cryptoutil/cryptoUtil.ts index 1983de9d0f..f36c6fb9a9 100644 --- a/shared/cryptoutil/cryptoUtil.ts +++ b/shared/cryptoutil/cryptoUtil.ts @@ -115,6 +115,10 @@ export async function decryptJson( encryptedData: string, ): Promise { try { + if (encryptedData === null || encryptedData === undefined) { + // to avoid crash in case of null or undefined + return ''; + } // Disable Encryption in debug mode if (DEBUG_MODE_ENABLED && __DEV__) { return JSON.parse(encryptedData); @@ -125,6 +129,7 @@ export async function decryptJson( CryptoJS.enc.Utf8, ); } + return await SecureKeystore.decryptData(ENCRYPTION_ID, encryptedData); } catch (e) { console.error('error decryptJson:', e);