@0xpolygonid/js-sdk 1.11.1 → 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.
- package/dist/browser/esm/index.js +229 -2
- package/dist/browser/esm/index.js.map +1 -1
- package/dist/browser/umd/index.js +287 -53
- package/dist/browser/umd/index.js.map +1 -1
- package/dist/node/cjs/iden3comm/constants.js +5 -1
- package/dist/node/cjs/iden3comm/handlers/index.js +1 -0
- package/dist/node/cjs/iden3comm/handlers/payment.js +232 -0
- package/dist/node/cjs/iden3comm/types/index.js +1 -0
- package/dist/node/cjs/iden3comm/types/protocol/payment.js +2 -0
- package/dist/node/cjs/verifiable/constants.js +28 -1
- package/dist/node/esm/iden3comm/constants.js +5 -1
- package/dist/node/esm/iden3comm/handlers/index.js +1 -0
- package/dist/node/esm/iden3comm/handlers/payment.js +203 -0
- package/dist/node/esm/iden3comm/types/index.js +1 -0
- package/dist/node/esm/iden3comm/types/protocol/payment.js +1 -0
- package/dist/node/esm/verifiable/constants.js +27 -0
- package/dist/types/iden3comm/constants.d.ts +2 -0
- package/dist/types/iden3comm/handlers/index.d.ts +1 -0
- package/dist/types/iden3comm/handlers/message-handler.d.ts +2 -1
- package/dist/types/iden3comm/handlers/payment.d.ts +100 -0
- package/dist/types/iden3comm/types/index.d.ts +1 -0
- package/dist/types/iden3comm/types/protocol/payment.d.ts +47 -0
- package/dist/types/verifiable/constants.d.ts +24 -0
- package/package.json +4 -4
|
@@ -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
|
|
@@ -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);
|
|
@@ -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.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,30 @@ 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
|
+
*
|
|
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
|
+
*
|
|
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
|
+
*
|
|
137
|
+
* @enum {string}
|
|
138
|
+
*/
|
|
139
|
+
var PaymentType;
|
|
140
|
+
(function (PaymentType) {
|
|
141
|
+
PaymentType["Iden3PaymentCryptoV1"] = "Iden3PaymentCryptoV1";
|
|
142
|
+
})(PaymentType = exports.PaymentType || (exports.PaymentType = {}));
|
|
@@ -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
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { PROTOCOL_MESSAGE_TYPE } from '../constants';
|
|
2
|
+
import { MediaType } from '../constants';
|
|
3
|
+
import { DID } from '@iden3/js-iden3-core';
|
|
4
|
+
import * as uuid from 'uuid';
|
|
5
|
+
import { proving } from '@iden3/js-jwz';
|
|
6
|
+
import { byteEncoder } from '../../utils';
|
|
7
|
+
import { AbstractMessageHandler } from './message-handler';
|
|
8
|
+
import { PaymentRequestDataType, PaymentRequestType, PaymentType } from '../../verifiable';
|
|
9
|
+
/**
|
|
10
|
+
* @beta
|
|
11
|
+
* createPaymentRequest is a function to create protocol payment-request message
|
|
12
|
+
* @param {DID} sender - sender did
|
|
13
|
+
* @param {DID} receiver - receiver did
|
|
14
|
+
* @param {string} agent - agent URL
|
|
15
|
+
* @param {PaymentRequestInfo[]} payments - payments
|
|
16
|
+
* @returns `PaymentRequestMessage`
|
|
17
|
+
*/
|
|
18
|
+
export function createPaymentRequest(sender, receiver, agent, payments) {
|
|
19
|
+
const uuidv4 = uuid.v4();
|
|
20
|
+
const request = {
|
|
21
|
+
id: uuidv4,
|
|
22
|
+
thid: uuidv4,
|
|
23
|
+
from: sender.string(),
|
|
24
|
+
to: receiver.string(),
|
|
25
|
+
typ: MediaType.PlainMessage,
|
|
26
|
+
type: PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE,
|
|
27
|
+
body: {
|
|
28
|
+
agent,
|
|
29
|
+
payments
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
return request;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @beta
|
|
36
|
+
* createPayment is a function to create protocol payment message
|
|
37
|
+
* @param {DID} sender - sender did
|
|
38
|
+
* @param {DID} receiver - receiver did
|
|
39
|
+
* @param {PaymentInfo[]} payments - payments
|
|
40
|
+
* @returns `PaymentMessage`
|
|
41
|
+
*/
|
|
42
|
+
export function createPayment(sender, receiver, payments) {
|
|
43
|
+
const uuidv4 = uuid.v4();
|
|
44
|
+
const request = {
|
|
45
|
+
id: uuidv4,
|
|
46
|
+
thid: uuidv4,
|
|
47
|
+
from: sender.string(),
|
|
48
|
+
to: receiver.string(),
|
|
49
|
+
typ: MediaType.PlainMessage,
|
|
50
|
+
type: PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE,
|
|
51
|
+
body: {
|
|
52
|
+
payments
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
return request;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
*
|
|
59
|
+
* Allows to process PaymentRequest protocol message
|
|
60
|
+
* @beta
|
|
61
|
+
* @class PaymentHandler
|
|
62
|
+
* @implements implements IPaymentHandler interface
|
|
63
|
+
*/
|
|
64
|
+
export class PaymentHandler extends AbstractMessageHandler {
|
|
65
|
+
/**
|
|
66
|
+
* @beta Creates an instance of PaymentHandler.
|
|
67
|
+
* @param {IPackageManager} _packerMgr - package manager to unpack message envelope
|
|
68
|
+
* @param {PaymentHandlerParams} _params - payment handler params
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
constructor(_packerMgr, _params) {
|
|
72
|
+
super();
|
|
73
|
+
this._packerMgr = _packerMgr;
|
|
74
|
+
this._params = _params;
|
|
75
|
+
}
|
|
76
|
+
async handle(message, context) {
|
|
77
|
+
switch (message.type) {
|
|
78
|
+
case PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE:
|
|
79
|
+
return await this.handlePaymentRequestMessage(message, context);
|
|
80
|
+
case PROTOCOL_MESSAGE_TYPE.PAYMENT_MESSAGE_TYPE:
|
|
81
|
+
await this.handlePayment(message, context);
|
|
82
|
+
return null;
|
|
83
|
+
default:
|
|
84
|
+
return super.handle(message, context);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @inheritdoc IPaymentHandler#parsePaymentRequest
|
|
89
|
+
*/
|
|
90
|
+
async parsePaymentRequest(request) {
|
|
91
|
+
const { unpackedMessage: message } = await this._packerMgr.unpack(request);
|
|
92
|
+
const paymentRequest = message;
|
|
93
|
+
if (message.type !== PROTOCOL_MESSAGE_TYPE.PAYMENT_REQUEST_MESSAGE_TYPE) {
|
|
94
|
+
throw new Error('Invalid media type');
|
|
95
|
+
}
|
|
96
|
+
return paymentRequest;
|
|
97
|
+
}
|
|
98
|
+
async handlePaymentRequestMessage(paymentRequest, ctx) {
|
|
99
|
+
if (!paymentRequest.to) {
|
|
100
|
+
throw new Error(`failed request. empty 'to' field`);
|
|
101
|
+
}
|
|
102
|
+
if (!paymentRequest.from) {
|
|
103
|
+
throw new Error(`failed request. empty 'from' field`);
|
|
104
|
+
}
|
|
105
|
+
if (!paymentRequest.body?.payments?.length) {
|
|
106
|
+
throw new Error(`failed request. no 'payments' in body`);
|
|
107
|
+
}
|
|
108
|
+
if (!ctx.paymentHandler) {
|
|
109
|
+
throw new Error(`please provide payment handler in context`);
|
|
110
|
+
}
|
|
111
|
+
const senderDID = DID.parse(paymentRequest.to);
|
|
112
|
+
const receiverDID = DID.parse(paymentRequest.from);
|
|
113
|
+
const payments = [];
|
|
114
|
+
for (let i = 0; i < paymentRequest.body.payments.length; i++) {
|
|
115
|
+
const paymentReq = paymentRequest.body.payments[i];
|
|
116
|
+
if (paymentReq.type !== PaymentRequestType.PaymentRequest) {
|
|
117
|
+
throw new Error(`failed request. not supported '${paymentReq.type}' payment type `);
|
|
118
|
+
}
|
|
119
|
+
if (paymentReq.data.type !== PaymentRequestDataType.Iden3PaymentRequestCryptoV1) {
|
|
120
|
+
throw new Error(`failed request. not supported '${paymentReq.data.type}' payment type `);
|
|
121
|
+
}
|
|
122
|
+
const txId = await ctx.paymentHandler(paymentReq.data);
|
|
123
|
+
payments.push({
|
|
124
|
+
id: paymentReq.data.id,
|
|
125
|
+
type: PaymentType.Iden3PaymentCryptoV1,
|
|
126
|
+
paymentData: {
|
|
127
|
+
txId
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
const paymentMessage = createPayment(senderDID, receiverDID, payments);
|
|
132
|
+
const response = await this.packMessage(paymentMessage, senderDID);
|
|
133
|
+
const agentResult = await fetch(paymentRequest.body.agent, {
|
|
134
|
+
method: 'POST',
|
|
135
|
+
body: response,
|
|
136
|
+
headers: {
|
|
137
|
+
'Content-Type': 'application/octet-stream'
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
const arrayBuffer = await agentResult.arrayBuffer();
|
|
141
|
+
if (!arrayBuffer.byteLength) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const { unpackedMessage } = await this._packerMgr.unpack(new Uint8Array(arrayBuffer));
|
|
145
|
+
return unpackedMessage;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @inheritdoc IPaymentHandler#handlePaymentRequest
|
|
149
|
+
*/
|
|
150
|
+
async handlePaymentRequest(request, opts) {
|
|
151
|
+
if (this._params.packerParams.mediaType === MediaType.SignedMessage &&
|
|
152
|
+
!this._params.packerParams.packerOptions) {
|
|
153
|
+
throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
|
|
154
|
+
}
|
|
155
|
+
const paymentRequest = await this.parsePaymentRequest(request);
|
|
156
|
+
if (!paymentRequest.from) {
|
|
157
|
+
throw new Error(`failed request. empty 'from' field`);
|
|
158
|
+
}
|
|
159
|
+
if (!paymentRequest.to) {
|
|
160
|
+
throw new Error(`failed request. empty 'to' field`);
|
|
161
|
+
}
|
|
162
|
+
const agentMessage = await this.handlePaymentRequestMessage(paymentRequest, opts);
|
|
163
|
+
if (!agentMessage) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
const senderDID = DID.parse(paymentRequest.to);
|
|
167
|
+
return this.packMessage(agentMessage, senderDID);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* @inheritdoc IPaymentHandler#handlePayment
|
|
171
|
+
*/
|
|
172
|
+
async handlePayment(payment, opts) {
|
|
173
|
+
if (opts.paymentRequest.from !== payment.to) {
|
|
174
|
+
throw new Error(`sender of the request is not a target of response - expected ${opts.paymentRequest.from}, given ${payment.to}`);
|
|
175
|
+
}
|
|
176
|
+
if (!payment.body?.payments.length) {
|
|
177
|
+
throw new Error(`failed request. empty 'payments' field in body`);
|
|
178
|
+
}
|
|
179
|
+
for (let i = 0; i < payment.body.payments.length; i++) {
|
|
180
|
+
const p = payment.body?.payments[i];
|
|
181
|
+
const paymentRequestData = opts.paymentRequest.body?.payments.find((r) => r.data.id === p.id);
|
|
182
|
+
if (!paymentRequestData) {
|
|
183
|
+
throw new Error(`can't find payment request for payment id ${p.id}`);
|
|
184
|
+
}
|
|
185
|
+
if (!opts.paymentValidationHandler) {
|
|
186
|
+
throw new Error(`please provide payment validation handler in options`);
|
|
187
|
+
}
|
|
188
|
+
await opts.paymentValidationHandler(p.paymentData.txId, paymentRequestData.data);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
async packMessage(message, senderDID) {
|
|
192
|
+
const responseEncoded = byteEncoder.encode(JSON.stringify(message));
|
|
193
|
+
const packerOpts = this._params.packerParams.mediaType === MediaType.SignedMessage
|
|
194
|
+
? this._params.packerParams.packerOptions
|
|
195
|
+
: {
|
|
196
|
+
provingMethodAlg: proving.provingMethodGroth16AuthV2Instance.methodAlg
|
|
197
|
+
};
|
|
198
|
+
return await this._packerMgr.pack(this._params.packerParams.mediaType, responseEncoded, {
|
|
199
|
+
senderDID,
|
|
200
|
+
...packerOpts
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -110,3 +110,30 @@ export var RefreshServiceType;
|
|
|
110
110
|
(function (RefreshServiceType) {
|
|
111
111
|
RefreshServiceType["Iden3RefreshService2023"] = "Iden3RefreshService2023";
|
|
112
112
|
})(RefreshServiceType || (RefreshServiceType = {}));
|
|
113
|
+
/**
|
|
114
|
+
* PaymentRequestType type for payment requests
|
|
115
|
+
*
|
|
116
|
+
* @enum {string}
|
|
117
|
+
*/
|
|
118
|
+
export var PaymentRequestType;
|
|
119
|
+
(function (PaymentRequestType) {
|
|
120
|
+
PaymentRequestType["PaymentRequest"] = "PaymentRequest";
|
|
121
|
+
})(PaymentRequestType || (PaymentRequestType = {}));
|
|
122
|
+
/**
|
|
123
|
+
* PaymentRequestDataType type for payment requests
|
|
124
|
+
*
|
|
125
|
+
* @enum {string}
|
|
126
|
+
*/
|
|
127
|
+
export var PaymentRequestDataType;
|
|
128
|
+
(function (PaymentRequestDataType) {
|
|
129
|
+
PaymentRequestDataType["Iden3PaymentRequestCryptoV1"] = "Iden3PaymentRequestCryptoV1";
|
|
130
|
+
})(PaymentRequestDataType || (PaymentRequestDataType = {}));
|
|
131
|
+
/**
|
|
132
|
+
* PaymentType type for payment responses
|
|
133
|
+
*
|
|
134
|
+
* @enum {string}
|
|
135
|
+
*/
|
|
136
|
+
export var PaymentType;
|
|
137
|
+
(function (PaymentType) {
|
|
138
|
+
PaymentType["Iden3PaymentCryptoV1"] = "Iden3PaymentCryptoV1";
|
|
139
|
+
})(PaymentType || (PaymentType = {}));
|
|
@@ -19,6 +19,8 @@ export declare const PROTOCOL_MESSAGE_TYPE: Readonly<{
|
|
|
19
19
|
CREDENTIAL_ONCHAIN_OFFER_MESSAGE_TYPE: string;
|
|
20
20
|
PROPOSAL_REQUEST_MESSAGE_TYPE: string;
|
|
21
21
|
PROPOSAL_MESSAGE_TYPE: string;
|
|
22
|
+
PAYMENT_REQUEST_MESSAGE_TYPE: string;
|
|
23
|
+
PAYMENT_MESSAGE_TYPE: string;
|
|
22
24
|
}>;
|
|
23
25
|
/**
|
|
24
26
|
* Media types for iden3 comm communication protocol
|
|
@@ -2,6 +2,7 @@ import { BasicMessage, IPackageManager } from '../types';
|
|
|
2
2
|
import { AuthMessageHandlerOptions } from './auth';
|
|
3
3
|
import { RevocationStatusMessageHandlerOptions } from './revocation-status';
|
|
4
4
|
import { ContractMessageHandlerOptions } from './contract-request';
|
|
5
|
+
import { PaymentHandlerOptions, PaymentRequestMessageHandlerOptions } from './payment';
|
|
5
6
|
/**
|
|
6
7
|
* iden3 Protocol message handler interface
|
|
7
8
|
*/
|
|
@@ -77,7 +78,7 @@ export declare class MessageHandler {
|
|
|
77
78
|
* @param context - An object containing information specific to the type of message being handled.
|
|
78
79
|
* @returns A Promise that resolves to a Uint8Array of the packed response, or null if no response was generated.
|
|
79
80
|
*/
|
|
80
|
-
handleMessage(bytes: Uint8Array, context: AuthMessageHandlerOptions | ContractMessageHandlerOptions | RevocationStatusMessageHandlerOptions |
|
|
81
|
+
handleMessage(bytes: Uint8Array, context: AuthMessageHandlerOptions | ContractMessageHandlerOptions | RevocationStatusMessageHandlerOptions | PaymentRequestMessageHandlerOptions | PaymentHandlerOptions | {
|
|
81
82
|
[key: string]: unknown;
|
|
82
83
|
}): Promise<Uint8Array | null>;
|
|
83
84
|
}
|