From e2a8b956517737ddc48be4faaef6ae371ac14d29 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 26 Sep 2014 11:34:55 -0700 Subject: [PATCH] VerifyTransaction --- lib/bitcoind.js | 8 +++++++- src/bitcoindjs.cc | 31 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index d1a596a2..c61a32aa 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -324,7 +324,8 @@ function Block(data) { } Block.prototype.verify = function() { - return this._verified = this._verified || bitcoindjs.verifyBlock(this.toHex()); + return this._verified = this._verified + || bitcoindjs.verifyBlock(this.toHex()); }; Block.prototype.toBinary = function() { @@ -441,6 +442,11 @@ function Transaction(data) { this.toHex(); } +Transaction.prototype.verify = function() { + return this._verified = this._verified + || bitcoindjs.verifyTransaction(this.toHex()); +}; + Transaction.prototype.getSerializeSize = function() { ; }; diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index bd08c5a2..c08b9a62 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -127,6 +127,7 @@ NAN_METHOD(PollBlocks); NAN_METHOD(PollMempool); NAN_METHOD(BroadcastTx); NAN_METHOD(VerifyBlock); +NAN_METHOD(VerifyTransaction); static void async_start_node_work(uv_work_t *req); @@ -1102,7 +1103,6 @@ NAN_METHOD(VerifyBlock) { CBlock block; CDataStream ssData(ParseHex(blockHex), SER_NETWORK, PROTOCOL_VERSION); - CTransaction tx; ssData >> block; CValidationState state; @@ -1111,6 +1111,34 @@ NAN_METHOD(VerifyBlock) { NanReturnValue(NanNew(valid)); } +/** + * VerifyTransaction + */ + +NAN_METHOD(VerifyTransaction) { + NanScope(); + + if (args.Length() < 1 || !args[0]->IsString()) { + return NanThrowError( + "Usage: bitcoindjs.verifyTransaction(txHex)"); + } + + String::Utf8Value txHex_(args[0]->ToString()); + std::string txHex = std::string(*txHex_); + + CTransaction tx; + CDataStream ssData(ParseHex(txHex), SER_NETWORK, PROTOCOL_VERSION); + ssData >> tx; + + CValidationState state; + bool valid = CheckTransaction(tx, state); + + string reason; + bool standard = IsStandardTx(tx, reason); + + NanReturnValue(NanNew(valid && standard)); +} + /** * Conversions */ @@ -1343,6 +1371,7 @@ init(Handle target) { NODE_SET_METHOD(target, "pollMempool", PollMempool); NODE_SET_METHOD(target, "broadcastTx", BroadcastTx); NODE_SET_METHOD(target, "verifyBlock", VerifyBlock); + NODE_SET_METHOD(target, "verifyTransaction", VerifyTransaction); } NODE_MODULE(bitcoindjs, init)