Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor submitZKPResponse -> check method id #140

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Kolezhniuk marked this conversation as resolved.
Show resolved Hide resolved
"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
24 changes: 16 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,7 @@ import { ZeroKnowledgeProofResponse } from '../../iden3comm';
* @class OnChainZKPVerifier
*/
export class OnChainZKPVerifier implements IOnChainZKPVerifier {
private readonly _supportedMethodId = 'b68967e2';
Kolezhniuk marked this conversation as resolved.
Show resolved Hide resolved
/**
*
* Creates an instance of OnChainZKPVerifier.
Expand All @@ -24,24 +25,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
Loading