From 016bc6e3ed3a1b052ff0a58de5273bb835d229f3 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 11 Dec 2014 10:27:20 -0300 Subject: [PATCH] fixing tests for new script internals --- lib/script.js | 61 +++++++++++++++++++++----------------- test/script.js | 20 ++++++------- test/script_interpreter.js | 2 -- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/lib/script.js b/lib/script.js index 05d7c837b..e5e4468e0 100644 --- a/lib/script.js +++ b/lib/script.js @@ -87,7 +87,9 @@ Script.fromBuffer = function(buffer) { opcodenum: opcodenum }); } else { - script.chunks.push(opcodenum); + script.chunks.push({ + opcodenum: opcodenum + }); } } @@ -99,13 +101,9 @@ Script.prototype.toBuffer = function() { for (var i = 0; i < this.chunks.length; i++) { var chunk = this.chunks[i]; - var opcodenum; - if (typeof chunk === 'number') { - opcodenum = chunk; - bw.writeUInt8(opcodenum); - } else { - opcodenum = chunk.opcodenum; - bw.writeUInt8(chunk.opcodenum); + var opcodenum = chunk.opcodenum; + bw.writeUInt8(chunk.opcodenum); + if (chunk.buf) { if (opcodenum < Opcode.map.OP_PUSHDATA1) { bw.write(chunk.buf); } else if (opcodenum === Opcode.map.OP_PUSHDATA1) { @@ -163,7 +161,9 @@ Script.fromString = function(str) { }); i = i + 3; } else { - script.chunks.push(opcodenum); + script.chunks.push({ + opcodenum: opcodenum + }); i = i + 1; } } @@ -175,23 +175,25 @@ Script.prototype.toString = function() { for (var i = 0; i < this.chunks.length; i++) { var chunk = this.chunks[i]; - var opcodenum; - if (typeof chunk === 'number') { - opcodenum = chunk; - str = str + Opcode(opcodenum).toString() + ' '; - } else { - opcodenum = chunk.opcodenum; - if (opcodenum === Opcode.map.OP_PUSHDATA1 || - opcodenum === Opcode.map.OP_PUSHDATA2 || - opcodenum === Opcode.map.OP_PUSHDATA4) { - str = str + Opcode(opcodenum).toString() + ' '; + var opcodenum = chunk.opcodenum; + if (!chunk.buf) { + if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') { + str = str + ' ' + Opcode(opcodenum).toString(); + } else { + str = str + ' ' + '0x' + opcodenum.toString(16); } - str = str + chunk.len + ' '; - str = str + '0x' + chunk.buf.toString('hex') + ' '; + } else { + if (opcodenum === Opcode.OP_PUSHDATA1 || + opcodenum === Opcode.OP_PUSHDATA2 || + opcodenum === Opcode.OP_PUSHDATA4) { + str = str + ' ' + Opcode(opcodenum).toString(); + } + str = str + ' ' + chunk.len; + str = str + ' ' + '0x' + chunk.buf.toString('hex'); } } - return str.substr(0, str.length - 1); + return str.substr(1); }; // script classification methods @@ -200,11 +202,14 @@ Script.prototype.toString = function() { * @returns true if this is a pay to pubkey hash output script */ Script.prototype.isPublicKeyHashOut = function() { - return this.chunks[0] === Opcode('OP_DUP').toNumber() && - this.chunks[1] === Opcode('OP_HASH160').toNumber() && + return this.chunks[0] && this.chunks[0].opcodenum === Opcode.OP_DUP && + this.chunks[1] && + this.chunks[1].opcodenum === Opcode.OP_HASH160 && this.chunks[2].buf && - this.chunks[3] === Opcode('OP_EQUALVERIFY').toNumber() && - this.chunks[4] === Opcode('OP_CHECKSIG').toNumber(); + this.chunks[3] && + this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY && + this.chunks[4] && + this.chunks[4].opcodenum === Opcode.OP_CHECKSIG; }; /** @@ -301,7 +306,7 @@ Script.prototype.isMultisigIn = function() { * @returns true if this is an OP_RETURN data script */ Script.prototype.isDataOut = function() { - return (this.chunks[0] === Opcode('OP_RETURN').toNumber() && + return (this.chunks[0].opcodenum === Opcode('OP_RETURN').toNumber() && (this.chunks.length === 1 || (this.chunks.length === 2 && this.chunks[1].buf && @@ -470,7 +475,7 @@ Script.prototype.removeCodeseparators = function() { * requiring m of those public keys to spend * @param {PublicKey[]} pubkeys - list of all public keys controlling the output * @param {number} m - amount of required signatures to spend the output - * @param {Object} [opts] - Several options: + * @param {Object} [opts] - Several options: * - noSorting: defaults to false, if true, don't sort the given * public keys before creating the script */ diff --git a/test/script.js b/test/script.js index 736079cfb..558e19aef 100644 --- a/test/script.js +++ b/test/script.js @@ -22,7 +22,7 @@ describe('Script', function() { buf[0] = Opcode('OP_0').toNumber(); var script = Script.fromBuffer(buf); script.chunks.length.should.equal(1); - script.chunks[0].should.equal(buf[0]); + script.chunks[0].opcodenum.should.equal(buf[0]); }); it('should parse this buffer containing another OP code', function() { @@ -30,7 +30,7 @@ describe('Script', function() { buf[0] = Opcode('OP_CHECKMULTISIG').toNumber(); var script = Script.fromBuffer(buf); script.chunks.length.should.equal(1); - script.chunks[0].should.equal(buf[0]); + script.chunks[0].opcodenum.should.equal(buf[0]); }); it('should parse this buffer containing three bytes of data', function() { @@ -75,9 +75,9 @@ describe('Script', function() { buf[buf.length - 1] = Opcode('OP_0').toNumber(); var script = Script.fromBuffer(buf); script.chunks.length.should.equal(3); - script.chunks[0].should.equal(buf[0]); + script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[1].buf.toString('hex').should.equal('010203'); - script.chunks[2].should.equal(buf[buf.length - 1]); + script.chunks[2].opcodenum.should.equal(buf[buf.length - 1]); }); }); @@ -89,7 +89,7 @@ describe('Script', function() { buf[0] = Opcode('OP_0').toNumber(); var script = Script.fromBuffer(buf); script.chunks.length.should.equal(1); - script.chunks[0].should.equal(buf[0]); + script.chunks[0].opcodenum.should.equal(buf[0]); script.toBuffer().toString('hex').should.equal(buf.toString('hex')); }); @@ -98,7 +98,7 @@ describe('Script', function() { buf[0] = Opcode('OP_CHECKMULTISIG').toNumber(); var script = Script.fromBuffer(buf); script.chunks.length.should.equal(1); - script.chunks[0].should.equal(buf[0]); + script.chunks[0].opcodenum.should.equal(buf[0]); script.toBuffer().toString('hex').should.equal(buf.toString('hex')); }); @@ -148,9 +148,9 @@ describe('Script', function() { buf[buf.length - 1] = Opcode('OP_0').toNumber(); var script = Script.fromBuffer(buf); script.chunks.length.should.equal(3); - script.chunks[0].should.equal(buf[0]); + script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[1].buf.toString('hex').should.equal('010203'); - script.chunks[2].should.equal(buf[buf.length - 1]); + script.chunks[2].opcodenum.should.equal(buf[buf.length - 1]); script.toBuffer().toString('hex').should.equal(buf.toString('hex')); }); @@ -182,9 +182,9 @@ describe('Script', function() { buf[buf.length - 1] = Opcode('OP_0').toNumber(); var script = Script.fromBuffer(buf); script.chunks.length.should.equal(3); - script.chunks[0].should.equal(buf[0]); + script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[1].buf.toString('hex').should.equal('010203'); - script.chunks[2].should.equal(buf[buf.length - 1]); + script.chunks[2].opcodenum.should.equal(buf[buf.length - 1]); script.toString().toString('hex').should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0'); }); diff --git a/test/script_interpreter.js b/test/script_interpreter.js index f12c34263..802b5db4c 100644 --- a/test/script_interpreter.js +++ b/test/script_interpreter.js @@ -58,9 +58,7 @@ describe('ScriptInterpreter', function() { var verified; var si = ScriptInterpreter(); verified = si.verify(Script('OP_1'), Script('OP_1')); - console.log(si.errstr); verified.should.equal(true); - var verified = ScriptInterpreter().verify(Script('OP_1'), Script('OP_0')); verified.should.equal(false); verified = ScriptInterpreter().verify(Script('OP_0'), Script('OP_1'));