Skip to content

Commit

Permalink
move demo scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kitounliu committed Jul 25, 2024
1 parent b03be15 commit 4fb09c8
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 58 deletions.
4 changes: 3 additions & 1 deletion demo-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"zkdvrfAddress": "0x1C2FfFeEee7d396597D60adAdAC63c2c48B829AF",
"rpcUrl": "http://localhost:8545",
"zkdvrfAddress": "0x9B27Cd80c4B3823a94334d9285001F0cE959a37b",
"adminKey": "0x58936e48436d9764c0c09775444fe6700d0a8aa84d4d6c09e110f70fc87c7289",
"memberAddresses": [
"0x85A6bCc74CB8570BEfF8526aec7d0Dfb6F128A60",
"0x7D9e5627B650650Fa3E848364Cb5B0b696B885f2",
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 16 additions & 15 deletions scripts/admin.ts → scripts/demo/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand All @@ -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])
}
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions scripts/nidkg.ts → scripts/demo/nidkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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`])
}

Expand Down
21 changes: 11 additions & 10 deletions scripts/random.ts → scripts/demo/random.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -60,8 +61,8 @@ async function main() {

main().then(() => {
process.exit(0);
})
.catch((error) => {
console.error(error);
process.exitCode = 1;
});
})
.catch((error) => {
console.error(error);
process.exitCode = 1;
});
15 changes: 8 additions & 7 deletions scripts/register.ts → scripts/demo/register.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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}`

Expand Down Expand Up @@ -65,4 +66,4 @@ async function main() {
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
});
7 changes: 5 additions & 2 deletions scripts/lottery/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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++) {
Expand Down
23 changes: 12 additions & 11 deletions scripts/lottery/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
3 changes: 2 additions & 1 deletion scripts/lottery/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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
Expand Down

0 comments on commit 4fb09c8

Please sign in to comment.