@0xpolygonid/js-sdk 1.23.0 → 1.24.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.
Files changed (56) hide show
  1. package/dist/browser/esm/index.js +3103 -358
  2. package/dist/browser/esm/index.js.map +1 -1
  3. package/dist/browser/umd/index.js +3106 -356
  4. package/dist/browser/umd/index.js.map +1 -1
  5. package/dist/node/cjs/credentials/credential-wallet.js +11 -78
  6. package/dist/node/cjs/credentials/index.js +1 -0
  7. package/dist/node/cjs/credentials/status/did-resolver-revocation.js +21 -0
  8. package/dist/node/cjs/iden3comm/handlers/fetch.js +32 -0
  9. package/dist/node/cjs/iden3comm/handlers/payment.js +38 -13
  10. package/dist/node/cjs/storage/blockchain/abi/ERC20.json +1 -0
  11. package/dist/node/cjs/storage/blockchain/did-resolver-readonly-storage.js +72 -0
  12. package/dist/node/cjs/storage/blockchain/{erc20-permit-sig.js → erc20-helper.js} +14 -1
  13. package/dist/node/cjs/storage/blockchain/index.js +3 -1
  14. package/dist/node/cjs/storage/blockchain/onchain-issuer-adapter/non-merklized/version/v0.0.1/onchain-non-merklized-issuer-adapter.js +245 -0
  15. package/dist/node/cjs/storage/blockchain/onchain-issuer.js +82 -0
  16. package/dist/node/cjs/storage/blockchain/onchain-zkp-verifier.js +38 -34
  17. package/dist/node/cjs/storage/interfaces/index.js +1 -0
  18. package/dist/node/cjs/storage/interfaces/onchain-issuer.js +2 -0
  19. package/dist/node/cjs/utils/did-helper.js +3 -3
  20. package/dist/node/cjs/verifiable/constants.js +1 -0
  21. package/dist/node/cjs/verifiable/credential.js +86 -0
  22. package/dist/node/esm/credentials/credential-wallet.js +12 -79
  23. package/dist/node/esm/credentials/index.js +1 -0
  24. package/dist/node/esm/credentials/status/did-resolver-revocation.js +17 -0
  25. package/dist/node/esm/iden3comm/handlers/fetch.js +32 -0
  26. package/dist/node/esm/iden3comm/handlers/payment.js +38 -14
  27. package/dist/node/esm/storage/blockchain/abi/ERC20.json +1 -0
  28. package/dist/node/esm/storage/blockchain/did-resolver-readonly-storage.js +68 -0
  29. package/dist/node/esm/storage/blockchain/{erc20-permit-sig.js → erc20-helper.js} +14 -2
  30. package/dist/node/esm/storage/blockchain/index.js +3 -1
  31. package/dist/node/esm/storage/blockchain/onchain-issuer-adapter/non-merklized/version/v0.0.1/onchain-non-merklized-issuer-adapter.js +241 -0
  32. package/dist/node/esm/storage/blockchain/onchain-issuer.js +78 -0
  33. package/dist/node/esm/storage/blockchain/onchain-zkp-verifier.js +38 -34
  34. package/dist/node/esm/storage/interfaces/index.js +1 -0
  35. package/dist/node/esm/storage/interfaces/onchain-issuer.js +1 -0
  36. package/dist/node/esm/utils/did-helper.js +3 -3
  37. package/dist/node/esm/verifiable/constants.js +1 -0
  38. package/dist/node/esm/verifiable/credential.js +88 -2
  39. package/dist/types/credentials/credential-wallet.d.ts +0 -6
  40. package/dist/types/credentials/index.d.ts +1 -0
  41. package/dist/types/credentials/models.d.ts +8 -0
  42. package/dist/types/credentials/status/did-resolver-revocation.d.ts +7 -0
  43. package/dist/types/iden3comm/handlers/fetch.d.ts +8 -0
  44. package/dist/types/iden3comm/handlers/payment.d.ts +3 -2
  45. package/dist/types/storage/blockchain/did-resolver-readonly-storage.d.ts +16 -0
  46. package/dist/types/storage/blockchain/{erc20-permit-sig.d.ts → erc20-helper.d.ts} +8 -1
  47. package/dist/types/storage/blockchain/index.d.ts +3 -1
  48. package/dist/types/storage/blockchain/onchain-issuer-adapter/non-merklized/version/v0.0.1/onchain-non-merklized-issuer-adapter.d.ts +66 -0
  49. package/dist/types/storage/blockchain/onchain-issuer.d.ts +40 -0
  50. package/dist/types/storage/blockchain/onchain-zkp-verifier.d.ts +11 -12
  51. package/dist/types/storage/entities/state.d.ts +2 -0
  52. package/dist/types/storage/interfaces/index.d.ts +1 -0
  53. package/dist/types/storage/interfaces/onchain-issuer.d.ts +12 -0
  54. package/dist/types/verifiable/constants.d.ts +1 -0
  55. package/dist/types/verifiable/credential.d.ts +15 -2
  56. package/package.json +3 -2
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OnchainNonMerklizedIssuerAdapter = void 0;
4
+ const js_iden3_core_1 = require("@iden3/js-iden3-core");
5
+ const onchain_non_merklized_issuer_base_abi_1 = require("@iden3/onchain-non-merklized-issuer-base-abi");
6
+ const verifiable_1 = require("../../../../../../verifiable");
7
+ const js_jsonld_merklization_1 = require("@iden3/js-jsonld-merklization");
8
+ const circuits_1 = require("../../../../../../circuits");
9
+ const js_merkletree_1 = require("@iden3/js-merkletree");
10
+ const ethers_1 = require("ethers");
11
+ const js_iden3_core_2 = require("@iden3/js-iden3-core");
12
+ var NonMerklizedIssuerInterfaces;
13
+ (function (NonMerklizedIssuerInterfaces) {
14
+ NonMerklizedIssuerInterfaces["InterfaceDetection"] = "0x01ffc9a7";
15
+ NonMerklizedIssuerInterfaces["InterfaceNonMerklizedIssuer"] = "0x58874949";
16
+ NonMerklizedIssuerInterfaces["InterfaceGetCredential"] = "0x5d1ca631";
17
+ })(NonMerklizedIssuerInterfaces || (NonMerklizedIssuerInterfaces = {}));
18
+ var ValueHashes;
19
+ (function (ValueHashes) {
20
+ ValueHashes["BooleanTrue"] = "18586133768512220936620570745912940619677854269274689475585506675881198879027";
21
+ ValueHashes["BooleanFalse"] = "19014214495641488759237505126948346942972912379615652741039992445865937985820";
22
+ })(ValueHashes || (ValueHashes = {}));
23
+ /**
24
+ * `OnchainNonMerklizedIssuerAdapter` provides functionality to interact with a non-merklized on-chain credential issuer.
25
+ * This adapter enables interface detection, credential retrieval, and conversion to the W3C Verifiable Credential format.
26
+ *
27
+ * @public
28
+ * @beta
29
+ * @class OnchainNonMerklizedIssuerAdapter
30
+ */
31
+ class OnchainNonMerklizedIssuerAdapter {
32
+ /**
33
+ * Initializes an instance of `OnchainNonMerklizedIssuerAdapter`.
34
+ *
35
+ * @param ethConnectionConfig The configuration for the Ethereum connection.
36
+ * @param issuerDid The decentralized identifier (DID) of the issuer.
37
+ * @param merklizationOptions Optional settings for merklization.
38
+ */
39
+ constructor(ethConnectionConfig, issuerDid, options) {
40
+ if (!ethConnectionConfig.chainId) {
41
+ throw new Error('Chain ID is required');
42
+ }
43
+ this._chainId = ethConnectionConfig.chainId;
44
+ this._contractAddress = ethers_1.ethers.getAddress(ethers_1.ethers.hexlify(js_iden3_core_1.Id.ethAddressFromId(js_iden3_core_1.DID.idFromDID(issuerDid))));
45
+ this._contract = onchain_non_merklized_issuer_base_abi_1.NonMerklizedIssuerBase__factory.connect(this._contractAddress, new ethers_1.ethers.JsonRpcProvider(ethConnectionConfig.url));
46
+ this._issuerDid = issuerDid;
47
+ this._merklizationOptions = options?.merklizationOptions;
48
+ }
49
+ /**
50
+ * Checks if the contract supports required interfaces.
51
+ * Throws an error if any required interface is unsupported.
52
+ *
53
+ * @throws Error - If required interfaces are not supported.
54
+ */
55
+ async isInterfaceSupported() {
56
+ const supportedInterfaces = [
57
+ {
58
+ name: 'Interface detection ERC-165',
59
+ value: NonMerklizedIssuerInterfaces.InterfaceDetection
60
+ },
61
+ {
62
+ name: 'Interface non-merklized issuer',
63
+ value: NonMerklizedIssuerInterfaces.InterfaceNonMerklizedIssuer
64
+ },
65
+ {
66
+ name: 'Interface get credential',
67
+ value: NonMerklizedIssuerInterfaces.InterfaceGetCredential
68
+ }
69
+ ];
70
+ const unsupportedInterfaces = await Promise.all(supportedInterfaces.map(async (interfaceObj) => {
71
+ const isSupported = await this._contract.supportsInterface(interfaceObj.value);
72
+ return isSupported ? null : interfaceObj.name;
73
+ }));
74
+ const unsupportedInterfacesFiltered = unsupportedInterfaces.filter((interfaceName) => interfaceName !== null);
75
+ if (unsupportedInterfacesFiltered.length > 0) {
76
+ throw new Error(`Unsupported interfaces: ${unsupportedInterfacesFiltered.join(', ')}`);
77
+ }
78
+ }
79
+ /**
80
+ * Retrieves a credential from the on-chain non-merklized contract.
81
+ * @param userId The user's core.Id.
82
+ * @param credentialId The unique identifier of the credential.
83
+ */
84
+ async getCredential(userId, credentialId) {
85
+ const [credentialData, coreClaimBigInts, credentialSubjectFields] = await this._contract.getCredential(userId.bigInt(), credentialId);
86
+ return { credentialData, coreClaimBigInts, credentialSubjectFields };
87
+ }
88
+ /**
89
+ * Retrieves the credential IDs of a user.
90
+ * @param userId The user's core.Id.
91
+ * @returns An array of credential IDs.
92
+ */
93
+ async getUserCredentialsIds(userId) {
94
+ return this._contract.getUserCredentialIds(userId.bigInt());
95
+ }
96
+ /**
97
+ * Converts on-chain credential to a verifiable credential.
98
+ *
99
+ * @param credentialData Data structure of the credential from the contract.
100
+ * @param coreClaimBigInts Claim data in bigint format.
101
+ * @param credentialSubjectFields Subject fields of the credential.
102
+ */
103
+ async convertOnChainInfoToW3CCredential(credentialData, coreClaimBigInts, credentialSubjectFields) {
104
+ const c = new js_iden3_core_1.Claim().unMarshalJson(JSON.stringify(coreClaimBigInts.map((b) => b.toString())));
105
+ const credentialSubject = await this.convertCredentialSubject(c, credentialData.context, credentialData._type, credentialSubjectFields);
106
+ const credentialRequest = {
107
+ id: this.credentialId(credentialData.id),
108
+ credentialSchema: credentialData.credentialSchema.id,
109
+ type: credentialData._type,
110
+ credentialSubject: credentialSubject,
111
+ expiration: c.getExpirationDate()?.getTime(),
112
+ displayMethod: this.convertDisplayMethod(credentialData.displayMethod),
113
+ context: credentialData.context,
114
+ revocationOpts: {
115
+ id: this._contractAddress,
116
+ nonce: Number(c.getRevocationNonce()),
117
+ type: verifiable_1.CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023
118
+ },
119
+ issuanceDate: (0, js_iden3_core_2.getDateFromUnixTimestamp)(Number(credentialData.issuanceDate)).getTime()
120
+ };
121
+ const existenceProof = await this.existenceProof(c);
122
+ const w3c = verifiable_1.W3CCredential.fromCredentialRequest(this._issuerDid, credentialRequest);
123
+ w3c.proof = [existenceProof];
124
+ return w3c;
125
+ }
126
+ credentialId(id) {
127
+ return `urn:iden3:onchain:${this._chainId}:${this._contractAddress}:${id}`;
128
+ }
129
+ async convertCredentialSubject(coreClaim, contractContexts, credentialType, credentialSubjectFields) {
130
+ const contractContextsStr = JSON.stringify({
131
+ '@context': contractContexts
132
+ });
133
+ const credentialSubject = {};
134
+ for (const f of credentialSubjectFields) {
135
+ const dataType = await js_jsonld_merklization_1.Path.newTypeFromContext(contractContextsStr, `${credentialType}.${f.key}`, this._merklizationOptions);
136
+ switch (dataType) {
137
+ case circuits_1.XSDNS.Boolean: {
138
+ switch (f.rawValue.toString()) {
139
+ case ValueHashes.BooleanTrue:
140
+ credentialSubject[f.key] = true;
141
+ break;
142
+ case ValueHashes.BooleanFalse:
143
+ credentialSubject[f.key] = false;
144
+ break;
145
+ }
146
+ break;
147
+ }
148
+ case (circuits_1.XSDNS.NonNegativeInteger,
149
+ circuits_1.XSDNS.NonPositiveInteger,
150
+ circuits_1.XSDNS.NegativeInteger,
151
+ circuits_1.XSDNS.PositiveInteger):
152
+ {
153
+ credentialSubject[f.key] = f.value.toString();
154
+ break;
155
+ }
156
+ case circuits_1.XSDNS.Integer: {
157
+ credentialSubject[f.key] = Number(f.value);
158
+ break;
159
+ }
160
+ case circuits_1.XSDNS.String: {
161
+ this.validateSourceValue(dataType, f.value, f.rawValue);
162
+ credentialSubject[f.key] = f.rawValue;
163
+ break;
164
+ }
165
+ case circuits_1.XSDNS.DateTime: {
166
+ const timestamp = BigInt(f.rawValue);
167
+ const sourceTimestamp = (0, js_iden3_core_2.getDateFromUnixTimestamp)(Number(timestamp)).toISOString();
168
+ this.validateSourceValue(dataType, f.value, sourceTimestamp);
169
+ credentialSubject[f.key] = sourceTimestamp;
170
+ break;
171
+ }
172
+ case circuits_1.XSDNS.Double: {
173
+ const rawFloat = Number(f.rawValue);
174
+ this.validateSourceValue(dataType, f.value, rawFloat);
175
+ credentialSubject[f.key] = rawFloat;
176
+ break;
177
+ }
178
+ default: {
179
+ throw new Error(`Unsupported data type ${dataType}`);
180
+ }
181
+ }
182
+ }
183
+ credentialSubject['type'] = credentialType;
184
+ const subjectId = coreClaim.getId();
185
+ const subjectDid = js_iden3_core_1.DID.parseFromId(subjectId);
186
+ credentialSubject['id'] = subjectDid.string();
187
+ return credentialSubject;
188
+ }
189
+ async existenceProof(coreClaim) {
190
+ const [mtpProof, stateInfo] = await this._contract.getClaimProofWithStateInfo(coreClaim.hIndex());
191
+ if (!mtpProof.existence) {
192
+ throw new Error('Claim does not exist');
193
+ }
194
+ const latestStateHash = js_merkletree_1.Hash.fromBigInt(stateInfo.state);
195
+ const latestClaimsOfRootHash = js_merkletree_1.Hash.fromBigInt(stateInfo.claimsRoot);
196
+ const latestRevocationOfRootHash = js_merkletree_1.Hash.fromBigInt(stateInfo.revocationsRoot);
197
+ const latestRootsOfRootHash = js_merkletree_1.Hash.fromBigInt(stateInfo.rootsRoot);
198
+ const p = new js_merkletree_1.Proof({
199
+ siblings: mtpProof.siblings.map((s) => js_merkletree_1.Hash.fromBigInt(s)),
200
+ existence: mtpProof.existence,
201
+ nodeAux: mtpProof.auxExistence
202
+ ? {
203
+ key: js_merkletree_1.Hash.fromBigInt(mtpProof.auxIndex),
204
+ value: js_merkletree_1.Hash.fromBigInt(mtpProof.auxValue)
205
+ }
206
+ : undefined
207
+ });
208
+ return new verifiable_1.Iden3SparseMerkleTreeProof({
209
+ issuerData: {
210
+ id: this._issuerDid,
211
+ state: {
212
+ value: latestStateHash,
213
+ claimsTreeRoot: latestClaimsOfRootHash,
214
+ revocationTreeRoot: latestRevocationOfRootHash,
215
+ rootOfRoots: latestRootsOfRootHash
216
+ }
217
+ },
218
+ mtp: p,
219
+ coreClaim: coreClaim
220
+ });
221
+ }
222
+ async validateSourceValue(dataType, originHash, source) {
223
+ const sourceHash = await js_jsonld_merklization_1.Merklizer.hashValue(dataType, source);
224
+ if (sourceHash !== originHash) {
225
+ throw new Error(`Invalid source value for ${dataType} type`);
226
+ }
227
+ }
228
+ convertDisplayMethod(onchainDisplayMethod) {
229
+ if (!onchainDisplayMethod.id || !onchainDisplayMethod._type) {
230
+ return undefined;
231
+ }
232
+ switch (onchainDisplayMethod._type) {
233
+ case verifiable_1.DisplayMethodType.Iden3BasicDisplayMethodV1: {
234
+ return {
235
+ id: onchainDisplayMethod.id,
236
+ type: verifiable_1.DisplayMethodType.Iden3BasicDisplayMethodV1
237
+ };
238
+ }
239
+ default: {
240
+ throw new Error(`Unsupported display method type ${onchainDisplayMethod._type}`);
241
+ }
242
+ }
243
+ }
244
+ }
245
+ exports.OnchainNonMerklizedIssuerAdapter = OnchainNonMerklizedIssuerAdapter;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OnchainIssuer = void 0;
4
+ const js_iden3_core_1 = require("@iden3/js-iden3-core");
5
+ const ethers_1 = require("ethers");
6
+ const onchain_non_merklized_issuer_base_abi_1 = require("@iden3/onchain-non-merklized-issuer-base-abi");
7
+ const onchain_non_merklized_issuer_adapter_1 = require("./onchain-issuer-adapter/non-merklized/version/v0.0.1/onchain-non-merklized-issuer-adapter");
8
+ /**
9
+ * Represents an adapter for interacting with on-chain issuers.
10
+ *
11
+ * @public
12
+ * @beta
13
+ * @class OnchainIssuer
14
+ */
15
+ class OnchainIssuer {
16
+ /**
17
+ * Initializes an instance of `Adapter`.
18
+ * @param config The configuration for the Ethereum connection.
19
+ * @param merklizationOptions Optional settings for merklization.
20
+ */
21
+ constructor(config, options) {
22
+ this._ethConnectionConfig = config;
23
+ this._onchainIssuerOptions = options;
24
+ }
25
+ /**
26
+ * Retrieves a credential from the on-chain issuer.
27
+ * @param issuerDID The issuer's core.DID.
28
+ * @param userId The user's core.Id.
29
+ * @param credentialId The unique identifier of the credential.
30
+ */
31
+ async getCredential(issuerDID, userDID, credentialId) {
32
+ const { contract, connection } = this.getContractConnection(issuerDID);
33
+ const response = await contract.getCredentialAdapterVersion();
34
+ switch (response) {
35
+ case '0.0.1': {
36
+ const adapter = new onchain_non_merklized_issuer_adapter_1.OnchainNonMerklizedIssuerAdapter(connection, issuerDID, {
37
+ merklizationOptions: this._onchainIssuerOptions?.merklizationOptions
38
+ });
39
+ await adapter.isInterfaceSupported();
40
+ const { credentialData, coreClaimBigInts, credentialSubjectFields } = await adapter.getCredential(js_iden3_core_1.DID.idFromDID(userDID), credentialId);
41
+ return await adapter.convertOnChainInfoToW3CCredential(credentialData, coreClaimBigInts, credentialSubjectFields);
42
+ }
43
+ default:
44
+ throw new Error(`Unsupported adapter version ${response}`);
45
+ }
46
+ }
47
+ /**
48
+ * Retrieves the credential identifiers for a user from the on-chain issuer.
49
+ * @param issuerDID The issuer's core.DID.
50
+ * @param userId The user's core.Id.
51
+ */
52
+ async getUserCredentialIds(issuerDID, userDID) {
53
+ const { contract, connection } = this.getContractConnection(issuerDID);
54
+ const response = await contract.getCredentialAdapterVersion();
55
+ switch (response) {
56
+ case '0.0.1': {
57
+ const adapter = new onchain_non_merklized_issuer_adapter_1.OnchainNonMerklizedIssuerAdapter(connection, issuerDID, {
58
+ merklizationOptions: this._onchainIssuerOptions?.merklizationOptions
59
+ });
60
+ await adapter.isInterfaceSupported();
61
+ return await adapter.getUserCredentialsIds(js_iden3_core_1.DID.idFromDID(userDID));
62
+ }
63
+ default:
64
+ throw new Error(`Unsupported adapter version ${response}`);
65
+ }
66
+ }
67
+ getContractConnection(did) {
68
+ const issuerId = js_iden3_core_1.DID.idFromDID(did);
69
+ const chainId = (0, js_iden3_core_1.chainIDfromDID)(did);
70
+ const contractAddress = ethers_1.ethers.getAddress(ethers_1.ethers.hexlify(js_iden3_core_1.Id.ethAddressFromId(issuerId)));
71
+ const connection = this._ethConnectionConfig.find((c) => c.chainId === chainId);
72
+ if (!connection) {
73
+ throw new Error(`No connection found for chain ID ${chainId}`);
74
+ }
75
+ if (!connection.url) {
76
+ throw new Error(`No URL found for chain ID ${chainId}`);
77
+ }
78
+ const contract = new ethers_1.Contract(contractAddress, onchain_non_merklized_issuer_base_abi_1.INonMerklizedIssuerABI, new ethers_1.ethers.JsonRpcProvider(connection.url));
79
+ return { contract, connection };
80
+ }
81
+ }
82
+ exports.OnchainIssuer = OnchainIssuer;
@@ -43,23 +43,8 @@ class OnChainZKPVerifier {
43
43
  constructor(_configs, _opts) {
44
44
  this._configs = _configs;
45
45
  this._opts = _opts;
46
- /**
47
- * supported circuits
48
- */
49
- this._supportedCircuits = [
50
- circuits_1.CircuitId.AtomicQueryMTPV2OnChain,
51
- circuits_1.CircuitId.AtomicQuerySigV2OnChain,
52
- circuits_1.CircuitId.AtomicQueryV3OnChain
53
- ];
54
- /**
55
- * abi coder to encode/decode structures to solidity bytes
56
- */
57
- this._abiCoder = new ethers_1.ethers.AbiCoder();
58
46
  }
59
- /**
60
- * {@inheritDoc IOnChainZKPVerifier.prepareTxArgsSubmitV1}
61
- */
62
- async prepareTxArgsSubmitV1(txData, zkProofResponse) {
47
+ static async prepareTxArgsSubmitV1(txData, zkProofResponse) {
63
48
  if (txData.method_id.replace('0x', '') !== FunctionSignatures.SubmitZKPResponseV1) {
64
49
  throw new Error(`prepareTxArgsSubmitV1 function doesn't implement requested method id. Only '0x${FunctionSignatures.SubmitZKPResponseV1}' is supported.`);
65
50
  }
@@ -77,6 +62,12 @@ class OnChainZKPVerifier {
77
62
  ];
78
63
  return payload;
79
64
  }
65
+ /**
66
+ * {@inheritDoc IOnChainZKPVerifier.prepareTxArgsSubmitV1}
67
+ */
68
+ async prepareTxArgsSubmitV1(txData, zkProofResponse) {
69
+ return OnChainZKPVerifier.prepareTxArgsSubmitV1(txData, zkProofResponse);
70
+ }
80
71
  /**
81
72
  * {@inheritDoc IOnChainZKPVerifier.submitZKPResponse}
82
73
  */
@@ -166,13 +157,10 @@ class OnChainZKPVerifier {
166
157
  const { txnHash } = await transactionService.sendTransactionRequest(ethSigner, request);
167
158
  return new Map().set(txnHash, zkProofResponses);
168
159
  }
169
- async prepareTxArgsSubmitV2(txData, zkProofResponses) {
160
+ static async prepareTxArgsSubmitV2(resolverUrl, txData, zkProofResponses) {
170
161
  if (txData.method_id.replace('0x', '') !== FunctionSignatures.SubmitZKPResponseV2) {
171
162
  throw new Error(`submit cross chain doesn't implement requested method id. Only '0x${FunctionSignatures.SubmitZKPResponseV2}' is supported.`);
172
163
  }
173
- if (!this._opts?.didResolverUrl) {
174
- throw new Error(`did resolver url required for crosschain verification`);
175
- }
176
164
  const gistUpdateArr = [];
177
165
  const stateUpdateArr = [];
178
166
  const payload = [];
@@ -197,7 +185,9 @@ class OnChainZKPVerifier {
197
185
  continue;
198
186
  }
199
187
  gistUpdateResolutionsPending.push(JSON.stringify(gist));
200
- gistUpdateResolutions.push(this.resolveDidDocumentEip712MessageAndSignature(js_iden3_core_1.DID.parseFromId(gist.id), this._opts.didResolverUrl, { gist: gist.root }));
188
+ gistUpdateResolutions.push(this.resolveDidDocumentEip712MessageAndSignature(js_iden3_core_1.DID.parseFromId(gist.id), resolverUrl, {
189
+ gist: gist.root
190
+ }));
201
191
  }
202
192
  const stateUpdateResolutions = [];
203
193
  for (const state of stateInfo.states) {
@@ -206,7 +196,7 @@ class OnChainZKPVerifier {
206
196
  continue;
207
197
  }
208
198
  stateUpdateResolutionsPending.push(JSON.stringify(state));
209
- stateUpdateResolutions.push(this.resolveDidDocumentEip712MessageAndSignature(js_iden3_core_1.DID.parseFromId(state.id), this._opts.didResolverUrl, {
199
+ stateUpdateResolutions.push(this.resolveDidDocumentEip712MessageAndSignature(js_iden3_core_1.DID.parseFromId(state.id), resolverUrl, {
210
200
  state: state.state
211
201
  }));
212
202
  }
@@ -240,10 +230,16 @@ class OnChainZKPVerifier {
240
230
  const crossChainProofs = this.packCrossChainProofs(gistUpdateArr, stateUpdateArr);
241
231
  return [payload, crossChainProofs];
242
232
  }
243
- packZkpProof(inputs, a, b, c) {
244
- return this._abiCoder.encode(['uint256[] inputs', 'uint256[2]', 'uint256[2][2]', 'uint256[2]'], [inputs, a, b, c]);
233
+ async prepareTxArgsSubmitV2(txData, zkProofResponses) {
234
+ if (!this._opts?.didResolverUrl) {
235
+ throw new Error(`did resolver url required for crosschain verification`);
236
+ }
237
+ return OnChainZKPVerifier.prepareTxArgsSubmitV2(this._opts.didResolverUrl, txData, zkProofResponses);
238
+ }
239
+ static packZkpProof(inputs, a, b, c) {
240
+ return new ethers_1.ethers.AbiCoder().encode(['uint256[] inputs', 'uint256[2]', 'uint256[2][2]', 'uint256[2]'], [inputs, a, b, c]);
245
241
  }
246
- packCrossChainProofs(gistUpdateArr, stateUpdateArr) {
242
+ static packCrossChainProofs(gistUpdateArr, stateUpdateArr) {
247
243
  const proofs = [];
248
244
  for (const globalStateUpdate of gistUpdateArr) {
249
245
  proofs.push({
@@ -257,10 +253,10 @@ class OnChainZKPVerifier {
257
253
  proof: this.packIdentityStateMsg(stateUpdate)
258
254
  });
259
255
  }
260
- return this._abiCoder.encode(['tuple(' + 'string proofType,' + 'bytes proof' + ')[]'], [proofs]);
256
+ return new ethers_1.ethers.AbiCoder().encode(['tuple(' + 'string proofType,' + 'bytes proof' + ')[]'], [proofs]);
261
257
  }
262
- packGlobalStateMsg(msg) {
263
- return this._abiCoder.encode([
258
+ static packGlobalStateMsg(msg) {
259
+ return new ethers_1.ethers.AbiCoder().encode([
264
260
  'tuple(' +
265
261
  'tuple(' +
266
262
  'uint256 timestamp,' +
@@ -272,8 +268,8 @@ class OnChainZKPVerifier {
272
268
  ')'
273
269
  ], [msg]);
274
270
  }
275
- packIdentityStateMsg(msg) {
276
- return this._abiCoder.encode([
271
+ static packIdentityStateMsg(msg) {
272
+ return new ethers_1.ethers.AbiCoder().encode([
277
273
  'tuple(' +
278
274
  'tuple(' +
279
275
  'uint256 timestamp,' +
@@ -285,10 +281,10 @@ class OnChainZKPVerifier {
285
281
  ')'
286
282
  ], [msg]);
287
283
  }
288
- packMetadatas(metas) {
289
- return this._abiCoder.encode(['tuple(' + 'string key,' + 'bytes value' + ')[]'], [metas]);
284
+ static packMetadatas(metas) {
285
+ return new ethers_1.ethers.AbiCoder().encode(['tuple(' + 'string key,' + 'bytes value' + ')[]'], [metas]);
290
286
  }
291
- getOnChainGistRootStatePubSignals(onChainCircuitId, inputs) {
287
+ static getOnChainGistRootStatePubSignals(onChainCircuitId, inputs) {
292
288
  let atomicQueryPubSignals;
293
289
  switch (onChainCircuitId) {
294
290
  case circuits_1.CircuitId.AtomicQueryMTPV2OnChain:
@@ -305,7 +301,7 @@ class OnChainZKPVerifier {
305
301
  atomicQueryPubSignals.pubSignalsUnmarshal(encodedInputs);
306
302
  return atomicQueryPubSignals.getStatesInfo();
307
303
  }
308
- async resolveDidDocumentEip712MessageAndSignature(did, resolverUrl, opts) {
304
+ static async resolveDidDocumentEip712MessageAndSignature(did, resolverUrl, opts) {
309
305
  const didDoc = await (0, utils_1.resolveDidDocument)(did, resolverUrl, {
310
306
  ...opts,
311
307
  signature: utils_1.DIDDocumentSignature.EthereumEip712Signature2021
@@ -339,3 +335,11 @@ class OnChainZKPVerifier {
339
335
  }
340
336
  }
341
337
  exports.OnChainZKPVerifier = OnChainZKPVerifier;
338
+ /**
339
+ * supported circuits
340
+ */
341
+ OnChainZKPVerifier._supportedCircuits = [
342
+ circuits_1.CircuitId.AtomicQueryMTPV2OnChain,
343
+ circuits_1.CircuitId.AtomicQuerySigV2OnChain,
344
+ circuits_1.CircuitId.AtomicQueryV3OnChain
345
+ ];
@@ -23,3 +23,4 @@ __exportStar(require("./data-source"), exports);
23
23
  __exportStar(require("./circuits"), exports);
24
24
  __exportStar(require("./onchain-zkp-verifier"), exports);
25
25
  __exportStar(require("./onchain-revocation"), exports);
26
+ __exportStar(require("./onchain-issuer"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -72,7 +72,7 @@ const validateDIDDocumentAuth = async (did, resolverURL, state) => {
72
72
  };
73
73
  exports.validateDIDDocumentAuth = validateDIDDocumentAuth;
74
74
  const resolveDIDDocumentAuth = async (did, resolveURL, state) => {
75
- let url = `${resolveURL}/${did.string().replace(/:/g, '%3A')}`;
75
+ let url = `${resolveURL}/${encodeURIComponent(did.string())}`;
76
76
  if (state) {
77
77
  url += `?state=${state.hex()}`;
78
78
  }
@@ -89,11 +89,11 @@ function emptyStateDID(did) {
89
89
  return emptyDID;
90
90
  }
91
91
  const resolveDidDocument = async (did, resolverUrl, opts) => {
92
- let didString = did.string().replace(/:/g, '%3A');
92
+ let didString = encodeURIComponent(did.string());
93
93
  // for gist resolve we have to `hide` user did (look into resolver implementation)
94
94
  const isGistRequest = opts?.gist && !opts.state;
95
95
  if (isGistRequest) {
96
- didString = emptyStateDID(did).string();
96
+ didString = encodeURIComponent(emptyStateDID(did).string());
97
97
  }
98
98
  let url = `${resolverUrl}/1.0/identifiers/${didString}`;
99
99
  if (opts?.signature) {
@@ -157,6 +157,7 @@ var SupportedCurrencies;
157
157
  SupportedCurrencies["ETH_WEI"] = "ETHWEI";
158
158
  SupportedCurrencies["ETH_GWEI"] = "ETHGWEI";
159
159
  SupportedCurrencies["MATIC"] = "MATIC";
160
+ SupportedCurrencies["POL"] = "POL";
160
161
  SupportedCurrencies["USDT"] = "USDT";
161
162
  SupportedCurrencies["USDC"] = "USDC";
162
163
  })(SupportedCurrencies = exports.SupportedCurrencies || (exports.SupportedCurrencies = {}));
@@ -51,6 +51,92 @@ class W3CCredential {
51
51
  this.credentialSubject = {};
52
52
  this.issuer = '';
53
53
  }
54
+ /**
55
+ *
56
+ * @param issuer - DID of the issuer
57
+ * @param request - Credential request
58
+ * @returns - W3C Credential
59
+ */
60
+ static fromCredentialRequest(issuer, request) {
61
+ if (!request.id) {
62
+ throw new Error('Credential id is required');
63
+ }
64
+ if (!request.context) {
65
+ throw new Error('Credential context is required');
66
+ }
67
+ const context = [
68
+ constants_1.VerifiableConstants.JSONLD_SCHEMA.W3C_CREDENTIAL_2018,
69
+ constants_1.VerifiableConstants.JSONLD_SCHEMA.IDEN3_CREDENTIAL,
70
+ ...request.context
71
+ ];
72
+ const credentialType = [
73
+ constants_1.VerifiableConstants.CREDENTIAL_TYPE.W3C_VERIFIABLE_CREDENTIAL,
74
+ request.type
75
+ ];
76
+ const credentialSubject = request.credentialSubject;
77
+ credentialSubject['type'] = request.type;
78
+ const cr = new W3CCredential();
79
+ cr.id = request.id;
80
+ cr['@context'] = context;
81
+ cr.type = credentialType;
82
+ cr.credentialSubject = credentialSubject;
83
+ cr.issuer = issuer.string();
84
+ cr.credentialSchema = {
85
+ id: request.credentialSchema,
86
+ type: constants_1.VerifiableConstants.JSON_SCHEMA_VALIDATOR
87
+ };
88
+ cr.credentialStatus = W3CCredential.buildCredentialStatus(request, issuer);
89
+ request.expiration && (cr.expirationDate = new Date(request.expiration).toISOString());
90
+ request.refreshService && (cr.refreshService = request.refreshService);
91
+ request.displayMethod && (cr.displayMethod = request.displayMethod);
92
+ request.issuanceDate && (cr.issuanceDate = new Date(request.issuanceDate).toISOString());
93
+ return cr;
94
+ }
95
+ /**
96
+ * Builds credential status
97
+ * @param {CredentialRequest} request
98
+ * @returns `CredentialStatus`
99
+ */
100
+ static buildCredentialStatus(request, issuer) {
101
+ const credentialStatus = {
102
+ id: request.revocationOpts.id,
103
+ type: request.revocationOpts.type,
104
+ revocationNonce: request.revocationOpts.nonce
105
+ };
106
+ switch (request.revocationOpts.type) {
107
+ case constants_1.CredentialStatusType.SparseMerkleTreeProof:
108
+ return {
109
+ ...credentialStatus,
110
+ id: `${credentialStatus.id.replace(/\/$/, '')}/${credentialStatus.revocationNonce}`
111
+ };
112
+ case constants_1.CredentialStatusType.Iden3ReverseSparseMerkleTreeProof:
113
+ return {
114
+ ...credentialStatus,
115
+ id: request.revocationOpts.issuerState
116
+ ? `${credentialStatus.id.replace(/\/$/, '')}/node?state=${request.revocationOpts.issuerState}`
117
+ : `${credentialStatus.id.replace(/\/$/, '')}`
118
+ };
119
+ case constants_1.CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023: {
120
+ const issuerId = js_iden3_core_1.DID.idFromDID(issuer);
121
+ const chainId = (0, js_iden3_core_1.getChainId)(js_iden3_core_1.DID.blockchainFromId(issuerId), js_iden3_core_1.DID.networkIdFromId(issuerId));
122
+ const searchParams = [
123
+ ['revocationNonce', request.revocationOpts.nonce?.toString() || ''],
124
+ ['contractAddress', `${chainId}:${request.revocationOpts.id}`],
125
+ ['state', request.revocationOpts.issuerState || '']
126
+ ]
127
+ .filter(([, value]) => Boolean(value))
128
+ .map(([key, value]) => `${key}=${value}`)
129
+ .join('&');
130
+ return {
131
+ ...credentialStatus,
132
+ // `[did]:[methodid]:[chain]:[network]:[id]/credentialStatus?(revocationNonce=value)&[contractAddress=[chainID]:[contractAddress]]&(state=issuerState)`
133
+ id: `${issuer.string()}/credentialStatus?${searchParams}`
134
+ };
135
+ }
136
+ default:
137
+ return credentialStatus;
138
+ }
139
+ }
54
140
  toJSON() {
55
141
  return {
56
142
  ...this,