@0xpolygonid/js-sdk 1.8.1 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -115,6 +115,16 @@ class CredentialWallet {
115
115
  }
116
116
  throw new Error('all auth bjj credentials are revoked');
117
117
  }
118
+ /**
119
+ * {@inheritDoc ICredentialWallet.getAllAuthBJJCredentials}
120
+ */
121
+ async getAllAuthBJJCredentials(did) {
122
+ return this._storage.credential.findCredentialsByQuery({
123
+ context: verifiable_1.VerifiableConstants.AUTH.AUTH_BJJ_CREDENTIAL_SCHEMA_JSONLD_URL,
124
+ type: verifiable_1.VerifiableConstants.AUTH.AUTH_BJJ_CREDENTIAL_TYPE,
125
+ allowedIssuers: [did.string()]
126
+ });
127
+ }
118
128
  /**
119
129
  * {@inheritDoc ICredentialWallet.getRevocationStatusFromCredential}
120
130
  */
@@ -61,13 +61,19 @@ class IdentityWallet {
61
61
  this._storage = _storage;
62
62
  this._credentialWallet = _credentialWallet;
63
63
  this._opts = _opts;
64
+ this._credentialStatusPublisherRegistry = this.getCredentialStatusPublisherRegistry(_opts);
65
+ }
66
+ getCredentialStatusPublisherRegistry(_opts) {
64
67
  if (!_opts?.credentialStatusPublisherRegistry) {
65
- this._credentialStatusPublisherRegistry = new credential_status_publisher_1.CredentialStatusPublisherRegistry();
66
- this._credentialStatusPublisherRegistry.register(verifiable_1.CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, new credential_status_publisher_1.Iden3SmtRhsCredentialStatusPublisher());
68
+ const registry = new credential_status_publisher_1.CredentialStatusPublisherRegistry();
69
+ const emptyPublisher = { publish: () => Promise.resolve() };
70
+ registry.register(verifiable_1.CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, new credential_status_publisher_1.Iden3SmtRhsCredentialStatusPublisher());
71
+ registry.register(verifiable_1.CredentialStatusType.SparseMerkleTreeProof, emptyPublisher);
72
+ registry.register(verifiable_1.CredentialStatusType.Iden3commRevocationStatusV1, emptyPublisher);
73
+ return registry;
67
74
  }
68
75
  else {
69
- this._credentialStatusPublisherRegistry = this._opts
70
- ?.credentialStatusPublisherRegistry;
76
+ return this._opts?.credentialStatusPublisherRegistry;
71
77
  }
72
78
  }
73
79
  /**
@@ -305,7 +311,7 @@ class IdentityWallet {
305
311
  catch (e) {
306
312
  throw new Error(`Error create w3c credential ${e.message}`);
307
313
  }
308
- const issuerAuthBJJCredential = await this._credentialWallet.getAuthBJJCredential(issuerDID);
314
+ const { authCredential: issuerAuthBJJCredential } = await this.getActualAuthCredential(issuerDID);
309
315
  const coreClaimOpts = {
310
316
  revNonce: req.revocationOpts.nonce,
311
317
  subjectPosition: req.subjectPosition,
@@ -314,7 +320,7 @@ class IdentityWallet {
314
320
  version: 0,
315
321
  merklizeOpts: { ...opts, documentLoader: loader }
316
322
  };
317
- const coreClaim = await schema_processor_1.Parser.parseClaim(credential, coreClaimOpts);
323
+ const coreClaim = await credential.toCoreClaim(coreClaimOpts);
318
324
  const { hi, hv } = coreClaim.hiHv();
319
325
  const coreClaimHash = js_crypto_1.poseidon.hash([hi, hv]);
320
326
  const signature = await this.signChallenge(coreClaimHash, issuerAuthBJJCredential);
@@ -339,6 +345,25 @@ class IdentityWallet {
339
345
  credential.proof = [sigProof];
340
346
  return credential;
341
347
  }
348
+ /** {@inheritDoc IIdentityWallet.getActualAuthCredential} */
349
+ async getActualAuthCredential(did, treeStateInfo) {
350
+ const authCredentials = await this._credentialWallet.getAllAuthBJJCredentials(did);
351
+ for (let i = 0; i < authCredentials.length; i++) {
352
+ const incProof = await this.generateCredentialMtp(did, authCredentials[i], treeStateInfo);
353
+ if (!incProof.proof.existence) {
354
+ continue;
355
+ }
356
+ const nonRevProof = await this.generateNonRevocationMtp(did, authCredentials[i], treeStateInfo);
357
+ if (!nonRevProof.proof.existence) {
358
+ return {
359
+ authCredential: authCredentials[i],
360
+ incProof,
361
+ nonRevProof
362
+ };
363
+ }
364
+ }
365
+ throw new Error('no auth credentials found');
366
+ }
342
367
  /** {@inheritDoc IIdentityWallet.revokeCredential} */
343
368
  async revokeCredential(issuerDID, credential) {
344
369
  const issuerTree = await this.getDIDTreeModel(issuerDID);
@@ -265,8 +265,8 @@ class InputGenerator {
265
265
  circuitInputs.proofType = proofType;
266
266
  circuitInputs.linkNonce = params.linkNonce ?? BigInt(0);
267
267
  circuitInputs.verifierID = params.verifierDid ? js_iden3_core_1.DID.idFromDID(params.verifierDid) : undefined;
268
- circuitInputs.nullifierSessionID = proofReq.params?.nullifierSessionID
269
- ? BigInt(proofReq.params?.nullifierSessionID?.toString())
268
+ circuitInputs.nullifierSessionID = proofReq.params?.nullifierSessionId
269
+ ? BigInt(proofReq.params?.nullifierSessionId?.toString())
270
270
  : BigInt(0);
271
271
  let isEthIdentity = true;
272
272
  try {
@@ -375,9 +375,7 @@ class InputGenerator {
375
375
  return circuitClaim;
376
376
  }
377
377
  async prepareAuthBJJCredential(did, treeStateInfo) {
378
- const authCredential = await this._credentialWallet.getAuthBJJCredential(did);
379
- const incProof = await this._identityWallet.generateCredentialMtp(did, authCredential, treeStateInfo);
380
- const nonRevProof = await this._identityWallet.generateNonRevocationMtp(did, authCredential, treeStateInfo);
378
+ const { authCredential, incProof, nonRevProof } = await this._identityWallet.getActualAuthCredential(did, treeStateInfo);
381
379
  const authCoreClaim = authCredential.getCoreClaimFromProof(verifiable_1.ProofType.Iden3SparseMerkleTreeProof);
382
380
  if (!authCoreClaim) {
383
381
  throw new Error('auth core claim is not defined for auth bjj credential');
@@ -89,6 +89,16 @@ export class CredentialWallet {
89
89
  }
90
90
  throw new Error('all auth bjj credentials are revoked');
91
91
  }
92
+ /**
93
+ * {@inheritDoc ICredentialWallet.getAllAuthBJJCredentials}
94
+ */
95
+ async getAllAuthBJJCredentials(did) {
96
+ return this._storage.credential.findCredentialsByQuery({
97
+ context: VerifiableConstants.AUTH.AUTH_BJJ_CREDENTIAL_SCHEMA_JSONLD_URL,
98
+ type: VerifiableConstants.AUTH.AUTH_BJJ_CREDENTIAL_TYPE,
99
+ allowedIssuers: [did.string()]
100
+ });
101
+ }
92
102
  /**
93
103
  * {@inheritDoc ICredentialWallet.getRevocationStatusFromCredential}
94
104
  */
@@ -4,7 +4,7 @@ import { poseidon, PublicKey, sha256, Signature, Hex, getRandomBytes } from '@id
4
4
  import { hashElems, ZERO_HASH } from '@iden3/js-merkletree';
5
5
  import { generateProfileDID, subjectPositionIndex } from './common';
6
6
  import * as uuid from 'uuid';
7
- import { Parser, JsonSchemaValidator, cacheLoader } from '../schema-processor';
7
+ import { JsonSchemaValidator, cacheLoader } from '../schema-processor';
8
8
  import { MerkleTreeType } from '../storage';
9
9
  import { VerifiableConstants, BJJSignatureProof2021, MerklizedRootPosition, SubjectPosition, W3CCredential, Iden3SparseMerkleTreeProof, ProofType, CredentialStatusType } from '../verifiable';
10
10
  import { getKMSIdByAuthCredential, getNodesRepresentation, pushHashesToRHS } from '../credentials';
@@ -35,13 +35,19 @@ export class IdentityWallet {
35
35
  this._storage = _storage;
36
36
  this._credentialWallet = _credentialWallet;
37
37
  this._opts = _opts;
38
+ this._credentialStatusPublisherRegistry = this.getCredentialStatusPublisherRegistry(_opts);
39
+ }
40
+ getCredentialStatusPublisherRegistry(_opts) {
38
41
  if (!_opts?.credentialStatusPublisherRegistry) {
39
- this._credentialStatusPublisherRegistry = new CredentialStatusPublisherRegistry();
40
- this._credentialStatusPublisherRegistry.register(CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, new Iden3SmtRhsCredentialStatusPublisher());
42
+ const registry = new CredentialStatusPublisherRegistry();
43
+ const emptyPublisher = { publish: () => Promise.resolve() };
44
+ registry.register(CredentialStatusType.Iden3ReverseSparseMerkleTreeProof, new Iden3SmtRhsCredentialStatusPublisher());
45
+ registry.register(CredentialStatusType.SparseMerkleTreeProof, emptyPublisher);
46
+ registry.register(CredentialStatusType.Iden3commRevocationStatusV1, emptyPublisher);
47
+ return registry;
41
48
  }
42
49
  else {
43
- this._credentialStatusPublisherRegistry = this._opts
44
- ?.credentialStatusPublisherRegistry;
50
+ return this._opts?.credentialStatusPublisherRegistry;
45
51
  }
46
52
  }
47
53
  /**
@@ -279,7 +285,7 @@ export class IdentityWallet {
279
285
  catch (e) {
280
286
  throw new Error(`Error create w3c credential ${e.message}`);
281
287
  }
282
- const issuerAuthBJJCredential = await this._credentialWallet.getAuthBJJCredential(issuerDID);
288
+ const { authCredential: issuerAuthBJJCredential } = await this.getActualAuthCredential(issuerDID);
283
289
  const coreClaimOpts = {
284
290
  revNonce: req.revocationOpts.nonce,
285
291
  subjectPosition: req.subjectPosition,
@@ -288,7 +294,7 @@ export class IdentityWallet {
288
294
  version: 0,
289
295
  merklizeOpts: { ...opts, documentLoader: loader }
290
296
  };
291
- const coreClaim = await Parser.parseClaim(credential, coreClaimOpts);
297
+ const coreClaim = await credential.toCoreClaim(coreClaimOpts);
292
298
  const { hi, hv } = coreClaim.hiHv();
293
299
  const coreClaimHash = poseidon.hash([hi, hv]);
294
300
  const signature = await this.signChallenge(coreClaimHash, issuerAuthBJJCredential);
@@ -313,6 +319,25 @@ export class IdentityWallet {
313
319
  credential.proof = [sigProof];
314
320
  return credential;
315
321
  }
322
+ /** {@inheritDoc IIdentityWallet.getActualAuthCredential} */
323
+ async getActualAuthCredential(did, treeStateInfo) {
324
+ const authCredentials = await this._credentialWallet.getAllAuthBJJCredentials(did);
325
+ for (let i = 0; i < authCredentials.length; i++) {
326
+ const incProof = await this.generateCredentialMtp(did, authCredentials[i], treeStateInfo);
327
+ if (!incProof.proof.existence) {
328
+ continue;
329
+ }
330
+ const nonRevProof = await this.generateNonRevocationMtp(did, authCredentials[i], treeStateInfo);
331
+ if (!nonRevProof.proof.existence) {
332
+ return {
333
+ authCredential: authCredentials[i],
334
+ incProof,
335
+ nonRevProof
336
+ };
337
+ }
338
+ }
339
+ throw new Error('no auth credentials found');
340
+ }
316
341
  /** {@inheritDoc IIdentityWallet.revokeCredential} */
317
342
  async revokeCredential(issuerDID, credential) {
318
343
  const issuerTree = await this.getDIDTreeModel(issuerDID);
@@ -262,8 +262,8 @@ export class InputGenerator {
262
262
  circuitInputs.proofType = proofType;
263
263
  circuitInputs.linkNonce = params.linkNonce ?? BigInt(0);
264
264
  circuitInputs.verifierID = params.verifierDid ? DID.idFromDID(params.verifierDid) : undefined;
265
- circuitInputs.nullifierSessionID = proofReq.params?.nullifierSessionID
266
- ? BigInt(proofReq.params?.nullifierSessionID?.toString())
265
+ circuitInputs.nullifierSessionID = proofReq.params?.nullifierSessionId
266
+ ? BigInt(proofReq.params?.nullifierSessionId?.toString())
267
267
  : BigInt(0);
268
268
  let isEthIdentity = true;
269
269
  try {
@@ -372,9 +372,7 @@ export class InputGenerator {
372
372
  return circuitClaim;
373
373
  }
374
374
  async prepareAuthBJJCredential(did, treeStateInfo) {
375
- const authCredential = await this._credentialWallet.getAuthBJJCredential(did);
376
- const incProof = await this._identityWallet.generateCredentialMtp(did, authCredential, treeStateInfo);
377
- const nonRevProof = await this._identityWallet.generateNonRevocationMtp(did, authCredential, treeStateInfo);
375
+ const { authCredential, incProof, nonRevProof } = await this._identityWallet.getActualAuthCredential(did, treeStateInfo);
378
376
  const authCoreClaim = authCredential.getCoreClaimFromProof(ProofType.Iden3SparseMerkleTreeProof);
379
377
  if (!authCoreClaim) {
380
378
  throw new Error('auth core claim is not defined for auth bjj credential');
@@ -68,12 +68,19 @@ export interface ICredentialWallet {
68
68
  */
69
69
  filterByCredentialSubject(credentials: W3CCredential[], subject: DID): Promise<W3CCredential[]>;
70
70
  /**
71
- * Finds Auth BJJ credential for given user
71
+ * Finds first non-revoked Auth BJJ credential for given user
72
72
  *
73
73
  * @param {DID} did - the issuer of Auth BJJ credential
74
74
  * @returns `Promise<W3CCredential>` W3CCredential with AuthBJJCredential type
75
75
  */
76
76
  getAuthBJJCredential(did: DID): Promise<W3CCredential>;
77
+ /**
78
+ * Finds all Auth BJJ credentials for given user
79
+ *
80
+ * @param {DID} did - the issuer of Auth BJJ credential
81
+ * @returns `Promise<W3CCredential[]>` W3CCredentials with AuthBJJCredential type
82
+ */
83
+ getAllAuthBJJCredentials(did: DID): Promise<W3CCredential[]>;
77
84
  /**
78
85
  * Fetches or Builds a revocation status for a given credential
79
86
  * Supported types for credentialStatus field: SparseMerkleTreeProof, Iden3ReverseSparseMerkleTreeProof
@@ -137,6 +144,10 @@ export declare class CredentialWallet implements ICredentialWallet {
137
144
  * {@inheritDoc ICredentialWallet.getAuthBJJCredential}
138
145
  */
139
146
  getAuthBJJCredential(did: DID): Promise<W3CCredential>;
147
+ /**
148
+ * {@inheritDoc ICredentialWallet.getAllAuthBJJCredentials}
149
+ */
150
+ getAllAuthBJJCredentials(did: DID): Promise<W3CCredential[]>;
140
151
  /**
141
152
  * {@inheritDoc ICredentialWallet.getRevocationStatusFromCredential}
142
153
  */
@@ -1,6 +1,7 @@
1
1
  import { ZKProof } from '@iden3/js-jwz';
2
2
  import { MediaType } from '../../constants';
3
3
  import { JSONObject, ProtocolMessage } from '../packer';
4
+ import { DID } from '@iden3/js-iden3-core';
4
5
  /** AuthorizationResponseMessage is struct the represents iden3message authorization response */
5
6
  export type AuthorizationResponseMessage = {
6
7
  id: string;
@@ -41,7 +42,10 @@ export type ZeroKnowledgeProofRequest = {
41
42
  circuitId: string;
42
43
  optional?: boolean;
43
44
  query: JSONObject;
44
- params?: JSONObject;
45
+ params?: {
46
+ nullifierSessionId?: string | number;
47
+ verifierDid?: DID;
48
+ };
45
49
  };
46
50
  /** ZeroKnowledgeProofResponse represents structure of zkp response */
47
51
  export type ZeroKnowledgeProofResponse = {
@@ -247,6 +247,23 @@ export interface IIdentityWallet {
247
247
  * @returns `{Promise<void>}`
248
248
  */
249
249
  updateIdentityState(issuerDID: DID, published: boolean, treeState?: TreeState): Promise<void>;
250
+ /**
251
+ *
252
+ * gets actual auth credential with proofs for provided tree state or latest from the trees.
253
+ *
254
+ * @param {DID} issuerDID - identifier of the issuer
255
+ * @param {TreeState} treeStateInfo - optional, state for retrieval
256
+ * @returns `{Promise<{
257
+ authCredential: W3CCredential;
258
+ incProof: MerkleTreeProofWithTreeState;
259
+ nonRevProof: MerkleTreeProofWithTreeState;
260
+ }>}`
261
+ */
262
+ getActualAuthCredential(did: DID, treeStateInfo?: TreeState): Promise<{
263
+ authCredential: W3CCredential;
264
+ incProof: MerkleTreeProofWithTreeState;
265
+ nonRevProof: MerkleTreeProofWithTreeState;
266
+ }>;
250
267
  }
251
268
  /**
252
269
  * @public
@@ -276,6 +293,7 @@ export declare class IdentityWallet implements IIdentityWallet {
276
293
  constructor(_kms: KMS, _storage: IDataStorage, _credentialWallet: ICredentialWallet, _opts?: {
277
294
  credentialStatusPublisherRegistry?: CredentialStatusPublisherRegistry | undefined;
278
295
  } | undefined);
296
+ private getCredentialStatusPublisherRegistry;
279
297
  /**
280
298
  * {@inheritDoc IIdentityWallet.createIdentity}
281
299
  */
@@ -313,6 +331,12 @@ export declare class IdentityWallet implements IIdentityWallet {
313
331
  signChallenge(challenge: bigint, credential: W3CCredential): Promise<Signature>;
314
332
  /** {@inheritDoc IIdentityWallet.issueCredential} */
315
333
  issueCredential(issuerDID: DID, req: CredentialRequest, opts?: Options): Promise<W3CCredential>;
334
+ /** {@inheritDoc IIdentityWallet.getActualAuthCredential} */
335
+ getActualAuthCredential(did: DID, treeStateInfo?: TreeState): Promise<{
336
+ authCredential: W3CCredential;
337
+ incProof: MerkleTreeProofWithTreeState;
338
+ nonRevProof: MerkleTreeProofWithTreeState;
339
+ }>;
316
340
  /** {@inheritDoc IIdentityWallet.revokeCredential} */
317
341
  revokeCredential(issuerDID: DID, credential: W3CCredential): Promise<number>;
318
342
  /** {@inheritDoc IIdentityWallet.addCredentialsToMerkleTree} */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xpolygonid/js-sdk",
3
- "version": "1.8.1",
3
+ "version": "1.8.2",
4
4
  "description": "SDK to work with Polygon ID",
5
5
  "main": "dist/node/cjs/index.js",
6
6
  "module": "dist/node/esm/index.js",