@0xpolygonid/js-sdk 1.9.0 → 1.9.1

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.
@@ -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();
@@ -85457,6 +85461,86 @@ var PolygonIdSdk = (function (exports) {
85457
85461
  }
85458
85462
  }
85459
85463
 
85464
+ /**
85465
+ * Groups the ZeroKnowledgeProofRequest objects based on their groupId.
85466
+ * Returns a Map where the key is the groupId and the value is an object containing the query and linkNonce.
85467
+ *
85468
+ * @param requestScope - An array of ZeroKnowledgeProofRequest objects.
85469
+ * @returns A Map<number, { query: JSONObject; linkNonce: number }> representing the grouped queries.
85470
+ */
85471
+ const getGroupedQueries = (requestScope) => requestScope.reduce((acc, proofReq) => {
85472
+ const groupId = proofReq.query.groupId;
85473
+ if (!groupId) {
85474
+ return acc;
85475
+ }
85476
+ const existedData = acc.get(groupId);
85477
+ if (!existedData) {
85478
+ const seed = V$3(12);
85479
+ const dataView = new DataView(seed.buffer);
85480
+ const linkNonce = dataView.getUint32(0);
85481
+ acc.set(groupId, { query: proofReq.query, linkNonce });
85482
+ return acc;
85483
+ }
85484
+ const credentialSubject = mergeObjects(existedData.query.credentialSubject, proofReq.query.credentialSubject);
85485
+ acc.set(groupId, {
85486
+ ...existedData,
85487
+ query: {
85488
+ skipClaimRevocationCheck: existedData.query.skipClaimRevocationCheck || proofReq.query.skipClaimRevocationCheck,
85489
+ ...existedData.query,
85490
+ credentialSubject
85491
+ }
85492
+ });
85493
+ return acc;
85494
+ }, new Map());
85495
+ /**
85496
+ * Processes zero knowledge proof requests.
85497
+ *
85498
+ * @param senderIdentifier - The identifier of the sender.
85499
+ * @param requests - An array of zero knowledge proof requests.
85500
+ * @param from - The identifier of the sender.
85501
+ * @param proofService - The proof service.
85502
+ * @param opts - Additional options for processing the requests.
85503
+ * @returns A promise that resolves to an array of zero knowledge proof responses.
85504
+ */
85505
+ const processZeroKnowledgeProofRequests = async (senderIdentifier, requests, from, proofService, opts) => {
85506
+ const requestScope = requests ?? [];
85507
+ const combinedQueries = getGroupedQueries(requestScope);
85508
+ const groupedCredentialsCache = new Map();
85509
+ const zkpResponses = [];
85510
+ for (const proofReq of requestScope) {
85511
+ if (!opts.supportedCircuits.includes(proofReq.circuitId)) {
85512
+ throw new Error(`Circuit ${proofReq.circuitId} is not allowed`);
85513
+ }
85514
+ const query = proofReq.query;
85515
+ const groupId = query.groupId;
85516
+ const combinedQueryData = combinedQueries.get(groupId);
85517
+ if (groupId) {
85518
+ if (!combinedQueryData) {
85519
+ throw new Error(`Invalid group id ${query.groupId}`);
85520
+ }
85521
+ const combinedQuery = combinedQueryData.query;
85522
+ if (!groupedCredentialsCache.has(groupId)) {
85523
+ const credWithRevStatus = await proofService.findCredentialByProofQuery(senderIdentifier, combinedQueryData.query);
85524
+ if (!credWithRevStatus.cred) {
85525
+ throw new Error(`Credential not found for query ${JSON.stringify(combinedQuery)}`);
85526
+ }
85527
+ groupedCredentialsCache.set(groupId, credWithRevStatus);
85528
+ }
85529
+ }
85530
+ const credWithRevStatus = groupedCredentialsCache.get(groupId);
85531
+ const zkpRes = await proofService.generateProof(proofReq, senderIdentifier, {
85532
+ verifierDid: from,
85533
+ challenge: opts.challenge,
85534
+ skipRevocation: Boolean(query.skipClaimRevocationCheck),
85535
+ credential: credWithRevStatus?.cred,
85536
+ credentialRevocationStatus: credWithRevStatus?.revStatus,
85537
+ linkNonce: combinedQueryData?.linkNonce ? BigInt(combinedQueryData.linkNonce) : undefined
85538
+ });
85539
+ zkpResponses.push(zkpRes);
85540
+ }
85541
+ return zkpResponses;
85542
+ };
85543
+
85460
85544
  /**
85461
85545
  * createAuthorizationRequest is a function to create protocol authorization request
85462
85546
  * @param {string} reason - reason to request proof
@@ -85511,6 +85595,12 @@ var PolygonIdSdk = (function (exports) {
85511
85595
  constructor(_packerMgr, _proofService) {
85512
85596
  this._packerMgr = _packerMgr;
85513
85597
  this._proofService = _proofService;
85598
+ this._supportedCircuits = [
85599
+ exports.CircuitId.AtomicQueryV3,
85600
+ exports.CircuitId.AtomicQuerySigV2,
85601
+ exports.CircuitId.AtomicQueryMTPV2,
85602
+ exports.CircuitId.LinkedMultiQuery10
85603
+ ];
85514
85604
  }
85515
85605
  /**
85516
85606
  * @inheritdoc IAuthHandler#parseAuthorizationRequest
@@ -85545,6 +85635,11 @@ var PolygonIdSdk = (function (exports) {
85545
85635
  did = W$2.parse(authRequest.to);
85546
85636
  }
85547
85637
  const guid = v4();
85638
+ if (!authRequest.from) {
85639
+ throw new Error('auth request should contain from field');
85640
+ }
85641
+ const from = W$2.parse(authRequest.from);
85642
+ const responseScope = await processZeroKnowledgeProofRequests(did, authRequest?.body.scope, from, this._proofService, { ...opts, supportedCircuits: this._supportedCircuits });
85548
85643
  const authResponse = {
85549
85644
  id: guid,
85550
85645
  typ: opts.mediaType,
@@ -85552,64 +85647,11 @@ var PolygonIdSdk = (function (exports) {
85552
85647
  thid: authRequest.thid ?? guid,
85553
85648
  body: {
85554
85649
  message: authRequest?.body?.message,
85555
- scope: []
85650
+ scope: responseScope
85556
85651
  },
85557
85652
  from: did.string(),
85558
85653
  to: authRequest.from
85559
85654
  };
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
85655
  const msgBytes = byteEncoder.encode(JSON.stringify(authResponse));
85614
85656
  const packerOpts = opts.mediaType === MediaType.SignedMessage
85615
85657
  ? opts.packerOptions
@@ -85878,7 +85920,7 @@ var PolygonIdSdk = (function (exports) {
85878
85920
  this._packerMgr = _packerMgr;
85879
85921
  this._proofService = _proofService;
85880
85922
  this._zkpVerifier = _zkpVerifier;
85881
- this._allowedCircuits = [
85923
+ this._supportedCircuits = [
85882
85924
  exports.CircuitId.AtomicQueryMTPV2OnChain,
85883
85925
  exports.CircuitId.AtomicQuerySigV2OnChain,
85884
85926
  exports.CircuitId.AtomicQueryV3OnChain
@@ -85915,25 +85957,14 @@ var PolygonIdSdk = (function (exports) {
85915
85957
  if (!opts.ethSigner) {
85916
85958
  throw new Error("Can't sign transaction. Provide Signer in options.");
85917
85959
  }
85918
- const zkRequests = [];
85919
85960
  const { chain_id } = ciRequest.body.transaction_data;
85920
85961
  const networkFlag = Object.keys(u$2).find((key) => u$2[key] === chain_id);
85921
85962
  if (!networkFlag) {
85922
85963
  throw new Error(`Invalid chain id ${chain_id}`);
85923
85964
  }
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);
85965
+ const verifierDid = ciRequest.from ? W$2.parse(ciRequest.from) : undefined;
85966
+ const zkpResponses = await processZeroKnowledgeProofRequests(did, ciRequest?.body?.scope, verifierDid, this._proofService, { ...opts, supportedCircuits: this._supportedCircuits });
85967
+ return this._zkpVerifier.submitZKPResponse(opts.ethSigner, ciRequest.body.transaction_data, zkpResponses);
85937
85968
  }
85938
85969
  }
85939
85970
 
@@ -86002,6 +86033,93 @@ var PolygonIdSdk = (function (exports) {
86002
86033
  }
86003
86034
  }
86004
86035
 
86036
+ /**
86037
+ *
86038
+ * Allows to process RevocationStatusRequest protocol message
86039
+ *
86040
+
86041
+ * @class RevocationStatusHandler
86042
+ * @implements implements IRevocationStatusHandler interface
86043
+ */
86044
+ class RevocationStatusHandler {
86045
+ /**
86046
+ * Creates an instance of RevocationStatusHandler.
86047
+ * @param {IPackageManager} _packerMgr - package manager to unpack message envelope
86048
+ * @param {IIdentityWallet} _identityWallet - identity wallet
86049
+ *
86050
+ */
86051
+ constructor(_packerMgr, _identityWallet) {
86052
+ this._packerMgr = _packerMgr;
86053
+ this._identityWallet = _identityWallet;
86054
+ }
86055
+ /**
86056
+ * @inheritdoc IRevocationStatusHandler#parseRevocationStatusRequest
86057
+ */
86058
+ async parseRevocationStatusRequest(request) {
86059
+ const { unpackedMessage: message } = await this._packerMgr.unpack(request);
86060
+ const ciRequest = message;
86061
+ if (message.type !== PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_REQUEST_MESSAGE_TYPE) {
86062
+ throw new Error('Invalid media type');
86063
+ }
86064
+ return ciRequest;
86065
+ }
86066
+ /**
86067
+ * @inheritdoc IRevocationStatusHandler#handleRevocationStatusRequest
86068
+ */
86069
+ async handleRevocationStatusRequest(did, request, opts) {
86070
+ if (!opts) {
86071
+ opts = {
86072
+ mediaType: MediaType.PlainMessage
86073
+ };
86074
+ }
86075
+ if (opts.mediaType === MediaType.SignedMessage && !opts.packerOptions) {
86076
+ throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
86077
+ }
86078
+ const rsRequest = await this.parseRevocationStatusRequest(request);
86079
+ if (!rsRequest.to) {
86080
+ throw new Error(`failed request. empty 'to' field`);
86081
+ }
86082
+ if (!rsRequest.from) {
86083
+ throw new Error(`failed request. empty 'from' field`);
86084
+ }
86085
+ if (!rsRequest.body?.revocation_nonce) {
86086
+ throw new Error(`failed request. empty 'revocation_nonce' field`);
86087
+ }
86088
+ const issuerDID = W$2.parse(rsRequest.to);
86089
+ const mtpWithTreeState = await this._identityWallet.generateNonRevocationMtpWithNonce(issuerDID, BigInt(rsRequest.body.revocation_nonce), opts?.treeState);
86090
+ const treeState = mtpWithTreeState.treeState;
86091
+ const revStatus = {
86092
+ issuer: {
86093
+ state: treeState?.state.string(),
86094
+ claimsTreeRoot: treeState.claimsRoot.string(),
86095
+ revocationTreeRoot: treeState.revocationRoot.string(),
86096
+ rootOfRoots: treeState.rootOfRoots.string()
86097
+ },
86098
+ mtp: mtpWithTreeState.proof
86099
+ };
86100
+ const packerOpts = opts.mediaType === MediaType.SignedMessage
86101
+ ? opts.packerOptions
86102
+ : {
86103
+ provingMethodAlg: T.provingMethodGroth16AuthV2Instance.methodAlg
86104
+ };
86105
+ const senderDID = W$2.parse(rsRequest.to);
86106
+ const guid = v4();
86107
+ const response = {
86108
+ id: guid,
86109
+ typ: MediaType.PlainMessage,
86110
+ type: PROTOCOL_MESSAGE_TYPE.REVOCATION_STATUS_RESPONSE_MESSAGE_TYPE,
86111
+ thid: rsRequest.thid ?? guid,
86112
+ body: revStatus,
86113
+ from: did.string(),
86114
+ to: rsRequest.from
86115
+ };
86116
+ return this._packerMgr.pack(opts.mediaType, byteEncoder.encode(JSON.stringify(response)), {
86117
+ senderDID,
86118
+ ...packerOpts
86119
+ });
86120
+ }
86121
+ }
86122
+
86005
86123
  exports.AbstractPrivateKeyStore = AbstractPrivateKeyStore;
86006
86124
  exports.AgentResolver = AgentResolver;
86007
86125
  exports.AtomicQueryMTPV2Inputs = AtomicQueryMTPV2Inputs;
@@ -86084,6 +86202,7 @@ var PolygonIdSdk = (function (exports) {
86084
86202
  exports.QueryOperators = QueryOperators;
86085
86203
  exports.RHSResolver = RHSResolver;
86086
86204
  exports.RefreshHandler = RefreshHandler;
86205
+ exports.RevocationStatusHandler = RevocationStatusHandler;
86087
86206
  exports.SDK_EVENTS = SDK_EVENTS;
86088
86207
  exports.Scalar = Scalar$1;
86089
86208
  exports.Sec256k1Provider = Sec256k1Provider;
@@ -86168,6 +86287,7 @@ var PolygonIdSdk = (function (exports) {
86168
86287
  exports.parseSerializationAttr = parseSerializationAttr;
86169
86288
  exports.prepareCircuitArrayValues = prepareCircuitArrayValues;
86170
86289
  exports.prepareSiblingsStr = prepareSiblingsStr;
86290
+ exports.processZeroKnowledgeProofRequests = processZeroKnowledgeProofRequests;
86171
86291
  exports.pushHashesToRHS = pushHashesToRHS;
86172
86292
  exports.resolveDIDDocumentAuth = resolveDIDDocumentAuth;
86173
86293
  exports.resolvePath = resolvePath;