From 1d7ea5763a7c85be9e501b9dbac39ef564f27d7f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 22 Sep 2014 12:38:33 -0700 Subject: [PATCH] parse gettx. --- src/bitcoindjs.cc | 99 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 9 deletions(-) diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index cb0b907c..a44c595b 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -754,7 +754,7 @@ async_get_block_after(uv_work_t *req) { entry->Set(NanNew("locktime"), NanNew(tx.nLockTime)); Local vin = NanNew(); - int e = 0; + int vi = 0; BOOST_FOREACH(const CTxIn& txin, tx.vin) { Local in = NanNew(); if (tx.IsCoinBase()) { @@ -768,17 +768,18 @@ async_get_block_after(uv_work_t *req) { in->Set(NanNew("scriptSig"), o); } in->Set(NanNew("sequence"), NanNew((boost::int64_t)txin.nSequence)); - vin->Set(e, in); + vin->Set(vi, in); + vi++; } entry->Set(NanNew("vin"), vin); Local vout = NanNew(); - for (unsigned int j = 0; j < tx.vout.size(); j++) { - const CTxOut& txout = tx.vout[j]; + for (unsigned int vo = 0; vo < tx.vout.size(); vo++) { + const CTxOut& txout = tx.vout[vo]; Local out = NanNew(); //out->Set(NanNew("value"), NanNew(ValueFromAmount(txout.nValue))); out->Set(NanNew("value"), NanNew(txout.nValue)); - out->Set(NanNew("n"), NanNew((boost::int64_t)j)); + out->Set(NanNew("n"), NanNew((boost::int64_t)vo)); // ScriptPubKeyToJSON(txout.scriptPubKey, o, true); Local o = NanNew(); @@ -810,7 +811,7 @@ async_get_block_after(uv_work_t *req) { } out->Set(NanNew("scriptPubKey"), o); - vout->Set(j, out); + vout->Set(vo, out); } entry->Set(NanNew("vout"), vout); @@ -935,13 +936,93 @@ NAN_METHOD(GetTx) { ssTx << tx; string strHex = HexStr(ssTx.begin(), ssTx.end()); - Local obj = NanNew(); - obj->Set(NanNew("hex"), NanNew(strHex.c_str())); + Local entry = NanNew(); + entry->Set(NanNew("txid"), NanNew(tx.GetHash().GetHex())); + entry->Set(NanNew("version"), NanNew(tx.nVersion)); + entry->Set(NanNew("locktime"), NanNew(tx.nLockTime)); + + Local vin = NanNew(); + int vi = 0; + BOOST_FOREACH(const CTxIn& txin, tx.vin) { + Local in = NanNew(); + if (tx.IsCoinBase()) { + in->Set(NanNew("coinbase"), NanNew(HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); + } else { + in->Set(NanNew("txid"), NanNew(txin.prevout.hash.GetHex())); + in->Set(NanNew("vout"), NanNew((boost::int64_t)txin.prevout.n)); + Local o = NanNew(); + o->Set(NanNew("asm"), NanNew(txin.scriptSig.ToString())); + o->Set(NanNew("hex"), NanNew(HexStr(txin.scriptSig.begin(), txin.scriptSig.end()))); + in->Set(NanNew("scriptSig"), o); + } + in->Set(NanNew("sequence"), NanNew((boost::int64_t)txin.nSequence)); + vin->Set(vi, in); + vi++; + } + entry->Set(NanNew("vin"), vin); + + Local vout = NanNew(); + for (unsigned int vo = 0; vo < tx.vout.size(); vo++) { + const CTxOut& txout = tx.vout[vo]; + Local out = NanNew(); + //out->Set(NanNew("value"), NanNew(ValueFromAmount(txout.nValue))); + out->Set(NanNew("value"), NanNew(txout.nValue)); + out->Set(NanNew("n"), NanNew((boost::int64_t)vo)); + + // ScriptPubKeyToJSON(txout.scriptPubKey, o, true); + Local o = NanNew(); + { + const CScript& scriptPubKey = txout.scriptPubKey; + Local out = o; + bool fIncludeHex = true; + // --- + txnouttype type; + vector addresses; + int nRequired; + out->Set(NanNew("asm"), NanNew(scriptPubKey.ToString())); + if (fIncludeHex) { + out->Set(NanNew("hex"), NanNew(HexStr(scriptPubKey.begin(), scriptPubKey.end()))); + } + if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { + out->Set(NanNew("type"), NanNew(GetTxnOutputType(type))); + } else { + out->Set(NanNew("reqSigs"), NanNew(nRequired)); + out->Set(NanNew("type"), NanNew(GetTxnOutputType(type))); + Local a = NanNew(); + int k = 0; + BOOST_FOREACH(const CTxDestination& addr, addresses) { + a->Set(k, NanNew(CBitcoinAddress(addr).ToString())); + k++; + } + out->Set(NanNew("addresses"), a); + } + } + out->Set(NanNew("scriptPubKey"), o); + + vout->Set(vo, out); + } + entry->Set(NanNew("vout"), vout); + + if (hashBlock != 0) { + entry->Set(NanNew("blockhash"), NanNew(hashBlock.GetHex())); + map::iterator mi = mapBlockIndex.find(hashBlock); + if (mi != mapBlockIndex.end() && (*mi).second) { + CBlockIndex* pindex = (*mi).second; + if (chainActive.Contains(pindex)) { + entry->Set(NanNew("confirmations"), + NanNew(1 + chainActive.Height() - pindex->nHeight)); + entry->Set(NanNew("time"), NanNew((boost::int64_t)pindex->nTime)); + entry->Set(NanNew("blocktime"), NanNew((boost::int64_t)pindex->nTime)); + } else { + entry->Set(NanNew("confirmations"), NanNew(0)); + } + } + } const unsigned argc = 2; Local argv[argc] = { Local::New(Null()), - Local::New(obj) + Local::New(entry) }; TryCatch try_catch; cb->Call(Context::GetCurrent()->Global(), argc, argv);