import { BigNumber, utils } from "ethers"; import { EthereumAccount, Commitment } from "./types"; import { OWNERSHIP_SIGNATURE_MESSAGE } from "./constants"; import { buildPoseidon, EddsaAccount, SNARK_FIELD, EddsaPublicKey, EddsaSignature, Poseidon, } from "@sismo-core/crypto"; export class CommitmentMapperTester { private eddsaAccount: EddsaAccount; private mapping: Map; private poseidon; constructor(eddsaAccount: EddsaAccount, poseidon: Poseidon) { this.eddsaAccount = eddsaAccount; this.poseidon = poseidon; this.mapping = new Map(); } public static async generate() { // Secret seed replace by a random seed for the tester version const randomSeed = BigNumber.from(123); const eddsaAccount = await EddsaAccount.generateFromSeed(randomSeed); const poseidon = await buildPoseidon(); return new CommitmentMapperTester(eddsaAccount, poseidon); } public async commit( ethAddress: string, ethAddressSig: string, commitment: Commitment ): Promise { ethAddress = ethAddress.toLowerCase(); const addressFromSignature = utils .verifyMessage(OWNERSHIP_SIGNATURE_MESSAGE, ethAddressSig) .toLowerCase(); if (ethAddress != addressFromSignature) throw new Error("Invalid signature"); const lastCommintment = this.mapping.get(ethAddress); if (lastCommintment && commitment != lastCommintment) throw new Error("Ethereum address already commited with another secret"); this.mapping.set(ethAddress, commitment); const ethAddressBigNumber = BigNumber.from(ethAddress).mod(SNARK_FIELD); const msg = this.poseidon([ethAddressBigNumber, commitment]); const signature = this.eddsaAccount.sign(msg); return signature; } public getPubKey(): EddsaPublicKey { return this.eddsaAccount.getPubKey(); } }