From d556a0c33dbe508b60308d07385356979b9aa9f6 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 29 Apr 2015 18:55:42 -0300 Subject: [PATCH] fix regressions --- lib/encoding/bufferreader.js | 3 +++ lib/script/script.js | 4 ---- lib/transaction/input/input.js | 3 ++- lib/transaction/transaction.js | 2 +- test/script/interpreter.js | 12 +++++++----- test/script/script.js | 15 --------------- test/transaction/input/input.js | 1 - 7 files changed, 13 insertions(+), 27 deletions(-) diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index 70fe2fe..bb881fb 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -9,6 +9,9 @@ var BufferReader = function BufferReader(buf) { if (!(this instanceof BufferReader)) { return new BufferReader(buf); } + if (_.isUndefined(buf)) { + return; + } if (Buffer.isBuffer(buf)) { this.set({ buf: buf diff --git a/lib/script/script.js b/lib/script/script.js index 4c050a3..b0cd0f2 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -93,10 +93,6 @@ Script.fromBuffer = function(buffer) { opcodenum: opcodenum }); } else { - var op = Opcode.reverseMap[opcodenum]; - if (!op) { - throw new errors.Script.InvalidBuffer(buffer.toString('hex')); - } script.chunks.push({ opcodenum: opcodenum }); diff --git a/lib/transaction/input/input.js b/lib/transaction/input/input.js index 260c561..6cbffc6 100644 --- a/lib/transaction/input/input.js +++ b/lib/transaction/input/input.js @@ -123,7 +123,8 @@ Input.prototype.setScript = function(script) { this._scriptBuffer = new Buffer(script, 'hex'); } else if (_.isString(script)) { // human readable string script - this._scriptBuffer = script.toBuffer(); + this._script = new Script(script); + this._scriptBuffer = this._script.toBuffer(); } else if (BufferUtil.isBuffer(script)) { // buffer script this._scriptBuffer = new buffer.Buffer(script); diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 5d0713c..1b1b986 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -1017,7 +1017,7 @@ Transaction.prototype.verify = function() { var isCoinbase = this.isCoinbase(); if (isCoinbase) { - var buf = this.inputs[0]._script.toBuffer(); + var buf = this.inputs[0]._scriptBuffer; if (buf.length < 2 || buf.length > 100) { return 'coinbase transaction script size invalid'; } diff --git a/test/script/interpreter.js b/test/script/interpreter.js index 5808a18..71d5588 100644 --- a/test/script/interpreter.js +++ b/test/script/interpreter.js @@ -36,6 +36,7 @@ Script.fromBitcoindString = function(str) { var tstr = token.slice(1, token.length - 1); var cbuf = new Buffer(tstr); tbuf = Script().add(cbuf).toBuffer(); + //bw.writeUInt8(tstr.length); bw.write(tbuf); } else if (typeof Opcode['OP_' + token] !== 'undefined') { opstr = 'OP_' + token; @@ -196,9 +197,6 @@ describe('Interpreter', function() { var scriptPubkey = Script.fromBitcoindString(vector[1]); var flags = getFlags(vector[2]); - //testToFromString(scriptSig); - //testToFromString(scriptPubkey); - var hashbuf = new Buffer(32); hashbuf.fill(0); var credtx = new Transaction(); @@ -242,7 +240,8 @@ describe('Interpreter', function() { var fullScriptString = vector[0] + ' ' + vector[1]; var comment = descstr ? (' (' + descstr + ')') : ''; it('should pass script_' + (expected ? '' : 'in') + 'valid ' + - 'vector #' + c + ': ' + fullScriptString + comment, function() { + 'vector #' + c + ': ' + fullScriptString + comment, + function() { testFixture(vector, expected); }); }); @@ -279,12 +278,15 @@ describe('Interpreter', function() { var tx = new Transaction(txhex); var allInputsVerified = true; tx.inputs.forEach(function(txin, j) { + if (txin.isNull()) { + return; + } var scriptSig = txin.script; var txidhex = txin.prevTxId.toString('hex'); var txoutnum = txin.outputIndex; var scriptPubkey = map[txidhex + ':' + txoutnum]; should.exist(scriptPubkey); - should.exist(scriptSig); + (scriptSig !== undefined).should.equal(true); var interp = new Interpreter(); var verified = interp.verify(scriptSig, scriptPubkey, tx, j, flags); if (!verified) { diff --git a/test/script/script.js b/test/script/script.js index 6ac9612..19c1bef 100644 --- a/test/script/script.js +++ b/test/script/script.js @@ -11,8 +11,6 @@ var Opcode = bitcore.Opcode; var PublicKey = bitcore.PublicKey; var Address = bitcore.Address; -var script_valid = require('../data/bitcoind/script_valid'); - describe('Script', function() { it('should make a new script', function() { @@ -754,19 +752,6 @@ describe('Script', function() { Script().add(new Buffer('a')).equals(Script().add(new Buffer('b'))).should.equal(false); }); }); - describe('coinbase transaction input script for tx ', function() { - it('fails for that specific malformed script', function() { - var hex = '03984b05' + // push 0x03 bytes with block height - 'e4' + // attempt to push 0xe4 bytes, but should use OP_PUSHDATA 0xe4 - 'b883e5bda9e7a59ee4bb99e9b1bcfabe6d6d5cb348c1c7d58062783520' + - '2f5ad93c2f3db10bb850a1a513979f8328d9f35aff1000000000000000' + - '006189dd01cf00004d696e6564206279207975313333353131373131'; - var fails = function() { - return new Script(hex); - }; - fails.should.throw('Invalid script buffer: can\'t parse valid script from given buffer'); - }); - }); }); diff --git a/test/transaction/input/input.js b/test/transaction/input/input.js index d4324a9..c07b550 100644 --- a/test/transaction/input/input.js +++ b/test/transaction/input/input.js @@ -73,7 +73,6 @@ describe('Transaction.Input', function() { it('fromObject should work', function() { var input = Input.fromJSON(coinbaseJSON); var obj = input.toObject(); - obj.script = new Buffer(obj.script, 'hex'); Input.fromObject(obj).should.deep.equal(input); obj.script = 42; Input.fromObject.bind(null, obj).should.throw('Invalid argument type: script');