From 54edc851e0d8e27c9447843959cd4afd1abc8f16 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 17 Jul 2015 15:55:36 -0400 Subject: [PATCH] Add getMempoolOutputs method. --- integration/index.js | 6 +++++ lib/bitcoind.js | 4 +++ src/bitcoindjs.cc | 58 +++++++++++++++++++++++++++++++++++++++++++- src/bitcoindjs.h | 2 ++ 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/integration/index.js b/integration/index.js index fd89630b..7e51af33 100644 --- a/integration/index.js +++ b/integration/index.js @@ -133,7 +133,13 @@ describe('Basic Functionality', function() { var work = bitcoind.getChainWork(hash); should.equal(work, undefined); }); + }); + describe('get outputs by address from the mempool', function() { + it('will do it', function() { + var outputs = bitcoind.getMempoolOutputs('n28S35tqEMbt6vNad7A5K3mZ7vdn8dZ86X'); + Array.isArray(outputs).should.equal(true); + }); }); }); diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 46218747..6d05f567 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -382,6 +382,10 @@ Bitcoin.prototype.getTransactionWithBlock = function(txid, blockhash, callback) }); }; +Bitcoin.prototype.getMempoolOutputs = function(address) { + return bitcoindjs.getMempoolOutputs(address); +}; + Bitcoin.prototype.getInfo = function() { if (bitcoin.stopping) return []; return bitcoindjs.getInfo(); diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index fcff27a0..7db6ec94 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -983,6 +983,62 @@ NAN_METHOD(GetInfo) { NanReturnValue(obj); } +/** + * GetMempoolOutputs + * bitcoindjs.getMempoolOutputs() + * Will return outputs by address from the mempool. + */ +NAN_METHOD(GetMempoolOutputs) { + Isolate* isolate = Isolate::GetCurrent(); + HandleScope scope(isolate); + + Local outputs = Array::New(isolate); + int arrayIndex = 0; + + std::map mapTx = mempool.mapTx; + + for(std::map::iterator it = mapTx.begin(); it != mapTx.end(); it++) { + uint256 txid = it->first; + CTxMemPoolEntry entry = it->second; + + const CTransaction tx = entry.GetTx(); + + int outputIndex = 0; + + BOOST_FOREACH(const CTxOut& txout, tx.vout) { + + CScript script = txout.scriptPubKey; + + txnouttype type; + vector > hash; + + if (Solver(script, type, hash)) { + if (type == TX_PUBKEYHASH || type == TX_SCRIPTHASH) { + + Local output = NanNew(); + + // todo: include the script + output->Set(NanNew("script"), NanNew("")); + + uint64_t satoshis = txout.nValue; + output->Set(NanNew("satoshis"), NanNew(satoshis)); // can't go above 2 ^ 53 -1 + output->Set(NanNew("txid"), NanNew(txid.GetHex())); + + output->Set(NanNew("outputIndex"), NanNew(outputIndex)); + + outputs->Set(arrayIndex, output); + arrayIndex++; + } + } + + outputIndex++; + } + } + + NanReturnValue(outputs); + +} + /** * Helpers */ @@ -1020,7 +1076,7 @@ init(Handle target) { NODE_SET_METHOD(target, "getInfo", GetInfo); NODE_SET_METHOD(target, "isSpent", IsSpent); NODE_SET_METHOD(target, "getChainWork", GetChainWork); - + NODE_SET_METHOD(target, "getMempoolOutputs", GetMempoolOutputs); } NODE_MODULE(bitcoindjs, init) diff --git a/src/bitcoindjs.h b/src/bitcoindjs.h index ea825a87..3e4c4fed 100644 --- a/src/bitcoindjs.h +++ b/src/bitcoindjs.h @@ -29,3 +29,5 @@ NAN_METHOD(GetTransaction); NAN_METHOD(GetInfo); NAN_METHOD(IsSpent); NAN_METHOD(GetChainWork); +NAN_METHOD(GetMempoolOutputs); +