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.deps._;
var $ = bitcore.util.preconditions;
var Transaction = bitcore.Transaction;
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.on('block/block', this.transactionEventHandler.bind(this));
this._bus.subscribe('block/block');
this._bus.on('p2p/transaction', this.blockEventHandler.bind(this));
this._bus.on('p2p/transaction', this.transactionEventHandler.bind(this));
this._bus.subscribe('p2p/transaction');
//this._bus.on('p2p/block', this.blockEventHandler.bind(this));
//this._bus.subscribe('p2p/block');
callback();
};
@ -273,14 +272,12 @@ InsightAPI.prototype.getPublishEvents = function() {
];
};
InsightAPI.prototype.blockEventHandler = function(hashBuffer) {
// Notify inv subscribers
InsightAPI.prototype.blockEventHandler = function(block) {
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) {
var tx = new Transaction().fromBuffer(txBuffer);
InsightAPI.prototype.transactionEventHandler = function(tx) {
var result = this.txController.transformInvTransaction(tx);
for (var i = 0; i < this.subscriptions.inv.length; i++) {

View File

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