1 import type { PrivateKeyReference, SessionKey } from '@proton/crypto';
2 import { CryptoProxy } from '@proton/crypto';
3 import type { Address, Key } from '@proton/shared/lib/interfaces';
5 export async function generatePrivateKey(name = 'name', email = 'name@example.com'): Promise<PrivateKeyReference> {
6 const { privateKeys } = await generateKeys(name, email);
7 if (privateKeys.length !== 1) {
8 throw new Error('Private key was not generated');
10 return privateKeys[0];
13 export async function generateKeys(name = 'name', email = 'name@example.com') {
14 const privateKey = await CryptoProxy.generateKey({
15 userIDs: [{ name, email }],
17 const privateKeyArmored = await CryptoProxy.exportPrivateKey({ privateKey: privateKey, passphrase: null });
18 const publicKey = await CryptoProxy.importPublicKey({ armoredKey: privateKeyArmored });
19 const publicKeyArmored = await CryptoProxy.exportPublicKey({ key: publicKey });
26 publicKeys: [publicKey],
27 privateKeys: [privateKey],
31 type SessionKeyAlgorithm = Parameters<typeof CryptoProxy.generateSessionKeyForAlgorithm>[0];
32 export async function generateSessionKey(algorithm: SessionKeyAlgorithm = 'aes256'): Promise<SessionKey> {
34 data: await CryptoProxy.generateSessionKeyForAlgorithm(algorithm),
39 export const generateAddress = async (keys: Key[], email = 'test@pm.me'): Promise<Address> => {
47 * Load Crypto API outside of web workers, for testing purposes.
49 export async function setupCryptoProxyForTesting() {
50 const useV6Canary = Math.random() < 0.5;
51 // dynamic import to avoid loading the library unless required
52 const { Api: CryptoApi } = useV6Canary
53 ? await import('@proton/crypto/lib/worker/api_v6_canary')
54 : await import('@proton/crypto/lib/worker/api');
56 CryptoProxy.setEndpoint(
57 // @ts-ignore the v6 canary is effectively compatible
59 (endpoint) => endpoint.clearKeyStore()
63 export function releaseCryptoProxy() {
64 return CryptoProxy.releaseEndpoint();