Repository navigation
Expand file tree
/
Copy pathproof-service.ts
More file actions
550 lines (484 loc) · 17.2 KB
/
Copy pathproof-service.ts
File metadata and controls
550 lines (484 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import {
BytesHelper,
DID,
MerklizedRootPosition,
getDateFromUnixTimestamp
} from '@iden3/js-iden3-core';
import {
AuthV2Inputs,
AuthV2PubSignals,
CircuitId,
Operators,
Query,
TreeState,
ValueProof
} from '../circuits';
import { ICredentialWallet } from '../credentials';
import { IIdentityWallet } from '../identity';
import {
createVerifiablePresentation,
ProofQuery,
RevocationStatus,
W3CCredential
} from '../verifiable';
import {
PreparedCredential,
QueryMetadata,
parseCredentialSubject,
parseQueryMetadata,
toGISTProof,
transformQueryValueToBigInts
} from './common';
import { IZKProver, NativeProver } from './provers/prover';
import { Merklizer, Options, getDocumentLoader } from '@iden3/js-jsonld-merklization';
import { ZKProof } from '@iden3/js-jwz';
import { Signer } from 'ethers';
import {
StateVerificationOpts,
JSONObject,
ZeroKnowledgeProofRequest,
ZeroKnowledgeProofResponse,
PROTOCOL_CONSTANTS
} from '../iden3comm';
import { cacheLoader } from '../schema-processor';
import { ICircuitStorage, IStateStorage } from '../storage';
import { byteDecoder, byteEncoder } from '../utils/encoding';
import {
InputGenerator,
ProofGenerationOptions,
ProofInputsParams
} from './provers/inputs-generator';
import { PubSignalsVerifier, VerifyContext } from './verifiers/pub-signals-verifier';
import { VerifyOpts } from './verifiers';
export interface QueryWithFieldName {
query: Query;
fieldName: string;
rawValue?: unknown;
isSelectiveDisclosure?: boolean;
}
/**
* Metadata that returns on verification
* @type VerificationResultMetadata
*/
export type VerificationResultMetadata = {
linkID?: number;
};
/**
* List of options to customize ProofService
*/
export type ProofServiceOptions = Options & {
prover?: IZKProver;
};
export interface ProofVerifyOpts {
query: ProofQuery;
sender: string;
opts?: VerifyOpts;
params?: JSONObject;
}
export interface IProofService {
/**
* Verification of zkp proof for given circuit id
*
* @param {ZKProof} zkp - proof to verify
* @param {CircuitId} circuitId - circuit id
* @returns `{Promise<boolean>}`
*/
verifyProof(zkp: ZKProof, circuitName: CircuitId): Promise<boolean>;
/**
* Verification of zkp proof and pub signals for given circuit id
*
* @param {ZeroKnowledgeProofResponse} response - zero knowledge proof response
* @param {ProofVerifyOpts} opts - proof verification options
* @returns `{Promise<VerificationResultMetadata>}`
*/
verifyZKPResponse(
proofResp: ZeroKnowledgeProofResponse,
opts: ProofVerifyOpts
): Promise<VerificationResultMetadata>;
/**
* Generate proof from given identity and credential for protocol proof request
*
* @param {ZeroKnowledgeProofRequest} proofReq - protocol zkp request
* @param {DID} identifier - did that will generate proof
* @param {W3CCredential} credential - credential that will be used for proof generation
* @param {ProofGenerationOptions} opts - options that will be used for proof generation
*
* @returns `Promise<ZeroKnowledgeProofResponse>`
*/
generateProof(
proofReq: ZeroKnowledgeProofRequest,
identifier: DID,
opts?: ProofGenerationOptions
): Promise<ZeroKnowledgeProofResponse>;
/**
* generates auth inputs
*
* @param {Uint8Array} hash - challenge that will be signed
* @param {DID} did - identity that will generate a proof
* @param {Number} profileNonce - identity that will generate a proof
* @param {CircuitId} circuitId - circuit id for authentication
* @returns `Promise<Uint8Array>`
*/
generateAuthV2Inputs(hash: Uint8Array, did: DID, circuitId: CircuitId): Promise<Uint8Array>;
/**
* state verification function
*
* @param {string} circuitId - id of authentication circuit
* @param {Array<string>} pubSignals - public signals of authentication circuit
* @returns `Promise<boolean>`
*/
verifyState(circuitId: string, pubSignals: Array<string>): Promise<boolean>;
/**
* transitState is done always to the latest state
*
* Generates a state transition proof and publishes state to the blockchain
*
* @param {DID} did - identity that will transit state
* @param {TreeState} oldTreeState - previous tree state
* @param {boolean} isOldStateGenesis - is a transition state is done from genesis state
* @param {Signer} ethSigner - signer for transaction
* @returns `{Promise<string>}` - transaction hash is returned
*/
transitState(
did: DID,
oldTreeState: TreeState,
isOldStateGenesis: boolean,
stateStorage: IStateStorage,
ethSigner: Signer
): Promise<string>;
findCredentialByProofQuery(
did: DID,
query: ProofQuery,
opts?: { skipClaimRevocationCheck: boolean }
): Promise<{ cred: W3CCredential; revStatus: RevocationStatus | undefined }>;
}
/**
* Proof service is an implementation of IProofService
* that works with a native groth16 prover
*
* @public
* @class ProofService
* @implements implements IProofService interface
*/
export class ProofService implements IProofService {
private readonly _prover: IZKProver;
private readonly _ldOptions: Options;
private readonly _inputsGenerator: InputGenerator;
private readonly _pubSignalsVerifier: PubSignalsVerifier;
/**
* Creates an instance of ProofService.
* @param {IIdentityWallet} _identityWallet - identity wallet
* @param {ICredentialWallet} _credentialWallet - credential wallet
* @param {ICircuitStorage} _circuitStorage - circuit storage to load proving / verification files
* @param {IStateStorage} _stateStorage - state storage to get GIST proof / publish state
*/
constructor(
private readonly _identityWallet: IIdentityWallet,
private readonly _credentialWallet: ICredentialWallet,
_circuitStorage: ICircuitStorage,
private readonly _stateStorage: IStateStorage,
opts?: ProofServiceOptions
) {
this._prover = opts?.prover ?? new NativeProver(_circuitStorage);
this._ldOptions = { ...opts, documentLoader: opts?.documentLoader ?? cacheLoader(opts) };
this._inputsGenerator = new InputGenerator(_identityWallet, _credentialWallet, _stateStorage);
this._pubSignalsVerifier = new PubSignalsVerifier(
opts?.documentLoader ?? cacheLoader(opts),
_stateStorage
);
}
/** {@inheritdoc IProofService.verifyProof} */
async verifyProof(zkp: ZKProof, circuitId: CircuitId): Promise<boolean> {
return this._prover.verify(zkp, circuitId);
}
/** {@inheritdoc IProofService.verify} */
async verifyZKPResponse(
proofResp: ZeroKnowledgeProofResponse,
opts: ProofVerifyOpts
): Promise<VerificationResultMetadata> {
const proofValid = await this._prover.verify(proofResp, proofResp.circuitId);
if (!proofValid) {
throw Error(
`Proof with circuit id ${proofResp.circuitId} and request id ${proofResp.id} is not valid`
);
}
const verifyContext: VerifyContext = {
pubSignals: proofResp.pub_signals,
query: opts.query,
verifiablePresentation: proofResp.vp as JSON,
sender: opts.sender,
challenge: BigInt(proofResp.id),
opts: opts.opts,
params: opts.params
};
const pubSignals = await this._pubSignalsVerifier.verify(proofResp.circuitId, verifyContext);
return { linkID: (pubSignals as unknown as { linkID?: number }).linkID };
}
/** {@inheritdoc IProofService.generateProof} */
async generateProof(
proofReq: ZeroKnowledgeProofRequest,
identifier: DID,
opts?: ProofGenerationOptions
): Promise<ZeroKnowledgeProofResponse> {
if (!opts) {
opts = {
skipRevocation: false,
challenge: 0n
};
}
let credentialWithRevStatus: {
cred: W3CCredential | undefined;
revStatus: RevocationStatus | undefined;
} = { cred: opts.credential, revStatus: opts.credentialRevocationStatus };
if (!opts.credential) {
credentialWithRevStatus = await this.findCredentialByProofQuery(identifier, proofReq.query);
}
if (opts.credential && !opts.credentialRevocationStatus && !opts.skipRevocation) {
const revStatus = await this._credentialWallet.getRevocationStatusFromCredential(
opts.credential
);
credentialWithRevStatus = { cred: opts.credential, revStatus };
}
if (!credentialWithRevStatus.cred) {
throw new Error(`credential not found for query ${JSON.stringify(proofReq.query)}`);
}
const credentialCoreClaim = await this._identityWallet.getCoreClaimFromCredential(
credentialWithRevStatus.cred
);
const { nonce: authProfileNonce, genesisDID } =
await this._identityWallet.getGenesisDIDMetadata(identifier);
const preparedCredential: PreparedCredential = {
credential: credentialWithRevStatus.cred,
credentialCoreClaim,
revStatus: credentialWithRevStatus.revStatus
};
const subjectDID = DID.parse(preparedCredential.credential.credentialSubject['id'] as string);
const { nonce: credentialSubjectProfileNonce, genesisDID: subjectGenesisDID } =
await this._identityWallet.getGenesisDIDMetadata(subjectDID);
if (subjectGenesisDID.string() !== genesisDID.string()) {
throw new Error('subject and auth profiles are not derived from the same did');
}
const propertiesMetadata = parseCredentialSubject(
proofReq.query.credentialSubject as JSONObject
);
if (!propertiesMetadata.length) {
throw new Error('no queries in zkp request');
}
const mtPosition = preparedCredential.credentialCoreClaim.getMerklizedPosition();
let mk: Merklizer | undefined;
if (mtPosition !== MerklizedRootPosition.None) {
mk = await preparedCredential.credential.merklize(this._ldOptions);
}
const context = proofReq.query['context'] as string;
const groupId = proofReq.query['groupId'] as number;
const ldContext = await this.loadLdContext(context);
const credentialType = proofReq.query['type'] as string;
const queriesMetadata: QueryMetadata[] = [];
const circuitQueries: Query[] = [];
for (const propertyMetadata of propertiesMetadata) {
const queryMetadata = await parseQueryMetadata(
propertyMetadata,
byteDecoder.decode(ldContext),
credentialType,
this._ldOptions
);
queriesMetadata.push(queryMetadata);
const circuitQuery = await this.toCircuitsQuery(
preparedCredential.credential,
queryMetadata,
mk
);
circuitQueries.push(circuitQuery);
}
const inputs = await this.generateInputs(
preparedCredential,
genesisDID,
proofReq,
{
...opts,
authProfileNonce,
credentialSubjectProfileNonce,
linkNonce: groupId ? opts.linkNonce : 0n
},
circuitQueries
);
const sdQueries = queriesMetadata.filter((q) => q.operator === Operators.SD);
let vp: object | undefined;
if (sdQueries.length) {
vp = createVerifiablePresentation(
context,
credentialType,
preparedCredential.credential,
sdQueries
);
}
const { proof, pub_signals } = await this._prover.generate(inputs, proofReq.circuitId);
return {
id: proofReq.id,
circuitId: proofReq.circuitId,
vp,
proof,
pub_signals
};
}
/** {@inheritdoc IProofService.transitState} */
async transitState(
did: DID,
oldTreeState: TreeState,
isOldStateGenesis: boolean,
stateStorage: IStateStorage, // for compatibility with previous versions we leave this parameter
ethSigner: Signer
): Promise<string> {
return this._identityWallet.transitState(
did,
oldTreeState,
isOldStateGenesis,
ethSigner,
this._prover
);
}
private async generateInputs(
preparedCredential: PreparedCredential,
identifier: DID,
proofReq: ZeroKnowledgeProofRequest,
params: ProofInputsParams,
circuitQueries: Query[]
): Promise<Uint8Array> {
return this._inputsGenerator.generateInputs({
preparedCredential,
identifier,
proofReq,
params,
circuitQueries
});
}
private async toCircuitsQuery(
credential: W3CCredential,
queryMetadata: QueryMetadata,
merklizedCredential?: Merklizer
): Promise<Query> {
if (queryMetadata.merklizedSchema && !merklizedCredential) {
throw new Error('merklized root position is set to None for merklized schema');
}
if (!queryMetadata.merklizedSchema && merklizedCredential) {
throw new Error('merklized root position is not set to None for non-merklized schema');
}
const query = new Query();
query.slotIndex = queryMetadata.slotIndex;
query.operator = queryMetadata.operator;
query.values = queryMetadata.values;
if (queryMetadata.merklizedSchema && merklizedCredential) {
const { proof, value: mtValue } = await merklizedCredential.proof(queryMetadata.path);
query.valueProof = new ValueProof();
query.valueProof.mtp = proof;
query.valueProof.path = queryMetadata.claimPathKey;
const mtEntry = (await mtValue?.mtEntry()) ?? 0n;
query.valueProof.value = mtEntry;
if (!queryMetadata.fieldName) {
query.values = [mtEntry];
return query;
}
}
if (queryMetadata.operator === Operators.SD) {
const [first, ...rest] = queryMetadata.fieldName.split('.');
let v = credential.credentialSubject[first];
for (const part of rest) {
v = (v as JSONObject)[part];
}
if (typeof v === 'undefined') {
throw new Error(`credential doesn't contain value for field ${queryMetadata.fieldName}`);
}
query.values = await transformQueryValueToBigInts(v, queryMetadata.datatype);
}
return query;
}
private async loadLdContext(context: string): Promise<Uint8Array> {
const loader = getDocumentLoader(this._ldOptions);
let ldSchema: object;
try {
ldSchema = (await loader(context)).document;
} catch (e) {
throw new Error(`can't load ld context from url ${context}`);
}
return byteEncoder.encode(JSON.stringify(ldSchema));
}
/** {@inheritdoc IProofService.generateAuthV2Inputs} */
async generateAuthV2Inputs(
hash: Uint8Array,
did: DID,
circuitId: CircuitId
): Promise<Uint8Array> {
if (circuitId !== CircuitId.AuthV2) {
throw new Error('CircuitId is not supported');
}
const { nonce: authProfileNonce, genesisDID } =
await this._identityWallet.getGenesisDIDMetadata(did);
const challenge = BytesHelper.bytesToInt(hash.reverse());
const authPrepared = await this._inputsGenerator.prepareAuthBJJCredential(genesisDID);
const signature = await this._identityWallet.signChallenge(challenge, authPrepared.credential);
const id = DID.idFromDID(genesisDID);
const stateProof = await this._stateStorage.getGISTProof(id.bigInt());
const gistProof = toGISTProof(stateProof);
const authInputs = new AuthV2Inputs();
authInputs.genesisID = id;
authInputs.profileNonce = BigInt(authProfileNonce);
authInputs.authClaim = authPrepared.coreClaim;
authInputs.authClaimIncMtp = authPrepared.incProof.proof;
authInputs.authClaimNonRevMtp = authPrepared.nonRevProof.proof;
authInputs.treeState = authPrepared.incProof.treeState;
authInputs.signature = signature;
authInputs.challenge = challenge;
authInputs.gistProof = gistProof;
return authInputs.inputsMarshal();
}
async verifyState(
circuitId: string,
pubSignals: string[],
opts: StateVerificationOpts = {
acceptedStateTransitionDelay: PROTOCOL_CONSTANTS.DEFAULT_AUTH_VERIFY_DELAY
}
): Promise<boolean> {
if (circuitId !== CircuitId.AuthV2) {
throw new Error(`CircuitId is not supported ${circuitId}`);
}
const authV2PubSignals = new AuthV2PubSignals().pubSignalsUnmarshal(
byteEncoder.encode(JSON.stringify(pubSignals))
);
const gistRoot = authV2PubSignals.GISTRoot.bigInt();
const userId = authV2PubSignals.userID.bigInt();
const globalStateInfo = await this._stateStorage.getGISTRootInfo(gistRoot, userId);
if (globalStateInfo.root !== gistRoot) {
throw new Error(`gist info contains invalid state`);
}
if (globalStateInfo.replacedByRoot !== 0n) {
if (globalStateInfo.replacedAtTimestamp === 0n) {
throw new Error(`state was replaced, but replaced time unknown`);
}
const timeDiff =
Date.now() -
getDateFromUnixTimestamp(Number(globalStateInfo.replacedAtTimestamp)).getTime();
if (
timeDiff >
(opts?.acceptedStateTransitionDelay ?? PROTOCOL_CONSTANTS.DEFAULT_AUTH_VERIFY_DELAY)
) {
throw new Error('global state is outdated');
}
}
return true;
}
async findCredentialByProofQuery(
did: DID,
query: ProofQuery
): Promise<{ cred: W3CCredential; revStatus: RevocationStatus | undefined }> {
const credentials = await this._identityWallet.findOwnedCredentialsByDID(did, query);
if (!credentials.length) {
throw new Error(`no credentials belong to did or its profiles`);
}
// For EQ / IN / NIN / LT / GT operations selective if credential satisfies query - we can get any.
// TODO: choose credential for selective credentials
const credential = query.skipClaimRevocationCheck
? { cred: credentials[0], revStatus: undefined }
: await this._credentialWallet.findNonRevokedCredential(credentials);
return credential;
}
}