From 1311369964bca3fb35eb82e1e73384c363e40fec Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Tue, 3 Oct 2023 18:55:22 +0300 Subject: [PATCH] rename zkp verifier & inject eth configs[] --- src/iden3comm/handlers/contract-request.ts | 11 ++--- src/storage/blockchain/index.ts | 2 +- ...kp-verifier.ts => onchain-zkp-verifier.ts} | 31 +++++++++---- src/storage/interfaces/index.ts | 2 +- ...kp-verifier.ts => onchain-zkp-verifier.ts} | 13 +++--- tests/handlers/contract-request.test.ts | 43 +++++++++---------- 6 files changed, 54 insertions(+), 48 deletions(-) rename src/storage/blockchain/{zkp-verifier.ts => onchain-zkp-verifier.ts} (66%) rename src/storage/interfaces/{zkp-verifier.ts => onchain-zkp-verifier.ts} (65%) diff --git a/src/iden3comm/handlers/contract-request.ts b/src/iden3comm/handlers/contract-request.ts index d4c6b9ce..3367ef4b 100644 --- a/src/iden3comm/handlers/contract-request.ts +++ b/src/iden3comm/handlers/contract-request.ts @@ -10,7 +10,7 @@ import { ContractInvokeRequest } from '../types/protocol/contract-request'; import { DID } from '@iden3/js-iden3-core'; -import { EthConnectionConfig, IZKPVerifier } from '../../storage'; +import { IOnChainZKPVerifier } from '../../storage'; /** * Interface that allows the processing of the contract request @@ -60,15 +60,14 @@ export class ContractRequestHandler implements IContractRequestHandler { * Creates an instance of ContractRequestHandler. * @param {IPackageManager} _packerMgr - package manager to unpack message envelope * @param {IProofService} _proofService - proof service to verify zk proofs - * @param {IZKPVerifier} _zkpVerifier - zkp verifier to submit response + * @param {IOnChainZKPVerifier} _zkpVerifier - zkp verifier to submit response * */ constructor( private readonly _packerMgr: IPackageManager, private readonly _proofService: IProofService, - private readonly _zkpVerifier: IZKPVerifier, - private readonly _ethConfig: EthConnectionConfig + private readonly _zkpVerifier: IOnChainZKPVerifier ) {} /** @@ -139,12 +138,10 @@ export class ContractRequestHandler implements IContractRequestHandler { const txData = ciRequest.body.transaction_data; - const requestConfig = {...this._ethConfig, chainId: txData.chain_id, }; - return this._zkpVerifier.submitZKPResponse( txData.contract_address, opts.ethSigner, - requestConfig, + txData.chain_id, zkRequests ); } diff --git a/src/storage/blockchain/index.ts b/src/storage/blockchain/index.ts index 50979afc..c5dc801a 100644 --- a/src/storage/blockchain/index.ts +++ b/src/storage/blockchain/index.ts @@ -1,3 +1,3 @@ export * from './state'; export * from './chainid'; -export * from './zkp-verifier'; +export * from './onchain-zkp-verifier'; diff --git a/src/storage/blockchain/zkp-verifier.ts b/src/storage/blockchain/onchain-zkp-verifier.ts similarity index 66% rename from src/storage/blockchain/zkp-verifier.ts rename to src/storage/blockchain/onchain-zkp-verifier.ts index 629659bc..c86db7bb 100644 --- a/src/storage/blockchain/zkp-verifier.ts +++ b/src/storage/blockchain/onchain-zkp-verifier.ts @@ -1,33 +1,46 @@ import abi from './zkp-verifier-abi.json'; import { ethers, Signer } from 'ethers'; import { EthConnectionConfig } from './state'; -import { IZKPVerifier } from '../interfaces/zkp-verifier'; +import { IOnChainZKPVerifier } from '../interfaces/onchain-zkp-verifier'; import { ZeroKnowledgeProofResponse } from '../../iden3comm'; /** - * ZKPVerifier is a class that allows to interact with the ZKPVerifier contract + * OnChainZKPVerifier is a class that allows to interact with the OnChainZKPVerifier contract * and submitZKPResponse. * * @beta - * @class ZKPVerifier + * @class OnChainZKPVerifier */ -export class ZKPVerifier implements IZKPVerifier { +export class OnChainZKPVerifier implements IOnChainZKPVerifier { /** - * Submit ZKP Responses to ZKPVerifier contract. + * + * Creates an instance of OnChainZKPVerifier. * @public - * @param {string} address - ZKPVerifier contract address + * @param {EthConnectionConfig[]} - array of ETH configs + */ + + constructor(private readonly _configs: EthConnectionConfig[]) {} + + /** + * Submit ZKP Responses to OnChainZKPVerifier contract. + * @public + * @param {string} address - OnChainZKPVerifier contract address * @param {Signer} ethSigner - tx signer - * @param {EthConnectionConfig} ethConfig - ETH config + * @param {number} chainId - chain Id * @param {ZeroKnowledgeProofResponse[]} zkProofResponses - zkProofResponses * @returns {Promise>} - map of transaction hash - ZeroKnowledgeProofResponse */ public async submitZKPResponse( address: string, ethSigner: Signer, - ethConfig: EthConnectionConfig, + chainId: number, zkProofResponses: ZeroKnowledgeProofResponse[] ): Promise> { - const provider = new ethers.providers.JsonRpcProvider(ethConfig); + const chainConfig = this._configs.find((i) => i.chainId == chainId); + if (!chainConfig) { + throw new Error(`config for chain id ${chainId} was not found`); + } + const provider = new ethers.providers.JsonRpcProvider(chainConfig); const verifierContract: ethers.Contract = new ethers.Contract(address, abi, provider); ethSigner = ethSigner.connect(provider); const contract = verifierContract.connect(ethSigner); diff --git a/src/storage/interfaces/index.ts b/src/storage/interfaces/index.ts index aab27e90..e7779ac4 100644 --- a/src/storage/interfaces/index.ts +++ b/src/storage/interfaces/index.ts @@ -5,4 +5,4 @@ export * from './state'; export * from './data-storage'; export * from './data-source'; export * from './circuits'; -export * from './zkp-verifier'; +export * from './onchain-zkp-verifier'; diff --git a/src/storage/interfaces/zkp-verifier.ts b/src/storage/interfaces/onchain-zkp-verifier.ts similarity index 65% rename from src/storage/interfaces/zkp-verifier.ts rename to src/storage/interfaces/onchain-zkp-verifier.ts index 2abc675f..d2e99557 100644 --- a/src/storage/interfaces/zkp-verifier.ts +++ b/src/storage/interfaces/onchain-zkp-verifier.ts @@ -1,27 +1,26 @@ import { Signer } from 'ethers'; import { ZeroKnowledgeProofResponse } from '../../iden3comm'; -import { EthConnectionConfig } from '../blockchain'; /** * Interface that defines methods for ZKP verifier * * @beta - * @interface IZKPVerifier + * @interface IOnChainZKPVerifier */ -export interface IZKPVerifier { +export interface IOnChainZKPVerifier { /** - * Submit ZKP Responses to ZKPVerifier contract. + * Submit ZKP Responses to OnChainZKPVerifier contract. * @public - * @param {string} address - ZKPVerifier contract address + * @param {string} address - OnChainZKPVerifier contract address * @param {Signer} ethSigner - tx signer - * @param {EthConnectionConfig} ethConfig - ETH config + * @param {number} chainId - chain Id * @param {ZeroKnowledgeProofResponse[]} zkProofResponses - zkProofResponses * @returns {Promise>} - map of transaction hash - ZeroKnowledgeProofResponse */ submitZKPResponse( address: string, ethSigner: Signer, - ethConfig: EthConnectionConfig, + chainId: number, zkProofResponses: ZeroKnowledgeProofResponse[] ): Promise>; } diff --git a/tests/handlers/contract-request.test.ts b/tests/handlers/contract-request.test.ts index 7706f65e..28413185 100644 --- a/tests/handlers/contract-request.test.ts +++ b/tests/handlers/contract-request.test.ts @@ -8,14 +8,13 @@ import { IdentityWallet, byteEncoder, EthStateStorage, - EthConnectionConfig, - ZKPVerifier, + OnChainZKPVerifier, defaultEthConnectionConfig, hexToBytes } from '../../src'; import { BjjProvider, KMS, KmsKeyType } from '../../src/kms'; import { InMemoryPrivateKeyStore } from '../../src/kms/store'; -import { IDataStorage, IStateStorage, IZKPVerifier } from '../../src/storage/interfaces'; +import { IDataStorage, IStateStorage, IOnChainZKPVerifier } from '../../src/storage/interfaces'; import { InMemoryDataSource, InMemoryMerkleTreeStorage } from '../../src/storage/memory'; import { CredentialRequest, CredentialWallet } from '../../src/credentials'; import { ProofService } from '../../src/proof'; @@ -99,11 +98,11 @@ describe('contract-request', () => { } }; - const mockZKPVerifier: IZKPVerifier = { + const mockZKPVerifier: IOnChainZKPVerifier = { submitZKPResponse: async ( address: string, signer: Signer, - ethConfig: EthConnectionConfig, + chainId: number, zkProofResponses: ZeroKnowledgeProofResponse[] ) => { const response = new Map(); @@ -190,7 +189,7 @@ describe('contract-request', () => { proofService.generateAuthV2Inputs.bind(proofService), proofService.verifyState.bind(proofService) ); - contractRequest = new ContractRequestHandler(packageMgr, proofService, mockZKPVerifier, defaultEthConnectionConfig); + contractRequest = new ContractRequestHandler(packageMgr, proofService, mockZKPVerifier); }); it('contract request flow', async () => { @@ -295,9 +294,8 @@ describe('contract-request', () => { expect(ciResponse.has('txhash1')).to.be.true; }); - // SKIPPED : integration test - it('contract request flow - integration test', async () => { - + // SKIPPED : integration test + it.skip('contract request flow - integration test', async () => { const stateEthConfig = defaultEthConnectionConfig; stateEthConfig.url = rpcUrl; stateEthConfig.contractAddress = '0x134b1be34911e39a8397ec6289782989729807a4'; @@ -335,8 +333,8 @@ describe('contract-request', () => { proofService.generateAuthV2Inputs.bind(proofService), proofService.verifyState.bind(proofService) ); - contractRequest = new ContractRequestHandler(packageMgr, proofService, mockZKPVerifier, defaultEthConnectionConfig); - + contractRequest = new ContractRequestHandler(packageMgr, proofService, mockZKPVerifier); + const { did: userDID, credential: cred } = await idWallet.createIdentity({ method: DidMethod.Iden3, blockchain: Blockchain.Polygon, @@ -398,18 +396,19 @@ describe('contract-request', () => { } }; - let contractAddress = '0xE826f870852D7eeeB79B2C030298f9B5DAA8C8a3'; - let conf = defaultEthConnectionConfig; + const contractAddress = '0xE826f870852D7eeeB79B2C030298f9B5DAA8C8a3'; + const conf = defaultEthConnectionConfig; conf.contractAddress = contractAddress; conf.url = rpcUrl; + conf.chainId = 80001; - const zkpVerifier = new ZKPVerifier(); - contractRequest = new ContractRequestHandler(packageMgr, proofService, zkpVerifier, conf); + const zkpVerifier = new OnChainZKPVerifier([conf]); + contractRequest = new ContractRequestHandler(packageMgr, proofService, zkpVerifier); const transactionData: ContractInvokeTransactionData = { contract_address: contractAddress, method_id: 'b68967e2', - chain_id: 80001 + chain_id: conf.chainId }; const ciRequestBody: ContractInvokeRequestBody = { @@ -427,16 +426,13 @@ describe('contract-request', () => { body: ciRequestBody }; - const ethSigner = new ethers.Wallet( - walletKey - ); + const ethSigner = new ethers.Wallet(walletKey); - const challenge = BytesHelper.bytesToInt(hexToBytes(ethSigner.address)) + const challenge = BytesHelper.bytesToInt(hexToBytes(ethSigner.address)); const options: ContractInvokeHandlerOptions = { ethSigner, challenge - }; const msgBytes = byteEncoder.encode(JSON.stringify(ciRequest)); const ciResponse = await contractRequest.handleContractInvokeRequest( @@ -446,7 +442,8 @@ describe('contract-request', () => { ); expect(ciResponse).not.be.undefined; - expect((ciResponse.values().next().value as ZeroKnowledgeProofResponse).id).to.be.equal(proofReq.id); - + expect((ciResponse.values().next().value as ZeroKnowledgeProofResponse).id).to.be.equal( + proofReq.id + ); }); });