diff --git a/example/index.js b/example/index.js index fcbb8d2e..acf286f8 100755 --- a/example/index.js +++ b/example/index.js @@ -9,6 +9,10 @@ bitcoind.start(function(err) { console.log('bitcoind: error="%s"', err.message); }); bitcoind.on('open', function(status) { + setTimeout(function() { + var block = bitcoind.getBlock(0); + console.log(block); + }, 1000); console.log('bitcoind: status="%s"', status); }); }); diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 1cbe5f45..5533b395 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -119,6 +119,10 @@ Bitcoin.prototype.start = function(callback) { } }; +Bitcoin.prototype.getBlock = function(hash) { + return JSON.parse(bitcoindjs.getBlock(hash)); +}; + Bitcoin.prototype.log = Bitcoin.prototype.info = function() { if (typeof arguments[0] !== 'string') { diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index a37edde3..e8a21ce6 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -77,6 +77,12 @@ #include "threadsafety.h" #include "version.h" +#include "json/json_spirit_value.h" + +// using namespace json_spirit; + +extern json_spirit::Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex); + /** * Bitcoin Globals * Relevant: @@ -345,6 +351,19 @@ NAN_METHOD(IsStopped) { NAN_METHOD(GetBlock) { NanScope(); + std::string strHash = "0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"; + uint256 hash(strHash); + CBlock block; + CBlockIndex* pblockindex = mapBlockIndex[hash]; + ReadBlockFromDisk(block, pblockindex); + json_spirit::Object result = blockToJSON(block, pblockindex); + json_spirit::Object rpc_result = JSONRPCReplyObj(result, json_spirit::Value::null, 0); + std::string out = json_spirit::write_string(json_spirit::Value(rpc_result), false) + "\n"; + // https://www.google.com/search?q=json.parse%20in%20v8%20c%2B%2B + // https://v8.googlecode.com/svn/trunk/src/json-parser.h + // NanReturnValue(NanNew(v8::ParseJsonValue(NanNew(out)))); + NanReturnValue(NanNew(out)); +#if 0 int nHeight = 0; CBlockIndex *pindex = chainActive[nHeight]; CBlock block; @@ -356,9 +375,11 @@ NAN_METHOD(GetBlock) { // return true; // } // } - // NanReturnValue(NanNew("")); + blockToJSON(block); + NanReturnValue(NanNew(block.ToString())); } NanReturnValue(NanNew(false)); +#endif } /**