Skip to content

Commit

Permalink
add rest of e2e tests for registry
Browse files Browse the repository at this point in the history
  • Loading branch information
luketchang committed Sep 6, 2023
1 parent 945dea6 commit 56deaaa
Showing 1 changed file with 130 additions and 42 deletions.
172 changes: 130 additions & 42 deletions packages/e2e-tests/tests/CanonAddrRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CanonicalAddressRegistry } from "@nocturne-xyz/contracts";
import {
Asset,
NocturneSigner,
SolidityProof,
compressPoint,
computeCanonAddrRegistryEntryDigest,
packToSolidityProof,
Expand Down Expand Up @@ -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,
Expand All @@ -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 {}
});
});

0 comments on commit 56deaaa

Please sign in to comment.