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.

382 lignes
9.9KB

  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("/address/:address", function(req, res) {
  230. var address = req.params.address;
  231. res.locals.address = address;
  232. res.locals.result = {};
  233. // TODO handle RPC error
  234. rpcApi.getBalanceFromAddress(client, address).then(function(result) {
  235. res.locals.result.address = result.address;
  236. res.render("address");
  237. });
  238. });
  239. router.get("/tx/:transactionId", function(req, res) {
  240. var txid = req.params.transactionId;
  241. var output = -1;
  242. if (req.query.output) {
  243. output = parseInt(req.query.output);
  244. }
  245. res.locals.txid = txid;
  246. res.locals.output = output;
  247. res.locals.result = {};
  248. // TODO handle RPC error
  249. rpcApi.getRawTransaction(txid).then(function(rawTxResult) {
  250. res.locals.result.getrawtransaction = rawTxResult;
  251. client.cmd('getblock', rawTxResult.blockhash, function(err3, result3, resHeaders3) {
  252. res.locals.result.getblock = result3;
  253. var txids = [];
  254. for (var i = 0; i < rawTxResult.vin.length; i++) {
  255. if (!rawTxResult.vin[i].coinbase) {
  256. txids.push(rawTxResult.vin[i].txid);
  257. }
  258. }
  259. rpcApi.getRawTransactions(txids).then(function(txInputs) {
  260. res.locals.result.txInputs = txInputs;
  261. res.render("transaction");
  262. });
  263. });
  264. });
  265. });
  266. router.get("/terminal", function(req, res) {
  267. if (!env.debug) {
  268. res.send("Debug mode is off.");
  269. return;
  270. }
  271. res.render("terminal");
  272. });
  273. router.post("/terminal", function(req, res) {
  274. if (!env.debug) {
  275. res.send("Debug mode is off.");
  276. return;
  277. }
  278. client.cmd(req.body.cmd, function(err, result, resHeaders) {
  279. console.log(result);
  280. console.log(err);
  281. console.log(resHeaders);
  282. res.send(JSON.stringify(result, null, 4));
  283. });
  284. });
  285. module.exports = router;