Fixed transaction transformation.
This commit is contained in:
parent
e31ec2f276
commit
4f6eb68c50
17
lib/index.js
17
lib/index.js
@ -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++) {
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user