From 1913237a3f14e83160588badcbe0f33f70d6d1f1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 1 Oct 2014 16:42:07 -0700 Subject: [PATCH] more promising conversion functions. --- src/bitcoindjs.cc | 204 ++++++++++++++-------------------------------- 1 file changed, 61 insertions(+), 143 deletions(-) diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 832b45ec..a1bc7a3d 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -2368,7 +2368,7 @@ NAN_METHOD(WalletImportKey) { pwalletMain->nTimeFirstKey = 1; // 0 would be considered 'no value' // Do this on the threadpool instead. - // if (fRescan) { + // if (data->fRescan) { // pwalletMain->ScanForWalletTransactions(chainActive.Genesis(), true); // } } @@ -2676,8 +2676,11 @@ hextx_to_ctx(std::string tx_hex, CTransaction& ctx) { } } -#if 1 +#if 0 /* + +NOTES: + class CBlockHeader { public: @@ -2727,79 +2730,61 @@ public: static inline void jsblock_to_cblock(const Local obj, CBlock& block, CBlockIndex* blockindex) { - // block.GetHash().GetHex().c_str() = obj->Get(NanNew("hash"))->ToString(); - // txGen.GetDepthInMainChain()= obj->Get(NanNew("confirmations"))->IntegerValue(); - // (int)::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) = - // obj->Get(NanNew("size"))->IntegerValue(); if (blockindex) { blockindex->nHeight = obj->Get(NanNew("height"))->IntegerValue(); } block->nVersion = (int)obj->Get(NanNew("version"))->IntegerValue(); - String::Utf8Value mhash__(obj->Get(NanNew("previousblockhash"))->ToString()); + String::AsciiValue mhash__(obj->Get(NanNew("previousblockhash"))->ToString()); std::string mhash_ = *mhash__; if (mhash_[1] != 'x') mhash_ = "0x" + mhash_; uint256 mhash(mhash_); block->hashMerkleRoot = mhash; - // Local merkletree = Local::Cast(obj->Get("merkletree")); - // block->vMerkleTree = NULL; - Local txs = Local::Cast(obj->Get("tx")); for (int ti = 0; ti < txs->Length(); ti++) { - Local entry = txs->Get(ti); + Local entry = Local::Cast(txs->Get(ti)); CTransaction tx; Local entry = NanNew(); - // CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); - // ssTx << tx; - // std::string strHex = HexStr(ssTx.begin(), ssTx.end()); - // strHex = entry->Get(NanNew("hex"))->ToString(); - // tx.GetHash().GetHex() = entry->Get(NanNew("txid"))->ToString(); tx.nVersion = entry->Get(NanNew("version"))->IntegerValue(); tx.nLockTime = entry->Get(NanNew("locktime"))->IntegerValue(); Local vin = Local::Cast(entry->Get("vin")); for (int vi = 0; vi < vin->Length(); vi++) { CTxIn txin; - Local in = vin->Get(vi); + Local in = Local::Cast(vin->Get(vi)); + std::string shash; if (in->Get(NanNew("coinbase"))->IsString()) { - String::Utf8Value shash_(obj->Get(NanNew("coinbase"))->ToString()); - std::string shash = *shash_; - CScript scriptSig(shash); - txin.scriptSig = scriptSig; - // HexStr(txin.scriptSig.begin(), txin.scriptSig.end()) = in->Get(NanNew("coinbase"))->ToString(); + String::AsciiValue shash_(obj->Get(NanNew("coinbase"))->ToString()); + shash = *shash_; } else { - String::Utf8Value shash_(obj->Get(NanNew("scriptSig"))->ToString()); - std::string shash = *shash_; - CScript scriptSig(shash); - txin.scriptSig = scriptSig; - - // txin.prevout.hash.GetHex() = in->Get(NanNew("txid"))->ToString(); - String::Utf8Value phash__(in->Get(NanNew("txid"))->ToString()); - std::string phash_ = *phash__; - if (phash_[1] != 'x') phash_ = "0x" + phash_; - uint256 phash(phash_); - txin.prevout.hash = phash; - txin.prevout.n = (boost::int64_t)in->Get(NanNew("vout"))->IntegerValue(); - // Local o = in->Get(NanNew("scriptSig")); - // txin.scriptSig.ToString() = o->Get(NanNew("asm"))->ToString(); - // HexStr(txin.scriptSig.begin(), txin.scriptSig.end()) = o->Get(NanNew("hex"))->ToString(); + String::AsciiValue shash_(obj->Get(NanNew("scriptSig"))->ToString()); + shash = *shash_; } - (boost::int64_t)txin.nSequence = in->Get(NanNew("sequence"))->IntegerValue(); + CScript scriptSig(shash); + txin.scriptSig = scriptSig; + String::AsciiValue phash__(in->Get(NanNew("txid"))->ToString()); + std::string phash_ = *phash__; + if (phash_[1] != 'x') phash_ = "0x" + phash_; + uint256 phash(phash_); + txin.prevout.hash = phash; + txin.prevout.n = (boost::int64_t)in->Get(NanNew("vout"))->IntegerValue(); + txin.nSequence = (boost::int64_t)in->Get(NanNew("sequence"))->IntegerValue(); + + tx.vin.push_back(txin); } Local vout = Local::Cast(entry->Get("vout")); for (unsigned int vo = 0; vo < vout->Length(); vo++) { const CTxOut txout; - Local out = vout->Get(vo); + Local out = Local::Cast(vout->Get(vo)); txout.nValue = (int64_t)out->Get(NanNew("value"))->IntegerValue(); - // vo = (boost::int64_t)out->Get(NanNew("n"))->IntegerValue(); - - String::Utf8Value phash__(in->Get(NanNew("scriptPubKey"))->Get(NanNew("hex"))); + Local spk = Local::Cast(in->Get(NanNew("scriptPubKey"))); + String::AsciiValue phash__(spk->Get(NanNew("hex"))); std::string phash_ = *phash__; if (phash_[1] != 'x') phash_ = "0x" + phash_; uint256 phash(phash_); @@ -2807,140 +2792,73 @@ jsblock_to_cblock(const Local obj, CBlock& block, CBlockIndex* blockinde txout.scriptPubKey = scriptPubKey; -/* - Local o = out->Get(NanNew("scriptPubKey")); - { - CScript scriptPubKey; - Local out = o; - bool fIncludeHex = true; - - txnouttype type; - vector addresses; - int nRequired; - scriptPubKey.ToString() = out->Get(NanNew("asm"))->ToString(); - if (fIncludeHex) { - HexStr(scriptPubKey.begin(), scriptPubKey.end()) = out->Set(NanNew("hex"))->ToString(); - } - if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { - GetTxnOutputType(type) = out->Set(NanNew("type"))->ToString(); - } else { - nRequired = out->Set(NanNew("reqSigs"))->IntegerValue(); - GetTxnOutputType(type) = out->Set(NanNew("type"))->ToString(); - Local a = Local::Cast(out->Get("addresses")); - for (int ai = 0; ai < a->Length(); ai++) { - CTxDestination addr; - CBitcoinAddress(addr).ToString() = a->Get(ai)->ToString(); - } - } - } -*/ - } - - if (entry->Get(NanNew("blockhash"))->IsString()) { - const uint256 hashBlock; - hashBlock.GetHex() = entry->Get(NanNew("blockhash"))->ToString(); - if (entry->Get(NanNew("time"))->IsNumber()) { - 1 + chainActive.Height() - pindex->nHeight = entry->Get(NanNew("confirmations"))->IntegerValue(); - (boost::int64_t)pindex->nTime = entry->Get(NanNew("time"))->IntegerValue(); - (boost::int64_t)pindex->nTime = entry->Get(NanNew("blocktime"))->IntegerValue(); - } else { - 0 = entry->Get(NanNew("confirmations"))->IntegerValue(); - } + tx.vout.push_back(txout); } + block->vtx.push_back(tx); } block->nTime = (unsigned int)obj->Get(NanNew("time"))->IntegerValue(); block->nNonce = (unsigned int)obj->Get(NanNew("nonce"))->IntegerValue(); block->nBits = (unsigned int)obj->Get(NanNew("bits"))->IntegerValue(); - // GetDifficulty(blockindex) = obj->Get(NanNew("difficulty"))->IntegerValue(); - // blockindex->nChainWork.GetHex() = obj->Get(NanNew("chainwork"))->ToString(); if (obj->Get(NanNew("previousblockhash"))->IsString()) { - String::Utf8Value hash__(obj->Get(NanNew("previousblockhash"))->ToString()); + String::AsciiValue hash__(obj->Get(NanNew("previousblockhash"))->ToString()); std::string hash_ = *hash__; if (hash_[1] != 'x') hash_ = "0x" + hash_; uint256 hash(hash_); - // if (blockindex) { - // blockindex->pprev->GetBlockHash().GetHex() = obj->Get(NanNew("previousblockhash"))->ToString(); - // } block->hashPrevBlock = hash; } else { uint256 hash(std::string("0000000000000000000000000000000000000000000000000000000000000000")); block->hashPrevBlock = hash; } - // if (obj->Get(NanNew("nextblockhash"))->IsString()) { - // CBlockIndex pnext; - // CBlockIndex *pnext = chainActive.Next(blockindex); - // pnext->GetBlockHash().GetHex() = obj->Get(NanNew("nextblockhash"))->ToString(); - // } } static inline void jstx_to_ctx(const Local entry, CTransaction& tx, uint256 hashBlock) { - CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); - ssTx << tx; - std::string strHex = HexStr(ssTx.begin(), ssTx.end()); - strHex = entry->Get(NanNew("hex"), NanNew(strHex))->ToString(); - tx.GetHash().GetHex() = entry->Get(NanNew("txid"))->ToString(); tx.nVersion = entry->Get(NanNew("version"))->IntegerValue(); tx.nLockTime = entry->Get(NanNew("locktime"))->IntegerValue(); Local vin = Local::Cast(entry->Get("vin")); for (int vi = 0; vi < vin->Length(); vi++) { - const CTxIn txin; - Local in = vin->Get(vi); - if (in->Get(NanNew("coinbase")->IsString()) { - HexStr(txin.scriptSig.begin(), txin.scriptSig.end()) = in->Get(NanNew("coinbase"))->ToString(); + CTxIn txin; + Local in = Local::Cast(vin->Get(vi)); + std::string shash; + if (in->Get(NanNew("coinbase"))->IsString()) { + String::AsciiValue shash_(obj->Get(NanNew("coinbase"))->ToString()); + shash = *shash_; } else { - txin.prevout.hash.GetHex() = in->Get(NanNew("txid"))->ToString(); - (boost::int64_t)txin.prevout.n = in->Get(NanNew("vout"))->IntegerValue(); - Local o = in->Get(NanNew("scriptSig")); - txin.scriptSig.ToString() = o->Get(NanNew("asm"))->ToString(); - HexStr(txin.scriptSig.begin(), txin.scriptSig.end()) = o->Get(NanNew("hex"))->ToString(); + String::AsciiValue shash_(obj->Get(NanNew("scriptSig"))->ToString()); + shash = *shash_; } - (boost::int64_t)txin.nSequence = in->Get(NanNew("sequence"))->IntegerValue(); + CScript scriptSig(shash); + txin.scriptSig = scriptSig; + String::AsciiValue phash__(in->Get(NanNew("txid"))->ToString()); + std::string phash_ = *phash__; + if (phash_[1] != 'x') phash_ = "0x" + phash_; + uint256 phash(phash_); + txin.prevout.hash = phash; + txin.prevout.n = (boost::int64_t)in->Get(NanNew("vout"))->IntegerValue(); + txin.nSequence = (boost::int64_t)in->Get(NanNew("sequence"))->IntegerValue(); + + tx.vin.push_bask(txin); } Local vout = Local::Cast(entry->Get("vout")); for (unsigned int vo = 0; vo < vout->Length(); vo++) { - CTxOut txout; - Local out = vout->Get(vo); - txout.nValue = out->Get(NanNew("value"))->IntegerValue(); - (boost::int64_t)vo = out->Get(NanNew("n"))->IntegerValue(); + const CTxOut txout; + Local out = Local::Cast(vout->Get(vo)); - Local o = out->Get(NanNew("scriptPubKey")); - { - CScript scriptPubKey; - Local out = o; + txout.nValue = (int64_t)out->Get(NanNew("value"))->IntegerValue(); - int nRequired; - scriptPubKey.ToString() = out->Get(NanNew("asm"))->ToString(); - if (out->Get(NanNew("hex"))->IsString()) { - HexStr(scriptPubKey.begin(), scriptPubKey.end()) = out->Get(NanNew("hex"))->ToString(); - } - if (out->Get(NanNew("type"))->IsString()) { - GetTxnOutputType(type) = out->Get(NanNew("type"))->ToString(); - } else { - nRequired = out->Get(NanNew("reqSigs"))->IntegerValue(); - GetTxnOutputType(type) = out->Get(NanNew("type"))->ToString(); - Local a = Local::Cast(out->Get("addresses")); - for (int ai = 0; ai < a->Length(); ai++) { - CTxDestination addr; - CBitcoinAddress(addr).ToString() = a->get(ai)->ToString(); - } - } - } - } + Local spk = Local::Cast(in->Get(NanNew("scriptPubKey"))); + String::AsciiValue phash__(spk->Get(NanNew("hex"))); + std::string phash_ = *phash__; + if (phash_[1] != 'x') phash_ = "0x" + phash_; + uint256 phash(phash_); + CScriptPubKey scriptPubKey(phash); - if (entry->Get(NanNew("blockhash"))->IsString()) { - hashBlock.GetHex() = entry->Get(NanNew("blockhash"))->ToString(); - CBlockIndex pindex; - if (entry->Get("time")->IsNumber()) { - 1 + chainActive.Height() - pindex->nHeight = entry->Get(NanNew("confirmations"))->IntegerValue(); - (boost::int64_t)pindex->nTime = entry->Get(NanNew("time"))->IntegerValue(); - (boost::int64_t)pindex->nTime = entry->Get(NanNew("blocktime"))->IntegerValue(); - } else { - 0 = entry->Get(NanNew("confirmations"))->IntegerValue(); - } + txout.scriptPubKey = scriptPubKey; + + tx.vout.push_back(txout); } } #endif