Expose verifyScript from the daemon

This commit is contained in:
Braydon Fuller 2015-07-16 17:51:33 -04:00 committed by Patrick Nagurny
parent beaa7cf450
commit 162088f8f4
5 changed files with 57 additions and 8 deletions

View File

@ -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();
}

View File

@ -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"

View File

@ -137,6 +137,43 @@ struct async_tx_data {
Eternal<Function> 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<Number>(valid));
}
/**
* Helpers
@ -1120,6 +1157,8 @@ init(Handle<Object> 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)

View File

@ -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);

View File

@ -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();
});