diff --git a/packages/sismo-connect-server/tests/hydras3-verifier.test.ts b/packages/sismo-connect-server/tests/hydras3-verifier.test.ts index 2e3ff26..a826b48 100644 --- a/packages/sismo-connect-server/tests/hydras3-verifier.test.ts +++ b/packages/sismo-connect-server/tests/hydras3-verifier.test.ts @@ -1,5 +1,5 @@ -import { sismoConnectSimpleClaimResponseMock } from './mocks' -import { BigNumber } from '@ethersproject/bignumber' +import { sismoConnectSimpleClaimResponseMock } from "./mocks"; +import { BigNumber } from "@ethersproject/bignumber"; import { ClaimType, VerifiedClaim, @@ -8,40 +8,37 @@ import { AuthType, AuthRequest, VerifiedAuth, -} from '../src' -import { - decodeProofData, - encodeProofData, -} from '../src/verifier/utils/proofData' -import { ethers } from 'ethers' -import { HydraS3VerifierMocked } from './hydras3-verifier-mocked' -import { encodeRequestIdentifier } from '../src/verifier/utils/encodeRequestIdentifier' -import { ProofPublicInputs } from '../src/verifier/hydra-verifiers' -import { encodeAccountsTreeValue } from '../src/verifier/utils/encodeAccountsTreeValue' -import { SNARK_FIELD } from '@sismo-core/hydra-s3' - -describe('HydraS3 Verifier test', () => { - let appId: string - let groupId: string - let groupTimestamp: number | 'latest' - let value: number - let claimType: ClaimType +} from "../src"; +import { decodeProofData, encodeProofData } from "../src/verifier/utils/proofData"; +import { ethers } from "ethers"; +import { HydraS3VerifierMocked } from "./hydras3-verifier-mocked"; +import { encodeRequestIdentifier } from "../src/verifier/utils/encodeRequestIdentifier"; +import { ProofPublicInputs } from "../src/verifier/hydra-verifiers"; +import { encodeAccountsTreeValue } from "../src/verifier/utils/encodeAccountsTreeValue"; +import { SNARK_FIELD } from "@sismo-core/hydra-s3"; + +describe("HydraS3 Verifier test", () => { + let appId: string; + let groupId: string; + let groupTimestamp: number | "latest"; + let value: number; + let claimType: ClaimType; let signedMessage: string; - let authProofPublicInputs: ProofPublicInputs - let claimProofPublicInputs: ProofPublicInputs - let vaultProofPublicInputs: ProofPublicInputs + let authProofPublicInputs: ProofPublicInputs; + let claimProofPublicInputs: ProofPublicInputs; + let vaultProofPublicInputs: ProofPublicInputs; - let verifiedClaim: VerifiedClaim - let verifiedAuth: VerifiedAuth - let verifiedVault: VerifiedAuth + let verifiedClaim: VerifiedClaim; + let verifiedAuth: VerifiedAuth; + let verifiedVault: VerifiedAuth; - let claimProof: SismoConnectProof - let vaultProof: SismoConnectProof - let authProof: SismoConnectProof + let claimProof: SismoConnectProof; + let vaultProof: SismoConnectProof; + let authProof: SismoConnectProof; - let hydraS3VerifierMocked: HydraS3VerifierMocked - let namespace: string + let hydraS3VerifierMocked: HydraS3VerifierMocked; + let namespace: string; let authType: AuthType; let vaultType: AuthType; @@ -49,38 +46,33 @@ describe('HydraS3 Verifier test', () => { let userId: string; let vaultId: string; - let proofIdentifier: string + let proofIdentifier: string; let commitmentMapperPubKey: [BigNumber, BigNumber]; - let expectVerifyClaimToThrow: ( - proof: SismoConnectProof, - expectedError: string - ) => Promise + let expectVerifyClaimToThrow: (proof: SismoConnectProof, expectedError: string) => Promise; beforeAll(() => { - appId = '0xf4977993e52606cfd67b7a1cde717069'; - groupId = '0x8543f5652418334ff011c1888fd8d96f'; - namespace = 'main'; - groupTimestamp = 'latest'; + appId = "0xf4977993e52606cfd67b7a1cde717069"; + groupId = "0x8543f5652418334ff011c1888fd8d96f"; + namespace = "main"; + groupTimestamp = "latest"; value = 1; claimType = ClaimType.GTE; - vaultId = "0x106e40fa5d1d2ace741f7cab6f9d55c614bf25c3febebe148a2fa09e3bbaa096"; + vaultId = "0x106e40fa5d1d2ace741f7cab6f9d55c614bf25c3febebe148a2fa09e3bbaa096"; userId = "0x1002000000000000000001553049041977724928"; vaultType = 0; authType = 2; - signedMessage = 'Hello'; + signedMessage = "Hello"; - const snarkClaimProof = decodeProofData(sismoConnectSimpleClaimResponseMock.proofs[0].proofData); + const snarkClaimProof = decodeProofData( + sismoConnectSimpleClaimResponseMock.proofs[0].proofData + ); claimProofPublicInputs = { destinationIdentifier: BigNumber.from(snarkClaimProof.input[0]).toHexString(), extraData: BigNumber.from(snarkClaimProof.input[1]).toHexString(), - commitmentMapperPubKeyX: BigNumber.from( - snarkClaimProof.input[2] - ).toHexString(), - commitmentMapperPubKeyY: BigNumber.from( - snarkClaimProof.input[3] - ).toHexString(), + commitmentMapperPubKeyX: BigNumber.from(snarkClaimProof.input[2]).toHexString(), + commitmentMapperPubKeyY: BigNumber.from(snarkClaimProof.input[3]).toHexString(), registryTreeRoot: BigNumber.from(snarkClaimProof.input[4]).toHexString(), requestIdentifier: BigNumber.from(snarkClaimProof.input[5]).toString(), proofIdentifier: BigNumber.from(snarkClaimProof.input[6]).toString(), @@ -89,25 +81,19 @@ describe('HydraS3 Verifier test', () => { claimType: BigNumber.from(snarkClaimProof.input[9]).toString(), vaultIdentifier: BigNumber.from(snarkClaimProof.input[10]).toHexString(), vaultNamespace: BigNumber.from(snarkClaimProof.input[11]).toHexString(), - sourceVerificationEnabled: BigNumber.from( - snarkClaimProof.input[12] - ).toHexString(), - destinationVerificationEnabled: BigNumber.from( - snarkClaimProof.input[13] - ).toHexString(), - } + sourceVerificationEnabled: BigNumber.from(snarkClaimProof.input[12]).toHexString(), + destinationVerificationEnabled: BigNumber.from(snarkClaimProof.input[13]).toHexString(), + }; - const snarkVaultProof = decodeProofData(sismoConnectSimpleClaimResponseMock.proofs[1].proofData); + const snarkVaultProof = decodeProofData( + sismoConnectSimpleClaimResponseMock.proofs[1].proofData + ); vaultProofPublicInputs = { destinationIdentifier: BigNumber.from(snarkVaultProof.input[0]).toHexString(), extraData: BigNumber.from(snarkVaultProof.input[1]).toHexString(), - commitmentMapperPubKeyX: BigNumber.from( - snarkVaultProof.input[2] - ).toHexString(), - commitmentMapperPubKeyY: BigNumber.from( - snarkVaultProof.input[3] - ).toHexString(), + commitmentMapperPubKeyX: BigNumber.from(snarkVaultProof.input[2]).toHexString(), + commitmentMapperPubKeyY: BigNumber.from(snarkVaultProof.input[3]).toHexString(), registryTreeRoot: BigNumber.from(snarkVaultProof.input[4]).toHexString(), requestIdentifier: BigNumber.from(snarkVaultProof.input[5]).toString(), proofIdentifier: BigNumber.from(snarkVaultProof.input[6]).toString(), @@ -116,25 +102,17 @@ describe('HydraS3 Verifier test', () => { claimType: BigNumber.from(snarkVaultProof.input[9]).toString(), vaultIdentifier: BigNumber.from(snarkVaultProof.input[10]).toHexString(), vaultNamespace: BigNumber.from(snarkVaultProof.input[11]).toHexString(), - sourceVerificationEnabled: BigNumber.from( - snarkVaultProof.input[12] - ).toHexString(), - destinationVerificationEnabled: BigNumber.from( - snarkVaultProof.input[13] - ).toHexString(), - } - + sourceVerificationEnabled: BigNumber.from(snarkVaultProof.input[12]).toHexString(), + destinationVerificationEnabled: BigNumber.from(snarkVaultProof.input[13]).toHexString(), + }; + const snarkAuthProof = decodeProofData(sismoConnectSimpleClaimResponseMock.proofs[2].proofData); authProofPublicInputs = { destinationIdentifier: BigNumber.from(snarkAuthProof.input[0]).toHexString(), extraData: BigNumber.from(snarkAuthProof.input[1]).toHexString(), - commitmentMapperPubKeyX: BigNumber.from( - snarkAuthProof.input[2] - ).toHexString(), - commitmentMapperPubKeyY: BigNumber.from( - snarkAuthProof.input[3] - ).toHexString(), + commitmentMapperPubKeyX: BigNumber.from(snarkAuthProof.input[2]).toHexString(), + commitmentMapperPubKeyY: BigNumber.from(snarkAuthProof.input[3]).toHexString(), registryTreeRoot: BigNumber.from(snarkAuthProof.input[4]).toHexString(), requestIdentifier: BigNumber.from(snarkAuthProof.input[5]).toString(), proofIdentifier: BigNumber.from(snarkAuthProof.input[6]).toString(), @@ -143,22 +121,18 @@ describe('HydraS3 Verifier test', () => { claimType: BigNumber.from(snarkAuthProof.input[9]).toString(), vaultIdentifier: BigNumber.from(snarkAuthProof.input[10]).toHexString(), vaultNamespace: BigNumber.from(snarkAuthProof.input[11]).toHexString(), - sourceVerificationEnabled: BigNumber.from( - snarkAuthProof.input[12] - ).toHexString(), - destinationVerificationEnabled: BigNumber.from( - snarkAuthProof.input[13] - ).toHexString(), - } + sourceVerificationEnabled: BigNumber.from(snarkAuthProof.input[12]).toHexString(), + destinationVerificationEnabled: BigNumber.from(snarkAuthProof.input[13]).toHexString(), + }; commitmentMapperPubKey = [ BigNumber.from(snarkAuthProof.input[2]), BigNumber.from(snarkAuthProof.input[3]), - ] + ]; hydraS3VerifierMocked = new HydraS3VerifierMocked({ commitmentMapperPubKey, - }) + }); const proofId = BigNumber.from(snarkClaimProof.input[6]).toHexString(); @@ -176,9 +150,9 @@ describe('HydraS3 Verifier test', () => { snarkClaimProof.c, snarkClaimProof.input ), - } + }; - verifiedVault= { + verifiedVault = { authType: vaultType, userId: vaultId, isSelectableByUser: true, @@ -188,8 +162,8 @@ describe('HydraS3 Verifier test', () => { snarkVaultProof.b, snarkVaultProof.c, snarkVaultProof.input - ) - } + ), + }; verifiedAuth = { authType, @@ -201,83 +175,78 @@ describe('HydraS3 Verifier test', () => { snarkAuthProof.b, snarkAuthProof.c, snarkAuthProof.input - ) - } + ), + }; claimProof = sismoConnectSimpleClaimResponseMock.proofs[0]; vaultProof = sismoConnectSimpleClaimResponseMock.proofs[1]; authProof = sismoConnectSimpleClaimResponseMock.proofs[2]; - proofIdentifier = claimProofPublicInputs.proofIdentifier + proofIdentifier = claimProofPublicInputs.proofIdentifier; - expectVerifyClaimToThrow = async ( - proof: SismoConnectProof, - errorMessage: string - ) => { + expectVerifyClaimToThrow = async (proof: SismoConnectProof, errorMessage: string) => { await expect( hydraS3VerifierMocked.verifyClaimProof({ namespace, appId, proof, }) - ).rejects.toThrow(errorMessage) - } - }) + ).rejects.toThrow(errorMessage); + }; + }); + + afterAll(() => { + globalThis.curve_bn128.terminate(); + }); /**********************************************************************************/ /************************************* CLAIM **************************************/ /**********************************************************************************/ - describe('Verify Claim proof', () => { - describe('Validate input', () => { - it('Should throw with incorrect input claimType', async () => { - const invalidProof = JSON.parse( - JSON.stringify(claimProof) - ) as SismoConnectProof - invalidProof.claims = invalidProof.claims as ClaimRequest[] + describe("Verify Claim proof", () => { + describe("Validate input", () => { + it("Should throw with incorrect input claimType", async () => { + const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof; + invalidProof.claims = invalidProof.claims as ClaimRequest[]; - invalidProof.claims[0].claimType = ClaimType.EQ - const claimTypeFromInput = claimProofPublicInputs.claimType === '0' + invalidProof.claims[0].claimType = ClaimType.EQ; + const claimTypeFromInput = claimProofPublicInputs.claimType === "0"; await expectVerifyClaimToThrow( invalidProof, `on proofId "${proofIdentifier}" claimType "${ invalidProof.claims[0].claimType }" mismatch with proof input claimType "${!claimTypeFromInput}"` - ) - }) - - it('Should throw with incorrect input claim value', async () => { - const invalidProof = JSON.parse( - JSON.stringify(claimProof) - ) as SismoConnectProof - invalidProof.claims = invalidProof.claims as ClaimRequest[] - invalidProof.claims[0].value = 2 + ); + }); + + it("Should throw with incorrect input claim value", async () => { + const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof; + invalidProof.claims = invalidProof.claims as ClaimRequest[]; + invalidProof.claims[0].value = 2; await expectVerifyClaimToThrow( invalidProof, `on proofId "${proofIdentifier}" value "${invalidProof.claims[0].value}" mismatch with proof input claimValue "${claimProofPublicInputs.claimValue}"` - ) - }) - - it('Should throw with incorrect input requestIdentifier', async () => { - const invalidProof = JSON.parse( - JSON.stringify(claimProof) - ) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.input[5] = '1' + ); + }); + + it("Should throw with incorrect input requestIdentifier", async () => { + const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.input[5] = "1"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; const requestIdentifier = encodeRequestIdentifier( appId, groupId, groupTimestamp, namespace - ) + ); await expectVerifyClaimToThrow( invalidProof, @@ -286,22 +255,20 @@ describe('HydraS3 Verifier test', () => { ).toHexString()}" mismatch with proof input requestIdentifier "${BigNumber.from( proofDecoded.input[5] ).toHexString()}"` - ) - }) - - it('Should throw with incorrect input commitmentMapperPubKeyX', async () => { - const invalidProof = JSON.parse( - JSON.stringify(claimProof) - ) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.input[2] = '0x1' + ); + }); + + it("Should throw with incorrect input commitmentMapperPubKeyX", async () => { + const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.input[2] = "0x1"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expectVerifyClaimToThrow( invalidProof, @@ -310,22 +277,20 @@ describe('HydraS3 Verifier test', () => { ).toHexString()}" mismatch with proof input commitmentMapperPubKeyX "${BigNumber.from( proofDecoded.input[2] ).toHexString()}"` - ) - }) - - it('Should throw with incorrect input commitmentMapperPubKeyY', async () => { - const invalidProof = JSON.parse( - JSON.stringify(claimProof) - ) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.input[3] = '0x1' + ); + }); + + it("Should throw with incorrect input commitmentMapperPubKeyY", async () => { + const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.input[3] = "0x1"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expectVerifyClaimToThrow( invalidProof, @@ -334,91 +299,87 @@ describe('HydraS3 Verifier test', () => { ).toHexString()}" mismatch with proof input commitmentMapperPubKeyY "${BigNumber.from( proofDecoded.input[3] ).toHexString()}"` - ) - }) - - it('should throw with incorrect input sourceVerificationEnabled', async () => { - const invalidProof = JSON.parse( - JSON.stringify(claimProof) - ) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.input[12] = '123456789' + ); + }); + + it("should throw with incorrect input sourceVerificationEnabled", async () => { + const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.input[12] = "123456789"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expectVerifyClaimToThrow( invalidProof, `on proofId "${proofIdentifier}" proof input sourceVerificationEnabled must be 1` - ) - }) - - it('Should throw with incorrect accountsTreeValue', async () => { - const invalidProof = JSON.parse( - JSON.stringify(claimProof) - ) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.input[8] = '123456789' + ); + }); + + it("Should throw with incorrect accountsTreeValue", async () => { + const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.input[8] = "123456789"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded - invalidProof.claims = invalidProof.claims as ClaimRequest[] + ); + invalidProof.proofData = proofEncoded; + invalidProof.claims = invalidProof.claims as ClaimRequest[]; await expectVerifyClaimToThrow( invalidProof, `on proofId "${proofIdentifier}" groupId "${invalidProof.claims[0].groupId}" or timestamp "${invalidProof.claims[0].groupTimestamp}" incorrect` - ) - }) - }) - - describe('proof validity', () => { - it('Should return false', async () => { - const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.a[0] = '123456789' + ); + }); + }); + + describe("proof validity", () => { + it("Should return false", async () => { + const invalidProof = JSON.parse(JSON.stringify(claimProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.a[0] = "123456789"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expect( - hydraS3VerifierMocked.verifyClaimProof({ + hydraS3VerifierMocked.verifyClaimProof({ appId, namespace, proof: invalidProof, }) - ).rejects.toThrow('Snark Proof Invalid!') - }) + ).rejects.toThrow("Snark Proof Invalid!"); + }); - it('Should return true', async () => { + it("Should return true", async () => { const isVerified = await hydraS3VerifierMocked.verifyClaimProof({ appId, namespace, proof: claimProof, - }) - expect(isVerified).toEqual(verifiedClaim) - }) - }) - }) + }); + expect(isVerified).toEqual(verifiedClaim); + }); + }); + }); /**********************************************************************************/ /********************************** SIGNATURE *************************************/ /**********************************************************************************/ - describe('Verify Signature proof', () => { - describe('Validate input', () => { - it('Should throw on claim proof with incorrect input signature', async () => { + describe("Verify Signature proof", () => { + describe("Validate input", () => { + it("Should throw on claim proof with incorrect input signature", async () => { const invalidMessage = "invalid message"; const expectedSignedMessage = BigNumber.from( @@ -430,16 +391,16 @@ describe('HydraS3 Verifier test', () => { ).mod(SNARK_FIELD); const messageClaim = `on proofId "${proofIdentifier}" extraData "${expectedSignedMessage.toHexString()}" mismatch with signedMessage "${invalidSignedMessage.toHexString()}"`; - + await expect( hydraS3VerifierMocked.verifySignedMessageProof({ proof: claimProof, - signedMessage: invalidMessage + signedMessage: invalidMessage, }) - ).rejects.toThrow(messageClaim) - }) + ).rejects.toThrow(messageClaim); + }); - it('Should throw on auth proof with incorrect input signature', async () => { + it("Should throw on auth proof with incorrect input signature", async () => { const invalidMessage = "invalid message"; const expectedSignedMessage = BigNumber.from( @@ -451,218 +412,210 @@ describe('HydraS3 Verifier test', () => { ).mod(SNARK_FIELD); const messageAuth = `on proofId "0" extraData "${expectedSignedMessage.toHexString()}" mismatch with signedMessage "${invalidSignedMessage.toHexString()}"`; - + await expect( hydraS3VerifierMocked.verifySignedMessageProof({ proof: authProof, - signedMessage: invalidMessage + signedMessage: invalidMessage, }) - ).rejects.toThrow(messageAuth) - }) - }) - }) + ).rejects.toThrow(messageAuth); + }); + }); + }); /**********************************************************************************/ /************************************* AUTH ***************************************/ /**********************************************************************************/ - describe('Verify Auth proof', () => { - describe('Validate input', () => { - it('Should throw with incorrect input destination identifier', async () => { - const invalidProof = JSON.parse( - JSON.stringify(authProof) - ) as SismoConnectProof - invalidProof.auths = invalidProof.auths as AuthRequest[] + describe("Verify Auth proof", () => { + describe("Validate input", () => { + it("Should throw with incorrect input destination identifier", async () => { + const invalidProof = JSON.parse(JSON.stringify(authProof)) as SismoConnectProof; + invalidProof.auths = invalidProof.auths as AuthRequest[]; invalidProof.auths[0].userId = "2"; await expect( hydraS3VerifierMocked.verifyAuthProof({ proof: invalidProof, - signedMessage + signedMessage, }) - ).rejects.toThrow(`userId \"0x02\" mismatch with proof input destinationIdentifier 0x1002000000000000000001553049041977724928`) - }) - - it('should throw with incorrect input destinationVerificationEnabled', async () => { - const invalidProof = JSON.parse( - JSON.stringify(authProof) - ) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.input[13] = '123456789' + ).rejects.toThrow( + `userId \"0x02\" mismatch with proof input destinationIdentifier 0x1002000000000000000001553049041977724928` + ); + }); + + it("should throw with incorrect input destinationVerificationEnabled", async () => { + const invalidProof = JSON.parse(JSON.stringify(authProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.input[13] = "123456789"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expect( hydraS3VerifierMocked.verifyAuthProof({ proof: invalidProof, - signedMessage + signedMessage, }) - ).rejects.toThrow("proof input destinationVerificationEnabled must be 1") - }) - - it('Should throw with incorrect input commitmentMapperPubKeyX', async () => { - const invalidProof = JSON.parse( - JSON.stringify(authProof) - ) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.input[2] = '0x1' + ).rejects.toThrow("proof input destinationVerificationEnabled must be 1"); + }); + + it("Should throw with incorrect input commitmentMapperPubKeyX", async () => { + const invalidProof = JSON.parse(JSON.stringify(authProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.input[2] = "0x1"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expect( hydraS3VerifierMocked.verifyAuthProof({ proof: invalidProof, - signedMessage + signedMessage, }) - ).rejects.toThrow(`commitmentMapperPubKeyX "${BigNumber.from( - commitmentMapperPubKey[0] - ).toHexString()}" mismatch with proof input commitmentMapperPubKeyX "${BigNumber.from( - proofDecoded.input[2] - ).toHexString()}"`) - }) - - it('Should throw with incorrect input commitmentMapperPubKeyY', async () => { - const invalidProof = JSON.parse( - JSON.stringify(authProof) - ) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.input[3] = '0x1' + ).rejects.toThrow( + `commitmentMapperPubKeyX "${BigNumber.from( + commitmentMapperPubKey[0] + ).toHexString()}" mismatch with proof input commitmentMapperPubKeyX "${BigNumber.from( + proofDecoded.input[2] + ).toHexString()}"` + ); + }); + + it("Should throw with incorrect input commitmentMapperPubKeyY", async () => { + const invalidProof = JSON.parse(JSON.stringify(authProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.input[3] = "0x1"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expect( hydraS3VerifierMocked.verifyAuthProof({ proof: invalidProof, - signedMessage + signedMessage, }) - ).rejects.toThrow(`commitmentMapperPubKeyY "${BigNumber.from( - commitmentMapperPubKey[1] - ).toHexString()}" mismatch with proof input commitmentMapperPubKeyY "${BigNumber.from( - proofDecoded.input[3] - ).toHexString()}"`) - }) - }) - - describe('proof validity', () => { - it('Should return false', async () => { - const invalidProof = JSON.parse(JSON.stringify(authProof)) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.a[0] = '123456789' + ).rejects.toThrow( + `commitmentMapperPubKeyY "${BigNumber.from( + commitmentMapperPubKey[1] + ).toHexString()}" mismatch with proof input commitmentMapperPubKeyY "${BigNumber.from( + proofDecoded.input[3] + ).toHexString()}"` + ); + }); + }); + + describe("proof validity", () => { + it("Should return false", async () => { + const invalidProof = JSON.parse(JSON.stringify(authProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.a[0] = "123456789"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expect( hydraS3VerifierMocked.verifyAuthProof({ proof: invalidProof, - signedMessage + signedMessage, }) - ).rejects.toThrow('Snark Proof Invalid!') - }) + ).rejects.toThrow("Snark Proof Invalid!"); + }); - it('Should return true', async () => { + it("Should return true", async () => { const isVerified = await hydraS3VerifierMocked.verifyAuthProof({ proof: authProof, - signedMessage - }) - expect(isVerified).toEqual(verifiedAuth) - }) - }) - }) + signedMessage, + }); + expect(isVerified).toEqual(verifiedAuth); + }); + }); + }); /**********************************************************************************/ /************************************* VAULT ***************************************/ /**********************************************************************************/ - describe('Verify Vault proof', () => { - describe('Validate input', () => { - - it('Should throw with incorrect input Vault identifier', async () => { - const invalidProof = JSON.parse( - JSON.stringify(vaultProof) - ) as SismoConnectProof - invalidProof.auths = invalidProof.auths as AuthRequest[] + describe("Verify Vault proof", () => { + describe("Validate input", () => { + it("Should throw with incorrect input Vault identifier", async () => { + const invalidProof = JSON.parse(JSON.stringify(vaultProof)) as SismoConnectProof; + invalidProof.auths = invalidProof.auths as AuthRequest[]; invalidProof.auths[0].userId = "2"; await expect( hydraS3VerifierMocked.verifyAuthProof({ proof: invalidProof, - signedMessage + signedMessage, }) - ).rejects.toThrow(`userId \"0x02\" mismatch with proof input vaultIdentifier 0x106e40fa5d1d2ace741f7cab6f9d55c614bf25c3febebe148a2fa09e3bbaa096`) - }) - }) - - describe('proof validity', () => { - it('Should return false', async () => { - const invalidProof = JSON.parse(JSON.stringify(vaultProof)) as SismoConnectProof - const proofDecoded = decodeProofData(invalidProof.proofData) - proofDecoded.a[0] = '123456789' + ).rejects.toThrow( + `userId \"0x02\" mismatch with proof input vaultIdentifier 0x106e40fa5d1d2ace741f7cab6f9d55c614bf25c3febebe148a2fa09e3bbaa096` + ); + }); + }); + + describe("proof validity", () => { + it("Should return false", async () => { + const invalidProof = JSON.parse(JSON.stringify(vaultProof)) as SismoConnectProof; + const proofDecoded = decodeProofData(invalidProof.proofData); + proofDecoded.a[0] = "123456789"; const proofEncoded = encodeProofData( proofDecoded.a, proofDecoded.b, proofDecoded.c, proofDecoded.input - ) - invalidProof.proofData = proofEncoded + ); + invalidProof.proofData = proofEncoded; await expect( hydraS3VerifierMocked.verifyAuthProof({ proof: invalidProof, - signedMessage + signedMessage, }) - ).rejects.toThrow('Snark Proof Invalid!') - }) + ).rejects.toThrow("Snark Proof Invalid!"); + }); - it('Should return true', async () => { + it("Should return true", async () => { const isVerified = await hydraS3VerifierMocked.verifyAuthProof({ proof: vaultProof, - signedMessage - }) - expect(isVerified).toEqual(verifiedVault) - }) - }) - }) + signedMessage, + }); + expect(isVerified).toEqual(verifiedVault); + }); + }); + }); /**********************************************************************************/ /*********************************** HELPERS **************************************/ /**********************************************************************************/ - it('Should encode the right request identifier', async () => { - const requestIdentifier = encodeRequestIdentifier( - appId, - groupId, - groupTimestamp, - namespace - ) + it("Should encode the right request identifier", async () => { + const requestIdentifier = encodeRequestIdentifier(appId, groupId, groupTimestamp, namespace); expect(BigNumber.from(requestIdentifier).toString()).toEqual( claimProofPublicInputs.requestIdentifier - ) - }) + ); + }); - it('Should encode the right Accounts Tree value', async () => { - const accountsTreeValue = encodeAccountsTreeValue(groupId, groupTimestamp) + it("Should encode the right Accounts Tree value", async () => { + const accountsTreeValue = encodeAccountsTreeValue(groupId, groupTimestamp); expect(BigNumber.from(accountsTreeValue).toString()).toEqual( claimProofPublicInputs.accountsTreeValue - ) - }) -}) + ); + }); +}); diff --git a/packages/sismo-connect-server/tests/sismo-connect-e2e.test.ts b/packages/sismo-connect-server/tests/sismo-connect-e2e.test.ts index 4799a16..bf0fe8b 100644 --- a/packages/sismo-connect-server/tests/sismo-connect-e2e.test.ts +++ b/packages/sismo-connect-server/tests/sismo-connect-e2e.test.ts @@ -8,55 +8,57 @@ import { SISMO_CONNECT_VERSION, AuthRequest, AuthType, -} from '../src' -import { BigNumber } from '@ethersproject/bignumber' -import { decodeProofData } from '../src/verifier/utils/proofData' -import { JsonRpcProviderMock } from '../src/verifier/libs/onchain-provider' -import { sismoConnectSimpleClaimResponseMock } from './mocks' - -describe('SismoConnect', () => { - let verifiedClaim: VerifiedClaim - let sismoConnect: SismoConnectServer - let sismoConnectResponse: SismoConnectResponse - let appId: string - let groupId: string - let namespace: string - let claimRequest: ClaimRequest - let authRequests: AuthRequest[] - let groupTimestamp: number | 'latest' - let value: number - let claimType: ClaimType - let commitmentMapperPubKey: [string, string] - let signedMessage: string +} from "../src"; +import { BigNumber } from "@ethersproject/bignumber"; +import { decodeProofData } from "../src/verifier/utils/proofData"; +import { JsonRpcProviderMock } from "../src/verifier/libs/onchain-provider"; +import { sismoConnectSimpleClaimResponseMock } from "./mocks"; +describe("SismoConnect", () => { + let verifiedClaim: VerifiedClaim; + let sismoConnect: SismoConnectServer; + let sismoConnectResponse: SismoConnectResponse; + let appId: string; + let groupId: string; + let namespace: string; + let claimRequest: ClaimRequest; + let authRequests: AuthRequest[]; + let groupTimestamp: number | "latest"; + let value: number; + let claimType: ClaimType; + let commitmentMapperPubKey: [string, string]; + let signedMessage: string; beforeEach(async () => { - appId = '0xf4977993e52606cfd67b7a1cde717069'; - groupId = '0x8543f5652418334ff011c1888fd8d96f'; - namespace = 'main'; - groupTimestamp = 'latest'; + appId = "0xf4977993e52606cfd67b7a1cde717069"; + groupId = "0x8543f5652418334ff011c1888fd8d96f"; + namespace = "main"; + groupTimestamp = "latest"; value = 1; claimType = ClaimType.GTE; - signedMessage = 'Hello'; + signedMessage = "Hello"; sismoConnectResponse = sismoConnectSimpleClaimResponseMock; - + const snarkProof = decodeProofData(sismoConnectResponse.proofs[0].proofData); commitmentMapperPubKey = [ BigNumber.from(snarkProof.input[2]).toHexString(), BigNumber.from(snarkProof.input[3]).toHexString(), - ] + ]; claimRequest = { groupId, - } + }; - authRequests = [{ - authType: AuthType.VAULT - }, { - authType: AuthType.TWITTER - }] + authRequests = [ + { + authType: AuthType.VAULT, + }, + { + authType: AuthType.TWITTER, + }, + ]; sismoConnect = SismoConnect({ config: { @@ -70,34 +72,38 @@ describe('SismoConnect', () => { }, }, }, - }) + }); verifiedClaim = { groupId, groupTimestamp, value, claimType, - extraData: '', + extraData: "", isSelectableByUser: false, proofId: BigNumber.from(snarkProof.input[6]).toHexString(), proofData: sismoConnectResponse.proofs[0].proofData, - } - }) + }; + }); + + afterAll(() => { + globalThis.curve_bn128.terminate(); + }); - describe('Sismo Connect server', () => { - describe('verify with statements', () => { - it('should throw with an invalid sismoConnectResponse', async () => { - const invalidSismoConnectResponse = undefined + describe("Sismo Connect server", () => { + describe("verify with statements", () => { + it("should throw with an invalid sismoConnectResponse", async () => { + const invalidSismoConnectResponse = undefined; await expect( sismoConnect.verify(invalidSismoConnectResponse as any, { claims: [claimRequest], namespace, }) - ).rejects.toEqual(Error(`sismoConnectResponse provided is undefined`)) - }) + ).rejects.toEqual(Error(`sismoConnectResponse provided is undefined`)); + }); - it('should throw with an invalid sismoConnectResponse', async () => { - const invalidSismoConnectResponse = {} + it("should throw with an invalid sismoConnectResponse", async () => { + const invalidSismoConnectResponse = {}; await expect( sismoConnect.verify(invalidSismoConnectResponse as any, { claims: [claimRequest], @@ -107,14 +113,14 @@ describe('SismoConnect', () => { Error( `no version provided in your sismoConnectResponse, please use the sismoConnectResponse that was returned by the Sismo vault app` ) - ) - }) + ); + }); - it('should throw with an invalid version', async () => { + it("should throw with an invalid version", async () => { const invalidSismoConnectResponse = JSON.parse( JSON.stringify(sismoConnectResponse) - ) as SismoConnectResponse - invalidSismoConnectResponse.version = 'invalid-version' + ) as SismoConnectResponse; + invalidSismoConnectResponse.version = "invalid-version"; await expect( sismoConnect.verify(invalidSismoConnectResponse, { claims: [claimRequest], @@ -124,14 +130,14 @@ describe('SismoConnect', () => { Error( `version of the sismoConnectResponse "${invalidSismoConnectResponse.version}" not compatible with this version "${SISMO_CONNECT_VERSION}"` ) - ) - }) + ); + }); - it('should throw with an invalid appId', async () => { + it("should throw with an invalid appId", async () => { const invalidSismoConnectResponse = JSON.parse( JSON.stringify(sismoConnectResponse) - ) as SismoConnectResponse - invalidSismoConnectResponse.appId = '0x123' + ) as SismoConnectResponse; + invalidSismoConnectResponse.appId = "0x123"; await expect( sismoConnect.verify(invalidSismoConnectResponse, { claims: [claimRequest], @@ -141,14 +147,14 @@ describe('SismoConnect', () => { Error( `sismoConnectResponse appId "${invalidSismoConnectResponse.appId}" does not match with server appId "${appId}"` ) - ) - }) + ); + }); - it('should throw with an invalid namespace', async () => { + it("should throw with an invalid namespace", async () => { const invalidSismoConnectResponse = JSON.parse( JSON.stringify(sismoConnectResponse) - ) as SismoConnectResponse - invalidSismoConnectResponse.namespace = 'main2' + ) as SismoConnectResponse; + invalidSismoConnectResponse.namespace = "main2"; await expect( sismoConnect.verify(invalidSismoConnectResponse, { claims: [claimRequest], @@ -158,16 +164,16 @@ describe('SismoConnect', () => { Error( `sismoConnectResponse namespace "${invalidSismoConnectResponse.namespace}" does not match with server namespace "${namespace}"` ) - ) - }) + ); + }); - it('should throw with an invalid groupId', async () => { + it("should throw with an invalid groupId", async () => { const invalidSismoConnectResponse = JSON.parse( JSON.stringify(sismoConnectResponse) - ) as SismoConnectResponse - invalidSismoConnectResponse.proofs[0].claims = - invalidSismoConnectResponse.proofs[0].claims as ClaimRequest[] - invalidSismoConnectResponse.proofs[0].claims[0].groupId = '0x123' + ) as SismoConnectResponse; + invalidSismoConnectResponse.proofs[0].claims = invalidSismoConnectResponse.proofs[0] + .claims as ClaimRequest[]; + invalidSismoConnectResponse.proofs[0].claims[0].groupId = "0x123"; await expect( sismoConnect.verify(invalidSismoConnectResponse, { claims: [claimRequest], @@ -177,16 +183,16 @@ describe('SismoConnect', () => { Error( `A required proof is missing for the claimRequest with groupId ${claimRequest.groupId}, groupTimestamp ${claimRequest.groupTimestamp} and claimType ${claimRequest.claimType}` ) - ) - }) + ); + }); - it('should throw with an invalid groupTimestamp', async () => { + it("should throw with an invalid groupTimestamp", async () => { const invalidSismoConnectResponse = JSON.parse( JSON.stringify(sismoConnectResponse) - ) as SismoConnectResponse - invalidSismoConnectResponse.proofs[0].claims = - invalidSismoConnectResponse.proofs[0].claims as ClaimRequest[] - invalidSismoConnectResponse.proofs[0].claims[0].groupTimestamp = 123456 + ) as SismoConnectResponse; + invalidSismoConnectResponse.proofs[0].claims = invalidSismoConnectResponse.proofs[0] + .claims as ClaimRequest[]; + invalidSismoConnectResponse.proofs[0].claims[0].groupTimestamp = 123456; await expect( sismoConnect.verify(invalidSismoConnectResponse, { claims: [claimRequest], @@ -196,16 +202,16 @@ describe('SismoConnect', () => { Error( `A required proof is missing for the claimRequest with groupId ${claimRequest.groupId}, groupTimestamp ${claimRequest.groupTimestamp} and claimType ${claimRequest.claimType}` ) - ) - }) + ); + }); - it('should throw with an invalid comparator', async () => { + it("should throw with an invalid comparator", async () => { const invalidSismoConnectResponse = JSON.parse( JSON.stringify(sismoConnectResponse) - ) as SismoConnectResponse - invalidSismoConnectResponse.proofs[0].claims = - invalidSismoConnectResponse.proofs[0].claims as ClaimRequest[] - invalidSismoConnectResponse.proofs[0].claims[0].claimType = ClaimType.LT + ) as SismoConnectResponse; + invalidSismoConnectResponse.proofs[0].claims = invalidSismoConnectResponse.proofs[0] + .claims as ClaimRequest[]; + invalidSismoConnectResponse.proofs[0].claims[0].claimType = ClaimType.LT; await expect( sismoConnect.verify(invalidSismoConnectResponse, { claims: [claimRequest], @@ -215,17 +221,17 @@ describe('SismoConnect', () => { Error( `A required proof is missing for the claimRequest with groupId ${claimRequest.groupId}, groupTimestamp ${claimRequest.groupTimestamp} and claimType ${claimRequest.claimType}` ) - ) - }) + ); + }); - it('should throw with an invalid value', async () => { + it("should throw with an invalid value", async () => { const invalidSismoConnectResponse = JSON.parse( JSON.stringify(sismoConnectResponse) - ) as SismoConnectResponse + ) as SismoConnectResponse; - invalidSismoConnectResponse.proofs[0].claims = - invalidSismoConnectResponse.proofs[0].claims as ClaimRequest[] - invalidSismoConnectResponse.proofs[0].claims[0].value = -1 + invalidSismoConnectResponse.proofs[0].claims = invalidSismoConnectResponse.proofs[0] + .claims as ClaimRequest[]; + invalidSismoConnectResponse.proofs[0].claims[0].value = -1; await expect( sismoConnect.verify(invalidSismoConnectResponse, { @@ -239,44 +245,38 @@ describe('SismoConnect', () => { `The proof value ${ invalidSismoConnectResponse.proofs[0].claims[0].value } is not equal or greater than the requested value ${ - (sismoConnectResponse.proofs[0].claims as ClaimRequest[])[0] - ?.value + (sismoConnectResponse.proofs[0].claims as ClaimRequest[])[0]?.value }` ) - ) - }) + ); + }); - it('Should verify', async () => { - const sismoConnectVerifiedResult = await sismoConnect.verify( - sismoConnectResponse, - { - claims: [claimRequest], - auths: authRequests, - signature: { message: signedMessage }, - namespace: 'main', - } - ) + it("Should verify", async () => { + const sismoConnectVerifiedResult = await sismoConnect.verify(sismoConnectResponse, { + claims: [claimRequest], + auths: authRequests, + signature: { message: signedMessage }, + namespace: "main", + }); - expect(sismoConnectVerifiedResult.claims[0]).toEqual(verifiedClaim) - }) - }) + expect(sismoConnectVerifiedResult.claims[0]).toEqual(verifiedClaim); + }); + }); - describe('verify without claim', () => { - it('should throw with no claimRequest, no authRequest and no signedMessage', async () => { + describe("verify without claim", () => { + it("should throw with no claimRequest, no authRequest and no signedMessage", async () => { const sismoConnectResponseWithoutSignature = { ...sismoConnectResponse, - signedMessage: undefined - } as SismoConnectResponse + signedMessage: undefined, + } as SismoConnectResponse; await expect( sismoConnect.verify(sismoConnectResponseWithoutSignature, { namespace, }) ).rejects.toEqual( - Error( - `No claim, no auth and no signed message in the proof, please provide at least one` - ) - ) - }) - }) - }) -}) + Error(`No claim, no auth and no signed message in the proof, please provide at least one`) + ); + }); + }); + }); +});