From 56deaaa0782f5bc08ea3becf72b8b96710241106 Mon Sep 17 00:00:00 2001 From: Luke Tchang Date: Wed, 6 Sep 2023 09:45:37 -0400 Subject: [PATCH] add rest of e2e tests for registry --- packages/e2e-tests/tests/CanonAddrRegistry.ts | 172 +++++++++++++----- 1 file changed, 130 insertions(+), 42 deletions(-) diff --git a/packages/e2e-tests/tests/CanonAddrRegistry.ts b/packages/e2e-tests/tests/CanonAddrRegistry.ts index c3699e34a..d70e77a9c 100644 --- a/packages/e2e-tests/tests/CanonAddrRegistry.ts +++ b/packages/e2e-tests/tests/CanonAddrRegistry.ts @@ -6,6 +6,7 @@ import { CanonicalAddressRegistry } from "@nocturne-xyz/contracts"; import { Asset, NocturneSigner, + SolidityProof, compressPoint, computeCanonAddrRegistryEntryDigest, packToSolidityProof, @@ -55,33 +56,31 @@ describe("Canonical Address Registry", async () => { await teardown(); }); - it("generates sig proof and registers canon addr", async () => { - bobEoa; - nocturneSignerBob; - - console.log("formatting proof inputs..."); - - const aliceCanonAddr = nocturneSignerAlice.canonicalAddress(); - const aliceCanonAddrCompressed = compressPoint(aliceCanonAddr); - const nonce = await canonAddrRegistry._compressedCanonAddrToNonce( - aliceCanonAddrCompressed - ); + async function generateRegisterInputs( + eoa: ethers.Wallet, + nocturneSigner: NocturneSigner + ): Promise<[bigint, SolidityProof]> { + const canonAddr = nocturneSigner.canonicalAddress(); + const compressedCanonAddr = compressPoint(canonAddr); + const nonce = ( + await canonAddrRegistry._compressedCanonAddrToNonce(compressedCanonAddr) + ).toBigInt(); const digest = computeCanonAddrRegistryEntryDigest( { - ethAddress: aliceEoa.address, - perCanonAddrNonce: nonce.toBigInt(), + ethAddress: eoa.address, + perCanonAddrNonce: nonce, }, 31337n, canonAddrRegistry.address ); - const sig = nocturneSignerAlice.sign(digest); + const sig = nocturneSigner.sign(digest); - const spendPubKey = nocturneSignerAlice.spendPk; - const vkNonce = nocturneSignerAlice.vkNonce; + const spendPubKey = nocturneSigner.spendPk; + const vkNonce = nocturneSigner.vkNonce; const proofInputs: CanonAddrSigCheckInputs = { - canonAddr: aliceCanonAddr, + canonAddr, msg: digest, sig, spendPubkey: spendPubKey, @@ -92,39 +91,128 @@ describe("Canonical Address Registry", async () => { proofInputs ); - expect( - ( - await canonAddrRegistry._compressedCanonAddrToNonce( - aliceCanonAddrCompressed - ) - ).toBigInt() - ).to.equal(0n); - expect( - ( - await canonAddrRegistry._ethAddressToCompressedCanonAddr( - aliceEoa.address - ) - ).toBigInt() - ).to.equal(0n); + return [compressedCanonAddr, packToSolidityProof(proof)]; + } + + async function register( + eoa: ethers.Wallet, + compressedCanonAddr: bigint, + proof: SolidityProof + ) { + const nonce = ( + await canonAddrRegistry._compressedCanonAddrToNonce(compressedCanonAddr) + ).toBigInt(); - console.log("submitting call..."); + console.log( + `submitting call to map ${eoa.address} to ${compressedCanonAddr}...` + ); await canonAddrRegistry - .connect(aliceEoa) - .setCanonAddr(aliceCanonAddrCompressed, packToSolidityProof(proof)); + .connect(eoa) + .setCanonAddr(compressedCanonAddr, proof); expect( ( - await canonAddrRegistry._compressedCanonAddrToNonce( - aliceCanonAddrCompressed - ) + await canonAddrRegistry._compressedCanonAddrToNonce(compressedCanonAddr) ).toBigInt() - ).to.equal(1n); + ).to.equal(nonce + 1n); expect( ( - await canonAddrRegistry._ethAddressToCompressedCanonAddr( - aliceEoa.address - ) + await canonAddrRegistry._ethAddressToCompressedCanonAddr(eoa.address) ).toBigInt() - ).to.equal(aliceCanonAddrCompressed); + ).to.equal(compressedCanonAddr); + } + + it("generates sig proof and registers canon addr", async () => { + // Register alice 1st time + let [compressedCanonAddr, proof] = await generateRegisterInputs( + aliceEoa, + nocturneSignerAlice + ); + await register(aliceEoa, compressedCanonAddr, proof); + + // Register alice 2nd time + [compressedCanonAddr, proof] = await generateRegisterInputs( + aliceEoa, + nocturneSignerAlice + ); + await register(aliceEoa, compressedCanonAddr, proof); + + // Register bob 1st time using alice canon addr + [compressedCanonAddr, proof] = await generateRegisterInputs( + bobEoa, + nocturneSignerAlice + ); + await register(bobEoa, compressedCanonAddr, proof); + + // Register bob 2nd time using bob canon addr + [compressedCanonAddr, proof] = await generateRegisterInputs( + bobEoa, + nocturneSignerBob + ); + await register(bobEoa, compressedCanonAddr, proof); + }); + + it("reverts if msg.sender !match digest", async () => { + const [compressedCanonAddr, proof] = await generateRegisterInputs( + aliceEoa, + nocturneSignerAlice + ); + try { + await register(bobEoa, compressedCanonAddr, proof); // send from bobEoa + throw new Error("invalid msg.sender, should have reverted"); + } catch {} + }); + + it("reverts if proof is invalid", async () => { + const [compressedCanonAddr, proof] = await generateRegisterInputs( + aliceEoa, + nocturneSignerAlice + ); + proof[0] += 1n; // invalid proof + + try { + await register(aliceEoa, compressedCanonAddr, proof); + throw new Error("invalid proof, should have reverted"); + } catch {} + }); + + it("reverts if digest is invalid", async () => { + const canonAddr = nocturneSignerAlice.canonicalAddress(); + const compressedCanonAddr = compressPoint(canonAddr); + const nonce = ( + await canonAddrRegistry._compressedCanonAddrToNonce(compressedCanonAddr) + ).toBigInt(); + + const digest = computeCanonAddrRegistryEntryDigest( + { + ethAddress: aliceEoa.address, + perCanonAddrNonce: nonce + 1n, + }, + 31337n, + canonAddrRegistry.address + ); + + const sig = nocturneSignerAlice.sign(digest); + + const spendPubKey = nocturneSignerAlice.spendPk; + const vkNonce = nocturneSignerAlice.vkNonce; + + const proofInputs: CanonAddrSigCheckInputs = { + canonAddr, + msg: digest, + sig, + spendPubkey: spendPubKey, + vkNonce, + }; + + const { proof } = await canonAddrSigCheckProver.proveCanonAddrSigCheck( + proofInputs + ); + try { + await canonAddrRegistry + .connect(aliceEoa) + .setCanonAddr(compressedCanonAddr, packToSolidityProof(proof)); + throw new Error("invalid digest, should have reverted"); + } catch {} }); });