diff --git a/lib/services/address/index.js b/lib/services/address/index.js index e80f4238..271272a5 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -9,7 +9,6 @@ var bitcore = require('bitcore-lib'); var Unit = bitcore.Unit; var _ = bitcore.deps._; var Encoding = require('./encoding'); -var utils = require('../../utils'); var Transform = require('stream').Transform; var assert = require('assert'); @@ -56,9 +55,7 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba var txList = _.flatten(txLists); var results = { - totalItems: txList.length, - from: options.from, - to: options.to, + totalCount: txList.length, items: txList }; @@ -350,9 +347,33 @@ AddressService.prototype._getAddressHistory = function(address, options, callbac return callback(); } - results.push(tx); + assert(tx.__height >- 0, 'tx must have a height'); + self._header.getHeaderHash(tx.__height, function(err, hash) { - callback(); + if(err) { + log.error(err); + txStream.emit('error', err); + return callback(); + } + + tx.__blockhash = hash; + + var outputSatoshis = 0; + tx.outputs.forEach(function(output) { + outputSatoshis += output.value; + }); + + var inputSatoshis = 0; + tx.__inputValues.forEach(function(value) { + inputSatoshis += value; + }); + + tx.__outputSatoshis = outputSatoshis; + tx.__inputSatoshis = inputSatoshis; + results.push(tx); + callback(); + + }); }); diff --git a/lib/services/transaction/encoding.js b/lib/services/transaction/encoding.js index 962b9d39..39238272 100644 --- a/lib/services/transaction/encoding.js +++ b/lib/services/transaction/encoding.js @@ -14,6 +14,7 @@ Encoding.prototype.decodeTransactionKey = function(buffer) { return buffer.slice(2).toString('hex'); }; +// TODO: maybe we should be storing the block hash here too. Encoding.prototype.encodeTransactionValue = function(transaction) { var heightBuffer = new Buffer(4); heightBuffer.writeUInt32BE(transaction.__height); diff --git a/lib/services/transaction/index.js b/lib/services/transaction/index.js index 6789fc5f..739540ff 100644 --- a/lib/services/transaction/index.js +++ b/lib/services/transaction/index.js @@ -38,7 +38,33 @@ TransactionService.prototype.getAPIMethods = function() { }; TransactionService.prototype.getDetailedTransaction = function(txid, options, callback) { - this.getTransaction(txid, options, callback); + var self = this; + + this.getTransaction(txid, options, function(err, tx) { + + if (err) { + return callback(err); + } + + if (!tx) { + return callback(); + } + + self._header.getHeaderHash(tx.__height, function(err, hash) { + + if (err) { + return callback(err); + } + + assert(hash, 'Tx must have a hash entry in header db for given height.'); + + tx.__blockhash = hash; + + callback(null, tx); + + }); + + }); }; TransactionService.prototype.getTransaction = function(txid, options, callback) { diff --git a/test/services/address/index.unit.js b/test/services/address/index.unit.js index f7570a40..fcf5e467 100644 --- a/test/services/address/index.unit.js +++ b/test/services/address/index.unit.js @@ -64,9 +64,7 @@ describe('Address Service', function() { } expect(res).to.be.deep.equal({ - totalItems: 3, - from: 12, - to: 14, + totalCount: 3, items: [ {}, {}, {} ] }); @@ -76,22 +74,24 @@ describe('Address Service', function() { }); - describe('#_getAddresHistory', function() { + describe('#_getAddressHistory', function() { it('should get the address history', function(done) { var encoding = new Encoding(new Buffer('0001', 'hex')); addressService._encoding = encoding; + var getHeaderHash = sandbox.stub().callsArgWith(1, null, 'aa'); + addressService._header = { getHeaderHash: getHeaderHash }; var address = 'a'; var opts = { from: 12, to: 14 }; var txid = '1c6ea4a55a3edaac0a05e93b52908f607376a8fdc5387c492042f8baa6c05085'; var data = [ null, encoding.encodeAddressIndexKey(address, 123, txid, 1, 1) ]; - var getTransaction = sandbox.stub().callsArgWith(2, null, {}); + var getTransaction = sandbox.stub().callsArgWith(2, null, { __height: 123, outputs: [ { value: 1 } ], __inputValues: [ 1 ] }); addressService._tx = { getTransaction: getTransaction }; var txidStream = new Readable(); txidStream._read = function() { txidStream.push(data.pop()); - } + }; var createReadStream = sandbox.stub().returns(txidStream); addressService._db = { createKeyStream: createReadStream }; @@ -101,7 +101,22 @@ describe('Address Service', function() { return done(err); } expect(getTransaction.calledOnce).to.be.true; - expect(res).to.deep.equal([{}]); + expect(res).to.deep.equal([ + { + __blockhash: 'aa', + __height: 123, + __inputSatoshis: 1, + __inputValues: [ + 1 + ], + __outputSatoshis: 1, + outputs: [ + { + value: 1 + } + ] + } + ]); done(); }); }); @@ -125,7 +140,7 @@ describe('Address Service', function() { txidStream._read = function() { txidStream.push(data.pop()); - } + }; var createReadStream = sandbox.stub().returns(txidStream); addressService._db = { createKeyStream: createReadStream }; @@ -181,14 +196,14 @@ describe('Address Service', function() { return done(err); } expect(res[0]).to.deep.equal({ - address: "a", + address: 'a', amount: 0.0012, confirmations: 27, confirmationsFromCache: true, satoshis: 120000, - scriptPubKey: "76a91449f8c749a9960dc29b5cbe7d2397cea7d26611bb88ac", + scriptPubKey: '76a91449f8c749a9960dc29b5cbe7d2397cea7d26611bb88ac', ts: 1546300800, - txid: "25e28f9fb0ada5353b7d98d85af5524b2f8df5b0b0e2d188f05968bceca603eb", + txid: '25e28f9fb0ada5353b7d98d85af5524b2f8df5b0b0e2d188f05968bceca603eb', vout: 1 }); done();