@0xpolygonid/js-sdk 1.5.0 → 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 +1706 -473
  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 +3 -3
@@ -0,0 +1,315 @@
1
+ import { Claim, Id } from '@iden3/js-iden3-core';
2
+ import { BaseConfig, bigIntArrayToStringArray, prepareSiblingsStr, existenceToInt, getNodeAuxValue, prepareCircuitArrayValues } from './common';
3
+ import { CircuitError, ValueProof } from './models';
4
+ import { Hash, Proof, ZERO_HASH } from '@iden3/js-merkletree';
5
+ import { byteDecoder, byteEncoder } from '../utils';
6
+ import { ProofType } from '../verifiable';
7
+ const zero = '0';
8
+ /**
9
+ * AtomicQueryV3OnChainInputs ZK private inputs for credentialAtomicQueryV3OnChain.circom
10
+ *
11
+ * @beta
12
+ * @class AtomicQueryV3OnChainInputs
13
+ * @extends {BaseConfig}
14
+ */
15
+ export class AtomicQueryV3OnChainInputs extends BaseConfig {
16
+ validate() {
17
+ if (!this.requestID) {
18
+ throw new Error(CircuitError.EmptyRequestID);
19
+ }
20
+ if (!this.claim.nonRevProof.proof) {
21
+ throw new Error(CircuitError.EmptyClaimNonRevProof);
22
+ }
23
+ if (!this.query.values) {
24
+ throw new Error(CircuitError.EmptyQueryValue);
25
+ }
26
+ if (!this.proofType) {
27
+ throw new Error(CircuitError.InvalidProofType);
28
+ }
29
+ if (!this.challenge) {
30
+ throw new Error(CircuitError.EmptyChallenge);
31
+ }
32
+ if (this.authEnabled === 1) {
33
+ if (!this.authClaimIncMtp) {
34
+ throw new Error(CircuitError.EmptyAuthClaimProof);
35
+ }
36
+ if (!this.authClaimNonRevMtp) {
37
+ throw new Error(CircuitError.EmptyAuthClaimNonRevProof);
38
+ }
39
+ if (!this.signature) {
40
+ throw new Error(CircuitError.EmptyChallengeSignature);
41
+ }
42
+ if (!this.gistProof.proof) {
43
+ throw new Error(CircuitError.EmptyGISTProof);
44
+ }
45
+ }
46
+ if (this.proofType === ProofType.BJJSignature) {
47
+ if (!this.claim.signatureProof?.issuerAuthIncProof.proof) {
48
+ throw new Error(CircuitError.EmptyIssuerAuthClaimProof);
49
+ }
50
+ if (!this.claim.signatureProof.issuerAuthNonRevProof.proof) {
51
+ throw new Error(CircuitError.EmptyIssuerAuthClaimNonRevProof);
52
+ }
53
+ if (!this.claim.signatureProof.signature) {
54
+ throw new Error(CircuitError.EmptyClaimSignature);
55
+ }
56
+ }
57
+ if (this.proofType === ProofType.Iden3SparseMerkleTreeProof) {
58
+ if (!this.claim?.incProof?.proof) {
59
+ throw new Error(CircuitError.EmptyClaimProof);
60
+ }
61
+ }
62
+ }
63
+ fillMTPProofsWithZero(s) {
64
+ s.issuerClaimMtp = prepareSiblingsStr(new Proof(), this.getMTLevel());
65
+ s.issuerClaimClaimsTreeRoot = ZERO_HASH.bigInt().toString();
66
+ s.issuerClaimRevTreeRoot = ZERO_HASH.bigInt().toString();
67
+ s.issuerClaimRootsTreeRoot = ZERO_HASH.bigInt().toString();
68
+ s.issuerClaimIdenState = ZERO_HASH.bigInt().toString();
69
+ }
70
+ fillSigProofWithZero(s) {
71
+ s.issuerClaimSignatureR8x = zero;
72
+ s.issuerClaimSignatureR8y = zero;
73
+ s.issuerClaimSignatureS = zero;
74
+ s.issuerAuthClaim = new Claim().marshalJson();
75
+ s.issuerAuthClaimMtp = prepareSiblingsStr(new Proof(), this.getMTLevel());
76
+ s.issuerAuthClaimsTreeRoot = zero;
77
+ s.issuerAuthRevTreeRoot = zero;
78
+ s.issuerAuthRootsTreeRoot = zero;
79
+ s.issuerAuthClaimNonRevMtp = prepareSiblingsStr(new Proof(), this.getMTLevel());
80
+ s.issuerAuthClaimNonRevMtpAuxHi = ZERO_HASH.bigInt().toString();
81
+ s.issuerAuthClaimNonRevMtpAuxHv = ZERO_HASH.bigInt().toString();
82
+ s.issuerAuthClaimNonRevMtpNoAux = zero;
83
+ s.issuerAuthState = zero;
84
+ }
85
+ fillAuthWithZero(s) {
86
+ s.authClaim = new Claim().marshalJson();
87
+ s.userClaimsTreeRoot = ZERO_HASH.bigInt().toString();
88
+ s.userRevTreeRoot = ZERO_HASH.bigInt().toString();
89
+ s.userRootsTreeRoot = ZERO_HASH.bigInt().toString();
90
+ s.userState = ZERO_HASH.bigInt().toString();
91
+ s.authClaimIncMtp = prepareSiblingsStr(new Proof(), this.getMTLevel());
92
+ s.authClaimNonRevMtp = prepareSiblingsStr(new Proof(), this.getMTLevel());
93
+ s.challengeSignatureR8x = zero;
94
+ s.challengeSignatureR8y = zero;
95
+ s.challengeSignatureS = zero;
96
+ s.gistRoot = ZERO_HASH.bigInt().toString();
97
+ s.gistMtp = prepareSiblingsStr(new Proof(), this.getMTLevelOnChain());
98
+ s.authClaimNonRevMtpAuxHi = ZERO_HASH.bigInt().toString();
99
+ s.authClaimNonRevMtpAuxHv = ZERO_HASH.bigInt().toString();
100
+ s.authClaimNonRevMtpNoAux = zero;
101
+ s.gistMtpAuxHi = ZERO_HASH.bigInt().toString();
102
+ s.gistMtpAuxHv = ZERO_HASH.bigInt().toString();
103
+ s.gistMtpNoAux = zero;
104
+ }
105
+ // InputsMarshal returns Circom private inputs for credentialAtomicQueryV3OnChain.circom
106
+ inputsMarshal() {
107
+ this.validate();
108
+ if (this.query.valueProof) {
109
+ this.query.validate();
110
+ this.query.valueProof.validate();
111
+ }
112
+ let valueProof = this.query.valueProof;
113
+ if (!valueProof) {
114
+ valueProof = new ValueProof();
115
+ valueProof.path = 0n;
116
+ valueProof.value = 0n;
117
+ valueProof.mtp = new Proof();
118
+ }
119
+ const treeState = this.claim.nonRevProof.treeState;
120
+ if (!treeState) {
121
+ throw new Error(CircuitError.EmptyTreeState);
122
+ }
123
+ const s = {
124
+ requestID: this.requestID.toString(),
125
+ userGenesisID: this.id.bigInt().toString(),
126
+ profileNonce: this.profileNonce.toString(),
127
+ claimSubjectProfileNonce: this.claimSubjectProfileNonce.toString(),
128
+ issuerID: this.claim.issuerID.bigInt().toString(),
129
+ issuerClaim: this.claim.claim.marshalJson(),
130
+ issuerClaimNonRevClaimsTreeRoot: treeState.claimsRoot.bigInt().toString(),
131
+ issuerClaimNonRevRevTreeRoot: treeState.revocationRoot.bigInt().toString(),
132
+ issuerClaimNonRevRootsTreeRoot: treeState.rootOfRoots.bigInt().toString(),
133
+ issuerClaimNonRevState: treeState.state.bigInt().toString(),
134
+ issuerClaimNonRevMtp: prepareSiblingsStr(this.claim.nonRevProof.proof, this.getMTLevel()),
135
+ claimSchema: this.claim.claim.getSchemaHash().bigInt().toString(),
136
+ claimPathMtp: prepareSiblingsStr(valueProof.mtp, this.getMTLevelsClaim()),
137
+ claimPathValue: valueProof.value.toString(),
138
+ operator: this.query.operator,
139
+ timestamp: this.currentTimeStamp,
140
+ // value in this path in merklized json-ld document
141
+ slotIndex: this.query.slotIndex,
142
+ isRevocationChecked: 1
143
+ };
144
+ s.challenge = this.challenge?.toString();
145
+ if (this.authEnabled === 1) {
146
+ s.authClaim = this.authClaim?.marshalJson();
147
+ s.userClaimsTreeRoot = this.treeState.claimsRoot?.bigInt().toString();
148
+ s.userRevTreeRoot = this.treeState.revocationRoot?.bigInt().toString();
149
+ s.userRootsTreeRoot = this.treeState.rootOfRoots?.bigInt().toString();
150
+ s.userState = this.treeState.state?.bigInt().toString();
151
+ s.authClaimIncMtp = prepareSiblingsStr(this.authClaimIncMtp, this.getMTLevel());
152
+ s.authClaimNonRevMtp = prepareSiblingsStr(this.authClaimNonRevMtp, this.getMTLevel());
153
+ s.challengeSignatureR8x = this.signature.R8[0].toString();
154
+ s.challengeSignatureR8y = this.signature.R8[1].toString();
155
+ s.challengeSignatureS = this.signature.S.toString();
156
+ s.gistMtp =
157
+ this.gistProof && prepareSiblingsStr(this.gistProof.proof, this.getMTLevelOnChain());
158
+ const nodeAuxAuth = getNodeAuxValue(this.authClaimNonRevMtp);
159
+ s.authClaimNonRevMtpAuxHi = nodeAuxAuth.key.bigInt().toString();
160
+ s.authClaimNonRevMtpAuxHv = nodeAuxAuth.value.bigInt().toString();
161
+ s.authClaimNonRevMtpNoAux = nodeAuxAuth.noAux;
162
+ const globalNodeAux = getNodeAuxValue(this.gistProof.proof);
163
+ s.gistMtpAuxHi = globalNodeAux.key.bigInt().toString();
164
+ s.gistMtpAuxHv = globalNodeAux.value.bigInt().toString();
165
+ s.gistMtpNoAux = globalNodeAux.noAux;
166
+ s.gistRoot = this.gistProof.root.bigInt().toString();
167
+ }
168
+ else {
169
+ this.fillAuthWithZero(s);
170
+ }
171
+ if (this.skipClaimRevocationCheck) {
172
+ s.isRevocationChecked = 0;
173
+ }
174
+ if (this.proofType === ProofType.BJJSignature) {
175
+ s.proofType = '1';
176
+ s.issuerClaimSignatureR8x = this.claim.signatureProof?.signature.R8[0].toString();
177
+ s.issuerClaimSignatureR8y = this.claim.signatureProof?.signature.R8[1].toString();
178
+ s.issuerClaimSignatureS = this.claim.signatureProof?.signature.S.toString();
179
+ s.issuerAuthClaim = this.claim.signatureProof?.issuerAuthClaim?.marshalJson();
180
+ s.issuerAuthClaimMtp = prepareSiblingsStr(this.claim.signatureProof?.issuerAuthIncProof.proof, this.getMTLevel());
181
+ const issuerAuthTreeState = this.claim.nonRevProof.treeState;
182
+ if (!issuerAuthTreeState) {
183
+ throw new Error(CircuitError.EmptyTreeState);
184
+ }
185
+ s.issuerAuthClaimsTreeRoot = issuerAuthTreeState.claimsRoot.bigInt().toString();
186
+ s.issuerAuthRevTreeRoot = issuerAuthTreeState.revocationRoot.bigInt().toString();
187
+ s.issuerAuthRootsTreeRoot = issuerAuthTreeState.rootOfRoots.bigInt().toString();
188
+ s.issuerAuthClaimNonRevMtp = prepareSiblingsStr(this.claim.signatureProof?.issuerAuthNonRevProof.proof, this.getMTLevel());
189
+ const nodeAuxIssuerAuthNonRev = getNodeAuxValue(this.claim.signatureProof?.issuerAuthNonRevProof.proof);
190
+ s.issuerAuthClaimNonRevMtpAuxHi = nodeAuxIssuerAuthNonRev.key.bigInt().toString();
191
+ s.issuerAuthClaimNonRevMtpAuxHv = nodeAuxIssuerAuthNonRev.value.bigInt().toString();
192
+ s.issuerAuthClaimNonRevMtpNoAux = nodeAuxIssuerAuthNonRev.noAux;
193
+ s.issuerAuthState = this.claim.signatureProof?.issuerAuthIncProof.treeState?.state
194
+ .bigInt()
195
+ .toString();
196
+ this.fillMTPProofsWithZero(s);
197
+ }
198
+ else if (this.proofType === ProofType.Iden3SparseMerkleTreeProof) {
199
+ s.proofType = '2';
200
+ const incProofTreeState = this.claim.incProof?.treeState;
201
+ if (!incProofTreeState) {
202
+ throw new Error(CircuitError.EmptyTreeState);
203
+ }
204
+ s.issuerClaimMtp = prepareSiblingsStr(this.claim.incProof?.proof, this.getMTLevel());
205
+ s.issuerClaimClaimsTreeRoot = incProofTreeState.claimsRoot.bigInt().toString();
206
+ s.issuerClaimRevTreeRoot = incProofTreeState.revocationRoot.bigInt().toString();
207
+ s.issuerClaimRootsTreeRoot = incProofTreeState.rootOfRoots.bigInt().toString();
208
+ s.issuerClaimIdenState = incProofTreeState.state.bigInt().toString();
209
+ this.fillSigProofWithZero(s);
210
+ }
211
+ const nodeAuxNonRev = getNodeAuxValue(this.claim.nonRevProof.proof);
212
+ s.issuerClaimNonRevMtpAuxHi = nodeAuxNonRev.key.bigInt().toString();
213
+ s.issuerClaimNonRevMtpAuxHv = nodeAuxNonRev.value.bigInt().toString();
214
+ s.issuerClaimNonRevMtpNoAux = nodeAuxNonRev.noAux;
215
+ s.claimPathNotExists = existenceToInt(valueProof.mtp.existence);
216
+ const nodAuxJSONLD = getNodeAuxValue(valueProof.mtp);
217
+ s.claimPathMtpNoAux = nodAuxJSONLD.noAux;
218
+ s.claimPathMtpAuxHi = nodAuxJSONLD.key.bigInt().toString();
219
+ s.claimPathMtpAuxHv = nodAuxJSONLD.value.bigInt().toString();
220
+ s.claimPathKey = valueProof.path.toString();
221
+ const values = prepareCircuitArrayValues(this.query.values, this.getValueArrSize());
222
+ s.value = bigIntArrayToStringArray(values);
223
+ s.linkNonce = this.linkNonce.toString();
224
+ s.verifierID = this.verifierID?.bigInt().toString() ?? '0';
225
+ s.nullifierSessionID = this.nullifierSessionID.toString();
226
+ s.authEnabled = this.authEnabled.toString();
227
+ return byteEncoder.encode(JSON.stringify(s));
228
+ }
229
+ }
230
+ /**
231
+ * @beta
232
+ * AtomicQueryV3OnChainPubSignals public inputs
233
+ */
234
+ export class AtomicQueryV3OnChainPubSignals extends BaseConfig {
235
+ // PubSignalsUnmarshal unmarshal credentialAtomicQueryV3.circom public signals
236
+ pubSignalsUnmarshal(data) {
237
+ // expected order:
238
+ // merklized
239
+ // userID
240
+ // circuitQueryHash
241
+ // issuerState
242
+ // linkID
243
+ // nullifier
244
+ // operatorOutput
245
+ // proofType
246
+ // requestID
247
+ // challenge
248
+ // gistRoot
249
+ // issuerID
250
+ // isRevocationChecked
251
+ // issuerClaimNonRevState
252
+ // timestamp
253
+ // verifierID
254
+ // nullifierSessionID
255
+ // authEnabled
256
+ const sVals = JSON.parse(byteDecoder.decode(data));
257
+ let fieldIdx = 0;
258
+ // -- merklized
259
+ this.merklized = parseInt(sVals[fieldIdx]);
260
+ fieldIdx++;
261
+ // - userID
262
+ this.userID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
263
+ fieldIdx++;
264
+ // - circuitQueryHash
265
+ this.circuitQueryHash = BigInt(sVals[fieldIdx]);
266
+ fieldIdx++;
267
+ // - issuerState
268
+ this.issuerState = Hash.fromString(sVals[fieldIdx]);
269
+ fieldIdx++;
270
+ // - linkID
271
+ this.linkID = BigInt(sVals[fieldIdx]);
272
+ fieldIdx++;
273
+ // - nullifier
274
+ this.nullifier = BigInt(sVals[fieldIdx]);
275
+ fieldIdx++;
276
+ // - operatorOutput
277
+ this.operatorOutput = BigInt(sVals[fieldIdx]);
278
+ fieldIdx++;
279
+ // - proofType
280
+ this.proofType = parseInt(sVals[fieldIdx]);
281
+ fieldIdx++;
282
+ // - requestID
283
+ this.requestID = BigInt(sVals[fieldIdx]);
284
+ fieldIdx++;
285
+ // - challenge
286
+ this.challenge = BigInt(sVals[fieldIdx]);
287
+ fieldIdx++;
288
+ // - gistRoot
289
+ this.gistRoot = Hash.fromString(sVals[fieldIdx]);
290
+ fieldIdx++;
291
+ // - issuerID
292
+ this.issuerID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
293
+ fieldIdx++;
294
+ // - isRevocationChecked
295
+ this.isRevocationChecked = parseInt(sVals[fieldIdx]);
296
+ fieldIdx++;
297
+ // - issuerClaimNonRevState
298
+ this.issuerClaimNonRevState = Hash.fromString(sVals[fieldIdx]);
299
+ fieldIdx++;
300
+ // - timestamp
301
+ this.timestamp = parseInt(sVals[fieldIdx]);
302
+ fieldIdx++;
303
+ // - verifierID
304
+ if (sVals[fieldIdx] !== '0') {
305
+ this.verifierID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
306
+ }
307
+ fieldIdx++;
308
+ // - nullifierSessionID
309
+ this.nullifierSessionID = BigInt(sVals[fieldIdx]);
310
+ fieldIdx++;
311
+ // - authEnabled
312
+ this.authEnabled = parseInt(sVals[fieldIdx]);
313
+ return this;
314
+ }
315
+ }
@@ -0,0 +1,270 @@
1
+ import { Claim, Id, SchemaHash } from '@iden3/js-iden3-core';
2
+ import { BaseConfig, bigIntArrayToStringArray, prepareSiblingsStr, existenceToInt, getNodeAuxValue, prepareCircuitArrayValues } from './common';
3
+ import { CircuitError, ValueProof } from './models';
4
+ import { Hash, Proof, ZERO_HASH } from '@iden3/js-merkletree';
5
+ import { byteDecoder, byteEncoder } from '../utils';
6
+ import { ProofType } from '../verifiable';
7
+ const zero = '0';
8
+ /**
9
+ * AtomicQueryV3Inputs ZK private inputs for credentialAtomicQueryV3.circom
10
+ *
11
+ * @beta
12
+ * @class AtomicQueryV3Inputs
13
+ * @extends {BaseConfig}
14
+ */
15
+ export class AtomicQueryV3Inputs extends BaseConfig {
16
+ validate() {
17
+ if (!this.requestID) {
18
+ throw new Error(CircuitError.EmptyRequestID);
19
+ }
20
+ if (!this.claim.nonRevProof.proof) {
21
+ throw new Error(CircuitError.EmptyClaimNonRevProof);
22
+ }
23
+ if (!this.query.values) {
24
+ throw new Error(CircuitError.EmptyQueryValue);
25
+ }
26
+ if (!this.proofType) {
27
+ throw new Error(CircuitError.InvalidProofType);
28
+ }
29
+ if (this.proofType === ProofType.BJJSignature) {
30
+ if (!this.claim.signatureProof?.issuerAuthIncProof.proof) {
31
+ throw new Error(CircuitError.EmptyIssuerAuthClaimProof);
32
+ }
33
+ if (!this.claim.signatureProof.issuerAuthNonRevProof.proof) {
34
+ throw new Error(CircuitError.EmptyIssuerAuthClaimNonRevProof);
35
+ }
36
+ if (!this.claim.signatureProof.signature) {
37
+ throw new Error(CircuitError.EmptyClaimSignature);
38
+ }
39
+ }
40
+ if (this.proofType === ProofType.Iden3SparseMerkleTreeProof) {
41
+ if (!this.claim?.incProof?.proof) {
42
+ throw new Error(CircuitError.EmptyClaimProof);
43
+ }
44
+ }
45
+ }
46
+ fillMTPProofsWithZero(s) {
47
+ s.issuerClaimMtp = prepareSiblingsStr(new Proof(), this.getMTLevel());
48
+ s.issuerClaimClaimsTreeRoot = ZERO_HASH.bigInt().toString();
49
+ s.issuerClaimRevTreeRoot = ZERO_HASH.bigInt().toString();
50
+ s.issuerClaimRootsTreeRoot = ZERO_HASH.bigInt().toString();
51
+ s.issuerClaimIdenState = ZERO_HASH.bigInt().toString();
52
+ }
53
+ fillSigProofWithZero(s) {
54
+ s.issuerClaimSignatureR8x = zero;
55
+ s.issuerClaimSignatureR8y = zero;
56
+ s.issuerClaimSignatureS = zero;
57
+ s.issuerAuthClaim = new Claim().marshalJson();
58
+ s.issuerAuthClaimMtp = prepareSiblingsStr(new Proof(), this.getMTLevel());
59
+ s.issuerAuthClaimsTreeRoot = zero;
60
+ s.issuerAuthRevTreeRoot = zero;
61
+ s.issuerAuthRootsTreeRoot = zero;
62
+ s.issuerAuthClaimNonRevMtp = prepareSiblingsStr(new Proof(), this.getMTLevel());
63
+ s.issuerAuthClaimNonRevMtpAuxHi = ZERO_HASH.bigInt().toString();
64
+ s.issuerAuthClaimNonRevMtpAuxHv = ZERO_HASH.bigInt().toString();
65
+ s.issuerAuthClaimNonRevMtpNoAux = zero;
66
+ s.issuerAuthState = zero;
67
+ }
68
+ // InputsMarshal returns Circom private inputs for credentialAtomicQueryV3.circom
69
+ inputsMarshal() {
70
+ this.validate();
71
+ if (this.query.valueProof) {
72
+ this.query.validate();
73
+ this.query.valueProof.validate();
74
+ }
75
+ let valueProof = this.query.valueProof;
76
+ if (!valueProof) {
77
+ valueProof = new ValueProof();
78
+ valueProof.path = 0n;
79
+ valueProof.value = 0n;
80
+ valueProof.mtp = new Proof();
81
+ }
82
+ let treeState = this.claim.nonRevProof.treeState;
83
+ if (this.proofType === ProofType.BJJSignature && this.skipClaimRevocationCheck) {
84
+ treeState = this.claim.signatureProof?.issuerAuthNonRevProof.treeState;
85
+ }
86
+ if (!treeState) {
87
+ throw new Error(CircuitError.EmptyTreeState);
88
+ }
89
+ const s = {
90
+ requestID: this.requestID.toString(),
91
+ userGenesisID: this.id.bigInt().toString(),
92
+ profileNonce: this.profileNonce.toString(),
93
+ claimSubjectProfileNonce: this.claimSubjectProfileNonce.toString(),
94
+ issuerID: this.claim.issuerID.bigInt().toString(),
95
+ issuerClaim: this.claim.claim.marshalJson(),
96
+ issuerClaimNonRevClaimsTreeRoot: treeState.claimsRoot.bigInt().toString(),
97
+ issuerClaimNonRevRevTreeRoot: treeState.revocationRoot.bigInt().toString(),
98
+ issuerClaimNonRevRootsTreeRoot: treeState.rootOfRoots.bigInt().toString(),
99
+ issuerClaimNonRevState: treeState.state.bigInt().toString(),
100
+ issuerClaimNonRevMtp: prepareSiblingsStr(this.claim.nonRevProof.proof, this.getMTLevel()),
101
+ claimSchema: this.claim.claim.getSchemaHash().bigInt().toString(),
102
+ claimPathMtp: prepareSiblingsStr(valueProof.mtp, this.getMTLevelsClaim()),
103
+ claimPathValue: valueProof.value.toString(),
104
+ operator: this.query.operator,
105
+ timestamp: this.currentTimeStamp,
106
+ // value in this path in merklized json-ld document
107
+ slotIndex: this.query.slotIndex,
108
+ isRevocationChecked: 1
109
+ };
110
+ if (this.skipClaimRevocationCheck) {
111
+ s.isRevocationChecked = 0;
112
+ }
113
+ if (this.proofType === ProofType.BJJSignature) {
114
+ s.proofType = '1';
115
+ s.issuerClaimSignatureR8x = this.claim.signatureProof?.signature.R8[0].toString();
116
+ s.issuerClaimSignatureR8y = this.claim.signatureProof?.signature.R8[1].toString();
117
+ s.issuerClaimSignatureS = this.claim.signatureProof?.signature.S.toString();
118
+ s.issuerAuthClaim = this.claim.signatureProof?.issuerAuthClaim?.marshalJson();
119
+ s.issuerAuthClaimMtp = prepareSiblingsStr(this.claim.signatureProof?.issuerAuthIncProof.proof, this.getMTLevel());
120
+ s.issuerAuthClaimsTreeRoot = treeState.claimsRoot.bigInt().toString();
121
+ s.issuerAuthRevTreeRoot = treeState.revocationRoot.bigInt().toString();
122
+ s.issuerAuthRootsTreeRoot = treeState.rootOfRoots.bigInt().toString();
123
+ s.issuerAuthClaimNonRevMtp = prepareSiblingsStr(this.claim.signatureProof?.issuerAuthNonRevProof.proof, this.getMTLevel());
124
+ const nodeAuxIssuerAuthNonRev = getNodeAuxValue(this.claim.signatureProof?.issuerAuthNonRevProof.proof);
125
+ s.issuerAuthClaimNonRevMtpAuxHi = nodeAuxIssuerAuthNonRev.key.bigInt().toString();
126
+ s.issuerAuthClaimNonRevMtpAuxHv = nodeAuxIssuerAuthNonRev.value.bigInt().toString();
127
+ s.issuerAuthClaimNonRevMtpNoAux = nodeAuxIssuerAuthNonRev.noAux;
128
+ s.issuerAuthState = this.claim.signatureProof?.issuerAuthIncProof.treeState?.state
129
+ .bigInt()
130
+ .toString();
131
+ this.fillMTPProofsWithZero(s);
132
+ }
133
+ else if (this.proofType === ProofType.Iden3SparseMerkleTreeProof) {
134
+ s.proofType = '2';
135
+ const incProofTreeState = this.claim.incProof?.treeState;
136
+ if (!incProofTreeState) {
137
+ throw new Error(CircuitError.EmptyTreeState);
138
+ }
139
+ s.issuerClaimMtp = prepareSiblingsStr(this.claim.incProof?.proof, this.getMTLevel());
140
+ s.issuerClaimClaimsTreeRoot = incProofTreeState.claimsRoot.bigInt().toString();
141
+ s.issuerClaimRevTreeRoot = incProofTreeState.revocationRoot.bigInt().toString();
142
+ s.issuerClaimRootsTreeRoot = incProofTreeState.rootOfRoots.bigInt().toString();
143
+ s.issuerClaimIdenState = incProofTreeState.state.bigInt().toString();
144
+ this.fillSigProofWithZero(s);
145
+ }
146
+ const nodeAuxNonRev = getNodeAuxValue(this.claim.nonRevProof.proof);
147
+ s.issuerClaimNonRevMtpAuxHi = nodeAuxNonRev.key.bigInt().toString();
148
+ s.issuerClaimNonRevMtpAuxHv = nodeAuxNonRev.value.bigInt().toString();
149
+ s.issuerClaimNonRevMtpNoAux = nodeAuxNonRev.noAux;
150
+ s.claimPathNotExists = existenceToInt(valueProof.mtp.existence);
151
+ const nodAuxJSONLD = getNodeAuxValue(valueProof.mtp);
152
+ s.claimPathMtpNoAux = nodAuxJSONLD.noAux;
153
+ s.claimPathMtpAuxHi = nodAuxJSONLD.key.bigInt().toString();
154
+ s.claimPathMtpAuxHv = nodAuxJSONLD.value.bigInt().toString();
155
+ s.claimPathKey = valueProof.path.toString();
156
+ const values = prepareCircuitArrayValues(this.query.values, this.getValueArrSize());
157
+ s.value = bigIntArrayToStringArray(values);
158
+ s.linkNonce = this.linkNonce.toString();
159
+ s.verifierID = this.verifierID?.bigInt().toString() ?? '0';
160
+ s.nullifierSessionID = this.nullifierSessionID.toString();
161
+ return byteEncoder.encode(JSON.stringify(s));
162
+ }
163
+ }
164
+ /**
165
+ * @beta
166
+ * AtomicQueryV3PubSignals public inputs
167
+ */
168
+ export class AtomicQueryV3PubSignals extends BaseConfig {
169
+ constructor() {
170
+ super(...arguments);
171
+ this.value = [];
172
+ }
173
+ // PubSignalsUnmarshal unmarshal credentialAtomicQueryV3.circom public signals
174
+ pubSignalsUnmarshal(data) {
175
+ // expected order:
176
+ // merklized
177
+ // userID
178
+ // issuerState
179
+ // linkID
180
+ // nullifier
181
+ // operatorOutput
182
+ // proofType
183
+ // requestID
184
+ // issuerID
185
+ // isRevocationChecked
186
+ // issuerClaimNonRevState
187
+ // timestamp
188
+ // claimSchema
189
+ // claimPathNotExists
190
+ // claimPathKey
191
+ // slotIndex
192
+ // operator
193
+ // value
194
+ // verifierID
195
+ // nullifierSessionID
196
+ // 19 is a number of fields in AtomicQueryV3PubSignals before values, values is last element in the proof and
197
+ // it is length could be different base on the circuit configuration. The length could be modified by set value
198
+ // in ValueArraySize
199
+ const fieldLength = 19;
200
+ const sVals = JSON.parse(byteDecoder.decode(data));
201
+ if (sVals.length !== fieldLength + this.getValueArrSize()) {
202
+ throw new Error(`invalid number of Output values expected ${fieldLength + this.getValueArrSize()} got ${sVals.length}`);
203
+ }
204
+ let fieldIdx = 0;
205
+ // -- merklized
206
+ this.merklized = parseInt(sVals[fieldIdx]);
207
+ fieldIdx++;
208
+ // - userID
209
+ this.userID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
210
+ fieldIdx++;
211
+ // - issuerState
212
+ this.issuerState = Hash.fromString(sVals[fieldIdx]);
213
+ fieldIdx++;
214
+ // - linkID
215
+ this.linkID = BigInt(sVals[fieldIdx]);
216
+ fieldIdx++;
217
+ // - nullifier
218
+ this.nullifier = BigInt(sVals[fieldIdx]);
219
+ fieldIdx++;
220
+ // - operatorOutput
221
+ this.operatorOutput = BigInt(sVals[fieldIdx]);
222
+ fieldIdx++;
223
+ // - proofType
224
+ this.proofType = parseInt(sVals[fieldIdx]);
225
+ fieldIdx++;
226
+ // - requestID
227
+ this.requestID = BigInt(sVals[fieldIdx]);
228
+ fieldIdx++;
229
+ // - issuerID
230
+ this.issuerID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
231
+ fieldIdx++;
232
+ // - isRevocationChecked
233
+ this.isRevocationChecked = parseInt(sVals[fieldIdx]);
234
+ fieldIdx++;
235
+ // - issuerClaimNonRevState
236
+ this.issuerClaimNonRevState = Hash.fromString(sVals[fieldIdx]);
237
+ fieldIdx++;
238
+ // - timestamp
239
+ this.timestamp = parseInt(sVals[fieldIdx]);
240
+ fieldIdx++;
241
+ // - claimSchema
242
+ this.claimSchema = SchemaHash.newSchemaHashFromInt(BigInt(sVals[fieldIdx]));
243
+ fieldIdx++;
244
+ // - ClaimPathNotExists
245
+ this.claimPathNotExists = parseInt(sVals[fieldIdx]);
246
+ fieldIdx++;
247
+ // - ClaimPathKey
248
+ this.claimPathKey = BigInt(sVals[fieldIdx]);
249
+ fieldIdx++;
250
+ // - slotIndex
251
+ this.slotIndex = parseInt(sVals[fieldIdx]);
252
+ fieldIdx++;
253
+ // - operator
254
+ this.operator = parseInt(sVals[fieldIdx]);
255
+ fieldIdx++;
256
+ // - values
257
+ for (let index = 0; index < this.getValueArrSize(); index++) {
258
+ this.value.push(BigInt(sVals[fieldIdx]));
259
+ fieldIdx++;
260
+ }
261
+ // - verifierID
262
+ if (sVals[fieldIdx] !== '0') {
263
+ this.verifierID = Id.fromBigInt(BigInt(sVals[fieldIdx]));
264
+ }
265
+ fieldIdx++;
266
+ // - nullifierSessionID
267
+ this.nullifierSessionID = BigInt(sVals[fieldIdx]);
268
+ return this;
269
+ }
270
+ }
@@ -3,7 +3,7 @@ import { Hash, ZERO_HASH, swapEndianness } from '@iden3/js-merkletree';
3
3
  export const defaultMTLevels = 40; // max MT levels, default value for identity circuits
4
4
  export const defaultValueArraySize = 64; // max value array size, default value for identity circuits
5
5
  export const defaultMTLevelsOnChain = 64; // max MT levels on chain, default value for identity circuits
6
- export const defaultMTLevelsClaimsMerklization = 32; // max MT levels of JSON-LD merklization on claim
6
+ export const defaultMTLevelsClaim = 32; // max MT levels of JSON-LD merklization on claim
7
7
  export const ErrorEmptyAuthClaimProof = 'empty auth claim mtp proof';
8
8
  export const ErrorEmptyAuthClaimNonRevProof = 'empty auth claim non-revocation mtp proof';
9
9
  export const ErrorEmptyChallengeSignature = 'empty challenge signature';
@@ -29,14 +29,12 @@ export class BaseConfig {
29
29
  return this.mtLevel ? this.mtLevel : defaultMTLevels;
30
30
  }
31
31
  /**
32
- * getMTLevel max circuit MT levels
32
+ * GetMTLevelsClaim max jsonld Claim levels
33
33
  *
34
34
  * @returns number
35
35
  */
36
- getMTLevelsClaimMerklization() {
37
- return this.mtLevelClaimsMerklization
38
- ? this.mtLevelClaimsMerklization
39
- : defaultMTLevelsClaimsMerklization;
36
+ getMTLevelsClaim() {
37
+ return this.mtLevelClaim ? this.mtLevelClaim : defaultMTLevelsClaim;
40
38
  }
41
39
  /**
42
40
  * GetValueArrSize return size of circuits value array size
@@ -1,3 +1,18 @@
1
+ /**
2
+ * Represents the XSD namespace and its corresponding data types.
3
+ */
4
+ export var XSDNS;
5
+ (function (XSDNS) {
6
+ XSDNS["Boolean"] = "http://www.w3.org/2001/XMLSchema#boolean";
7
+ XSDNS["Integer"] = "http://www.w3.org/2001/XMLSchema#integer";
8
+ XSDNS["NonNegativeInteger"] = "http://www.w3.org/2001/XMLSchema#nonNegativeInteger";
9
+ XSDNS["NonPositiveInteger"] = "http://www.w3.org/2001/XMLSchema#nonPositiveInteger";
10
+ XSDNS["NegativeInteger"] = "http://www.w3.org/2001/XMLSchema#negativeInteger";
11
+ XSDNS["PositiveInteger"] = "http://www.w3.org/2001/XMLSchema#positiveInteger";
12
+ XSDNS["DateTime"] = "http://www.w3.org/2001/XMLSchema#dateTime";
13
+ XSDNS["Double"] = "http://www.w3.org/2001/XMLSchema#double";
14
+ XSDNS["String"] = "http://www.w3.org/2001/XMLSchema#string";
15
+ })(XSDNS || (XSDNS = {}));
1
16
  /**
2
17
  * List of available operators.
3
18
  *
@@ -12,6 +27,11 @@ export var Operators;
12
27
  Operators[Operators["IN"] = 4] = "IN";
13
28
  Operators[Operators["NIN"] = 5] = "NIN";
14
29
  Operators[Operators["NE"] = 6] = "NE";
30
+ Operators[Operators["LTE"] = 7] = "LTE";
31
+ Operators[Operators["GTE"] = 8] = "GTE";
32
+ Operators[Operators["BETWEEN"] = 9] = "BETWEEN";
33
+ Operators[Operators["SD"] = 16] = "SD";
34
+ Operators[Operators["NULLIFY"] = 17] = "NULLIFY";
15
35
  })(Operators || (Operators = {}));
16
36
  /** QueryOperators represents operators for atomic circuits */
17
37
  export const QueryOperators = {
@@ -21,7 +41,41 @@ export const QueryOperators = {
21
41
  $gt: Operators.GT,
22
42
  $in: Operators.IN,
23
43
  $nin: Operators.NIN,
24
- $ne: Operators.NE
44
+ $ne: Operators.NE,
45
+ $lte: Operators.LTE,
46
+ $gte: Operators.GTE,
47
+ $between: Operators.BETWEEN,
48
+ $sd: Operators.SD,
49
+ $nullify: Operators.NULLIFY
50
+ };
51
+ const allOperations = Object.values(QueryOperators);
52
+ export const availableTypesOperators = new Map([
53
+ [XSDNS.Boolean, [QueryOperators.$eq, QueryOperators.$ne]],
54
+ [XSDNS.Integer, allOperations],
55
+ [XSDNS.NonNegativeInteger, allOperations],
56
+ [XSDNS.PositiveInteger, allOperations],
57
+ [XSDNS.Double, [QueryOperators.$eq, QueryOperators.$ne, QueryOperators.$in, QueryOperators.$nin]],
58
+ [XSDNS.String, [QueryOperators.$eq, QueryOperators.$ne, QueryOperators.$in, QueryOperators.$nin]],
59
+ [XSDNS.DateTime, allOperations]
60
+ ]);
61
+ /**
62
+ * Checks if the given operation is valid for the specified datatype.
63
+ * @param datatype - The datatype to check the operation for.
64
+ * @param op - The operation to check.
65
+ * @returns True if the operation is valid, false otherwise.
66
+ */
67
+ export const isValidOperation = (datatype, op) => {
68
+ if (op === Operators.NOOP) {
69
+ return true;
70
+ }
71
+ if (!availableTypesOperators.has(datatype)) {
72
+ return false;
73
+ }
74
+ const ops = availableTypesOperators.get(datatype);
75
+ if (!ops) {
76
+ return false;
77
+ }
78
+ return ops.includes(op);
25
79
  };
26
80
  /**
27
81
  * Scalar is used to compare two scalar value.