From d5801c9172a3f779cfc04ddcff2bb24aa3a37680 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Fri, 24 Jul 2015 09:21:32 -0600 Subject: [PATCH] getTransactionsForAddress() without mempool --- lib/modules/address.js | 56 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/lib/modules/address.js b/lib/modules/address.js index 89e62874..69da0c19 100644 --- a/lib/modules/address.js +++ b/lib/modules/address.js @@ -31,7 +31,8 @@ AddressModule.prototype.getAPIMethods = function() { ['getBalance', this, this.getBalance, 2], ['getOutputs', this, this.getOutputs, 2], ['getUnspentOutputs', this, this.getUnspentOutputs, 2], - ['isSpent', this, this.isSpent, 2] + ['isSpent', this, this.isSpent, 2], + ['getTransactionsForAddress', this, this.getTransactionsForAddress, 2] ]; }; @@ -281,4 +282,55 @@ AddressModule.prototype.isSpent = function(output, queryMempool, callback) { }); }; -module.exports = AddressModule; +AddressModule.prototype.getTransactionsForAddress = function(address, queryMempool, callback) { + var self = this; + + var txids = []; + var key = [AddressModule.PREFIXES.OUTPUTS, address].join('-'); + + var stream = this.db.store.createReadStream({ + start: key, + end: key + '~' + }); + + stream.on('data', function(data) { + var key = data.key.split('-'); + txids.push(key[3]); + }); + + var error; + + stream.on('error', function(streamError) { + if (streamError) { + error = streamError; + } + }); + + stream.on('close', function() { + if (error) { + return callback(error); + } + + async.map( + txids, + function(txid, next) { + self.db.getTransaction(txid, next); + }, + function(err, transactions) { + if(err) { + return callback(err); + } + + if(queryMempool) { + // TODO + } + + callback(null, transactions); + }); + }); + + return stream; + +}; + +module.exports = AddressModule; \ No newline at end of file