sigops. redeem testing. input type testing.

This commit is contained in:
Christopher Jeffrey 2016-05-07 07:34:48 -07:00
parent 692f2c7cf0
commit db1f34c00d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 34 additions and 37 deletions

View File

@ -93,27 +93,43 @@ Input.prototype.getType = function getType() {
/** /**
* Get the redeem script. Will attempt to resolve nested * Get the redeem script. Will attempt to resolve nested
* redeem scripts if witnesspubkeyhash is behind a scripthash. * redeem scripts if witnessscripthash is behind a scripthash.
* @returns {Script?} Redeem script. * @returns {Script?} Redeem script.
*/ */
Input.prototype.getRedeem = function getRedeem() { Input.prototype.getRedeem = function getRedeem() {
var redeem = this.script; var redeem, prev;
var type;
if (this.isCoinbase()) if (this.isCoinbase())
return; return;
type = this.getType(); if (!this.coin) {
if (this.script.isScripthashInput()) {
redeem = this.script.getRedeem();
if (type === 'scripthash') { if (redeem && redeem.isWitnessScripthash())
redeem = redeem.getRedeem(); redeem = this.witness.getRedeem();
if (!redeem)
return; return redeem;
}
if (this.witness.isScripthashInput())
return this.witness.getRedeem();
return;
} }
if (redeem.isWitnessScripthash()) prev = this.coin.script;
redeem = this.witness.getRedeem();
if (prev.isScripthash()) {
prev = this.script.getRedeem();
redeem = prev;
}
if (prev && prev.isWitnessScripthash()) {
prev = this.witness.getRedeem();
redeem = prev;
}
return redeem; return redeem;
}; };

View File

@ -162,7 +162,7 @@ Witness.prototype.isMultisigInput = function isMultisigInput() {
*/ */
Witness.prototype.isScripthashInput = function isScripthashInput() { Witness.prototype.isScripthashInput = function isScripthashInput() {
return Script.isScripthashInput(this.items, true); return Script.isScripthashInput(this.items);
}; };
/** /**
@ -2241,15 +2241,6 @@ Script.prototype.getRedeem = function getRedeem() {
Script.getRedeem = function getRedeem(code) { Script.getRedeem = function getRedeem(code) {
var redeem = code[code.length - 1]; var redeem = code[code.length - 1];
if (typeof redeem === 'number') {
if (redeem > opcodes.OP_16)
return;
return new Script([redeem]);
}
if (Script.isBadPush(redeem))
return;
if (!Buffer.isBuffer(redeem)) if (!Buffer.isBuffer(redeem))
return; return;
@ -2389,7 +2380,7 @@ Script.getInputAddress = function getInputAddress(code, isWitness) {
if (Script.isMultisigInput(code, isWitness)) if (Script.isMultisigInput(code, isWitness))
return; return;
if (Script.isScripthashInput(code, isWitness)) { if (Script.isScripthashInput(code)) {
if (isWitness) { if (isWitness) {
return bcoin.address.compileData( return bcoin.address.compileData(
code[code.length - 1], code[code.length - 1],
@ -2455,7 +2446,7 @@ Script.getInputHash = function getInputHash(isWitness) {
if (Script.isMultisigInput(code, isWitness)) if (Script.isMultisigInput(code, isWitness))
return; return;
if (Script.isScripthashInput(code, isWitness)) { if (Script.isScripthashInput(code)) {
return isWitness return isWitness
? utils.sha256(code[code.length - 1]).toString('hex') ? utils.sha256(code[code.length - 1]).toString('hex')
: utils.ripesha(code[code.length - 1]).toString('hex') : utils.ripesha(code[code.length - 1]).toString('hex')
@ -2769,7 +2760,7 @@ Script.getInputType = function getInputType(code, isWitness) {
var type = (Script.isPubkeyInput(code) && 'pubkey') var type = (Script.isPubkeyInput(code) && 'pubkey')
|| (Script.isPubkeyhashInput(code) && 'pubkeyhash') || (Script.isPubkeyhashInput(code) && 'pubkeyhash')
|| (Script.isMultisigInput(code, isWitness) && 'multisig') || (Script.isMultisigInput(code, isWitness) && 'multisig')
|| (Script.isScripthashInput(code, isWitness) && 'scripthash') || (Script.isScripthashInput(code) && 'scripthash')
|| 'unknown'; || 'unknown';
if (isWitness) { if (isWitness) {
@ -2929,7 +2920,7 @@ Script.isMultisigInput = function isMultisigInput(code, isWitness) {
// We need to rule out scripthash // We need to rule out scripthash
// because it may look like multisig. // because it may look like multisig.
if (Script.isScripthashInput(code, isWitness)) if (Script.isScripthashInput(code))
return false; return false;
if (code.length < 3) if (code.length < 3)
@ -2961,22 +2952,12 @@ Script.prototype.isScripthashInput = function isScripthashInput() {
return Script.isScripthashInput(this.code); return Script.isScripthashInput(this.code);
}; };
Script.isScripthashInput = function isScripthashInput(code, isWitness) { Script.isScripthashInput = function isScripthashInput(code) {
var raw; var raw;
// Grab the raw redeem script. // Grab the raw redeem script.
raw = code[code.length - 1]; raw = code[code.length - 1];
// Need at least one data element with
// the redeem script. NOTE: NOT THE CASE FOR SEGWIT!
if (isWitness) {
if (code.length < 1)
return false;
} else {
if (code.length < 2)
return false;
}
// Last data element should be an array // Last data element should be an array
// for the redeem script. // for the redeem script.
if (!Buffer.isBuffer(raw)) if (!Buffer.isBuffer(raw))
@ -3552,7 +3533,7 @@ Script.prototype.getScripthashSigops = function getScripthashSigops(input) {
return 0; return 0;
} }
if (typeof op === 'number') if (!Buffer.isBuffer(op))
return 0; return 0;
redeem = new Script(op); redeem = new Script(op);
@ -3617,7 +3598,7 @@ Script.getWitnessSigops = function getWitnessSigops(input, output, witness, flag
// does not check the return value of GetOp. // does not check the return value of GetOp.
if (output.isScripthash() && input.isPushOnly()) { if (output.isScripthash() && input.isPushOnly()) {
redeem = input.getRedeem(); redeem = input.getRedeem();
if (redeem.isWitnessProgram()) if (redeem && redeem.isWitnessProgram())
return Script.witnessSigops(redeem.getWitnessProgram(), witness, flags); return Script.witnessSigops(redeem.getWitnessProgram(), witness, flags);
} }