Skip to content

Commit

Permalink
chore: update sha precompute error message
Browse files Browse the repository at this point in the history
  • Loading branch information
saleel committed Jun 27, 2024
1 parent 29d5c87 commit 384851a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
3 changes: 1 addition & 2 deletions packages/helpers/src/sha-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ export function generatePartialSHA({
}) {
let selectorIndex = 0;

// TODO: See if this (no preselector) could be handled at the circuit level
if (selectorString) {
const selector = new TextEncoder().encode(selectorString);
selectorIndex = findIndexInUint8Array(body, selector);

if (selectorIndex === -1) {
throw new Error('Provider SHA precompute selector not found in the body');
throw new Error(`SHA precompute selector "${selectorString}" not found in the body`);
}
}

Expand Down
44 changes: 21 additions & 23 deletions packages/helpers/tests/input-generators.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fs from 'fs';
import path from 'path';
import { generateEmailVerifierInputs } from '../src/input-generators';
import { bytesToString } from '../src/binary-format';
import fs from "fs";
import path from "path";
import { generateEmailVerifierInputs } from "../src/input-generators";
import { bytesToString } from "../src/binary-format";

jest.setTimeout(10000);

describe('Input generators', () => {
it('should generate input from raw email', async () => {
describe("Input generators", () => {
it("should generate input from raw email", async () => {
const email = fs.readFileSync(
path.join(__dirname, 'test-data/email-good.eml'),
path.join(__dirname, "test-data/email-good.eml")
);

const inputs = await generateEmailVerifierInputs(email);
Expand All @@ -22,9 +22,9 @@ describe('Input generators', () => {
expect(inputs.bodyHashIndex).toBeDefined();
});

it('should generate input without body params when ignoreBodyHash is true', async () => {
it("should generate input without body params when ignoreBodyHash is true", async () => {
const email = fs.readFileSync(
path.join(__dirname, 'test-data/email-good.eml'),
path.join(__dirname, "test-data/email-good.eml")
);

const inputs = await generateEmailVerifierInputs(email, {
Expand All @@ -40,37 +40,35 @@ describe('Input generators', () => {
expect(inputs.bodyHashIndex).toBeFalsy();
});

it('should generate input with SHA precompute selector', async () => {
it("should generate input with SHA precompute selector", async () => {
const email = fs.readFileSync(
path.join(__dirname, 'test-data/email-good-large.eml'),
path.join(__dirname, "test-data/email-good-large.eml")
);

const inputs = await generateEmailVerifierInputs(email, {
shaPrecomputeSelector: 'thousands',
shaPrecomputeSelector: "thousands",
});

expect(inputs.emailBody).toBeDefined();

const strBody = bytesToString(
Uint8Array.from(inputs.emailBody!.map((b) => Number(b))),
Uint8Array.from(inputs.emailBody!.map((b) => Number(b)))
);

const expected = 'h hundreds of thousands of blocks.'; // will round till previous 64x th byte
const expected = "h hundreds of thousands of blocks."; // will round till previous 64x th byte

expect(strBody.startsWith(expected)).toBeTruthy();
});

it('should throw if SHA precompute selector is invalid', async () => {
it("should throw if SHA precompute selector is invalid", async () => {
const email = fs.readFileSync(
path.join(__dirname, 'test-data/email-good.eml'),
path.join(__dirname, "test-data/email-good.eml")
);

expect(async () => {
await generateEmailVerifierInputs(email, {
shaPrecomputeSelector: 'Bla Bla',
});
}).rejects.toThrow(
'Provider SHA precompute selector not found in the body',
);
await expect(() =>
generateEmailVerifierInputs(email, {
shaPrecomputeSelector: "Bla Bla",
})
).rejects.toThrow('SHA precompute selector "Bla Bla" not found in the body');
});
});

0 comments on commit 384851a

Please sign in to comment.