25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

baseActionsRouter.js 12KB

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