Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / shared / lib / keys / getPrimaryKey.ts
blobd446d20e3bbe33a728df824dbdd649a2e8f3651b
1 import type { PrivateKeyReferenceV4, PrivateKeyReferenceV6 } from '@proton/crypto';
3 import type { ActiveAddressKeysByVersion, KeyPair } from '../interfaces';
5 export const getPrimaryKey = <T extends KeyPair>(keys: T[] = []): T | undefined => {
6     return keys[0];
7 };
9 export type PrimaryAddressKeys = [PrivateKeyReferenceV4] | [PrivateKeyReferenceV4, PrivateKeyReferenceV6];
10 export const getPrimaryAddressKeysForSigningByVersion = (keys: ActiveAddressKeysByVersion): PrimaryAddressKeys => {
11     const v4PrimaryKeys = keys.v4.filter(({ primary }) => !!primary);
12     const v6PrimaryKeys = keys.v6.filter(({ primary }) => !!primary);
13     if (v4PrimaryKeys.length === 0 || v4PrimaryKeys.length > 1 || v6PrimaryKeys.length > 1) {
14         throw new Error('Unexpected number of primary keys');
15     }
16     return v6PrimaryKeys.length > 0
17         ? [v4PrimaryKeys[0].privateKey, v6PrimaryKeys[0].privateKey]
18         : [v4PrimaryKeys[0].privateKey];