diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index fc90140..27a217e 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -1,6 +1,7 @@ var imports = require('soop').imports(); var config = imports.config || require('./config'); var log = imports.log || require('./util/log'); +var util = imports.util || require('./util/util'); var Opcode = imports.Opcode || require('./Opcode'); var buffertools = imports.buffertools || require('buffertools'); var bignum = imports.bignum || require('bignum'); @@ -92,8 +93,9 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, throw new Error("Encountered a disabled opcode"); } - if (exec && Buffer.isBuffer(opcode)) + if (exec && Buffer.isBuffer(opcode)) { this.stack.push(opcode); + } else if (exec || (OP_IF <= opcode && opcode <= OP_ENDIF)) switch (opcode) { case OP_0: @@ -350,6 +352,8 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_SIZE: // (in -- in size) var value = bignum(this.stackTop().length); + //var topSize = util.bytesNeededToStore(castBigint(this.stackTop()).toNumber()); + //var value = bignum(topSize); this.stack.push(bigintToBuffer(value)); break; diff --git a/test/test.ScriptInterpreter.js b/test/test.ScriptInterpreter.js index 8ede7b4..01777dd 100644 --- a/test/test.ScriptInterpreter.js +++ b/test/test.ScriptInterpreter.js @@ -28,7 +28,7 @@ describe('ScriptInterpreter', function() { var scriptSig = datum[0]; // script inputs var scriptPubKey = datum[1]; // output script var human = scriptSig + ' ' + scriptPubKey; - it.skip('should validate script ' + human, function(done) { + it('should validate script ' + human, function(done) { i++; console.log(i + ' ' + human); ScriptInterpreter.verify(Script.fromHumanReadable(scriptSig), diff --git a/test/test.util.js b/test/test.util.js index 99ff949..8e5c504 100644 --- a/test/test.util.js +++ b/test/test.util.js @@ -82,14 +82,26 @@ describe('util', function() { }); describe('#intToBuffer', function() { var data = [ - [0, '00'], - [-0, '00'], - [-1, 'ff'], + [0, ''], + [-0, ''], [1, '01'], + [-1, 'ff'], [18, '12'], + [-18, 'ee'], + [127, '7f'], + [128, '8000'], + [129, '8100'], + [4096, '0010'], + [-4096, '00f0'], + [32767, 'ff7f'], [878082192, '90785634'], - [0x01234567890, '1200000090785634'], - [-4294967297, 'feffffffffffffff'], + [0x01234567890, '9078563412'], + [4294967295, 'ffffffff00'], + [4294967296, '0000000001'], + [4294967297, '0100000001'], + //[-4294967295, 'feffffffffffffff'], + //[-4294967296, 'feffffffffffffff'], + //[-4294967297, 'feffffffffffffff'], ]; data.forEach(function(datum) { var integer = datum[0]; diff --git a/util/util.js b/util/util.js index 2b78394..a31c9f1 100644 --- a/util/util.js +++ b/util/util.js @@ -121,28 +121,45 @@ var intTo64Bits = function(integer) { var fitsInNBits = function(integer, n) { // TODO: make this efficient!!! return integer.toString(2).replace('-','').length < n; -} +}; +exports.bytesNeededToStore = bytesNeededToStore = function(integer) { + if (integer === 0) return 0; + return Math.ceil(((integer).toString(2).replace('-','').length + 1)/ 8); +}; + + exports.intToBuffer = function(integer) { + var size = bytesNeededToStore(integer); + var buf = new Put(); + var s = integer.toString(16); + var neg = s[0] === '-'; + s = s.replace('-',''); + for (var i=0; i