UNPKG

1.74 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const crypto_1 = require("crypto");
4const log_1 = require("../common/log");
5const log = log_1.create('validate-fulfillment-middleware');
6const IlpPacket = require("ilp-packet");
7const { WrongConditionError } = IlpPacket.Errors;
8class 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}
31exports.default = ValidateFulfillmentMiddleware;
32//# sourceMappingURL=validate-fulfillment.js.map
\No newline at end of file