refactor.

This commit is contained in:
Christopher Jeffrey 2016-02-28 20:26:10 -08:00
parent 15c63eb14a
commit 6b54897b57
2 changed files with 30 additions and 42 deletions

View File

@ -161,7 +161,6 @@ Block.prototype.__defineGetter__('commitmentHash', function() {
coinbase = this.txs[0]; coinbase = this.txs[0];
// Find the fucking commitment for segregated shitness
for (i = 0; i < coinbase.outputs.length; i++) { for (i = 0; i < coinbase.outputs.length; i++) {
commitment = coinbase.outputs[i].script; commitment = coinbase.outputs[i].script;
if (bcoin.script.isCommitment(commitment)) { if (bcoin.script.isCommitment(commitment)) {

View File

@ -15,20 +15,20 @@ var script = exports;
* Script * Script
*/ */
script.decode = function decode(s) { script.decode = function decode(buf) {
if (!s)
return [];
var opcodes = []; var opcodes = [];
var i = 0; var i = 0;
var b, opcode, len; var b, opcode, len;
while (i < s.length) { if (!buf)
b = s[i++]; return [];
while (i < buf.length) {
b = buf[i++];
// Next `b` bytes should be pushed to stack // Next `b` bytes should be pushed to stack
if (b >= 0x01 && b <= 0x4b) { if (b >= 0x01 && b <= 0x4b) {
opcodes.push(s.slice(i, i + b)); opcodes.push(buf.slice(i, i + b));
i += b; i += b;
utils.hidden(opcodes[opcodes.length - 1], 'pushdata', { utils.hidden(opcodes[opcodes.length - 1], 'pushdata', {
opcode: null, opcode: null,
@ -55,33 +55,33 @@ script.decode = function decode(s) {
opcode = constants.opcodesByVal[b]; opcode = constants.opcodesByVal[b];
if (i >= s.length) { if (i >= buf.length) {
opcodes.push(opcode || b); opcodes.push(opcode || b);
continue; continue;
} }
if (opcode === 'pushdata1') { if (opcode === 'pushdata1') {
len = s[i]; len = buf[i];
i += 1; i += 1;
opcodes.push(s.slice(i, i + len)); opcodes.push(buf.slice(i, i + len));
i += len; i += len;
utils.hidden(opcodes[opcodes.length - 1], 'pushdata', { utils.hidden(opcodes[opcodes.length - 1], 'pushdata', {
opcode: opcode, opcode: opcode,
len: len len: len
}); });
} else if (opcode === 'pushdata2') { } else if (opcode === 'pushdata2') {
len = utils.readU16(s, i); len = utils.readU16(buf, i);
i += 2; i += 2;
opcodes.push(s.slice(i, i + len)); opcodes.push(buf.slice(i, i + len));
i += len; i += len;
utils.hidden(opcodes[opcodes.length - 1], 'pushdata', { utils.hidden(opcodes[opcodes.length - 1], 'pushdata', {
opcode: opcode, opcode: opcode,
len: len len: len
}); });
} else if (opcode === 'pushdata4') { } else if (opcode === 'pushdata4') {
len = utils.readU32(s, i); len = utils.readU32(buf, i);
i += 4; i += 4;
opcodes.push(s.slice(i, i + len)); opcodes.push(buf.slice(i, i + len));
i += len; i += len;
utils.hidden(opcodes[opcodes.length - 1], 'pushdata', { utils.hidden(opcodes[opcodes.length - 1], 'pushdata', {
opcode: opcode, opcode: opcode,
@ -92,18 +92,12 @@ script.decode = function decode(s) {
} }
} }
utils.hidden(opcodes, '_raw', s); utils.hidden(opcodes, '_raw', buf);
return opcodes; return opcodes;
}; };
script.encode = function encode(s) { script.encode = function encode(s) {
if (!s)
return new Buffer([]);
if (s._raw)
return s._raw;
var opcodes = constants.opcodes; var opcodes = constants.opcodes;
var i = 0; var i = 0;
var instr; var instr;
@ -111,6 +105,12 @@ script.encode = function encode(s) {
var off = 0; var off = 0;
var res; var res;
if (!s)
return new Buffer([]);
if (s._raw)
return s._raw;
for (i = 0; i < s.length; i++) { for (i = 0; i < s.length; i++) {
instr = s[i]; instr = s[i];
@ -195,6 +195,8 @@ script.encode = function encode(s) {
continue; continue;
} }
assert(opcodes[instr] != null || typeof instr === 'number');
res[off++] = opcodes[instr] || instr; res[off++] = opcodes[instr] || instr;
} }
@ -206,7 +208,7 @@ script.encode = function encode(s) {
// Witnesses aren't scripts, but we still // Witnesses aren't scripts, but we still
// want to convert [0] to OP_0, [0xff] to 1negate, etc. // want to convert [0] to OP_0, [0xff] to 1negate, etc.
script.decodeWitness = function decodeWitness(witness) { script.decodeWitness = function decodeWitness(witness) {
var script = []; var s = [];
var chunk, i; var chunk, i;
for (i = 0; i < witness.length; i++) { for (i = 0; i < witness.length; i++) {
@ -221,18 +223,18 @@ script.decodeWitness = function decodeWitness(witness) {
op = '1negate'; 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 witness = [];
var chunk, i; var chunk, i;
for (i = 0; i < script.length; i++) { for (i = 0; i < s.length; i++) {
chunk = script[i]; chunk = s[i];
if (chunk === 0) if (chunk === 0)
chunk = new Buffer([]); chunk = new Buffer([]);
@ -429,26 +431,13 @@ script.verifyProgram = function verifyProgram(witness, output, tx, i, flags) {
if (stack.length !== 2) if (stack.length !== 2)
return false; return false;
if (!Buffer.isBuffer(stack[0]) || !Buffer.isBuffer(stack[1])) redeem = script.createPubkeyhash(program.data);
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'];
} else if (program.type === 'witnessscripthash') { } else if (program.type === 'witnessscripthash') {
if (stack.length === 0) if (stack.length === 0)
return false; return false;
witnessScript = stack.pop(); 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)) if (!utils.isEqual(utils.sha256(witnessScript), program.data))
return false; return false;