diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 457c9e96..8be45d1c 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -1332,10 +1332,10 @@ NAN_METHOD(FillTransaction) { } // Turn our CTransaction into a javascript Transaction - Local jstx = NanNew(); - ctx_to_jstx(ctx, 0, jstx); + Local new_jstx = NanNew(); + ctx_to_jstx(ctx, 0, new_jstx); - NanReturnValue(jstx); + NanReturnValue(new_jstx); } /** @@ -2676,51 +2676,55 @@ hextx_to_ctx(std::string tx_hex, CTransaction& ctx) { static inline void jsblock_to_cblock(const Local jsblock, CBlock& cblock) { - cblock->nVersion = (int)jsblock->Get(NanNew("version"))->IntegerValue(); + cblock.nVersion = (int)jsblock->Get(NanNew("version"))->IntegerValue(); String::AsciiValue mhash__(jsblock->Get(NanNew("merkleroot"))->ToString()); std::string mhash_ = *mhash__; if (mhash_[1] != 'x') mhash_ = "0x" + mhash_; uint256 mhash(mhash_); - cblock->hashMerkleRoot = mhash; - cblock->nTime = (unsigned int)jsblock->Get(NanNew("time"))->IntegerValue(); - cblock->nNonce = (unsigned int)jsblock->Get(NanNew("nonce"))->IntegerValue(); - cblock->nBits = (unsigned int)jsblock->Get(NanNew("bits"))->IntegerValue(); + cblock.hashMerkleRoot = mhash; + cblock.nTime = (unsigned int)jsblock->Get(NanNew("time"))->IntegerValue(); + cblock.nNonce = (unsigned int)jsblock->Get(NanNew("nonce"))->IntegerValue(); + cblock.nBits = (unsigned int)jsblock->Get(NanNew("bits"))->IntegerValue(); if (jsblock->Get(NanNew("previousblockhash"))->IsString()) { String::AsciiValue hash__(jsblock->Get(NanNew("previousblockhash"))->ToString()); std::string hash_ = *hash__; if (hash_[1] != 'x') hash_ = "0x" + hash_; uint256 hash(hash_); - cblock->hashPrevBlock = hash; + cblock.hashPrevBlock = hash; } else { - uint256 hash(std::string("0000000000000000000000000000000000000000000000000000000000000000")); - cblock->hashPrevBlock = hash; + // genesis block + uint256 hash(std::string("0x0000000000000000000000000000000000000000000000000000000000000000")); + cblock.hashPrevBlock = hash; } - Local txs = Local::Cast(jsblock->Get("tx")); - for (int ti = 0; ti < txs->Length(); ti++) { + Local txs = Local::Cast(jsblock->Get(NanNew("tx"))); + for (unsigned int ti = 0; ti < txs->Length(); ti++) { Local jstx = Local::Cast(txs->Get(ti)); CTransaction ctx; + // jstx_to_ctx(jstx, ctx); ctx.nVersion = jstx->Get(NanNew("version"))->IntegerValue(); ctx.nLockTime = jstx->Get(NanNew("locktime"))->IntegerValue(); - Local vin = Local::Cast(jstx->Get("vin")); - for (int vi = 0; vi < vin->Length(); vi++) { + Local vin = Local::Cast(jstx->Get(NanNew("vin"))); + for (unsigned int vi = 0; vi < vin->Length(); vi++) { CTxIn txin; Local in = Local::Cast(vin->Get(vi)); - std::string shash; + std::string shash_; if (in->Get(NanNew("coinbase"))->IsString()) { - String::AsciiValue shash_(jsblock->Get(NanNew("coinbase"))->ToString()); - shash = *shash_; + String::AsciiValue shash__(in->Get(NanNew("coinbase"))->ToString()); + shash_ = *shash__; } else { - String::AsciiValue shash_(jsblock->Get(NanNew("scriptSig"))->ToString()); - shash = *shash_; + String::AsciiValue shash__(in->Get(NanNew("scriptSig"))->ToString()); + shash_ = *shash__; } + if (shash_[1] != 'x') shash_ = "0x" + shash_; + uint256 shash(shash_); CScript scriptSig(shash); txin.scriptSig = scriptSig; @@ -2737,47 +2741,49 @@ jsblock_to_cblock(const Local jsblock, CBlock& cblock) { ctx.vin.push_back(txin); } - Local vout = Local::Cast(jstx->Get("vout")); - for (int vo = 0; vo < vout->Length(); vo++) { + Local vout = Local::Cast(jstx->Get(NanNew("vout"))); + for (unsigned int vo = 0; vo < vout->Length(); vo++) { CTxOut txout; Local out = Local::Cast(vout->Get(vo)); txout.nValue = (int64_t)out->Get(NanNew("value"))->IntegerValue(); - Local spk = Local::Cast(in->Get(NanNew("scriptPubKey"))); + Local spk = Local::Cast(out->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); + CScript scriptPubKey(phash); txout.scriptPubKey = scriptPubKey; ctx.vout.push_back(txout); } - cblock->vtx.push_back(ctx); + cblock.vtx.push_back(ctx); } } static inline void jstx_to_ctx(const Local jstx, CTransaction& ctx) { - ctx->nVersion = jstx->Get(NanNew("version"))->IntegerValue(); - ctx->nLockTime = jstx->Get(NanNew("locktime"))->IntegerValue(); + ctx.nVersion = jstx->Get(NanNew("version"))->IntegerValue(); + ctx.nLockTime = jstx->Get(NanNew("locktime"))->IntegerValue(); - Local vin = Local::Cast(jstx->Get("vin")); - for (int vi = 0; vi < vin->Length(); vi++) { + Local vin = Local::Cast(jstx->Get(NanNew("vin"))); + for (unsigned int vi = 0; vi < vin->Length(); vi++) { CTxIn txin; Local in = Local::Cast(vin->Get(vi)); - std::string shash; + std::string shash_; if (in->Get(NanNew("coinbase"))->IsString()) { - String::AsciiValue shash_(jsblock->Get(NanNew("coinbase"))->ToString()); - shash = *shash_; + String::AsciiValue shash__(in->Get(NanNew("coinbase"))->ToString()); + shash_ = *shash__; } else { - String::AsciiValue shash_(jsblock->Get(NanNew("scriptSig"))->ToString()); - shash = *shash_; + String::AsciiValue shash__(in->Get(NanNew("scriptSig"))->ToString()); + shash_ = *shash__; } + if (shash_[1] != 'x') shash_ = "0x" + shash_; + uint256 shash(shash_); CScript scriptSig(shash); txin.scriptSig = scriptSig; @@ -2792,26 +2798,26 @@ jstx_to_ctx(const Local jstx, CTransaction& ctx) { txin.prevout.n = (boost::int64_t)in->Get(NanNew("vout"))->IntegerValue(); txin.nSequence = (boost::int64_t)in->Get(NanNew("sequence"))->IntegerValue(); - ctx->vin.push_bask(txin); + ctx.vin.push_back(txin); } - Local vout = Local::Cast(jstx->Get("vout")); + Local vout = Local::Cast(jstx->Get(NanNew("vout"))); for (unsigned int vo = 0; vo < vout->Length(); vo++) { CTxOut txout; Local out = Local::Cast(vout->Get(vo)); txout.nValue = (int64_t)out->Get(NanNew("value"))->IntegerValue(); - Local spk = Local::Cast(in->Get(NanNew("scriptPubKey"))); + Local spk = Local::Cast(out->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); + CScript scriptPubKey(phash); txout.scriptPubKey = scriptPubKey; - ctx->vout.push_back(txout); + ctx.vout.push_back(txout); } }