@0xpolygonid/js-sdk 1.24.1 → 1.26.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.
- package/dist/browser/esm/index.js +376 -748
- package/dist/browser/esm/index.js.map +1 -1
- package/dist/browser/umd/index.js +377 -747
- package/dist/browser/umd/index.js.map +1 -1
- package/dist/node/cjs/circuits/auth-v2.js +6 -0
- package/dist/node/cjs/iden3comm/constants.js +6 -1
- package/dist/node/cjs/iden3comm/handlers/auth.js +16 -24
- package/dist/node/cjs/iden3comm/handlers/contract-request.js +7 -1
- package/dist/node/cjs/iden3comm/handlers/discovery-protocol.js +198 -0
- package/dist/node/cjs/iden3comm/handlers/index.js +1 -0
- package/dist/node/cjs/iden3comm/handlers/payment.js +27 -46
- package/dist/node/cjs/iden3comm/packageManager.js +12 -0
- package/dist/node/cjs/iden3comm/packers/jws.js +8 -7
- package/dist/node/cjs/iden3comm/packers/plain.js +8 -2
- package/dist/node/cjs/iden3comm/packers/zkp.js +10 -12
- package/dist/node/cjs/iden3comm/types/index.js +1 -0
- package/dist/node/cjs/iden3comm/types/protocol/discovery-protocol.js +16 -0
- 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/constants.js +6 -1
- package/dist/node/esm/iden3comm/handlers/auth.js +17 -25
- package/dist/node/esm/iden3comm/handlers/contract-request.js +7 -1
- package/dist/node/esm/iden3comm/handlers/discovery-protocol.js +169 -0
- package/dist/node/esm/iden3comm/handlers/index.js +1 -0
- package/dist/node/esm/iden3comm/handlers/payment.js +28 -46
- package/dist/node/esm/iden3comm/packageManager.js +12 -0
- package/dist/node/esm/iden3comm/packers/jws.js +9 -8
- package/dist/node/esm/iden3comm/packers/plain.js +9 -3
- package/dist/node/esm/iden3comm/packers/zkp.js +11 -13
- package/dist/node/esm/iden3comm/types/index.js +1 -0
- package/dist/node/esm/iden3comm/types/protocol/discovery-protocol.js +13 -0
- 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/constants.d.ts +2 -0
- package/dist/types/iden3comm/handlers/discovery-protocol.d.ts +100 -0
- package/dist/types/iden3comm/handlers/index.d.ts +1 -0
- package/dist/types/iden3comm/handlers/payment.d.ts +5 -9
- package/dist/types/iden3comm/packageManager.d.ts +2 -0
- package/dist/types/iden3comm/packers/jws.d.ts +2 -1
- package/dist/types/iden3comm/packers/plain.d.ts +1 -0
- package/dist/types/iden3comm/packers/zkp.d.ts +3 -2
- package/dist/types/iden3comm/types/index.d.ts +1 -0
- package/dist/types/iden3comm/types/packageManager.d.ts +6 -0
- package/dist/types/iden3comm/types/protocol/discovery-protocol.d.ts +41 -0
- 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
|
@@ -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,
|
|
@@ -15,6 +15,18 @@ export class PackageManager {
|
|
|
15
15
|
constructor() {
|
|
16
16
|
this.packers = new Map();
|
|
17
17
|
}
|
|
18
|
+
/** {@inheritDoc IPackageManager.getSupportedProfiles} */
|
|
19
|
+
getSupportedProfiles() {
|
|
20
|
+
const acceptProfiles = [];
|
|
21
|
+
const mediaTypes = this.getSupportedMediaTypes();
|
|
22
|
+
for (const mediaType of mediaTypes) {
|
|
23
|
+
const p = this.packers.get(mediaType);
|
|
24
|
+
if (p) {
|
|
25
|
+
acceptProfiles.push(...p.getSupportedProfiles());
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return [...new Set(acceptProfiles)];
|
|
29
|
+
}
|
|
18
30
|
/** {@inheritDoc IPackageManager.isProfileSupported} */
|
|
19
31
|
isProfileSupported(mediaType, profile) {
|
|
20
32
|
const p = this.packers.get(mediaType);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AcceptJwsAlgorithms, MediaType, SUPPORTED_PUBLIC_KEY_TYPES } from '../constants';
|
|
1
|
+
import { AcceptJwsAlgorithms, MediaType, ProtocolVersion, SUPPORTED_PUBLIC_KEY_TYPES } from '../constants';
|
|
2
2
|
import { extractPublicKeyBytes, resolveVerificationMethods } from '../utils/did';
|
|
3
3
|
import { keyPath } from '../../kms/';
|
|
4
4
|
import { verifyJWS } from 'did-jwt';
|
|
@@ -24,6 +24,8 @@ export class JWSPacker {
|
|
|
24
24
|
constructor(_kms, _documentResolver) {
|
|
25
25
|
this._kms = _kms;
|
|
26
26
|
this._documentResolver = _documentResolver;
|
|
27
|
+
this.supportedAlgorithms = [AcceptJwsAlgorithms.ES256K, AcceptJwsAlgorithms.ES256KR];
|
|
28
|
+
this.supportedProtocolVersions = [ProtocolVersion.V1];
|
|
27
29
|
}
|
|
28
30
|
/**
|
|
29
31
|
* Packs the given payload and returns a promise that resolves to the packed data.
|
|
@@ -86,24 +88,23 @@ export class JWSPacker {
|
|
|
86
88
|
}
|
|
87
89
|
/** {@inheritDoc IPacker.getSupportedProfiles} */
|
|
88
90
|
getSupportedProfiles() {
|
|
89
|
-
return
|
|
91
|
+
return this.supportedProtocolVersions.map((v) => `${v};env=${this.mediaType()};alg=${this.supportedAlgorithms.join(',')}`);
|
|
90
92
|
}
|
|
91
93
|
/** {@inheritDoc IPacker.isProfileSupported} */
|
|
92
94
|
isProfileSupported(profile) {
|
|
93
|
-
const { env, circuits, alg } = parseAcceptProfile(profile);
|
|
95
|
+
const { protocolVersion, env, circuits, alg } = parseAcceptProfile(profile);
|
|
96
|
+
if (!this.supportedProtocolVersions.includes(protocolVersion)) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
94
99
|
if (env !== this.mediaType()) {
|
|
95
100
|
return false;
|
|
96
101
|
}
|
|
97
102
|
if (circuits) {
|
|
98
103
|
throw new Error(`Circuits are not supported for ${env} media type`);
|
|
99
104
|
}
|
|
100
|
-
const
|
|
101
|
-
const algSupported = !alg?.length || alg.some((a) => supportedAlgArr.includes(a));
|
|
105
|
+
const algSupported = !alg?.length || alg.some((a) => this.supportedAlgorithms.includes(a));
|
|
102
106
|
return algSupported;
|
|
103
107
|
}
|
|
104
|
-
getSupportedAlgorithms() {
|
|
105
|
-
return [AcceptJwsAlgorithms.ES256K, AcceptJwsAlgorithms.ES256KR];
|
|
106
|
-
}
|
|
107
108
|
async resolveDidDoc(from) {
|
|
108
109
|
let didDocument;
|
|
109
110
|
try {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MediaType } from '../constants';
|
|
1
|
+
import { MediaType, ProtocolVersion } from '../constants';
|
|
2
2
|
import { byteDecoder, byteEncoder } from '../../utils';
|
|
3
3
|
import { parseAcceptProfile } from '../utils';
|
|
4
4
|
/**
|
|
@@ -9,6 +9,9 @@ import { parseAcceptProfile } from '../utils';
|
|
|
9
9
|
* @implements implements IPacker interface
|
|
10
10
|
*/
|
|
11
11
|
export class PlainPacker {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.supportedProtocolVersions = [ProtocolVersion.V1];
|
|
14
|
+
}
|
|
12
15
|
/**
|
|
13
16
|
* Packs a basic message using the specified parameters.
|
|
14
17
|
*
|
|
@@ -52,11 +55,14 @@ export class PlainPacker {
|
|
|
52
55
|
}
|
|
53
56
|
/** {@inheritDoc IPacker.getSupportedProfiles} */
|
|
54
57
|
getSupportedProfiles() {
|
|
55
|
-
return
|
|
58
|
+
return this.supportedProtocolVersions.map((v) => `${v};env=${this.mediaType()}`);
|
|
56
59
|
}
|
|
57
60
|
/** {@inheritDoc IPacker.isProfileSupported} */
|
|
58
61
|
isProfileSupported(profile) {
|
|
59
|
-
const { env, circuits, alg } = parseAcceptProfile(profile);
|
|
62
|
+
const { protocolVersion, env, circuits, alg } = parseAcceptProfile(profile);
|
|
63
|
+
if (!this.supportedProtocolVersions.includes(protocolVersion)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
60
66
|
if (env !== this.mediaType()) {
|
|
61
67
|
return false;
|
|
62
68
|
}
|
|
@@ -3,7 +3,7 @@ import { AuthV2PubSignals, CircuitId } from '../../circuits/index';
|
|
|
3
3
|
import { BytesHelper, DID } from '@iden3/js-iden3-core';
|
|
4
4
|
import { bytesToProtocolMessage } from '../utils/envelope';
|
|
5
5
|
import { ErrNoProvingMethodAlg, ErrPackedWithUnsupportedCircuit, ErrProofIsInvalid, ErrSenderNotUsedTokenCreation, ErrStateVerificationFailed, ErrUnknownCircuitID } from '../errors';
|
|
6
|
-
import { AcceptAuthCircuits, AcceptJwzAlgorithms, MediaType } from '../constants';
|
|
6
|
+
import { AcceptAuthCircuits, AcceptJwzAlgorithms, MediaType, ProtocolVersion } from '../constants';
|
|
7
7
|
import { byteDecoder, byteEncoder } from '../../utils';
|
|
8
8
|
import { DEFAULT_AUTH_VERIFY_DELAY } from '../constants';
|
|
9
9
|
import { parseAcceptProfile } from '../utils';
|
|
@@ -78,6 +78,9 @@ export class ZKPPacker {
|
|
|
78
78
|
this.provingParamsMap = provingParamsMap;
|
|
79
79
|
this.verificationParamsMap = verificationParamsMap;
|
|
80
80
|
this._opts = _opts;
|
|
81
|
+
this.supportedProtocolVersions = [ProtocolVersion.V1];
|
|
82
|
+
this.supportedAlgorithms = [AcceptJwzAlgorithms.Groth16];
|
|
83
|
+
this.supportedCircuitIds = [AcceptAuthCircuits.AuthV2];
|
|
81
84
|
}
|
|
82
85
|
/**
|
|
83
86
|
* Packs a basic message using the specified parameters.
|
|
@@ -139,28 +142,23 @@ export class ZKPPacker {
|
|
|
139
142
|
}
|
|
140
143
|
/** {@inheritDoc IPacker.getSupportedProfiles} */
|
|
141
144
|
getSupportedProfiles() {
|
|
142
|
-
return
|
|
143
|
-
`env=${this.mediaType()}&alg=${this.getSupportedAlgorithms().join(',')}&circuitIds=${this.getSupportedCircuitIds().join(',')}`
|
|
144
|
-
];
|
|
145
|
+
return this.supportedProtocolVersions.map((v) => `${v};env=${this.mediaType()};alg=${this.supportedAlgorithms.join(',')};circuitIds=${this.supportedCircuitIds.join(',')}`);
|
|
145
146
|
}
|
|
146
147
|
/** {@inheritDoc IPacker.isProfileSupported} */
|
|
147
148
|
isProfileSupported(profile) {
|
|
148
|
-
const { env, circuits, alg } = parseAcceptProfile(profile);
|
|
149
|
+
const { protocolVersion, env, circuits, alg } = parseAcceptProfile(profile);
|
|
150
|
+
if (!this.supportedProtocolVersions.includes(protocolVersion)) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
149
153
|
if (env !== this.mediaType()) {
|
|
150
154
|
return false;
|
|
151
155
|
}
|
|
152
|
-
const supportedCircuitIds = this.
|
|
156
|
+
const supportedCircuitIds = this.supportedCircuitIds;
|
|
153
157
|
const circuitIdSupported = !circuits?.length || circuits.some((c) => supportedCircuitIds.includes(c));
|
|
154
|
-
const supportedAlgArr = this.
|
|
158
|
+
const supportedAlgArr = this.supportedAlgorithms;
|
|
155
159
|
const algSupported = !alg?.length || alg.some((a) => supportedAlgArr.includes(a));
|
|
156
160
|
return algSupported && circuitIdSupported;
|
|
157
161
|
}
|
|
158
|
-
getSupportedAlgorithms() {
|
|
159
|
-
return [AcceptJwzAlgorithms.Groth16];
|
|
160
|
-
}
|
|
161
|
-
getSupportedCircuitIds() {
|
|
162
|
-
return [AcceptAuthCircuits.AuthV2];
|
|
163
|
-
}
|
|
164
162
|
}
|
|
165
163
|
const verifySender = async (token, msg) => {
|
|
166
164
|
switch (token.circuitId) {
|
|
@@ -7,6 +7,7 @@ export * from './protocol/contract-request';
|
|
|
7
7
|
export * from './protocol/proposal-request';
|
|
8
8
|
export * from './protocol/payment';
|
|
9
9
|
export * from './protocol/accept-profile';
|
|
10
|
+
export * from './protocol/discovery-protocol';
|
|
10
11
|
export * from './packer';
|
|
11
12
|
export * from './models';
|
|
12
13
|
export * from './packageManager';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** @beta DiscoverFeatureQueryType is enum for query type fields */
|
|
2
|
+
export var DiscoverFeatureQueryType;
|
|
3
|
+
(function (DiscoverFeatureQueryType) {
|
|
4
|
+
DiscoverFeatureQueryType["FeatureType"] = "feature-type";
|
|
5
|
+
})(DiscoverFeatureQueryType || (DiscoverFeatureQueryType = {}));
|
|
6
|
+
/** @beta DiscoveryProtocolFeatureType is enum for supported feature-types */
|
|
7
|
+
export var DiscoveryProtocolFeatureType;
|
|
8
|
+
(function (DiscoveryProtocolFeatureType) {
|
|
9
|
+
DiscoveryProtocolFeatureType["Accept"] = "accept";
|
|
10
|
+
DiscoveryProtocolFeatureType["Protocol"] = "protocol";
|
|
11
|
+
DiscoveryProtocolFeatureType["GoalCode"] = "goal-code";
|
|
12
|
+
DiscoveryProtocolFeatureType["Header"] = "header";
|
|
13
|
+
})(DiscoveryProtocolFeatureType || (DiscoveryProtocolFeatureType = {}));
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { keccak256 } from 'ethers';
|
|
2
|
+
import { byteEncoder, hexToBytes, isEthereumIdentity } from '../../utils';
|
|
3
|
+
import { CircuitId } from '../../circuits';
|
|
4
|
+
import { Hex } from '@iden3/js-crypto';
|
|
5
|
+
/**
|
|
6
|
+
* @beta
|
|
7
|
+
* Retrieves the request ID from circuit string.
|
|
8
|
+
* CircuitId.AuthV2 - 940499666
|
|
9
|
+
* @returns The request ID.
|
|
10
|
+
*/
|
|
11
|
+
export function calculateRequestIdForCircuit(circuitId) {
|
|
12
|
+
const circuitHash = keccak256(byteEncoder.encode(circuitId));
|
|
13
|
+
const dataView = new DataView(Hex.decodeString(circuitHash.replace('0x', '')).buffer);
|
|
14
|
+
const id = dataView.getUint32(0);
|
|
15
|
+
return id;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Prepares the zero-knowledge proof response for the AuthV2 circuit.
|
|
19
|
+
* @beta
|
|
20
|
+
* @param address - The address associated with the request.
|
|
21
|
+
* @param senderDid - The sender's decentralized identifier (DID).
|
|
22
|
+
* @param proofService - The proof service used to generate the proof.
|
|
23
|
+
* @returns A promise that resolves to an array of ZeroKnowledgeProofResponse objects.
|
|
24
|
+
*/
|
|
25
|
+
export async function prepareAuthV2ZeroKnowledgeResponse(address, senderDid, proofService) {
|
|
26
|
+
const circuitId = CircuitId.AuthV2;
|
|
27
|
+
// this is now hardcoded calculated value for 'authV2' that can be changed in the future.
|
|
28
|
+
const id = 940499666;
|
|
29
|
+
if (isEthereumIdentity(senderDid)) {
|
|
30
|
+
return [
|
|
31
|
+
{
|
|
32
|
+
circuitId,
|
|
33
|
+
id,
|
|
34
|
+
pub_signals: [],
|
|
35
|
+
proof: {
|
|
36
|
+
pi_a: [],
|
|
37
|
+
pi_b: [],
|
|
38
|
+
pi_c: [],
|
|
39
|
+
protocol: 'groth16'
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
const hash = Uint8Array.from([...hexToBytes(address), ...new Uint8Array(12)]).reverse();
|
|
45
|
+
const { proof, pub_signals } = await proofService.generateAuthV2Proof(hash, senderDid);
|
|
46
|
+
return [
|
|
47
|
+
{
|
|
48
|
+
circuitId,
|
|
49
|
+
id,
|
|
50
|
+
pub_signals,
|
|
51
|
+
proof
|
|
52
|
+
}
|
|
53
|
+
];
|
|
54
|
+
}
|
|
@@ -213,6 +213,12 @@ export class ProofService {
|
|
|
213
213
|
authInputs.gistProof = gistProof;
|
|
214
214
|
return authInputs.inputsMarshal();
|
|
215
215
|
}
|
|
216
|
+
/** {@inheritdoc IProofService.generateAuthV2Proof} */
|
|
217
|
+
async generateAuthV2Proof(challenge, did) {
|
|
218
|
+
const authInputs = await this.generateAuthV2Inputs(challenge, did, CircuitId.AuthV2);
|
|
219
|
+
const zkProof = await this._prover.generate(authInputs, CircuitId.AuthV2);
|
|
220
|
+
return zkProof;
|
|
221
|
+
}
|
|
216
222
|
async verifyState(circuitId, pubSignals, opts = {
|
|
217
223
|
acceptedStateTransitionDelay: PROTOCOL_CONSTANTS.DEFAULT_AUTH_VERIFY_DELAY
|
|
218
224
|
}) {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { JsonRpcProvider, Contract, ethers } from 'ethers';
|
|
2
2
|
import abi from './abi/ZkpVerifier.json';
|
|
3
3
|
import { TransactionService } from '../../blockchain';
|
|
4
|
-
import { DID } from '@iden3/js-iden3-core';
|
|
5
|
-
import { AtomicQueryMTPV2OnChainPubSignals, AtomicQuerySigV2OnChainPubSignals, AtomicQueryV3OnChainPubSignals, CircuitId } from '../../circuits';
|
|
4
|
+
import { chainIDfromDID, DID, Id } from '@iden3/js-iden3-core';
|
|
5
|
+
import { AtomicQueryMTPV2OnChainPubSignals, AtomicQuerySigV2OnChainPubSignals, AtomicQueryV3OnChainPubSignals, AuthV2PubSignals, CircuitId } from '../../circuits';
|
|
6
6
|
import { byteEncoder, DIDDocumentSignature, resolveDidDocument } from '../../utils';
|
|
7
7
|
import { poseidon } from '@iden3/js-crypto';
|
|
8
|
+
import { Hash } from '@iden3/js-merkletree';
|
|
8
9
|
const maxGasLimit = 10000000n;
|
|
9
10
|
/**
|
|
10
11
|
* Supported function signature for SubmitZKPResponse
|
|
@@ -151,54 +152,60 @@ export class OnChainZKPVerifier {
|
|
|
151
152
|
const { txnHash } = await transactionService.sendTransactionRequest(ethSigner, request);
|
|
152
153
|
return new Map().set(txnHash, zkProofResponses);
|
|
153
154
|
}
|
|
155
|
+
static getCrossChainResolvers(source, txDataChainId, type, didResolverUrl) {
|
|
156
|
+
return [
|
|
157
|
+
...new Set(source.map((info) => JSON.stringify({
|
|
158
|
+
id: info.id.string(),
|
|
159
|
+
[type]: type === 'gist' ? info.root?.string() : info.state?.string()
|
|
160
|
+
})))
|
|
161
|
+
].reduce((acc, s) => {
|
|
162
|
+
const info = JSON.parse(s);
|
|
163
|
+
const id = Id.fromString(info.id);
|
|
164
|
+
const chainId = chainIDfromDID(DID.parseFromId(id));
|
|
165
|
+
if (txDataChainId === chainId) {
|
|
166
|
+
return acc;
|
|
167
|
+
}
|
|
168
|
+
const promise = this.resolveDidDocumentEip712MessageAndSignature(DID.parseFromId(Id.fromString(info.id)), didResolverUrl, {
|
|
169
|
+
[type]: Hash.fromString(info[type])
|
|
170
|
+
});
|
|
171
|
+
return [...acc, promise];
|
|
172
|
+
}, []);
|
|
173
|
+
}
|
|
154
174
|
static async prepareTxArgsSubmitV2(resolverUrl, txData, zkProofResponses) {
|
|
155
175
|
if (txData.method_id.replace('0x', '') !== FunctionSignatures.SubmitZKPResponseV2) {
|
|
156
176
|
throw new Error(`submit cross chain doesn't implement requested method id. Only '0x${FunctionSignatures.SubmitZKPResponseV2}' is supported.`);
|
|
157
177
|
}
|
|
158
|
-
const
|
|
159
|
-
const
|
|
178
|
+
const gistUpdates = [];
|
|
179
|
+
const stateUpdates = [];
|
|
160
180
|
const payload = [];
|
|
161
|
-
|
|
162
|
-
const gistUpdateResolutionsPending = [];
|
|
163
|
-
const stateUpdateResolutionsPending = [];
|
|
181
|
+
const emptyBytes = '0x';
|
|
164
182
|
for (const zkProof of zkProofResponses) {
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
if (!this._supportedCircuits.includes(
|
|
183
|
+
const { id: requestId, pub_signals: inputs } = zkProof;
|
|
184
|
+
const proofCircuitId = zkProof.circuitId;
|
|
185
|
+
if (!this._supportedCircuits.includes(proofCircuitId)) {
|
|
168
186
|
throw new Error(`Circuit ${zkProof.circuitId} not supported by OnChainZKPVerifier`);
|
|
169
187
|
}
|
|
188
|
+
if (inputs.length === 0) {
|
|
189
|
+
payload.push({
|
|
190
|
+
requestId: requestId,
|
|
191
|
+
zkProof: emptyBytes,
|
|
192
|
+
data: emptyBytes
|
|
193
|
+
});
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
170
196
|
const zkProofEncoded = this.packZkpProof(inputs, zkProof.proof.pi_a.slice(0, 2), [
|
|
171
197
|
[zkProof.proof.pi_b[0][1], zkProof.proof.pi_b[0][0]],
|
|
172
198
|
[zkProof.proof.pi_b[1][1], zkProof.proof.pi_b[1][0]]
|
|
173
199
|
], zkProof.proof.pi_c.slice(0, 2));
|
|
174
|
-
const stateInfo = this.getOnChainGistRootStatePubSignals(
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (gistResolutionPending) {
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
gistUpdateResolutionsPending.push(JSON.stringify(gist));
|
|
182
|
-
gistUpdateResolutions.push(this.resolveDidDocumentEip712MessageAndSignature(DID.parseFromId(gist.id), resolverUrl, {
|
|
183
|
-
gist: gist.root
|
|
184
|
-
}));
|
|
185
|
-
}
|
|
186
|
-
const stateUpdateResolutions = [];
|
|
187
|
-
for (const state of stateInfo.states) {
|
|
188
|
-
const stateResolutionPending = stateUpdateResolutionsPending.find((s) => s == JSON.stringify(state));
|
|
189
|
-
if (stateResolutionPending) {
|
|
190
|
-
continue;
|
|
191
|
-
}
|
|
192
|
-
stateUpdateResolutionsPending.push(JSON.stringify(state));
|
|
193
|
-
stateUpdateResolutions.push(this.resolveDidDocumentEip712MessageAndSignature(DID.parseFromId(state.id), resolverUrl, {
|
|
194
|
-
state: state.state
|
|
195
|
-
}));
|
|
196
|
-
}
|
|
200
|
+
const stateInfo = this.getOnChainGistRootStatePubSignals(proofCircuitId, inputs);
|
|
201
|
+
const chainId = txData.chain_id;
|
|
202
|
+
const gistUpdateResolutions = this.getCrossChainResolvers(stateInfo.gists, chainId, 'gist', resolverUrl);
|
|
203
|
+
const stateUpdateResolutions = this.getCrossChainResolvers(stateInfo.states, chainId, 'state', resolverUrl);
|
|
197
204
|
if (gistUpdateResolutions.length > 0) {
|
|
198
|
-
|
|
205
|
+
gistUpdates.push(...(await Promise.all(gistUpdateResolutions)));
|
|
199
206
|
}
|
|
200
207
|
if (stateUpdateResolutions.length > 0) {
|
|
201
|
-
|
|
208
|
+
stateUpdates.push(...(await Promise.all(stateUpdateResolutions)));
|
|
202
209
|
}
|
|
203
210
|
const metadataArr = [];
|
|
204
211
|
if (zkProof.vp) {
|
|
@@ -214,14 +221,16 @@ export class OnChainZKPVerifier {
|
|
|
214
221
|
});
|
|
215
222
|
}
|
|
216
223
|
}
|
|
217
|
-
const metadata = metadataArr.length ? this.packMetadatas(metadataArr) :
|
|
224
|
+
const metadata = metadataArr.length ? this.packMetadatas(metadataArr) : emptyBytes;
|
|
218
225
|
payload.push({
|
|
219
|
-
requestId:
|
|
226
|
+
requestId: requestId,
|
|
220
227
|
zkProof: zkProofEncoded,
|
|
221
228
|
data: metadata
|
|
222
229
|
});
|
|
223
230
|
}
|
|
224
|
-
const crossChainProofs =
|
|
231
|
+
const crossChainProofs = gistUpdates.length || stateUpdates.length
|
|
232
|
+
? this.packCrossChainProofs(gistUpdates, stateUpdates)
|
|
233
|
+
: emptyBytes;
|
|
225
234
|
return [payload, crossChainProofs];
|
|
226
235
|
}
|
|
227
236
|
async prepareTxArgsSubmitV2(txData, zkProofResponses) {
|
|
@@ -279,18 +288,11 @@ export class OnChainZKPVerifier {
|
|
|
279
288
|
return new ethers.AbiCoder().encode(['tuple(' + 'string key,' + 'bytes value' + ')[]'], [metas]);
|
|
280
289
|
}
|
|
281
290
|
static getOnChainGistRootStatePubSignals(onChainCircuitId, inputs) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
atomicQueryPubSignals = new AtomicQueryMTPV2OnChainPubSignals();
|
|
286
|
-
break;
|
|
287
|
-
case CircuitId.AtomicQuerySigV2OnChain:
|
|
288
|
-
atomicQueryPubSignals = new AtomicQuerySigV2OnChainPubSignals();
|
|
289
|
-
break;
|
|
290
|
-
case CircuitId.AtomicQueryV3OnChain:
|
|
291
|
-
atomicQueryPubSignals = new AtomicQueryV3OnChainPubSignals();
|
|
292
|
-
break;
|
|
291
|
+
const PubSignals = this._supportedCircuitsPubSignalsMap[onChainCircuitId];
|
|
292
|
+
if (!PubSignals) {
|
|
293
|
+
throw new Error(`Circuit ${onChainCircuitId} not supported by OnChainZKPVerifier`);
|
|
293
294
|
}
|
|
295
|
+
const atomicQueryPubSignals = new PubSignals();
|
|
294
296
|
const encodedInputs = byteEncoder.encode(JSON.stringify(inputs));
|
|
295
297
|
atomicQueryPubSignals.pubSignalsUnmarshal(encodedInputs);
|
|
296
298
|
return atomicQueryPubSignals.getStatesInfo();
|
|
@@ -332,7 +334,14 @@ export class OnChainZKPVerifier {
|
|
|
332
334
|
* supported circuits
|
|
333
335
|
*/
|
|
334
336
|
OnChainZKPVerifier._supportedCircuits = [
|
|
337
|
+
CircuitId.AuthV2,
|
|
335
338
|
CircuitId.AtomicQueryMTPV2OnChain,
|
|
336
339
|
CircuitId.AtomicQuerySigV2OnChain,
|
|
337
340
|
CircuitId.AtomicQueryV3OnChain
|
|
338
341
|
];
|
|
342
|
+
OnChainZKPVerifier._supportedCircuitsPubSignalsMap = {
|
|
343
|
+
[CircuitId.AtomicQueryMTPV2OnChain]: AtomicQueryMTPV2OnChainPubSignals,
|
|
344
|
+
[CircuitId.AtomicQuerySigV2OnChain]: AtomicQuerySigV2OnChainPubSignals,
|
|
345
|
+
[CircuitId.AtomicQueryV3OnChain]: AtomicQueryV3OnChainPubSignals,
|
|
346
|
+
[CircuitId.AuthV2]: AuthV2PubSignals
|
|
347
|
+
};
|
|
@@ -97,9 +97,14 @@ export const resolveDidDocument = async (did, resolverUrl, opts) => {
|
|
|
97
97
|
if (opts?.gist) {
|
|
98
98
|
url += `${url.includes('?') ? '&' : '?'}gist=${opts.gist.hex()}`;
|
|
99
99
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
try {
|
|
101
|
+
const resp = await fetch(url);
|
|
102
|
+
const data = await resp.json();
|
|
103
|
+
return data;
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
throw new Error(`Failed to resolve DID document for ${did} ${e}`);
|
|
107
|
+
}
|
|
103
108
|
};
|
|
104
109
|
export const buildDIDFromEthPubKey = (didType, pubKeyEth) => {
|
|
105
110
|
// Use Keccak-256 hash function to get public key hash
|
|
@@ -146,6 +146,7 @@ export var SupportedPaymentProofType;
|
|
|
146
146
|
/**
|
|
147
147
|
* Media types for Payment supported currencies
|
|
148
148
|
* @beta
|
|
149
|
+
* @deprecated
|
|
149
150
|
* @enum {string}
|
|
150
151
|
*/
|
|
151
152
|
export var SupportedCurrencies;
|
|
@@ -155,8 +156,6 @@ export var SupportedCurrencies;
|
|
|
155
156
|
SupportedCurrencies["ETH_GWEI"] = "ETHGWEI";
|
|
156
157
|
SupportedCurrencies["MATIC"] = "MATIC";
|
|
157
158
|
SupportedCurrencies["POL"] = "POL";
|
|
158
|
-
SupportedCurrencies["USDT"] = "USDT";
|
|
159
|
-
SupportedCurrencies["USDC"] = "USDC";
|
|
160
159
|
})(SupportedCurrencies || (SupportedCurrencies = {}));
|
|
161
160
|
/**
|
|
162
161
|
* Supported features for payment-request
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Hash, Proof } from '@iden3/js-merkletree';
|
|
2
2
|
import { Claim, Id } from '@iden3/js-iden3-core';
|
|
3
3
|
import { GISTProof, TreeState } from './models';
|
|
4
|
-
import { BaseConfig } from './common';
|
|
4
|
+
import { BaseConfig, IStateInfoPubSignals, StatesInfo } from './common';
|
|
5
5
|
import { Signature } from '@iden3/js-crypto';
|
|
6
6
|
/**
|
|
7
7
|
* Auth v2 circuit representation
|
|
@@ -30,7 +30,7 @@ export declare class AuthV2Inputs extends BaseConfig {
|
|
|
30
30
|
* @public
|
|
31
31
|
* @class AuthV2PubSignals
|
|
32
32
|
*/
|
|
33
|
-
export declare class AuthV2PubSignals {
|
|
33
|
+
export declare class AuthV2PubSignals implements IStateInfoPubSignals {
|
|
34
34
|
userID: Id;
|
|
35
35
|
challenge: bigint;
|
|
36
36
|
GISTRoot: Hash;
|
|
@@ -41,4 +41,5 @@ export declare class AuthV2PubSignals {
|
|
|
41
41
|
* @returns AuthV2PubSignals
|
|
42
42
|
*/
|
|
43
43
|
pubSignalsUnmarshal(data: Uint8Array): AuthV2PubSignals;
|
|
44
|
+
getStatesInfo(): StatesInfo;
|
|
44
45
|
}
|
|
@@ -23,6 +23,8 @@ export declare const PROTOCOL_MESSAGE_TYPE: Readonly<{
|
|
|
23
23
|
PROPOSAL_MESSAGE_TYPE: "https://iden3-communication.io/credentials/0.1/proposal";
|
|
24
24
|
PAYMENT_REQUEST_MESSAGE_TYPE: "https://iden3-communication.io/credentials/0.1/payment-request";
|
|
25
25
|
PAYMENT_MESSAGE_TYPE: "https://iden3-communication.io/credentials/0.1/payment";
|
|
26
|
+
DISCOVERY_PROTOCOL_QUERIES_MESSAGE_TYPE: "https://didcomm.org/discover-features/2.0/queries";
|
|
27
|
+
DISCOVERY_PROTOCOL_DISCLOSE_MESSAGE_TYPE: "https://didcomm.org/discover-features/2.0/disclose";
|
|
26
28
|
}>;
|
|
27
29
|
/**
|
|
28
30
|
* Media types for iden3 comm communication protocol
|