1 | import * as IlpPacket from 'ilp-packet'
|
2 | import Config from '../services/config'
|
3 | import Accounts from '../services/accounts'
|
4 | import RouteBroadcaster from '../services/route-broadcaster'
|
5 | import RouteBuilder from '../services/route-builder'
|
6 | import IlpPrepareController from '../controllers/ilp-prepare'
|
7 | import { create as createLogger } from '../common/log'
|
8 | const log = createLogger('core-middleware')
|
9 | import reduct = require('reduct')
|
10 | const { InvalidPacketError } = IlpPacket.Errors
|
11 |
|
12 | export default class Core {
|
13 | protected config: Config
|
14 | protected accounts: Accounts
|
15 | protected routeBroadcaster: RouteBroadcaster
|
16 | protected routeBuilder: RouteBuilder
|
17 | protected ilpPrepareController: IlpPrepareController
|
18 |
|
19 | constructor (deps: reduct.Injector) {
|
20 | this.config = deps(Config)
|
21 | this.accounts = deps(Accounts)
|
22 | this.routeBroadcaster = deps(RouteBroadcaster)
|
23 | this.routeBuilder = deps(RouteBuilder)
|
24 |
|
25 | this.ilpPrepareController = deps(IlpPrepareController)
|
26 | }
|
27 |
|
28 | async processData (data: Buffer, accountId: string, outbound: (data: Buffer, accountId: string) => Promise<Buffer>): Promise<Buffer> {
|
29 | if (!this.accounts.getInfo(accountId)) {
|
30 | log.warn('got data from unknown account id. accountId=%s', accountId)
|
31 | throw new Error('got data from unknown account id. accountId=' + accountId)
|
32 | }
|
33 |
|
34 | if (!Buffer.isBuffer(data)) {
|
35 | log.error('data handler was passed a non-buffer. typeof=%s data=%s', typeof data, data)
|
36 | throw new Error('data handler was passed a non-buffer. typeof=' + typeof data)
|
37 | }
|
38 |
|
39 | switch (data[0]) {
|
40 | case IlpPacket.Type.TYPE_ILP_PREPARE:
|
41 | return this.ilpPrepareController.sendData(data, accountId, outbound)
|
42 | default:
|
43 | log.error('received invalid packet type. source=%s type=%s', accountId, data[0])
|
44 | throw new InvalidPacketError('invalid packet type received. type=' + data[0])
|
45 | }
|
46 | }
|
47 | }
|