From 4f6eb68c504654c6ccc6943b1e04aadfee1237a5 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Mon, 25 Sep 2017 21:21:54 -0400 Subject: [PATCH] Fixed transaction transformation. --- lib/index.js | 17 +++++++---------- lib/transactions.js | 32 ++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/index.js b/lib/index.js index 25ec926..c942c8f 100644 --- a/lib/index.js +++ b/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++) { diff --git a/lib/transactions.js b/lib/transactions.js index 0f5ff2a..1dd0e73 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -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,