Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var imports = require('soop').imports();
  4.  
  5. var bitcore = require('bitcore'),
  6. RpcClient = bitcore.RpcClient,
  7. BitcoreBlock = bitcore.Block,
  8. util = require('util'),
  9. config = require('../config/config');
  10.  
  11. var bitcoreRpc = imports.bitcoreRpc || new RpcClient(config.bitcoind);
  12.  
  13. function Rpc() {
  14. }
  15.  
  16. Rpc._parseTxResult = function(info) {
  17. var b = new Buffer(info.hex,'hex');
  18.  
  19. // remove fields we dont need, to speed and adapt the information
  20. delete info.hex;
  21.  
  22. // Inputs => add index + coinBase flag
  23. var n =0;
  24. info.vin.forEach(function(i) {
  25. i.n = n++;
  26. if (i.coinbase) info.isCoinBase = true;
  27. });
  28.  
  29. // Outputs => add total
  30. var valueOutSat = 0;
  31. info.vout.forEach( function(o) {
  32. o.value = o.value.toFixed(8);
  33. valueOutSat += o.value * bitcore.util.COIN;
  34. });
  35. info.valueOut = valueOutSat.toFixed(0) / bitcore.util.COIN;
  36. info.size = b.length;
  37.  
  38. return info;
  39. };
  40.  
  41.  
  42. Rpc.errMsg = function(err) {
  43. var e = err;
  44. e.message += util.format(' [Host: %s:%d User:%s Using password:%s]',
  45. bitcoreRpc.host,
  46. bitcoreRpc.port,
  47. bitcoreRpc.user,
  48. bitcoreRpc.pass?'yes':'no'
  49. );
  50. return e;
  51. };
  52.  
  53. Rpc.getTxInfo = function(txid, doNotParse, cb) {
  54. var self = this;
  55.  
  56. if (typeof doNotParse === 'function') {
  57. cb = doNotParse;
  58. doNotParse = false;
  59. }
  60.  
  61. bitcoreRpc.getRawTransaction(txid, 1, function(err, txInfo) {
  62. // Not found?
  63. if (err && err.code === -5) return cb();
  64. if (err) return cb(self.errMsg(err));
  65.  
  66. var info = doNotParse ? txInfo.result : self._parseTxResult(txInfo.result);
  67. return cb(null,info);
  68. });
  69. };
  70.  
  71.  
  72. Rpc.blockIndex = function(height, cb) {
  73. var self = this;
  74.  
  75. bitcoreRpc.getBlockHash(height, function(err, bh){
  76. if (err) return cb(self.errMsg(err));
  77. cb(null, { blockHash: bh.result });
  78. });
  79. };
  80.  
  81. Rpc.getBlock = function(hash, cb) {
  82. var self = this;
  83.  
  84. bitcoreRpc.getBlock(hash, function(err,info) {
  85. // Not found?
  86. if (err && err.code === -5) return cb();
  87. if (err) return cb(self.errMsg(err));
  88.  
  89.  
  90. if (info.result.height)
  91. info.result.reward = (BitcoreBlock.getBlockValue(info.result.height) / bitcore.util.COIN);
  92. info.result.reward = Math.floor(info.result.reward);
  93.  
  94. return cb(err,info.result);
  95. });
  96. };
  97.  
  98. Rpc.sendRawTransaction = function(rawtx, cb) {
  99. bitcoreRpc.sendRawTransaction(rawtx, function(err, txid) {
  100. if (err) return cb(err);
  101.  
  102. return cb(err, txid.result);
  103. });
  104. };
  105.  
  106. Rpc.verifyMessage = function(address, signature, message, cb) {
  107. var self = this;
  108. bitcoreRpc.verifyMessage(address, signature, message, function(err, message) {
  109. if (err && (err.code === -3 || err.code === -5))
  110. return cb(err); // -3 = invalid address, -5 = malformed base64 / etc.
  111. if (err)
  112. return cb(self.errMsg(err));
  113.  
  114. return cb(err, message.result);
  115. });
  116. };
  117.  
  118. module.exports = require('soop')(Rpc);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement