diff --git a/lib/script.js b/lib/script.js index 16f366a77..4b7eb2922 100644 --- a/lib/script.js +++ b/lib/script.js @@ -105,6 +105,44 @@ Script.prototype.toBuffer = function() { return bw.concat(); }; +Script.prototype.fromString = function(str) { + this.chunks = []; + + var tokens = str.split(' '); + var i = 0; + while (i < tokens.length) { + var token = tokens[i]; + var opcode = Opcode(token); + var opcodenum = opcode.toNumber(); + + if (typeof opcodenum === 'undefined') { + opcodenum = parseInt(token); + if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) { + this.chunks.push({ + buf: new Buffer(tokens[i + 1].slice(2), 'hex'), + len: opcodenum, + opcodenum: opcodenum + }); + i = i + 2; + } + else { + throw new Error('Invalid script'); + } + } else if (opcodenum === Opcode.map.OP_PUSHDATA1 || opcodenum === Opcode.map.OP_PUSHDATA2 || opcodenum === Opcode.map.OP_PUSHDATA4) { + this.chunks.push({ + buf: new Buffer(tokens[i + 2].slice(2), 'hex'), + len: parseInt(tokens[i + 1]), + opcodenum: opcodenum + }); + i = i + 3; + } else { + this.chunks.push(opcodenum); + i = i + 1; + } + } + return this; +}; + Script.prototype.toString = function() { var str = ""; @@ -115,7 +153,8 @@ Script.prototype.toString = function() { str = str + Opcode(opcodenum).toString() + " "; } else { var opcodenum = chunk.opcodenum; - str = str + Opcode(opcodenum).toString() + " " ; + if (opcodenum === Opcode.map.OP_PUSHDATA1 || opcodenum === Opcode.map.OP_PUSHDATA2 || opcodenum === Opcode.map.OP_PUSHDATA4) + str = str + Opcode(opcodenum).toString() + " " ; str = str + chunk.len + " " ; str = str + "0x" + chunk.buf.toString('hex') + " "; } diff --git a/test/script.js b/test/script.js index 50110c81d..a8af6ca77 100644 --- a/test/script.js +++ b/test/script.js @@ -151,6 +151,17 @@ describe('Script', function() { }); + describe('#fromString', function() { + + it('should parse these known scripts', function() { + Script().fromString('OP_0 OP_PUSHDATA4 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0'); + Script().fromString('OP_0 OP_PUSHDATA2 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA2 3 0x010203 OP_0'); + Script().fromString('OP_0 OP_PUSHDATA1 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA1 3 0x010203 OP_0'); + Script().fromString('OP_0 3 0x010203 OP_0').toString().should.equal('OP_0 3 0x010203 OP_0'); + }); + + }); + describe('#toString', function() { it('should output this buffer an OP code, data, and another OP code', function() {