o1js-email-verify
Version:
Implemented using [o1js](https://github.com/o1-labs/o1js), this project is a reimplementation of [zk-email](https://github.com/zkemail/zk-email-verify), leveraging the Mina proving system [Kimchi](https://o1-labs.github.io/proof-systems/specs/kimchi.html#
66 lines • 3.29 kB
JavaScript
import { sha256Pad, generatePartialSHA, MAX_BODY_PADDED_BYTES, MAX_HEADER_PADDED_BYTES } from '@zk-email/helpers';
import { verifyDKIMSignature } from '@zk-email/helpers/dist/dkim';
/**
*
* @description Generate circuit inputs for the EmailVerifier circuit from raw email content
* @param rawEmail Full email content as a buffer or string
* @param params Arguments to control the input generation
* @returns Circuit inputs for the EmailVerifier circuit
*/
export async function generateEmailVerifierInputs(rawEmail, params = {}) {
const dkimResult = await verifyDKIMSignature(rawEmail);
return generateEmailVerifierInputsFromDKIMResult(dkimResult, params);
}
/**
*
* @description Generate circuit inputs for the EmailVerifier circuit from DKIMVerification result
* @param dkimResult DKIMVerificationResult containing email data and verification result
* @param params Arguments to control the input generation
* @returns Circuit inputs for the EmailVerifier circuit
*/
export function generateEmailVerifierInputsFromDKIMResult(dkimResult, params = {}) {
const { headers, body, bodyHash, publicKey, signature, } = dkimResult;
// SHA add padding
const [messagePadded, messagePaddedLen] = sha256Pad(headers, params.maxHeadersLength || MAX_HEADER_PADDED_BYTES);
// const circuitInputs: CircuitInput = {
// emailHeader: Uint8ArrayToCharArray(messagePadded), // Packed into 1 byte signals
// emailHeaderLength: messagePaddedLen.toString(),
// pubkey: toCircomBigIntBytes(publicKey),
// signature: toCircomBigIntBytes(signature),
// };
if (!params.ignoreBodyHashCheck) {
if (!body || !bodyHash) {
throw new Error('body and bodyHash are required when ignoreBodyHashCheck is false');
}
const bodyHashIndex = headers.toString().indexOf(bodyHash);
const maxBodyLength = params.maxBodyLength || MAX_BODY_PADDED_BYTES;
// 65 comes from the 64 at the end and the 1 bit in the start, then 63 comes from the formula to round it up to the nearest 64.
// see sha256algorithm.com for a more full explanation of padding length
const bodySHALength = Math.floor((body.length + 63 + 65) / 64) * 64;
const [bodyPadded, bodyPaddedLen] = sha256Pad(body, Math.max(maxBodyLength, bodySHALength));
const { precomputedSha, bodyRemaining, bodyRemainingLength } = generatePartialSHA({
body: bodyPadded,
bodyLength: bodyPaddedLen,
selectorString: params.shaPrecomputeSelector,
maxRemainingBodyLength: maxBodyLength,
});
// circuitInputs.emailBodyLength = bodyRemainingLength.toString();
// circuitInputs.precomputedSHA = Uint8ArrayToCharArray(precomputedSha);
// circuitInputs.bodyHashIndex = bodyHashIndex.toString();
// circuitInputs.emailBody = Uint8ArrayToCharArray(bodyRemaining);
}
// return circuitInputs;
}
// class DKIMInput extends Struct({
// emailHeader: Bytes,
// emailHeaderLength: Field,
// pubkey: Bigint2048,
// signature: Bigint2048,
// }) { }
// class BodyCheckInput extends Struct({
// emailBody: Bytes,
// emailBodyLength: Field,
// precomputedSHA: Bytes,
// bodyHashIndex: Field,
// }) { }
//# sourceMappingURL=input-generators.js.map