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