From 6b54897b57f9cc973fa191f480fda0610022a745 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 28 Feb 2016 20:26:10 -0800 Subject: [PATCH] refactor. --- lib/bcoin/block.js | 1 - lib/bcoin/script.js | 71 +++++++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 118581e3..b34ac88f 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -161,7 +161,6 @@ Block.prototype.__defineGetter__('commitmentHash', function() { coinbase = this.txs[0]; - // Find the fucking commitment for segregated shitness for (i = 0; i < coinbase.outputs.length; i++) { commitment = coinbase.outputs[i].script; if (bcoin.script.isCommitment(commitment)) { diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index ee148c59..6986267e 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -15,20 +15,20 @@ var script = exports; * Script */ -script.decode = function decode(s) { - if (!s) - return []; - +script.decode = function decode(buf) { var opcodes = []; var i = 0; var b, opcode, len; - while (i < s.length) { - b = s[i++]; + if (!buf) + return []; + + while (i < buf.length) { + b = buf[i++]; // Next `b` bytes should be pushed to stack if (b >= 0x01 && b <= 0x4b) { - opcodes.push(s.slice(i, i + b)); + opcodes.push(buf.slice(i, i + b)); i += b; utils.hidden(opcodes[opcodes.length - 1], 'pushdata', { opcode: null, @@ -55,33 +55,33 @@ script.decode = function decode(s) { opcode = constants.opcodesByVal[b]; - if (i >= s.length) { + if (i >= buf.length) { opcodes.push(opcode || b); continue; } if (opcode === 'pushdata1') { - len = s[i]; + len = buf[i]; i += 1; - opcodes.push(s.slice(i, i + len)); + opcodes.push(buf.slice(i, i + len)); i += len; utils.hidden(opcodes[opcodes.length - 1], 'pushdata', { opcode: opcode, len: len }); } else if (opcode === 'pushdata2') { - len = utils.readU16(s, i); + len = utils.readU16(buf, i); i += 2; - opcodes.push(s.slice(i, i + len)); + opcodes.push(buf.slice(i, i + len)); i += len; utils.hidden(opcodes[opcodes.length - 1], 'pushdata', { opcode: opcode, len: len }); } else if (opcode === 'pushdata4') { - len = utils.readU32(s, i); + len = utils.readU32(buf, i); i += 4; - opcodes.push(s.slice(i, i + len)); + opcodes.push(buf.slice(i, i + len)); i += len; utils.hidden(opcodes[opcodes.length - 1], 'pushdata', { opcode: opcode, @@ -92,18 +92,12 @@ script.decode = function decode(s) { } } - utils.hidden(opcodes, '_raw', s); + utils.hidden(opcodes, '_raw', buf); return opcodes; }; script.encode = function encode(s) { - if (!s) - return new Buffer([]); - - if (s._raw) - return s._raw; - var opcodes = constants.opcodes; var i = 0; var instr; @@ -111,6 +105,12 @@ script.encode = function encode(s) { var off = 0; var res; + if (!s) + return new Buffer([]); + + if (s._raw) + return s._raw; + for (i = 0; i < s.length; i++) { instr = s[i]; @@ -195,6 +195,8 @@ script.encode = function encode(s) { continue; } + assert(opcodes[instr] != null || typeof instr === 'number'); + res[off++] = opcodes[instr] || instr; } @@ -206,7 +208,7 @@ script.encode = function encode(s) { // Witnesses aren't scripts, but we still // want to convert [0] to OP_0, [0xff] to 1negate, etc. script.decodeWitness = function decodeWitness(witness) { - var script = []; + var s = []; var chunk, i; for (i = 0; i < witness.length; i++) { @@ -221,18 +223,18 @@ script.decodeWitness = function decodeWitness(witness) { op = '1negate'; } - script.push(op); + s.push(op); } - return script; + return s; }; -script.encodeWitness = function encodeWitness(script) { +script.encodeWitness = function encodeWitness(s) { var witness = []; var chunk, i; - for (i = 0; i < script.length; i++) { - chunk = script[i]; + for (i = 0; i < s.length; i++) { + chunk = s[i]; if (chunk === 0) chunk = new Buffer([]); @@ -429,26 +431,13 @@ script.verifyProgram = function verifyProgram(witness, output, tx, i, flags) { if (stack.length !== 2) return false; - if (!Buffer.isBuffer(stack[0]) || !Buffer.isBuffer(stack[1])) - return false; - - // Why the fuck are these allowed to be so big? - if (stack[0].length > 520 || stack[1].length > 520) - return false; - - redeem = ['dup', 'hash160', program.data, 'equalverify', 'checksig']; + redeem = script.createPubkeyhash(program.data); } else if (program.type === 'witnessscripthash') { if (stack.length === 0) return false; witnessScript = stack.pop(); - if (!Buffer.isBuffer(witnessScript)) - return false; - - if (witnessScript.length > constants.script.maxSize) - return false; - if (!utils.isEqual(utils.sha256(witnessScript), program.data)) return false;