1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const IlpPacket = require("ilp-packet");
|
4 | const log_1 = require("../common/log");
|
5 | const log = log_1.create('max-packet-amount-middleware');
|
6 | const bignumber_js_1 = require("bignumber.js");
|
7 | const { AmountTooLargeError } = IlpPacket.Errors;
|
8 | class MaxPacketAmountMiddleware {
|
9 | constructor(opts, { getInfo }) {
|
10 | this.getInfo = getInfo;
|
11 | }
|
12 | async applyToPipelines(pipelines, accountId) {
|
13 | const accountInfo = this.getInfo(accountId);
|
14 | if (!accountInfo) {
|
15 | throw new Error('account info unavailable. accountId=' + accountId);
|
16 | }
|
17 | if (accountInfo.maxPacketAmount) {
|
18 | const maxPacketAmount = accountInfo.maxPacketAmount;
|
19 | pipelines.incomingData.insertLast({
|
20 | name: 'maxPacketAmount',
|
21 | method: async (data, next) => {
|
22 | if (data[0] === IlpPacket.Type.TYPE_ILP_PREPARE) {
|
23 | const parsedPacket = IlpPacket.deserializeIlpPrepare(data);
|
24 | const amount = new bignumber_js_1.default(parsedPacket.amount);
|
25 | if (amount.gt(maxPacketAmount)) {
|
26 | log.debug('rejecting packet for exceeding max amount. accountId=%s maxAmount=%s actualAmount=%s', accountId, maxPacketAmount, parsedPacket.amount);
|
27 | throw new AmountTooLargeError(`packet size too large. maxAmount=${maxPacketAmount} actualAmount=${parsedPacket.amount}`, {
|
28 | receivedAmount: parsedPacket.amount,
|
29 | maximumAmount: maxPacketAmount
|
30 | });
|
31 | }
|
32 | }
|
33 | return next(data);
|
34 | }
|
35 | });
|
36 | }
|
37 | }
|
38 | }
|
39 | exports.default = MaxPacketAmountMiddleware;
|
40 |
|
\ | No newline at end of file |