exposure-keys
Version:
Temporary Exposure Keys (TEK) library
42 lines (41 loc) • 1.88 kB
JavaScript
import { loadAsync } from 'jszip';
import { TemporaryExposureKeyExport, TEKSignatureList } from '../proto/export';
function isZip(fileContent) {
// eslint-disable-next-line no-magic-numbers
const zipHeader = [0x50, 0x4b];
return Buffer.from(zipHeader).compare(fileContent, 0, 2) === 0;
}
export async function loadZip(fileContent) {
var _a, _b;
if (!isZip(fileContent)) {
throw new Error('The file you are trying to load is not a zip file.');
}
const unzippedData = await loadAsync(fileContent);
const keyData = await ((_a = unzippedData.file('export.bin')) === null || _a === void 0 ? void 0 : _a.async('nodebuffer'));
const signatureData = await ((_b = unzippedData.file('export.sig')) === null || _b === void 0 ? void 0 : _b.async('nodebuffer'));
if (!keyData) {
throw new Error(`The zip file doesn't contain key data`);
}
if (!signatureData) {
throw new Error(`The zip file doesn't contain a signature`);
}
return { keys: keyData, signature: signatureData };
}
export function loadKeys(fileContent) {
if (isZip(fileContent)) {
throw new Error('You are trying to load a zip file. Please use `loadZip()` for that');
}
const wantedHeader = 'EK Export v1 ';
const header = fileContent.slice(0, wantedHeader.length);
const key = fileContent.slice(wantedHeader.length);
if (header.toString('utf-8') !== wantedHeader) {
throw new Error('The key is missing a correct header');
}
return TemporaryExposureKeyExport.decode(key);
}
export function loadSignature(fileContent) {
return TEKSignatureList.decode(fileContent);
}
export function riskCount(temporaryExposureKeyExport, transmissionRiskLevel) {
return temporaryExposureKeyExport.keys.filter(key => key.transmissionRiskLevel && key.transmissionRiskLevel === transmissionRiskLevel).length;
}