From 895bb6bab93d7662dd019640ced47661d14ed1ff Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 15 Jul 2014 11:59:56 -0700 Subject: [PATCH] use correct constant & share between Transaction and ScriptInterpreter Closes https://github.com/bitpay/bitcore/issues/424 --- lib/ScriptInterpreter.js | 10 +++++----- lib/Transaction.js | 13 ++++--------- test/test.ScriptInterpreter.js | 10 ++++++++++ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/ScriptInterpreter.js b/lib/ScriptInterpreter.js index d5738b3..ac9912b 100644 --- a/lib/ScriptInterpreter.js +++ b/lib/ScriptInterpreter.js @@ -8,11 +8,6 @@ var Util = require('../util'); var Script = require('./Script'); var Key = require('./Key'); -var SIGHASH_ALL = 1; -var SIGHASH_NONE = 2; -var SIGHASH_SINGLE = 3; -var SIGHASH_ANYONECANPAY = 80; - var intToBufferSM = Util.intToBufferSM var bufferSMToInt = Util.bufferSMToInt; @@ -22,6 +17,11 @@ function ScriptInterpreter(opts) { this.disableUnsafeOpcodes = true; }; +var SIGHASH_ALL = ScriptInterpreter.SIGHASH_ALL = 1; +var SIGHASH_NONE = ScriptInterpreter.SIGHASH_NONE = 2; +var SIGHASH_SINGLE = ScriptInterpreter.SIGHASH_SINGLE = 3; +var SIGHASH_ANYONECANPAY = ScriptInterpreter.SIGHASH_ANYONECANPAY = 0x80; + ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, callback) { if ("function" !== typeof callback) { throw new Error("ScriptInterpreter.eval() requires a callback"); diff --git a/lib/Transaction.js b/lib/Transaction.js index eeb1f8b..0675624 100644 --- a/lib/Transaction.js +++ b/lib/Transaction.js @@ -299,15 +299,10 @@ Transaction.prototype.getAffectedKeys = function getAffectedKeys(txCache) { var OP_CODESEPARATOR = 171; -var SIGHASH_ALL = 1; -var SIGHASH_NONE = 2; -var SIGHASH_SINGLE = 3; -var SIGHASH_ANYONECANPAY = 0x80; - -Transaction.SIGHASH_ALL = SIGHASH_ALL; -Transaction.SIGHASH_NONE = SIGHASH_NONE; -Transaction.SIGHASH_SINGLE = SIGHASH_SINGLE; -Transaction.SIGHASH_ANYONECANPAY = SIGHASH_ANYONECANPAY; +var SIGHASH_ALL = Transaction.SIGHASH_ALL = ScriptInterpreter.SIGHASH_ALL; +var SIGHASH_NONE = Transaction.SIGHASH_NONE = ScriptInterpreter.SIGHASH_NONE; +var SIGHASH_SINGLE = Transaction.SIGHASH_SINGLE = ScriptInterpreter.SIGHASH_SINGLE; +var SIGHASH_ANYONECANPAY = Transaction.SIGHASH_ANYONECANPAY = ScriptInterpreter.SIGHASH_ANYONECANPAY; var TransactionSignatureSerializer = function(txTo, scriptCode, nIn, nHashType) { this.txTo = txTo; diff --git a/test/test.ScriptInterpreter.js b/test/test.ScriptInterpreter.js index 78864fe..0cac123 100644 --- a/test/test.ScriptInterpreter.js +++ b/test/test.ScriptInterpreter.js @@ -18,6 +18,16 @@ describe('ScriptInterpreter', function() { var si = new ScriptInterpreter(); should.exist(si); }); + + describe('SIGHASH constants', function() { + it('should equal the values from bitcoin core', function() { + ScriptInterpreter.SIGHASH_ALL.should.equal(1); + ScriptInterpreter.SIGHASH_NONE.should.equal(2); + ScriptInterpreter.SIGHASH_SINGLE.should.equal(3); + ScriptInterpreter.SIGHASH_ANYONECANPAY.should.equal(0x80); + }); + }); + var testScripts = function(data, valid) { data.forEach(function(datum) { if (datum.length < 2) throw new Error('Invalid test data');