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];
// 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)) {

View File

@ -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;