From ecadec5878a9d29b3f82474e0219becc102c92f3 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 26 Sep 2014 11:39:25 -0700 Subject: [PATCH] refactor verification. --- lib/bitcoind.js | 10 +++++----- src/bitcoindjs.cc | 24 ++++++++++++++---------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index c61a32aa..c2f242ef 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -321,11 +321,12 @@ function Block(data) { self[key] = data[key]; } }); + + this.toHex(); } Block.prototype.verify = function() { - return this._verified = this._verified - || bitcoindjs.verifyBlock(this.toHex()); + return this.verified = this.verified || bitcoindjs.verifyBlock(this); }; Block.prototype.toBinary = function() { @@ -394,7 +395,7 @@ Block.toBinary = function(block, type) { }; Block.prototype.toHex = function() { - return this._hex = this._hex || Block.toHex(this); + return this.hex = this.hex || Block.toHex(this); }; Block.toHex = function(block) { @@ -443,8 +444,7 @@ function Transaction(data) { } Transaction.prototype.verify = function() { - return this._verified = this._verified - || bitcoindjs.verifyTransaction(this.toHex()); + return this.verified = this.verified || bitcoindjs.verifyTransaction(this); }; Transaction.prototype.getSerializeSize = function() { diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index c08b9a62..24669856 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -1093,16 +1093,18 @@ async_broadcast_tx_after(uv_work_t *req) { NAN_METHOD(VerifyBlock) { NanScope(); - if (args.Length() < 1 || !args[0]->IsString()) { + if (args.Length() < 1 || !args[0]->IsObject()) { return NanThrowError( - "Usage: bitcoindjs.verifyBlock(blockHex)"); + "Usage: bitcoindjs.verifyBlock(block)"); } - String::Utf8Value blockHex_(args[0]->ToString()); - std::string blockHex = std::string(*blockHex_); + Local js_block = Local::Cast(args[0]); + + String::Utf8Value block_hex_(js_block->Get(NanNew("hex"))->ToString()); + std::string block_hex = std::string(*block_hex_); CBlock block; - CDataStream ssData(ParseHex(blockHex), SER_NETWORK, PROTOCOL_VERSION); + CDataStream ssData(ParseHex(block_hex), SER_NETWORK, PROTOCOL_VERSION); ssData >> block; CValidationState state; @@ -1118,16 +1120,18 @@ NAN_METHOD(VerifyBlock) { NAN_METHOD(VerifyTransaction) { NanScope(); - if (args.Length() < 1 || !args[0]->IsString()) { + if (args.Length() < 1 || !args[0]->IsObject()) { return NanThrowError( - "Usage: bitcoindjs.verifyTransaction(txHex)"); + "Usage: bitcoindjs.verifyTransaction(tx)"); } - String::Utf8Value txHex_(args[0]->ToString()); - std::string txHex = std::string(*txHex_); + Local js_tx = Local::Cast(args[0]); + + String::Utf8Value tx_hex_(js_tx->Get(NanNew("hex"))->ToString()); + std::string tx_hex = std::string(*tx_hex_); CTransaction tx; - CDataStream ssData(ParseHex(txHex), SER_NETWORK, PROTOCOL_VERSION); + CDataStream ssData(ParseHex(tx_hex), SER_NETWORK, PROTOCOL_VERSION); ssData >> tx; CValidationState state;