Skip to content

Commit

Permalink
send 1 tx in submit v2
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Aug 30, 2024
1 parent c34065f commit eb12d84
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/iden3comm/handlers/contract-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface IContractRequestHandler {
did: DID,
request: Uint8Array,
opts?: ContractInvokeHandlerOptions
): Promise<Map<string, ZeroKnowledgeProofResponse>>;
): Promise<Map<string, ZeroKnowledgeProofResponse> | string>;
}

/** ContractInvokeHandlerOptions represents contract invoke handler options */
Expand Down Expand Up @@ -102,7 +102,7 @@ export class ContractRequestHandler
private async handleContractInvoke(
message: ContractInvokeRequest,
ctx: ContractMessageHandlerOptions
): Promise<Map<string, ZeroKnowledgeProofResponse>> {
): Promise<Map<string, ZeroKnowledgeProofResponse> | string> {
if (message.type !== PROTOCOL_MESSAGE_TYPE.CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE) {
throw new Error('Invalid message type for contract invoke request');
}
Expand Down Expand Up @@ -176,7 +176,7 @@ export class ContractRequestHandler
did: DID,
request: Uint8Array,
opts: ContractInvokeHandlerOptions
): Promise<Map<string, ZeroKnowledgeProofResponse>> {
): Promise<Map<string, ZeroKnowledgeProofResponse> | string> {
const ciRequest = await this.parseContractInvokeRequest(request);

return this.handleContractInvoke(ciRequest, {
Expand All @@ -200,7 +200,7 @@ export class ContractRequestHandler
opts?: {
challenge?: bigint;
}
): Promise<Map<number, string>> {
): Promise<Map<number, string> | string> {
const message = await this.parseContractInvokeRequest(request);

if (message.type !== PROTOCOL_MESSAGE_TYPE.CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE) {
Expand Down
69 changes: 32 additions & 37 deletions src/storage/blockchain/onchain-zkp-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { GlobalStateUpdate, IdentityStateUpdate } from '../entities/state';
import { poseidon } from '@iden3/js-crypto';

// Cache for resolved gists and states
const gistCache = new Map<string, GlobalStateUpdate>();
const stateCache = new Map<string, IdentityStateUpdate>();
// const gistCache = new Map<string, GlobalStateUpdate>();
// const stateCache = new Map<string, IdentityStateUpdate>();

/**
* OnChainZKPVerifier is a class that allows to interact with the OnChainZKPVerifier contract
Expand Down Expand Up @@ -169,7 +169,7 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
public async prepareZKPResponseV2TxData(
txData: ContractInvokeTransactionData,
zkProofResponses: ZeroKnowledgeProofResponse[]
): Promise<Map<number, string>> {
): Promise<string> {
if (txData.method_id.replace('0x', '') !== OnChainZKPVerifier.SupportedMethodIdV2) {
throw new Error(
`submit cross chain doesn't implement requested method id. Only '0x${OnChainZKPVerifier.SupportedMethodIdV2}' is supported.`
Expand All @@ -180,7 +180,9 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
}
const verifierContract = new Contract(txData.contract_address, abi);

const response = new Map<number, string>();
const gistUpdateArr = [];
const stateUpdateArr = [];
const payload = [];
for (const zkProof of zkProofResponses) {
const requestID = zkProof.id;
const inputs = zkProof.pub_signals;
Expand All @@ -207,6 +209,7 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
zkProof.pub_signals
);

// todo: check if we already have gist resolved for this gist
const gistUpdateResolutions = [];
for (const gist of stateInfo.gists) {
gistUpdateResolutions.push(
Expand All @@ -218,6 +221,7 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
);
}

// todo: check if we already have state resolved for this gist
const stateUpdateResolutions = [];
for (const state of stateInfo.states) {
stateUpdateResolutions.push(
Expand All @@ -229,10 +233,10 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
);
}

const gistUpdateArr = (await Promise.all(gistUpdateResolutions)) as GlobalStateUpdate[];
const stateUpdateArr = (await Promise.all(stateUpdateResolutions)) as IdentityStateUpdate[];

const crossChainProofs = this.packCrossChainProofs(gistUpdateArr, stateUpdateArr);
gistUpdateArr.push(...((await Promise.all(gistUpdateResolutions)) as GlobalStateUpdate[]));
stateUpdateArr.push(
...((await Promise.all(stateUpdateResolutions)) as IdentityStateUpdate[])
);

const metadataArr: { key: string; value: Uint8Array }[] = [];
if (zkProof.vp) {
Expand All @@ -254,23 +258,20 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
}

const metadata = this.packMetadatas(metadataArr);
const payload = [
payload.push([
{
requestId: requestID,
zkProof: zkProofEncoded,
data: metadata
}
];

const txData = await verifierContract.submitZKPResponseV2.populateTransaction(
payload,
crossChainProofs
);

response.set(requestID, txData.data);
]);
}

return response;
const crossChainProofs = this.packCrossChainProofs(gistUpdateArr, stateUpdateArr);

return (
await verifierContract.submitZKPResponseV2.populateTransaction(payload, crossChainProofs)
).data;
}

/**
Expand All @@ -280,7 +281,7 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
ethSigner: Signer,
txData: ContractInvokeTransactionData,
zkProofResponses: ZeroKnowledgeProofResponse[]
): Promise<Map<string, ZeroKnowledgeProofResponse>> {
): Promise<string> {
const chainConfig = this._configs.find((i) => i.chainId == txData.chain_id);
if (!chainConfig) {
throw new Error(`config for chain id ${txData.chain_id} was not found`);
Expand All @@ -296,7 +297,7 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
const provider = new JsonRpcProvider(chainConfig.url, chainConfig.chainId);
ethSigner = ethSigner.connect(provider);

const txDataMap = await this.prepareZKPResponseV2TxData(txData, zkProofResponses);
const txRequestData = await this.prepareZKPResponseV2TxData(txData, zkProofResponses);
const feeData = await provider.getFeeData();
const maxFeePerGas = chainConfig.maxFeePerGas
? BigInt(chainConfig.maxFeePerGas)
Expand All @@ -305,25 +306,19 @@ export class OnChainZKPVerifier implements IOnChainZKPVerifier {
? BigInt(chainConfig.maxPriorityFeePerGas)
: feeData.maxPriorityFeePerGas;

const response = new Map<string, ZeroKnowledgeProofResponse>();
for (const zkProof of zkProofResponses) {
const payload = txDataMap.get(zkProof.id);
const request: TransactionRequest = {
to: txData.contract_address,
data: payload,
maxFeePerGas,
maxPriorityFeePerGas
};

const gasLimit = await ethSigner.estimateGas(request);
request.gasLimit = gasLimit;
const request: TransactionRequest = {
to: txData.contract_address,
data: txRequestData,
maxFeePerGas,
maxPriorityFeePerGas
};

const transactionService = new TransactionService(provider);
const { txnHash } = await transactionService.sendTransactionRequest(ethSigner, request);
response.set(txnHash, zkProof);
}
const gasLimit = await ethSigner.estimateGas(request);
request.gasLimit = gasLimit;

return response;
const transactionService = new TransactionService(provider);
const { txnHash } = await transactionService.sendTransactionRequest(ethSigner, request);
return txnHash;
}

private packZkpProof(inputs: string[], a: string[], b: string[][], c: string[]): string {
Expand Down
6 changes: 3 additions & 3 deletions src/storage/interfaces/onchain-zkp-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface IOnChainZKPVerifier {
ethSigner: Signer,
txData: ContractInvokeTransactionData,
zkProofResponses: ZeroKnowledgeProofResponse[]
): Promise<Map<string, ZeroKnowledgeProofResponse>>;
): Promise<string>;

/**
* Returns the Map of request id to transaction data for the ZKP verifier contract submission.
Expand All @@ -47,12 +47,12 @@ export interface IOnChainZKPVerifier {
): Promise<Map<number, string>>;

/**
* Returns the Map of request id to transaction data for the ZKP verifier contract submission V2.
* Returns transaction data for the ZKP verifier contract submission V2. (one tx call for all responses)
* @param txData
* @param zkProofResponses
*/
prepareZKPResponseV2TxData(
txData: ContractInvokeTransactionData,
zkProofResponses: ZeroKnowledgeProofResponse[]
): Promise<Map<number, string>>;
): Promise<string>;
}

0 comments on commit eb12d84

Please sign in to comment.