Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

baseActionsRouter.js 12KB

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