refactor script

This commit is contained in:
Manuel Araoz 2014-12-11 14:54:47 -03:00
parent 81f1469f9a
commit 53f6a31e73
1 changed files with 19 additions and 21 deletions

View File

@ -202,13 +202,11 @@ Script.prototype.toString = function() {
* @returns true if this is a pay to pubkey hash output script
*/
Script.prototype.isPublicKeyHashOut = function() {
return !!(this.chunks[0] && this.chunks[0].opcodenum === Opcode.OP_DUP &&
this.chunks[1] &&
return !!(this.chunks.length === 5 &&
this.chunks[0].opcodenum === Opcode.OP_DUP &&
this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
this.chunks[2].buf &&
this.chunks[3] &&
this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
this.chunks[4] &&
this.chunks[4].opcodenum === Opcode.OP_CHECKSIG);
};
@ -216,12 +214,11 @@ Script.prototype.isPublicKeyHashOut = function() {
* @returns true if this is a pay to public key hash input script
*/
Script.prototype.isPublicKeyHashIn = function() {
return !!(this.chunks.length === 2 &&
return this.chunks.length === 2 &&
this.chunks[0].buf &&
this.chunks[0].buf.length >= 0x47 &&
this.chunks[0].buf.length <= 0x49 &&
this.chunks[1].buf &&
PublicKey.isValid(this.chunks[1].buf));
PublicKey.isValid(this.chunks[1].buf);
};
/**
@ -231,7 +228,7 @@ Script.prototype.isPublicKeyOut = function() {
return this.chunks.length === 2 &&
bufferUtil.isBuffer(this.chunks[0].buf) &&
PublicKey.isValid(this.chunks[0].buf) &&
this.chunks[1] === Opcode('OP_CHECKSIG').toNumber();
this.chunks[1].opcodenum === Opcode('OP_CHECKSIG').toNumber();
};
/**
@ -249,10 +246,10 @@ Script.prototype.isPublicKeyIn = function() {
*/
Script.prototype.isScriptHashOut = function() {
return this.chunks.length === 3 &&
this.chunks[0] === Opcode('OP_HASH160').toNumber() &&
this.chunks[0].opcodenum === Opcode('OP_HASH160').toNumber() &&
this.chunks[1].buf &&
this.chunks[1].buf.length === 20 &&
this.chunks[2] === Opcode('OP_EQUAL').toNumber();
this.chunks[2].opcodenum === Opcode('OP_EQUAL').toNumber();
};
/**
@ -272,9 +269,7 @@ Script.prototype.isScriptHashIn = function() {
return false;
}
var redeemScript = new Script(scriptBuf);
console.log(redeemScript.toString());
var type = redeemScript.classify();
console.log('type='+type);
return type !== Script.types.UNKNOWN;
};
@ -288,15 +283,16 @@ Script.prototype.isMultisigOut = function() {
return obj.buf && bufferUtil.isBuffer(obj.buf);
}) &&
Opcode.isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) &&
this.chunks[this.chunks.length - 1] === Opcode.map.OP_CHECKMULTISIG);
this.chunks[this.chunks.length - 1].opcodenum === Opcode.map.OP_CHECKMULTISIG);
};
/**
* @returns true if this is a mutlsig input script
* @returns true if this is a multisig input script
*/
Script.prototype.isMultisigIn = function() {
return this.chunks[0] === 0 &&
return this.chunks.length >= 2 &&
this.chunks[0].opcodenum === 0 &&
this.chunks.slice(1, this.chunks.length).every(function(obj) {
return obj.buf &&
bufferUtil.isBuffer(obj.buf) &&
@ -308,12 +304,13 @@ Script.prototype.isMultisigIn = function() {
* @returns true if this is an OP_RETURN data script
*/
Script.prototype.isDataOut = function() {
return (this.chunks[0].opcodenum === Opcode('OP_RETURN').toNumber() &&
return this.chunks.length >= 1 &&
this.chunks[0].opcodenum === Opcode('OP_RETURN').toNumber() &&
(this.chunks.length === 1 ||
(this.chunks.length === 2 &&
this.chunks[1].buf &&
this.chunks[1].buf.length <= 40 &&
this.chunks[1].length === this.chunks.len)));
this.chunks[1].length === this.chunks.len));
};
/**
@ -322,8 +319,7 @@ Script.prototype.isDataOut = function() {
*/
Script.prototype.isPushOnly = function() {
return _.every(this.chunks, function(chunk) {
var opcodenum = chunk.opcodenum;
return !_.isUndefined(opcodenum) || chunk <= Opcode.map.OP_16;
return chunk.opcodenum <= Opcode.map.OP_16;
});
};
@ -431,7 +427,9 @@ Script.prototype._addOpcode = function(opcode, prepend) {
} else {
op = Opcode(opcode).toNumber();
}
this._insertAtPosition(op, prepend);
this._insertAtPosition({
opcodenum: op
}, prepend);
return this;
};
@ -462,7 +460,7 @@ Script.prototype._addBuffer = function(buf, prepend) {
Script.prototype.removeCodeseparators = function() {
var chunks = [];
for (var i = 0; i < this.chunks.length; i++) {
if (this.chunks[i] !== Opcode.OP_CODESEPARATOR) {
if (this.chunks[i].opcodenum !== Opcode.OP_CODESEPARATOR) {
chunks.push(this.chunks[i]);
}
}