@@ -2,9 +2,9 @@ import { CircuitId } from '../../circuits/models';
2
2
import { IProofService } from '../../proof/proof-service' ;
3
3
import { PROTOCOL_MESSAGE_TYPE } from '../constants' ;
4
4
import { BasicMessage , IPackageManager , ZeroKnowledgeProofResponse } from '../types' ;
5
- import { ContractInvokeRequest } from '../types/protocol/contract-request' ;
5
+ import { ContractInvokeRequest , ContractInvokeResponse } from '../types/protocol/contract-request' ;
6
6
import { DID , ChainIds } from '@iden3/js-iden3-core' ;
7
- import { IOnChainZKPVerifier } from '../../storage' ;
7
+ import { FunctionSignatures , IOnChainZKPVerifier } from '../../storage' ;
8
8
import { Signer } from 'ethers' ;
9
9
import { processZeroKnowledgeProofRequests } from './common' ;
10
10
import { AbstractMessageHandler , IProtocolMessageHandler } from './message-handler' ;
@@ -17,15 +17,15 @@ import { AbstractMessageHandler, IProtocolMessageHandler } from './message-handl
17
17
*/
18
18
export interface IContractRequestHandler {
19
19
/**
20
- * unpacks contract invoker request
20
+ * unpacks contract invoke request
21
21
* @beta
22
22
* @param {Uint8Array } request - raw byte message
23
23
* @returns `Promise<ContractInvokeRequest>`
24
24
*/
25
25
parseContractInvokeRequest ( request : Uint8Array ) : Promise < ContractInvokeRequest > ;
26
26
27
27
/**
28
- * handle contract invoker request
28
+ * handle contract invoke request
29
29
* @beta
30
30
* @param {did } did - sender DID
31
31
* @param {Uint8Array } request - raw byte message
@@ -91,9 +91,11 @@ export class ContractRequestHandler
91
91
ctx : ContractMessageHandlerOptions
92
92
) : Promise < BasicMessage | null > {
93
93
switch ( message . type ) {
94
- case PROTOCOL_MESSAGE_TYPE . CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE :
95
- await this . handleContractInvoke ( message as ContractInvokeRequest , ctx ) ;
96
- return null ;
94
+ case PROTOCOL_MESSAGE_TYPE . CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE : {
95
+ const ciMessage = message as ContractInvokeRequest ;
96
+ const txHashResponsesMap = await this . handleContractInvoke ( ciMessage , ctx ) ;
97
+ return this . createContractInvokeResponse ( ciMessage , txHashResponsesMap ) ;
98
+ }
97
99
default :
98
100
return super . handle ( message , ctx ) ;
99
101
}
@@ -102,7 +104,7 @@ export class ContractRequestHandler
102
104
private async handleContractInvoke (
103
105
message : ContractInvokeRequest ,
104
106
ctx : ContractMessageHandlerOptions
105
- ) : Promise < Map < string , ZeroKnowledgeProofResponse > > {
107
+ ) : Promise < Map < string , ZeroKnowledgeProofResponse [ ] > > {
106
108
if ( message . type !== PROTOCOL_MESSAGE_TYPE . CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE ) {
107
109
throw new Error ( 'Invalid message type for contract invoke request' ) ;
108
110
}
@@ -127,11 +129,31 @@ export class ContractRequestHandler
127
129
{ ethSigner, challenge, supportedCircuits : this . _supportedCircuits }
128
130
) ;
129
131
130
- return this . _zkpVerifier . submitZKPResponse (
131
- ethSigner ,
132
- message . body . transaction_data ,
133
- zkpResponses
134
- ) ;
132
+ const methodId = message . body . transaction_data . method_id . replace ( '0x' , '' ) ;
133
+ switch ( methodId ) {
134
+ case FunctionSignatures . SumbitZKPResponseV2 :
135
+ return this . _zkpVerifier . submitZKPResponseV2 (
136
+ ethSigner ,
137
+ message . body . transaction_data ,
138
+ zkpResponses
139
+ ) ;
140
+ case FunctionSignatures . SumbitZKPResponseV1 : {
141
+ const txHashZkpResponseMap = await this . _zkpVerifier . submitZKPResponse (
142
+ ethSigner ,
143
+ message . body . transaction_data ,
144
+ zkpResponses
145
+ ) ;
146
+ const response = new Map < string , ZeroKnowledgeProofResponse [ ] > ( ) ;
147
+ for ( const [ txHash , zkpResponse ] of txHashZkpResponseMap ) {
148
+ response . set ( txHash , [ zkpResponse ] ) ;
149
+ }
150
+ return response ;
151
+ }
152
+ default :
153
+ throw new Error (
154
+ `Not supported method id. Only '${ FunctionSignatures . SumbitZKPResponseV1 } and ${ FunctionSignatures . SumbitZKPResponseV2 } are supported.'`
155
+ ) ;
156
+ }
135
157
}
136
158
137
159
/**
@@ -151,8 +173,45 @@ export class ContractRequestHandler
151
173
}
152
174
153
175
/**
154
- * handle contract invoker request
176
+ * creates contract invoke response
177
+ * @private
178
+ * @beta
179
+ * @param {ContractInvokeRequest } request - ContractInvokeRequest
180
+ * @param { Map<string, ZeroKnowledgeProofResponse[]> } responses - map tx hash to array of ZeroKnowledgeProofResponses
181
+ * @returns `Promise<ContractInvokeResponse>`
182
+ */
183
+ private async createContractInvokeResponse (
184
+ request : ContractInvokeRequest ,
185
+ txHashToZkpResponseMap : Map < string , ZeroKnowledgeProofResponse [ ] >
186
+ ) : Promise < ContractInvokeResponse > {
187
+ const contractInvokeResponse : ContractInvokeResponse = {
188
+ id : request . id ,
189
+ thid : request . thid ,
190
+ type : PROTOCOL_MESSAGE_TYPE . CONTRACT_INVOKE_RESPONSE_MESSAGE_TYPE ,
191
+ from : request . to ,
192
+ to : request . from ,
193
+ body : {
194
+ transaction_data : request . body . transaction_data ,
195
+ reason : request . body . reason ,
196
+ scope : [ ]
197
+ }
198
+ } ;
199
+ for ( const [ txHash , zkpResponses ] of txHashToZkpResponseMap ) {
200
+ for ( const zkpResponse of zkpResponses ) {
201
+ contractInvokeResponse . body . scope . push ( {
202
+ txHash,
203
+ ...zkpResponse
204
+ } ) ;
205
+ }
206
+ }
207
+ return contractInvokeResponse ;
208
+ }
209
+
210
+ /**
211
+ * handle contract invoke request
212
+ * supports only 0xb68967e2 method id
155
213
* @beta
214
+ * @deprecated
156
215
* @param {did } did - sender DID
157
216
* @param {ContractInvokeRequest } request - contract invoke request
158
217
* @param {ContractInvokeHandlerOptions } opts - handler options
@@ -165,10 +224,37 @@ export class ContractRequestHandler
165
224
) : Promise < Map < string , ZeroKnowledgeProofResponse > > {
166
225
const ciRequest = await this . parseContractInvokeRequest ( request ) ;
167
226
168
- return this . handleContractInvoke ( ciRequest , {
169
- senderDid : did ,
170
- ethSigner : opts . ethSigner ,
171
- challenge : opts . challenge
172
- } ) ;
227
+ if ( ciRequest . body . transaction_data . method_id !== FunctionSignatures . SumbitZKPResponseV1 ) {
228
+ throw new Error ( `please use handle method to work with other method ids` ) ;
229
+ }
230
+
231
+ if ( ciRequest . type !== PROTOCOL_MESSAGE_TYPE . CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE ) {
232
+ throw new Error ( 'Invalid message type for contract invoke request' ) ;
233
+ }
234
+
235
+ const { ethSigner, challenge } = opts ;
236
+ if ( ! ethSigner ) {
237
+ throw new Error ( "Can't sign transaction. Provide Signer in options." ) ;
238
+ }
239
+
240
+ const { chain_id } = ciRequest . body . transaction_data ;
241
+ const networkFlag = Object . keys ( ChainIds ) . find ( ( key ) => ChainIds [ key ] === chain_id ) ;
242
+
243
+ if ( ! networkFlag ) {
244
+ throw new Error ( `Invalid chain id ${ chain_id } ` ) ;
245
+ }
246
+ const verifierDid = ciRequest . from ? DID . parse ( ciRequest . from ) : undefined ;
247
+ const zkpResponses = await processZeroKnowledgeProofRequests (
248
+ did ,
249
+ ciRequest ?. body ?. scope ,
250
+ verifierDid ,
251
+ this . _proofService ,
252
+ { ethSigner, challenge, supportedCircuits : this . _supportedCircuits }
253
+ ) ;
254
+ return this . _zkpVerifier . submitZKPResponse (
255
+ ethSigner ,
256
+ ciRequest . body . transaction_data ,
257
+ zkpResponses
258
+ ) ;
173
259
}
174
260
}
0 commit comments