From 4fb09c83c7fb9724742a76590792966192de00d8 Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Thu, 25 Jul 2024 10:45:09 +0100 Subject: [PATCH] move demo scripts --- demo-config.json | 4 +++- package.json | 10 +++++----- scripts/{ => demo}/admin.ts | 31 ++++++++++++++++--------------- scripts/{ => demo}/nidkg.ts | 12 +++++++----- scripts/{ => demo}/random.ts | 21 +++++++++++---------- scripts/{ => demo}/register.ts | 15 ++++++++------- scripts/lottery/admin.ts | 7 +++++-- scripts/lottery/deploy.ts | 23 ++++++++++++----------- scripts/lottery/play.ts | 3 ++- scripts/utils.ts | 2 +- 10 files changed, 70 insertions(+), 58 deletions(-) rename scripts/{ => demo}/admin.ts (92%) rename scripts/{ => demo}/nidkg.ts (94%) rename scripts/{ => demo}/random.ts (86%) rename scripts/{ => demo}/register.ts (88%) diff --git a/demo-config.json b/demo-config.json index e8e307c..6453432 100644 --- a/demo-config.json +++ b/demo-config.json @@ -1,5 +1,7 @@ { - "zkdvrfAddress": "0x1C2FfFeEee7d396597D60adAdAC63c2c48B829AF", + "rpcUrl": "http://localhost:8545", + "zkdvrfAddress": "0x9B27Cd80c4B3823a94334d9285001F0cE959a37b", + "adminKey": "0x58936e48436d9764c0c09775444fe6700d0a8aa84d4d6c09e110f70fc87c7289", "memberAddresses": [ "0x85A6bCc74CB8570BEfF8526aec7d0Dfb6F128A60", "0x7D9e5627B650650Fa3E848364Cb5B0b696B885f2", diff --git a/package.json b/package.json index 8998c32..b57b86f 100644 --- a/package.json +++ b/package.json @@ -33,12 +33,12 @@ "test": "npx hardhat test", "deploy": "npx hardhat run scripts/deploy.ts", "build": "npx hardhat compile", - "admin": "npx hardhat run scripts/admin.ts", - "admin:restart": "RESTART=true npx hardhat run scripts/admin.ts", - "register": "npx hardhat run scripts/register.ts", - "nidkg": "npx hardhat run scripts/nidkg.ts", + "admin": "npx hardhat run scripts/demo/admin.ts", + "admin:restart": "RESTART=true npx hardhat run scripts/demo/admin.ts", + "register": "npx hardhat run scripts/demo/register.ts", + "nidkg": "npx hardhat run scripts/demo/nidkg.ts", "member": "yarn register && yarn nidkg", - "random": "npx hardhat run scripts/random.ts", + "random": "npx hardhat run scripts/demo/random.ts", "lottery:deploy": "npx hardhat run scripts/lottery/deploy.ts", "lottery:admin": "npx hardhat run scripts/lottery/admin.ts", "lottery:play": "npx hardhat run scripts/lottery/play.ts", diff --git a/scripts/admin.ts b/scripts/demo/admin.ts similarity index 92% rename from scripts/admin.ts rename to scripts/demo/admin.ts index e6500c8..d7d991d 100644 --- a/scripts/admin.ts +++ b/scripts/demo/admin.ts @@ -9,10 +9,12 @@ import { sleep, waitForWriteJsonToFile, writeJsonToFile -} from "./utils"; -import { createInterface } from "readline"; +} from "../utils"; +import {createInterface} from "readline"; const config = readJsonFromFile("demo-config.json") +const rpcUrl = config.rpcUrl +const adminKey = config.adminKey const zkdvrfAddress = config.zkdvrfAddress const memberAdresses = config.memberAddresses @@ -21,11 +23,10 @@ interface Eval { } async function main() { - const netprovider = new providers.JsonRpcProvider(process.env.RPC_URL) - const accPrivateKey = process.env.PRIVATE_KEY ?? '' - const adminWallet = new Wallet(accPrivateKey, netprovider) + const netprovider = new providers.JsonRpcProvider(rpcUrl) + const adminWallet = new Wallet(adminKey, netprovider) - const Zkdvrf = await ethers.getContractFactory('zkdvrf') + const Zkdvrf = await ethers.getContractFactory('zkdvrf_pre') const contractABI = Zkdvrf.interface.format(); const contract = new ethers.Contract(zkdvrfAddress, contractABI, netprovider).connect(adminWallet) @@ -39,7 +40,7 @@ async function main() { if (!restart) { for (let i = 0; i < memberAdresses.length; i++) { const res = await contract.addPermissionedNodes(memberAdresses[i]) - // console.log(res) + // console.log(res) console.log("added member", memberAdresses[i]) } } @@ -73,7 +74,7 @@ async function main() { console.log("\nevent", eventDkg, count); // read all instances from contract const ppList = await contract.getPpList() - // console.log("\nppList = ", ppList) + // console.log("\nppList = ", ppList) // save ppList for rust backend const ppListHex = ppList.map(subList => subList.map(num => num.toHexString()) @@ -179,13 +180,13 @@ async function main() { const value = {x: evals[i][1][0].toHexString(), y: evals[i][1][1].toHexString()} const proof = {z: evals[i][2][0].toHexString(), c: evals[i][2][1].toHexString()} - const sigma = { - index: index, - value: value, - proof: proof, - } + const sigma = { + index: index, + value: value, + proof: proof, + } - pEvals.push(sigma) + pEvals.push(sigma) } const obj = JSON.stringify(pEvals) @@ -228,7 +229,7 @@ async function main() { // start listening for event const eventRandReady = `RandomReady` - contract.on(eventRandReady, async (roundNum, roundInput, event) => { + contract.on(eventRandReady, async (roundNum, roundInput, event) => { rl.question("\n 🔔 Do you want to initiate random again? (yes/no): ", async (answer) => { if (answer.toLowerCase() === "yes") { await initiateRand(eventRandReady); diff --git a/scripts/nidkg.ts b/scripts/demo/nidkg.ts similarity index 94% rename from scripts/nidkg.ts rename to scripts/demo/nidkg.ts index 8df1098..31f970e 100644 --- a/scripts/nidkg.ts +++ b/scripts/demo/nidkg.ts @@ -10,16 +10,18 @@ import { execPromise, instancesPath, waitForWriteJsonToFile -} from "./utils"; +} from "../utils"; const config = readJsonFromFile("demo-config.json") +const rpcUrl = config.rpcUrl const zkdvrfAddress = config.zkdvrfAddress const memberKeys = config.memberKeys + async function main() { - const netprovider = new providers.JsonRpcProvider(process.env.RPC_URL) + const netprovider = new providers.JsonRpcProvider(rpcUrl) - const Zkdvrf = await ethers.getContractFactory('zkdvrf') + const Zkdvrf = await ethers.getContractFactory('zkdvrf_pre') const contractABI = Zkdvrf.interface.format(); const contract = new ethers.Contract(zkdvrfAddress, contractABI, netprovider); @@ -52,9 +54,9 @@ async function main() { const cmd = `RUST_LOG=info ./target/release/client dkg derive` for (let i = 0; i < memberKeys.length; i++) { const index = indices[i] - const cmdMember = cmd + ` ${index} -f member_${i+1}` + const cmdMember = cmd + ` ${index} -f member_${i + 1}` console.log("running command <", cmdMember, ">...") - const res = await execPromise(cmdMember) + const res = await execPromise(cmdMember) console.log(res[`stderr`]) } diff --git a/scripts/random.ts b/scripts/demo/random.ts similarity index 86% rename from scripts/random.ts rename to scripts/demo/random.ts index 59a67c9..7f56b9f 100644 --- a/scripts/random.ts +++ b/scripts/demo/random.ts @@ -1,17 +1,18 @@ import hre, {artifacts, ethers} from "hardhat"; import {Contract, ContractFactory, providers, utils, Wallet} from "ethers"; -import { promisify } from 'util'; -import { exec } from "child_process"; -import {readJsonFromFile, writeJsonToFile, memberDir, mpksPath, execPromise, randDir} from "./utils"; +import {promisify} from 'util'; +import {exec} from "child_process"; +import {readJsonFromFile, writeJsonToFile, memberDir, mpksPath, execPromise, randDir} from "../utils"; const config = readJsonFromFile("demo-config.json") +const rpcUrl = config.rpcUrl const zkdvrfAddress = config.zkdvrfAddress const memberKeys = config.memberKeys async function main() { - const netprovider = new providers.JsonRpcProvider(process.env.RPC_URL) + const netprovider = new providers.JsonRpcProvider(rpcUrl) - const Zkdvrf = await ethers.getContractFactory('zkdvrf') + const Zkdvrf = await ethers.getContractFactory('zkdvrf_pre') const contractABI = Zkdvrf.interface.format(); const contract = new ethers.Contract(zkdvrfAddress, contractABI, netprovider); @@ -60,8 +61,8 @@ async function main() { main().then(() => { process.exit(0); - }) - .catch((error) => { - console.error(error); - process.exitCode = 1; - }); \ No newline at end of file +}) + .catch((error) => { + console.error(error); + process.exitCode = 1; + }); \ No newline at end of file diff --git a/scripts/register.ts b/scripts/demo/register.ts similarity index 88% rename from scripts/register.ts rename to scripts/demo/register.ts index f0322d6..b247b7d 100644 --- a/scripts/register.ts +++ b/scripts/demo/register.ts @@ -1,17 +1,18 @@ import hre, {artifacts, ethers} from "hardhat"; import {Contract, ContractFactory, providers, utils, Wallet} from "ethers"; -import { promisify } from 'util'; -import { exec } from "child_process"; -import {readJsonFromFile, writeJsonToFile, waitForWriteJsonToFile, memberDir, mpksPath, execPromise} from "./utils"; +import {promisify} from 'util'; +import {exec} from "child_process"; +import {readJsonFromFile, writeJsonToFile, waitForWriteJsonToFile, memberDir, mpksPath, execPromise} from "../utils"; const config = readJsonFromFile("demo-config.json") +const rpcUrl = config.rpcUrl const zkdvrfAddress = config.zkdvrfAddress const memberKeys = config.memberKeys async function main() { - const netprovider = new providers.JsonRpcProvider(process.env.RPC_URL) + const netprovider = new providers.JsonRpcProvider(rpcUrl) - const Zkdvrf = await ethers.getContractFactory('zkdvrf') + const Zkdvrf = await ethers.getContractFactory('zkdvrf_pre') const contractABI = Zkdvrf.interface.format(); const contract = new ethers.Contract(zkdvrfAddress, contractABI, netprovider); @@ -37,7 +38,7 @@ async function main() { const memberContract = contract.connect(memberWallet) // generate member secret key and member public key on grumpkin curve - const index = i+1 + const index = i + 1 const file = `member_${index}` const command = `RUST_LOG=info ./target/release/client keygen -f ${file}` @@ -65,4 +66,4 @@ async function main() { main().catch((error) => { console.error(error); process.exitCode = 1; - }); +}); diff --git a/scripts/lottery/admin.ts b/scripts/lottery/admin.ts index e44ac00..62d12ef 100644 --- a/scripts/lottery/admin.ts +++ b/scripts/lottery/admin.ts @@ -3,12 +3,13 @@ import {Contract, ContractFactory, providers, utils, Wallet} from "ethers"; import {readJsonFromFile} from "../utils"; const config = readJsonFromFile("demo-config.json") +const rpcUrl = config.rpcUrl const zkdvrfAddress = config.zkdvrfAddress const lotteryAddress = config.lotteryAddress const adminKey = config.lotteryAdminKey async function main() { - const netprovider = new providers.JsonRpcProvider(process.env.RPC_URL) + const netprovider = new providers.JsonRpcProvider(rpcUrl) const adminWallet = new Wallet(adminKey, netprovider) const Lottery = await ethers.getContractFactory('Lottery') @@ -38,6 +39,8 @@ async function main() { console.log("event", eventName, roundNum, input); // Proceed to the next step here if (roundNum == randRoundNumber) { + const playersBefore = await contract.getPlayers() + console.log("Players before:", playersBefore) // the random number is ready const res = await contract.pickWinner() // Check if the transaction was successful @@ -52,7 +55,7 @@ async function main() { console.log("Lottery contract status:", status) const players = await contract.getPlayers() - console.log("Players:", players) + console.log("Players after:", players) // query users balance for (let i = 0; i < players.length; i++) { diff --git a/scripts/lottery/deploy.ts b/scripts/lottery/deploy.ts index 9f915ff..e4fff01 100644 --- a/scripts/lottery/deploy.ts +++ b/scripts/lottery/deploy.ts @@ -3,24 +3,25 @@ import {Contract, ContractFactory, providers, utils, Wallet} from "ethers"; import {readJsonFromFile} from "../utils"; const config = readJsonFromFile("demo-config.json") +const rpcUrl = config.rpcUrl const zkdvrfAddress = config.zkdvrfAddress const adminKey = config.lotteryAdminKey async function main() { - const netprovider = new providers.JsonRpcProvider(process.env.RPC_URL) - const deployerWallet = new Wallet(adminKey, netprovider) + const netprovider = new providers.JsonRpcProvider(rpcUrl) + const deployerWallet = new Wallet(adminKey, netprovider) - const Lottery = await ethers.getContractFactory('Lottery') - const lottery = await Lottery.connect(deployerWallet).deploy(zkdvrfAddress) - await lottery.deployed() + const Lottery = await ethers.getContractFactory('Lottery') + const lottery = await Lottery.connect(deployerWallet).deploy(zkdvrfAddress) + await lottery.deployed() - console.log("Lottery contract deployed at", lottery.address) + console.log("Lottery contract deployed at", lottery.address) } main().then(() => { - process.exit(0); + process.exit(0); }) -.catch((error) => { - console.error(error); - process.exitCode = 1; -}); + .catch((error) => { + console.error(error); + process.exitCode = 1; + }); diff --git a/scripts/lottery/play.ts b/scripts/lottery/play.ts index ba9588e..4ce7267 100644 --- a/scripts/lottery/play.ts +++ b/scripts/lottery/play.ts @@ -3,12 +3,13 @@ import {providers, Wallet} from "ethers"; import {readJsonFromFile} from "../utils"; const config = readJsonFromFile("demo-config.json") +const rpcUrl = config.rpcUrl const lotteryAddress = config.lotteryAddress const playerKeys = config.lotteryPlayerKeys async function main() { - const netprovider = new providers.JsonRpcProvider(process.env.RPC_URL) + const netprovider = new providers.JsonRpcProvider(rpcUrl) const Lottery = await ethers.getContractFactory('Lottery') const contractABI = Lottery.interface.format(); diff --git a/scripts/utils.ts b/scripts/utils.ts index ae7edfd..d2d1f47 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -1,7 +1,6 @@ import fs from "fs"; import {promisify} from "util"; import {exec} from "child_process"; -import {ethers} from "hardhat"; export const memberDir = `./data/members/` export const mpksPath = `./data/mpks.json` @@ -10,6 +9,7 @@ export const instancesPath = `./data/dkg/all_instances.json` export const randDir = `./data/random/` export const execPromise = promisify(exec); + export function readJsonFromFile(filePath: string): any { try { // Read file content