From 19e15f91ca59ed5e97bf342a9e979aebd052fafa Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 10 Mar 2014 22:18:12 -0300 Subject: [PATCH 1/8] size tests working!!! :D --- ScriptInterpreter.js | 6 +++++- test/test.ScriptInterpreter.js | 2 +- test/test.util.js | 22 +++++++++++++++++----- util/util.js | 27 ++++++++++++++++++++++----- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index fc901406d..27a217ee5 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 8ede7b42d..01777dd57 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 99ff94911..8e5c5044f 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 2b78394d8..a31c9f186 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 Date: Tue, 18 Mar 2014 12:08:26 -0300 Subject: [PATCH 2/8] fix negative number arithmetic! --- ScriptInterpreter.js | 72 +++++++++++++++++++++------------- test/test.ScriptInterpreter.js | 21 +++++++--- test/test.examples.js | 4 +- test/test.util.js | 2 + util/util.js | 51 ++++++++++++------------ 5 files changed, 87 insertions(+), 63 deletions(-) diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 27a217ee5..d1992c7c6 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -119,7 +119,10 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_14: case OP_15: case OP_16: - this.stack.push(bigintToBuffer(opcode - OP_1 + 1)); + var opint = opcode - OP_1 + 1; + var opbuf = bigintToBuffer(opint); + console.log('op'+opcode+' = '+opint+', '+buffertools.toHex(opbuf)); + this.stack.push(opbuf); break; case OP_NOP: @@ -352,8 +355,6 @@ 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; @@ -396,6 +397,10 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, // (x1 x2 - bool) var v1 = this.stackTop(2); var v2 = this.stackTop(1); + console.log('equal'); + console.log(v1); + console.log(v2); + var value = buffertools.compare(v1, v2) === 0; // OP_NOTEQUAL is disabled because it would be too easy to say @@ -476,6 +481,9 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, // (x1 x2 -- out) var v1 = castBigint(this.stackTop(2)); var v2 = castBigint(this.stackTop(1)); + console.log('add'); + console.log(v1); + console.log(v2); var num; switch (opcode) { case OP_ADD: @@ -874,7 +882,6 @@ var castBigint = ScriptInterpreter.castBigint = function castBigint(v) { if (!v.length) { return bignum(0); } - // Arithmetic operands must be in range [-2^31...2^31] if (v.length > 4) { throw new Error("Bigint cast overflow (> 4 bytes)"); @@ -883,46 +890,55 @@ var castBigint = ScriptInterpreter.castBigint = function castBigint(v) { var w = new Buffer(v.length); v.copy(w); w = buffertools.reverse(w); - if (w[0] & 0x80) { - w[0] &= 0x7f; - return bignum.fromBuffer(w).neg(); + console.log('v ='+buffertools.toHex(w)); + var isNeg = w[0] & 0x80; + if (isNeg) { + for (var i = 0; i>> 0 - }; -}; var fitsInNBits = function(integer, n) { // TODO: make this efficient!!! return integer.toString(2).replace('-','').length < n; @@ -127,6 +121,21 @@ exports.bytesNeededToStore = bytesNeededToStore = function(integer) { return Math.ceil(((integer).toString(2).replace('-','').length + 1)/ 8); }; +exports.negativeBuffer = negativeBuffer = function(b) { + // implement two-complement negative + var c = new Buffer(b.length); + // negate each byte + for (var i=0; i=0; i--){ + c[i] += 1; + if (c[i] !== 0) break; + } + console.log('negative of '+buffertools.toHex(b)+' is '+buffertools.toHex(c)); + return c; +} exports.intToBuffer = function(integer) { var size = bytesNeededToStore(integer); @@ -139,27 +148,15 @@ exports.intToBuffer = function(integer) { if (si.lenght === 1) { si = '0' + si; } - buf.word8((neg?-1:1)*parseInt(si, 16)); + buf.word8(parseInt(si, 16)); } - return buf.buffer(); - - var data = null; - if (fitsInNBits(integer, 8)) { - data = new Buffer(1); - data.writeInt8(integer, 0); - } else if (fitsInNBits(integer, 16)) { - data = new Buffer(2); - data.writeInt16LE(integer, 0); - } else if (fitsInNBits(integer, 32)) { - data = new Buffer(4); - data.writeInt32LE(integer, 0); - } else { - var x = intTo64Bits(integer); - data = new Buffer(8); - data.writeInt32LE(x.hi, 0); // high part contains sign information (signed) - data.writeUInt32LE(x.lo, 4); // low part encoded as unsigned integer + var ret = buf.buffer(); + if (neg) { + ret = buffertools.reverse(ret); + ret = negativeBuffer(ret); + ret = buffertools.reverse(ret); } - return data; + return ret; }; var formatValue = exports.formatValue = function (valueBuffer) { From 9ef8b781826eac502783ff74b3dc2949cfc237ed Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Mar 2014 16:52:34 -0300 Subject: [PATCH 3/8] refactor int conversion, encoding, and utils --- Script.js | 2 +- ScriptInterpreter.js | 101 ++++++--------------------------- test/test.ScriptInterpreter.js | 19 ------- test/test.examples.js | 4 +- test/test.util.js | 32 ++++++++--- util/util.js | 78 ++++++++++++++++++++++++- 6 files changed, 120 insertions(+), 116 deletions(-) diff --git a/Script.js b/Script.js index 873969fd8..d0abe3a72 100644 --- a/Script.js +++ b/Script.js @@ -509,7 +509,7 @@ Script.stringToBuffer = function(s) { if (!isNaN(integer)) { // integer //console.log('integer'); - var data = util.intToBuffer(integer); + var data = util.intToBufferSM(integer); buf.put(Script.chunksToBuffer([data])); } else if (word[0] === '\'' && word[word.length-1] === '\'') { // string diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index d1992c7c6..39a240d9f 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -18,6 +18,9 @@ for (var i in Opcode.map) { eval(i + " = " + Opcode.map[i] + ";"); } +var intToBufferSM = Util.intToBufferSM +var bufferSMToInt = Util.bufferSMToInt; + function ScriptInterpreter() { this.stack = []; this.disableUnsafeOpcodes = true; @@ -120,8 +123,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_15: case OP_16: var opint = opcode - OP_1 + 1; - var opbuf = bigintToBuffer(opint); - console.log('op'+opcode+' = '+opint+', '+buffertools.toHex(opbuf)); + var opbuf = intToBufferSM(opint); this.stack.push(opbuf); break; @@ -246,7 +248,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_DEPTH: // -- stacksize var value = bignum(this.stack.length); - this.stack.push(bigintToBuffer(value)); + this.stack.push(intToBufferSM(value)); break; case OP_DROP: @@ -355,7 +357,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_SIZE: // (in -- in size) var value = bignum(this.stackTop().length); - this.stack.push(bigintToBuffer(value)); + this.stack.push(intToBufferSM(value)); break; case OP_INVERT: @@ -397,9 +399,6 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, // (x1 x2 - bool) var v1 = this.stackTop(2); var v2 = this.stackTop(1); - console.log('equal'); - console.log(v1); - console.log(v2); var value = buffertools.compare(v1, v2) === 0; @@ -430,7 +429,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_NOT: case OP_0NOTEQUAL: // (in -- out) - var num = castBigint(this.stackTop()); + var num = bufferSMToInt(this.stackTop()); switch (opcode) { case OP_1ADD: num = num.add(bignum(1)); @@ -457,7 +456,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, num = bignum(num.cmp(0) == 0 ? 0 : 1); break; } - this.stack[this.stack.length - 1] = bigintToBuffer(num); + this.stack[this.stack.length - 1] = intToBufferSM(num); break; case OP_ADD: @@ -479,11 +478,8 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_MIN: case OP_MAX: // (x1 x2 -- out) - var v1 = castBigint(this.stackTop(2)); - var v2 = castBigint(this.stackTop(1)); - console.log('add'); - console.log(v1); - console.log(v2); + var v1 = bufferSMToInt(this.stackTop(2)); + var v2 = bufferSMToInt(this.stackTop(1)); var num; switch (opcode) { case OP_ADD: @@ -559,7 +555,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, } this.stackPop(); this.stackPop(); - this.stack.push(bigintToBuffer(num)); + this.stack.push(intToBufferSM(num)); if (opcode === OP_NUMEQUALVERIFY) { if (castBool(this.stackTop())) { @@ -572,14 +568,14 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_WITHIN: // (x min max -- out) - var v1 = castBigint(this.stackTop(3)); - var v2 = castBigint(this.stackTop(2)); - var v3 = castBigint(this.stackTop(1)); + var v1 = bufferSMToInt(this.stackTop(3)); + var v2 = bufferSMToInt(this.stackTop(2)); + var v3 = bufferSMToInt(this.stackTop(1)); this.stackPop(); this.stackPop(); this.stackPop(); var value = v1.cmp(v2) >= 0 && v1.cmp(v3) < 0; - this.stack.push(bigintToBuffer(value ? 1 : 0)); + this.stack.push(intToBufferSM(value ? 1 : 0)); break; case OP_RIPEMD160: @@ -759,7 +755,6 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, return; default: - console.log('opcode '+opcode); throw new Error("Unknown opcode encountered"); } @@ -854,7 +849,7 @@ ScriptInterpreter.prototype.getPrimitiveStack = function getPrimitiveStack() { if (entry.length > 2) { return buffertools.toHex(entry.slice(0)); } - var num = castBigint(entry); + var num = bufferSMToInt(entry); if (num.cmp(-128) >= 0 && num.cmp(127) <= 0) { return num.toNumber(); } else { @@ -876,69 +871,7 @@ var castBool = ScriptInterpreter.castBool = function castBool(v) { return false; }; var castInt = ScriptInterpreter.castInt = function castInt(v) { - return castBigint(v).toNumber(); -}; -var castBigint = ScriptInterpreter.castBigint = function castBigint(v) { - if (!v.length) { - return bignum(0); - } - // Arithmetic operands must be in range [-2^31...2^31] - if (v.length > 4) { - throw new Error("Bigint cast overflow (> 4 bytes)"); - } - - var w = new Buffer(v.length); - v.copy(w); - w = buffertools.reverse(w); - console.log('v ='+buffertools.toHex(w)); - var isNeg = w[0] & 0x80; - if (isNeg) { - for (var i = 0; i 0) { + b = v.toBuffer(); + c = padSign(b); + c = buffertools.reverse(c); + } else if (cmp == 0) { + c = new Buffer([]); + } else { + b = v.neg().toBuffer(); + c = padSign(b); + c[0] |= 0x80; + c = buffertools.reverse(c); + } + return c; +}; + +/* + * Reverse of intToBufferSM + */ +exports.bufferSMToInt = function(v) { + if (!v.length) { + return bignum(0); + } + // Arithmetic operands must be in range [-2^31...2^31] + if (v.length > 4) { + throw new Error('Bigint cast overflow (> 4 bytes)'); + } + + var w = new Buffer(v.length); + v.copy(w); + w = buffertools.reverse(w); + var isNeg = w[0] & 0x80; + if (isNeg) { + w[0] &= 0x7f; + return bignum.fromBuffer(w).neg(); + } else { + return bignum.fromBuffer(w); + } +}; + + + var formatValue = exports.formatValue = function (valueBuffer) { var value = valueToBigInt(valueBuffer).toString(); var integerPart = value.length > 8 ? value.substr(0, value.length-8) : '0'; From 03d200bad76a03ac5423093062272c35dcd7a23c Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Mar 2014 18:46:19 -0300 Subject: [PATCH 4/8] some invalid script tests working --- Script.js | 22 +++++++---- ScriptInterpreter.js | 2 +- test/test.ScriptInterpreter.js | 71 ++++++++++++++++++++++++---------- test/test.examples.js | 4 +- 4 files changed, 68 insertions(+), 31 deletions(-) diff --git a/Script.js b/Script.js index d0abe3a72..2ef22c40c 100644 --- a/Script.js +++ b/Script.js @@ -33,8 +33,10 @@ function Script(buffer) { } else { this.buffer = util.EMPTY_BUFFER; } + console.log(buffertools.toHex(this.buffer)); this.chunks = []; this.parse(); + console.log(this.chunks); }; this.class = Script; @@ -51,19 +53,25 @@ Script.prototype.parse = function() { while (!parser.eof()) { var opcode = parser.word8(); - var len; + var len, chunk; if (opcode > 0 && opcode < OP_PUSHDATA1) { // Read some bytes of data, opcode value is the length of data this.chunks.push(parser.buffer(opcode)); - } else if (opcode == OP_PUSHDATA1) { + } else if (opcode === OP_PUSHDATA1) { len = parser.word8(); - this.chunks.push(parser.buffer(len)); - } else if (opcode == OP_PUSHDATA2) { + chunk = parser.buffer(len); + if (chunk.length < len) throw new Error('Invalid data size: not enough data'); + this.chunks.push(chunk); + } else if (opcode === OP_PUSHDATA2) { len = parser.word16le(); - this.chunks.push(parser.buffer(len)); - } else if (opcode == OP_PUSHDATA4) { + chunk = parser.buffer(len); + if (chunk.length < len) throw new Error('Invalid data size: not enough data'); + this.chunks.push(chunk); + } else if (opcode === OP_PUSHDATA4) { len = parser.word32le(); - this.chunks.push(parser.buffer(len)); + chunk = parser.buffer(len); + if (chunk.length < len) throw new Error('Invalid data size: not enough data'); + this.chunks.push(chunk); } else { this.chunks.push(opcode); } diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 39a240d9f..0f5210f09 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -30,7 +30,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, if ("function" !== typeof callback) { throw new Error("ScriptInterpreter.eval() requires a callback"); } - + var pc = 0; var execStack = []; var altStack = []; diff --git a/test/test.ScriptInterpreter.js b/test/test.ScriptInterpreter.js index dbdbae675..f245fcfa8 100644 --- a/test/test.ScriptInterpreter.js +++ b/test/test.ScriptInterpreter.js @@ -23,42 +23,71 @@ describe('ScriptInterpreter', function() { var si = new ScriptInterpreter(); should.exist(si); }); - testdata.dataScriptValid.forEach(function(datum) { - if (datum.length < 2) throw new Error('Invalid test data'); - var scriptSig = datum[0]; // script inputs - var scriptPubKey = datum[1]; // output script - var human = scriptSig + ' ' + scriptPubKey; - it('should validate script ' + human, function(done) { - ScriptInterpreter.verify(Script.fromHumanReadable(scriptSig), - Script.fromHumanReadable(scriptPubKey), - null, 0, 0, // tx, output index, and hashtype - function(err, result) { - should.not.exist(err); - result.should.equal(true); + var testScripts = function(data, valid) { + var i = 0; + data.forEach(function(datum) { + if (datum.length < 2) throw new Error('Invalid test data'); + var scriptSig = datum[0]; // script inputs + var scriptPubKey = datum[1]; // output script + var human = scriptSig + ' ' + scriptPubKey; + it('should ' + (!valid ? 'not ' : '') + 'validate script ' + human, function(done) { + console.log((!valid ? 'invalid ' : 'valid ') + human + ';' + (i++) + ' - ' + datum[2]); + try { + ScriptInterpreter.verify( + Script.fromHumanReadable(scriptSig), + Script.fromHumanReadable(scriptPubKey), + null, 0, 0, // tx, output index, and hashtype + function(err, result) { + if (valid) { + should.not.exist(err); + } else { + var failed = (typeof err !== 'undefined') || (result === false); + console.log('err=' + err); + console.log('result=' + result); + failed.should.equal(true); + } + if (typeof result !== 'undefined') { + result.should.equal(valid); + } + done(); + } + ); + } catch (e) { + if (valid) { + console.log(e); + } + valid.should.equal(false); done(); } - ); + + }); }); - }); + }; + testScripts(testdata.dataScriptValid, true); + testScripts(testdata.dataScriptInvalid, false); + + + testdata.dataSigCanonical.forEach(function(datum) { it('should validate valid canonical signatures', function() { - ScriptInterpreter.isCanonicalSignature(new Buffer(datum,'hex')).should.equal(true); + ScriptInterpreter.isCanonicalSignature(new Buffer(datum, 'hex')).should.equal(true); }); }); - testdata.dataSigNonCanonical.forEach(function(datum) { + testdata.dataSigNonCanonical.forEach(function(datum) { it('should NOT validate invalid canonical signatures', function() { var sig; var isHex; //is Hex? try { - sig =new Buffer(datum,'hex'); - isHex=1; - } catch (e) { } + sig = new Buffer(datum, 'hex'); + isHex = 1; + } catch (e) {} if (isHex) - ScriptInterpreter.isCanonicalSignature.bind(sig).should.throw(); + ScriptInterpreter.isCanonicalSignature.bind(sig).should. + throw (); }); }); - + }); diff --git a/test/test.examples.js b/test/test.examples.js index 4115bc7c5..55c9ccd2f 100644 --- a/test/test.examples.js +++ b/test/test.examples.js @@ -15,8 +15,8 @@ var examples = [ ]; describe('Examples', function() { - before(mute); - after(unmute); + //before(mute); + //after(unmute); examples.forEach(function(example) { it('valid '+example, function() { var ex = require('../examples/'+example); From ddb3e6de70082e2698fd3e33baeedda6f58bcb41 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 18 Mar 2014 19:17:43 -0300 Subject: [PATCH 5/8] invalid script test passing! --- Script.js | 4 +--- ScriptInterpreter.js | 5 +++-- test/test.ScriptInterpreter.js | 12 +++++------- test/test.examples.js | 4 ++-- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Script.js b/Script.js index 2ef22c40c..37d705cca 100644 --- a/Script.js +++ b/Script.js @@ -33,11 +33,9 @@ function Script(buffer) { } else { this.buffer = util.EMPTY_BUFFER; } - console.log(buffertools.toHex(this.buffer)); this.chunks = []; this.parse(); - console.log(this.chunks); -}; +} this.class = Script; Script.TX_UNKNOWN = TX_UNKNOWN; diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 0f5210f09..dce95817c 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -811,7 +811,7 @@ ScriptInterpreter.prototype.stackTop = function stackTop(offset) { }; ScriptInterpreter.prototype.stackBack = function stackBack() { - return this.stack[-1]; + return this.stack[this.stack.length -1]; }; /** @@ -944,7 +944,8 @@ function verifyStep3(scriptSig, scriptPubKey, txTo, nIn, return; } - assert.notEqual(siCopy.length, 0); + if (siCopy.length === 0) + throw new Error('siCopy should have length != 0'); var subscript = new Script(siCopy.stackPop()); diff --git a/test/test.ScriptInterpreter.js b/test/test.ScriptInterpreter.js index f245fcfa8..e4f579b31 100644 --- a/test/test.ScriptInterpreter.js +++ b/test/test.ScriptInterpreter.js @@ -31,19 +31,17 @@ describe('ScriptInterpreter', function() { var scriptPubKey = datum[1]; // output script var human = scriptSig + ' ' + scriptPubKey; it('should ' + (!valid ? 'not ' : '') + 'validate script ' + human, function(done) { - console.log((!valid ? 'invalid ' : 'valid ') + human + ';' + (i++) + ' - ' + datum[2]); try { - ScriptInterpreter.verify( - Script.fromHumanReadable(scriptSig), - Script.fromHumanReadable(scriptPubKey), - null, 0, 0, // tx, output index, and hashtype + ScriptInterpreter.verifyFull( + Script.fromHumanReadable(scriptSig), // scriptSig + Script.fromHumanReadable(scriptPubKey), // scriptPubKey + null, 0, 0, // tx, output index, hashtype + { verifyP2SH: !valid}, // only verify P2SH for invalid data set function(err, result) { if (valid) { should.not.exist(err); } else { var failed = (typeof err !== 'undefined') || (result === false); - console.log('err=' + err); - console.log('result=' + result); failed.should.equal(true); } if (typeof result !== 'undefined') { diff --git a/test/test.examples.js b/test/test.examples.js index 55c9ccd2f..4115bc7c5 100644 --- a/test/test.examples.js +++ b/test/test.examples.js @@ -15,8 +15,8 @@ var examples = [ ]; describe('Examples', function() { - //before(mute); - //after(unmute); + before(mute); + after(unmute); examples.forEach(function(example) { it('valid '+example, function() { var ex = require('../examples/'+example); From 856225d3773f27ee5e962ae382205c21bf8bbfa8 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 19 Mar 2014 12:02:03 -0300 Subject: [PATCH 6/8] fix for firefox --- package.json | 2 +- test/test.util.js | 11 +++++++---- util/util.js | 5 ++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 62f54eb48..eb5a3c5c2 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "browser-pack": "~2.0.1", "commander": "~2.1.0", "browserify-bignum": "git://github.com/maraoz/browserify-bignum.git", - "browserify-buffertools": "~1.0.2", + "browserify-buffertools": "git://github.com/maraoz/browserify-buffertools.git", "brfs": "~1.0.0", "uglifyify": "~1.2.3" }, diff --git a/test/test.util.js b/test/test.util.js index 45deab657..0833bcf57 100644 --- a/test/test.util.js +++ b/test/test.util.js @@ -82,6 +82,7 @@ describe('util', function() { }); describe('#intToBuffer2C', function() { var data = [ + /* [0, ''], [-0, ''], [1, '01'], @@ -92,7 +93,9 @@ describe('util', function() { [128, '8000'], [129, '8100'], [4096, '0010'], + */ [-4096, '00f0'], + /* [32767, 'ff7f'], [878082192, '90785634'], [0x01234567890, '9078563412'], @@ -101,15 +104,15 @@ describe('util', function() { [4294967297, '0100000001'], [2147483647, 'ffffff7f'], [-2147483647, '01000080'], - //[-4294967295, 'feffffffffffffff'], - //[-4294967296, 'feffffffffffffff'], - //[-4294967297, 'feffffffffffffff'], + */ ]; data.forEach(function(datum) { var integer = datum[0]; var result = datum[1]; it('should work for ' + integer, function() { - buffertools.toHex(coinUtil.intToBuffer2C(integer)).should.equal(result); + var buf = coinUtil.intToBuffer2C(integer); + var hex = buffertools.toHex(buf); + hex.should.equal(result); }); }); }); diff --git a/util/util.js b/util/util.js index 75555cef7..f5fc8c0fd 100644 --- a/util/util.js +++ b/util/util.js @@ -127,10 +127,12 @@ exports.negativeBuffer = negativeBuffer = function(b) { // negate each byte for (var i=0; i=0; i--){ c[i] += 1; + if (c[i] >= 256) c[i] -= 256; if (c[i] !== 0) break; } return c; @@ -153,7 +155,8 @@ exports.intToBuffer2C = function(integer) { if (si.lenght === 1) { si = '0' + si; } - buf.word8(parseInt(si, 16)); + var pi = parseInt(si, 16); + buf.word8(pi); } var ret = buf.buffer(); if (neg) { From 6ac48b2809982e6aceab6020e18a169895ecd1ca Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 19 Mar 2014 13:03:12 -0300 Subject: [PATCH 7/8] uncomment some test cases! --- test/test.util.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/test.util.js b/test/test.util.js index 0833bcf57..205cf228d 100644 --- a/test/test.util.js +++ b/test/test.util.js @@ -82,7 +82,6 @@ describe('util', function() { }); describe('#intToBuffer2C', function() { var data = [ - /* [0, ''], [-0, ''], [1, '01'], @@ -93,9 +92,7 @@ describe('util', function() { [128, '8000'], [129, '8100'], [4096, '0010'], - */ [-4096, '00f0'], - /* [32767, 'ff7f'], [878082192, '90785634'], [0x01234567890, '9078563412'], @@ -104,7 +101,6 @@ describe('util', function() { [4294967297, '0100000001'], [2147483647, 'ffffff7f'], [-2147483647, '01000080'], - */ ]; data.forEach(function(datum) { var integer = datum[0]; From cfe899ee769d573fec848f6296174dc0f36f991b Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 19 Mar 2014 13:16:01 -0300 Subject: [PATCH 8/8] fix Transaction#create tests --- util/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util/util.js b/util/util.js index f5fc8c0fd..7a3386970 100644 --- a/util/util.js +++ b/util/util.js @@ -98,7 +98,7 @@ var formatBuffer = exports.formatBuffer = function (buffer, maxLen) { var valueToBigInt = exports.valueToBigInt = function (valueBuffer) { if (Buffer.isBuffer(valueBuffer)) { - return bignum.fromBuffer(valueBuffer, {endian: 'little', size: 'auto'}); + return bignum.fromBuffer(valueBuffer, {endian: 'little', size: 8}); } else { return valueBuffer; } @@ -108,7 +108,7 @@ var bigIntToValue = exports.bigIntToValue = function (valueBigInt) { if (Buffer.isBuffer(valueBigInt)) { return valueBigInt; } else { - return valueBigInt.toBuffer({endian: 'little', size: 'auto'}); + return valueBigInt.toBuffer({endian: 'little', size: 8}); } };