Skip to content

Commit

Permalink
rename zkp verifier & inject eth configs[]
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Oct 3, 2023
1 parent 16138d3 commit 1311369
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
11 changes: 4 additions & 7 deletions src/iden3comm/handlers/contract-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
) {}

/**
Expand Down Expand Up @@ -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
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/blockchain/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './state';
export * from './chainid';
export * from './zkp-verifier';
export * from './onchain-zkp-verifier';
Original file line number Diff line number Diff line change
@@ -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<string, ZeroKnowledgeProofResponse>>} - map of transaction hash - ZeroKnowledgeProofResponse
*/
public async submitZKPResponse(
address: string,
ethSigner: Signer,
ethConfig: EthConnectionConfig,
chainId: number,
zkProofResponses: ZeroKnowledgeProofResponse[]
): Promise<Map<string, ZeroKnowledgeProofResponse>> {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/storage/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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<string, ZeroKnowledgeProofResponse>>} - map of transaction hash - ZeroKnowledgeProofResponse
*/
submitZKPResponse(
address: string,
ethSigner: Signer,
ethConfig: EthConnectionConfig,
chainId: number,
zkProofResponses: ZeroKnowledgeProofResponse[]
): Promise<Map<string, ZeroKnowledgeProofResponse>>;
}
43 changes: 20 additions & 23 deletions tests/handlers/contract-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string, ZeroKnowledgeProofResponse>();
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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(
Expand All @@ -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
);
});
});

0 comments on commit 1311369

Please sign in to comment.