1 import { CryptoProxy } from '@proton/crypto';
2 import { base64StringToUint8Array } from '@proton/shared/lib/helpers/encoding';
4 import type { Address as tsAddress, User as tsUser } from '../../lib/interfaces';
5 import { getDecryptedUserKeysHelper, getReplacedAddressKeyTokens, splitKeys } from '../../lib/keys';
6 import { getAddressKey, getUserKey } from './keyDataHelper';
8 const getSetup = async (forceSameUserKey = false) => {
9 const keyPassword = '1';
10 const userKeysFull = await Promise.all([
11 getUserKey('1', keyPassword),
12 getUserKey('2', keyPassword),
13 getUserKey('3', keyPassword),
15 const UserKeys = userKeysFull.map(({ Key }) => Key);
18 } as unknown as tsUser;
19 const address1 = 'test@test.com';
20 const address2 = 'test2@test.com';
21 const address3 = 'test3@test.com';
22 const userKeys = await getDecryptedUserKeysHelper(User, keyPassword);
23 const getUserPrivateKey = (index: number) => {
24 return userKeysFull[forceSameUserKey ? 0 : index].key.privateKey;
26 const addressKeysFull = await Promise.all([
27 getAddressKey('a', getUserPrivateKey(0), address1),
28 getAddressKey('b', getUserPrivateKey(0), address1),
29 getAddressKey('c', getUserPrivateKey(1), address1),
30 getAddressKey('d', getUserPrivateKey(1), address1),
31 getAddressKey('e', getUserPrivateKey(1), address1),
32 getAddressKey('f', getUserPrivateKey(2), address1),
34 const address2KeysFull = await Promise.all([
35 getAddressKey('g', getUserPrivateKey(0), address2),
36 getAddressKey('h', getUserPrivateKey(1), address2),
38 const address3KeysFull = await Promise.all([
39 getAddressKey('i', getUserPrivateKey(2), address3),
40 getAddressKey('j', getUserPrivateKey(2), address3),
46 Keys: addressKeysFull.map(({ Key }) => Key),
51 Keys: address2KeysFull.map(({ Key }) => Key),
56 Keys: address3KeysFull.map(({ Key }) => Key),
58 ] as unknown as tsAddress[];
64 privateKeys: splitKeys(userKeys).privateKeys,
68 describe('re-encrypt address keys', () => {
69 it('should get address key tokens and re-encrypt to another user key', async () => {
70 const setup = await getSetup();
71 const tokens = await getReplacedAddressKeyTokens({
72 addresses: setup.Addresses,
73 privateKey: setup.userKeys[0].privateKey,
74 privateKeys: setup.privateKeys,
76 const decryptedTokens = await Promise.all(
77 tokens.AddressKeyTokens.map(async (addressKeyToken) => {
78 await CryptoProxy.decryptSessionKey({
79 binaryMessage: base64StringToUint8Array(addressKeyToken.KeyPacket),
80 decryptionKeys: [setup.userKeys[0].privateKey],
85 expect(decryptedTokens.length).toBe(7);
88 it('should not re-encrypt tokens to the same user key', async () => {
89 const setup = await getSetup(true);
90 const tokens = await getReplacedAddressKeyTokens({
91 addresses: setup.Addresses,
92 privateKey: setup.userKeys[0].privateKey,
93 privateKeys: setup.privateKeys,
95 const decryptedTokens = await Promise.all(
96 tokens.AddressKeyTokens.map(async (addressKeyToken) => {
97 await CryptoProxy.decryptSessionKey({
98 binaryMessage: base64StringToUint8Array(addressKeyToken.KeyPacket),
99 decryptionKeys: [setup.userKeys[0].privateKey],
104 expect(decryptedTokens.length).toBe(0);