Skip to content

Commit

Permalink
Add registry event and test
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <jim.zhang@kaleido.io>
  • Loading branch information
jimthematrix committed Aug 7, 2024
1 parent 66197cd commit add8e32
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions solidity/contracts/lib/registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ abstract contract Registry {
SmtLib.Data internal _publicKeysTree;
using SmtLib for SmtLib.Data;

event IdentityRegistered(uint256[2] publicKey);

error AlreadyRegistered(uint256[2]);

constructor() {
Expand All @@ -47,6 +49,7 @@ abstract contract Registry {
revert AlreadyRegistered(publicKey);
}
_publicKeysTree.addLeaf(nodeHash, nodeHash);
emit IdentityRegistered(publicKey);
}

/// @dev returns whether the given public key is registered
Expand Down
10 changes: 10 additions & 0 deletions solidity/test/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ export function parseUTXOEvents(zetoTokenContract: any, result: ContractTransact
}
return returnValues;
}

export function parseRegistryEvents(registryContract: any, result: ContractTransactionReceipt) {
for (const log of result.logs || []) {
const event = registryContract.interface.parseLog(log as any);
if (event?.name === 'IdentityRegistered') {
return event?.args.publicKey;
}
}
return null;
}
18 changes: 11 additions & 7 deletions solidity/test/zeto_anon_nullifier_kyc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { groth16 } from 'snarkjs';
import { Merkletree, InMemoryDB, str2Bytes } from '@iden3/js-merkletree';
import zetoModule from '../ignition/modules/zeto_anon_nullifier_kyc';
import erc20Module from '../ignition/modules/erc20';
import { UTXO, User, newUser, newUTXO, newNullifier, doMint, ZERO_UTXO, parseUTXOEvents } from './lib/utils';
import { UTXO, User, newUser, newUTXO, newNullifier, doMint, ZERO_UTXO, parseUTXOEvents, parseRegistryEvents } from './lib/utils';
import { loadProvingKeys, prepareDepositProof, prepareNullifierWithdrawProof } from './utils';

describe("Zeto based fungible token with anonymity, KYC, using nullifiers without encryption", function () {
Expand Down Expand Up @@ -58,11 +58,11 @@ describe("Zeto based fungible token with anonymity, KYC, using nullifiers withou
await tx1.wait();

const tx2 = await zeto.connect(deployer).register(Alice.babyJubPublicKey);
await tx2.wait();
const result1 = await tx2.wait();
const tx3 = await zeto.connect(deployer).register(Bob.babyJubPublicKey);
await tx3.wait();
const result2 = await tx3.wait();
const tx4 = await zeto.connect(deployer).register(Charlie.babyJubPublicKey);
await tx4.wait();
const result3 = await tx4.wait();

circuit = await loadCircuit('anon_nullifier_kyc');
({ provingKeyFile: provingKey } = loadProvingKeys('anon_nullifier_kyc'));
Expand All @@ -75,9 +75,13 @@ describe("Zeto based fungible token with anonymity, KYC, using nullifiers withou

const storage3 = new InMemoryDB(str2Bytes(""))
smtKyc = new Merkletree(storage3, true, 10);
await smtKyc.add(kycHash(Alice.babyJubPublicKey), kycHash(Alice.babyJubPublicKey));
await smtKyc.add(kycHash(Bob.babyJubPublicKey), kycHash(Bob.babyJubPublicKey));
await smtKyc.add(kycHash(Charlie.babyJubPublicKey), kycHash(Charlie.babyJubPublicKey));

const publicKey1 = parseRegistryEvents(zeto, result1);
await smtKyc.add(kycHash(publicKey1), kycHash(publicKey1));
const publicKey2 = parseRegistryEvents(zeto, result2);
await smtKyc.add(kycHash(publicKey2), kycHash(publicKey2));
const publicKey3 = parseRegistryEvents(zeto, result3);
await smtKyc.add(kycHash(publicKey3), kycHash(publicKey3));
});

it("onchain SMT root should be equal to the offchain SMT root", async function () {
Expand Down

0 comments on commit add8e32

Please sign in to comment.