mtx: refactor.

This commit is contained in:
Christopher Jeffrey 2016-08-21 11:04:56 -07:00
parent d17ba07475
commit d26db89a21
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -700,10 +700,23 @@ MTX.prototype.signature = function signature(index, prev, key, type, version) {
*/ */
MTX.prototype.isSigned = function isSigned() { MTX.prototype.isSigned = function isSigned() {
var i, input, prev, vector, m, len, j; for (var i = 0; i < this.inputs.length; i++) {
if (!this.isInputSigned(i))
return false;
}
};
for (i = 0; i < this.inputs.length; i++) { /**
input = this.inputs[i]; * Test whether an input is fully-signed.
* @param {Number} index
* @returns {Boolean}
*/
MTX.prototype.isInputSigned = function isInputSigned(index) {
var input = this.inputs[index];
var prev, vector, m, redeem, j, result;
assert(input, 'Input does not exist.');
// We can't check for signatures unless // We can't check for signatures unless
// we have the previous output. // we have the previous output.
@ -715,7 +728,7 @@ MTX.prototype.isSigned = function isSigned() {
// Script length, needed for multisig // Script length, needed for multisig
vector = input.script; vector = input.script;
len = vector.length; redeem = false;
// We need to grab the redeem script when // We need to grab the redeem script when
// signing p2sh transactions. // signing p2sh transactions.
@ -723,7 +736,7 @@ MTX.prototype.isSigned = function isSigned() {
prev = input.script.getRedeem(); prev = input.script.getRedeem();
if (!prev) if (!prev)
return false; return false;
len = vector.length - 1; redeem = true;
} }
// If the output script is a witness program, // If the output script is a witness program,
@ -734,23 +747,43 @@ MTX.prototype.isSigned = function isSigned() {
if (!prev) if (!prev)
return false; return false;
vector = input.witness; vector = input.witness;
len = vector.length - 1; redeem = true;
} else if (prev.isWitnessPubkeyhash()) { } else if (prev.isWitnessPubkeyhash()) {
prev = Script.fromPubkeyhash(prev.get(1)); prev = Script.fromPubkeyhash(prev.get(1));
vector = input.witness; vector = input.witness;
len = vector.length; redeem = false;
} }
if (redeem) {
redeem = vector.pop();
result = this.isVectorSigned(prev);
vector.push(redeem);
return result;
}
return this.isVectorSigned(prev, vector);
};
/**
* Test whether a vector is fully-signed.
* @param {Script} prev
* @param {Script|Witness} vector
* @returns {Boolean}
*/
MTX.prototype.isVectorSigned = function isVectorSigned(prev, vector) {
var m;
if (prev.isPubkey()) { if (prev.isPubkey()) {
if (!Script.isSignature(vector.get(0))) if (!Script.isSignature(vector.get(0)))
return false; return false;
continue; return true;
} }
if (prev.isPubkeyhash()) { if (prev.isPubkeyhash()) {
if (!Script.isSignature(vector.get(0))) if (!Script.isSignature(vector.get(0)))
return false; return false;
continue; return true;
} }
if (prev.isMultisig()) { if (prev.isMultisig()) {
@ -758,23 +791,20 @@ MTX.prototype.isSigned = function isSigned() {
m = prev.getSmall(0); m = prev.getSmall(0);
// Ensure all members are signatures. // Ensure all members are signatures.
for (j = 1; j < len; j++) { for (j = 1; j < vector.length; j++) {
if (!Script.isSignature(vector.get(j))) if (!Script.isSignature(vector.get(j)))
return false; return false;
} }
// Ensure we have the correct number // Ensure we have the correct number
// of required signatures. // of required signatures.
if (len - 1 !== m) if (vector.length - 1 !== m)
return false; return false;
continue;
}
return false;
}
return true; return true;
}
return false;
}; };
/** /**
@ -879,7 +909,7 @@ MTX.prototype.signAsync = function signAsync(ring, type, callback) {
*/ */
MTX.prototype.isScripted = function isScripted() { MTX.prototype.isScripted = function isScripted() {
var i, input; var i;
if (this.outputs.length === 0) if (this.outputs.length === 0)
return false; return false;
@ -888,13 +918,28 @@ MTX.prototype.isScripted = function isScripted() {
return false; return false;
for (i = 0; i < this.inputs.length; i++) { for (i = 0; i < this.inputs.length; i++) {
input = this.inputs[i]; if (!this.isInputScripted(i))
return false;
}
return true;
};
/**
* Test whether the input at least
* has all script templates built.
* @returns {Boolean}
*/
MTX.prototype.isInputScripted = function isInputScripted(index) {
var input = this.inputs[index];
assert(input, 'Input does not exist.');
if (input.script.raw.length === 0 if (input.script.raw.length === 0
&& input.witness.items.length === 0) { && input.witness.items.length === 0) {
return false; return false;
} }
}
return true; return true;
}; };