Fixed transaction transformation.

This commit is contained in:
Chris Kleeschulte 2017-09-25 21:21:54 -04:00
parent e31ec2f276
commit 4f6eb68c50
No known key found for this signature in database
GPG Key ID: 33195D27EF6BDB7F
2 changed files with 27 additions and 22 deletions

View File

@ -17,7 +17,6 @@ var morgan = require('morgan');
var bitcore = require('bitcore-lib'); var bitcore = require('bitcore-lib');
var _ = bitcore.deps._; var _ = bitcore.deps._;
var $ = bitcore.util.preconditions; var $ = bitcore.util.preconditions;
var Transaction = bitcore.Transaction;
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
/** /**
@ -101,12 +100,12 @@ InsightAPI.prototype.start = function(callback) {
this._bus = this.node.openBus({remoteAddress: 'localhost-insight-api'}); this._bus = this.node.openBus({remoteAddress: 'localhost-insight-api'});
} }
this._bus.on('block/block', this.transactionEventHandler.bind(this)); this._bus.on('p2p/transaction', this.transactionEventHandler.bind(this));
this._bus.subscribe('block/block');
this._bus.on('p2p/transaction', this.blockEventHandler.bind(this));
this._bus.subscribe('p2p/transaction'); this._bus.subscribe('p2p/transaction');
//this._bus.on('p2p/block', this.blockEventHandler.bind(this));
//this._bus.subscribe('p2p/block');
callback(); callback();
}; };
@ -273,14 +272,12 @@ InsightAPI.prototype.getPublishEvents = function() {
]; ];
}; };
InsightAPI.prototype.blockEventHandler = function(hashBuffer) { InsightAPI.prototype.blockEventHandler = function(block) {
// Notify inv subscribers
for (var i = 0; i < this.subscriptions.inv.length; i++) { for (var i = 0; i < this.subscriptions.inv.length; i++) {
this.subscriptions.inv[i].emit('block', hashBuffer.toString('hex')); this.subscriptions.inv[i].emit('block', block.rhash());
} }
}; };
InsightAPI.prototype.transactionEventHandler = function(txBuffer) { InsightAPI.prototype.transactionEventHandler = function(tx) {
var tx = new Transaction().fromBuffer(txBuffer);
var result = this.txController.transformInvTransaction(tx); var result = this.txController.transformInvTransaction(tx);
for (var i = 0; i < this.subscriptions.inv.length; i++) { for (var i = 0; i < this.subscriptions.inv.length; i++) {

View File

@ -17,6 +17,9 @@ function TxController(node) {
if (this.node.network === 'livenet') { if (this.node.network === 'livenet') {
this._network = 'main'; this._network = 'main';
} }
if (this._network === 'regtest') {
this._network = 'testnet';
}
} }
TxController.prototype.show = function(req, res) { TxController.prototype.show = function(req, res) {
@ -156,12 +159,12 @@ TxController.prototype.transformOutput = function(options, output, index) {
transformed.scriptPubKey.asm = output.script.toASM(); transformed.scriptPubKey.asm = output.script.toASM();
} }
if (!options.noSpent) { //if (!options.noSpent) {
// These aren't implemented in the new api // These aren't implemented in the new api
//transformed.spentTxId = output.spentTxId || null; // we aren't tracking this with the bcoin implementation //transformed.spentTxId = output.spentTxId || null; // we aren't tracking this with the bcoin implementation
//transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex; //transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex;
//transformed.spentHeight = output.spentHeight || null; //transformed.spentHeight = output.spentHeight || null;
} //}
var address = output.getAddress(); var address = output.getAddress();
if (address) { if (address) {
@ -173,29 +176,34 @@ TxController.prototype.transformOutput = function(options, output, index) {
}; };
TxController.prototype.transformInvTransaction = function(transaction) { TxController.prototype.transformInvTransaction = function(transaction) {
var self = this;
var valueOut = 0; var valueOut = 0;
var vout = []; var vout = [];
for (var i = 0; i < transaction.outputs.length; i++) { for (var i = 0; i < transaction.outputs.length; i++) {
var output = transaction.outputs[i]; var output = transaction.outputs[i];
valueOut += output.satoshis; valueOut += output.value;
if (output.script) { if (output.script) {
var address = output.script.toAddress(self.node.network); var address = output.getAddress();
if (address) {
var obj = {}; if (!address) {
obj[address.toString()] = output.satoshis; continue;
vout.push(obj);
} }
address.network = this._network;
address = address.toString();
var obj = {};
obj[address] = output.value;
vout.push(obj);
} }
} }
var isRBF = _.any(_.pluck(transaction.inputs, 'sequenceNumber'), function(seq) { var isRBF = _.any(_.pluck(transaction.inputs, 'sequence'), function(seq) {
return seq < MAXINT - 1; return seq < MAXINT - 1;
}); });
var transformed = { var transformed = {
txid: transaction.hash, txid: transaction.txid(),
valueOut: valueOut / 1e8, valueOut: valueOut / 1e8,
vout: vout, vout: vout,
isRBF: isRBF, isRBF: isRBF,