1 import type { KeyInfo } from '@proton/crypto';
2 import { CryptoProxy } from '@proton/crypto';
3 import isTruthy from '@proton/utils/isTruthy';
5 import { readFileAsString } from '../helpers/file';
7 export interface ArmoredKeyWithInfo extends KeyInfo {
9 * Armored key corresponding to the key info.
10 * This could be a decrypted private key (see `this.keyIsDecrypted`), so handle with care.
15 const ARMORED_KEY_EXPR =
16 /-----BEGIN PGP (PRIVATE|PUBLIC) KEY BLOCK-----(?:(?!-----)[\s\S])*-----END PGP (PRIVATE|PUBLIC) KEY BLOCK-----/g;
18 export const parseArmoredKeys = (fileString: string) => {
19 return fileString.match(ARMORED_KEY_EXPR) || [];
22 export const parseKeys = (filesAsStrings: string[] = []) => {
23 const armoredKeys = parseArmoredKeys(filesAsStrings.join('\n'));
24 if (!armoredKeys.length) {
29 armoredKeys.map(async (armoredKey) => {
31 const keyInfo = await CryptoProxy.getKeyInfo({ armoredKey });
32 const ArmoredKeyWithInfo = {
36 return ArmoredKeyWithInfo;
41 ).then((result) => result.filter<ArmoredKeyWithInfo>(isTruthy));
44 export const parseKeyFiles = async (files: File[] = []) => {
45 const filesAsStrings = await Promise.all(files.map((file) => readFileAsString(file))).catch(() => []);
46 return parseKeys(filesAsStrings);