2014-02-13 11:14:22 -08:00
|
|
|
'use strict';
|
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var imports = require('soop').imports();
|
2014-02-13 11:14:22 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var RpcClient = require('bitcore/RpcClient'),
|
|
|
|
BitcoreBlock = require('bitcore/Block'),
|
|
|
|
bitcoreUtil = require('bitcore/util/util'),
|
|
|
|
util = require('util'),
|
|
|
|
config = require('../config/config');
|
2014-02-13 11:14:22 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
var bitcoreRpc = imports.bitcoreRpc || new RpcClient(config.bitcoind);
|
2014-02-13 11:14:22 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
function Rpc() {
|
|
|
|
}
|
2014-02-13 11:14:22 -08:00
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
Rpc._parseTxResult = function(info) {
|
|
|
|
var b = new Buffer(info.hex,'hex');
|
|
|
|
|
|
|
|
// remove fields we dont need, to speed and adapt the information
|
|
|
|
delete info.hex;
|
|
|
|
|
|
|
|
// Inputs => add index + coinBase flag
|
|
|
|
var n =0;
|
|
|
|
info.vin.forEach(function(i) {
|
|
|
|
i.n = n++;
|
|
|
|
if (i.coinbase) info.isCoinBase = true;
|
|
|
|
if (i.scriptSig) delete i.scriptSig.hex;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Outputs => add total
|
|
|
|
var valueOutSat = 0;
|
|
|
|
info.vout.forEach( function(o) {
|
|
|
|
valueOutSat += o.value * bitcoreUtil.COIN;
|
|
|
|
delete o.scriptPubKey.hex;
|
|
|
|
});
|
|
|
|
info.valueOut = parseInt(valueOutSat) / bitcoreUtil.COIN;
|
|
|
|
info.size = b.length;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Rpc.errMsg = function(err) {
|
|
|
|
var e = err;
|
|
|
|
e.message += util.format(' [Host: %s:%d User:%s Using password:%s]',
|
|
|
|
bitcoreRpc.host,
|
|
|
|
bitcoreRpc.port,
|
|
|
|
bitcoreRpc.user,
|
|
|
|
bitcoreRpc.pass?'yes':'no'
|
|
|
|
);
|
|
|
|
return e;
|
|
|
|
};
|
|
|
|
|
|
|
|
Rpc.getTxInfo = function(txid, doNotParse, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (typeof doNotParse === 'function') {
|
|
|
|
cb = doNotParse;
|
|
|
|
doNotParse = false;
|
2014-02-13 11:14:22 -08:00
|
|
|
}
|
|
|
|
|
2014-03-05 18:03:56 -08:00
|
|
|
bitcoreRpc.getRawTransaction(txid, 1, function(err, txInfo) {
|
|
|
|
// Not found?
|
|
|
|
if (err && err.code === -5) return cb();
|
|
|
|
if (err) return cb(self.errMsg(err));
|
|
|
|
|
|
|
|
var info = doNotParse ? txInfo.result : self._parseTxResult(txInfo.result);
|
|
|
|
return cb(null,info);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Rpc.blockIndex = function(height, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
bitcoreRpc.getBlockHash(height, function(err, bh){
|
|
|
|
if (err) return cb(self.errMsg(err));
|
|
|
|
cb(null, { blockHash: bh.result });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Rpc.getBlock = function(hash, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
bitcoreRpc.getBlock(hash, function(err,info) {
|
|
|
|
// Not found?
|
|
|
|
if (err && err.code === -5) return cb();
|
|
|
|
if (err) return cb(self.errMsg(err));
|
|
|
|
|
|
|
|
|
|
|
|
if (info.result.height)
|
|
|
|
info.result.reward = BitcoreBlock.getBlockValue(info.result.height) / bitcoreUtil.COIN ;
|
|
|
|
|
|
|
|
return cb(err,info.result);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Rpc.sendRawTransaction = function(rawtx, cb) {
|
|
|
|
var self = this;
|
|
|
|
bitcoreRpc.sendRawTransaction(rawtx, function(err, txid) {
|
|
|
|
if (err && err.code === -5) return cb(err); // transaction already in block chain
|
|
|
|
if (err) return cb(self.errMsg(err));
|
|
|
|
|
|
|
|
return cb(err, txid.result);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = require('soop')(Rpc);
|
2014-02-13 11:14:22 -08:00
|
|
|
|
|
|
|
|