1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const crypto_1 = require("crypto");
|
4 | const log_1 = require("../common/log");
|
5 | const log = log_1.create('validate-fulfillment-middleware');
|
6 | const IlpPacket = require("ilp-packet");
|
7 | const { WrongConditionError } = IlpPacket.Errors;
|
8 | class ValidateFulfillmentMiddleware {
|
9 | async applyToPipelines(pipelines, accountId) {
|
10 | pipelines.outgoingData.insertLast({
|
11 | name: 'validateFulfillment',
|
12 | method: async (data, next) => {
|
13 | if (data[0] === IlpPacket.Type.TYPE_ILP_PREPARE) {
|
14 | const { executionCondition } = IlpPacket.deserializeIlpPrepare(data);
|
15 | const result = await next(data);
|
16 | if (result[0] === IlpPacket.Type.TYPE_ILP_FULFILL) {
|
17 | const { fulfillment } = IlpPacket.deserializeIlpFulfill(result);
|
18 | const calculatedCondition = crypto_1.createHash('sha256').update(fulfillment).digest();
|
19 | if (!calculatedCondition.equals(executionCondition)) {
|
20 | log.error('received incorrect fulfillment from account. accountId=%s fulfillment=%s calculatedCondition=%s executionCondition=%s', accountId, fulfillment.toString('base64'), calculatedCondition.toString('base64'), executionCondition.toString('base64'));
|
21 | throw new WrongConditionError('fulfillment did not match expected value.');
|
22 | }
|
23 | }
|
24 | return result;
|
25 | }
|
26 | return next(data);
|
27 | }
|
28 | });
|
29 | }
|
30 | }
|
31 | exports.default = ValidateFulfillmentMiddleware;
|
32 |
|
\ | No newline at end of file |