From b37e39abca9feab06989d13b35f3eb1d72268392 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 23 Sep 2014 21:28:03 -0700 Subject: [PATCH] Script().writeXX convenience methods Script().writeOp('OP_CHECKMULTISIG'), or... Script().writeOp(174), or... Script().writeBuffer([push data buffer]), or... Script().write([op string, number, or push data buffer]) These convenience methods let you easily write a script. --- lib/script.js | 42 ++++++++++++++++++++++++++++++++++++++++++ test/script.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/lib/script.js b/lib/script.js index 461109321..63376b27e 100644 --- a/lib/script.js +++ b/lib/script.js @@ -234,4 +234,46 @@ Script.prototype.isScripthashIn = function() { } }; +Script.prototype.write = function(obj) { + if (typeof obj === 'string') + this.writeOp(obj); + else if (typeof obj === 'number') + this.writeOp(obj); + else if (Buffer.isBuffer(obj)) + this.writeBuffer(obj); + else if (typeof obj === 'object') + this.chunks.push(obj); + else + throw new Error('Invalid script chunk'); + return this; +}; + +Script.prototype.writeOp = function(str) { + if (typeof str === 'number') + this.chunks.push(str); + else + this.chunks.push(Opcode(str).toNumber()); + return this; +}; + +Script.prototype.writeBuffer = function(buf) { + var opcodenum; + var len = buf.length; + if (buf.length > 0 && buf.length < Opcode.map.OP_PUSHDATA1) { + opcodenum = buf.length; + } else if (buf.length < Math.pow(2, 8)) { + opcodenum = Opcode.map.OP_PUSHDATA1; + } else if (buf.length < Math.pow(2, 16)) { + opcodenum = Opcode.map.OP_PUSHDATA2; + } else if (buf.length < Math.pow(2, 32)) { + opcodenum = Opcode.map.OP_PUSHDATA4; + } + this.chunks.push({ + buf: buf, + len: len, + opcodenum: opcodenum + }); + return this; +}; + module.exports = Script; diff --git a/test/script.js b/test/script.js index 4aed785f7..83f1b148e 100644 --- a/test/script.js +++ b/test/script.js @@ -265,4 +265,44 @@ describe('Script', function() { }); + describe('#writeOp', function() { + + it('should write these ops', function() { + Script().writeOp('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG'); + Script().writeOp(Opcode.map.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG'); + }); + + }); + + describe('#writeBuffer', function() { + + it('should write these push data', function() { + var buf = new Buffer(1); + buf.fill(0); + Script().writeBuffer(buf).toString().should.equal('1 0x00'); + buf = new Buffer(255); + buf.fill(0); + Script().writeBuffer(buf).toString().should.equal('OP_PUSHDATA1 255 0x' + buf.toString('hex')); + buf = new Buffer(256); + buf.fill(0); + Script().writeBuffer(buf).toString().should.equal('OP_PUSHDATA2 256 0x' + buf.toString('hex')); + buf = new Buffer(Math.pow(2, 16)); + buf.fill(0); + Script().writeBuffer(buf).toString().should.equal('OP_PUSHDATA4 ' + Math.pow(2, 16) + ' 0x' + buf.toString('hex')); + }); + + }); + + describe('#write', function() { + + it('should write both pushdata and non-pushdata chunks', function() { + Script().write('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG'); + Script().write(Opcode.map.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG'); + var buf = new Buffer(1); + buf.fill(0); + Script().write(buf).toString().should.equal('1 0x00'); + }); + + }); + });