Skip to content

Commit

Permalink
Merge pull request #4 from zkemail/feat/relayer-refactor
Browse files Browse the repository at this point in the history
Feat/relayer refactor
  • Loading branch information
Bisht13 authored Apr 4, 2024
2 parents 82641ee + dce9d19 commit e7cf23e
Show file tree
Hide file tree
Showing 30 changed files with 944 additions and 321 deletions.
144 changes: 111 additions & 33 deletions Cargo.lock

Large diffs are not rendered by default.

40 changes: 4 additions & 36 deletions packages/circuits/helpers/email_auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import { promisify } from "util";
import { generateCircuitInputs } from "@zk-email/helpers/dist/input-helpers";
const emailWalletUtils = require("../../utils");
// import { generateCircuitInputs } from "@zk-email/helpers/dist/input-helpers";
const relayerUtils = require("../../utils");


export async function genEmailAuthInput(emailFilePath: string, accountCode: string):
Expand All @@ -18,38 +18,6 @@ export async function genEmailAuthInput(emailFilePath: string, accountCode: stri
code_idx: number
}> {
const emailRaw = await promisify(fs.readFile)(emailFilePath, "utf8");
const parsedEmail = await emailWalletUtils.parseEmail(emailRaw);
const emailCircuitInputs = generateCircuitInputs({
body: Buffer.from(""),
message: Buffer.from(parsedEmail.canonicalizedHeader),
bodyHash: "",
rsaSignature: BigInt(parsedEmail.signature),
rsaPublicKey: BigInt(parsedEmail.publicKey),
maxMessageLength: 1024,
maxBodyLength: 64,
ignoreBodyHashCheck: true
});
const from_addr_idxes = emailWalletUtils.extractFromAddrIdxes(parsedEmail.canonicalizedHeader)[0];
const fromEmailAddrPart = parsedEmail.canonicalizedHeader.slice(from_addr_idxes[0], from_addr_idxes[1]);
const subject_idx = emailWalletUtils.extractSubjectAllIdxes(parsedEmail.canonicalizedHeader)[0][0];
const domainIdx = emailWalletUtils.extractEmailDomainIdxes(fromEmailAddrPart)[0][0];
const timestampIdx = emailWalletUtils.extractTimestampIdxes(parsedEmail.canonicalizedHeader)[0][0];
let codeIdx = 0;
try {
codeIdx = emailWalletUtils.extractInvitationCodeIdxes(parsedEmail.canonicalizedHeader)[0][0];
} catch (e) {
console.log("No invitation code in header");
}
return {
padded_header: emailCircuitInputs.in_padded,
public_key: emailCircuitInputs.pubkey,
signature: emailCircuitInputs.signature,
padded_header_len: emailCircuitInputs.in_len_padded_bytes,
account_code: accountCode,
from_addr_idx: from_addr_idxes[0],
subject_idx: subject_idx,
domain_idx: domainIdx,
timestamp_idx: timestampIdx,
code_idx: codeIdx,
};
const jsonStr = await relayerUtils.genEmailAuthInput(emailRaw, accountCode);
return JSON.parse(jsonStr);
}
10 changes: 5 additions & 5 deletions packages/circuits/helpers/recipient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import { promisify } from "util";
import { generateCircuitInputs } from "@zk-email/helpers/dist/input-helpers";
const emailWalletUtils = require("../../utils");
const relayerUtils = require("../../utils");


export async function genRecipientInput(emailFilePath: string):
Expand All @@ -10,17 +10,17 @@ export async function genRecipientInput(emailFilePath: string):
rand: string
}> {
const emailRaw = await promisify(fs.readFile)(emailFilePath, "utf8");
const parsedEmail = await emailWalletUtils.parseEmail(emailRaw);
const subjectEmailIdxes = emailWalletUtils.extractSubjectAllIdxes(parsedEmail.canonicalizedHeader)[0];
const parsedEmail = await relayerUtils.parseEmail(emailRaw);
const subjectEmailIdxes = relayerUtils.extractSubjectAllIdxes(parsedEmail.canonicalizedHeader)[0];
const subject = parsedEmail.canonicalizedHeader.slice(subjectEmailIdxes[0], subjectEmailIdxes[1]);
let subjectEmailAddrIdx = 0;
try {
subjectEmailAddrIdx = emailWalletUtils.extractEmailAddrIdxes(subject)[0][0];
subjectEmailAddrIdx = relayerUtils.extractEmailAddrIdxes(subject)[0][0];
} catch (e) {
console.log("No email address in subject");
subjectEmailAddrIdx = 0;
}
const rand = emailWalletUtils.extractRandFromSignature(parsedEmail.signature);
const rand = relayerUtils.extractRandFromSignature(parsedEmail.signature);
return {
subject_email_addr_idx: subjectEmailAddrIdx,
rand: rand
Expand Down
107 changes: 54 additions & 53 deletions packages/circuits/tests/email_auth.test.ts

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions packages/circuits/tests/invitation_code_regex.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const circom_tester = require("circom_tester");
const wasm_tester = circom_tester.wasm;
import * as path from "path";
const emailWalletUtils = require("../../utils");
const relayerUtils = require("../../utils");
const option = {
include: path.join(__dirname, "../../../node_modules")
};
Expand All @@ -14,7 +14,7 @@ describe("Invitation Code Regex", () => {
const codeStr = "Code 123abc";
// const prefixLen = "ACCOUNTKEY.0x".length;
// const revealed = "123abc";
const paddedStr = emailWalletUtils.padString(codeStr, 256);
const paddedStr = relayerUtils.padString(codeStr, 256);
const circuitInputs = {
msg: paddedStr,
};
Expand All @@ -23,7 +23,7 @@ describe("Invitation Code Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = emailWalletUtils.extractInvitationCodeIdxes(codeStr)[0];
const prefixIdxes = relayerUtils.extractInvitationCodeIdxes(codeStr)[0];
for (let idx = 0; idx < 256; ++idx) {
if (idx >= prefixIdxes[0] && idx < prefixIdxes[1]) {
expect(BigInt(paddedStr[idx])).toEqual(witness[2 + idx]);
Expand All @@ -37,7 +37,7 @@ describe("Invitation Code Regex", () => {
const codeStr = "Swap 0.1 ETH to DAI code 123abc";
// const prefixLen = "sepolia+ACCOUNTKEY.0x".length;
// const revealed = "123abc";
const paddedStr = emailWalletUtils.padString(codeStr, 256);
const paddedStr = relayerUtils.padString(codeStr, 256);
const circuitInputs = {
msg: paddedStr,
};
Expand All @@ -46,7 +46,7 @@ describe("Invitation Code Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = emailWalletUtils.extractInvitationCodeIdxes(codeStr)[0];
const prefixIdxes = relayerUtils.extractInvitationCodeIdxes(codeStr)[0];
// const revealedStartIdx = emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8"))[0][0];
// console.log(emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8")));
for (let idx = 0; idx < 256; ++idx) {
Expand All @@ -62,7 +62,7 @@ describe("Invitation Code Regex", () => {
const codeStr = "Send 0.1 ETH to [email protected] code 123abc";
// const prefixLen = "sepolia+ACCOUNTKEY.0x".length;
// const revealed = "123abc";
const paddedStr = emailWalletUtils.padString(codeStr, 256);
const paddedStr = relayerUtils.padString(codeStr, 256);
const circuitInputs = {
msg: paddedStr,
};
Expand All @@ -71,7 +71,7 @@ describe("Invitation Code Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = emailWalletUtils.extractInvitationCodeIdxes(codeStr)[0];
const prefixIdxes = relayerUtils.extractInvitationCodeIdxes(codeStr)[0];
// const revealedStartIdx = emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8"))[0][0];
// console.log(emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8")));
for (let idx = 0; idx < 256; ++idx) {
Expand All @@ -87,7 +87,7 @@ describe("Invitation Code Regex", () => {
const codeStr = "[email protected]";
// const prefixLen = "sepolia+ACCOUNTKEY.0x".length;
// const revealed = "123abc";
const paddedStr = emailWalletUtils.padString(codeStr, 256);
const paddedStr = relayerUtils.padString(codeStr, 256);
const circuitInputs = {
msg: paddedStr,
};
Expand All @@ -96,7 +96,7 @@ describe("Invitation Code Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = emailWalletUtils.extractInvitationCodeIdxes(codeStr)[0];
const prefixIdxes = relayerUtils.extractInvitationCodeIdxes(codeStr)[0];
// const revealedStartIdx = emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8"))[0][0];
// console.log(emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8")));
for (let idx = 0; idx < 256; ++idx) {
Expand All @@ -112,7 +112,7 @@ describe("Invitation Code Regex", () => {
const codeStr = "Swap 0.1 ETH to DAI code 123abc";
// const prefixLen = "sepolia+ACCOUNTKEY.0x".length;
// const revealed = "123abc";
const paddedStr = emailWalletUtils.padString(codeStr, 256);
const paddedStr = relayerUtils.padString(codeStr, 256);
const circuitInputs = {
msg: paddedStr,
};
Expand All @@ -121,7 +121,7 @@ describe("Invitation Code Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = emailWalletUtils.extractInvitationCodeWithPrefixIdxes(codeStr)[0];
const prefixIdxes = relayerUtils.extractInvitationCodeWithPrefixIdxes(codeStr)[0];
// const revealedStartIdx = emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8"))[0][0];
// console.log(emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8")));
for (let idx = 0; idx < 256; ++idx) {
Expand All @@ -137,7 +137,7 @@ describe("Invitation Code Regex", () => {
const codeStr = "Re: Accept guardian request for 0x04884491560f38342C56E26BDD0fEAbb68E2d2FC Code 01eb9b204cc24c3baee11accc37d253a9c53e92b1a2cc07763475c135d575b76";
// const prefixLen = "sepolia+ACCOUNTKEY.0x".length;
// const revealed = "123abc";
const paddedStr = emailWalletUtils.padString(codeStr, 256);
const paddedStr = relayerUtils.padString(codeStr, 256);
const circuitInputs = {
msg: paddedStr,
};
Expand All @@ -146,7 +146,7 @@ describe("Invitation Code Regex", () => {
await circuit.checkConstraints(witness);
// console.log(witness);
expect(1n).toEqual(witness[1]);
const prefixIdxes = emailWalletUtils.extractInvitationCodeWithPrefixIdxes(codeStr)[0];
const prefixIdxes = relayerUtils.extractInvitationCodeWithPrefixIdxes(codeStr)[0];
// const revealedStartIdx = emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8"))[0][0];
// console.log(emailWalletUtils.extractSubstrIdxes(codeStr, readFileSync(path.join(__dirname, "../src/regexes/invitation_code.json"), "utf8")));
for (let idx = 0; idx < 256; ++idx) {
Expand Down
Loading

0 comments on commit e7cf23e

Please sign in to comment.