@0xpolygonid/js-sdk 1.21.1 → 1.23.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 +1394 -278
- package/dist/browser/esm/index.js.map +1 -1
- package/dist/browser/umd/index.js +1399 -278
- package/dist/browser/umd/index.js.map +1 -1
- package/dist/node/cjs/iden3comm/constants.js +25 -1
- package/dist/node/cjs/iden3comm/handlers/auth.js +52 -8
- package/dist/node/cjs/iden3comm/handlers/common.js +13 -1
- package/dist/node/cjs/iden3comm/handlers/contract-request.js +5 -1
- package/dist/node/cjs/iden3comm/handlers/credential-proposal.js +14 -3
- package/dist/node/cjs/iden3comm/handlers/fetch.js +12 -2
- package/dist/node/cjs/iden3comm/handlers/message-handler.js +4 -0
- package/dist/node/cjs/iden3comm/handlers/payment.js +240 -28
- package/dist/node/cjs/iden3comm/handlers/revocation-status.js +4 -0
- package/dist/node/cjs/iden3comm/index.js +1 -0
- package/dist/node/cjs/iden3comm/packageManager.js +12 -0
- package/dist/node/cjs/iden3comm/packers/jws.js +21 -0
- package/dist/node/cjs/iden3comm/packers/plain.js +19 -0
- package/dist/node/cjs/iden3comm/packers/zkp.js +25 -0
- package/dist/node/cjs/iden3comm/types/index.js +1 -0
- package/dist/node/cjs/iden3comm/types/protocol/accept-profile.js +2 -0
- package/dist/node/cjs/iden3comm/utils/accept-profile.js +107 -0
- package/dist/node/cjs/iden3comm/utils/index.js +1 -0
- package/dist/node/cjs/proof/provers/inputs-generator.js +2 -1
- package/dist/node/cjs/storage/blockchain/abi/ERC20Permit.json +1 -0
- package/dist/node/cjs/storage/blockchain/erc20-permit-sig.js +46 -0
- package/dist/node/cjs/storage/blockchain/index.js +1 -0
- package/dist/node/cjs/storage/filters/jsonQuery.js +5 -3
- package/dist/node/cjs/verifiable/constants.js +28 -11
- package/dist/node/esm/iden3comm/constants.js +24 -0
- package/dist/node/esm/iden3comm/handlers/auth.js +55 -11
- package/dist/node/esm/iden3comm/handlers/common.js +11 -0
- package/dist/node/esm/iden3comm/handlers/contract-request.js +7 -3
- package/dist/node/esm/iden3comm/handlers/credential-proposal.js +15 -4
- package/dist/node/esm/iden3comm/handlers/fetch.js +12 -2
- package/dist/node/esm/iden3comm/handlers/message-handler.js +4 -0
- package/dist/node/esm/iden3comm/handlers/payment.js +240 -29
- package/dist/node/esm/iden3comm/handlers/revocation-status.js +4 -0
- package/dist/node/esm/iden3comm/index.js +1 -0
- package/dist/node/esm/iden3comm/packageManager.js +12 -0
- package/dist/node/esm/iden3comm/packers/jws.js +22 -1
- package/dist/node/esm/iden3comm/packers/plain.js +19 -0
- package/dist/node/esm/iden3comm/packers/zkp.js +26 -1
- package/dist/node/esm/iden3comm/types/index.js +1 -0
- package/dist/node/esm/iden3comm/types/protocol/accept-profile.js +1 -0
- package/dist/node/esm/iden3comm/utils/accept-profile.js +102 -0
- package/dist/node/esm/iden3comm/utils/index.js +1 -0
- package/dist/node/esm/proof/provers/inputs-generator.js +2 -1
- package/dist/node/esm/storage/blockchain/abi/ERC20Permit.json +1 -0
- package/dist/node/esm/storage/blockchain/erc20-permit-sig.js +39 -0
- package/dist/node/esm/storage/blockchain/index.js +1 -0
- package/dist/node/esm/storage/filters/jsonQuery.js +5 -3
- package/dist/node/esm/verifiable/constants.js +27 -10
- package/dist/types/iden3comm/constants.d.ts +16 -0
- package/dist/types/iden3comm/handlers/auth.d.ts +19 -7
- package/dist/types/iden3comm/handlers/common.d.ts +7 -1
- package/dist/types/iden3comm/handlers/contract-request.d.ts +2 -2
- package/dist/types/iden3comm/handlers/credential-proposal.d.ts +9 -4
- package/dist/types/iden3comm/handlers/fetch.d.ts +22 -6
- package/dist/types/iden3comm/handlers/message-handler.d.ts +6 -0
- package/dist/types/iden3comm/handlers/payment.d.ts +70 -10
- package/dist/types/iden3comm/handlers/revocation-status.d.ts +2 -2
- package/dist/types/iden3comm/index.d.ts +1 -0
- package/dist/types/iden3comm/packageManager.d.ts +4 -0
- package/dist/types/iden3comm/packers/jws.d.ts +5 -0
- package/dist/types/iden3comm/packers/plain.d.ts +4 -0
- package/dist/types/iden3comm/packers/zkp.d.ts +6 -0
- package/dist/types/iden3comm/types/index.d.ts +1 -0
- package/dist/types/iden3comm/types/packageManager.d.ts +14 -0
- package/dist/types/iden3comm/types/packer.d.ts +25 -0
- package/dist/types/iden3comm/types/protocol/accept-profile.d.ts +7 -0
- package/dist/types/iden3comm/types/protocol/auth.d.ts +1 -0
- package/dist/types/iden3comm/types/protocol/credentials.d.ts +6 -6
- package/dist/types/iden3comm/types/protocol/messages.d.ts +2 -2
- package/dist/types/iden3comm/types/protocol/payment.d.ts +87 -13
- package/dist/types/iden3comm/types/protocol/proof.d.ts +3 -3
- package/dist/types/iden3comm/types/protocol/revocation.d.ts +2 -2
- package/dist/types/iden3comm/utils/accept-profile.d.ts +3 -0
- package/dist/types/iden3comm/utils/index.d.ts +1 -0
- package/dist/types/storage/blockchain/erc20-permit-sig.d.ts +12 -0
- package/dist/types/storage/blockchain/index.d.ts +1 -0
- package/dist/types/verifiable/constants.d.ts +27 -11
- package/package.json +2 -2
|
@@ -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.createPaymentRequest = void 0;
|
|
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");
|
|
@@ -32,6 +32,8 @@ const js_jwz_1 = require("@iden3/js-jwz");
|
|
|
32
32
|
const utils_1 = require("../../utils");
|
|
33
33
|
const message_handler_1 = require("./message-handler");
|
|
34
34
|
const verifiable_1 = require("../../verifiable");
|
|
35
|
+
const ethers_1 = require("ethers");
|
|
36
|
+
const common_1 = require("./common");
|
|
35
37
|
/**
|
|
36
38
|
* @beta
|
|
37
39
|
* createPaymentRequest is a function to create protocol payment-request message
|
|
@@ -41,7 +43,7 @@ const verifiable_1 = require("../../verifiable");
|
|
|
41
43
|
* @param {PaymentRequestInfo[]} payments - payments
|
|
42
44
|
* @returns `PaymentRequestMessage`
|
|
43
45
|
*/
|
|
44
|
-
function createPaymentRequest(sender, receiver, agent, payments) {
|
|
46
|
+
function createPaymentRequest(sender, receiver, agent, payments, opts) {
|
|
45
47
|
const uuidv4 = uuid.v4();
|
|
46
48
|
const request = {
|
|
47
49
|
id: uuidv4,
|
|
@@ -53,20 +55,59 @@ function createPaymentRequest(sender, receiver, agent, payments) {
|
|
|
53
55
|
body: {
|
|
54
56
|
agent,
|
|
55
57
|
payments
|
|
56
|
-
}
|
|
58
|
+
},
|
|
59
|
+
created_time: (0, js_iden3_core_1.getUnixTimestamp)(new Date()),
|
|
60
|
+
expires_time: opts?.expires_time ? (0, js_iden3_core_1.getUnixTimestamp)(opts.expires_time) : undefined
|
|
57
61
|
};
|
|
58
62
|
return request;
|
|
59
63
|
}
|
|
60
64
|
exports.createPaymentRequest = createPaymentRequest;
|
|
65
|
+
async function verifyEIP712TypedData(data, resolver) {
|
|
66
|
+
const paymentData = data.type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
67
|
+
? {
|
|
68
|
+
recipient: data.recipient,
|
|
69
|
+
amount: data.amount,
|
|
70
|
+
expirationDate: new Date(data.expirationDate).getTime(),
|
|
71
|
+
nonce: data.nonce,
|
|
72
|
+
metadata: '0x'
|
|
73
|
+
}
|
|
74
|
+
: {
|
|
75
|
+
tokenAddress: data.tokenAddress,
|
|
76
|
+
recipient: data.recipient,
|
|
77
|
+
amount: data.amount,
|
|
78
|
+
expirationDate: new Date(data.expirationDate).getTime(),
|
|
79
|
+
nonce: data.nonce,
|
|
80
|
+
metadata: '0x'
|
|
81
|
+
};
|
|
82
|
+
const proof = Array.isArray(data.proof) ? data.proof[0] : data.proof;
|
|
83
|
+
const typesFetchResult = await fetch(proof.eip712.types);
|
|
84
|
+
const types = await typesFetchResult.json();
|
|
85
|
+
delete types.EIP712Domain;
|
|
86
|
+
const recovered = ethers_1.ethers.verifyTypedData(proof.eip712.domain, types, paymentData, proof.proofValue);
|
|
87
|
+
const { didDocument } = await resolver.resolve(proof.verificationMethod);
|
|
88
|
+
if (didDocument?.verificationMethod) {
|
|
89
|
+
for (const verificationMethod of didDocument.verificationMethod) {
|
|
90
|
+
if (verificationMethod.blockchainAccountId?.split(':').slice(-1)[0].toLowerCase() ===
|
|
91
|
+
recovered.toLowerCase()) {
|
|
92
|
+
return recovered;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
throw new Error('failed request. issuer DIDDocument does not contain any verificationMethods');
|
|
98
|
+
}
|
|
99
|
+
throw new Error(`failed request. no matching verificationMethod`);
|
|
100
|
+
}
|
|
101
|
+
exports.verifyEIP712TypedData = verifyEIP712TypedData;
|
|
61
102
|
/**
|
|
62
103
|
* @beta
|
|
63
104
|
* createPayment is a function to create protocol payment message
|
|
64
105
|
* @param {DID} sender - sender did
|
|
65
106
|
* @param {DID} receiver - receiver did
|
|
66
|
-
* @param {
|
|
107
|
+
* @param {PaymentMessageBody} body - payments
|
|
67
108
|
* @returns `PaymentMessage`
|
|
68
109
|
*/
|
|
69
|
-
function createPayment(sender, receiver, payments) {
|
|
110
|
+
function createPayment(sender, receiver, payments, opts) {
|
|
70
111
|
const uuidv4 = uuid.v4();
|
|
71
112
|
const request = {
|
|
72
113
|
id: uuidv4,
|
|
@@ -77,7 +118,9 @@ function createPayment(sender, receiver, payments) {
|
|
|
77
118
|
type: constants_1.PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE,
|
|
78
119
|
body: {
|
|
79
120
|
payments
|
|
80
|
-
}
|
|
121
|
+
},
|
|
122
|
+
created_time: (0, js_iden3_core_1.getUnixTimestamp)(new Date()),
|
|
123
|
+
expires_time: opts?.expires_time ? (0, js_iden3_core_1.getUnixTimestamp)(opts.expires_time) : undefined
|
|
81
124
|
};
|
|
82
125
|
return request;
|
|
83
126
|
}
|
|
@@ -140,21 +183,28 @@ class PaymentHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
140
183
|
const receiverDID = js_iden3_core_1.DID.parse(paymentRequest.from);
|
|
141
184
|
const payments = [];
|
|
142
185
|
for (let i = 0; i < paymentRequest.body.payments.length; i++) {
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
186
|
+
const { data } = paymentRequest.body.payments[i];
|
|
187
|
+
const selectedPayment = Array.isArray(data)
|
|
188
|
+
? data.find((p) => {
|
|
189
|
+
return p.type === verifiable_1.PaymentRequestDataType.Iden3PaymentRequestCryptoV1
|
|
190
|
+
? p.id === ctx.nonce
|
|
191
|
+
: p.nonce === ctx.nonce;
|
|
192
|
+
})
|
|
193
|
+
: data;
|
|
194
|
+
if (!selectedPayment) {
|
|
195
|
+
throw new Error(`failed request. no payment in request for nonce ${ctx.nonce}`);
|
|
146
196
|
}
|
|
147
|
-
|
|
148
|
-
|
|
197
|
+
switch (selectedPayment.type) {
|
|
198
|
+
case verifiable_1.PaymentRequestDataType.Iden3PaymentRequestCryptoV1:
|
|
199
|
+
payments.push(await this.handleIden3PaymentRequestCryptoV1(selectedPayment, ctx.paymentHandler));
|
|
200
|
+
break;
|
|
201
|
+
case verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1:
|
|
202
|
+
payments.push(await this.handleIden3PaymentRailsRequestV1(selectedPayment, ctx.paymentHandler));
|
|
203
|
+
break;
|
|
204
|
+
case verifiable_1.PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1:
|
|
205
|
+
payments.push(await this.handleIden3PaymentRailsERC20RequestV1(selectedPayment, ctx.paymentHandler, ctx.erc20TokenApproveHandler));
|
|
206
|
+
break;
|
|
149
207
|
}
|
|
150
|
-
const txId = await ctx.paymentHandler(paymentReq.data);
|
|
151
|
-
payments.push({
|
|
152
|
-
id: paymentReq.data.id,
|
|
153
|
-
type: verifiable_1.PaymentType.Iden3PaymentCryptoV1,
|
|
154
|
-
paymentData: {
|
|
155
|
-
txId
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
208
|
}
|
|
159
209
|
const paymentMessage = createPayment(senderDID, receiverDID, payments);
|
|
160
210
|
const response = await this.packMessage(paymentMessage, senderDID);
|
|
@@ -187,6 +237,9 @@ class PaymentHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
187
237
|
if (!paymentRequest.to) {
|
|
188
238
|
throw new Error(`failed request. empty 'to' field`);
|
|
189
239
|
}
|
|
240
|
+
if (!opts?.allowExpiredMessages) {
|
|
241
|
+
(0, common_1.verifyExpiresTime)(paymentRequest);
|
|
242
|
+
}
|
|
190
243
|
const agentMessage = await this.handlePaymentRequestMessage(paymentRequest, opts);
|
|
191
244
|
if (!agentMessage) {
|
|
192
245
|
return null;
|
|
@@ -197,24 +250,122 @@ class PaymentHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
197
250
|
/**
|
|
198
251
|
* @inheritdoc IPaymentHandler#handlePayment
|
|
199
252
|
*/
|
|
200
|
-
async handlePayment(payment,
|
|
201
|
-
if (
|
|
202
|
-
|
|
253
|
+
async handlePayment(payment, params) {
|
|
254
|
+
if (!params?.allowExpiredMessages) {
|
|
255
|
+
(0, common_1.verifyExpiresTime)(payment);
|
|
256
|
+
}
|
|
257
|
+
if (params.paymentRequest.from !== payment.to) {
|
|
258
|
+
throw new Error(`sender of the request is not a target of response - expected ${params.paymentRequest.from}, given ${payment.to}`);
|
|
203
259
|
}
|
|
204
260
|
if (!payment.body.payments.length) {
|
|
205
261
|
throw new Error(`failed request. empty 'payments' field in body`);
|
|
206
262
|
}
|
|
263
|
+
if (!params.paymentValidationHandler) {
|
|
264
|
+
throw new Error(`please provide payment validation handler in options`);
|
|
265
|
+
}
|
|
207
266
|
for (let i = 0; i < payment.body.payments.length; i++) {
|
|
208
267
|
const p = payment.body.payments[i];
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
268
|
+
const nonce = p.type === verifiable_1.PaymentType.Iden3PaymentCryptoV1 ? p.id : p.nonce;
|
|
269
|
+
const requestDataArr = params.paymentRequest.body.payments
|
|
270
|
+
.map((r) => (Array.isArray(r.data) ? r.data : [r.data]))
|
|
271
|
+
.flat();
|
|
272
|
+
const requestData = requestDataArr.find((r) => r.type === verifiable_1.PaymentRequestDataType.Iden3PaymentRequestCryptoV1
|
|
273
|
+
? r.id === nonce
|
|
274
|
+
: r.nonce === nonce);
|
|
275
|
+
if (!requestData) {
|
|
276
|
+
throw new Error(`can't find payment request for payment ${p.type === verifiable_1.PaymentType.Iden3PaymentCryptoV1 ? 'id' : 'nonce'} ${nonce}`);
|
|
212
277
|
}
|
|
213
|
-
|
|
214
|
-
|
|
278
|
+
await params.paymentValidationHandler(p.paymentData.txId, requestData);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* @inheritdoc IPaymentHandler#createPaymentRailsV1
|
|
283
|
+
*/
|
|
284
|
+
async createPaymentRailsV1(sender, receiver, agent, signer, payments) {
|
|
285
|
+
const paymentRequestInfo = [];
|
|
286
|
+
for (let i = 0; i < payments.length; i++) {
|
|
287
|
+
const { credentials, description } = payments[i];
|
|
288
|
+
const dataArr = [];
|
|
289
|
+
for (let j = 0; j < payments[i].chains.length; j++) {
|
|
290
|
+
const { features, nonce, amount, currency, chainId, expirationDate, type } = payments[i].chains[j];
|
|
291
|
+
const multiChainConfig = this._params.multiChainPaymentConfig?.find((c) => c.chainId === chainId);
|
|
292
|
+
if (!multiChainConfig) {
|
|
293
|
+
throw new Error(`failed request. no config for chain ${chainId}`);
|
|
294
|
+
}
|
|
295
|
+
const { recipient, paymentContract, erc20TokenAddressArr } = multiChainConfig;
|
|
296
|
+
const tokenAddress = erc20TokenAddressArr.find((t) => t.symbol === currency)?.address;
|
|
297
|
+
if (type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1 && !tokenAddress) {
|
|
298
|
+
throw new Error(`failed request. no token address for currency ${currency}`);
|
|
299
|
+
}
|
|
300
|
+
const expirationTime = expirationDate
|
|
301
|
+
? new Date(expirationDate).getTime()
|
|
302
|
+
: new Date(new Date().setHours(new Date().getHours() + 1)).getTime();
|
|
303
|
+
const typeUrl = `https://schema.iden3.io/core/json/${type}.json`;
|
|
304
|
+
const typesFetchResult = await fetch(typeUrl);
|
|
305
|
+
const types = await typesFetchResult.json();
|
|
306
|
+
delete types.EIP712Domain;
|
|
307
|
+
const paymentData = type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
308
|
+
? {
|
|
309
|
+
recipient,
|
|
310
|
+
amount,
|
|
311
|
+
expirationDate: expirationTime,
|
|
312
|
+
nonce,
|
|
313
|
+
metadata: '0x'
|
|
314
|
+
}
|
|
315
|
+
: {
|
|
316
|
+
tokenAddress,
|
|
317
|
+
recipient,
|
|
318
|
+
amount,
|
|
319
|
+
expirationDate: expirationTime,
|
|
320
|
+
nonce,
|
|
321
|
+
metadata: '0x'
|
|
322
|
+
};
|
|
323
|
+
const domain = {
|
|
324
|
+
name: 'MCPayment',
|
|
325
|
+
version: '1.0.0',
|
|
326
|
+
chainId,
|
|
327
|
+
verifyingContract: paymentContract
|
|
328
|
+
};
|
|
329
|
+
const signature = await signer.signTypedData(domain, types, paymentData);
|
|
330
|
+
const proof = [
|
|
331
|
+
{
|
|
332
|
+
type: verifiable_1.SupportedPaymentProofType.EthereumEip712Signature2021,
|
|
333
|
+
proofPurpose: 'assertionMethod',
|
|
334
|
+
proofValue: signature,
|
|
335
|
+
verificationMethod: `did:pkh:eip155:${chainId}:${await signer.getAddress()}`,
|
|
336
|
+
created: new Date().toISOString(),
|
|
337
|
+
eip712: {
|
|
338
|
+
types: typeUrl,
|
|
339
|
+
primaryType: 'Iden3PaymentRailsRequestV1',
|
|
340
|
+
domain
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
];
|
|
344
|
+
const d = {
|
|
345
|
+
type: verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1,
|
|
346
|
+
'@context': [
|
|
347
|
+
`https://schema.iden3.io/core/jsonld/payment.jsonld#${type}`,
|
|
348
|
+
'https://w3id.org/security/suites/eip712sig-2021/v1'
|
|
349
|
+
],
|
|
350
|
+
recipient,
|
|
351
|
+
amount: amount.toString(),
|
|
352
|
+
currency,
|
|
353
|
+
expirationDate: new Date(expirationTime).toISOString(),
|
|
354
|
+
nonce: nonce.toString(),
|
|
355
|
+
metadata: '0x',
|
|
356
|
+
proof
|
|
357
|
+
};
|
|
358
|
+
dataArr.push(type === verifiable_1.PaymentRequestDataType.Iden3PaymentRailsRequestV1
|
|
359
|
+
? d
|
|
360
|
+
: { ...d, type, tokenAddress: tokenAddress || '', features: features || [] });
|
|
215
361
|
}
|
|
216
|
-
|
|
362
|
+
paymentRequestInfo.push({
|
|
363
|
+
data: dataArr,
|
|
364
|
+
credentials,
|
|
365
|
+
description
|
|
366
|
+
});
|
|
217
367
|
}
|
|
368
|
+
return createPaymentRequest(sender, receiver, agent, paymentRequestInfo);
|
|
218
369
|
}
|
|
219
370
|
async packMessage(message, senderDID) {
|
|
220
371
|
const responseEncoded = utils_1.byteEncoder.encode(JSON.stringify(message));
|
|
@@ -228,5 +379,66 @@ class PaymentHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
228
379
|
...packerOpts
|
|
229
380
|
});
|
|
230
381
|
}
|
|
382
|
+
async handleIden3PaymentRequestCryptoV1(data, paymentHandler) {
|
|
383
|
+
if (data.expiration && new Date(data.expiration) < new Date()) {
|
|
384
|
+
throw new Error(`failed request. expired request`);
|
|
385
|
+
}
|
|
386
|
+
const txId = await paymentHandler(data);
|
|
387
|
+
return {
|
|
388
|
+
id: data.id,
|
|
389
|
+
'@context': 'https://schema.iden3.io/core/jsonld/payment.jsonld#Iden3PaymentCryptoV1',
|
|
390
|
+
type: verifiable_1.PaymentType.Iden3PaymentCryptoV1,
|
|
391
|
+
paymentData: {
|
|
392
|
+
txId
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
async handleIden3PaymentRailsRequestV1(data, paymentHandler) {
|
|
397
|
+
if (data.expirationDate && new Date(data.expirationDate) < new Date()) {
|
|
398
|
+
throw new Error(`failed request. expired request`);
|
|
399
|
+
}
|
|
400
|
+
const signer = await verifyEIP712TypedData(data, this._params.documentResolver);
|
|
401
|
+
if (this._params.allowedSigners && !this._params.allowedSigners.includes(signer)) {
|
|
402
|
+
throw new Error(`failed request. signer is not in the allowed signers list`);
|
|
403
|
+
}
|
|
404
|
+
const txId = await paymentHandler(data);
|
|
405
|
+
const proof = Array.isArray(data.proof) ? data.proof[0] : data.proof;
|
|
406
|
+
return {
|
|
407
|
+
nonce: data.nonce,
|
|
408
|
+
type: verifiable_1.PaymentType.Iden3PaymentRailsV1,
|
|
409
|
+
'@context': 'https://schema.iden3.io/core/jsonld/payment.jsonld#Iden3PaymentRailsV1',
|
|
410
|
+
paymentData: {
|
|
411
|
+
txId,
|
|
412
|
+
chainId: proof.eip712.domain.chainId
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
async handleIden3PaymentRailsERC20RequestV1(data, paymentHandler, approveHandler) {
|
|
417
|
+
if (data.expirationDate && new Date(data.expirationDate) < new Date()) {
|
|
418
|
+
throw new Error(`failed request. expired request`);
|
|
419
|
+
}
|
|
420
|
+
const signer = await verifyEIP712TypedData(data, this._params.documentResolver);
|
|
421
|
+
if (this._params.allowedSigners && !this._params.allowedSigners.includes(signer)) {
|
|
422
|
+
throw new Error(`failed request. signer is not in the allowed signers list`);
|
|
423
|
+
}
|
|
424
|
+
if (!data.features?.includes(verifiable_1.PaymentFeatures.EIP_2612) && !approveHandler) {
|
|
425
|
+
throw new Error(`please provide erc20TokenApproveHandler in context for ERC-20 payment type`);
|
|
426
|
+
}
|
|
427
|
+
if (approveHandler) {
|
|
428
|
+
await approveHandler(data);
|
|
429
|
+
}
|
|
430
|
+
const txId = await paymentHandler(data);
|
|
431
|
+
const proof = Array.isArray(data.proof) ? data.proof[0] : data.proof;
|
|
432
|
+
return {
|
|
433
|
+
nonce: data.nonce,
|
|
434
|
+
type: verifiable_1.PaymentType.Iden3PaymentRailsERC20V1,
|
|
435
|
+
'@context': 'https://schema.iden3.io/core/jsonld/payment.jsonld#Iden3PaymentRailsERC20V1',
|
|
436
|
+
paymentData: {
|
|
437
|
+
txId,
|
|
438
|
+
chainId: proof.eip712.domain.chainId,
|
|
439
|
+
tokenAddress: data.tokenAddress
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
}
|
|
231
443
|
}
|
|
232
444
|
exports.PaymentHandler = PaymentHandler;
|
|
@@ -31,6 +31,7 @@ const uuid = __importStar(require("uuid"));
|
|
|
31
31
|
const utils_1 = require("../../utils");
|
|
32
32
|
const js_jwz_1 = require("@iden3/js-jwz");
|
|
33
33
|
const message_handler_1 = require("./message-handler");
|
|
34
|
+
const common_1 = require("./common");
|
|
34
35
|
/**
|
|
35
36
|
*
|
|
36
37
|
* Allows to process RevocationStatusRequest protocol message
|
|
@@ -123,6 +124,9 @@ class RevocationStatusHandler extends message_handler_1.AbstractMessageHandler {
|
|
|
123
124
|
throw new Error(`jws packer options are required for ${constants_2.MediaType.SignedMessage}`);
|
|
124
125
|
}
|
|
125
126
|
const rsRequest = await this.parseRevocationStatusRequest(request);
|
|
127
|
+
if (!opts.allowExpiredMessages) {
|
|
128
|
+
(0, common_1.verifyExpiresTime)(rsRequest);
|
|
129
|
+
}
|
|
126
130
|
const response = await this.handleRevocationStatusRequestMessage(rsRequest, {
|
|
127
131
|
senderDid: did,
|
|
128
132
|
mediaType: opts.mediaType,
|
|
@@ -32,5 +32,6 @@ __exportStar(require("./packers"), exports);
|
|
|
32
32
|
__exportStar(require("./types"), exports);
|
|
33
33
|
__exportStar(require("./handlers"), exports);
|
|
34
34
|
__exportStar(require("./utils/did"), exports);
|
|
35
|
+
__exportStar(require("./utils/accept-profile"), exports);
|
|
35
36
|
const PROTOCOL_CONSTANTS = __importStar(require("./constants"));
|
|
36
37
|
exports.PROTOCOL_CONSTANTS = PROTOCOL_CONSTANTS;
|
|
@@ -18,6 +18,18 @@ class PackageManager {
|
|
|
18
18
|
constructor() {
|
|
19
19
|
this.packers = new Map();
|
|
20
20
|
}
|
|
21
|
+
/** {@inheritDoc IPackageManager.isProfileSupported} */
|
|
22
|
+
isProfileSupported(mediaType, profile) {
|
|
23
|
+
const p = this.packers.get(mediaType);
|
|
24
|
+
if (!p) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return p.isProfileSupported(profile);
|
|
28
|
+
}
|
|
29
|
+
/** {@inheritDoc IPackageManager.getSupportedMediaTypes} */
|
|
30
|
+
getSupportedMediaTypes() {
|
|
31
|
+
return [...this.packers.keys()];
|
|
32
|
+
}
|
|
21
33
|
/** {@inheritDoc IPackageManager.registerPackers} */
|
|
22
34
|
registerPackers(packers) {
|
|
23
35
|
packers.forEach((p) => {
|
|
@@ -8,6 +8,7 @@ const did_jwt_1 = require("did-jwt");
|
|
|
8
8
|
const did_resolver_1 = require("did-resolver");
|
|
9
9
|
const encoding_1 = require("../../utils/encoding");
|
|
10
10
|
const utils_1 = require("../../utils");
|
|
11
|
+
const utils_2 = require("../utils");
|
|
11
12
|
/**
|
|
12
13
|
* Packer that can pack message to JWZ token,
|
|
13
14
|
* and unpack and validate JWZ envelope
|
|
@@ -86,6 +87,26 @@ class JWSPacker {
|
|
|
86
87
|
mediaType() {
|
|
87
88
|
return constants_1.MediaType.SignedMessage;
|
|
88
89
|
}
|
|
90
|
+
/** {@inheritDoc IPacker.getSupportedProfiles} */
|
|
91
|
+
getSupportedProfiles() {
|
|
92
|
+
return [`env=${this.mediaType()}&alg=${this.getSupportedAlgorithms().join(',')}`];
|
|
93
|
+
}
|
|
94
|
+
/** {@inheritDoc IPacker.isProfileSupported} */
|
|
95
|
+
isProfileSupported(profile) {
|
|
96
|
+
const { env, circuits, alg } = (0, utils_2.parseAcceptProfile)(profile);
|
|
97
|
+
if (env !== this.mediaType()) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (circuits) {
|
|
101
|
+
throw new Error(`Circuits are not supported for ${env} media type`);
|
|
102
|
+
}
|
|
103
|
+
const supportedAlgArr = this.getSupportedAlgorithms();
|
|
104
|
+
const algSupported = !alg?.length || alg.some((a) => supportedAlgArr.includes(a));
|
|
105
|
+
return algSupported;
|
|
106
|
+
}
|
|
107
|
+
getSupportedAlgorithms() {
|
|
108
|
+
return [constants_1.AcceptJwsAlgorithms.ES256K, constants_1.AcceptJwsAlgorithms.ES256KR];
|
|
109
|
+
}
|
|
89
110
|
async resolveDidDoc(from) {
|
|
90
111
|
let didDocument;
|
|
91
112
|
try {
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PlainPacker = void 0;
|
|
4
4
|
const constants_1 = require("../constants");
|
|
5
5
|
const utils_1 = require("../../utils");
|
|
6
|
+
const utils_2 = require("../utils");
|
|
6
7
|
/**
|
|
7
8
|
* Plain packer just serializes bytes to JSON and adds media type
|
|
8
9
|
*
|
|
@@ -52,5 +53,23 @@ class PlainPacker {
|
|
|
52
53
|
mediaType() {
|
|
53
54
|
return constants_1.MediaType.PlainMessage;
|
|
54
55
|
}
|
|
56
|
+
/** {@inheritDoc IPacker.getSupportedProfiles} */
|
|
57
|
+
getSupportedProfiles() {
|
|
58
|
+
return [`env=${this.mediaType()}`];
|
|
59
|
+
}
|
|
60
|
+
/** {@inheritDoc IPacker.isProfileSupported} */
|
|
61
|
+
isProfileSupported(profile) {
|
|
62
|
+
const { env, circuits, alg } = (0, utils_2.parseAcceptProfile)(profile);
|
|
63
|
+
if (env !== this.mediaType()) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (circuits) {
|
|
67
|
+
throw new Error(`Circuits are not supported for ${env} media type`);
|
|
68
|
+
}
|
|
69
|
+
if (alg) {
|
|
70
|
+
throw new Error(`Algorithms are not supported for ${env} media type`);
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
55
74
|
}
|
|
56
75
|
exports.PlainPacker = PlainPacker;
|
|
@@ -9,6 +9,7 @@ const errors_1 = require("../errors");
|
|
|
9
9
|
const constants_1 = require("../constants");
|
|
10
10
|
const utils_1 = require("../../utils");
|
|
11
11
|
const constants_2 = require("../constants");
|
|
12
|
+
const utils_2 = require("../utils");
|
|
12
13
|
const { getProvingMethod } = js_jwz_1.proving;
|
|
13
14
|
/**
|
|
14
15
|
* Handler to
|
|
@@ -141,6 +142,30 @@ class ZKPPacker {
|
|
|
141
142
|
mediaType() {
|
|
142
143
|
return constants_1.MediaType.ZKPMessage;
|
|
143
144
|
}
|
|
145
|
+
/** {@inheritDoc IPacker.getSupportedProfiles} */
|
|
146
|
+
getSupportedProfiles() {
|
|
147
|
+
return [
|
|
148
|
+
`env=${this.mediaType()}&alg=${this.getSupportedAlgorithms().join(',')}&circuitIds=${this.getSupportedCircuitIds().join(',')}`
|
|
149
|
+
];
|
|
150
|
+
}
|
|
151
|
+
/** {@inheritDoc IPacker.isProfileSupported} */
|
|
152
|
+
isProfileSupported(profile) {
|
|
153
|
+
const { env, circuits, alg } = (0, utils_2.parseAcceptProfile)(profile);
|
|
154
|
+
if (env !== this.mediaType()) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const supportedCircuitIds = this.getSupportedCircuitIds();
|
|
158
|
+
const circuitIdSupported = !circuits?.length || circuits.some((c) => supportedCircuitIds.includes(c));
|
|
159
|
+
const supportedAlgArr = this.getSupportedAlgorithms();
|
|
160
|
+
const algSupported = !alg?.length || alg.some((a) => supportedAlgArr.includes(a));
|
|
161
|
+
return algSupported && circuitIdSupported;
|
|
162
|
+
}
|
|
163
|
+
getSupportedAlgorithms() {
|
|
164
|
+
return [constants_1.AcceptJwzAlgorithms.Groth16];
|
|
165
|
+
}
|
|
166
|
+
getSupportedCircuitIds() {
|
|
167
|
+
return [constants_1.AcceptAuthCircuits.AuthV2];
|
|
168
|
+
}
|
|
144
169
|
}
|
|
145
170
|
exports.ZKPPacker = ZKPPacker;
|
|
146
171
|
const verifySender = async (token, msg) => {
|
|
@@ -22,6 +22,7 @@ __exportStar(require("./protocol/revocation"), exports);
|
|
|
22
22
|
__exportStar(require("./protocol/contract-request"), exports);
|
|
23
23
|
__exportStar(require("./protocol/proposal-request"), exports);
|
|
24
24
|
__exportStar(require("./protocol/payment"), exports);
|
|
25
|
+
__exportStar(require("./protocol/accept-profile"), exports);
|
|
25
26
|
__exportStar(require("./packer"), exports);
|
|
26
27
|
__exportStar(require("./models"), exports);
|
|
27
28
|
__exportStar(require("./packageManager"), exports);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseAcceptProfile = exports.buildAccept = void 0;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
function isProtocolVersion(value) {
|
|
6
|
+
return Object.values(constants_1.ProtocolVersion).includes(value);
|
|
7
|
+
}
|
|
8
|
+
function isMediaType(value) {
|
|
9
|
+
return Object.values(constants_1.MediaType).includes(value);
|
|
10
|
+
}
|
|
11
|
+
function isAcceptAuthCircuits(value) {
|
|
12
|
+
return Object.values(constants_1.AcceptAuthCircuits).includes(value);
|
|
13
|
+
}
|
|
14
|
+
function isAcceptJwsAlgorithms(value) {
|
|
15
|
+
return Object.values(constants_1.AcceptJwsAlgorithms).includes(value);
|
|
16
|
+
}
|
|
17
|
+
function isAcceptJwzAlgorithms(value) {
|
|
18
|
+
return Object.values(constants_1.AcceptJwzAlgorithms).includes(value);
|
|
19
|
+
}
|
|
20
|
+
const buildAccept = (profiles) => {
|
|
21
|
+
const result = [];
|
|
22
|
+
for (const profile of profiles) {
|
|
23
|
+
let accept = `${profile.protocolVersion};env=${profile.env}`;
|
|
24
|
+
if (profile.circuits?.length) {
|
|
25
|
+
accept += `;circuitId=${profile.circuits.join(',')}`;
|
|
26
|
+
}
|
|
27
|
+
if (profile.alg?.length) {
|
|
28
|
+
accept += `;alg=${profile.alg.join(',')}`;
|
|
29
|
+
}
|
|
30
|
+
result.push(accept);
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
exports.buildAccept = buildAccept;
|
|
35
|
+
const parseAcceptProfile = (profile) => {
|
|
36
|
+
const params = profile.split(';');
|
|
37
|
+
if (params.length < 2) {
|
|
38
|
+
throw new Error('Invalid accept profile');
|
|
39
|
+
}
|
|
40
|
+
const protocolVersion = params[0].trim();
|
|
41
|
+
if (!isProtocolVersion(protocolVersion)) {
|
|
42
|
+
throw new Error(`Protocol version '${protocolVersion}' not supported`);
|
|
43
|
+
}
|
|
44
|
+
const envParam = params[1].split('=');
|
|
45
|
+
if (envParam.length !== 2) {
|
|
46
|
+
throw new Error(`Invalid accept profile 'env' parameter`);
|
|
47
|
+
}
|
|
48
|
+
const env = params[1].split('=')[1].trim();
|
|
49
|
+
if (!isMediaType(env)) {
|
|
50
|
+
throw new Error(`Envelop '${env}' not supported`);
|
|
51
|
+
}
|
|
52
|
+
const circuitsIndex = params.findIndex((i) => i.includes('circuitId='));
|
|
53
|
+
if (env !== constants_1.MediaType.ZKPMessage && circuitsIndex > 0) {
|
|
54
|
+
throw new Error(`Circuits not supported for env '${env}'`);
|
|
55
|
+
}
|
|
56
|
+
let circuits = undefined;
|
|
57
|
+
if (circuitsIndex > 0) {
|
|
58
|
+
circuits = params[circuitsIndex]
|
|
59
|
+
.split('=')[1]
|
|
60
|
+
.split(',')
|
|
61
|
+
.map((i) => i.trim())
|
|
62
|
+
.map((i) => {
|
|
63
|
+
if (!isAcceptAuthCircuits(i)) {
|
|
64
|
+
throw new Error(`Circuit '${i}' not supported`);
|
|
65
|
+
}
|
|
66
|
+
return i;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const algIndex = params.findIndex((i) => i.includes('alg='));
|
|
70
|
+
let alg = undefined;
|
|
71
|
+
if (algIndex > 0) {
|
|
72
|
+
if (env === constants_1.MediaType.ZKPMessage) {
|
|
73
|
+
alg = params[algIndex]
|
|
74
|
+
.split('=')[1]
|
|
75
|
+
.split(',')
|
|
76
|
+
.map((i) => {
|
|
77
|
+
i = i.trim();
|
|
78
|
+
if (!isAcceptJwzAlgorithms(i)) {
|
|
79
|
+
throw new Error(`Algorithm '${i}' not supported for '${env}'`);
|
|
80
|
+
}
|
|
81
|
+
return i;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
else if (env === constants_1.MediaType.SignedMessage) {
|
|
85
|
+
alg = params[algIndex]
|
|
86
|
+
.split('=')[1]
|
|
87
|
+
.split(',')
|
|
88
|
+
.map((i) => {
|
|
89
|
+
i = i.trim();
|
|
90
|
+
if (!isAcceptJwsAlgorithms(i)) {
|
|
91
|
+
throw new Error(`Algorithm '${i}' not supported for '${env}'`);
|
|
92
|
+
}
|
|
93
|
+
return i;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
throw new Error(`Algorithm not supported for '${env}'`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
protocolVersion,
|
|
102
|
+
env,
|
|
103
|
+
circuits,
|
|
104
|
+
alg
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
exports.parseAcceptProfile = parseAcceptProfile;
|