From 7f9bd550a4aa9c097b4fff3da9a72daece93bdb3 Mon Sep 17 00:00:00 2001 From: volodymyr-basiuk <31999965+volodymyr-basiuk@users.noreply.github.com> Date: Thu, 5 Oct 2023 13:53:34 +0300 Subject: [PATCH] refactor submitZKPResponse -> check method id (#140) * refactor submitZKPResponse -> check method id --- package-lock.json | 4 +-- package.json | 2 +- src/iden3comm/handlers/contract-request.ts | 5 +--- .../blockchain/onchain-zkp-verifier.ts | 29 ++++++++++++++----- .../interfaces/onchain-zkp-verifier.ts | 8 ++--- tests/handlers/contract-request.test.ts | 8 ++--- 6 files changed, 30 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index a26304f7..5083aef0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@0xpolygonid/js-sdk", - "version": "1.2.0", + "version": "1.2.1", "license": "AGPL-3.0", "dependencies": { "@iden3/js-crypto": "1.0.1", diff --git a/package.json b/package.json index 95b581bd..7772e3ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xpolygonid/js-sdk", - "version": "1.2.0", + "version": "1.2.1", "description": "SDK to work with Polygon ID", "main": "dist/cjs/index.js", "module": "dist/esm_esbuild/index.js", diff --git a/src/iden3comm/handlers/contract-request.ts b/src/iden3comm/handlers/contract-request.ts index 6e272163..d1592bb2 100644 --- a/src/iden3comm/handlers/contract-request.ts +++ b/src/iden3comm/handlers/contract-request.ts @@ -141,12 +141,9 @@ export class ContractRequestHandler implements IContractRequestHandler { zkRequests.push(zkpRes); } - const txData = ciRequest.body.transaction_data; - return this._zkpVerifier.submitZKPResponse( - txData.contract_address, opts.ethSigner, - txData.chain_id, + ciRequest.body.transaction_data, zkRequests ); } diff --git a/src/storage/blockchain/onchain-zkp-verifier.ts b/src/storage/blockchain/onchain-zkp-verifier.ts index bbc96cb4..77f1da9f 100644 --- a/src/storage/blockchain/onchain-zkp-verifier.ts +++ b/src/storage/blockchain/onchain-zkp-verifier.ts @@ -2,7 +2,7 @@ import abi from './zkp-verifier-abi.json'; import { ethers, Signer } from 'ethers'; import { EthConnectionConfig } from './state'; import { IOnChainZKPVerifier } from '../interfaces/onchain-zkp-verifier'; -import { ZeroKnowledgeProofResponse } from '../../iden3comm'; +import { ContractInvokeTransactionData, ZeroKnowledgeProofResponse } from '../../iden3comm'; /** * OnChainZKPVerifier is a class that allows to interact with the OnChainZKPVerifier contract @@ -12,6 +12,12 @@ import { ZeroKnowledgeProofResponse } from '../../iden3comm'; * @class OnChainZKPVerifier */ export class OnChainZKPVerifier implements IOnChainZKPVerifier { + /** + * solidity identifier for function signature: + * function submitZKPResponse(uint64 requestId, uint256[] calldata inputs, + * uint256[2] calldata a, uint256[2][2] calldata b, uint256[2] calldata c) public + */ + private readonly _supportedMethodId = 'b68967e2'; /** * * Creates an instance of OnChainZKPVerifier. @@ -24,24 +30,31 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier { /** * Submit ZKP Responses to OnChainZKPVerifier contract. * @beta - * @param {string} address - OnChainZKPVerifier contract address * @param {Signer} ethSigner - tx signer - * @param {number} chainId - chain Id + * @param {txData} ContractInvokeTransactionData - transaction data * @param {ZeroKnowledgeProofResponse[]} zkProofResponses - zkProofResponses * @returns {Promise>} - map of transaction hash - ZeroKnowledgeProofResponse */ public async submitZKPResponse( - address: string, ethSigner: Signer, - chainId: number, + txData: ContractInvokeTransactionData, zkProofResponses: ZeroKnowledgeProofResponse[] ): Promise> { - const chainConfig = this._configs.find((i) => i.chainId == chainId); + const chainConfig = this._configs.find((i) => i.chainId == txData.chain_id); if (!chainConfig) { - throw new Error(`config for chain id ${chainId} was not found`); + throw new Error(`config for chain id ${txData.chain_id} was not found`); + } + if (txData.method_id.replace('0x', '') !== this._supportedMethodId) { + throw new Error( + `submit doesn't implement requested method id. Only '0x${this._supportedMethodId}' is supported.` + ); } const provider = new ethers.providers.JsonRpcProvider(chainConfig); - const verifierContract: ethers.Contract = new ethers.Contract(address, abi, provider); + const verifierContract: ethers.Contract = new ethers.Contract( + txData.contract_address, + abi, + provider + ); ethSigner = ethSigner.connect(provider); const contract = verifierContract.connect(ethSigner); diff --git a/src/storage/interfaces/onchain-zkp-verifier.ts b/src/storage/interfaces/onchain-zkp-verifier.ts index bf9d5df0..dba233c2 100644 --- a/src/storage/interfaces/onchain-zkp-verifier.ts +++ b/src/storage/interfaces/onchain-zkp-verifier.ts @@ -1,5 +1,5 @@ import { Signer } from 'ethers'; -import { ZeroKnowledgeProofResponse } from '../../iden3comm'; +import { ContractInvokeTransactionData, ZeroKnowledgeProofResponse } from '../../iden3comm'; /** * Interface that defines methods for ZKP verifier @@ -11,16 +11,14 @@ export interface IOnChainZKPVerifier { /** * Submit ZKP Responses to OnChainZKPVerifier contract. * @beta - * @param {string} address - OnChainZKPVerifier contract address * @param {Signer} ethSigner - tx signer - * @param {number} chainId - chain Id + * @param {txData} ContractInvokeTransactionData - transaction data * @param {ZeroKnowledgeProofResponse[]} zkProofResponses - zkProofResponses * @returns {Promise>} - map of transaction hash - ZeroKnowledgeProofResponse */ submitZKPResponse( - address: string, ethSigner: Signer, - chainId: number, + txData: ContractInvokeTransactionData, zkProofResponses: ZeroKnowledgeProofResponse[] ): Promise>; } diff --git a/tests/handlers/contract-request.test.ts b/tests/handlers/contract-request.test.ts index 28413185..055c525c 100644 --- a/tests/handlers/contract-request.test.ts +++ b/tests/handlers/contract-request.test.ts @@ -100,9 +100,8 @@ describe('contract-request', () => { const mockZKPVerifier: IOnChainZKPVerifier = { submitZKPResponse: async ( - address: string, signer: Signer, - chainId: number, + txData: ContractInvokeTransactionData, zkProofResponses: ZeroKnowledgeProofResponse[] ) => { const response = new Map(); @@ -275,10 +274,7 @@ describe('contract-request', () => { body: ciRequestBody }; - const ethSigner = new ethers.Wallet( - walletKey, - (dataStorage.states as EthStateStorage).provider - ); + const ethSigner = new ethers.Wallet(walletKey); const options: ContractInvokeHandlerOptions = { ethSigner,