@0xpolygonid/js-sdk 1.12.0 → 1.13.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.
@@ -7496,6 +7496,33 @@ 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
+ *
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
+ *
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
+ *
7520
+ * @enum {string}
7521
+ */
7522
+ exports.PaymentType = void 0;
7523
+ (function (PaymentType) {
7524
+ PaymentType["Iden3PaymentCryptoV1"] = "Iden3PaymentCryptoV1";
7525
+ })(exports.PaymentType || (exports.PaymentType = {}));
7499
7526
 
7500
7527
  /**
7501
7528
  * Iden3SparseMerkleProof is a iden3 protocol merkle tree proof
@@ -53622,7 +53649,11 @@ var PolygonIdSdk = (function (exports) {
53622
53649
  // ProposalRequestMessageType is type for proposal-request message
53623
53650
  PROPOSAL_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal-request',
53624
53651
  // ProposalMessageType is type for proposal message
53625
- PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal'
53652
+ PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal',
53653
+ // PaymentRequestMessageType is type for payment-request message
53654
+ PAYMENT_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment-request',
53655
+ // PaymentMessageType is type for payment message
53656
+ PAYMENT_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment'
53626
53657
  });
53627
53658
  /**
53628
53659
  * Media types for iden3 comm communication protocol
@@ -79046,6 +79077,202 @@ var PolygonIdSdk = (function (exports) {
79046
79077
  }
79047
79078
  }
79048
79079
 
79080
+ /**
79081
+ * @beta
79082
+ * createPaymentRequest is a function to create protocol payment-request message
79083
+ * @param {DID} sender - sender did
79084
+ * @param {DID} receiver - receiver did
79085
+ * @param {string} agent - agent URL
79086
+ * @param {PaymentRequestInfo[]} payments - payments
79087
+ * @returns `PaymentRequestMessage`
79088
+ */
79089
+ function createPaymentRequest(sender, receiver, agent, payments) {
79090
+ const uuidv4 = v4();
79091
+ const request = {
79092
+ id: uuidv4,
79093
+ thid: uuidv4,
79094
+ from: sender.string(),
79095
+ to: receiver.string(),
79096
+ typ: MediaType.PlainMessage,
79097
+ type: PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE,
79098
+ body: {
79099
+ agent,
79100
+ payments
79101
+ }
79102
+ };
79103
+ return request;
79104
+ }
79105
+ /**
79106
+ * @beta
79107
+ * createPayment is a function to create protocol payment message
79108
+ * @param {DID} sender - sender did
79109
+ * @param {DID} receiver - receiver did
79110
+ * @param {PaymentInfo[]} payments - payments
79111
+ * @returns `PaymentMessage`
79112
+ */
79113
+ function createPayment(sender, receiver, payments) {
79114
+ const uuidv4 = v4();
79115
+ const request = {
79116
+ id: uuidv4,
79117
+ thid: uuidv4,
79118
+ from: sender.string(),
79119
+ to: receiver.string(),
79120
+ typ: MediaType.PlainMessage,
79121
+ type: PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE,
79122
+ body: {
79123
+ payments
79124
+ }
79125
+ };
79126
+ return request;
79127
+ }
79128
+ /**
79129
+ *
79130
+ * Allows to process PaymentRequest protocol message
79131
+ * @beta
79132
+ * @class PaymentHandler
79133
+ * @implements implements IPaymentHandler interface
79134
+ */
79135
+ class PaymentHandler extends AbstractMessageHandler {
79136
+ /**
79137
+ * @beta Creates an instance of PaymentHandler.
79138
+ * @param {IPackageManager} _packerMgr - package manager to unpack message envelope
79139
+ * @param {PaymentHandlerParams} _params - payment handler params
79140
+ *
79141
+ */
79142
+ constructor(_packerMgr, _params) {
79143
+ super();
79144
+ this._packerMgr = _packerMgr;
79145
+ this._params = _params;
79146
+ }
79147
+ async handle(message, context) {
79148
+ switch (message.type) {
79149
+ case PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE:
79150
+ return await this.handlePaymentRequestMessage(message, context);
79151
+ case PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE:
79152
+ await this.handlePayment(message, context);
79153
+ return null;
79154
+ default:
79155
+ return super.handle(message, context);
79156
+ }
79157
+ }
79158
+ /**
79159
+ * @inheritdoc IPaymentHandler#parsePaymentRequest
79160
+ */
79161
+ async parsePaymentRequest(request) {
79162
+ const { unpackedMessage: message } = await this._packerMgr.unpack(request);
79163
+ const paymentRequest = message;
79164
+ if (message.type !== PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE) {
79165
+ throw new Error('Invalid media type');
79166
+ }
79167
+ return paymentRequest;
79168
+ }
79169
+ async handlePaymentRequestMessage(paymentRequest, ctx) {
79170
+ if (!paymentRequest.to) {
79171
+ throw new Error(`failed request. empty 'to' field`);
79172
+ }
79173
+ if (!paymentRequest.from) {
79174
+ throw new Error(`failed request. empty 'from' field`);
79175
+ }
79176
+ if (!paymentRequest.body?.payments?.length) {
79177
+ throw new Error(`failed request. no 'payments' in body`);
79178
+ }
79179
+ if (!ctx.paymentHandler) {
79180
+ throw new Error(`please provide payment handler in context`);
79181
+ }
79182
+ const senderDID = Z$2.parse(paymentRequest.to);
79183
+ const receiverDID = Z$2.parse(paymentRequest.from);
79184
+ const payments = [];
79185
+ for (let i = 0; i < paymentRequest.body.payments.length; i++) {
79186
+ const paymentReq = paymentRequest.body.payments[i];
79187
+ if (paymentReq.type !== exports.PaymentRequestType.PaymentRequest) {
79188
+ throw new Error(`failed request. not supported '${paymentReq.type}' payment type `);
79189
+ }
79190
+ if (paymentReq.data.type !== exports.PaymentRequestDataType.Iden3PaymentRequestCryptoV1) {
79191
+ throw new Error(`failed request. not supported '${paymentReq.data.type}' payment type `);
79192
+ }
79193
+ const txId = await ctx.paymentHandler(paymentReq.data);
79194
+ payments.push({
79195
+ id: paymentReq.data.id,
79196
+ type: exports.PaymentType.Iden3PaymentCryptoV1,
79197
+ paymentData: {
79198
+ txId
79199
+ }
79200
+ });
79201
+ }
79202
+ const paymentMessage = createPayment(senderDID, receiverDID, payments);
79203
+ const response = await this.packMessage(paymentMessage, senderDID);
79204
+ const agentResult = await fetch(paymentRequest.body.agent, {
79205
+ method: 'POST',
79206
+ body: response,
79207
+ headers: {
79208
+ 'Content-Type': 'application/octet-stream'
79209
+ }
79210
+ });
79211
+ const arrayBuffer = await agentResult.arrayBuffer();
79212
+ if (!arrayBuffer.byteLength) {
79213
+ return null;
79214
+ }
79215
+ const { unpackedMessage } = await this._packerMgr.unpack(new Uint8Array(arrayBuffer));
79216
+ return unpackedMessage;
79217
+ }
79218
+ /**
79219
+ * @inheritdoc IPaymentHandler#handlePaymentRequest
79220
+ */
79221
+ async handlePaymentRequest(request, opts) {
79222
+ if (this._params.packerParams.mediaType === MediaType.SignedMessage &&
79223
+ !this._params.packerParams.packerOptions) {
79224
+ throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
79225
+ }
79226
+ const paymentRequest = await this.parsePaymentRequest(request);
79227
+ if (!paymentRequest.from) {
79228
+ throw new Error(`failed request. empty 'from' field`);
79229
+ }
79230
+ if (!paymentRequest.to) {
79231
+ throw new Error(`failed request. empty 'to' field`);
79232
+ }
79233
+ const agentMessage = await this.handlePaymentRequestMessage(paymentRequest, opts);
79234
+ if (!agentMessage) {
79235
+ return null;
79236
+ }
79237
+ const senderDID = Z$2.parse(paymentRequest.to);
79238
+ return this.packMessage(agentMessage, senderDID);
79239
+ }
79240
+ /**
79241
+ * @inheritdoc IPaymentHandler#handlePayment
79242
+ */
79243
+ async handlePayment(payment, opts) {
79244
+ if (opts.paymentRequest.from !== payment.to) {
79245
+ throw new Error(`sender of the request is not a target of response - expected ${opts.paymentRequest.from}, given ${payment.to}`);
79246
+ }
79247
+ if (!payment.body?.payments.length) {
79248
+ throw new Error(`failed request. empty 'payments' field in body`);
79249
+ }
79250
+ for (let i = 0; i < payment.body.payments.length; i++) {
79251
+ const p = payment.body?.payments[i];
79252
+ const paymentRequestData = opts.paymentRequest.body?.payments.find((r) => r.data.id === p.id);
79253
+ if (!paymentRequestData) {
79254
+ throw new Error(`can't find payment request for payment id ${p.id}`);
79255
+ }
79256
+ if (!opts.paymentValidationHandler) {
79257
+ throw new Error(`please provide payment validation handler in options`);
79258
+ }
79259
+ await opts.paymentValidationHandler(p.paymentData.txId, paymentRequestData.data);
79260
+ }
79261
+ }
79262
+ async packMessage(message, senderDID) {
79263
+ const responseEncoded = byteEncoder.encode(JSON.stringify(message));
79264
+ const packerOpts = this._params.packerParams.mediaType === MediaType.SignedMessage
79265
+ ? this._params.packerParams.packerOptions
79266
+ : {
79267
+ provingMethodAlg: T.provingMethodGroth16AuthV2Instance.methodAlg
79268
+ };
79269
+ return await this._packerMgr.pack(this._params.packerParams.mediaType, responseEncoded, {
79270
+ senderDID,
79271
+ ...packerOpts
79272
+ });
79273
+ }
79274
+ }
79275
+
79049
79276
  exports.AbstractMessageHandler = AbstractMessageHandler;
79050
79277
  exports.AbstractPrivateKeyStore = AbstractPrivateKeyStore;
79051
79278
  exports.AgentResolver = AgentResolver;
@@ -79124,6 +79351,7 @@ var PolygonIdSdk = (function (exports) {
79124
79351
  exports.PROTOCOL_CONSTANTS = constants;
79125
79352
  exports.PackageManager = PackageManager;
79126
79353
  exports.Parser = Parser;
79354
+ exports.PaymentHandler = PaymentHandler;
79127
79355
  exports.PlainPacker = PlainPacker;
79128
79356
  exports.ProofNode = ProofNode;
79129
79357
  exports.ProofService = ProofService;
@@ -79175,6 +79403,8 @@ var PolygonIdSdk = (function (exports) {
79175
79403
  exports.core = index$1;
79176
79404
  exports.createAuthorizationRequest = createAuthorizationRequest;
79177
79405
  exports.createAuthorizationRequestWithMessage = createAuthorizationRequestWithMessage;
79406
+ exports.createPayment = createPayment;
79407
+ exports.createPaymentRequest = createPaymentRequest;
79178
79408
  exports.createProposal = createProposal;
79179
79409
  exports.createProposalRequest = createProposalRequest;
79180
79410
  exports.createSchemaHash = createSchemaHash;