From 34c798113e4fea07c9abccc4fbbbbc4e5075b737 Mon Sep 17 00:00:00 2001 From: mhchia Date: Sun, 2 Jul 2023 00:35:36 +0800 Subject: [PATCH] build: examples use the latest API --- examples/browser/package-lock.json | 8 +- examples/browser/package.json | 2 +- examples/browser/src/index.ts | 30 +++- examples/node/package-lock.json | 218 ++++++++++++++++++++++++----- examples/node/package.json | 2 +- examples/node/src/index.ts | 28 +++- 6 files changed, 232 insertions(+), 56 deletions(-) diff --git a/examples/browser/package-lock.json b/examples/browser/package-lock.json index e8c294c5..9eb9282c 100644 --- a/examples/browser/package-lock.json +++ b/examples/browser/package-lock.json @@ -13,7 +13,7 @@ "@rollup/plugin-json": "^6.0.0", "@rollup/plugin-node-resolve": "^15.0.2", "@rollup/plugin-replace": "^5.0.2", - "rlnjs": "^3.1.3", + "rlnjs": "^3.1.4", "rollup": "^3.20.2", "rollup-plugin-cleaner": "^1.0.0", "rollup-plugin-polyfill-node": "^0.12.0", @@ -1235,9 +1235,9 @@ } }, "node_modules/rlnjs": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/rlnjs/-/rlnjs-3.1.3.tgz", - "integrity": "sha512-YV/xap6WUTFnI332L3qvtDlzD5r4XwOkUIQAqdSjZx3El9wqify3uq5SOTc7zG6lJNz5MAZG8Wbqf0VqYm2G5A==", + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/rlnjs/-/rlnjs-3.1.4.tgz", + "integrity": "sha512-BKTBATi5pofLv3LxP+H2tV4riNkeMB6DSV6TQ9jaMqtv9MHCm165P87QjPa40X/fgomSmhsJU9VPFOuoBcN6XQ==", "dependencies": { "@ethersproject/bytes": "^5.7.0", "@ethersproject/keccak256": "^5.7.0", diff --git a/examples/browser/package.json b/examples/browser/package.json index 8a3e803e..bc193fa9 100644 --- a/examples/browser/package.json +++ b/examples/browser/package.json @@ -21,6 +21,6 @@ "rollup-plugin-polyfill-node": "^0.12.0", "rollup-plugin-typescript2": "^0.34.1", "rollup-plugin-visualizer": "^5.9.0", - "rlnjs": "^3.1.3" + "rlnjs": "^3.1.4" } } diff --git a/examples/browser/src/index.ts b/examples/browser/src/index.ts index 5a0728b7..75a6d369 100644 --- a/examples/browser/src/index.ts +++ b/examples/browser/src/index.ts @@ -1,6 +1,6 @@ import { ethers } from "ethers"; -import { RLN } from "rlnjs"; -import { deployERC20, deployRLNContract, deployVerifier, rlnParams, treeDepth, url, withdrawParams } from "./configs"; +import { ICache, MemoryCache, RLN, Status } from "rlnjs"; +import { deployERC20, deployRLNContract, deployVerifier, rlnParams, treeDepth, url, withdrawParams } from "./configs.js"; async function main() { @@ -42,8 +42,8 @@ async function main() { const rlnContractAtBlock = await provider.getBlockNumber() console.log(`Deployed RLN contract at ${rlnContractAddress} at block ${rlnContractAtBlock}`) - function createRLNInstance() { - return new RLN({ + function createRLNInstance(cache?: ICache) { + return RLN.createWithContractRegistry({ /* Required */ rlnIdentifier, provider, @@ -57,6 +57,7 @@ async function main() { verificationKey: rlnParams.verificationKey, withdrawWasmFilePath: withdrawParams.wasmFilePath, withdrawFinalZkeyPath: withdrawParams.finalZkeyPath, + cache, }) } @@ -116,7 +117,13 @@ async function main() { /* Slash */ console.log("Try `slash` by making rlnAnother create more than " + `${messageLimit} proofs and get slashed by rln`) - const rlnAnother = createRLNInstance() + class ResettableCache extends MemoryCache { + async reset() { + this.cache = {} + } + } + const resettableCache = new ResettableCache() + const rlnAnother = createRLNInstance(resettableCache) console.log(`rlnAnother created: identityCommitment=${rlnAnother.identityCommitment}`) class FaultyMessageIDCounter { private counter: bigint = BigInt(0) @@ -130,16 +137,24 @@ async function main() { } } console.log(`Registering rlnAnother...`) + // Intentionally uses a faulty message ID counter, so that it will use the same message ID + // and exceed the message limit. This will cause it to get slashed. await rlnAnother.register(messageLimit, new FaultyMessageIDCounter(messageLimit)); console.log(`Creating proof0 for rlnAnother...`) const proof0 = await rlnAnother.createProof(epoch, message0); console.log(`Creating proof1 for rlnAnother...`) + // Intentionally clear the cache of rlnAnother, so that it will create a proof which + // will cause a breach and get slashed. + resettableCache.reset() const proof1 = await rlnAnother.createProof(epoch, message1); console.log(`rln saving proof0 from rlnAnother...`) - await rln.saveProof(proof0); + const res0 = await rln.saveProof(proof0); + if (res0.status != Status.VALID) { + throw new Error(`rlnAnother's proof should have been valid`); + } console.log(`rln saving proof1 for rlnAnother...`) const res1 = await rln.saveProof(proof1); - if (res1.status != "breach") { + if (res1.status != Status.BREACH) { throw new Error(`rlnAnother's secret should have been breached`); } const secret = res1.secret as bigint @@ -149,6 +164,7 @@ async function main() { throw new Error(`rlnAnother should have been slashed`); } console.log(`Successfully slashed rlnAnother`); + RLN.cleanUp() } main().catch((e) => { diff --git a/examples/node/package-lock.json b/examples/node/package-lock.json index ea40a960..9db84e65 100644 --- a/examples/node/package-lock.json +++ b/examples/node/package-lock.json @@ -10,7 +10,7 @@ "license": "ISC", "dependencies": { "ethers": "^6.6.0", - "rlnjs": "^3.1.0" + "rlnjs": "^3.1.4" }, "devDependencies": { "hardhat": "^2.15.0" @@ -2286,16 +2286,39 @@ } }, "node_modules/circom_runtime": { - "version": "0.1.20", - "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.20.tgz", - "integrity": "sha512-61AnccA8Ozo5apyDf3hR1JMDDNx1DttKll2bdxVpNjUaTiawDuuYE0VNmRvuoKlcy/WAY+HtD3K994WGrOFhJQ==", + "version": "0.1.22", + "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.22.tgz", + "integrity": "sha512-V/XYZWBhbZY8SotkaGH4FbiDYAZ8a1Md++MBiKPDOuWS/NIJB+Q+XIiTC8zKMgoDaa9cd2OiTvsC9J6te7twNg==", "dependencies": { - "ffjavascript": "0.2.55" + "ffjavascript": "0.2.57" }, "bin": { "calcwit": "calcwit.js" } }, + "node_modules/circom_runtime/node_modules/ffjavascript": { + "version": "0.2.57", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.57.tgz", + "integrity": "sha512-V+vxZ/zPNcthrWmqfe/1YGgqdkTamJeXiED0tsk7B84g40DKlrTdx47IqZuiygqAVG6zMw4qYuvXftIJWsmfKQ==", + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.0", + "web-worker": "^1.2.0" + } + }, + "node_modules/circom_runtime/node_modules/wasmbuilder": { + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/wasmbuilder/-/wasmbuilder-0.0.16.tgz", + "integrity": "sha512-Qx3lEFqaVvp1cEYW7Bfi+ebRJrOiwz2Ieu7ZG2l7YyeSJIok/reEQCQCuicj/Y32ITIJuGIM9xZQppGx5LrQdA==" + }, + "node_modules/circom_runtime/node_modules/wasmcurves": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.2.0.tgz", + "integrity": "sha512-3e2rbxdujOwaod657gxgmdhZNn+i1qKdHO3Y/bK+8E7bV8ttV/fu5FO4/WLBACF375cK0QDLOP+65Na63qYuWA==", + "dependencies": { + "wasmbuilder": "0.0.16" + } + }, "node_modules/classic-level": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/classic-level/-/classic-level-1.3.0.tgz", @@ -4044,14 +4067,37 @@ ] }, "node_modules/r1csfile": { - "version": "0.0.40", - "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.40.tgz", - "integrity": "sha512-3tKaFLncf42ZTRpPMlgyiFBdk6kir4S4O3X+u4UQjgLYoDPHfizazNbK0Jzj++PVIXVUFAqugSbIo4W3UDuHcQ==", + "version": "0.0.45", + "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.45.tgz", + "integrity": "sha512-YKIp4D441aZ6OoI9y+bfAyb2j4Cl+OFq/iiX6pPWDrL4ZO968h0dq0w07i65edvrTt7/G43mTnl0qEuLXyp/Yw==", "dependencies": { "@iden3/bigarray": "0.0.2", "@iden3/binfileutils": "0.0.11", "fastfile": "0.0.20", - "ffjavascript": "0.2.55" + "ffjavascript": "0.2.57" + } + }, + "node_modules/r1csfile/node_modules/ffjavascript": { + "version": "0.2.57", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.57.tgz", + "integrity": "sha512-V+vxZ/zPNcthrWmqfe/1YGgqdkTamJeXiED0tsk7B84g40DKlrTdx47IqZuiygqAVG6zMw4qYuvXftIJWsmfKQ==", + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.0", + "web-worker": "^1.2.0" + } + }, + "node_modules/r1csfile/node_modules/wasmbuilder": { + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/wasmbuilder/-/wasmbuilder-0.0.16.tgz", + "integrity": "sha512-Qx3lEFqaVvp1cEYW7Bfi+ebRJrOiwz2Ieu7ZG2l7YyeSJIok/reEQCQCuicj/Y32ITIJuGIM9xZQppGx5LrQdA==" + }, + "node_modules/r1csfile/node_modules/wasmcurves": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.2.0.tgz", + "integrity": "sha512-3e2rbxdujOwaod657gxgmdhZNn+i1qKdHO3Y/bK+8E7bV8ttV/fu5FO4/WLBACF375cK0QDLOP+65Na63qYuWA==", + "dependencies": { + "wasmbuilder": "0.0.16" } }, "node_modules/randombytes": { @@ -4157,9 +4203,9 @@ } }, "node_modules/rlnjs": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/rlnjs/-/rlnjs-3.1.0.tgz", - "integrity": "sha512-XTfJD56LLSTwlhNgWiTEM68u9IwBIP4KBK7yfwKJ0rjVVrcTglwYex7vX4ntdhGfpZQYhI0Z2yjoZZxh/3OL7w==", + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/rlnjs/-/rlnjs-3.1.4.tgz", + "integrity": "sha512-BKTBATi5pofLv3LxP+H2tV4riNkeMB6DSV6TQ9jaMqtv9MHCm165P87QjPa40X/fgomSmhsJU9VPFOuoBcN6XQ==", "dependencies": { "@ethersproject/bytes": "^5.7.0", "@ethersproject/keccak256": "^5.7.0", @@ -4171,7 +4217,7 @@ "ethers": "^6.4.0", "ffjavascript": "0.2.55", "poseidon-lite": "^0.0.2", - "snarkjs": "^0.4.22" + "snarkjs": "^0.7.0" } }, "node_modules/rlp": { @@ -4306,25 +4352,48 @@ } }, "node_modules/snarkjs": { - "version": "0.4.27", - "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.4.27.tgz", - "integrity": "sha512-2CH4JpOIkaoEiPvc/d9eiA7Vs0mC2ZnQAhFIFF+qp8eVxhHpDXFZn50hEZhcb8lypGry8ZiiEQ73a3hOFOUbYQ==", + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.7.0.tgz", + "integrity": "sha512-Vu5W+0Va6X1xvlCllpZ2r3/S7MafnL6IrAv09lk/F+VNDHuHEHx3xopR9Kr70p2KpbBBJ/HB9VCDZWism8WGlA==", "dependencies": { "@iden3/binfileutils": "0.0.11", "bfj": "^7.0.2", "blake2b-wasm": "^2.4.0", - "circom_runtime": "0.1.20", + "circom_runtime": "0.1.22", "ejs": "^3.1.6", "fastfile": "0.0.20", - "ffjavascript": "0.2.55", + "ffjavascript": "0.2.59", "js-sha3": "^0.8.0", "logplease": "^1.2.15", - "r1csfile": "0.0.40" + "r1csfile": "0.0.45" }, "bin": { "snarkjs": "build/cli.cjs" } }, + "node_modules/snarkjs/node_modules/ffjavascript": { + "version": "0.2.59", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.59.tgz", + "integrity": "sha512-QssOEUv+wilz9Sg7Zaj6KWAm7QceOAEsFuEBTltUsDo1cjn11rA/LGYvzFBPbzNfxRlZxwgJ7uxpCQcdDlrNfw==", + "dependencies": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.1", + "web-worker": "^1.2.0" + } + }, + "node_modules/snarkjs/node_modules/wasmbuilder": { + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/wasmbuilder/-/wasmbuilder-0.0.16.tgz", + "integrity": "sha512-Qx3lEFqaVvp1cEYW7Bfi+ebRJrOiwz2Ieu7ZG2l7YyeSJIok/reEQCQCuicj/Y32ITIJuGIM9xZQppGx5LrQdA==" + }, + "node_modules/snarkjs/node_modules/wasmcurves": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.2.1.tgz", + "integrity": "sha512-9ciO7bUE5bgpbOcdK7IO3enrSVIKHwrQmPibok4GLJWaCA7Wyqc9PRYnu5HbiFv9NDFNqVKPtU5R6Is5KujBLg==", + "dependencies": { + "wasmbuilder": "0.0.16" + } + }, "node_modules/solc": { "version": "0.7.3", "resolved": "https://registry.npmjs.org/solc/-/solc-0.7.3.tgz", @@ -6458,11 +6527,36 @@ } }, "circom_runtime": { - "version": "0.1.20", - "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.20.tgz", - "integrity": "sha512-61AnccA8Ozo5apyDf3hR1JMDDNx1DttKll2bdxVpNjUaTiawDuuYE0VNmRvuoKlcy/WAY+HtD3K994WGrOFhJQ==", + "version": "0.1.22", + "resolved": "https://registry.npmjs.org/circom_runtime/-/circom_runtime-0.1.22.tgz", + "integrity": "sha512-V/XYZWBhbZY8SotkaGH4FbiDYAZ8a1Md++MBiKPDOuWS/NIJB+Q+XIiTC8zKMgoDaa9cd2OiTvsC9J6te7twNg==", "requires": { - "ffjavascript": "0.2.55" + "ffjavascript": "0.2.57" + }, + "dependencies": { + "ffjavascript": { + "version": "0.2.57", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.57.tgz", + "integrity": "sha512-V+vxZ/zPNcthrWmqfe/1YGgqdkTamJeXiED0tsk7B84g40DKlrTdx47IqZuiygqAVG6zMw4qYuvXftIJWsmfKQ==", + "requires": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.0", + "web-worker": "^1.2.0" + } + }, + "wasmbuilder": { + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/wasmbuilder/-/wasmbuilder-0.0.16.tgz", + "integrity": "sha512-Qx3lEFqaVvp1cEYW7Bfi+ebRJrOiwz2Ieu7ZG2l7YyeSJIok/reEQCQCuicj/Y32ITIJuGIM9xZQppGx5LrQdA==" + }, + "wasmcurves": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.2.0.tgz", + "integrity": "sha512-3e2rbxdujOwaod657gxgmdhZNn+i1qKdHO3Y/bK+8E7bV8ttV/fu5FO4/WLBACF375cK0QDLOP+65Na63qYuWA==", + "requires": { + "wasmbuilder": "0.0.16" + } + } } }, "classic-level": { @@ -7759,14 +7853,39 @@ "dev": true }, "r1csfile": { - "version": "0.0.40", - "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.40.tgz", - "integrity": "sha512-3tKaFLncf42ZTRpPMlgyiFBdk6kir4S4O3X+u4UQjgLYoDPHfizazNbK0Jzj++PVIXVUFAqugSbIo4W3UDuHcQ==", + "version": "0.0.45", + "resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.45.tgz", + "integrity": "sha512-YKIp4D441aZ6OoI9y+bfAyb2j4Cl+OFq/iiX6pPWDrL4ZO968h0dq0w07i65edvrTt7/G43mTnl0qEuLXyp/Yw==", "requires": { "@iden3/bigarray": "0.0.2", "@iden3/binfileutils": "0.0.11", "fastfile": "0.0.20", - "ffjavascript": "0.2.55" + "ffjavascript": "0.2.57" + }, + "dependencies": { + "ffjavascript": { + "version": "0.2.57", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.57.tgz", + "integrity": "sha512-V+vxZ/zPNcthrWmqfe/1YGgqdkTamJeXiED0tsk7B84g40DKlrTdx47IqZuiygqAVG6zMw4qYuvXftIJWsmfKQ==", + "requires": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.0", + "web-worker": "^1.2.0" + } + }, + "wasmbuilder": { + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/wasmbuilder/-/wasmbuilder-0.0.16.tgz", + "integrity": "sha512-Qx3lEFqaVvp1cEYW7Bfi+ebRJrOiwz2Ieu7ZG2l7YyeSJIok/reEQCQCuicj/Y32ITIJuGIM9xZQppGx5LrQdA==" + }, + "wasmcurves": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.2.0.tgz", + "integrity": "sha512-3e2rbxdujOwaod657gxgmdhZNn+i1qKdHO3Y/bK+8E7bV8ttV/fu5FO4/WLBACF375cK0QDLOP+65Na63qYuWA==", + "requires": { + "wasmbuilder": "0.0.16" + } + } } }, "randombytes": { @@ -7851,9 +7970,9 @@ } }, "rlnjs": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/rlnjs/-/rlnjs-3.1.0.tgz", - "integrity": "sha512-XTfJD56LLSTwlhNgWiTEM68u9IwBIP4KBK7yfwKJ0rjVVrcTglwYex7vX4ntdhGfpZQYhI0Z2yjoZZxh/3OL7w==", + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/rlnjs/-/rlnjs-3.1.4.tgz", + "integrity": "sha512-BKTBATi5pofLv3LxP+H2tV4riNkeMB6DSV6TQ9jaMqtv9MHCm165P87QjPa40X/fgomSmhsJU9VPFOuoBcN6XQ==", "requires": { "@ethersproject/bytes": "^5.7.0", "@ethersproject/keccak256": "^5.7.0", @@ -7865,7 +7984,7 @@ "ethers": "^6.4.0", "ffjavascript": "0.2.55", "poseidon-lite": "^0.0.2", - "snarkjs": "^0.4.22" + "snarkjs": "^0.7.0" } }, "rlp": { @@ -7970,20 +8089,45 @@ } }, "snarkjs": { - "version": "0.4.27", - "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.4.27.tgz", - "integrity": "sha512-2CH4JpOIkaoEiPvc/d9eiA7Vs0mC2ZnQAhFIFF+qp8eVxhHpDXFZn50hEZhcb8lypGry8ZiiEQ73a3hOFOUbYQ==", + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/snarkjs/-/snarkjs-0.7.0.tgz", + "integrity": "sha512-Vu5W+0Va6X1xvlCllpZ2r3/S7MafnL6IrAv09lk/F+VNDHuHEHx3xopR9Kr70p2KpbBBJ/HB9VCDZWism8WGlA==", "requires": { "@iden3/binfileutils": "0.0.11", "bfj": "^7.0.2", "blake2b-wasm": "^2.4.0", - "circom_runtime": "0.1.20", + "circom_runtime": "0.1.22", "ejs": "^3.1.6", "fastfile": "0.0.20", - "ffjavascript": "0.2.55", + "ffjavascript": "0.2.59", "js-sha3": "^0.8.0", "logplease": "^1.2.15", - "r1csfile": "0.0.40" + "r1csfile": "0.0.45" + }, + "dependencies": { + "ffjavascript": { + "version": "0.2.59", + "resolved": "https://registry.npmjs.org/ffjavascript/-/ffjavascript-0.2.59.tgz", + "integrity": "sha512-QssOEUv+wilz9Sg7Zaj6KWAm7QceOAEsFuEBTltUsDo1cjn11rA/LGYvzFBPbzNfxRlZxwgJ7uxpCQcdDlrNfw==", + "requires": { + "wasmbuilder": "0.0.16", + "wasmcurves": "0.2.1", + "web-worker": "^1.2.0" + } + }, + "wasmbuilder": { + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/wasmbuilder/-/wasmbuilder-0.0.16.tgz", + "integrity": "sha512-Qx3lEFqaVvp1cEYW7Bfi+ebRJrOiwz2Ieu7ZG2l7YyeSJIok/reEQCQCuicj/Y32ITIJuGIM9xZQppGx5LrQdA==" + }, + "wasmcurves": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/wasmcurves/-/wasmcurves-0.2.1.tgz", + "integrity": "sha512-9ciO7bUE5bgpbOcdK7IO3enrSVIKHwrQmPibok4GLJWaCA7Wyqc9PRYnu5HbiFv9NDFNqVKPtU5R6Is5KujBLg==", + "requires": { + "wasmbuilder": "0.0.16" + } + } } }, "solc": { diff --git a/examples/node/package.json b/examples/node/package.json index 3de0d9ab..3664e83f 100644 --- a/examples/node/package.json +++ b/examples/node/package.json @@ -12,7 +12,7 @@ "license": "ISC", "dependencies": { "ethers": "^6.6.0", - "rlnjs": "^3.1.0" + "rlnjs": "^3.1.4" }, "devDependencies": { "hardhat": "^2.15.0" diff --git a/examples/node/src/index.ts b/examples/node/src/index.ts index 1f277461..75a6d369 100644 --- a/examples/node/src/index.ts +++ b/examples/node/src/index.ts @@ -1,5 +1,5 @@ import { ethers } from "ethers"; -import { RLN } from "rlnjs"; +import { ICache, MemoryCache, RLN, Status } from "rlnjs"; import { deployERC20, deployRLNContract, deployVerifier, rlnParams, treeDepth, url, withdrawParams } from "./configs.js"; @@ -42,8 +42,8 @@ async function main() { const rlnContractAtBlock = await provider.getBlockNumber() console.log(`Deployed RLN contract at ${rlnContractAddress} at block ${rlnContractAtBlock}`) - function createRLNInstance() { - return new RLN({ + function createRLNInstance(cache?: ICache) { + return RLN.createWithContractRegistry({ /* Required */ rlnIdentifier, provider, @@ -57,6 +57,7 @@ async function main() { verificationKey: rlnParams.verificationKey, withdrawWasmFilePath: withdrawParams.wasmFilePath, withdrawFinalZkeyPath: withdrawParams.finalZkeyPath, + cache, }) } @@ -116,7 +117,13 @@ async function main() { /* Slash */ console.log("Try `slash` by making rlnAnother create more than " + `${messageLimit} proofs and get slashed by rln`) - const rlnAnother = createRLNInstance() + class ResettableCache extends MemoryCache { + async reset() { + this.cache = {} + } + } + const resettableCache = new ResettableCache() + const rlnAnother = createRLNInstance(resettableCache) console.log(`rlnAnother created: identityCommitment=${rlnAnother.identityCommitment}`) class FaultyMessageIDCounter { private counter: bigint = BigInt(0) @@ -130,16 +137,24 @@ async function main() { } } console.log(`Registering rlnAnother...`) + // Intentionally uses a faulty message ID counter, so that it will use the same message ID + // and exceed the message limit. This will cause it to get slashed. await rlnAnother.register(messageLimit, new FaultyMessageIDCounter(messageLimit)); console.log(`Creating proof0 for rlnAnother...`) const proof0 = await rlnAnother.createProof(epoch, message0); console.log(`Creating proof1 for rlnAnother...`) + // Intentionally clear the cache of rlnAnother, so that it will create a proof which + // will cause a breach and get slashed. + resettableCache.reset() const proof1 = await rlnAnother.createProof(epoch, message1); console.log(`rln saving proof0 from rlnAnother...`) - await rln.saveProof(proof0); + const res0 = await rln.saveProof(proof0); + if (res0.status != Status.VALID) { + throw new Error(`rlnAnother's proof should have been valid`); + } console.log(`rln saving proof1 for rlnAnother...`) const res1 = await rln.saveProof(proof1); - if (res1.status != "breach") { + if (res1.status != Status.BREACH) { throw new Error(`rlnAnother's secret should have been breached`); } const secret = res1.secret as bigint @@ -149,6 +164,7 @@ async function main() { throw new Error(`rlnAnother should have been slashed`); } console.log(`Successfully slashed rlnAnother`); + RLN.cleanUp() } main().catch((e) => {