diff --git a/lib/types/crypto-types.js b/lib/types/crypto-types.js --- a/lib/types/crypto-types.js +++ b/lib/types/crypto-types.js @@ -163,6 +163,11 @@ notificationsInitializationInfo: OlmSessionInitializationInfo, keyserverID: string, ) => Promise, + +reassignNotificationsSession?: ( + prevCookie: ?string, + newCookie: ?string, + keyserverID: string, + ) => Promise, +getOneTimeKeys: (numberOfKeys: number) => Promise, +validateAndUploadPrekeys: (authMetadata: AuthMetadata) => Promise, +signMessage: (message: string) => Promise, diff --git a/web/crypto/olm-api.js b/web/crypto/olm-api.js --- a/web/crypto/olm-api.js +++ b/web/crypto/olm-api.js @@ -49,6 +49,7 @@ contentInboundSessionCreator: proxyToWorker('contentInboundSessionCreator'), contentOutboundSessionCreator: proxyToWorker('contentOutboundSessionCreator'), notificationsSessionCreator: proxyToWorker('notificationsSessionCreator'), + reassignNotificationsSession: proxyToWorker('reassignNotificationsSession'), getOneTimeKeys: proxyToWorker('getOneTimeKeys'), validateAndUploadPrekeys: proxyToWorker('validateAndUploadPrekeys'), signMessage: proxyToWorker('signMessage'), diff --git a/web/shared-worker/worker/worker-crypto.js b/web/shared-worker/worker/worker-crypto.js --- a/web/shared-worker/worker/worker-crypto.js +++ b/web/shared-worker/worker/worker-crypto.js @@ -360,6 +360,15 @@ } } +async function reassignLocalForageItem(source: string, destination: string) { + const value = await localforage.getItem(source); + if (!value) { + return; + } + await localforage.setItem(destination, value); + await localforage.removeItem(source); +} + const olmAPI: OlmAPI = { async initializeCryptoAccount(): Promise { const sqliteQueryExecutor = getSQLiteQueryExecutor(); @@ -590,6 +599,39 @@ return initialNotificationsEncryptedMessage; }, + async reassignNotificationsSession( + prevCookie: ?string, + newCookie: ?string, + keyserverID: string, + ): Promise { + const platformDetails = getPlatformDetails(); + if (!platformDetails) { + throw new Error('Worker not initialized'); + } + + const prevPersistenceKeys = getNotifsPersistenceKeys( + prevCookie, + keyserverID, + platformDetails, + ); + + const newPersistenceKeys = getNotifsPersistenceKeys( + newCookie, + keyserverID, + platformDetails, + ); + + await Promise.all([ + reassignLocalForageItem( + prevPersistenceKeys.notifsOlmDataContentKey, + newPersistenceKeys.notifsOlmDataContentKey, + ), + reassignLocalForageItem( + prevPersistenceKeys.notifsOlmDataEncryptionKeyDBLabel, + newPersistenceKeys.notifsOlmDataEncryptionKeyDBLabel, + ), + ]); + }, async getOneTimeKeys(numberOfKeys: number): Promise { if (!cryptoStore) { throw new Error('Crypto account not initialized');