You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
8.6KB

  1. var express = require('express');
  2. var router = express.Router();
  3. var util = require('util');
  4. var moment = require('moment');
  5. var utils = require('./../app/utils');
  6. var env = require("./../app/env");
  7. var bitcoin = require("bitcoin");
  8. var rpcApi = require("./../app/rpcApi");
  9. var fs = require('fs');
  10. var stream = fs.createWriteStream("proglog.txt");
  11. router.get("/", function(req, res) {
  12. if (!req.session.host) {
  13. if (req.cookies['rpc-host']) {
  14. res.locals.host = req.cookies['rpc-host'];
  15. }
  16. if (req.cookies['rpc-port']) {
  17. res.locals.port = req.cookies['rpc-port'];
  18. }
  19. if (req.cookies['rpc-username']) {
  20. res.locals.username = req.cookies['rpc-username'];
  21. }
  22. res.render("connect");
  23. res.end();
  24. return;
  25. }
  26. var client = global.client;
  27. rpcApi.getInfo().then(function(getinfo) {
  28. res.locals.getinfo = getinfo;
  29. var blockHeights = [];
  30. if (getinfo.blocks) {
  31. for (var i = 0; i < 10; i++) {
  32. blockHeights.push(getinfo.blocks - i);
  33. }
  34. }
  35. rpcApi.getBlocksByHeight(blockHeights).then(function(latestBlocks) {
  36. res.locals.latestBlocks = latestBlocks;
  37. res.render("index");
  38. });
  39. }).catch(function(err) {
  40. console.log(err);
  41. res.locals.userMessage = "Unable to connect to Vivocoin Node at " + req.session.host + ":" + req.session.port;
  42. res.render("index");
  43. });
  44. });
  45. router.get("/node-info", function(req, res) {
  46. var client = global.client;
  47. stream.write("yyyyyyyyyyyyyyyyyyyyyyy\n");
  48. rpcApi.getInfo().then(function(getinfo) {
  49. res.locals.getinfo = getinfo;
  50. rpcApi.getMiningInfo().then(function(getmininginfo) {
  51. res.locals.getmininginfo = getmininginfo;
  52. console.log(getmininginfo);
  53. stream.write('mininghash:'+ getmininginfo.networkhashps +"\n");
  54. res.render("node-info");
  55. });
  56. }).catch(function(err) {
  57. res.locals.userMessage = "Unable to connect to Vivocoin Node at " + req.session.host + ":" + req.session.port;
  58. console.log(err);
  59. res.render("node-info");
  60. });
  61. });
  62. router.get("/mempool", function(req, res) {
  63. var client = global.client;
  64. rpcApi.getMempoolInfo().then(function(getmempoolinfo) {
  65. res.locals.getmempoolinfo = getmempoolinfo;
  66. rpcApi.getMempoolStats().then(function(mempoolstats) {
  67. res.locals.mempoolstats = mempoolstats;
  68. res.render("mempool");
  69. });
  70. }).catch(function(err) {
  71. res.locals.userMessage = "Unable to connect to Vivocoin Node at " + req.session.host + ":" + req.session.port;
  72. console.log(err);
  73. res.render("mempool");
  74. });
  75. });
  76. router.post("/connect", function(req, res) {
  77. var host = req.body.host;
  78. var port = req.body.port;
  79. var username = req.body.username;
  80. var password = req.body.password;
  81. res.cookie('rpc-host', host);
  82. res.cookie('rpc-port', port);
  83. res.cookie('rpc-username', username);
  84. req.session.host = host;
  85. req.session.port = port;
  86. req.session.username = username;
  87. var client = new bitcoin.Client({
  88. host: host,
  89. port: port,
  90. user: username,
  91. pass: password,
  92. timeout: 30000
  93. });
  94. console.log("created client: " + client);
  95. global.client = client;
  96. req.session.userMessage = "<strong>Connected via RPC</strong>: " + username + " @ " + host + ":" + port;
  97. req.session.userMessageType = "success";
  98. res.redirect("/");
  99. });
  100. router.get("/blocks", function(req, res) {
  101. var limit = 20;
  102. var offset = 0;
  103. var sort = "desc";
  104. if (req.query.limit) {
  105. limit = parseInt(req.query.limit);
  106. }
  107. if (req.query.offset) {
  108. offset = parseInt(req.query.offset);
  109. }
  110. if (req.query.sort) {
  111. sort = req.query.sort;
  112. }
  113. res.locals.limit = limit;
  114. res.locals.offset = offset;
  115. res.locals.sort = sort;
  116. res.locals.paginationBaseUrl = "/blocks";
  117. rpcApi.getInfo().then(function(getinfo) {
  118. res.locals.blockCount = getinfo.blocks;
  119. res.locals.blockOffset = offset;
  120. var blockHeights = [];
  121. if (sort == "desc") {
  122. for (var i = (getinfo.blocks - offset); i > (getinfo.blocks - offset - limit); i--) {
  123. blockHeights.push(i);
  124. }
  125. } else {
  126. for (var i = offset; i < (offset + limit); i++) {
  127. blockHeights.push(i);
  128. }
  129. }
  130. rpcApi.getBlocksByHeight(blockHeights).then(function(blocks) {
  131. res.locals.blocks = blocks;
  132. res.render("blocks");
  133. });
  134. }).catch(function(err) {
  135. res.locals.userMessage = "Unable to connect to Vivocoin Node at " + req.session.host + ":" + req.session.port;
  136. console.log(err);
  137. res.render("blocks");
  138. });
  139. });
  140. router.post("/search", function(req, res) {
  141. if (!req.body.query) {
  142. req.session.userMessage = "Enter a block height, block hash, or transaction id.";
  143. res.redirect("/");
  144. return;
  145. }
  146. var query = req.body.query.toLowerCase();
  147. rpcApi.getRawTransaction(query).then(function(tx) {
  148. if (tx) {
  149. res.redirect("/tx/" + query);
  150. return;
  151. }
  152. }).catch(function(err) {
  153. rpcApi.getBlockByHash(query).then(function(blockByHash) {
  154. if (blockByHash) {
  155. res.redirect("/block/" + query);
  156. return;
  157. }
  158. }).catch(function(err) {
  159. if (isNaN(query)) {
  160. req.session.userMessage = "No results found for query: " + query;
  161. res.redirect("/");
  162. return;
  163. }
  164. rpcApi.getBlockByHeight(parseInt(query)).then(function(blockByHeight) {
  165. if (blockByHeight) {
  166. res.redirect("/block-height/" + query);
  167. return;
  168. }
  169. }).catch(function(err) {
  170. req.session.userMessage = "No results found for query: " + query;
  171. res.redirect("/");
  172. });
  173. });
  174. });
  175. });
  176. router.get("/block-height/:blockHeight", function(req, res) {
  177. var client = global.client;
  178. var blockHeight = parseInt(req.params.blockHeight);
  179. res.locals.blockHeight = blockHeight;
  180. res.locals.result = {};
  181. var limit = 20;
  182. var offset = 0;
  183. if (req.query.limit) {
  184. limit = parseInt(req.query.limit);
  185. }
  186. if (req.query.offset) {
  187. offset = parseInt(req.query.offset);
  188. }
  189. res.locals.limit = limit;
  190. res.locals.offset = offset;
  191. res.locals.paginationBaseUrl = "/block-height/" + blockHeight;
  192. client.cmd('getblockhash', blockHeight, function(err, result, resHeaders) {
  193. if (err) {
  194. // TODO handle RPC error
  195. return console.log(err);
  196. }
  197. res.locals.result.getblockhash = result;
  198. rpcApi.getBlockData(client, result, limit, offset).then(function(result) {
  199. res.locals.result.getblock = result.getblock;
  200. res.locals.result.transactions = result.transactions;
  201. res.locals.result.txInputsByTransaction = result.txInputsByTransaction;
  202. res.render("block-height");
  203. });
  204. });
  205. });
  206. router.get("/block/:blockHash", function(req, res) {
  207. var blockHash = req.params.blockHash;
  208. res.locals.blockHash = blockHash;
  209. res.locals.result = {};
  210. var limit = 20;
  211. var offset = 0;
  212. if (req.query.limit) {
  213. limit = parseInt(req.query.limit);
  214. }
  215. if (req.query.offset) {
  216. offset = parseInt(req.query.offset);
  217. }
  218. res.locals.limit = limit;
  219. res.locals.offset = offset;
  220. res.locals.paginationBaseUrl = "/block/" + blockHash;
  221. // TODO handle RPC error
  222. rpcApi.getBlockData(client, blockHash, limit, offset).then(function(result) {
  223. res.locals.result.getblock = result.getblock;
  224. res.locals.result.transactions = result.transactions;
  225. res.locals.result.txInputsByTransaction = result.txInputsByTransaction;
  226. res.render("block");
  227. });
  228. });
  229. router.get("/tx/:transactionId", function(req, res) {
  230. var txid = req.params.transactionId;
  231. var output = -1;
  232. if (req.query.output) {
  233. output = parseInt(req.query.output);
  234. }
  235. res.locals.txid = txid;
  236. res.locals.output = output;
  237. res.locals.result = {};
  238. // TODO handle RPC error
  239. rpcApi.getRawTransaction(txid).then(function(rawTxResult) {
  240. res.locals.result.getrawtransaction = rawTxResult;
  241. client.cmd('getblock', rawTxResult.blockhash, function(err3, result3, resHeaders3) {
  242. res.locals.result.getblock = result3;
  243. var txids = [];
  244. for (var i = 0; i < rawTxResult.vin.length; i++) {
  245. if (!rawTxResult.vin[i].coinbase) {
  246. txids.push(rawTxResult.vin[i].txid);
  247. }
  248. }
  249. rpcApi.getRawTransactions(txids).then(function(txInputs) {
  250. res.locals.result.txInputs = txInputs;
  251. res.render("transaction");
  252. });
  253. });
  254. });
  255. });
  256. router.get("/terminal", function(req, res) {
  257. if (!env.debug) {
  258. res.send("Debug mode is off.");
  259. return;
  260. }
  261. res.render("terminal");
  262. });
  263. router.post("/terminal", function(req, res) {
  264. if (!env.debug) {
  265. res.send("Debug mode is off.");
  266. return;
  267. }
  268. client.cmd(req.body.cmd, function(err, result, resHeaders) {
  269. console.log(result);
  270. console.log(err);
  271. console.log(resHeaders);
  272. res.send(JSON.stringify(result, null, 4));
  273. });
  274. });
  275. module.exports = router;