diff --git a/lib/script.js b/lib/script.js index 4b56b378d..16f366a77 100644 --- a/lib/script.js +++ b/lib/script.js @@ -105,4 +105,23 @@ Script.prototype.toBuffer = function() { return bw.concat(); }; +Script.prototype.toString = function() { + var str = ""; + + for (var i = 0; i < this.chunks.length; i++) { + var chunk = this.chunks[i]; + if (typeof chunk === 'number') { + var opcodenum = chunk; + str = str + Opcode(opcodenum).toString() + " "; + } else { + var opcodenum = chunk.opcodenum; + str = str + Opcode(opcodenum).toString() + " " ; + str = str + chunk.len + " " ; + str = str + "0x" + chunk.buf.toString('hex') + " "; + } + } + + return str.substr(0, str.length - 1); +}; + module.exports = Script; diff --git a/test/script.js b/test/script.js index 6f19ba757..50110c81d 100644 --- a/test/script.js +++ b/test/script.js @@ -151,4 +151,22 @@ describe('Script', function() { }); + describe('#toString', function() { + + it('should output this buffer an OP code, data, and another OP code', function() { + var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]); + buf[0] = Opcode('OP_0').toNumber(); + buf[1] = Opcode('OP_PUSHDATA4').toNumber(); + buf.writeUInt16LE(3, 2); + 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[1].buf.toString('hex').should.equal('010203'); + script.chunks[2].should.equal(buf[buf.length - 1]); + script.toString().toString('hex').should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0'); + }); + + }); + });