@0xpolygonid/js-sdk 1.9.0 → 1.9.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.
Files changed (31) hide show
  1. package/dist/browser/esm/index.js +201 -78
  2. package/dist/browser/esm/index.js.map +1 -1
  3. package/dist/browser/umd/index.js +202 -77
  4. package/dist/browser/umd/index.js.map +1 -1
  5. package/dist/node/cjs/iden3comm/handlers/auth.js +14 -55
  6. package/dist/node/cjs/iden3comm/handlers/common.js +85 -0
  7. package/dist/node/cjs/iden3comm/handlers/contract-request.js +5 -15
  8. package/dist/node/cjs/iden3comm/handlers/index.js +2 -0
  9. package/dist/node/cjs/iden3comm/handlers/revocation-status.js +119 -0
  10. package/dist/node/cjs/identity/identity-wallet.js +4 -0
  11. package/dist/node/cjs/proof/common.js +11 -3
  12. package/dist/node/cjs/proof/proof-service.js +1 -4
  13. package/dist/node/cjs/storage/filters/jsonQuery.js +1 -1
  14. package/dist/node/esm/iden3comm/handlers/auth.js +15 -56
  15. package/dist/node/esm/iden3comm/handlers/common.js +81 -0
  16. package/dist/node/esm/iden3comm/handlers/contract-request.js +5 -15
  17. package/dist/node/esm/iden3comm/handlers/index.js +2 -0
  18. package/dist/node/esm/iden3comm/handlers/revocation-status.js +92 -0
  19. package/dist/node/esm/identity/identity-wallet.js +4 -0
  20. package/dist/node/esm/proof/common.js +12 -4
  21. package/dist/node/esm/proof/proof-service.js +1 -4
  22. package/dist/node/esm/storage/filters/jsonQuery.js +1 -1
  23. package/dist/types/iden3comm/handlers/auth.d.ts +1 -0
  24. package/dist/types/iden3comm/handlers/common.d.ts +23 -0
  25. package/dist/types/iden3comm/handlers/contract-request.d.ts +1 -1
  26. package/dist/types/iden3comm/handlers/index.d.ts +2 -0
  27. package/dist/types/iden3comm/handlers/revocation-status.d.ts +59 -0
  28. package/dist/types/identity/identity-wallet.d.ts +13 -1
  29. package/dist/types/verifiable/credential.d.ts +1 -1
  30. package/dist/types/verifiable/proof.d.ts +5 -1
  31. package/package.json +1 -1
@@ -60108,7 +60108,7 @@ var PolygonIdSdk = (function (exports) {
60108
60108
  }
60109
60109
  const credentialPathValue = resolvePath(credential, this.path);
60110
60110
  if ((credentialPathValue === null || credentialPathValue === undefined) &&
60111
- this.operatorFunc !== existsComparator) {
60111
+ this.operatorFunc !== comparatorOptions.$exists) {
60112
60112
  return false;
60113
60113
  }
60114
60114
  if (this.isReverseParams) {
@@ -62129,6 +62129,10 @@ var PolygonIdSdk = (function (exports) {
62129
62129
  async generateNonRevocationMtp(did, credential, treeState) {
62130
62130
  const coreClaim = await this.getCoreClaimFromCredential(credential);
62131
62131
  const revNonce = coreClaim.getRevocationNonce();
62132
+ return this.generateNonRevocationMtpWithNonce(did, revNonce, treeState);
62133
+ }
62134
+ /** {@inheritDoc IIdentityWallet.generateNonRevocationMtpWithNonce} */
62135
+ async generateNonRevocationMtpWithNonce(did, revNonce, treeState) {
62132
62136
  const treesModel = await this.getDIDTreeModel(did);
62133
62137
  const revocationTree = await this._storage.mt.getMerkleTreeByIdentifierAndType(did.string(), exports.MerkleTreeType.Revocations);
62134
62138
  const claimsRoot = await treesModel.claimsTree.root();
@@ -62528,12 +62532,14 @@ var PolygonIdSdk = (function (exports) {
62528
62532
  throw new Error(`field does not exist in the schema ${e.message}`);
62529
62533
  }
62530
62534
  }
62531
- if (propertyQuery.operatorValue) {
62535
+ if (propertyQuery.operatorValue !== undefined) {
62532
62536
  if (!isValidOperation(query.datatype, propertyQuery.operator)) {
62533
62537
  throw new Error(`operator ${propertyQuery.operator} is not supported for datatype ${query.datatype}`);
62534
62538
  }
62535
- const datatype = propertyQuery.operator === exports.Operators.EXISTS ? exports.XSDNS.Boolean : query.datatype;
62536
- query.values = await transformQueryValueToBigInts(propertyQuery.operatorValue, datatype);
62539
+ query.values =
62540
+ propertyQuery.operator === exports.Operators.EXISTS
62541
+ ? transformExistsValue(propertyQuery.operatorValue)
62542
+ : await transformQueryValueToBigInts(propertyQuery.operatorValue, query.datatype);
62537
62543
  }
62538
62544
  return query;
62539
62545
  };
@@ -62553,6 +62559,12 @@ var PolygonIdSdk = (function (exports) {
62553
62559
  }
62554
62560
  return values;
62555
62561
  };
62562
+ const transformExistsValue = (value) => {
62563
+ if (typeof value == 'boolean') {
62564
+ return [BigInt(value)];
62565
+ }
62566
+ throw new Error('exists operator value must be true or false');
62567
+ };
62556
62568
 
62557
62569
  /* eslint-disable require-await */
62558
62570
  /* eslint-disable @typescript-eslint/no-explicit-any */
@@ -84784,10 +84796,7 @@ var PolygonIdSdk = (function (exports) {
84784
84796
  query.valueProof = new ValueProof();
84785
84797
  query.valueProof.mtp = proof;
84786
84798
  query.valueProof.path = queryMetadata.claimPathKey;
84787
- const mtEntry = await mtValue?.mtEntry();
84788
- if (!mtEntry) {
84789
- throw new Error(`can't merklize credential: no merkle tree entry found`);
84790
- }
84799
+ const mtEntry = (await mtValue?.mtEntry()) ?? 0n;
84791
84800
  query.valueProof.value = mtEntry;
84792
84801
  if (!queryMetadata.fieldName) {
84793
84802
  query.values = [mtEntry];
@@ -85457,6 +85466,86 @@ var PolygonIdSdk = (function (exports) {
85457
85466
  }
85458
85467
  }
85459
85468
 
85469
+ /**
85470
+ * Groups the ZeroKnowledgeProofRequest objects based on their groupId.
85471
+ * Returns a Map where the key is the groupId and the value is an object containing the query and linkNonce.
85472
+ *
85473
+ * @param requestScope - An array of ZeroKnowledgeProofRequest objects.
85474
+ * @returns A Map<number, { query: JSONObject; linkNonce: number }> representing the grouped queries.
85475
+ */
85476
+ const getGroupedQueries = (requestScope) => requestScope.reduce((acc, proofReq) => {
85477
+ const groupId = proofReq.query.groupId;
85478
+ if (!groupId) {
85479
+ return acc;
85480
+ }
85481
+ const existedData = acc.get(groupId);
85482
+ if (!existedData) {
85483
+ const seed = V$3(12);
85484
+ const dataView = new DataView(seed.buffer);
85485
+ const linkNonce = dataView.getUint32(0);
85486
+ acc.set(groupId, { query: proofReq.query, linkNonce });
85487
+ return acc;
85488
+ }
85489
+ const credentialSubject = mergeObjects(existedData.query.credentialSubject, proofReq.query.credentialSubject);
85490
+ acc.set(groupId, {
85491
+ ...existedData,
85492
+ query: {
85493
+ skipClaimRevocationCheck: existedData.query.skipClaimRevocationCheck || proofReq.query.skipClaimRevocationCheck,
85494
+ ...existedData.query,
85495
+ credentialSubject
85496
+ }
85497
+ });
85498
+ return acc;
85499
+ }, new Map());
85500
+ /**
85501
+ * Processes zero knowledge proof requests.
85502
+ *
85503
+ * @param senderIdentifier - The identifier of the sender.
85504
+ * @param requests - An array of zero knowledge proof requests.
85505
+ * @param from - The identifier of the sender.
85506
+ * @param proofService - The proof service.
85507
+ * @param opts - Additional options for processing the requests.
85508
+ * @returns A promise that resolves to an array of zero knowledge proof responses.
85509
+ */
85510
+ const processZeroKnowledgeProofRequests = async (senderIdentifier, requests, from, proofService, opts) => {
85511
+ const requestScope = requests ?? [];
85512
+ const combinedQueries = getGroupedQueries(requestScope);
85513
+ const groupedCredentialsCache = new Map();
85514
+ const zkpResponses = [];
85515
+ for (const proofReq of requestScope) {
85516
+ if (!opts.supportedCircuits.includes(proofReq.circuitId)) {
85517
+ throw new Error(`Circuit ${proofReq.circuitId} is not allowed`);
85518
+ }
85519
+ const query = proofReq.query;
85520
+ const groupId = query.groupId;
85521
+ const combinedQueryData = combinedQueries.get(groupId);
85522
+ if (groupId) {
85523
+ if (!combinedQueryData) {
85524
+ throw new Error(`Invalid group id ${query.groupId}`);
85525
+ }
85526
+ const combinedQuery = combinedQueryData.query;
85527
+ if (!groupedCredentialsCache.has(groupId)) {
85528
+ const credWithRevStatus = await proofService.findCredentialByProofQuery(senderIdentifier, combinedQueryData.query);
85529
+ if (!credWithRevStatus.cred) {
85530
+ throw new Error(`Credential not found for query ${JSON.stringify(combinedQuery)}`);
85531
+ }
85532
+ groupedCredentialsCache.set(groupId, credWithRevStatus);
85533
+ }
85534
+ }
85535
+ const credWithRevStatus = groupedCredentialsCache.get(groupId);
85536
+ const zkpRes = await proofService.generateProof(proofReq, senderIdentifier, {
85537
+ verifierDid: from,
85538
+ challenge: opts.challenge,
85539
+ skipRevocation: Boolean(query.skipClaimRevocationCheck),
85540
+ credential: credWithRevStatus?.cred,
85541
+ credentialRevocationStatus: credWithRevStatus?.revStatus,
85542
+ linkNonce: combinedQueryData?.linkNonce ? BigInt(combinedQueryData.linkNonce) : undefined
85543
+ });
85544
+ zkpResponses.push(zkpRes);
85545
+ }
85546
+ return zkpResponses;
85547
+ };
85548
+
85460
85549
  /**
85461
85550
  * createAuthorizationRequest is a function to create protocol authorization request
85462
85551
  * @param {string} reason - reason to request proof
@@ -85511,6 +85600,12 @@ var PolygonIdSdk = (function (exports) {
85511
85600
  constructor(_packerMgr, _proofService) {
85512
85601
  this._packerMgr = _packerMgr;
85513
85602
  this._proofService = _proofService;
85603
+ this._supportedCircuits = [
85604
+ exports.CircuitId.AtomicQueryV3,
85605
+ exports.CircuitId.AtomicQuerySigV2,
85606
+ exports.CircuitId.AtomicQueryMTPV2,
85607
+ exports.CircuitId.LinkedMultiQuery10
85608
+ ];
85514
85609
  }
85515
85610
  /**
85516
85611
  * @inheritdoc IAuthHandler#parseAuthorizationRequest
@@ -85545,6 +85640,11 @@ var PolygonIdSdk = (function (exports) {
85545
85640
  did = W$2.parse(authRequest.to);
85546
85641
  }
85547
85642
  const guid = v4();
85643
+ if (!authRequest.from) {
85644
+ throw new Error('auth request should contain from field');
85645
+ }
85646
+ const from = W$2.parse(authRequest.from);
85647
+ const responseScope = await processZeroKnowledgeProofRequests(did, authRequest?.body.scope, from, this._proofService, { ...opts, supportedCircuits: this._supportedCircuits });
85548
85648
  const authResponse = {
85549
85649
  id: guid,
85550
85650
  typ: opts.mediaType,
@@ -85552,64 +85652,11 @@ var PolygonIdSdk = (function (exports) {
85552
85652
  thid: authRequest.thid ?? guid,
85553
85653
  body: {
85554
85654
  message: authRequest?.body?.message,
85555
- scope: []
85655
+ scope: responseScope
85556
85656
  },
85557
85657
  from: did.string(),
85558
85658
  to: authRequest.from
85559
85659
  };
85560
- const requestScope = authRequest.body.scope;
85561
- const combinedQueries = requestScope.reduce((acc, proofReq) => {
85562
- const groupId = proofReq.query.groupId;
85563
- if (!groupId) {
85564
- return acc;
85565
- }
85566
- const existedData = acc.get(groupId);
85567
- if (!existedData) {
85568
- const seed = V$3(12);
85569
- const dataView = new DataView(seed.buffer);
85570
- const linkNonce = dataView.getUint32(0);
85571
- acc.set(groupId, { query: proofReq.query, linkNonce });
85572
- return acc;
85573
- }
85574
- const credentialSubject = mergeObjects(existedData.query.credentialSubject, proofReq.query.credentialSubject);
85575
- acc.set(groupId, {
85576
- ...existedData,
85577
- query: {
85578
- skipClaimRevocationCheck: existedData.query.skipClaimRevocationCheck || proofReq.query.skipClaimRevocationCheck,
85579
- ...existedData.query,
85580
- credentialSubject
85581
- }
85582
- });
85583
- return acc;
85584
- }, new Map());
85585
- const groupedCredentialsCache = new Map();
85586
- for (const proofReq of requestScope) {
85587
- const query = proofReq.query;
85588
- const groupId = query.groupId;
85589
- const combinedQueryData = combinedQueries.get(groupId);
85590
- if (groupId) {
85591
- if (!combinedQueryData) {
85592
- throw new Error(`Invalid group id ${query.groupId}`);
85593
- }
85594
- const combinedQuery = combinedQueryData.query;
85595
- if (!groupedCredentialsCache.has(groupId)) {
85596
- const credWithRevStatus = await this._proofService.findCredentialByProofQuery(did, combinedQueryData.query);
85597
- if (!credWithRevStatus.cred) {
85598
- throw new Error(`Credential not found for query ${JSON.stringify(combinedQuery)}`);
85599
- }
85600
- groupedCredentialsCache.set(groupId, credWithRevStatus);
85601
- }
85602
- }
85603
- const credWithRevStatus = groupedCredentialsCache.get(groupId);
85604
- const zkpRes = await this._proofService.generateProof(proofReq, did, {
85605
- verifierDid: W$2.parse(authRequest.from),
85606
- skipRevocation: Boolean(query.skipClaimRevocationCheck),
85607
- credential: credWithRevStatus?.cred,
85608
- credentialRevocationStatus: credWithRevStatus?.revStatus,
85609
- linkNonce: combinedQueryData?.linkNonce ? BigInt(combinedQueryData.linkNonce) : undefined
85610
- });
85611
- authResponse.body.scope.push(zkpRes);
85612
- }
85613
85660
  const msgBytes = byteEncoder.encode(JSON.stringify(authResponse));
85614
85661
  const packerOpts = opts.mediaType === MediaType.SignedMessage
85615
85662
  ? opts.packerOptions
@@ -85878,7 +85925,7 @@ var PolygonIdSdk = (function (exports) {
85878
85925
  this._packerMgr = _packerMgr;
85879
85926
  this._proofService = _proofService;
85880
85927
  this._zkpVerifier = _zkpVerifier;
85881
- this._allowedCircuits = [
85928
+ this._supportedCircuits = [
85882
85929
  exports.CircuitId.AtomicQueryMTPV2OnChain,
85883
85930
  exports.CircuitId.AtomicQuerySigV2OnChain,
85884
85931
  exports.CircuitId.AtomicQueryV3OnChain
@@ -85915,25 +85962,14 @@ var PolygonIdSdk = (function (exports) {
85915
85962
  if (!opts.ethSigner) {
85916
85963
  throw new Error("Can't sign transaction. Provide Signer in options.");
85917
85964
  }
85918
- const zkRequests = [];
85919
85965
  const { chain_id } = ciRequest.body.transaction_data;
85920
85966
  const networkFlag = Object.keys(u$2).find((key) => u$2[key] === chain_id);
85921
85967
  if (!networkFlag) {
85922
85968
  throw new Error(`Invalid chain id ${chain_id}`);
85923
85969
  }
85924
- for (const proofReq of ciRequest.body.scope) {
85925
- if (!this._allowedCircuits.includes(proofReq.circuitId)) {
85926
- throw new Error(`Can't handle circuit ${proofReq.circuitId}. Only onchain circuits are allowed.`);
85927
- }
85928
- const query = proofReq.query;
85929
- const zkpRes = await this._proofService.generateProof(proofReq, did, {
85930
- skipRevocation: query.skipClaimRevocationCheck ?? false,
85931
- challenge: opts.challenge,
85932
- verifierDid: ciRequest.from ? W$2.parse(ciRequest.from) : undefined
85933
- });
85934
- zkRequests.push(zkpRes);
85935
- }
85936
- return this._zkpVerifier.submitZKPResponse(opts.ethSigner, ciRequest.body.transaction_data, zkRequests);
85970
+ const verifierDid = ciRequest.from ? W$2.parse(ciRequest.from) : undefined;
85971
+ const zkpResponses = await processZeroKnowledgeProofRequests(did, ciRequest?.body?.scope, verifierDid, this._proofService, { ...opts, supportedCircuits: this._supportedCircuits });
85972
+ return this._zkpVerifier.submitZKPResponse(opts.ethSigner, ciRequest.body.transaction_data, zkpResponses);
85937
85973
  }
85938
85974
  }
85939
85975
 
@@ -86002,6 +86038,93 @@ var PolygonIdSdk = (function (exports) {
86002
86038
  }
86003
86039
  }
86004
86040
 
86041
+ /**
86042
+ *
86043
+ * Allows to process RevocationStatusRequest protocol message
86044
+ *
86045
+
86046
+ * @class RevocationStatusHandler
86047
+ * @implements implements IRevocationStatusHandler interface
86048
+ */
86049
+ class RevocationStatusHandler {
86050
+ /**
86051
+ * Creates an instance of RevocationStatusHandler.
86052
+ * @param {IPackageManager} _packerMgr - package manager to unpack message envelope
86053
+ * @param {IIdentityWallet} _identityWallet - identity wallet
86054
+ *
86055
+ */
86056
+ constructor(_packerMgr, _identityWallet) {
86057
+ this._packerMgr = _packerMgr;
86058
+ this._identityWallet = _identityWallet;
86059
+ }
86060
+ /**
86061
+ * @inheritdoc IRevocationStatusHandler#parseRevocationStatusRequest
86062
+ */
86063
+ async parseRevocationStatusRequest(request) {
86064
+ const { unpackedMessage: message } = await this._packerMgr.unpack(request);
86065
+ const ciRequest = message;
86066
+ if (message.type !== PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_REQUEST_MESSAGE_TYPE) {
86067
+ throw new Error('Invalid media type');
86068
+ }
86069
+ return ciRequest;
86070
+ }
86071
+ /**
86072
+ * @inheritdoc IRevocationStatusHandler#handleRevocationStatusRequest
86073
+ */
86074
+ async handleRevocationStatusRequest(did, request, opts) {
86075
+ if (!opts) {
86076
+ opts = {
86077
+ mediaType: MediaType.PlainMessage
86078
+ };
86079
+ }
86080
+ if (opts.mediaType === MediaType.SignedMessage && !opts.packerOptions) {
86081
+ throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
86082
+ }
86083
+ const rsRequest = await this.parseRevocationStatusRequest(request);
86084
+ if (!rsRequest.to) {
86085
+ throw new Error(`failed request. empty 'to' field`);
86086
+ }
86087
+ if (!rsRequest.from) {
86088
+ throw new Error(`failed request. empty 'from' field`);
86089
+ }
86090
+ if (!rsRequest.body?.revocation_nonce) {
86091
+ throw new Error(`failed request. empty 'revocation_nonce' field`);
86092
+ }
86093
+ const issuerDID = W$2.parse(rsRequest.to);
86094
+ const mtpWithTreeState = await this._identityWallet.generateNonRevocationMtpWithNonce(issuerDID, BigInt(rsRequest.body.revocation_nonce), opts?.treeState);
86095
+ const treeState = mtpWithTreeState.treeState;
86096
+ const revStatus = {
86097
+ issuer: {
86098
+ state: treeState?.state.string(),
86099
+ claimsTreeRoot: treeState.claimsRoot.string(),
86100
+ revocationTreeRoot: treeState.revocationRoot.string(),
86101
+ rootOfRoots: treeState.rootOfRoots.string()
86102
+ },
86103
+ mtp: mtpWithTreeState.proof
86104
+ };
86105
+ const packerOpts = opts.mediaType === MediaType.SignedMessage
86106
+ ? opts.packerOptions
86107
+ : {
86108
+ provingMethodAlg: T.provingMethodGroth16AuthV2Instance.methodAlg
86109
+ };
86110
+ const senderDID = W$2.parse(rsRequest.to);
86111
+ const guid = v4();
86112
+ const response = {
86113
+ id: guid,
86114
+ typ: MediaType.PlainMessage,
86115
+ type: PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE,
86116
+ thid: rsRequest.thid ?? guid,
86117
+ body: revStatus,
86118
+ from: did.string(),
86119
+ to: rsRequest.from
86120
+ };
86121
+ return this._packerMgr.pack(opts.mediaType, byteEncoder.encode(JSON.stringify(response)), {
86122
+ senderDID,
86123
+ ...packerOpts
86124
+ });
86125
+ }
86126
+ }
86127
+
86005
86128
  exports.AbstractPrivateKeyStore = AbstractPrivateKeyStore;
86006
86129
  exports.AgentResolver = AgentResolver;
86007
86130
  exports.AtomicQueryMTPV2Inputs = AtomicQueryMTPV2Inputs;
@@ -86084,6 +86207,7 @@ var PolygonIdSdk = (function (exports) {
86084
86207
  exports.QueryOperators = QueryOperators;
86085
86208
  exports.RHSResolver = RHSResolver;
86086
86209
  exports.RefreshHandler = RefreshHandler;
86210
+ exports.RevocationStatusHandler = RevocationStatusHandler;
86087
86211
  exports.SDK_EVENTS = SDK_EVENTS;
86088
86212
  exports.Scalar = Scalar$1;
86089
86213
  exports.Sec256k1Provider = Sec256k1Provider;
@@ -86168,6 +86292,7 @@ var PolygonIdSdk = (function (exports) {
86168
86292
  exports.parseSerializationAttr = parseSerializationAttr;
86169
86293
  exports.prepareCircuitArrayValues = prepareCircuitArrayValues;
86170
86294
  exports.prepareSiblingsStr = prepareSiblingsStr;
86295
+ exports.processZeroKnowledgeProofRequests = processZeroKnowledgeProofRequests;
86171
86296
  exports.pushHashesToRHS = pushHashesToRHS;
86172
86297
  exports.resolveDIDDocumentAuth = resolveDIDDocumentAuth;
86173
86298
  exports.resolvePath = resolvePath;