@0xpolygonid/js-sdk 1.24.1 → 1.25.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.
- package/dist/browser/esm/index.js +138 -702
- package/dist/browser/esm/index.js.map +1 -1
- package/dist/browser/umd/index.js +136 -701
- package/dist/browser/umd/index.js.map +1 -1
- package/dist/node/cjs/circuits/auth-v2.js +6 -0
- package/dist/node/cjs/iden3comm/handlers/contract-request.js +7 -1
- package/dist/node/cjs/iden3comm/handlers/payment.js +27 -46
- package/dist/node/cjs/iden3comm/utils/contract-request.utils.js +59 -0
- package/dist/node/cjs/iden3comm/utils/index.js +1 -0
- package/dist/node/cjs/proof/proof-service.js +6 -0
- package/dist/node/cjs/storage/blockchain/onchain-zkp-verifier.js +56 -47
- package/dist/node/cjs/utils/did-helper.js +8 -3
- package/dist/node/cjs/verifiable/constants.js +1 -2
- package/dist/node/esm/circuits/auth-v2.js +6 -0
- package/dist/node/esm/iden3comm/handlers/contract-request.js +7 -1
- package/dist/node/esm/iden3comm/handlers/payment.js +28 -46
- package/dist/node/esm/iden3comm/utils/contract-request.utils.js +54 -0
- package/dist/node/esm/iden3comm/utils/index.js +1 -0
- package/dist/node/esm/proof/proof-service.js +6 -0
- package/dist/node/esm/storage/blockchain/onchain-zkp-verifier.js +58 -49
- package/dist/node/esm/utils/did-helper.js +8 -3
- package/dist/node/esm/verifiable/constants.js +1 -2
- package/dist/types/circuits/auth-v2.d.ts +3 -2
- package/dist/types/iden3comm/handlers/payment.d.ts +5 -9
- package/dist/types/iden3comm/types/protocol/payment.d.ts +6 -5
- package/dist/types/iden3comm/utils/contract-request.utils.d.ts +20 -0
- package/dist/types/iden3comm/utils/index.d.ts +1 -0
- package/dist/types/proof/proof-service.d.ts +10 -0
- package/dist/types/storage/blockchain/onchain-zkp-verifier.d.ts +2 -0
- package/dist/types/verifiable/constants.d.ts +2 -3
- package/package.json +1 -1
|
@@ -92,5 +92,11 @@ class AuthV2PubSignals {
|
|
|
92
92
|
this.GISTRoot = js_merkletree_1.Hash.fromString(sVals[2]);
|
|
93
93
|
return this;
|
|
94
94
|
}
|
|
95
|
+
getStatesInfo() {
|
|
96
|
+
return {
|
|
97
|
+
states: [],
|
|
98
|
+
gists: [{ id: this.userID, root: this.GISTRoot }]
|
|
99
|
+
};
|
|
100
|
+
}
|
|
95
101
|
}
|
|
96
102
|
exports.AuthV2PubSignals = AuthV2PubSignals;
|
|
@@ -30,6 +30,7 @@ class ContractRequestHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
30
30
|
this._proofService = _proofService;
|
|
31
31
|
this._zkpVerifier = _zkpVerifier;
|
|
32
32
|
this._supportedCircuits = [
|
|
33
|
+
models_1.CircuitId.AuthV2,
|
|
33
34
|
models_1.CircuitId.AtomicQueryMTPV2OnChain,
|
|
34
35
|
models_1.CircuitId.AtomicQuerySigV2OnChain,
|
|
35
36
|
models_1.CircuitId.AtomicQueryV3OnChain
|
|
@@ -60,7 +61,12 @@ class ContractRequestHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
60
61
|
throw new Error(`Invalid chain id ${chain_id}`);
|
|
61
62
|
}
|
|
62
63
|
const verifierDid = message.from ? js_iden3_core_1.DID.parse(message.from) : undefined;
|
|
63
|
-
const
|
|
64
|
+
const { scope = [] } = message.body;
|
|
65
|
+
const zkpResponses = await (0, common_1.processZeroKnowledgeProofRequests)(did, scope, verifierDid, this._proofService, {
|
|
66
|
+
ethSigner,
|
|
67
|
+
challenge,
|
|
68
|
+
supportedCircuits: this._supportedCircuits
|
|
69
|
+
});
|
|
64
70
|
const methodId = message.body.transaction_data.method_id.replace('0x', '');
|
|
65
71
|
switch (methodId) {
|
|
66
72
|
case storage_1.FunctionSignatures.SubmitZKPResponseV2:
|
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.PaymentHandler = exports.createPayment = exports.
|
|
26
|
+
exports.PaymentHandler = exports.createPayment = exports.verifyEIP712TypedData = exports.createPaymentRequest = void 0;
|
|
27
27
|
const constants_1 = require("../constants");
|
|
28
28
|
const constants_2 = require("../constants");
|
|
29
29
|
const js_iden3_core_1 = require("@iden3/js-iden3-core");
|
|
@@ -63,11 +63,10 @@ function createPaymentRequest(sender, receiver, agent, payments, opts) {
|
|
|
63
63
|
}
|
|
64
64
|
exports.createPaymentRequest = createPaymentRequest;
|
|
65
65
|
async function verifyEIP712TypedData(data, resolver) {
|
|
66
|
-
const convertedAmount = await convertPaymentAmount(data.amount, data.currency);
|
|
67
66
|
const paymentData = data.type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
68
67
|
? {
|
|
69
68
|
recipient: data.recipient,
|
|
70
|
-
amount:
|
|
69
|
+
amount: data.amount,
|
|
71
70
|
expirationDate: (0, js_iden3_core_1.getUnixTimestamp)(new Date(data.expirationDate)),
|
|
72
71
|
nonce: data.nonce,
|
|
73
72
|
metadata: '0x'
|
|
@@ -75,7 +74,7 @@ async function verifyEIP712TypedData(data, resolver) {
|
|
|
75
74
|
: {
|
|
76
75
|
tokenAddress: data.tokenAddress,
|
|
77
76
|
recipient: data.recipient,
|
|
78
|
-
amount:
|
|
77
|
+
amount: data.amount,
|
|
79
78
|
expirationDate: (0, js_iden3_core_1.getUnixTimestamp)(new Date(data.expirationDate)),
|
|
80
79
|
nonce: data.nonce,
|
|
81
80
|
metadata: '0x'
|
|
@@ -100,31 +99,6 @@ async function verifyEIP712TypedData(data, resolver) {
|
|
|
100
99
|
throw new Error(`failed request. no matching verificationMethod`);
|
|
101
100
|
}
|
|
102
101
|
exports.verifyEIP712TypedData = verifyEIP712TypedData;
|
|
103
|
-
async function convertPaymentAmount(amount, currency) {
|
|
104
|
-
let convertedAmount = 0n;
|
|
105
|
-
switch (currency) {
|
|
106
|
-
case verifiable_1.SupportedCurrencies.ETH:
|
|
107
|
-
case verifiable_1.SupportedCurrencies.POL:
|
|
108
|
-
case verifiable_1.SupportedCurrencies.MATIC:
|
|
109
|
-
convertedAmount = (0, ethers_1.parseEther)(amount.toString());
|
|
110
|
-
break;
|
|
111
|
-
case verifiable_1.SupportedCurrencies.ETH_WEI:
|
|
112
|
-
convertedAmount = (0, ethers_1.parseUnits)(amount.toString(), 'wei');
|
|
113
|
-
break;
|
|
114
|
-
case verifiable_1.SupportedCurrencies.ETH_GWEI:
|
|
115
|
-
convertedAmount = (0, ethers_1.parseUnits)(amount.toString(), 'gwei');
|
|
116
|
-
break;
|
|
117
|
-
case verifiable_1.SupportedCurrencies.USDC:
|
|
118
|
-
case verifiable_1.SupportedCurrencies.USDT: {
|
|
119
|
-
convertedAmount = (0, ethers_1.parseUnits)(amount.toString(), 6);
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
default:
|
|
123
|
-
throw new Error(`failed request. unsupported currency ${currency}`);
|
|
124
|
-
}
|
|
125
|
-
return convertedAmount;
|
|
126
|
-
}
|
|
127
|
-
exports.convertPaymentAmount = convertPaymentAmount;
|
|
128
102
|
/**
|
|
129
103
|
* @beta
|
|
130
104
|
* createPayment is a function to create protocol payment message
|
|
@@ -312,35 +286,38 @@ class PaymentHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
312
286
|
for (let i = 0; i < payments.length; i++) {
|
|
313
287
|
const { credentials, description } = payments[i];
|
|
314
288
|
const dataArr = [];
|
|
315
|
-
for (let j = 0; j < payments[i].
|
|
316
|
-
const {
|
|
289
|
+
for (let j = 0; j < payments[i].options.length; j++) {
|
|
290
|
+
const { nonce, amount, chainId, optionId, expirationDate } = payments[i].options[j];
|
|
317
291
|
const multiChainConfig = this._params.multiChainPaymentConfig?.find((c) => c.chainId === chainId);
|
|
318
292
|
if (!multiChainConfig) {
|
|
319
293
|
throw new Error(`failed request. no config for chain ${chainId}`);
|
|
320
294
|
}
|
|
321
|
-
const { recipient,
|
|
322
|
-
const
|
|
323
|
-
if (
|
|
324
|
-
throw new Error(`failed request. no
|
|
295
|
+
const { recipient, paymentRails, options } = multiChainConfig;
|
|
296
|
+
const option = options.find((t) => t.id === optionId);
|
|
297
|
+
if (!option) {
|
|
298
|
+
throw new Error(`failed request. no option for id ${optionId}`);
|
|
299
|
+
}
|
|
300
|
+
if (option.type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1 &&
|
|
301
|
+
!option.contractAddress) {
|
|
302
|
+
throw new Error(`failed request. no token address for option id ${optionId}`);
|
|
325
303
|
}
|
|
326
304
|
const expirationDateRequired = expirationDate ?? new Date(new Date().setHours(new Date().getHours() + 1));
|
|
327
|
-
const typeUrl = `https://schema.iden3.io/core/json/${type}.json`;
|
|
305
|
+
const typeUrl = `https://schema.iden3.io/core/json/${option.type}.json`;
|
|
328
306
|
const typesFetchResult = await fetch(typeUrl);
|
|
329
307
|
const types = await typesFetchResult.json();
|
|
330
308
|
delete types.EIP712Domain;
|
|
331
|
-
const
|
|
332
|
-
const paymentData = type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
309
|
+
const paymentData = option.type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
333
310
|
? {
|
|
334
311
|
recipient,
|
|
335
|
-
amount:
|
|
312
|
+
amount: amount,
|
|
336
313
|
expirationDate: (0, js_iden3_core_1.getUnixTimestamp)(expirationDateRequired),
|
|
337
314
|
nonce,
|
|
338
315
|
metadata: '0x'
|
|
339
316
|
}
|
|
340
317
|
: {
|
|
341
|
-
tokenAddress,
|
|
318
|
+
tokenAddress: option.contractAddress,
|
|
342
319
|
recipient,
|
|
343
|
-
amount:
|
|
320
|
+
amount: amount,
|
|
344
321
|
expirationDate: (0, js_iden3_core_1.getUnixTimestamp)(expirationDateRequired),
|
|
345
322
|
nonce,
|
|
346
323
|
metadata: '0x'
|
|
@@ -349,7 +326,7 @@ class PaymentHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
349
326
|
name: 'MCPayment',
|
|
350
327
|
version: '1.0.0',
|
|
351
328
|
chainId,
|
|
352
|
-
verifyingContract:
|
|
329
|
+
verifyingContract: paymentRails
|
|
353
330
|
};
|
|
354
331
|
const signature = await signer.signTypedData(domain, types, paymentData);
|
|
355
332
|
const proof = [
|
|
@@ -369,20 +346,24 @@ class PaymentHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
369
346
|
const d = {
|
|
370
347
|
type: verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1,
|
|
371
348
|
'@context': [
|
|
372
|
-
`https://schema.iden3.io/core/jsonld/payment.jsonld#${type}`,
|
|
349
|
+
`https://schema.iden3.io/core/jsonld/payment.jsonld#${option.type}`,
|
|
373
350
|
'https://w3id.org/security/suites/eip712sig-2021/v1'
|
|
374
351
|
],
|
|
375
352
|
recipient,
|
|
376
353
|
amount: amount.toString(),
|
|
377
|
-
currency,
|
|
378
354
|
expirationDate: expirationDateRequired.toISOString(),
|
|
379
355
|
nonce: nonce.toString(),
|
|
380
356
|
metadata: '0x',
|
|
381
357
|
proof
|
|
382
358
|
};
|
|
383
|
-
dataArr.push(type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
359
|
+
dataArr.push(option.type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
384
360
|
? d
|
|
385
|
-
: {
|
|
361
|
+
: {
|
|
362
|
+
...d,
|
|
363
|
+
type: option.type,
|
|
364
|
+
tokenAddress: option.contractAddress || '',
|
|
365
|
+
features: option.features || []
|
|
366
|
+
});
|
|
386
367
|
}
|
|
387
368
|
paymentRequestInfo.push({
|
|
388
369
|
data: dataArr,
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prepareAuthV2ZeroKnowledgeResponse = exports.calculateRequestIdForCircuit = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
const circuits_1 = require("../../circuits");
|
|
7
|
+
const js_crypto_1 = require("@iden3/js-crypto");
|
|
8
|
+
/**
|
|
9
|
+
* @beta
|
|
10
|
+
* Retrieves the request ID from circuit string.
|
|
11
|
+
* CircuitId.AuthV2 - 940499666
|
|
12
|
+
* @returns The request ID.
|
|
13
|
+
*/
|
|
14
|
+
function calculateRequestIdForCircuit(circuitId) {
|
|
15
|
+
const circuitHash = (0, ethers_1.keccak256)(utils_1.byteEncoder.encode(circuitId));
|
|
16
|
+
const dataView = new DataView(js_crypto_1.Hex.decodeString(circuitHash.replace('0x', '')).buffer);
|
|
17
|
+
const id = dataView.getUint32(0);
|
|
18
|
+
return id;
|
|
19
|
+
}
|
|
20
|
+
exports.calculateRequestIdForCircuit = calculateRequestIdForCircuit;
|
|
21
|
+
/**
|
|
22
|
+
* Prepares the zero-knowledge proof response for the AuthV2 circuit.
|
|
23
|
+
* @beta
|
|
24
|
+
* @param address - The address associated with the request.
|
|
25
|
+
* @param senderDid - The sender's decentralized identifier (DID).
|
|
26
|
+
* @param proofService - The proof service used to generate the proof.
|
|
27
|
+
* @returns A promise that resolves to an array of ZeroKnowledgeProofResponse objects.
|
|
28
|
+
*/
|
|
29
|
+
async function prepareAuthV2ZeroKnowledgeResponse(address, senderDid, proofService) {
|
|
30
|
+
const circuitId = circuits_1.CircuitId.AuthV2;
|
|
31
|
+
// this is now hardcoded calculated value for 'authV2' that can be changed in the future.
|
|
32
|
+
const id = 940499666;
|
|
33
|
+
if ((0, utils_1.isEthereumIdentity)(senderDid)) {
|
|
34
|
+
return [
|
|
35
|
+
{
|
|
36
|
+
circuitId,
|
|
37
|
+
id,
|
|
38
|
+
pub_signals: [],
|
|
39
|
+
proof: {
|
|
40
|
+
pi_a: [],
|
|
41
|
+
pi_b: [],
|
|
42
|
+
pi_c: [],
|
|
43
|
+
protocol: 'groth16'
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
const hash = Uint8Array.from([...(0, utils_1.hexToBytes)(address), ...new Uint8Array(12)]).reverse();
|
|
49
|
+
const { proof, pub_signals } = await proofService.generateAuthV2Proof(hash, senderDid);
|
|
50
|
+
return [
|
|
51
|
+
{
|
|
52
|
+
circuitId,
|
|
53
|
+
id,
|
|
54
|
+
pub_signals,
|
|
55
|
+
proof
|
|
56
|
+
}
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
exports.prepareAuthV2ZeroKnowledgeResponse = prepareAuthV2ZeroKnowledgeResponse;
|
|
@@ -17,4 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./envelope"), exports);
|
|
18
18
|
__exportStar(require("./message"), exports);
|
|
19
19
|
__exportStar(require("./did"), exports);
|
|
20
|
+
__exportStar(require("./contract-request.utils"), exports);
|
|
20
21
|
__exportStar(require("./accept-profile"), exports);
|
|
@@ -216,6 +216,12 @@ class ProofService {
|
|
|
216
216
|
authInputs.gistProof = gistProof;
|
|
217
217
|
return authInputs.inputsMarshal();
|
|
218
218
|
}
|
|
219
|
+
/** {@inheritdoc IProofService.generateAuthV2Proof} */
|
|
220
|
+
async generateAuthV2Proof(challenge, did) {
|
|
221
|
+
const authInputs = await this.generateAuthV2Inputs(challenge, did, circuits_1.CircuitId.AuthV2);
|
|
222
|
+
const zkProof = await this._prover.generate(authInputs, circuits_1.CircuitId.AuthV2);
|
|
223
|
+
return zkProof;
|
|
224
|
+
}
|
|
219
225
|
async verifyState(circuitId, pubSignals, opts = {
|
|
220
226
|
acceptedStateTransitionDelay: iden3comm_1.PROTOCOL_CONSTANTS.DEFAULT_AUTH_VERIFY_DELAY
|
|
221
227
|
}) {
|
|
@@ -11,6 +11,7 @@ const js_iden3_core_1 = require("@iden3/js-iden3-core");
|
|
|
11
11
|
const circuits_1 = require("../../circuits");
|
|
12
12
|
const utils_1 = require("../../utils");
|
|
13
13
|
const js_crypto_1 = require("@iden3/js-crypto");
|
|
14
|
+
const js_merkletree_1 = require("@iden3/js-merkletree");
|
|
14
15
|
const maxGasLimit = 10000000n;
|
|
15
16
|
/**
|
|
16
17
|
* Supported function signature for SubmitZKPResponse
|
|
@@ -157,54 +158,60 @@ class OnChainZKPVerifier {
|
|
|
157
158
|
const { txnHash } = await transactionService.sendTransactionRequest(ethSigner, request);
|
|
158
159
|
return new Map().set(txnHash, zkProofResponses);
|
|
159
160
|
}
|
|
161
|
+
static getCrossChainResolvers(source, txDataChainId, type, didResolverUrl) {
|
|
162
|
+
return [
|
|
163
|
+
...new Set(source.map((info) => JSON.stringify({
|
|
164
|
+
id: info.id.string(),
|
|
165
|
+
[type]: type === 'gist' ? info.root?.string() : info.state?.string()
|
|
166
|
+
})))
|
|
167
|
+
].reduce((acc, s) => {
|
|
168
|
+
const info = JSON.parse(s);
|
|
169
|
+
const id = js_iden3_core_1.Id.fromString(info.id);
|
|
170
|
+
const chainId = (0, js_iden3_core_1.chainIDfromDID)(js_iden3_core_1.DID.parseFromId(id));
|
|
171
|
+
if (txDataChainId === chainId) {
|
|
172
|
+
return acc;
|
|
173
|
+
}
|
|
174
|
+
const promise = this.resolveDidDocumentEip712MessageAndSignature(js_iden3_core_1.DID.parseFromId(js_iden3_core_1.Id.fromString(info.id)), didResolverUrl, {
|
|
175
|
+
[type]: js_merkletree_1.Hash.fromString(info[type])
|
|
176
|
+
});
|
|
177
|
+
return [...acc, promise];
|
|
178
|
+
}, []);
|
|
179
|
+
}
|
|
160
180
|
static async prepareTxArgsSubmitV2(resolverUrl, txData, zkProofResponses) {
|
|
161
181
|
if (txData.method_id.replace('0x', '') !== FunctionSignatures.SubmitZKPResponseV2) {
|
|
162
182
|
throw new Error(`submit cross chain doesn't implement requested method id. Only '0x${FunctionSignatures.SubmitZKPResponseV2}' is supported.`);
|
|
163
183
|
}
|
|
164
|
-
const
|
|
165
|
-
const
|
|
184
|
+
const gistUpdates = [];
|
|
185
|
+
const stateUpdates = [];
|
|
166
186
|
const payload = [];
|
|
167
|
-
|
|
168
|
-
const gistUpdateResolutionsPending = [];
|
|
169
|
-
const stateUpdateResolutionsPending = [];
|
|
187
|
+
const emptyBytes = '0x';
|
|
170
188
|
for (const zkProof of zkProofResponses) {
|
|
171
|
-
const
|
|
172
|
-
const
|
|
173
|
-
if (!this._supportedCircuits.includes(
|
|
189
|
+
const { id: requestId, pub_signals: inputs } = zkProof;
|
|
190
|
+
const proofCircuitId = zkProof.circuitId;
|
|
191
|
+
if (!this._supportedCircuits.includes(proofCircuitId)) {
|
|
174
192
|
throw new Error(`Circuit ${zkProof.circuitId} not supported by OnChainZKPVerifier`);
|
|
175
193
|
}
|
|
194
|
+
if (inputs.length === 0) {
|
|
195
|
+
payload.push({
|
|
196
|
+
requestId: requestId,
|
|
197
|
+
zkProof: emptyBytes,
|
|
198
|
+
data: emptyBytes
|
|
199
|
+
});
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
176
202
|
const zkProofEncoded = this.packZkpProof(inputs, zkProof.proof.pi_a.slice(0, 2), [
|
|
177
203
|
[zkProof.proof.pi_b[0][1], zkProof.proof.pi_b[0][0]],
|
|
178
204
|
[zkProof.proof.pi_b[1][1], zkProof.proof.pi_b[1][0]]
|
|
179
205
|
], zkProof.proof.pi_c.slice(0, 2));
|
|
180
|
-
const stateInfo = this.getOnChainGistRootStatePubSignals(
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (gistResolutionPending) {
|
|
185
|
-
continue;
|
|
186
|
-
}
|
|
187
|
-
gistUpdateResolutionsPending.push(JSON.stringify(gist));
|
|
188
|
-
gistUpdateResolutions.push(this.resolveDidDocumentEip712MessageAndSignature(js_iden3_core_1.DID.parseFromId(gist.id), resolverUrl, {
|
|
189
|
-
gist: gist.root
|
|
190
|
-
}));
|
|
191
|
-
}
|
|
192
|
-
const stateUpdateResolutions = [];
|
|
193
|
-
for (const state of stateInfo.states) {
|
|
194
|
-
const stateResolutionPending = stateUpdateResolutionsPending.find((s) => s == JSON.stringify(state));
|
|
195
|
-
if (stateResolutionPending) {
|
|
196
|
-
continue;
|
|
197
|
-
}
|
|
198
|
-
stateUpdateResolutionsPending.push(JSON.stringify(state));
|
|
199
|
-
stateUpdateResolutions.push(this.resolveDidDocumentEip712MessageAndSignature(js_iden3_core_1.DID.parseFromId(state.id), resolverUrl, {
|
|
200
|
-
state: state.state
|
|
201
|
-
}));
|
|
202
|
-
}
|
|
206
|
+
const stateInfo = this.getOnChainGistRootStatePubSignals(proofCircuitId, inputs);
|
|
207
|
+
const chainId = txData.chain_id;
|
|
208
|
+
const gistUpdateResolutions = this.getCrossChainResolvers(stateInfo.gists, chainId, 'gist', resolverUrl);
|
|
209
|
+
const stateUpdateResolutions = this.getCrossChainResolvers(stateInfo.states, chainId, 'state', resolverUrl);
|
|
203
210
|
if (gistUpdateResolutions.length > 0) {
|
|
204
|
-
|
|
211
|
+
gistUpdates.push(...(await Promise.all(gistUpdateResolutions)));
|
|
205
212
|
}
|
|
206
213
|
if (stateUpdateResolutions.length > 0) {
|
|
207
|
-
|
|
214
|
+
stateUpdates.push(...(await Promise.all(stateUpdateResolutions)));
|
|
208
215
|
}
|
|
209
216
|
const metadataArr = [];
|
|
210
217
|
if (zkProof.vp) {
|
|
@@ -220,14 +227,16 @@ class OnChainZKPVerifier {
|
|
|
220
227
|
});
|
|
221
228
|
}
|
|
222
229
|
}
|
|
223
|
-
const metadata = metadataArr.length ? this.packMetadatas(metadataArr) :
|
|
230
|
+
const metadata = metadataArr.length ? this.packMetadatas(metadataArr) : emptyBytes;
|
|
224
231
|
payload.push({
|
|
225
|
-
requestId:
|
|
232
|
+
requestId: requestId,
|
|
226
233
|
zkProof: zkProofEncoded,
|
|
227
234
|
data: metadata
|
|
228
235
|
});
|
|
229
236
|
}
|
|
230
|
-
const crossChainProofs =
|
|
237
|
+
const crossChainProofs = gistUpdates.length || stateUpdates.length
|
|
238
|
+
? this.packCrossChainProofs(gistUpdates, stateUpdates)
|
|
239
|
+
: emptyBytes;
|
|
231
240
|
return [payload, crossChainProofs];
|
|
232
241
|
}
|
|
233
242
|
async prepareTxArgsSubmitV2(txData, zkProofResponses) {
|
|
@@ -285,18 +294,11 @@ class OnChainZKPVerifier {
|
|
|
285
294
|
return new ethers_1.ethers.AbiCoder().encode(['tuple(' + 'string key,' + 'bytes value' + ')[]'], [metas]);
|
|
286
295
|
}
|
|
287
296
|
static getOnChainGistRootStatePubSignals(onChainCircuitId, inputs) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
atomicQueryPubSignals = new circuits_1.AtomicQueryMTPV2OnChainPubSignals();
|
|
292
|
-
break;
|
|
293
|
-
case circuits_1.CircuitId.AtomicQuerySigV2OnChain:
|
|
294
|
-
atomicQueryPubSignals = new circuits_1.AtomicQuerySigV2OnChainPubSignals();
|
|
295
|
-
break;
|
|
296
|
-
case circuits_1.CircuitId.AtomicQueryV3OnChain:
|
|
297
|
-
atomicQueryPubSignals = new circuits_1.AtomicQueryV3OnChainPubSignals();
|
|
298
|
-
break;
|
|
297
|
+
const PubSignals = this._supportedCircuitsPubSignalsMap[onChainCircuitId];
|
|
298
|
+
if (!PubSignals) {
|
|
299
|
+
throw new Error(`Circuit ${onChainCircuitId} not supported by OnChainZKPVerifier`);
|
|
299
300
|
}
|
|
301
|
+
const atomicQueryPubSignals = new PubSignals();
|
|
300
302
|
const encodedInputs = utils_1.byteEncoder.encode(JSON.stringify(inputs));
|
|
301
303
|
atomicQueryPubSignals.pubSignalsUnmarshal(encodedInputs);
|
|
302
304
|
return atomicQueryPubSignals.getStatesInfo();
|
|
@@ -339,7 +341,14 @@ exports.OnChainZKPVerifier = OnChainZKPVerifier;
|
|
|
339
341
|
* supported circuits
|
|
340
342
|
*/
|
|
341
343
|
OnChainZKPVerifier._supportedCircuits = [
|
|
344
|
+
circuits_1.CircuitId.AuthV2,
|
|
342
345
|
circuits_1.CircuitId.AtomicQueryMTPV2OnChain,
|
|
343
346
|
circuits_1.CircuitId.AtomicQuerySigV2OnChain,
|
|
344
347
|
circuits_1.CircuitId.AtomicQueryV3OnChain
|
|
345
348
|
];
|
|
349
|
+
OnChainZKPVerifier._supportedCircuitsPubSignalsMap = {
|
|
350
|
+
[circuits_1.CircuitId.AtomicQueryMTPV2OnChain]: circuits_1.AtomicQueryMTPV2OnChainPubSignals,
|
|
351
|
+
[circuits_1.CircuitId.AtomicQuerySigV2OnChain]: circuits_1.AtomicQuerySigV2OnChainPubSignals,
|
|
352
|
+
[circuits_1.CircuitId.AtomicQueryV3OnChain]: circuits_1.AtomicQueryV3OnChainPubSignals,
|
|
353
|
+
[circuits_1.CircuitId.AuthV2]: circuits_1.AuthV2PubSignals
|
|
354
|
+
};
|
|
@@ -105,9 +105,14 @@ const resolveDidDocument = async (did, resolverUrl, opts) => {
|
|
|
105
105
|
if (opts?.gist) {
|
|
106
106
|
url += `${url.includes('?') ? '&' : '?'}gist=${opts.gist.hex()}`;
|
|
107
107
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
try {
|
|
109
|
+
const resp = await fetch(url);
|
|
110
|
+
const data = await resp.json();
|
|
111
|
+
return data;
|
|
112
|
+
}
|
|
113
|
+
catch (e) {
|
|
114
|
+
throw new Error(`Failed to resolve DID document for ${did} ${e}`);
|
|
115
|
+
}
|
|
111
116
|
};
|
|
112
117
|
exports.resolveDidDocument = resolveDidDocument;
|
|
113
118
|
const buildDIDFromEthPubKey = (didType, pubKeyEth) => {
|
|
@@ -149,6 +149,7 @@ var SupportedPaymentProofType;
|
|
|
149
149
|
/**
|
|
150
150
|
* Media types for Payment supported currencies
|
|
151
151
|
* @beta
|
|
152
|
+
* @deprecated
|
|
152
153
|
* @enum {string}
|
|
153
154
|
*/
|
|
154
155
|
var SupportedCurrencies;
|
|
@@ -158,8 +159,6 @@ var SupportedCurrencies;
|
|
|
158
159
|
SupportedCurrencies["ETH_GWEI"] = "ETHGWEI";
|
|
159
160
|
SupportedCurrencies["MATIC"] = "MATIC";
|
|
160
161
|
SupportedCurrencies["POL"] = "POL";
|
|
161
|
-
SupportedCurrencies["USDT"] = "USDT";
|
|
162
|
-
SupportedCurrencies["USDC"] = "USDC";
|
|
163
162
|
})(SupportedCurrencies = exports.SupportedCurrencies || (exports.SupportedCurrencies = {}));
|
|
164
163
|
/**
|
|
165
164
|
* Supported features for payment-request
|
|
@@ -27,6 +27,7 @@ export class ContractRequestHandler extends AbstractMessageHandler {
|
|
|
27
27
|
this._proofService = _proofService;
|
|
28
28
|
this._zkpVerifier = _zkpVerifier;
|
|
29
29
|
this._supportedCircuits = [
|
|
30
|
+
CircuitId.AuthV2,
|
|
30
31
|
CircuitId.AtomicQueryMTPV2OnChain,
|
|
31
32
|
CircuitId.AtomicQuerySigV2OnChain,
|
|
32
33
|
CircuitId.AtomicQueryV3OnChain
|
|
@@ -57,7 +58,12 @@ export class ContractRequestHandler extends AbstractMessageHandler {
|
|
|
57
58
|
throw new Error(`Invalid chain id ${chain_id}`);
|
|
58
59
|
}
|
|
59
60
|
const verifierDid = message.from ? DID.parse(message.from) : undefined;
|
|
60
|
-
const
|
|
61
|
+
const { scope = [] } = message.body;
|
|
62
|
+
const zkpResponses = await processZeroKnowledgeProofRequests(did, scope, verifierDid, this._proofService, {
|
|
63
|
+
ethSigner,
|
|
64
|
+
challenge,
|
|
65
|
+
supportedCircuits: this._supportedCircuits
|
|
66
|
+
});
|
|
61
67
|
const methodId = message.body.transaction_data.method_id.replace('0x', '');
|
|
62
68
|
switch (methodId) {
|
|
63
69
|
case FunctionSignatures.SubmitZKPResponseV2:
|
|
@@ -5,8 +5,8 @@ import * as uuid from 'uuid';
|
|
|
5
5
|
import { proving } from '@iden3/js-jwz';
|
|
6
6
|
import { byteEncoder } from '../../utils';
|
|
7
7
|
import { AbstractMessageHandler } from './message-handler';
|
|
8
|
-
import { PaymentFeatures, PaymentRequestDataType, PaymentType,
|
|
9
|
-
import { ethers
|
|
8
|
+
import { PaymentFeatures, PaymentRequestDataType, PaymentType, SupportedPaymentProofType } from '../../verifiable';
|
|
9
|
+
import { ethers } from 'ethers';
|
|
10
10
|
import { verifyExpiresTime } from './common';
|
|
11
11
|
/**
|
|
12
12
|
* @beta
|
|
@@ -36,11 +36,10 @@ export function createPaymentRequest(sender, receiver, agent, payments, opts) {
|
|
|
36
36
|
return request;
|
|
37
37
|
}
|
|
38
38
|
export async function verifyEIP712TypedData(data, resolver) {
|
|
39
|
-
const convertedAmount = await convertPaymentAmount(data.amount, data.currency);
|
|
40
39
|
const paymentData = data.type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
41
40
|
? {
|
|
42
41
|
recipient: data.recipient,
|
|
43
|
-
amount:
|
|
42
|
+
amount: data.amount,
|
|
44
43
|
expirationDate: getUnixTimestamp(new Date(data.expirationDate)),
|
|
45
44
|
nonce: data.nonce,
|
|
46
45
|
metadata: '0x'
|
|
@@ -48,7 +47,7 @@ export async function verifyEIP712TypedData(data, resolver) {
|
|
|
48
47
|
: {
|
|
49
48
|
tokenAddress: data.tokenAddress,
|
|
50
49
|
recipient: data.recipient,
|
|
51
|
-
amount:
|
|
50
|
+
amount: data.amount,
|
|
52
51
|
expirationDate: getUnixTimestamp(new Date(data.expirationDate)),
|
|
53
52
|
nonce: data.nonce,
|
|
54
53
|
metadata: '0x'
|
|
@@ -72,30 +71,6 @@ export async function verifyEIP712TypedData(data, resolver) {
|
|
|
72
71
|
}
|
|
73
72
|
throw new Error(`failed request. no matching verificationMethod`);
|
|
74
73
|
}
|
|
75
|
-
export async function convertPaymentAmount(amount, currency) {
|
|
76
|
-
let convertedAmount = 0n;
|
|
77
|
-
switch (currency) {
|
|
78
|
-
case SupportedCurrencies.ETH:
|
|
79
|
-
case SupportedCurrencies.POL:
|
|
80
|
-
case SupportedCurrencies.MATIC:
|
|
81
|
-
convertedAmount = parseEther(amount.toString());
|
|
82
|
-
break;
|
|
83
|
-
case SupportedCurrencies.ETH_WEI:
|
|
84
|
-
convertedAmount = parseUnits(amount.toString(), 'wei');
|
|
85
|
-
break;
|
|
86
|
-
case SupportedCurrencies.ETH_GWEI:
|
|
87
|
-
convertedAmount = parseUnits(amount.toString(), 'gwei');
|
|
88
|
-
break;
|
|
89
|
-
case SupportedCurrencies.USDC:
|
|
90
|
-
case SupportedCurrencies.USDT: {
|
|
91
|
-
convertedAmount = parseUnits(amount.toString(), 6);
|
|
92
|
-
break;
|
|
93
|
-
}
|
|
94
|
-
default:
|
|
95
|
-
throw new Error(`failed request. unsupported currency ${currency}`);
|
|
96
|
-
}
|
|
97
|
-
return convertedAmount;
|
|
98
|
-
}
|
|
99
74
|
/**
|
|
100
75
|
* @beta
|
|
101
76
|
* createPayment is a function to create protocol payment message
|
|
@@ -282,35 +257,38 @@ export class PaymentHandler extends AbstractMessageHandler {
|
|
|
282
257
|
for (let i = 0; i < payments.length; i++) {
|
|
283
258
|
const { credentials, description } = payments[i];
|
|
284
259
|
const dataArr = [];
|
|
285
|
-
for (let j = 0; j < payments[i].
|
|
286
|
-
const {
|
|
260
|
+
for (let j = 0; j < payments[i].options.length; j++) {
|
|
261
|
+
const { nonce, amount, chainId, optionId, expirationDate } = payments[i].options[j];
|
|
287
262
|
const multiChainConfig = this._params.multiChainPaymentConfig?.find((c) => c.chainId === chainId);
|
|
288
263
|
if (!multiChainConfig) {
|
|
289
264
|
throw new Error(`failed request. no config for chain ${chainId}`);
|
|
290
265
|
}
|
|
291
|
-
const { recipient,
|
|
292
|
-
const
|
|
293
|
-
if (
|
|
294
|
-
throw new Error(`failed request. no
|
|
266
|
+
const { recipient, paymentRails, options } = multiChainConfig;
|
|
267
|
+
const option = options.find((t) => t.id === optionId);
|
|
268
|
+
if (!option) {
|
|
269
|
+
throw new Error(`failed request. no option for id ${optionId}`);
|
|
270
|
+
}
|
|
271
|
+
if (option.type === PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1 &&
|
|
272
|
+
!option.contractAddress) {
|
|
273
|
+
throw new Error(`failed request. no token address for option id ${optionId}`);
|
|
295
274
|
}
|
|
296
275
|
const expirationDateRequired = expirationDate ?? new Date(new Date().setHours(new Date().getHours() + 1));
|
|
297
|
-
const typeUrl = `https://schema.iden3.io/core/json/${type}.json`;
|
|
276
|
+
const typeUrl = `https://schema.iden3.io/core/json/${option.type}.json`;
|
|
298
277
|
const typesFetchResult = await fetch(typeUrl);
|
|
299
278
|
const types = await typesFetchResult.json();
|
|
300
279
|
delete types.EIP712Domain;
|
|
301
|
-
const
|
|
302
|
-
const paymentData = type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
280
|
+
const paymentData = option.type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
303
281
|
? {
|
|
304
282
|
recipient,
|
|
305
|
-
amount:
|
|
283
|
+
amount: amount,
|
|
306
284
|
expirationDate: getUnixTimestamp(expirationDateRequired),
|
|
307
285
|
nonce,
|
|
308
286
|
metadata: '0x'
|
|
309
287
|
}
|
|
310
288
|
: {
|
|
311
|
-
tokenAddress,
|
|
289
|
+
tokenAddress: option.contractAddress,
|
|
312
290
|
recipient,
|
|
313
|
-
amount:
|
|
291
|
+
amount: amount,
|
|
314
292
|
expirationDate: getUnixTimestamp(expirationDateRequired),
|
|
315
293
|
nonce,
|
|
316
294
|
metadata: '0x'
|
|
@@ -319,7 +297,7 @@ export class PaymentHandler extends AbstractMessageHandler {
|
|
|
319
297
|
name: 'MCPayment',
|
|
320
298
|
version: '1.0.0',
|
|
321
299
|
chainId,
|
|
322
|
-
verifyingContract:
|
|
300
|
+
verifyingContract: paymentRails
|
|
323
301
|
};
|
|
324
302
|
const signature = await signer.signTypedData(domain, types, paymentData);
|
|
325
303
|
const proof = [
|
|
@@ -339,20 +317,24 @@ export class PaymentHandler extends AbstractMessageHandler {
|
|
|
339
317
|
const d = {
|
|
340
318
|
type: PaymentRequestDataType.Iden3PaymentRailsRequestV1,
|
|
341
319
|
'@context': [
|
|
342
|
-
`https://schema.iden3.io/core/jsonld/payment.jsonld#${type}`,
|
|
320
|
+
`https://schema.iden3.io/core/jsonld/payment.jsonld#${option.type}`,
|
|
343
321
|
'https://w3id.org/security/suites/eip712sig-2021/v1'
|
|
344
322
|
],
|
|
345
323
|
recipient,
|
|
346
324
|
amount: amount.toString(),
|
|
347
|
-
currency,
|
|
348
325
|
expirationDate: expirationDateRequired.toISOString(),
|
|
349
326
|
nonce: nonce.toString(),
|
|
350
327
|
metadata: '0x',
|
|
351
328
|
proof
|
|
352
329
|
};
|
|
353
|
-
dataArr.push(type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
330
|
+
dataArr.push(option.type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
354
331
|
? d
|
|
355
|
-
: {
|
|
332
|
+
: {
|
|
333
|
+
...d,
|
|
334
|
+
type: option.type,
|
|
335
|
+
tokenAddress: option.contractAddress || '',
|
|
336
|
+
features: option.features || []
|
|
337
|
+
});
|
|
356
338
|
}
|
|
357
339
|
paymentRequestInfo.push({
|
|
358
340
|
data: dataArr,
|