Skip to content

Commit

Permalink
feat(verify)!: use local proof file generate proof params
Browse files Browse the repository at this point in the history
  • Loading branch information
murongg committed Feb 23, 2024
1 parent 7538e5e commit 448a8f3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/cle-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"dependencies": {
"@antfu/utils": "^0.7.6",
"@ora-io/cle-api": "^1.0.3-alpha.11",
"@ora-io/zkwasm-service-helper": "^1.0.3",
"api": "^6.1.1",
"assemblyscript": "^0.27.12",
"await-to-js": "^3.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/cle-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Usage cases:
yamlPath: config.YamlPath,
zkWasmProviderUrl: config.ZkwasmProviderUrl,
jsonRpcProviderUrl: config.JsonRpcProviderUrl,
outputProofFilePath: config.OutputProofFilePath,
})
})

Expand Down
56 changes: 46 additions & 10 deletions packages/cle-cli/src/commands/verify.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
// import fs from 'node:fs'
import { Error, getVerifyProofParamsByTaskID, verify as verifyApi } from '@ora-io/cle-api'
import fs from 'node:fs'
import type { ProofParams } from '@ora-io/cle-api'
import { Error, constants, verify as verifyApi } from '@ora-io/cle-api'
import { ethers } from 'ethers'
import { ZkWasmUtil } from '@ora-io/zkwasm-service-helper'
import { logger } from '../logger'
import { loadJsonRpcProviderUrl, loadYamlFromPath, logDivider } from '../utils'
import type { UserConfig } from '../config'
// import { AggregatorVerifierAddress } from '../constants'
import { parseTemplateTag } from '../tag'
import { TAGS } from '../constants'

export interface VerifyOptions {
taskId: string
yamlPath: string
zkWasmProviderUrl: string
jsonRpcProviderUrl: UserConfig['JsonRpcProviderUrl']
outputProofFilePath: string
}
export async function verify(options: VerifyOptions) {
logger.info('>> VERIFY PROOF ONCHAIN')
const { yamlPath, taskId, zkWasmProviderUrl, jsonRpcProviderUrl } = options
const { yamlPath, taskId, jsonRpcProviderUrl, outputProofFilePath } = options

const cleYaml = loadYamlFromPath(yamlPath)
if (!cleYaml) {
Expand All @@ -26,15 +31,17 @@ export async function verify(options: VerifyOptions) {
// Get deployed verification contract address.
// TODO: I reused this func to save code, but the naming is a bit misleading, fix it later.
// const verifierAddress = loadJsonRpcProviderUrl(cleYaml, AggregatorVerifierAddress, false)
const proofParams = await getVerifyProofParamsByTaskID(taskId, zkWasmProviderUrl)
const proofParams = getVerifyProofParamsByProofFile(taskId, outputProofFilePath)
const network = cleYaml.decidePublishNetwork()
if (!network) {
logger.error('[-] ERROR: Failed to get network')
return
}
const verifierAddress = (constants.AggregatorVerifierAddress as any)[network]

const verifyResult = await verifyApi(
{
cleYaml,
wasmUint8Array: null,
},
proofParams,
jsonRpcUrl || '',
{ verifierAddress, provider: new ethers.providers.JsonRpcProvider(jsonRpcUrl) },
).catch((error: Error) => {
if (error instanceof Error.ProveTaskNotReady)
logger.error(`>> PROOF IS NOT READY. ${error.message}`)
Expand All @@ -50,3 +57,32 @@ export async function verify(options: VerifyOptions) {

return verifyResult
}

function getVerifyProofParamsByProofFile(taskId: string, outputProofFilePath: string): ProofParams {
const outputProofFile = parseTemplateTag(outputProofFilePath, {
...TAGS,
taskId: taskId || '',
})

const content = fs.readFileSync(outputProofFile, 'utf-8')
const regex = /Instances:\n(.*?)\n\nBatched Instances:\n(.*?)\n\nProof transcripts:\n(.*?)\n\nAux data:\n(.*?)\n\n/s
const matches = content.match(regex)

if (matches) {
const instancesValue = matches[1].trim().split('\n')
const batchedInstancesValue = matches[2].trim().split('\n')
const proofTranscriptsValue = matches[3].trim().split('\n')
const auxDataValue = matches[4].trim().split('\n')

return {
instances: ZkWasmUtil.hexStringsToBytes(instancesValue, 32),
batch_instances: ZkWasmUtil.hexStringsToBytes(batchedInstancesValue, 32),
aggregate_proof: ZkWasmUtil.hexStringsToBytes(proofTranscriptsValue, 32),
aux: ZkWasmUtil.hexStringsToBytes(auxDataValue, 32),
}
}
else {
logger.error('[-] ERROR: Failed to get proof file')
process.exit(1)
}
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 448a8f3

Please sign in to comment.