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.

476 lines
12KB

  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. var query = req.body.query;
  148. if (query === null || query === "null" || query.length < 1) {
  149. res.redirect("/");
  150. return;
  151. }
  152. query = query.replace(/,/g, "");
  153. console.log('query is' + query);
  154. if (query.length < 9) {
  155. if (typeof query == "number") {
  156. res.redirect("/block-height/" + query);
  157. console.log('This is a block');
  158. return;
  159. }
  160. } else {
  161. console.log('Bigger than 9 char');
  162. }
  163. if (query.length < 40) {
  164. if (query.startsWith('V')) {
  165. res.redirect("/address/" + query);
  166. console.log('This is not number');
  167. return;
  168. } else {
  169. console.log('Does not start with V');
  170. }
  171. }
  172. console.log(query.length + ':Not less that 40 chars - going to check transaction and then block hash');
  173. rpcApi.getRawTransaction(query).then(function(tx) {
  174. if (tx) {
  175. res.redirect("/tx/" + query);
  176. return;
  177. }
  178. }).catch(function(err) {
  179. rpcApi.getBlockByHash(query).then(function(blockByHash) {
  180. if (blockByHash) {
  181. res.redirect("/block/" + query);
  182. return;
  183. }
  184. }).catch(function(err) {
  185. if (isNaN(query)) {
  186. req.session.userMessage = "No results found for query: " + query;
  187. res.redirect("/");
  188. return;
  189. }
  190. rpcApi.getBlockByHeight(parseInt(query)).then(function(blockByHeight) {
  191. if (blockByHeight) {
  192. res.redirect("/block-height/" + query);
  193. return;
  194. }
  195. }).catch(function(err) {
  196. req.session.userMessage = "No results found for query: " + query;
  197. res.redirect("/");
  198. });
  199. });
  200. });
  201. });
  202. router.get("/block-height/:blockHeight", function(req, res) {
  203. var client = global.client;
  204. var blockHeight = parseInt(req.params.blockHeight);
  205. res.locals.blockHeight = blockHeight;
  206. res.locals.result = {};
  207. var limit = 20;
  208. var offset = 0;
  209. if (req.query.limit) {
  210. limit = parseInt(req.query.limit);
  211. }
  212. if (req.query.offset) {
  213. offset = parseInt(req.query.offset);
  214. }
  215. res.locals.limit = limit;
  216. res.locals.offset = offset;
  217. res.locals.paginationBaseUrl = "/block-height/" + blockHeight;
  218. client.cmd('getblockhash', blockHeight, function(err, result, resHeaders) {
  219. if (err) {
  220. // TODO handle RPC error
  221. return console.log(err);
  222. }
  223. res.locals.result.getblockhash = result;
  224. rpcApi.getBlockData(client, result, limit, offset).then(function(result) {
  225. res.locals.result.getblock = result.getblock;
  226. res.locals.result.transactions = result.transactions;
  227. res.locals.result.txInputsByTransaction = result.txInputsByTransaction;
  228. res.render("block-height");
  229. });
  230. });
  231. });
  232. router.get("/block/:blockHash", function(req, res) {
  233. var blockHash = req.params.blockHash;
  234. res.locals.blockHash = blockHash;
  235. res.locals.result = {};
  236. var limit = 20;
  237. var offset = 0;
  238. if (req.query.limit) {
  239. limit = parseInt(req.query.limit);
  240. }
  241. if (req.query.offset) {
  242. offset = parseInt(req.query.offset);
  243. }
  244. res.locals.limit = limit;
  245. res.locals.offset = offset;
  246. res.locals.paginationBaseUrl = "/block/" + blockHash;
  247. // TODO handle RPC error
  248. rpcApi.getBlockData(client, blockHash, limit, offset).then(function(result) {
  249. res.locals.result.getblock = result.getblock;
  250. res.locals.result.transactions = result.transactions;
  251. res.locals.result.txInputsByTransaction = result.txInputsByTransaction;
  252. res.render("block");
  253. });
  254. });
  255. router.get("/address/:address", function(req, res) {
  256. var address = req.params.address;
  257. res.locals.address = address;
  258. console.log("address is:" + address);
  259. console.log("/usr/local/bin/vivo-cli -conf=/etc/masternodes/vivo_n1.conf getaddressbalance \'{\"addresses\": [\"" + address + "\"]}\'");
  260. res.locals.result = {};
  261. const { exec } = require('child_process');
  262. var balance = "Not Found";
  263. var received = "Not Found";
  264. var yourscript = exec('/usr/local/bin/vivo-cli -conf=/etc/masternodes/vivo_n1.conf getaddressbalance \'{\"addresses\": [\"' + address + '"]}\'',
  265. //var yourscript = exec('whoami',
  266. (error, stdout, stderr) => {
  267. console.log(stdout);
  268. const json = stdout;
  269. const obj = JSON.parse(json);
  270. console.log(obj.balance);
  271. console.log(obj.received);
  272. res.locals.balance = parseInt(obj.balance) / 100000000;
  273. res.locals.received = parseInt(obj.received) / 100000000;
  274. console.log(stderr);
  275. if (error !== null) {
  276. console.log(`exec error: ${error}`);
  277. }
  278. res.render("address");
  279. });
  280. /*
  281. const { spawn } = require("child_process");
  282. //const ls = spawn("/usr/local/bin/vivo-cli", [" -conf=/etc/masternodes/vivo_n1.conf \'{\"addresses\": [\"" + address + "\"]}\'"]);
  283. const ls = spawn("ls", "-lah");
  284. ls.stdout.on("data", data => {
  285. console.log(`stdout: ${data}`);
  286. });
  287. ls.stderr.on("data", data => {
  288. console.log(`stderr: ${data}`);
  289. });
  290. ls.on('error', (error) => {
  291. console.log(`error: ${error.message}`);
  292. });
  293. ls.on("close", code => {
  294. console.log(`child process exited with code ${code}`);
  295. });
  296. */
  297. });
  298. router.get("/tx/:transactionId", function(req, res) {
  299. var txid = req.params.transactionId;
  300. var output = -1;
  301. if (req.query.output) {
  302. output = parseInt(req.query.output);
  303. }
  304. res.locals.txid = txid;
  305. res.locals.output = output;
  306. res.locals.result = {};
  307. // TODO handle RPC error
  308. rpcApi.getRawTransaction(txid).then(function(rawTxResult) {
  309. res.locals.result.getrawtransaction = rawTxResult;
  310. client.cmd('getblock', rawTxResult.blockhash, function(err3, result3, resHeaders3) {
  311. res.locals.result.getblock = result3;
  312. var txids = [];
  313. for (var i = 0; i < rawTxResult.vin.length; i++) {
  314. if (!rawTxResult.vin[i].coinbase) {
  315. txids.push(rawTxResult.vin[i].txid);
  316. }
  317. }
  318. rpcApi.getRawTransactions(txids).then(function(txInputs) {
  319. res.locals.result.txInputs = txInputs;
  320. res.render("transaction");
  321. });
  322. });
  323. });
  324. });
  325. router.get("/terminal", function(req, res) {
  326. if (!env.debug) {
  327. res.send("Debug mode is off.");
  328. return;
  329. }
  330. res.render("terminal");
  331. });
  332. router.post("/terminal", function(req, res) {
  333. if (!env.debug) {
  334. res.send("Debug mode is off.");
  335. return;
  336. }
  337. client.cmd(req.body.cmd, function(err, result, resHeaders) {
  338. console.log(result);
  339. console.log(err);
  340. console.log(resHeaders);
  341. res.send(JSON.stringify(result, null, 4));
  342. });
  343. });
  344. module.exports = router;