var BufferReader = require('./bufferreader'); var BufferWriter = require('./bufferwriter'); var Opcode = require('./opcode'); var Script = function Script(buf) { if (!(this instanceof Script)) return new Script(buf); this.chunks = []; if (Buffer.isBuffer(buf)) { this.fromBuffer(buf); } else if (typeof buf === 'string') { var str = buf; this.fromString(str); } else if (typeof buf !== 'undefined') { var obj = buf; this.set(obj); } }; Script.prototype.set = function(obj) { this.chunks = obj.chunks || this.chunks; return this; }; Script.prototype.fromBuffer = function(buf) { this.chunks = []; var br = new BufferReader(buf); while (!br.eof()) { var opcodenum = br.readUInt8(); var len, buf; if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) { len = opcodenum; this.chunks.push({ buf: br.buffer(len), len: len, opcodenum: opcodenum }); } else if (opcodenum === Opcode.map.OP_PUSHDATA1) { len = br.readUInt8(); var buf = br.buffer(len); this.chunks.push({ buf: buf, len: len, opcodenum: opcodenum }); } else if (opcodenum === Opcode.map.OP_PUSHDATA2) { len = br.readUInt16LE(); buf = br.buffer(len); this.chunks.push({ buf: buf, len: len, opcodenum: opcodenum }); } else if (opcodenum === Opcode.map.OP_PUSHDATA4) { len = br.readUInt32LE(); buf = br.buffer(len); this.chunks.push({ buf: buf, len: len, opcodenum: opcodenum }); } else { this.chunks.push(opcodenum); } } return this; }; Script.prototype.toBuffer = function() { var bw = new BufferWriter(); for (var i = 0; i < this.chunks.length; i++) { var chunk = this.chunks[i]; if (typeof chunk === 'number') { var opcodenum = chunk; bw.writeUInt8(opcodenum); } else { var opcodenum = chunk.opcodenum; bw.writeUInt8(chunk.opcodenum); if (opcodenum < Opcode.map.OP_PUSHDATA1) { bw.write(chunk.buf); } else if (opcodenum === Opcode.map.OP_PUSHDATA1) { bw.writeUInt8(chunk.len); bw.write(chunk.buf); } else if (opcodenum === Opcode.map.OP_PUSHDATA2) { bw.writeUInt16LE(chunk.len); bw.write(chunk.buf); } else if (opcodenum === Opcode.map.OP_PUSHDATA4) { bw.writeUInt32LE(chunk.len); bw.write(chunk.buf); } } } 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;