UNPKG

ntrnetwork

Version:

Transledger peer to peer wire communication

185 lines (168 loc) 8.19 kB
/* ----------------------------------------------------------------------------- app.js Description: This is the front end server handler for the Ledger. It is based on Express.js It is a REST service supporting the following methods: POST /tx : Deposit or withdrawal into a ledger account POST /create : Create a new ledger account --------------------------------------------------------------------------------*/ // PACKAGES // ============================================================================ // call the packages we need const assert = require('assert'); // assert variable validation const express = require('express'); // call express 4 const cors = require('cors'); // to support cross domain calls const app = express(); // define our app using express const environment = require('./environment'); // configutation varaiables const bodyParser = require('body-parser'); // to parse form body variables const Broadcaster = require('../src/index').Broadcaster;// Get the Broadcaster class const broadcaster = new Broadcaster(0x00); // create a channel with universal listener const MESSAGE_CODES = require('../src').MESSAGE_CODES; // TX, AUDIT, TRANSACT const router = express.Router(); // express endpoint router var port = process.env.PORT || 9099; // set our port // ROUTES FOR OUR API // ============================================================================= // all of our routes will be prefixed with / app.use(bodyParser.json()); // this will let us get the data from a POST app.use(cors()); app.use('/', router); app.use(logErrors); app.use(clientErrorHandler); app.use(errorHandler); // take transfer requests as input app.route("/connect") .get( (req, res) => { res.status(200).send({status: true}); }); app.route("/api/v1/request") .post( (req, res) => { try { console.log(`${Date().toString().substring(0, 24)} app: received data with Tx: ${req.body}`) const setPgm = new Set(["itBTC","itLTC","itBCH", "iBTC", "iLTC", "iBCH"]); const setCoins = new Set(["tBTC", "tLTC", "tBCH", "BTC", "LTC", "BCH"]); assert(req.body.appID) assert(req.body.ticker); assert(req.body.transactionID); assert(req.body.sourceNetwork); assert(req.body.sourceAddress); if (setPgm.has(req.body.ticker) ) { assert(req.body.from); } assert(req.body.destinationNetwork); assert(req.body.destinationAddress); if (setCoins.has(req.body.ticker) ) { assert(req.body.tokenContractAddress); } } catch(error) { res.status(400).json({ error: `${Date().toString().substring(0, 24)} Bad JSON request - read the documentation` }); // Bad Request return; } console.log(`${Date().toString().substring(0, 24)} app: broadcasted a transaction request with ID: ${req.body.transactionID}`) broadcaster.publish(MESSAGE_CODES.TX, req.body); res.status(200).send(); } ); app.route('/audit') .post( (req, res) => { try { console.log(`${Date().toString().substring(0, 24)} app: received data with Tx: ${req.body}`); assert(req.body.status); assert(req.body.TR.timestamp); assert(req.body.TR.sourceKey); assert(req.body.TR.destKey); const setPgm = new Set(["itBTC","itLTC","itBCH", "iBTC", "iLTC", "iBCH"]); const setCoins = new Set(["tBTC", "tLTC", "tBCH", "BTC", "LTC", "BCH"]); assert(req.body.TR.transferRequest.appID) assert(req.body.TR.transferRequest.ticker); assert(req.body.TR.transferRequest.transactionID); assert(req.body.TR.transferRequest.sourceNetwork); assert(req.body.TR.transferRequest.sourceAddress); if (setPgm.has(req.body.TR.transferRequest.ticker) ) { assert(req.body.TR.transferRequest.from); } assert(req.body.TR.transferRequest.destinationNetwork); assert(req.body.TR.transferRequest.destinationAddress); if (setCoins.has(req.body.TR.transferRequest.ticker) ) { assert(req.body.TR.transferRequest.tokenContractAddress); } assert(req.body.TR.transferRequest.brdcTender); assert(req.body.TR.transferRequest.onlyReqConf); } catch(error) { res.status(400).json({ error: `${Date().toString().substring(0, 24)} Bad JSON request - ${error}` }); // Bad Request return; } broadcaster.publish(MESSAGE_CODES.AUDIT, req.body); res.status(200).send(); } ); // send the collection of registered peers app.route('/peers/:channel') .get( (req, res) => { const queueLength = broadcaster.peersQueue.length; const queueLength2 = broadcaster.peersQueue.filter((o) => o.ts <= Date.now()).length; let channel = ""; switch (broadcaster.channelID) { case 00: channel = "Universal channel"; break; // 0x00 case 21: channel = "Alpha"; break; // 0x015 case 22: channel = "testnet"; break; // 0x016 case 20: channel = "production"; break; // 0x014 } let reply = { ServerChannel: `${broadcaster.channelID}:${channel}`, version: environment.version, status: `${Date().toString().substring(0, 24)} Total nodes in DPT: ${broadcaster.dptSize}, open slots: ${broadcaster.OpenSlots}, queue: ${queueLength} / ${queueLength2}`, slots: { open: broadcaster.OpenSlots, size: broadcaster.peerSize, list: broadcaster.slots }, 'active peers': { maxPeers: broadcaster.maxPeers, list: broadcaster.peers }, '_peers': { Size: broadcaster.peerSize, list: broadcaster._peers }, peersQueue: broadcaster.peersQueue, dpt: { size: broadcaster.dptSize, list: broadcaster.dptList }, } res.status(200).send(reply); } ); app.route('/peersQueue') .get( (req, res) => { console.log(`${Date().toString().substring(0, 24)} app: received request for the peerQueue list`); const list = broadcaster.getPeerQueue(); res.status(200).send(list); } ); // send collection kept in transient memory // transactions are flushed at each 30 seconds. app.route('/tx') .get( (req, res) => { console.log(`${Date().toString().substring(0, 24)} app: received request for the transaction list`) const tx = broadcaster.getTransactions(); res.status(200).json(tx); } ); // ERROR HANDLING SECTION //============================================================================= function logErrors (error, req, res, next) { console.error(`${Date().toString().substring(0, 24)} [express] ERROR: request routing -> ${error.message}`); next(error) } function clientErrorHandler (error, req, res, next) { if (req.xhr) { res.status(500).send(`${Date().toString().substring(0, 24)} [express] ERROR: request routing -> ${error.message}`); } else { next(error) } } function errorHandler (error, req, res, next) { res.status(500) res.send(`${Date().toString().substring(0, 24)} [express] ERROR: request routing -> ${error.message}`); } // START THE SERVER // ============================================================================= app.listen(port); console.log(`${Date().toString().substring(0, 24)} [app] Interblockchain node server ${environment.version} started on port: ${port}`);