@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
@@ -39,7 +39,11 @@ exports.PROTOCOL_MESSAGE_TYPE = Object.freeze({
39
39
  // ProposalRequestMessageType is type for proposal-request message
40
40
  PROPOSAL_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal-request',
41
41
  // ProposalMessageType is type for proposal message
42
- PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal'
42
+ PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal',
43
+ // PaymentRequestMessageType is type for payment-request message
44
+ PAYMENT_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment-request',
45
+ // PaymentMessageType is type for payment message
46
+ PAYMENT_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment'
43
47
  });
44
48
  /**
45
49
  * Media types for iden3 comm communication protocol
@@ -57,9 +57,17 @@ class FetchHandler extends message_handler_1.AbstractMessageHandler {
57
57
  }
58
58
  async handle(message, ctx) {
59
59
  switch (message.type) {
60
- case constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE:
61
- await this.handleOfferMessage(message, ctx);
62
- return null;
60
+ case constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE: {
61
+ const result = await this.handleOfferMessage(message, ctx);
62
+ if (Array.isArray(result)) {
63
+ const credWallet = this.opts?.credentialWallet;
64
+ if (!credWallet)
65
+ throw new Error('Credential wallet is not provided');
66
+ await credWallet.saveAll(result);
67
+ return null;
68
+ }
69
+ return result;
70
+ }
63
71
  case constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE:
64
72
  return this.handleFetchRequest(message);
65
73
  case constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE:
@@ -73,8 +81,7 @@ class FetchHandler extends message_handler_1.AbstractMessageHandler {
73
81
  ctx.mediaType = constants_1.MediaType.ZKPMessage;
74
82
  }
75
83
  const credentials = [];
76
- for (let index = 0; index < offerMessage.body.credentials.length; index++) {
77
- const credentialInfo = offerMessage.body.credentials[index];
84
+ for (const credentialInfo of offerMessage.body.credentials) {
78
85
  const guid = uuid.v4();
79
86
  const fetchRequest = {
80
87
  id: guid,
@@ -98,7 +105,6 @@ class FetchHandler extends message_handler_1.AbstractMessageHandler {
98
105
  senderDID,
99
106
  ...packerOpts
100
107
  }));
101
- let message;
102
108
  try {
103
109
  if (!offerMessage?.body?.url) {
104
110
  throw new Error(`could not fetch W3C credential, body url is missing`);
@@ -111,14 +117,18 @@ class FetchHandler extends message_handler_1.AbstractMessageHandler {
111
117
  },
112
118
  body: token
113
119
  });
114
- if (resp.status !== 200) {
115
- throw new Error(`could not fetch W3C credential, ${credentialInfo?.id}`);
120
+ const arrayBuffer = await resp.arrayBuffer();
121
+ if (!arrayBuffer.byteLength) {
122
+ throw new Error(`could not fetch , ${credentialInfo?.id}, response is empty`);
123
+ }
124
+ const { unpackedMessage: message } = await this._packerMgr.unpack(new Uint8Array(arrayBuffer));
125
+ if (message.type !== constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE) {
126
+ return message;
116
127
  }
117
- message = await resp.json();
118
128
  credentials.push(verifiable_1.W3CCredential.fromJSON(message.body.credential));
119
129
  }
120
130
  catch (e) {
121
- throw new Error(`could not fetch W3C credential, ${credentialInfo?.id}, error: ${e.message ?? e}`);
131
+ throw new Error(`could not fetch protocol message for credential offer id: , ${credentialInfo?.id}, error: ${e.message ?? e}`);
122
132
  }
123
133
  }
124
134
  return credentials;
@@ -137,11 +147,15 @@ class FetchHandler extends message_handler_1.AbstractMessageHandler {
137
147
  throw new Error(`jws packer options are required for ${constants_1.MediaType.SignedMessage}`);
138
148
  }
139
149
  const offerMessage = await FetchHandler.unpackMessage(this._packerMgr, offer, constants_2.PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE);
140
- return this.handleOfferMessage(offerMessage, {
150
+ const result = await this.handleOfferMessage(offerMessage, {
141
151
  mediaType: opts?.mediaType,
142
152
  headers: opts?.headers,
143
153
  packerOptions: opts?.packerOptions
144
154
  });
155
+ if (Array.isArray(result)) {
156
+ return result;
157
+ }
158
+ throw new Error('invalid protocol message response');
145
159
  }
146
160
  async handleFetchRequest(msgRequest) {
147
161
  if (!msgRequest.to) {
@@ -154,7 +168,7 @@ class FetchHandler extends message_handler_1.AbstractMessageHandler {
154
168
  const userDID = js_iden3_core_1.DID.parse(msgRequest.from);
155
169
  const credId = msgRequest.body?.id;
156
170
  if (!credId) {
157
- throw new Error('nvalid credential id in fetch request body');
171
+ throw new Error('invalid credential id in fetch request body');
158
172
  }
159
173
  if (!this.opts?.credentialWallet) {
160
174
  throw new Error('please, provide credential wallet in options');
@@ -22,3 +22,4 @@ __exportStar(require("./revocation-status"), exports);
22
22
  __exportStar(require("./common"), exports);
23
23
  __exportStar(require("./credential-proposal"), exports);
24
24
  __exportStar(require("./message-handler"), exports);
25
+ __exportStar(require("./payment"), exports);
@@ -0,0 +1,232 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.PaymentHandler = exports.createPayment = exports.createPaymentRequest = void 0;
27
+ const constants_1 = require("../constants");
28
+ const constants_2 = require("../constants");
29
+ const js_iden3_core_1 = require("@iden3/js-iden3-core");
30
+ const uuid = __importStar(require("uuid"));
31
+ const js_jwz_1 = require("@iden3/js-jwz");
32
+ const utils_1 = require("../../utils");
33
+ const message_handler_1 = require("./message-handler");
34
+ const verifiable_1 = require("../../verifiable");
35
+ /**
36
+ * @beta
37
+ * createPaymentRequest is a function to create protocol payment-request message
38
+ * @param {DID} sender - sender did
39
+ * @param {DID} receiver - receiver did
40
+ * @param {string} agent - agent URL
41
+ * @param {PaymentRequestInfo[]} payments - payments
42
+ * @returns `PaymentRequestMessage`
43
+ */
44
+ function createPaymentRequest(sender, receiver, agent, payments) {
45
+ const uuidv4 = uuid.v4();
46
+ const request = {
47
+ id: uuidv4,
48
+ thid: uuidv4,
49
+ from: sender.string(),
50
+ to: receiver.string(),
51
+ typ: constants_2.MediaType.PlainMessage,
52
+ type: constants_1.PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE,
53
+ body: {
54
+ agent,
55
+ payments
56
+ }
57
+ };
58
+ return request;
59
+ }
60
+ exports.createPaymentRequest = createPaymentRequest;
61
+ /**
62
+ * @beta
63
+ * createPayment is a function to create protocol payment message
64
+ * @param {DID} sender - sender did
65
+ * @param {DID} receiver - receiver did
66
+ * @param {PaymentInfo[]} payments - payments
67
+ * @returns `PaymentMessage`
68
+ */
69
+ function createPayment(sender, receiver, payments) {
70
+ const uuidv4 = uuid.v4();
71
+ const request = {
72
+ id: uuidv4,
73
+ thid: uuidv4,
74
+ from: sender.string(),
75
+ to: receiver.string(),
76
+ typ: constants_2.MediaType.PlainMessage,
77
+ type: constants_1.PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE,
78
+ body: {
79
+ payments
80
+ }
81
+ };
82
+ return request;
83
+ }
84
+ exports.createPayment = createPayment;
85
+ /**
86
+ *
87
+ * Allows to process PaymentRequest protocol message
88
+ * @beta
89
+ * @class PaymentHandler
90
+ * @implements implements IPaymentHandler interface
91
+ */
92
+ class PaymentHandler extends message_handler_1.AbstractMessageHandler {
93
+ /**
94
+ * @beta Creates an instance of PaymentHandler.
95
+ * @param {IPackageManager} _packerMgr - package manager to unpack message envelope
96
+ * @param {PaymentHandlerParams} _params - payment handler params
97
+ *
98
+ */
99
+ constructor(_packerMgr, _params) {
100
+ super();
101
+ this._packerMgr = _packerMgr;
102
+ this._params = _params;
103
+ }
104
+ async handle(message, context) {
105
+ switch (message.type) {
106
+ case constants_1.PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE:
107
+ return await this.handlePaymentRequestMessage(message, context);
108
+ case constants_1.PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE:
109
+ await this.handlePayment(message, context);
110
+ return null;
111
+ default:
112
+ return super.handle(message, context);
113
+ }
114
+ }
115
+ /**
116
+ * @inheritdoc IPaymentHandler#parsePaymentRequest
117
+ */
118
+ async parsePaymentRequest(request) {
119
+ const { unpackedMessage: message } = await this._packerMgr.unpack(request);
120
+ const paymentRequest = message;
121
+ if (message.type !== constants_1.PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE) {
122
+ throw new Error('Invalid media type');
123
+ }
124
+ return paymentRequest;
125
+ }
126
+ async handlePaymentRequestMessage(paymentRequest, ctx) {
127
+ if (!paymentRequest.to) {
128
+ throw new Error(`failed request. empty 'to' field`);
129
+ }
130
+ if (!paymentRequest.from) {
131
+ throw new Error(`failed request. empty 'from' field`);
132
+ }
133
+ if (!paymentRequest.body?.payments?.length) {
134
+ throw new Error(`failed request. no 'payments' in body`);
135
+ }
136
+ if (!ctx.paymentHandler) {
137
+ throw new Error(`please provide payment handler in context`);
138
+ }
139
+ const senderDID = js_iden3_core_1.DID.parse(paymentRequest.to);
140
+ const receiverDID = js_iden3_core_1.DID.parse(paymentRequest.from);
141
+ const payments = [];
142
+ for (let i = 0; i < paymentRequest.body.payments.length; i++) {
143
+ const paymentReq = paymentRequest.body.payments[i];
144
+ if (paymentReq.type !== verifiable_1.PaymentRequestType.PaymentRequest) {
145
+ throw new Error(`failed request. not supported '${paymentReq.type}' payment type `);
146
+ }
147
+ if (paymentReq.data.type !== verifiable_1.PaymentRequestDataType.Iden3PaymentRequestCryptoV1) {
148
+ throw new Error(`failed request. not supported '${paymentReq.data.type}' payment type `);
149
+ }
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
+ }
159
+ const paymentMessage = createPayment(senderDID, receiverDID, payments);
160
+ const response = await this.packMessage(paymentMessage, senderDID);
161
+ const agentResult = await fetch(paymentRequest.body.agent, {
162
+ method: 'POST',
163
+ body: response,
164
+ headers: {
165
+ 'Content-Type': 'application/octet-stream'
166
+ }
167
+ });
168
+ const arrayBuffer = await agentResult.arrayBuffer();
169
+ if (!arrayBuffer.byteLength) {
170
+ return null;
171
+ }
172
+ const { unpackedMessage } = await this._packerMgr.unpack(new Uint8Array(arrayBuffer));
173
+ return unpackedMessage;
174
+ }
175
+ /**
176
+ * @inheritdoc IPaymentHandler#handlePaymentRequest
177
+ */
178
+ async handlePaymentRequest(request, opts) {
179
+ if (this._params.packerParams.mediaType === constants_2.MediaType.SignedMessage &&
180
+ !this._params.packerParams.packerOptions) {
181
+ throw new Error(`jws packer options are required for ${constants_2.MediaType.SignedMessage}`);
182
+ }
183
+ const paymentRequest = await this.parsePaymentRequest(request);
184
+ if (!paymentRequest.from) {
185
+ throw new Error(`failed request. empty 'from' field`);
186
+ }
187
+ if (!paymentRequest.to) {
188
+ throw new Error(`failed request. empty 'to' field`);
189
+ }
190
+ const agentMessage = await this.handlePaymentRequestMessage(paymentRequest, opts);
191
+ if (!agentMessage) {
192
+ return null;
193
+ }
194
+ const senderDID = js_iden3_core_1.DID.parse(paymentRequest.to);
195
+ return this.packMessage(agentMessage, senderDID);
196
+ }
197
+ /**
198
+ * @inheritdoc IPaymentHandler#handlePayment
199
+ */
200
+ async handlePayment(payment, opts) {
201
+ if (opts.paymentRequest.from !== payment.to) {
202
+ throw new Error(`sender of the request is not a target of response - expected ${opts.paymentRequest.from}, given ${payment.to}`);
203
+ }
204
+ if (!payment.body?.payments.length) {
205
+ throw new Error(`failed request. empty 'payments' field in body`);
206
+ }
207
+ for (let i = 0; i < payment.body.payments.length; i++) {
208
+ const p = payment.body?.payments[i];
209
+ const paymentRequestData = opts.paymentRequest.body?.payments.find((r) => r.data.id === p.id);
210
+ if (!paymentRequestData) {
211
+ throw new Error(`can't find payment request for payment id ${p.id}`);
212
+ }
213
+ if (!opts.paymentValidationHandler) {
214
+ throw new Error(`please provide payment validation handler in options`);
215
+ }
216
+ await opts.paymentValidationHandler(p.paymentData.txId, paymentRequestData.data);
217
+ }
218
+ }
219
+ async packMessage(message, senderDID) {
220
+ const responseEncoded = utils_1.byteEncoder.encode(JSON.stringify(message));
221
+ const packerOpts = this._params.packerParams.mediaType === constants_2.MediaType.SignedMessage
222
+ ? this._params.packerParams.packerOptions
223
+ : {
224
+ provingMethodAlg: js_jwz_1.proving.provingMethodGroth16AuthV2Instance.methodAlg
225
+ };
226
+ return await this._packerMgr.pack(this._params.packerParams.mediaType, responseEncoded, {
227
+ senderDID,
228
+ ...packerOpts
229
+ });
230
+ }
231
+ }
232
+ exports.PaymentHandler = PaymentHandler;
@@ -21,5 +21,6 @@ __exportStar(require("./protocol/proof"), exports);
21
21
  __exportStar(require("./protocol/revocation"), exports);
22
22
  __exportStar(require("./protocol/contract-request"), exports);
23
23
  __exportStar(require("./protocol/proposal-request"), exports);
24
+ __exportStar(require("./protocol/payment"), exports);
24
25
  __exportStar(require("./packer"), exports);
25
26
  __exportStar(require("./packageManager"), exports);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -129,10 +129,23 @@ const parseQueryMetadata = async (propertyQuery, ldContextJSON, credentialType,
129
129
  if (!(0, circuits_1.isValidOperation)(query.datatype, propertyQuery.operator)) {
130
130
  throw new Error(`operator ${propertyQuery.operator} is not supported for datatype ${query.datatype}`);
131
131
  }
132
- query.values =
133
- propertyQuery.operator === circuits_1.Operators.EXISTS
134
- ? transformExistsValue(propertyQuery.operatorValue)
135
- : await (0, exports.transformQueryValueToBigInts)(propertyQuery.operatorValue, query.datatype);
132
+ if ((propertyQuery.operator === circuits_1.Operators.NOOP || propertyQuery.operator === circuits_1.Operators.SD) &&
133
+ propertyQuery.operatorValue) {
134
+ throw new Error(`operator value should be undefined for ${propertyQuery.operator} operator`);
135
+ }
136
+ let values;
137
+ switch (propertyQuery.operator) {
138
+ case circuits_1.Operators.NOOP:
139
+ case circuits_1.Operators.SD:
140
+ values = [];
141
+ break;
142
+ case circuits_1.Operators.EXISTS:
143
+ values = transformExistsValue(propertyQuery.operatorValue);
144
+ break;
145
+ default:
146
+ values = await (0, exports.transformQueryValueToBigInts)(propertyQuery.operatorValue, query.datatype);
147
+ }
148
+ query.values = values;
136
149
  }
137
150
  return query;
138
151
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RefreshServiceType = exports.SubjectPosition = exports.MerklizedRootPosition = exports.ProofPurpose = exports.CredentialStatusType = exports.ProofType = exports.VerifiableConstants = void 0;
3
+ exports.SupportedCurrencies = exports.PaymentType = exports.PaymentRequestDataType = exports.PaymentRequestType = exports.RefreshServiceType = exports.SubjectPosition = exports.MerklizedRootPosition = exports.ProofPurpose = exports.CredentialStatusType = exports.ProofType = exports.VerifiableConstants = void 0;
4
4
  /** Verifiable constants */
5
5
  exports.VerifiableConstants = Object.freeze({
6
6
  ERRORS: {
@@ -113,3 +113,39 @@ var RefreshServiceType;
113
113
  (function (RefreshServiceType) {
114
114
  RefreshServiceType["Iden3RefreshService2023"] = "Iden3RefreshService2023";
115
115
  })(RefreshServiceType = exports.RefreshServiceType || (exports.RefreshServiceType = {}));
116
+ /**
117
+ * PaymentRequestType type for payment requests
118
+ * @beta
119
+ * @enum {string}
120
+ */
121
+ var PaymentRequestType;
122
+ (function (PaymentRequestType) {
123
+ PaymentRequestType["PaymentRequest"] = "PaymentRequest";
124
+ })(PaymentRequestType = exports.PaymentRequestType || (exports.PaymentRequestType = {}));
125
+ /**
126
+ * PaymentRequestDataType type for payment requests
127
+ * @beta
128
+ * @enum {string}
129
+ */
130
+ var PaymentRequestDataType;
131
+ (function (PaymentRequestDataType) {
132
+ PaymentRequestDataType["Iden3PaymentRequestCryptoV1"] = "Iden3PaymentRequestCryptoV1";
133
+ })(PaymentRequestDataType = exports.PaymentRequestDataType || (exports.PaymentRequestDataType = {}));
134
+ /**
135
+ * PaymentType type for payment responses
136
+ * @beta
137
+ * @enum {string}
138
+ */
139
+ var PaymentType;
140
+ (function (PaymentType) {
141
+ PaymentType["Iden3PaymentCryptoV1"] = "Iden3PaymentCryptoV1";
142
+ })(PaymentType = exports.PaymentType || (exports.PaymentType = {}));
143
+ /**
144
+ * Media types for Payment supported currencies
145
+ * @beta
146
+ * @enum {number}
147
+ */
148
+ var SupportedCurrencies;
149
+ (function (SupportedCurrencies) {
150
+ SupportedCurrencies["ETH"] = "ETH";
151
+ })(SupportedCurrencies = exports.SupportedCurrencies || (exports.SupportedCurrencies = {}));
@@ -36,7 +36,11 @@ export const PROTOCOL_MESSAGE_TYPE = Object.freeze({
36
36
  // ProposalRequestMessageType is type for proposal-request message
37
37
  PROPOSAL_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal-request',
38
38
  // ProposalMessageType is type for proposal message
39
- PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal'
39
+ PROPOSAL_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/proposal',
40
+ // PaymentRequestMessageType is type for payment-request message
41
+ PAYMENT_REQUEST_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment-request',
42
+ // PaymentMessageType is type for payment message
43
+ PAYMENT_MESSAGE_TYPE: IDEN3_PROTOCOL + 'credentials/0.1/payment'
40
44
  });
41
45
  /**
42
46
  * Media types for iden3 comm communication protocol
@@ -31,9 +31,17 @@ export class FetchHandler extends AbstractMessageHandler {
31
31
  }
32
32
  async handle(message, ctx) {
33
33
  switch (message.type) {
34
- case PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE:
35
- await this.handleOfferMessage(message, ctx);
36
- return null;
34
+ case PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE: {
35
+ const result = await this.handleOfferMessage(message, ctx);
36
+ if (Array.isArray(result)) {
37
+ const credWallet = this.opts?.credentialWallet;
38
+ if (!credWallet)
39
+ throw new Error('Credential wallet is not provided');
40
+ await credWallet.saveAll(result);
41
+ return null;
42
+ }
43
+ return result;
44
+ }
37
45
  case PROTOCOL_MESSAGE_TYPE.CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE:
38
46
  return this.handleFetchRequest(message);
39
47
  case PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE:
@@ -47,8 +55,7 @@ export class FetchHandler extends AbstractMessageHandler {
47
55
  ctx.mediaType = MediaType.ZKPMessage;
48
56
  }
49
57
  const credentials = [];
50
- for (let index = 0; index < offerMessage.body.credentials.length; index++) {
51
- const credentialInfo = offerMessage.body.credentials[index];
58
+ for (const credentialInfo of offerMessage.body.credentials) {
52
59
  const guid = uuid.v4();
53
60
  const fetchRequest = {
54
61
  id: guid,
@@ -72,7 +79,6 @@ export class FetchHandler extends AbstractMessageHandler {
72
79
  senderDID,
73
80
  ...packerOpts
74
81
  }));
75
- let message;
76
82
  try {
77
83
  if (!offerMessage?.body?.url) {
78
84
  throw new Error(`could not fetch W3C credential, body url is missing`);
@@ -85,14 +91,18 @@ export class FetchHandler extends AbstractMessageHandler {
85
91
  },
86
92
  body: token
87
93
  });
88
- if (resp.status !== 200) {
89
- throw new Error(`could not fetch W3C credential, ${credentialInfo?.id}`);
94
+ const arrayBuffer = await resp.arrayBuffer();
95
+ if (!arrayBuffer.byteLength) {
96
+ throw new Error(`could not fetch , ${credentialInfo?.id}, response is empty`);
97
+ }
98
+ const { unpackedMessage: message } = await this._packerMgr.unpack(new Uint8Array(arrayBuffer));
99
+ if (message.type !== PROTOCOL_MESSAGE_TYPE.CREDENTIAL_ISSUANCE_RESPONSE_MESSAGE_TYPE) {
100
+ return message;
90
101
  }
91
- message = await resp.json();
92
102
  credentials.push(W3CCredential.fromJSON(message.body.credential));
93
103
  }
94
104
  catch (e) {
95
- throw new Error(`could not fetch W3C credential, ${credentialInfo?.id}, error: ${e.message ?? e}`);
105
+ throw new Error(`could not fetch protocol message for credential offer id: , ${credentialInfo?.id}, error: ${e.message ?? e}`);
96
106
  }
97
107
  }
98
108
  return credentials;
@@ -111,11 +121,15 @@ export class FetchHandler extends AbstractMessageHandler {
111
121
  throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
112
122
  }
113
123
  const offerMessage = await FetchHandler.unpackMessage(this._packerMgr, offer, PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE);
114
- return this.handleOfferMessage(offerMessage, {
124
+ const result = await this.handleOfferMessage(offerMessage, {
115
125
  mediaType: opts?.mediaType,
116
126
  headers: opts?.headers,
117
127
  packerOptions: opts?.packerOptions
118
128
  });
129
+ if (Array.isArray(result)) {
130
+ return result;
131
+ }
132
+ throw new Error('invalid protocol message response');
119
133
  }
120
134
  async handleFetchRequest(msgRequest) {
121
135
  if (!msgRequest.to) {
@@ -128,7 +142,7 @@ export class FetchHandler extends AbstractMessageHandler {
128
142
  const userDID = DID.parse(msgRequest.from);
129
143
  const credId = msgRequest.body?.id;
130
144
  if (!credId) {
131
- throw new Error('nvalid credential id in fetch request body');
145
+ throw new Error('invalid credential id in fetch request body');
132
146
  }
133
147
  if (!this.opts?.credentialWallet) {
134
148
  throw new Error('please, provide credential wallet in options');
@@ -6,3 +6,4 @@ export * from './revocation-status';
6
6
  export * from './common';
7
7
  export * from './credential-proposal';
8
8
  export * from './message-handler';
9
+ export * from './payment';