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 => {
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');
16 return v6PrimaryKeys.length > 0
17 ? [v4PrimaryKeys[0].privateKey, v6PrimaryKeys[0].privateKey]
18 : [v4PrimaryKeys[0].privateKey];