-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optimistic proof verification (#23)
- Loading branch information
Showing
26 changed files
with
727 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "zkverifyjs", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Submit proofs to zkVerify and query proof state with ease using our npm package.", | ||
"author": "Horizen Labs <[email protected]>", | ||
"license": "GPL-3.0", | ||
|
@@ -85,6 +85,7 @@ | |
"@types/web3": "^1.2.2", | ||
"@typescript-eslint/eslint-plugin": "^8.2.0", | ||
"@typescript-eslint/parser": "^8.2.0", | ||
"async-mutex": "^0.5.0", | ||
"conventional-changelog-cli": "^5.0.0", | ||
"eslint": "^9.9.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { AccountConnection, WalletConnection } from '../connection/types'; | ||
import { createSubmitProofExtrinsic } from '../extrinsic'; | ||
import { format } from '../format'; | ||
import { ProofData } from '../../types'; | ||
import { SubmittableExtrinsic } from '@polkadot/api/types'; | ||
import { FormattedProofData } from '../format/types'; | ||
import { ProofOptions } from '../../session/types'; | ||
import { VerifyInput } from '../verify/types'; | ||
import { interpretDryRunResponse } from '../../utils/helpers'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
|
||
export const optimisticVerify = async ( | ||
connection: AccountConnection | WalletConnection, | ||
proofOptions: ProofOptions, | ||
input: VerifyInput, | ||
): Promise<{ success: boolean; message: string }> => { | ||
const { api } = connection; | ||
|
||
try { | ||
const transaction = buildTransaction(api, proofOptions, input); | ||
|
||
const submittableExtrinsicHex = transaction.toHex(); | ||
const dryRunResult = await api.rpc.system.dryRun(submittableExtrinsicHex); | ||
const { success, message } = await interpretDryRunResponse( | ||
api, | ||
dryRunResult.toHex(), | ||
); | ||
|
||
return { success, message }; | ||
} catch (error) { | ||
const errorMessage = error instanceof Error ? error.message : String(error); | ||
return { | ||
success: false, | ||
message: `Optimistic verification failed: ${errorMessage}`, | ||
}; | ||
} | ||
}; | ||
|
||
/** | ||
* Builds a transaction from the provided input. | ||
* @param api - The Polkadot.js API instance. | ||
* @param proofOptions - Options for the proof. | ||
* @param input - Input for the verification (proofData or extrinsic). | ||
* @returns A SubmittableExtrinsic ready for dryRun. | ||
* @throws If input is invalid or cannot be formatted. | ||
*/ | ||
const buildTransaction = ( | ||
api: ApiPromise, | ||
proofOptions: ProofOptions, | ||
input: VerifyInput, | ||
): SubmittableExtrinsic<'promise'> => { | ||
if ('proofData' in input && input.proofData) { | ||
const { proof, publicSignals, vk } = input.proofData as ProofData; | ||
const formattedProofData: FormattedProofData = format( | ||
proofOptions, | ||
proof, | ||
publicSignals, | ||
vk, | ||
); | ||
return createSubmitProofExtrinsic( | ||
api, | ||
proofOptions.proofType, | ||
formattedProofData, | ||
); | ||
} | ||
|
||
if ('extrinsic' in input && input.extrinsic) { | ||
return input.extrinsic; | ||
} | ||
|
||
throw new Error( | ||
`Invalid input provided. Expected either 'proofData' or 'extrinsic'. Received: ${JSON.stringify(input)}`, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ProofOptions } from '../../types'; | ||
import { VerifyInput } from '../../../api/verify/types'; | ||
import { CurveType, Library, ProofType } from '../../../config'; | ||
|
||
export type OptimisticProofMethodMap = { | ||
[K in keyof typeof ProofType]: ( | ||
library?: Library, | ||
curve?: CurveType, | ||
) => OptimisticVerificationBuilder; | ||
}; | ||
|
||
export class OptimisticVerificationBuilder { | ||
constructor( | ||
private readonly executeOptimisticVerify: ( | ||
proofOptions: ProofOptions, | ||
input: VerifyInput, | ||
) => Promise<{ success: boolean; message: string }>, | ||
private readonly proofOptions: ProofOptions, | ||
) {} | ||
|
||
/** | ||
* Executes the optimistic verification process. | ||
* @param {VerifyInput} input - Input for the verification, either proofData or an extrinsic. | ||
* @returns {Promise<{ success: boolean; message: string }>} Resolves with an object indicating success or failure and any message. | ||
*/ | ||
async execute( | ||
input: VerifyInput, | ||
): Promise<{ success: boolean; message: string }> { | ||
return this.executeOptimisticVerify(this.proofOptions, input); | ||
} | ||
} |
Oops, something went wrong.