Skip to content

Commit

Permalink
refactor submitZKPResponse -> check method id (#140)
Browse files Browse the repository at this point in the history
* refactor submitZKPResponse -> check method id
  • Loading branch information
volodymyr-basiuk authored Oct 5, 2023
1 parent c6030a4 commit 7f9bd55
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 1 addition & 4 deletions src/iden3comm/handlers/contract-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
29 changes: 21 additions & 8 deletions src/storage/blockchain/onchain-zkp-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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<string, ZeroKnowledgeProofResponse>>} - map of transaction hash - ZeroKnowledgeProofResponse
*/
public async submitZKPResponse(
address: string,
ethSigner: Signer,
chainId: number,
txData: ContractInvokeTransactionData,
zkProofResponses: ZeroKnowledgeProofResponse[]
): Promise<Map<string, ZeroKnowledgeProofResponse>> {
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);

Expand Down
8 changes: 3 additions & 5 deletions src/storage/interfaces/onchain-zkp-verifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Signer } from 'ethers';
import { ZeroKnowledgeProofResponse } from '../../iden3comm';
import { ContractInvokeTransactionData, ZeroKnowledgeProofResponse } from '../../iden3comm';

/**
* Interface that defines methods for ZKP verifier
Expand All @@ -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<string, ZeroKnowledgeProofResponse>>} - map of transaction hash - ZeroKnowledgeProofResponse
*/
submitZKPResponse(
address: string,
ethSigner: Signer,
chainId: number,
txData: ContractInvokeTransactionData,
zkProofResponses: ZeroKnowledgeProofResponse[]
): Promise<Map<string, ZeroKnowledgeProofResponse>>;
}
8 changes: 2 additions & 6 deletions tests/handlers/contract-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ZeroKnowledgeProofResponse>();
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7f9bd55

Please sign in to comment.