@0xpolygonid/js-sdk 1.12.0 → 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/dist/browser/esm/index.js +281 -18
  2. package/dist/browser/esm/index.js.map +1 -1
  3. package/dist/browser/umd/index.js +283 -17
  4. package/dist/browser/umd/index.js.map +1 -1
  5. package/dist/node/cjs/iden3comm/constants.js +5 -1
  6. package/dist/node/cjs/iden3comm/handlers/fetch.js +26 -12
  7. package/dist/node/cjs/iden3comm/handlers/index.js +1 -0
  8. package/dist/node/cjs/iden3comm/handlers/payment.js +232 -0
  9. package/dist/node/cjs/iden3comm/types/index.js +1 -0
  10. package/dist/node/cjs/iden3comm/types/protocol/payment.js +2 -0
  11. package/dist/node/cjs/proof/common.js +17 -4
  12. package/dist/node/cjs/verifiable/constants.js +37 -1
  13. package/dist/node/esm/iden3comm/constants.js +5 -1
  14. package/dist/node/esm/iden3comm/handlers/fetch.js +26 -12
  15. package/dist/node/esm/iden3comm/handlers/index.js +1 -0
  16. package/dist/node/esm/iden3comm/handlers/payment.js +203 -0
  17. package/dist/node/esm/iden3comm/types/index.js +1 -0
  18. package/dist/node/esm/iden3comm/types/protocol/payment.js +1 -0
  19. package/dist/node/esm/proof/common.js +17 -4
  20. package/dist/node/esm/verifiable/constants.js +36 -0
  21. package/dist/types/iden3comm/constants.d.ts +2 -0
  22. package/dist/types/iden3comm/handlers/index.d.ts +1 -0
  23. package/dist/types/iden3comm/handlers/message-handler.d.ts +2 -1
  24. package/dist/types/iden3comm/handlers/payment.d.ts +100 -0
  25. package/dist/types/iden3comm/types/index.d.ts +1 -0
  26. package/dist/types/iden3comm/types/protocol/credentials.d.ts +3 -3
  27. package/dist/types/iden3comm/types/protocol/payment.d.ts +48 -0
  28. package/dist/types/verifiable/constants.d.ts +32 -0
  29. package/package.json +1 -1
@@ -7496,6 +7496,42 @@ var PolygonIdSdk = (function (exports) {
7496
7496
  (function (RefreshServiceType) {
7497
7497
  RefreshServiceType["Iden3RefreshService2023"] = "Iden3RefreshService2023";
7498
7498
  })(exports.RefreshServiceType || (exports.RefreshServiceType = {}));
7499
+ /**
7500
+ * PaymentRequestType type for payment requests
7501
+ * @beta
7502
+ * @enum {string}
7503
+ */
7504
+ exports.PaymentRequestType = void 0;
7505
+ (function (PaymentRequestType) {
7506
+ PaymentRequestType["PaymentRequest"] = "PaymentRequest";
7507
+ })(exports.PaymentRequestType || (exports.PaymentRequestType = {}));
7508
+ /**
7509
+ * PaymentRequestDataType type for payment requests
7510
+ * @beta
7511
+ * @enum {string}
7512
+ */
7513
+ exports.PaymentRequestDataType = void 0;
7514
+ (function (PaymentRequestDataType) {
7515
+ PaymentRequestDataType["Iden3PaymentRequestCryptoV1"] = "Iden3PaymentRequestCryptoV1";
7516
+ })(exports.PaymentRequestDataType || (exports.PaymentRequestDataType = {}));
7517
+ /**
7518
+ * PaymentType type for payment responses
7519
+ * @beta
7520
+ * @enum {string}
7521
+ */
7522
+ exports.PaymentType = void 0;
7523
+ (function (PaymentType) {
7524
+ PaymentType["Iden3PaymentCryptoV1"] = "Iden3PaymentCryptoV1";
7525
+ })(exports.PaymentType || (exports.PaymentType = {}));
7526
+ /**
7527
+ * Media types for Payment supported currencies
7528
+ * @beta
7529
+ * @enum {number}
7530
+ */
7531
+ exports.SupportedCurrencies = void 0;
7532
+ (function (SupportedCurrencies) {
7533
+ SupportedCurrencies["ETH"] = "ETH";
7534
+ })(exports.SupportedCurrencies || (exports.SupportedCurrencies = {}));
7499
7535
 
7500
7536
  /**
7501
7537
  * Iden3SparseMerkleProof is a iden3 protocol merkle tree proof
@@ -53622,7 +53658,11 @@ var PolygonIdSdk = (function (exports) {
53622
53658
  // ProposalRequestMessageType is type for proposal-request message
53623
53659
  PROPOSAL_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal-request',
53624
53660
  // ProposalMessageType is type for proposal message
53625
- PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal'
53661
+ PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal',
53662
+ // PaymentRequestMessageType is type for payment-request message
53663
+ PAYMENT_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment-request',
53664
+ // PaymentMessageType is type for payment message
53665
+ PAYMENT_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment'
53626
53666
  });
53627
53667
  /**
53628
53668
  * Media types for iden3 comm communication protocol
@@ -54279,10 +54319,23 @@ var PolygonIdSdk = (function (exports) {
54279
54319
  if (!isValidOperation(query.datatype, propertyQuery.operator)) {
54280
54320
  throw new Error(`operator ${propertyQuery.operator} is not supported for datatype ${query.datatype}`);
54281
54321
  }
54282
- query.values =
54283
- propertyQuery.operator === exports.Operators.EXISTS
54284
- ? transformExistsValue(propertyQuery.operatorValue)
54285
- : await transformQueryValueToBigInts(propertyQuery.operatorValue, query.datatype);
54322
+ if ((propertyQuery.operator === exports.Operators.NOOP || propertyQuery.operator === exports.Operators.SD) &&
54323
+ propertyQuery.operatorValue) {
54324
+ throw new Error(`operator value should be undefined for ${propertyQuery.operator} operator`);
54325
+ }
54326
+ let values;
54327
+ switch (propertyQuery.operator) {
54328
+ case exports.Operators.NOOP:
54329
+ case exports.Operators.SD:
54330
+ values = [];
54331
+ break;
54332
+ case exports.Operators.EXISTS:
54333
+ values = transformExistsValue(propertyQuery.operatorValue);
54334
+ break;
54335
+ default:
54336
+ values = await transformQueryValueToBigInts(propertyQuery.operatorValue, query.datatype);
54337
+ }
54338
+ query.values = values;
54286
54339
  }
54287
54340
  return query;
54288
54341
  };
@@ -78424,9 +78477,17 @@ var PolygonIdSdk = (function (exports) {
78424
78477
  }
78425
78478
  async handle(message, ctx) {
78426
78479
  switch (message.type) {
78427
- case PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE:
78428
- await this.handleOfferMessage(message, ctx);
78429
- return null;
78480
+ case PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE: {
78481
+ const result = await this.handleOfferMessage(message, ctx);
78482
+ if (Array.isArray(result)) {
78483
+ const credWallet = this.opts?.credentialWallet;
78484
+ if (!credWallet)
78485
+ throw new Error('Credential wallet is not provided');
78486
+ await credWallet.saveAll(result);
78487
+ return null;
78488
+ }
78489
+ return result;
78490
+ }
78430
78491
  case PROTOCOL_MESSAGE_TYPE.CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE:
78431
78492
  return this.handleFetchRequest(message);
78432
78493
  case PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE:
@@ -78440,8 +78501,7 @@ var PolygonIdSdk = (function (exports) {
78440
78501
  ctx.mediaType = MediaType.ZKPMessage;
78441
78502
  }
78442
78503
  const credentials = [];
78443
- for (let index = 0; index < offerMessage.body.credentials.length; index++) {
78444
- const credentialInfo = offerMessage.body.credentials[index];
78504
+ for (const credentialInfo of offerMessage.body.credentials) {
78445
78505
  const guid = v4();
78446
78506
  const fetchRequest = {
78447
78507
  id: guid,
@@ -78465,7 +78525,6 @@ var PolygonIdSdk = (function (exports) {
78465
78525
  senderDID,
78466
78526
  ...packerOpts
78467
78527
  }));
78468
- let message;
78469
78528
  try {
78470
78529
  if (!offerMessage?.body?.url) {
78471
78530
  throw new Error(`could not fetch W3C credential, body url is missing`);
@@ -78478,14 +78537,18 @@ var PolygonIdSdk = (function (exports) {
78478
78537
  },
78479
78538
  body: token
78480
78539
  });
78481
- if (resp.status !== 200) {
78482
- throw new Error(`could not fetch W3C credential, ${credentialInfo?.id}`);
78540
+ const arrayBuffer = await resp.arrayBuffer();
78541
+ if (!arrayBuffer.byteLength) {
78542
+ throw new Error(`could not fetch , ${credentialInfo?.id}, response is empty`);
78543
+ }
78544
+ const { unpackedMessage: message } = await this._packerMgr.unpack(new Uint8Array(arrayBuffer));
78545
+ if (message.type !== PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE) {
78546
+ return message;
78483
78547
  }
78484
- message = await resp.json();
78485
78548
  credentials.push(W3CCredential.fromJSON(message.body.credential));
78486
78549
  }
78487
78550
  catch (e) {
78488
- throw new Error(`could not fetch W3C credential, ${credentialInfo?.id}, error: ${e.message ?? e}`);
78551
+ throw new Error(`could not fetch protocol message for credential offer id: , ${credentialInfo?.id}, error: ${e.message ?? e}`);
78489
78552
  }
78490
78553
  }
78491
78554
  return credentials;
@@ -78504,11 +78567,15 @@ var PolygonIdSdk = (function (exports) {
78504
78567
  throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
78505
78568
  }
78506
78569
  const offerMessage = await FetchHandler.unpackMessage(this._packerMgr, offer, PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE);
78507
- return this.handleOfferMessage(offerMessage, {
78570
+ const result = await this.handleOfferMessage(offerMessage, {
78508
78571
  mediaType: opts?.mediaType,
78509
78572
  headers: opts?.headers,
78510
78573
  packerOptions: opts?.packerOptions
78511
78574
  });
78575
+ if (Array.isArray(result)) {
78576
+ return result;
78577
+ }
78578
+ throw new Error('invalid protocol message response');
78512
78579
  }
78513
78580
  async handleFetchRequest(msgRequest) {
78514
78581
  if (!msgRequest.to) {
@@ -78521,7 +78588,7 @@ var PolygonIdSdk = (function (exports) {
78521
78588
  const userDID = Z$2.parse(msgRequest.from);
78522
78589
  const credId = msgRequest.body?.id;
78523
78590
  if (!credId) {
78524
- throw new Error('nvalid credential id in fetch request body');
78591
+ throw new Error('invalid credential id in fetch request body');
78525
78592
  }
78526
78593
  if (!this.opts?.credentialWallet) {
78527
78594
  throw new Error('please, provide credential wallet in options');
@@ -79046,6 +79113,202 @@ var PolygonIdSdk = (function (exports) {
79046
79113
  }
79047
79114
  }
79048
79115
 
79116
+ /**
79117
+ * @beta
79118
+ * createPaymentRequest is a function to create protocol payment-request message
79119
+ * @param {DID} sender - sender did
79120
+ * @param {DID} receiver - receiver did
79121
+ * @param {string} agent - agent URL
79122
+ * @param {PaymentRequestInfo[]} payments - payments
79123
+ * @returns `PaymentRequestMessage`
79124
+ */
79125
+ function createPaymentRequest(sender, receiver, agent, payments) {
79126
+ const uuidv4 = v4();
79127
+ const request = {
79128
+ id: uuidv4,
79129
+ thid: uuidv4,
79130
+ from: sender.string(),
79131
+ to: receiver.string(),
79132
+ typ: MediaType.PlainMessage,
79133
+ type: PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE,
79134
+ body: {
79135
+ agent,
79136
+ payments
79137
+ }
79138
+ };
79139
+ return request;
79140
+ }
79141
+ /**
79142
+ * @beta
79143
+ * createPayment is a function to create protocol payment message
79144
+ * @param {DID} sender - sender did
79145
+ * @param {DID} receiver - receiver did
79146
+ * @param {PaymentInfo[]} payments - payments
79147
+ * @returns `PaymentMessage`
79148
+ */
79149
+ function createPayment(sender, receiver, payments) {
79150
+ const uuidv4 = v4();
79151
+ const request = {
79152
+ id: uuidv4,
79153
+ thid: uuidv4,
79154
+ from: sender.string(),
79155
+ to: receiver.string(),
79156
+ typ: MediaType.PlainMessage,
79157
+ type: PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE,
79158
+ body: {
79159
+ payments
79160
+ }
79161
+ };
79162
+ return request;
79163
+ }
79164
+ /**
79165
+ *
79166
+ * Allows to process PaymentRequest protocol message
79167
+ * @beta
79168
+ * @class PaymentHandler
79169
+ * @implements implements IPaymentHandler interface
79170
+ */
79171
+ class PaymentHandler extends AbstractMessageHandler {
79172
+ /**
79173
+ * @beta Creates an instance of PaymentHandler.
79174
+ * @param {IPackageManager} _packerMgr - package manager to unpack message envelope
79175
+ * @param {PaymentHandlerParams} _params - payment handler params
79176
+ *
79177
+ */
79178
+ constructor(_packerMgr, _params) {
79179
+ super();
79180
+ this._packerMgr = _packerMgr;
79181
+ this._params = _params;
79182
+ }
79183
+ async handle(message, context) {
79184
+ switch (message.type) {
79185
+ case PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE:
79186
+ return await this.handlePaymentRequestMessage(message, context);
79187
+ case PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE:
79188
+ await this.handlePayment(message, context);
79189
+ return null;
79190
+ default:
79191
+ return super.handle(message, context);
79192
+ }
79193
+ }
79194
+ /**
79195
+ * @inheritdoc IPaymentHandler#parsePaymentRequest
79196
+ */
79197
+ async parsePaymentRequest(request) {
79198
+ const { unpackedMessage: message } = await this._packerMgr.unpack(request);
79199
+ const paymentRequest = message;
79200
+ if (message.type !== PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE) {
79201
+ throw new Error('Invalid media type');
79202
+ }
79203
+ return paymentRequest;
79204
+ }
79205
+ async handlePaymentRequestMessage(paymentRequest, ctx) {
79206
+ if (!paymentRequest.to) {
79207
+ throw new Error(`failed request. empty 'to' field`);
79208
+ }
79209
+ if (!paymentRequest.from) {
79210
+ throw new Error(`failed request. empty 'from' field`);
79211
+ }
79212
+ if (!paymentRequest.body?.payments?.length) {
79213
+ throw new Error(`failed request. no 'payments' in body`);
79214
+ }
79215
+ if (!ctx.paymentHandler) {
79216
+ throw new Error(`please provide payment handler in context`);
79217
+ }
79218
+ const senderDID = Z$2.parse(paymentRequest.to);
79219
+ const receiverDID = Z$2.parse(paymentRequest.from);
79220
+ const payments = [];
79221
+ for (let i = 0; i < paymentRequest.body.payments.length; i++) {
79222
+ const paymentReq = paymentRequest.body.payments[i];
79223
+ if (paymentReq.type !== exports.PaymentRequestType.PaymentRequest) {
79224
+ throw new Error(`failed request. not supported '${paymentReq.type}' payment type `);
79225
+ }
79226
+ if (paymentReq.data.type !== exports.PaymentRequestDataType.Iden3PaymentRequestCryptoV1) {
79227
+ throw new Error(`failed request. not supported '${paymentReq.data.type}' payment type `);
79228
+ }
79229
+ const txId = await ctx.paymentHandler(paymentReq.data);
79230
+ payments.push({
79231
+ id: paymentReq.data.id,
79232
+ type: exports.PaymentType.Iden3PaymentCryptoV1,
79233
+ paymentData: {
79234
+ txId
79235
+ }
79236
+ });
79237
+ }
79238
+ const paymentMessage = createPayment(senderDID, receiverDID, payments);
79239
+ const response = await this.packMessage(paymentMessage, senderDID);
79240
+ const agentResult = await fetch(paymentRequest.body.agent, {
79241
+ method: 'POST',
79242
+ body: response,
79243
+ headers: {
79244
+ 'Content-Type': 'application/octet-stream'
79245
+ }
79246
+ });
79247
+ const arrayBuffer = await agentResult.arrayBuffer();
79248
+ if (!arrayBuffer.byteLength) {
79249
+ return null;
79250
+ }
79251
+ const { unpackedMessage } = await this._packerMgr.unpack(new Uint8Array(arrayBuffer));
79252
+ return unpackedMessage;
79253
+ }
79254
+ /**
79255
+ * @inheritdoc IPaymentHandler#handlePaymentRequest
79256
+ */
79257
+ async handlePaymentRequest(request, opts) {
79258
+ if (this._params.packerParams.mediaType === MediaType.SignedMessage &&
79259
+ !this._params.packerParams.packerOptions) {
79260
+ throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
79261
+ }
79262
+ const paymentRequest = await this.parsePaymentRequest(request);
79263
+ if (!paymentRequest.from) {
79264
+ throw new Error(`failed request. empty 'from' field`);
79265
+ }
79266
+ if (!paymentRequest.to) {
79267
+ throw new Error(`failed request. empty 'to' field`);
79268
+ }
79269
+ const agentMessage = await this.handlePaymentRequestMessage(paymentRequest, opts);
79270
+ if (!agentMessage) {
79271
+ return null;
79272
+ }
79273
+ const senderDID = Z$2.parse(paymentRequest.to);
79274
+ return this.packMessage(agentMessage, senderDID);
79275
+ }
79276
+ /**
79277
+ * @inheritdoc IPaymentHandler#handlePayment
79278
+ */
79279
+ async handlePayment(payment, opts) {
79280
+ if (opts.paymentRequest.from !== payment.to) {
79281
+ throw new Error(`sender of the request is not a target of response - expected ${opts.paymentRequest.from}, given ${payment.to}`);
79282
+ }
79283
+ if (!payment.body?.payments.length) {
79284
+ throw new Error(`failed request. empty 'payments' field in body`);
79285
+ }
79286
+ for (let i = 0; i < payment.body.payments.length; i++) {
79287
+ const p = payment.body?.payments[i];
79288
+ const paymentRequestData = opts.paymentRequest.body?.payments.find((r) => r.data.id === p.id);
79289
+ if (!paymentRequestData) {
79290
+ throw new Error(`can't find payment request for payment id ${p.id}`);
79291
+ }
79292
+ if (!opts.paymentValidationHandler) {
79293
+ throw new Error(`please provide payment validation handler in options`);
79294
+ }
79295
+ await opts.paymentValidationHandler(p.paymentData.txId, paymentRequestData.data);
79296
+ }
79297
+ }
79298
+ async packMessage(message, senderDID) {
79299
+ const responseEncoded = byteEncoder.encode(JSON.stringify(message));
79300
+ const packerOpts = this._params.packerParams.mediaType === MediaType.SignedMessage
79301
+ ? this._params.packerParams.packerOptions
79302
+ : {
79303
+ provingMethodAlg: T.provingMethodGroth16AuthV2Instance.methodAlg
79304
+ };
79305
+ return await this._packerMgr.pack(this._params.packerParams.mediaType, responseEncoded, {
79306
+ senderDID,
79307
+ ...packerOpts
79308
+ });
79309
+ }
79310
+ }
79311
+
79049
79312
  exports.AbstractMessageHandler = AbstractMessageHandler;
79050
79313
  exports.AbstractPrivateKeyStore = AbstractPrivateKeyStore;
79051
79314
  exports.AgentResolver = AgentResolver;
@@ -79124,6 +79387,7 @@ var PolygonIdSdk = (function (exports) {
79124
79387
  exports.PROTOCOL_CONSTANTS = constants;
79125
79388
  exports.PackageManager = PackageManager;
79126
79389
  exports.Parser = Parser;
79390
+ exports.PaymentHandler = PaymentHandler;
79127
79391
  exports.PlainPacker = PlainPacker;
79128
79392
  exports.ProofNode = ProofNode;
79129
79393
  exports.ProofService = ProofService;
@@ -79175,6 +79439,8 @@ var PolygonIdSdk = (function (exports) {
79175
79439
  exports.core = index$1;
79176
79440
  exports.createAuthorizationRequest = createAuthorizationRequest;
79177
79441
  exports.createAuthorizationRequestWithMessage = createAuthorizationRequestWithMessage;
79442
+ exports.createPayment = createPayment;
79443
+ exports.createPaymentRequest = createPaymentRequest;
79178
79444
  exports.createProposal = createProposal;
79179
79445
  exports.createProposalRequest = createProposalRequest;
79180
79446
  exports.createSchemaHash = createSchemaHash;