diff --git a/lib/transaction.js b/lib/transaction.js index af4a386b..76893db7 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -2,7 +2,6 @@ var async = require('async'); var bitcore = require('bitcore'); -var bitcoinconsensus = require('libbitcoinconsensus'); var Transaction = bitcore.Transaction; var chainlib = require('chainlib'); var BaseTransaction = chainlib.Transaction; @@ -45,7 +44,7 @@ Transaction.prototype._validateInputs = function(db, poolTransactions, callback) [ self._populateInput.bind(self, db, input, poolTransactions), self._checkSpent.bind(self, db, input, poolTransactions), - self._checkScript.bind(self, input, self.inputs.indexOf(input)) + self._checkScript.bind(self, db, input, self.inputs.indexOf(input)) ], next ); @@ -100,11 +99,11 @@ Transaction.prototype._checkSpent = function(db, input, poolTransactions, callba }); }; -Transaction.prototype._checkScript = function(input, index, callback) { +Transaction.prototype._checkScript = function(db, input, index, callback) { if (input.output.script) { var scriptPubkey = input.output._scriptBuffer; var txTo = this.toBuffer(); - var valid = bitcoinconsensus.verifyScript(scriptPubkey, txTo, index); + var valid = db.bitcoind.verifyScript(scriptPubkey, txTo, index); if(valid) { return callback(); } diff --git a/package.json b/package.json index 70663d15..b7cdd308 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,6 @@ "nan": "1.3.0", "tiny": "0.0.10", "chainlib": "^0.1.1", - "libbitcoinconsensus": "^0.0.3", "errno": "^0.1.2", "async": "1.3.0", "memdown": "^1.0.0" diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 78ccd507..c349de82 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -137,6 +137,43 @@ struct async_tx_data { Eternal callback; }; +/** + * Verify Scripts + */ +NAN_METHOD(VerifyScript) { + NanScope(); + + if (!node::Buffer::HasInstance(args[0])) { + return NanThrowTypeError("First argument should be a Buffer."); + } + + if (!node::Buffer::HasInstance(args[1])) { + return NanThrowTypeError("Second argument should be a Buffer."); + } + + unsigned char *scriptPubKey = (unsigned char *) node::Buffer::Data(args[0]); + unsigned int scriptPubKeyLen = (unsigned int) node::Buffer::Length(args[0]); + + const unsigned char *txTo = (unsigned char *) node::Buffer::Data(args[1]); + unsigned int txToLen = (unsigned int)node::Buffer::Length(args[1]); + + unsigned int nIn = args[2]->NumberValue(); + unsigned int flags = args[3]->NumberValue(); + + bitcoinconsensus_error* err; + err = 0; + + int valid = bitcoinconsensus_verify_script(scriptPubKey, scriptPubKeyLen, txTo, txToLen, nIn, flags, err); + + if (!valid && err) { + NanThrowError("The transaction was not valid"); + } + + NanReturnValue(NanNew(valid)); + +} + + /** * Helpers @@ -1120,6 +1157,8 @@ init(Handle target) { NODE_SET_METHOD(target, "getChainWork", GetChainWork); NODE_SET_METHOD(target, "getMempoolOutputs", GetMempoolOutputs); NODE_SET_METHOD(target, "addMempoolUncheckedTransaction", AddMempoolUncheckedTransaction); + NODE_SET_METHOD(target, "verifyScript", VerifyScript); + } NODE_MODULE(bitcoindjs, init) diff --git a/src/bitcoindjs.h b/src/bitcoindjs.h index 28c625e9..40d1a803 100644 --- a/src/bitcoindjs.h +++ b/src/bitcoindjs.h @@ -11,6 +11,7 @@ #include "nan.h" #include "scheduler.h" #include "core_io.h" +#include "script/bitcoinconsensus.h" NAN_METHOD(StartBitcoind); NAN_METHOD(OnBlocksReady); @@ -24,3 +25,4 @@ NAN_METHOD(IsSpent); NAN_METHOD(GetChainWork); NAN_METHOD(GetMempoolOutputs); NAN_METHOD(AddMempoolUncheckedTransaction); +NAN_METHOD(VerifyScript); diff --git a/test/transaction.unit.js b/test/transaction.unit.js index d68ee536..cb3d56ab 100644 --- a/test/transaction.unit.js +++ b/test/transaction.unit.js @@ -62,7 +62,7 @@ describe('Bitcoin Transaction', function() { sinon.stub(tx, '_checkSpent', function(db, input, poolTransactions, callback) { return callback(); }); - sinon.stub(tx, '_checkScript', function(input, index, callback) { + sinon.stub(tx, '_checkScript', function(db, input, index, callback) { return callback(); }); @@ -222,8 +222,13 @@ describe('Bitcoin Transaction', function() { tx.fromString(transactionData[1].hex); var input = tx.inputs[0]; input.output = prevTx.outputs[0]; + var db = { + bitcoind: { + verifyScript: sinon.stub().returns(true) + } + }; - tx._checkScript(input, 0, function(err) { + tx._checkScript(db, input, 0, function(err) { should.not.exist(err); done(); }); @@ -235,8 +240,13 @@ describe('Bitcoin Transaction', function() { tx.fromString(transactionData[2].hex); var input = tx.inputs[0]; input.output = prevTx.outputs[0]; + var db = { + bitcoind: { + verifyScript: sinon.stub().returns(false) + } + }; - tx._checkScript(input, 0, function(err) { + tx._checkScript(db, input, 0, function(err) { should.exist(err); done(); });