Skip to content
This repository was archived by the owner on Mar 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #82 from hyperoracle/dev
Browse files Browse the repository at this point in the history
Dev - add td
  • Loading branch information
fewwwww authored Oct 20, 2023
2 parents aae0163 + c76ca0b commit b9fe1a6
Show file tree
Hide file tree
Showing 7 changed files with 697 additions and 32 deletions.
12 changes: 12 additions & 0 deletions api/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ export const networks = [
value: 1,
},
];

export const TdABI = [
"function setup(string memory wasmName, uint256 circuitSize) payable",
"function prove(string memory imageId, string memory privateInput, string memory publicInput) payable",
"function deploy(string memory imageId, uint256 chainid) payable",
];

export const TdConfig = {
contract: "0x25AA9Ec3CA462f5AEA7fbd83A207E29Df4691380",
queryrApi: "https://zkwasm.hyperoracle.io/td/",
providerUrl: "https://ethereum-sepolia.publicnode.com",
};
41 changes: 41 additions & 0 deletions api/common/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import fs from "fs";
import axios from "axios";
import axiosRetry from "axios-retry";
import FormData from "form-data";
import { networks } from "./constants.js";
import { logDivider } from "./log_utils.js";
import { loadZKGraphDestinations, loadZKGraphSources } from "./config_utils.js";
import { config } from "../../config.js";
import { TdConfig } from "../common/constants.js";

axiosRetry(axios, {
retries: 3,
retryDelay: (retryCount) => {
return retryCount * 1000;
},
});

export function fromHexString(hexString) {
hexString = hexString.startsWith("0x") ? hexString.slice(2) : hexString;
Expand Down Expand Up @@ -102,3 +114,32 @@ export async function validateProvider(ethersProvider) {
}
}
}

export async function queryTaskId(txhash) {
const response = await axios.get(
`${TdConfig.queryrApi}/task?txhash=${txhash}`,
);
const taskId = response.data.task.id;
return taskId;
}

export async function uoloadWasmToTd(wasmPath) {
let data = new FormData();
data.append("file", fs.createReadStream(wasmPath));

let requestConfig = {
method: "post",
maxBodyLength: Infinity,
url: `${TdConfig.queryrApi}/upload`,
headers: {
...data.getHeaders(),
},
data: data,
};

const response = await axios.request(requestConfig).catch((error) => {
throw error;
});

return response.data.filename;
}
68 changes: 62 additions & 6 deletions api/deploy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import fs from "fs";
import path from "path";
import { ethers } from "ethers";
import inquirer from "inquirer";
import { ZkWasmUtil } from "zkWasm-service-helper";
import { fileURLToPath } from "url";
import { program } from "commander";
import { config } from "../config.js";
import { getTargetNetwork } from "./common/utils.js";
import { getTargetNetwork, queryTaskId } from "./common/utils.js";
import { TdABI, TdConfig } from "./common/constants.js";
import { currentNpmScriptName, logDivider } from "./common/log_utils.js";
import { loadZKGraphDestinations } from "./common/config_utils.js";
import * as zkgapi from "@hyperoracle/zkgraph-api";
import { waitDeploy } from "@hyperoracle/zkgraph-api";

program.version("1.0.0");

Expand Down Expand Up @@ -43,11 +47,63 @@ if (currentNpmScriptName() === "deploy-local") {
const dirname = path.dirname(fileURLToPath(import.meta.url));
const wasm = fs.readFileSync(path.join(dirname, "../", wasmPath));
const wasmUnit8Array = new Uint8Array(wasm);
const deployedVerificationContractAddress = await zkgapi.deploy(
wasmUnit8Array,
targetNetwork.value,
const md5 = ZkWasmUtil.convertToMd5(wasmUnit8Array).toUpperCase();

console.log(`[*] IMAGE MD5: ${md5}`, "\n");

let fee = "0.005";
const feeInWei = ethers.utils.parseEther(fee);

const questions = [
{
type: "confirm",
name: "confirmation",
message: `You are going to publish a Deploy request to the Sepolia testnet, which would require ${fee} SepoliaETH. Proceed?`,
default: true,
},
];

inquirer.prompt(questions).then((answers) => {
if (!answers.confirmation) {
console.log("Task canceled.");
process.exit(0);
}
});

const provider = new ethers.providers.JsonRpcProvider(TdConfig.providerUrl);
const signer = new ethers.Wallet(config.UserPrivateKey, provider);

let dispatcherContract = new ethers.Contract(
TdConfig.contract,
TdABI,
provider,
).connect(signer);

const tx = await dispatcherContract.deploy(md5, targetNetwork.value, {
value: feeInWei,
});

let txhash = tx.hash;
console.log(
`[+] Deploy Request Transaction Sent: ${txhash}, Waiting for Confirmation`,
);

await tx.wait();

console.log("[+] Transaction Confirmed. Creating Deploy Task");

const taskId = await queryTaskId(txhash);
if (!taskId) {
console.log("[+] DEPLOY TASK FAILED. \n");
process.exit(1);
}
console.log(`[+] DEPLOY TASK STARTED. TASK ID: ${taskId}`, "\n");

const deployedVerificationContractAddress = await waitDeploy(
config.ZkwasmProviderUrl,
config.UserPrivateKey,
taskId,
md5,
targetNetwork.value,
true,
);

Expand Down
67 changes: 61 additions & 6 deletions api/prove.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
// TODO: add -o --outfile <file> under inputgen mode
import fs from "fs";
import path from "path";
import { ethers } from "ethers";
import inquirer from "inquirer";
import { ZkWasmUtil } from "zkWasm-service-helper";
import { fileURLToPath } from "url";
import { program } from "commander";
import { TdABI, TdConfig } from "./common/constants.js";
import { currentNpmScriptName, logDivider } from "./common/log_utils.js";
import { config } from "../config.js";
import { writeFileSync } from "fs";
import * as zkgapi from "@hyperoracle/zkgraph-api";
import { loadJsonRpcProviderUrl, validateProvider } from "./common/utils.js";
import {
loadJsonRpcProviderUrl,
validateProvider,
queryTaskId,
} from "./common/utils.js";
import { providers } from "ethers";
import { waitProve } from "@hyperoracle/zkgraph-api";

program.version("1.0.0");

Expand Down Expand Up @@ -132,15 +141,61 @@ switch (options.inputgen || options.test || options.prove) {

// Prove mode
case options.prove === true:
let result = await zkgapi.prove(
wasmUnit8Array,
let fee = "0.005";
const feeInWei = ethers.utils.parseEther(fee);

const questions = [
{
type: "confirm",
name: "confirmation",
message: `You are going to publish a Prove request to the Sepolia testnet, which would require ${fee} SepoliaETH. Proceed?`,
default: true,
},
];

inquirer.prompt(questions).then((answers) => {
if (!answers.confirmation) {
console.log("Task canceled.");
process.exit(0);
}
});
const provider = new ethers.providers.JsonRpcProvider(TdConfig.providerUrl);
const signer = new ethers.Wallet(config.UserPrivateKey, provider);

let dispatcherContract = new ethers.Contract(
TdConfig.contract,
TdABI,
provider,
).connect(signer);

const md5 = ZkWasmUtil.convertToMd5(wasmUnit8Array).toUpperCase();
const tx = await dispatcherContract.prove(
md5,
privateInputStr,
publicInputStr,
config.ZkwasmProviderUrl,
config.UserPrivateKey,
enableLog,
{
value: feeInWei,
},
);

const txhash = tx.hash;
console.log(
`[+] Prove Request Transaction Sent: ${txhash}, Waiting for Confirmation`,
);

await tx.wait();

console.log("[+] Transaction Confirmed. Creating Prove Task");

const taskId = await queryTaskId(txhash);
if (!taskId) {
console.log("[+] DEPLOY TASK FAILED. \n");
process.exit(1);
}
console.log(`[+] PROVE TASK STARTED. TASK ID: ${taskId}`, "\n");

const result = await waitProve(config.ZkwasmProviderUrl, taskId, true);

if (
result.instances === null &&
result.batch_instances === null &&
Expand Down
76 changes: 61 additions & 15 deletions api/setup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import fs from "fs";
import path from "path";
import { ethers } from "ethers";
import inquirer from "inquirer";
import { fileURLToPath } from "url";
import { config } from "../config.js";
import { queryTaskId, uoloadWasmToTd } from "./common/utils.js";
import { TdABI, TdConfig } from "./common/constants.js";
import { currentNpmScriptName, logDivider } from "./common/log_utils.js";
import * as zkgapi from "@hyperoracle/zkgraph-api";
import { waitSetup, zkwasm_imagedetails } from "@hyperoracle/zkgraph-api";
import { program } from "commander";

program.version("1.0.0");
Expand Down Expand Up @@ -36,21 +39,64 @@ if (options.circuitSize !== undefined) {
console.log(">> SET UP", "\n");

const dirname = path.dirname(fileURLToPath(import.meta.url));
const wasm = fs.readFileSync(path.join(dirname, "../", wasmPath));
const wasmUnit8Array = new Uint8Array(wasm);

let { md5, taskId, success } = await zkgapi.setup(
"poc.wasm",
wasmUnit8Array,
cirSz,
config.UserPrivateKey,
config.ZkwasmProviderUrl,
isLocal,
true,
const wasmFullPath = path.join(dirname, "../", wasmPath);

const md5 = await uoloadWasmToTd(wasmFullPath);
console.log(`[*] IMAGE MD5: ${md5}`, "\n");

let deatails = await zkwasm_imagedetails(config.ZkwasmProviderUrl, md5);
if (deatails[0].data.result[0] !== null) {
console.log(`[*] IMAGE ALREADY EXISTS`, "\n");
process.exit(1);
}

let fee = "0.005";
const feeInWei = ethers.utils.parseEther(fee);

const questions = [
{
type: "confirm",
name: "confirmation",
message: `You are going to publish a Setup request to the Sepolia testnet, which would require ${fee} SepoliaETH. Proceed?`,
default: true,
},
];

inquirer.prompt(questions).then((answers) => {
if (!answers.confirmation) {
console.log("Task canceled.");
process.exit(0);
}
});
const provider = new ethers.providers.JsonRpcProvider(TdConfig.providerUrl);
const signer = new ethers.Wallet(config.UserPrivateKey, provider);

let dispatcherContract = new ethers.Contract(
TdConfig.contract,
TdABI,
provider,
).connect(signer);
const tx = await dispatcherContract.setup(md5, cirSz, {
value: feeInWei,
});

const txhash = tx.hash;
console.log(
`[+] Setup Request Transaction Sent: ${txhash}, Waiting for Confirmation`,
);

// console.log(err)
// console.log(result)
await tx.wait();

console.log("[+] Transaction Confirmed. Creating Setup Task");
const taskId = await queryTaskId(txhash);
if (!taskId) {
console.log("[+] DEPLOY TASK FAILED. \n");
process.exit(1);
}
console.log(`[+] SETUP TASK STARTED. TASK ID: ${taskId}`, "\n");

const result = await waitSetup(config.ZkwasmProviderUrl, taskId, true);

logDivider();

process.exit(0);
Loading

0 comments on commit b9fe1a6

Please sign in to comment.