@0xpolygonid/js-sdk 1.5.1 → 1.6.0

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 (83) hide show
  1. package/dist/browser/esm/index.js +1685 -468
  2. package/dist/browser/esm/index.js.map +1 -1
  3. package/dist/browser/umd/index.js +1703 -470
  4. package/dist/browser/umd/index.js.map +1 -1
  5. package/dist/node/cjs/circuits/atomic-query-mtp-v2-on-chain.js +1 -1
  6. package/dist/node/cjs/circuits/atomic-query-mtp-v2.js +1 -1
  7. package/dist/node/cjs/circuits/atomic-query-sig-v2-on-chain.js +2 -2
  8. package/dist/node/cjs/circuits/atomic-query-sig-v2.js +11 -14
  9. package/dist/node/cjs/circuits/atomic-query-v3-on-chain.js +320 -0
  10. package/dist/node/cjs/circuits/atomic-query-v3.js +275 -0
  11. package/dist/node/cjs/circuits/common.js +5 -7
  12. package/dist/node/cjs/circuits/comparer.js +57 -2
  13. package/dist/node/cjs/circuits/index.js +4 -0
  14. package/dist/node/cjs/circuits/linked-multi-query.js +128 -0
  15. package/dist/node/cjs/circuits/linked-nullifier.js +57 -0
  16. package/dist/node/cjs/circuits/models.js +25 -1
  17. package/dist/node/cjs/credentials/credential-wallet.js +2 -10
  18. package/dist/node/cjs/credentials/index.js +1 -0
  19. package/dist/node/cjs/credentials/utils.js +23 -0
  20. package/dist/node/cjs/iden3comm/handlers/auth.js +49 -8
  21. package/dist/node/cjs/iden3comm/handlers/contract-request.js +20 -8
  22. package/dist/node/cjs/proof/common.js +104 -1
  23. package/dist/node/cjs/proof/inputs-generator.js +393 -0
  24. package/dist/node/cjs/proof/proof-service.js +105 -372
  25. package/dist/node/cjs/schema-processor/json/parser.js +7 -0
  26. package/dist/node/cjs/schema-processor/json/validator.js +1 -0
  27. package/dist/node/cjs/storage/filters/jsonQuery.js +73 -8
  28. package/dist/node/cjs/utils/did-helper.js +14 -0
  29. package/dist/node/cjs/utils/index.js +2 -0
  30. package/dist/node/cjs/utils/object.js +42 -0
  31. package/dist/node/cjs/verifiable/presentation.js +34 -33
  32. package/dist/node/cjs/verifiable/proof.js +2 -0
  33. package/dist/node/esm/circuits/atomic-query-mtp-v2-on-chain.js +1 -1
  34. package/dist/node/esm/circuits/atomic-query-mtp-v2.js +1 -1
  35. package/dist/node/esm/circuits/atomic-query-sig-v2-on-chain.js +2 -2
  36. package/dist/node/esm/circuits/atomic-query-sig-v2.js +11 -14
  37. package/dist/node/esm/circuits/atomic-query-v3-on-chain.js +315 -0
  38. package/dist/node/esm/circuits/atomic-query-v3.js +270 -0
  39. package/dist/node/esm/circuits/common.js +4 -6
  40. package/dist/node/esm/circuits/comparer.js +55 -1
  41. package/dist/node/esm/circuits/index.js +4 -0
  42. package/dist/node/esm/circuits/linked-multi-query.js +123 -0
  43. package/dist/node/esm/circuits/linked-nullifier.js +52 -0
  44. package/dist/node/esm/circuits/models.js +25 -1
  45. package/dist/node/esm/credentials/credential-wallet.js +2 -10
  46. package/dist/node/esm/credentials/index.js +1 -0
  47. package/dist/node/esm/credentials/utils.js +19 -0
  48. package/dist/node/esm/iden3comm/handlers/auth.js +50 -9
  49. package/dist/node/esm/iden3comm/handlers/contract-request.js +20 -8
  50. package/dist/node/esm/proof/common.js +101 -2
  51. package/dist/node/esm/proof/inputs-generator.js +389 -0
  52. package/dist/node/esm/proof/proof-service.js +111 -377
  53. package/dist/node/esm/schema-processor/json/parser.js +7 -0
  54. package/dist/node/esm/schema-processor/json/validator.js +1 -0
  55. package/dist/node/esm/storage/filters/jsonQuery.js +74 -9
  56. package/dist/node/esm/utils/did-helper.js +10 -0
  57. package/dist/node/esm/utils/index.js +2 -0
  58. package/dist/node/esm/utils/object.js +38 -0
  59. package/dist/node/esm/verifiable/presentation.js +30 -29
  60. package/dist/node/esm/verifiable/proof.js +2 -0
  61. package/dist/types/circuits/atomic-query-mtp-v2.d.ts +13 -13
  62. package/dist/types/circuits/atomic-query-v3-on-chain.d.ts +142 -0
  63. package/dist/types/circuits/atomic-query-v3.d.ts +120 -0
  64. package/dist/types/circuits/common.d.ts +4 -4
  65. package/dist/types/circuits/comparer.d.ts +33 -1
  66. package/dist/types/circuits/index.d.ts +4 -0
  67. package/dist/types/circuits/linked-multi-query.d.ts +38 -0
  68. package/dist/types/circuits/linked-nullifier.d.ts +37 -0
  69. package/dist/types/circuits/models.d.ts +20 -2
  70. package/dist/types/credentials/index.d.ts +1 -0
  71. package/dist/types/credentials/utils.d.ts +12 -0
  72. package/dist/types/iden3comm/types/packer.d.ts +1 -2
  73. package/dist/types/iden3comm/types/protocol/auth.d.ts +1 -0
  74. package/dist/types/proof/common.d.ts +34 -3
  75. package/dist/types/proof/inputs-generator.d.ts +46 -0
  76. package/dist/types/proof/proof-service.d.ts +14 -25
  77. package/dist/types/schema-processor/json/parser.d.ts +1 -0
  78. package/dist/types/storage/filters/jsonQuery.d.ts +1 -1
  79. package/dist/types/utils/did-helper.d.ts +6 -0
  80. package/dist/types/utils/index.d.ts +2 -0
  81. package/dist/types/utils/object.d.ts +12 -0
  82. package/dist/types/verifiable/presentation.d.ts +6 -10
  83. package/package.json +1 -1
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getUserDIDFromCredential = void 0;
4
+ const js_iden3_core_1 = require("@iden3/js-iden3-core");
5
+ /**
6
+ * Retrieves the user DID from a given credential.
7
+ * If the credential does not have a credentialSubject.id property, the issuer DID is returned.
8
+ * If the credentialSubject.id is not a string, an error is thrown.
9
+ * @param issuerDID The DID of the issuer.
10
+ * @param credential The credential object.
11
+ * @returns The user DID parsed from the credential.
12
+ * @throws Error if the credentialSubject.id is not a string.
13
+ */
14
+ const getUserDIDFromCredential = (issuerDID, credential) => {
15
+ if (!credential.credentialSubject.id) {
16
+ return issuerDID;
17
+ }
18
+ if (typeof credential.credentialSubject.id !== 'string') {
19
+ throw new Error('credential subject `id` is not a string');
20
+ }
21
+ return js_iden3_core_1.DID.parse(credential.credentialSubject.id);
22
+ };
23
+ exports.getUserDIDFromCredential = getUserDIDFromCredential;
@@ -30,6 +30,7 @@ const js_iden3_core_1 = require("@iden3/js-iden3-core");
30
30
  const js_jwz_1 = require("@iden3/js-jwz");
31
31
  const uuid = __importStar(require("uuid"));
32
32
  const utils_1 = require("../../utils");
33
+ const js_crypto_1 = require("@iden3/js-crypto");
33
34
  /**
34
35
  *
35
36
  * Allows to process AuthorizationRequest protocol message and produce JWZ response.
@@ -105,15 +106,55 @@ class AuthHandler {
105
106
  from: did.string(),
106
107
  to: authRequest.from
107
108
  };
108
- for (const proofReq of authRequest.body.scope) {
109
- const zkpReq = {
110
- id: proofReq.id,
111
- circuitId: proofReq.circuitId,
112
- query: proofReq.query
113
- };
109
+ const requestScope = authRequest.body.scope;
110
+ const combinedQueries = requestScope.reduce((acc, proofReq) => {
111
+ const groupId = proofReq.query.groupId;
112
+ if (!groupId) {
113
+ return acc;
114
+ }
115
+ const existedData = acc.get(groupId);
116
+ if (!existedData) {
117
+ const seed = (0, js_crypto_1.getRandomBytes)(12);
118
+ const dataView = new DataView(seed.buffer);
119
+ const linkNonce = dataView.getUint32(0);
120
+ acc.set(groupId, { query: proofReq.query, linkNonce });
121
+ return acc;
122
+ }
123
+ const credentialSubject = (0, utils_1.mergeObjects)(existedData.query.credentialSubject, proofReq.query.credentialSubject);
124
+ acc.set(groupId, {
125
+ ...existedData,
126
+ query: {
127
+ skipClaimRevocationCheck: existedData.query.skipClaimRevocationCheck || proofReq.query.skipClaimRevocationCheck,
128
+ ...existedData.query,
129
+ credentialSubject
130
+ }
131
+ });
132
+ return acc;
133
+ }, new Map());
134
+ const groupedCredentialsCache = new Map();
135
+ for (const proofReq of requestScope) {
114
136
  const query = proofReq.query;
115
- const zkpRes = await this._proofService.generateProof(zkpReq, did, {
116
- skipRevocation: query.skipClaimRevocationCheck ?? false
137
+ const groupId = query.groupId;
138
+ const combinedQueryData = combinedQueries.get(groupId);
139
+ if (groupId) {
140
+ if (!combinedQueryData) {
141
+ throw new Error(`Invalid group id ${query.groupId}`);
142
+ }
143
+ const combinedQuery = combinedQueryData.query;
144
+ if (!groupedCredentialsCache.has(groupId)) {
145
+ const credWithRevStatus = await this._proofService.findCredentialByProofQuery(did, combinedQueryData.query);
146
+ if (!credWithRevStatus.cred) {
147
+ throw new Error(`Credential not found for query ${JSON.stringify(combinedQuery)}`);
148
+ }
149
+ groupedCredentialsCache.set(groupId, credWithRevStatus);
150
+ }
151
+ }
152
+ const credWithRevStatus = groupedCredentialsCache.get(groupId);
153
+ const zkpRes = await this._proofService.generateProof(proofReq, did, {
154
+ skipRevocation: Boolean(query.skipClaimRevocationCheck),
155
+ credential: credWithRevStatus?.cred,
156
+ credentialRevocationStatus: credWithRevStatus?.revStatus,
157
+ linkNonce: combinedQueryData?.linkNonce ? BigInt(combinedQueryData.linkNonce) : undefined
117
158
  });
118
159
  authResponse.body.scope.push(zkpRes);
119
160
  }
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ContractRequestHandler = void 0;
4
4
  const models_1 = require("../../circuits/models");
5
5
  const constants_1 = require("../constants");
6
+ const js_iden3_core_1 = require("@iden3/js-iden3-core");
7
+ const utils_1 = require("../../utils");
6
8
  /**
7
9
  *
8
10
  * Allows to process ContractInvokeRequest protocol message
@@ -26,7 +28,8 @@ class ContractRequestHandler {
26
28
  this._zkpVerifier = _zkpVerifier;
27
29
  this._allowedCircuits = [
28
30
  models_1.CircuitId.AtomicQueryMTPV2OnChain,
29
- models_1.CircuitId.AtomicQuerySigV2OnChain
31
+ models_1.CircuitId.AtomicQuerySigV2OnChain,
32
+ models_1.CircuitId.AtomicQueryV3OnChain
30
33
  ];
31
34
  }
32
35
  /**
@@ -60,19 +63,28 @@ class ContractRequestHandler {
60
63
  throw new Error("Can't sign transaction. Provide Signer in options.");
61
64
  }
62
65
  const zkRequests = [];
66
+ const { contract_address, chain_id } = ciRequest.body.transaction_data;
67
+ const networkFlag = Object.keys(js_iden3_core_1.ChainIds).find((key) => js_iden3_core_1.ChainIds[key] === chain_id);
68
+ if (!networkFlag) {
69
+ throw new Error(`Invalid chain id ${chain_id}`);
70
+ }
71
+ const [blockchain, networkId] = networkFlag.split(':');
72
+ const verifierId = (0, utils_1.buildVerifierId)(contract_address, {
73
+ blockchain,
74
+ networkId,
75
+ // DidMethod.Iden3 is used based on discussions: all onchain issuers have iden3 did method by default. This can be changed in the release of v3 circuit.
76
+ method: js_iden3_core_1.DidMethod.Iden3
77
+ });
78
+ const verifierDid = js_iden3_core_1.DID.parseFromId(verifierId);
63
79
  for (const proofReq of ciRequest.body.scope) {
64
80
  if (!this._allowedCircuits.includes(proofReq.circuitId)) {
65
81
  throw new Error(`Can't handle circuit ${proofReq.circuitId}. Only onchain circuits are allowed.`);
66
82
  }
67
- const zkpReq = {
68
- id: proofReq.id,
69
- circuitId: proofReq.circuitId,
70
- query: proofReq.query
71
- };
72
83
  const query = proofReq.query;
73
- const zkpRes = await this._proofService.generateProof(zkpReq, did, {
84
+ const zkpRes = await this._proofService.generateProof(proofReq, did, {
74
85
  skipRevocation: query.skipClaimRevocationCheck ?? false,
75
- challenge: opts.challenge
86
+ challenge: opts.challenge,
87
+ verifierDid
76
88
  });
77
89
  zkRequests.push(zkpRes);
78
90
  }
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toGISTProof = exports.toClaimNonRevStatus = void 0;
3
+ exports.transformQueryValueToBigInts = exports.parseQueriesMetadata = exports.parseQueryMetadata = exports.parseCredentialSubject = exports.toGISTProof = exports.toClaimNonRevStatus = void 0;
4
4
  const js_merkletree_1 = require("@iden3/js-merkletree");
5
5
  const circuits_1 = require("../circuits");
6
+ const verifiable_1 = require("../verifiable");
7
+ const js_jsonld_merklization_1 = require("@iden3/js-jsonld-merklization");
8
+ const json_1 = require("../schema-processor/json");
9
+ const utils_1 = require("../utils");
10
+ const js_crypto_1 = require("@iden3/js-crypto");
6
11
  /**
7
12
  * converts verifiable RevocationStatus model to circuits structure
8
13
  *
@@ -10,6 +15,18 @@ const circuits_1 = require("../circuits");
10
15
  * @returns {ClaimNonRevStatus}
11
16
  */
12
17
  const toClaimNonRevStatus = (s) => {
18
+ if (!s) {
19
+ const hash = js_crypto_1.poseidon.hash(new Array(3).fill(0n));
20
+ return {
21
+ proof: new js_merkletree_1.Proof(),
22
+ treeState: {
23
+ state: js_merkletree_1.Hash.fromBigInt(hash),
24
+ claimsRoot: js_merkletree_1.ZERO_HASH,
25
+ revocationRoot: js_merkletree_1.ZERO_HASH,
26
+ rootOfRoots: js_merkletree_1.ZERO_HASH
27
+ }
28
+ };
29
+ }
13
30
  return {
14
31
  proof: s.mtp,
15
32
  treeState: (0, circuits_1.buildTreeState)(s.issuer.state, s.issuer.claimsTreeRoot, s.issuer.revocationTreeRoot, s.issuer.rootOfRoots)
@@ -45,3 +62,89 @@ const toGISTProof = (smtProof) => {
45
62
  };
46
63
  };
47
64
  exports.toGISTProof = toGISTProof;
65
+ const parseCredentialSubject = (credentialSubject) => {
66
+ // credentialSubject is empty
67
+ if (!credentialSubject) {
68
+ return [{ operator: circuits_1.QueryOperators.$eq, fieldName: '' }];
69
+ }
70
+ const queries = [];
71
+ const entries = Object.entries(credentialSubject);
72
+ if (!entries.length) {
73
+ throw new Error(`query must have at least 1 predicate`);
74
+ }
75
+ for (const [fieldName, fieldReq] of entries) {
76
+ const fieldReqEntries = Object.entries(fieldReq);
77
+ const isSelectiveDisclosure = fieldReqEntries.length === 0;
78
+ if (isSelectiveDisclosure) {
79
+ queries.push({ operator: circuits_1.QueryOperators.$sd, fieldName: fieldName });
80
+ continue;
81
+ }
82
+ for (const [operatorName, operatorValue] of fieldReqEntries) {
83
+ if (!circuits_1.QueryOperators[operatorName]) {
84
+ throw new Error(`operator is not supported by lib`);
85
+ }
86
+ const operator = circuits_1.QueryOperators[operatorName];
87
+ queries.push({ operator, fieldName, operatorValue });
88
+ }
89
+ }
90
+ return queries;
91
+ };
92
+ exports.parseCredentialSubject = parseCredentialSubject;
93
+ const parseQueryMetadata = async (propertyQuery, ldContextJSON, credentialType, options) => {
94
+ const datatype = await js_jsonld_merklization_1.Path.newTypeFromContext(ldContextJSON, `${credentialType}.${propertyQuery.fieldName}`, options);
95
+ const query = {
96
+ ...propertyQuery,
97
+ slotIndex: 0,
98
+ merklizedSchema: false,
99
+ datatype: datatype,
100
+ claimPathKey: BigInt(0),
101
+ values: [],
102
+ path: new js_jsonld_merklization_1.Path()
103
+ };
104
+ const serAttr = await json_1.Parser.getSerializationAttrFromContext(JSON.parse(ldContextJSON), options, credentialType);
105
+ if (!serAttr) {
106
+ query.merklizedSchema = true;
107
+ }
108
+ // for merklized credentials slotIndex in query must be equal to zero
109
+ // and not a position of merklization root.
110
+ // it has no influence on check in the off-chain circuits, but it aligns with onchain verification standard
111
+ if (!query.merklizedSchema) {
112
+ query.slotIndex = await json_1.Parser.getFieldSlotIndex(propertyQuery.fieldName, credentialType, utils_1.byteEncoder.encode(ldContextJSON));
113
+ }
114
+ else {
115
+ try {
116
+ const path = await (0, verifiable_1.buildFieldPath)(ldContextJSON, credentialType, propertyQuery.fieldName, options);
117
+ query.claimPathKey = await path.mtEntry();
118
+ query.path = path;
119
+ }
120
+ catch (e) {
121
+ throw new Error(`field does not exist in the schema ${e.message}`);
122
+ }
123
+ }
124
+ if (propertyQuery.operatorValue) {
125
+ if (!(0, circuits_1.isValidOperation)(datatype, propertyQuery.operator)) {
126
+ throw new Error(`operator ${propertyQuery.operator} is not supported for datatype ${datatype}`);
127
+ }
128
+ query.values = await (0, exports.transformQueryValueToBigInts)(propertyQuery.operatorValue, datatype);
129
+ }
130
+ return query;
131
+ };
132
+ exports.parseQueryMetadata = parseQueryMetadata;
133
+ const parseQueriesMetadata = async (credentialType, ldContextJSON, credentialSubject, options) => {
134
+ const queriesMetadata = (0, exports.parseCredentialSubject)(credentialSubject);
135
+ return Promise.all(queriesMetadata.map((m) => (0, exports.parseQueryMetadata)(m, ldContextJSON, credentialType, options)));
136
+ };
137
+ exports.parseQueriesMetadata = parseQueriesMetadata;
138
+ const transformQueryValueToBigInts = async (value, ldType) => {
139
+ const values = new Array(64).fill(BigInt(0));
140
+ if (Array.isArray(value)) {
141
+ for (let index = 0; index < value.length; index++) {
142
+ values[index] = await js_jsonld_merklization_1.Merklizer.hashValue(ldType, value[index]);
143
+ }
144
+ }
145
+ else {
146
+ values[0] = await js_jsonld_merklization_1.Merklizer.hashValue(ldType, value);
147
+ }
148
+ return values;
149
+ };
150
+ exports.transformQueryValueToBigInts = transformQueryValueToBigInts;
@@ -0,0 +1,393 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InputGenerator = void 0;
4
+ const js_iden3_core_1 = require("@iden3/js-iden3-core");
5
+ const verifiable_1 = require("../verifiable");
6
+ const circuits_1 = require("../circuits");
7
+ const common_1 = require("./common");
8
+ const credentials_1 = require("../credentials");
9
+ const circuitValidator = {
10
+ [circuits_1.CircuitId.AtomicQueryMTPV2]: { maxQueriesCount: 1 },
11
+ [circuits_1.CircuitId.AtomicQueryMTPV2OnChain]: { maxQueriesCount: 1 },
12
+ [circuits_1.CircuitId.AtomicQuerySigV2]: { maxQueriesCount: 1 },
13
+ [circuits_1.CircuitId.AtomicQuerySigV2OnChain]: { maxQueriesCount: 1 },
14
+ [circuits_1.CircuitId.AtomicQueryV3]: { maxQueriesCount: 1 },
15
+ [circuits_1.CircuitId.AtomicQueryV3OnChain]: { maxQueriesCount: 1 },
16
+ [circuits_1.CircuitId.AuthV2]: { maxQueriesCount: 0 },
17
+ [circuits_1.CircuitId.StateTransition]: { maxQueriesCount: 0 },
18
+ [circuits_1.CircuitId.LinkedNullifier]: { maxQueriesCount: 1 },
19
+ [circuits_1.CircuitId.LinkedMultiQuery10]: { maxQueriesCount: 10 }
20
+ };
21
+ class InputGenerator {
22
+ constructor(_identityWallet, _credentialWallet, _stateStorage) {
23
+ this._identityWallet = _identityWallet;
24
+ this._credentialWallet = _credentialWallet;
25
+ this._stateStorage = _stateStorage;
26
+ this.credentialAtomicQueryMTPV2PrepareInputs = async ({ preparedCredential, identifier, proofReq, params, circuitQueries }) => {
27
+ const circuitClaimData = await this.newCircuitClaimData(preparedCredential);
28
+ circuitClaimData.nonRevProof = (0, common_1.toClaimNonRevStatus)(preparedCredential.revStatus);
29
+ const circuitInputs = new circuits_1.AtomicQueryMTPV2Inputs();
30
+ circuitInputs.id = js_iden3_core_1.DID.idFromDID(identifier);
31
+ circuitInputs.requestID = BigInt(proofReq.id);
32
+ const query = circuitQueries[0];
33
+ query.operator = query.operator === circuits_1.Operators.SD ? circuits_1.Operators.EQ : query.operator;
34
+ circuitInputs.query = query;
35
+ circuitInputs.claim = {
36
+ issuerID: circuitClaimData.issuerId,
37
+ claim: circuitClaimData.claim,
38
+ incProof: { proof: circuitClaimData.proof, treeState: circuitClaimData.treeState },
39
+ nonRevProof: circuitClaimData.nonRevProof
40
+ };
41
+ circuitInputs.currentTimeStamp = (0, js_iden3_core_1.getUnixTimestamp)(new Date());
42
+ circuitInputs.claimSubjectProfileNonce = BigInt(params.credentialSubjectProfileNonce);
43
+ circuitInputs.profileNonce = BigInt(params.authProfileNonce);
44
+ circuitInputs.skipClaimRevocationCheck = params.skipRevocation;
45
+ return circuitInputs.inputsMarshal();
46
+ };
47
+ this.credentialAtomicQueryMTPV2OnChainPrepareInputs = async ({ preparedCredential, identifier, proofReq, params, circuitQueries }) => {
48
+ const circuitClaimData = await this.newCircuitClaimData(preparedCredential);
49
+ const authInfo = await this.prepareAuthBJJCredential(identifier);
50
+ const authClaimData = await this.newCircuitClaimData({
51
+ credential: authInfo.credential,
52
+ credentialCoreClaim: authInfo.coreClaim
53
+ });
54
+ circuitClaimData.nonRevProof = (0, common_1.toClaimNonRevStatus)(preparedCredential.revStatus);
55
+ const circuitInputs = new circuits_1.AtomicQueryMTPV2OnChainInputs();
56
+ const id = js_iden3_core_1.DID.idFromDID(identifier);
57
+ circuitInputs.id = js_iden3_core_1.DID.idFromDID(identifier);
58
+ circuitInputs.requestID = BigInt(proofReq.id);
59
+ const stateProof = await this._stateStorage.getGISTProof(id.bigInt());
60
+ const gistProof = (0, common_1.toGISTProof)(stateProof);
61
+ circuitInputs.gistProof = gistProof;
62
+ if (authClaimData?.treeState) {
63
+ circuitInputs.treeState = {
64
+ state: authClaimData?.treeState?.state,
65
+ claimsRoot: authClaimData?.treeState?.claimsRoot,
66
+ revocationRoot: authClaimData?.treeState?.revocationRoot,
67
+ rootOfRoots: authClaimData?.treeState?.rootOfRoots
68
+ };
69
+ }
70
+ circuitInputs.authClaim = authClaimData.claim;
71
+ circuitInputs.authClaimIncMtp = authClaimData.proof;
72
+ circuitInputs.authClaimNonRevMtp = authInfo.nonRevProof.proof;
73
+ if (!params.challenge) {
74
+ throw new Error('challenge must be provided for onchain circuits');
75
+ }
76
+ const signature = await this._identityWallet.signChallenge(params.challenge, authInfo.credential);
77
+ circuitInputs.signature = signature;
78
+ circuitInputs.challenge = params.challenge;
79
+ const query = circuitQueries[0];
80
+ query.operator = query.operator === circuits_1.Operators.SD ? circuits_1.Operators.EQ : query.operator;
81
+ circuitInputs.query = query;
82
+ circuitInputs.claim = {
83
+ issuerID: circuitClaimData.issuerId,
84
+ claim: circuitClaimData.claim,
85
+ incProof: { proof: circuitClaimData.proof, treeState: circuitClaimData.treeState },
86
+ nonRevProof: circuitClaimData.nonRevProof
87
+ };
88
+ circuitInputs.currentTimeStamp = (0, js_iden3_core_1.getUnixTimestamp)(new Date());
89
+ circuitInputs.claimSubjectProfileNonce = BigInt(params.credentialSubjectProfileNonce);
90
+ circuitInputs.profileNonce = BigInt(params.authProfileNonce);
91
+ circuitInputs.skipClaimRevocationCheck = params.skipRevocation;
92
+ return circuitInputs.inputsMarshal();
93
+ };
94
+ this.credentialAtomicQuerySigV2PrepareInputs = async ({ preparedCredential, identifier, proofReq, params, circuitQueries }) => {
95
+ const circuitClaimData = await this.newCircuitClaimData(preparedCredential);
96
+ circuitClaimData.nonRevProof = (0, common_1.toClaimNonRevStatus)(preparedCredential.revStatus);
97
+ const circuitInputs = new circuits_1.AtomicQuerySigV2Inputs();
98
+ circuitInputs.id = js_iden3_core_1.DID.idFromDID(identifier);
99
+ circuitInputs.claim = {
100
+ issuerID: circuitClaimData?.issuerId,
101
+ signatureProof: circuitClaimData.signatureProof,
102
+ claim: circuitClaimData.claim,
103
+ nonRevProof: circuitClaimData.nonRevProof
104
+ };
105
+ circuitInputs.requestID = BigInt(proofReq.id);
106
+ circuitInputs.claimSubjectProfileNonce = BigInt(params.credentialSubjectProfileNonce);
107
+ circuitInputs.profileNonce = BigInt(params.authProfileNonce);
108
+ circuitInputs.skipClaimRevocationCheck = params.skipRevocation;
109
+ const query = circuitQueries[0];
110
+ query.operator = query.operator === circuits_1.Operators.SD ? circuits_1.Operators.EQ : query.operator;
111
+ circuitInputs.query = query;
112
+ circuitInputs.currentTimeStamp = (0, js_iden3_core_1.getUnixTimestamp)(new Date());
113
+ return circuitInputs.inputsMarshal();
114
+ };
115
+ this.credentialAtomicQuerySigV2OnChainPrepareInputs = async ({ preparedCredential, identifier, proofReq, params, circuitQueries }) => {
116
+ const circuitClaimData = await this.newCircuitClaimData(preparedCredential);
117
+ const authInfo = await this.prepareAuthBJJCredential(identifier);
118
+ const authClaimData = await this.newCircuitClaimData({
119
+ credential: authInfo.credential,
120
+ credentialCoreClaim: authInfo.coreClaim
121
+ });
122
+ circuitClaimData.nonRevProof = (0, common_1.toClaimNonRevStatus)(preparedCredential.revStatus);
123
+ const circuitInputs = new circuits_1.AtomicQuerySigV2OnChainInputs();
124
+ const id = js_iden3_core_1.DID.idFromDID(identifier);
125
+ circuitInputs.id = id;
126
+ circuitInputs.claim = {
127
+ issuerID: circuitClaimData.issuerId,
128
+ signatureProof: circuitClaimData.signatureProof,
129
+ claim: circuitClaimData.claim,
130
+ nonRevProof: circuitClaimData.nonRevProof
131
+ };
132
+ circuitInputs.requestID = BigInt(proofReq.id);
133
+ circuitInputs.claimSubjectProfileNonce = BigInt(params.credentialSubjectProfileNonce);
134
+ circuitInputs.profileNonce = BigInt(params.authProfileNonce);
135
+ circuitInputs.skipClaimRevocationCheck = params.skipRevocation;
136
+ const query = circuitQueries[0];
137
+ query.operator = query.operator === circuits_1.Operators.SD ? circuits_1.Operators.EQ : query.operator;
138
+ circuitInputs.query = query;
139
+ circuitInputs.currentTimeStamp = (0, js_iden3_core_1.getUnixTimestamp)(new Date());
140
+ if (authClaimData.treeState) {
141
+ circuitInputs.treeState = {
142
+ state: authClaimData.treeState?.state,
143
+ claimsRoot: authClaimData.treeState?.claimsRoot,
144
+ revocationRoot: authClaimData.treeState?.revocationRoot,
145
+ rootOfRoots: authClaimData.treeState?.rootOfRoots
146
+ };
147
+ }
148
+ const stateProof = await this._stateStorage.getGISTProof(id.bigInt());
149
+ const gistProof = (0, common_1.toGISTProof)(stateProof);
150
+ circuitInputs.gistProof = gistProof;
151
+ circuitInputs.authClaim = authClaimData.claim;
152
+ circuitInputs.authClaimIncMtp = authClaimData.proof;
153
+ circuitInputs.authClaimNonRevMtp = authInfo.nonRevProof.proof;
154
+ if (!params.challenge) {
155
+ throw new Error('challenge must be provided for onchain circuits');
156
+ }
157
+ const signature = await this._identityWallet.signChallenge(params.challenge, authInfo.credential);
158
+ circuitInputs.signature = signature;
159
+ circuitInputs.challenge = params.challenge;
160
+ return circuitInputs.inputsMarshal();
161
+ };
162
+ this.credentialAtomicQueryV3PrepareInputs = async ({ preparedCredential, identifier, proofReq, params, circuitQueries }) => {
163
+ const circuitClaimData = await this.newCircuitClaimData(preparedCredential);
164
+ circuitClaimData.nonRevProof = (0, common_1.toClaimNonRevStatus)(preparedCredential.revStatus);
165
+ let proofType;
166
+ switch (proofReq.query.proofType) {
167
+ case verifiable_1.ProofType.BJJSignature:
168
+ proofType = verifiable_1.ProofType.BJJSignature;
169
+ break;
170
+ case verifiable_1.ProofType.Iden3SparseMerkleTreeProof:
171
+ proofType = verifiable_1.ProofType.Iden3SparseMerkleTreeProof;
172
+ break;
173
+ default:
174
+ if (circuitClaimData.proof) {
175
+ proofType = verifiable_1.ProofType.Iden3SparseMerkleTreeProof;
176
+ }
177
+ else if (circuitClaimData.signatureProof) {
178
+ proofType = verifiable_1.ProofType.BJJSignature;
179
+ }
180
+ else {
181
+ throw Error('claim has no MTP or signature proof');
182
+ }
183
+ break;
184
+ }
185
+ const circuitInputs = new circuits_1.AtomicQueryV3Inputs();
186
+ circuitInputs.id = js_iden3_core_1.DID.idFromDID(identifier);
187
+ circuitInputs.claim = {
188
+ issuerID: circuitClaimData?.issuerId,
189
+ signatureProof: circuitClaimData.signatureProof,
190
+ claim: circuitClaimData.claim,
191
+ nonRevProof: circuitClaimData.nonRevProof,
192
+ incProof: { proof: circuitClaimData.proof, treeState: circuitClaimData.treeState }
193
+ };
194
+ circuitInputs.requestID = BigInt(proofReq.id);
195
+ circuitInputs.claimSubjectProfileNonce = BigInt(params.credentialSubjectProfileNonce);
196
+ circuitInputs.profileNonce = BigInt(params.authProfileNonce);
197
+ circuitInputs.skipClaimRevocationCheck = params.skipRevocation;
198
+ const query = circuitQueries[0];
199
+ query.values = query.operator === circuits_1.Operators.SD ? new Array(64).fill(0) : query.values;
200
+ circuitInputs.query = query;
201
+ circuitInputs.currentTimeStamp = (0, js_iden3_core_1.getUnixTimestamp)(new Date());
202
+ circuitInputs.proofType = proofType;
203
+ circuitInputs.linkNonce = params.linkNonce ?? BigInt(0);
204
+ circuitInputs.verifierID = params.verifierDid ? js_iden3_core_1.DID.idFromDID(params.verifierDid) : undefined;
205
+ circuitInputs.nullifierSessionID = proofReq.params?.nullifierSessionId
206
+ ? BigInt(proofReq.params?.nullifierSessionId?.toString())
207
+ : BigInt(0);
208
+ return circuitInputs.inputsMarshal();
209
+ };
210
+ this.credentialAtomicQueryV3OnChainPrepareInputs = async ({ preparedCredential, identifier, proofReq, params, circuitQueries }) => {
211
+ const circuitClaimData = await this.newCircuitClaimData(preparedCredential);
212
+ circuitClaimData.nonRevProof = (0, common_1.toClaimNonRevStatus)(preparedCredential.revStatus);
213
+ let proofType;
214
+ switch (proofReq.query.proofType) {
215
+ case verifiable_1.ProofType.BJJSignature:
216
+ proofType = verifiable_1.ProofType.BJJSignature;
217
+ break;
218
+ case verifiable_1.ProofType.Iden3SparseMerkleTreeProof:
219
+ proofType = verifiable_1.ProofType.Iden3SparseMerkleTreeProof;
220
+ break;
221
+ default:
222
+ if (circuitClaimData.proof) {
223
+ proofType = verifiable_1.ProofType.Iden3SparseMerkleTreeProof;
224
+ }
225
+ else if (circuitClaimData.signatureProof) {
226
+ proofType = verifiable_1.ProofType.BJJSignature;
227
+ }
228
+ else {
229
+ throw Error('claim has no MTP or signature proof');
230
+ }
231
+ break;
232
+ }
233
+ const circuitInputs = new circuits_1.AtomicQueryV3OnChainInputs();
234
+ circuitInputs.id = js_iden3_core_1.DID.idFromDID(identifier);
235
+ circuitInputs.claim = {
236
+ issuerID: circuitClaimData?.issuerId,
237
+ signatureProof: circuitClaimData.signatureProof,
238
+ claim: circuitClaimData.claim,
239
+ nonRevProof: circuitClaimData.nonRevProof,
240
+ incProof: { proof: circuitClaimData.proof, treeState: circuitClaimData.treeState }
241
+ };
242
+ circuitInputs.requestID = BigInt(proofReq.id);
243
+ circuitInputs.claimSubjectProfileNonce = BigInt(params.credentialSubjectProfileNonce);
244
+ circuitInputs.profileNonce = BigInt(params.authProfileNonce);
245
+ circuitInputs.skipClaimRevocationCheck = params.skipRevocation;
246
+ const query = circuitQueries[0];
247
+ query.values = query.operator === circuits_1.Operators.SD ? new Array(64).fill(0) : query.values;
248
+ circuitInputs.query = query;
249
+ circuitInputs.currentTimeStamp = (0, js_iden3_core_1.getUnixTimestamp)(new Date());
250
+ circuitInputs.proofType = proofType;
251
+ circuitInputs.linkNonce = params.linkNonce ?? BigInt(0);
252
+ circuitInputs.verifierID = params.verifierDid ? js_iden3_core_1.DID.idFromDID(params.verifierDid) : undefined;
253
+ circuitInputs.nullifierSessionID = proofReq.params?.nullifierSessionID
254
+ ? BigInt(proofReq.params?.nullifierSessionID?.toString())
255
+ : BigInt(0);
256
+ let isEthIdentity = true;
257
+ try {
258
+ js_iden3_core_1.Id.ethAddressFromId(circuitInputs.id);
259
+ }
260
+ catch {
261
+ isEthIdentity = false;
262
+ }
263
+ circuitInputs.authEnabled = isEthIdentity ? 0 : 1;
264
+ circuitInputs.challenge = BigInt(params.challenge ?? 0);
265
+ const { nonce: authProfileNonce, genesisDID } = await this._identityWallet.getGenesisDIDMetadata(identifier);
266
+ const id = js_iden3_core_1.DID.idFromDID(genesisDID);
267
+ const stateProof = await this._stateStorage.getGISTProof(id.bigInt());
268
+ const gistProof = (0, common_1.toGISTProof)(stateProof);
269
+ circuitInputs.gistProof = gistProof;
270
+ // auth inputs
271
+ if (circuitInputs.authEnabled === 1) {
272
+ const authPrepared = await this.prepareAuthBJJCredential(genesisDID);
273
+ const authClaimData = await this.newCircuitClaimData({
274
+ credential: authPrepared.credential,
275
+ credentialCoreClaim: authPrepared.coreClaim
276
+ });
277
+ const signature = await this._identityWallet.signChallenge(circuitInputs.challenge, authPrepared.credential);
278
+ circuitInputs.profileNonce = BigInt(authProfileNonce);
279
+ circuitInputs.authClaim = authClaimData.claim;
280
+ circuitInputs.authClaimIncMtp = authClaimData.proof;
281
+ circuitInputs.authClaimNonRevMtp = authPrepared.nonRevProof.proof;
282
+ circuitInputs.treeState = authClaimData.treeState;
283
+ circuitInputs.signature = signature;
284
+ }
285
+ return circuitInputs.inputsMarshal();
286
+ };
287
+ this.linkedNullifierPrepareInputs = async ({ preparedCredential, identifier, proofReq, params }) => {
288
+ const circuitClaimData = await this.newCircuitClaimData(preparedCredential);
289
+ circuitClaimData.nonRevProof = (0, common_1.toClaimNonRevStatus)(preparedCredential.revStatus);
290
+ const circuitInputs = new circuits_1.LinkedNullifierInputs();
291
+ circuitInputs.linkNonce = params.linkNonce ?? BigInt(0);
292
+ circuitInputs.issuerClaim = circuitClaimData.claim;
293
+ circuitInputs.id = js_iden3_core_1.DID.idFromDID(identifier);
294
+ circuitInputs.claimSubjectProfileNonce = BigInt(params.credentialSubjectProfileNonce);
295
+ circuitInputs.verifierID = params.verifierDid ? js_iden3_core_1.DID.idFromDID(params.verifierDid) : undefined;
296
+ circuitInputs.nullifierSessionID = proofReq.params?.nullifierSessionID
297
+ ? BigInt(proofReq.params?.nullifierSessionID?.toString())
298
+ : BigInt(0);
299
+ return circuitInputs.inputsMarshal();
300
+ };
301
+ this.linkedMultiQuery10PrepareInputs = async ({ preparedCredential, proofReq, params, circuitQueries }) => {
302
+ const circuitClaimData = await this.newCircuitClaimData(preparedCredential);
303
+ circuitClaimData.nonRevProof = (0, common_1.toClaimNonRevStatus)(preparedCredential.revStatus);
304
+ const circuitInputs = new circuits_1.LinkedMultiQueryInputs();
305
+ circuitInputs.linkNonce = params.linkNonce ?? BigInt(0);
306
+ circuitInputs.claim = circuitClaimData.claim;
307
+ circuitInputs.query = circuitQueries;
308
+ circuitInputs.queryLength = circuitValidator[proofReq.circuitId].maxQueriesCount;
309
+ return circuitInputs.inputsMarshal();
310
+ };
311
+ }
312
+ async generateInputs(ctx) {
313
+ const { circuitId } = ctx.proofReq;
314
+ const fnName = `${circuitId.split('-')[0]}PrepareInputs`;
315
+ const queriesLength = ctx.circuitQueries.length;
316
+ if (queriesLength > circuitValidator[circuitId].maxQueriesCount) {
317
+ throw new Error(`circuit ${circuitId} supports only ${circuitValidator[circuitId].maxQueriesCount} queries`);
318
+ }
319
+ const fn = this[fnName];
320
+ if (!fn) {
321
+ throw new Error(`inputs generator for ${circuitId} not found`);
322
+ }
323
+ return fn(ctx);
324
+ }
325
+ async newCircuitClaimData(preparedCredential) {
326
+ const smtProof = preparedCredential.credential.getIden3SparseMerkleTreeProof();
327
+ const circuitClaim = new circuits_1.CircuitClaim();
328
+ circuitClaim.claim = preparedCredential.credentialCoreClaim;
329
+ circuitClaim.issuerId = js_iden3_core_1.DID.idFromDID(js_iden3_core_1.DID.parse(preparedCredential.credential.issuer));
330
+ if (smtProof) {
331
+ circuitClaim.proof = smtProof.mtp;
332
+ circuitClaim.treeState = {
333
+ state: smtProof.issuerData.state.value,
334
+ claimsRoot: smtProof.issuerData.state.claimsTreeRoot,
335
+ revocationRoot: smtProof.issuerData.state.revocationTreeRoot,
336
+ rootOfRoots: smtProof.issuerData.state.rootOfRoots
337
+ };
338
+ }
339
+ const sigProof = preparedCredential.credential.getBJJSignature2021Proof();
340
+ if (sigProof) {
341
+ const issuerDID = sigProof.issuerData.id;
342
+ const userDID = (0, credentials_1.getUserDIDFromCredential)(issuerDID, preparedCredential.credential);
343
+ const { credentialStatus, mtp, authCoreClaim } = sigProof.issuerData;
344
+ if (!credentialStatus) {
345
+ throw new Error("can't check the validity of issuer auth claim: no credential status in proof");
346
+ }
347
+ if (!mtp) {
348
+ throw new Error('issuer auth credential must have a mtp proof');
349
+ }
350
+ if (!authCoreClaim) {
351
+ throw new Error('issuer auth credential must have a core claim proof');
352
+ }
353
+ const opts = {
354
+ issuerGenesisState: sigProof.issuerData.state,
355
+ issuerDID,
356
+ userDID
357
+ };
358
+ const rs = await this._credentialWallet.getRevocationStatus(credentialStatus, opts);
359
+ const issuerAuthNonRevProof = (0, common_1.toClaimNonRevStatus)(rs);
360
+ circuitClaim.signatureProof = {
361
+ signature: sigProof.signature,
362
+ issuerAuthIncProof: {
363
+ proof: sigProof.issuerData.mtp,
364
+ treeState: {
365
+ state: sigProof.issuerData.state.value,
366
+ claimsRoot: sigProof.issuerData.state.claimsTreeRoot,
367
+ revocationRoot: sigProof.issuerData.state.revocationTreeRoot,
368
+ rootOfRoots: sigProof.issuerData.state.rootOfRoots
369
+ }
370
+ },
371
+ issuerAuthClaim: sigProof.issuerData.authCoreClaim,
372
+ issuerAuthNonRevProof
373
+ };
374
+ }
375
+ return circuitClaim;
376
+ }
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);
381
+ const authCoreClaim = authCredential.getCoreClaimFromProof(verifiable_1.ProofType.Iden3SparseMerkleTreeProof);
382
+ if (!authCoreClaim) {
383
+ throw new Error('auth core claim is not defined for auth bjj credential');
384
+ }
385
+ return {
386
+ credential: authCredential,
387
+ incProof,
388
+ nonRevProof,
389
+ coreClaim: authCoreClaim
390
+ };
391
+ }
392
+ }
393
+ exports.InputGenerator = InputGenerator;