From 4ae208fabc9b0d3e4507ca3cfee78bdd8933a7cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=A8=20Audrey=20=E2=9C=A8?= Date: Fri, 10 May 2024 16:08:05 -0400 Subject: [PATCH] rotate sends from original key to rotated key (#9130) --- .../src/tools/send/services/send.service.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/libs/common/src/tools/send/services/send.service.ts b/libs/common/src/tools/send/services/send.service.ts index fb67de5501..aad0b887c8 100644 --- a/libs/common/src/tools/send/services/send.service.ts +++ b/libs/common/src/tools/send/services/send.service.ts @@ -263,18 +263,26 @@ export class SendService implements InternalSendServiceAbstraction { throw new Error("New user key is required for rotation."); } + const originalUserKey = await this.cryptoService.getUserKey(); + const req = await firstValueFrom( - this.sends$.pipe(concatMap(async (sends) => this.toRotatedKeyRequestMap(sends, newUserKey))), + this.sends$.pipe( + concatMap(async (sends) => this.toRotatedKeyRequestMap(sends, originalUserKey, newUserKey)), + ), ); // separate return for easier debugging return req; } - private async toRotatedKeyRequestMap(sends: Send[], newUserKey: UserKey) { + private async toRotatedKeyRequestMap( + sends: Send[], + originalUserKey: UserKey, + rotateUserKey: UserKey, + ) { const requests = await Promise.all( sends.map(async (send) => { - const sendKey = await this.encryptService.decryptToBytes(send.key, newUserKey); - send.key = await this.encryptService.encrypt(sendKey, newUserKey); + const sendKey = await this.encryptService.decryptToBytes(send.key, originalUserKey); + send.key = await this.encryptService.encrypt(sendKey, rotateUserKey); return new SendWithIdRequest(send); }), );