more errors for scripting system.
This commit is contained in:
parent
ce0c6f4fc7
commit
50ab39aafb
@ -599,7 +599,7 @@ Stack.prototype.toalt = function toalt() {
|
|||||||
|
|
||||||
Stack.prototype.fromalt = function fromalt() {
|
Stack.prototype.fromalt = function fromalt() {
|
||||||
if (this.alt.length === 0)
|
if (this.alt.length === 0)
|
||||||
throw new ScriptError('INVALID_STACK_OPERATION', opcodes.OP_FROMALTSTACK);
|
throw new ScriptError('INVALID_ALTSTACK_OPERATION', opcodes.OP_FROMALTSTACK);
|
||||||
|
|
||||||
this.push(this.alt.pop());
|
this.push(this.alt.pop());
|
||||||
};
|
};
|
||||||
@ -1009,30 +1009,6 @@ Script.prototype.getSubscript = function getSubscript(lastSep) {
|
|||||||
return new Script(code);
|
return new Script(code);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the script and return false on script execution errors.
|
|
||||||
* @param {Stack} stack - Script execution stack.
|
|
||||||
* @param {Number?} flags - Script standard flags.
|
|
||||||
* @param {TX?} tx - Transaction being verified.
|
|
||||||
* @param {Number?} index - Index of input being verified.
|
|
||||||
* @param {Number?} version - Signature hash version (0=legacy, 1=segwit).
|
|
||||||
* @returns {Boolean} Whether the execution was successful.
|
|
||||||
*/
|
|
||||||
|
|
||||||
Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
|
||||||
try {
|
|
||||||
return this.interpret(stack, flags, tx, index, version);
|
|
||||||
} catch (e) {
|
|
||||||
if (e.type === 'ScriptError') {
|
|
||||||
bcoin.debug('Script error: %s.', e.message);
|
|
||||||
} else {
|
|
||||||
bcoin.debug('Script interpreter threw:');
|
|
||||||
bcoin.debug(e.stack + '');
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute and interpret the script.
|
* Execute and interpret the script.
|
||||||
* @param {Stack} stack - Script execution stack.
|
* @param {Stack} stack - Script execution stack.
|
||||||
@ -1044,7 +1020,7 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
|||||||
* @returns {Boolean} Whether the execution was successful.
|
* @returns {Boolean} Whether the execution was successful.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Script.prototype.interpret = function interpret(stack, flags, tx, index, version) {
|
Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||||
var ip = 0;
|
var ip = 0;
|
||||||
var lastSep = -1;
|
var lastSep = -1;
|
||||||
var opCount = 0;
|
var opCount = 0;
|
||||||
@ -1064,7 +1040,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
stack.negate = 0;
|
stack.negate = 0;
|
||||||
|
|
||||||
if (this.getSize() > constants.script.MAX_SIZE)
|
if (this.getSize() > constants.script.MAX_SIZE)
|
||||||
throw new ScriptError('SCRIPT_SIZE', null, -1);
|
throw new ScriptError('SCRIPT_SIZE');
|
||||||
|
|
||||||
for (ip = 0; ip < this.code.length; ip++) {
|
for (ip = 0; ip < this.code.length; ip++) {
|
||||||
op = this.code[ip];
|
op = this.code[ip];
|
||||||
@ -1200,7 +1176,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!tx)
|
if (!tx)
|
||||||
throw new ScriptError('NO_TX', op, ip);
|
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
|
||||||
|
|
||||||
if (stack.length === 0)
|
if (stack.length === 0)
|
||||||
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
|
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
|
||||||
@ -1226,7 +1202,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!tx)
|
if (!tx)
|
||||||
throw new ScriptError('NO_TX', op, ip);
|
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
|
||||||
|
|
||||||
if (stack.length === 0)
|
if (stack.length === 0)
|
||||||
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
|
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
|
||||||
@ -1355,7 +1331,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
res = utils.equals(stack.pop(), stack.pop());
|
res = utils.equals(stack.pop(), stack.pop());
|
||||||
if (op === opcodes.OP_EQUALVERIFY) {
|
if (op === opcodes.OP_EQUALVERIFY) {
|
||||||
if (!res)
|
if (!res)
|
||||||
throw new ScriptError('VERIFY', op, ip);
|
throw new ScriptError('EQUALVERIFY', op, ip);
|
||||||
} else {
|
} else {
|
||||||
stack.push(res ? STACK_TRUE : STACK_FALSE);
|
stack.push(res ? STACK_TRUE : STACK_FALSE);
|
||||||
}
|
}
|
||||||
@ -1466,12 +1442,12 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
break;
|
break;
|
||||||
case opcodes.OP_LSHIFT:
|
case opcodes.OP_LSHIFT:
|
||||||
if (n2.cmpn(0) < 0 || n2.cmpn(2048) > 0)
|
if (n2.cmpn(0) < 0 || n2.cmpn(2048) > 0)
|
||||||
throw new ScriptError('BAD_SHIFT', op, ip);
|
throw new ScriptError('UNKNOWN_ERROR', 'Bad shift.');
|
||||||
n = n1.ushln(n2.toNumber());
|
n = n1.ushln(n2.toNumber());
|
||||||
break;
|
break;
|
||||||
case opcodes.OP_RSHIFT:
|
case opcodes.OP_RSHIFT:
|
||||||
if (n2.cmpn(0) < 0 || n2.cmpn(2048) > 0)
|
if (n2.cmpn(0) < 0 || n2.cmpn(2048) > 0)
|
||||||
throw new ScriptError('BAD_SHIFT', op, ip);
|
throw new ScriptError('UNKNOWN_ERROR', 'Bad shift.');
|
||||||
n = n1.ushrn(n2.toNumber());
|
n = n1.ushrn(n2.toNumber());
|
||||||
break;
|
break;
|
||||||
case opcodes.OP_BOOLAND:
|
case opcodes.OP_BOOLAND:
|
||||||
@ -1570,7 +1546,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
case opcodes.OP_CHECKSIGVERIFY:
|
case opcodes.OP_CHECKSIGVERIFY:
|
||||||
case opcodes.OP_CHECKSIG: {
|
case opcodes.OP_CHECKSIG: {
|
||||||
if (!tx)
|
if (!tx)
|
||||||
throw new ScriptError('NO_TX', op, ip);
|
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
|
||||||
|
|
||||||
if (stack.length < 2)
|
if (stack.length < 2)
|
||||||
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
|
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
|
||||||
@ -1582,11 +1558,8 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
if (version === 0)
|
if (version === 0)
|
||||||
subscript.removeData(sig);
|
subscript.removeData(sig);
|
||||||
|
|
||||||
if (!Script.isValidSignature(sig, flags))
|
Script.validateSignature(sig, flags);
|
||||||
throw new ScriptError('SIG_DER_OR_HASHTYPE', op, ip);
|
Script.validateKey(key, flags);
|
||||||
|
|
||||||
if (!Script.isValidKey(key, flags))
|
|
||||||
throw new ScriptError('PUBKEYTYPE', op, ip);
|
|
||||||
|
|
||||||
type = sig[sig.length - 1];
|
type = sig[sig.length - 1];
|
||||||
|
|
||||||
@ -1605,7 +1578,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
case opcodes.OP_CHECKMULTISIGVERIFY:
|
case opcodes.OP_CHECKMULTISIGVERIFY:
|
||||||
case opcodes.OP_CHECKMULTISIG: {
|
case opcodes.OP_CHECKMULTISIG: {
|
||||||
if (!tx)
|
if (!tx)
|
||||||
throw new ScriptError('NO_TX', op, ip);
|
throw new ScriptError('UNKNOWN_ERROR', 'No TX passed in.');
|
||||||
|
|
||||||
i = 1;
|
i = 1;
|
||||||
if (stack.length < i)
|
if (stack.length < i)
|
||||||
@ -1653,11 +1626,8 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
sig = stack.top(-isig);
|
sig = stack.top(-isig);
|
||||||
key = stack.top(-ikey);
|
key = stack.top(-ikey);
|
||||||
|
|
||||||
if (!Script.isValidSignature(sig, flags))
|
Script.validateSignature(sig, flags);
|
||||||
throw new ScriptError('SIG_DER_OR_HASHTYPE', op, ip);
|
Script.validateKey(key, flags);
|
||||||
|
|
||||||
if (!Script.isValidKey(key, flags))
|
|
||||||
throw new ScriptError('PUBKEYTYPE', op, ip);
|
|
||||||
|
|
||||||
type = sig[sig.length - 1];
|
type = sig[sig.length - 1];
|
||||||
hash = tx.signatureHash(index, subscript, type, version);
|
hash = tx.signatureHash(index, subscript, type, version);
|
||||||
@ -1682,7 +1652,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
|
|
||||||
if (flags & constants.flags.VERIFY_NULLDUMMY) {
|
if (flags & constants.flags.VERIFY_NULLDUMMY) {
|
||||||
if (!Script.isDummy(stack.top(-1)))
|
if (!Script.isDummy(stack.top(-1)))
|
||||||
throw new ScriptError('VERIFY_NULLDUMMY', op, ip);
|
throw new ScriptError('SIG_NULLDUMMY', op, ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
stack.pop();
|
stack.pop();
|
||||||
@ -1713,7 +1683,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
v2 = Script.num(stack.pop(), flags).toNumber(); // begin
|
v2 = Script.num(stack.pop(), flags).toNumber(); // begin
|
||||||
v1 = stack.pop(); // string
|
v1 = stack.pop(); // string
|
||||||
if (v2 < 0 || v3 < v2)
|
if (v2 < 0 || v3 < v2)
|
||||||
throw new ScriptError('STRING_OUT_OF_RANGE', op, ip);
|
throw new ScriptError('UNKNOWN_ERROR', 'String out of range.');
|
||||||
if (v2 > v1.length)
|
if (v2 > v1.length)
|
||||||
v2 = v1.length;
|
v2 = v1.length;
|
||||||
if (v3 > v1.length)
|
if (v3 > v1.length)
|
||||||
@ -1728,7 +1698,7 @@ Script.prototype.interpret = function interpret(stack, flags, tx, index, version
|
|||||||
v2 = Script.num(stack.pop(), flags).toNumber(); // size
|
v2 = Script.num(stack.pop(), flags).toNumber(); // size
|
||||||
v1 = stack.pop(); // string
|
v1 = stack.pop(); // string
|
||||||
if (v2 < 0)
|
if (v2 < 0)
|
||||||
throw new ScriptError('STRING_OUT_OF_RANGE', op, ip);
|
throw new ScriptError('UNKNOWN_ERROR', 'String size out of range.');
|
||||||
if (v2 > v1.length)
|
if (v2 > v1.length)
|
||||||
v2 = v1.length;
|
v2 = v1.length;
|
||||||
if (op === opcodes.OP_LEFT)
|
if (op === opcodes.OP_LEFT)
|
||||||
@ -1905,7 +1875,7 @@ Script.num = function num(value, flags, size) {
|
|||||||
size = 4;
|
size = 4;
|
||||||
|
|
||||||
if (value.length > size)
|
if (value.length > size)
|
||||||
throw new ScriptError('Script number overflow.');
|
throw new ScriptError('UNKNOWN_ERROR', 'Script number overflow.');
|
||||||
|
|
||||||
if ((flags & constants.flags.VERIFY_MINIMALDATA) && value.length > 0) {
|
if ((flags & constants.flags.VERIFY_MINIMALDATA) && value.length > 0) {
|
||||||
// If the low bits on the last byte are unset,
|
// If the low bits on the last byte are unset,
|
||||||
@ -1917,8 +1887,11 @@ Script.num = function num(value, flags, size) {
|
|||||||
// to avoid negative zero (also avoids positive
|
// to avoid negative zero (also avoids positive
|
||||||
// zero).
|
// zero).
|
||||||
if (!(value[value.length - 1] & 0x7f)) {
|
if (!(value[value.length - 1] & 0x7f)) {
|
||||||
if (value.length === 1 || !(value[value.length - 2] & 0x80))
|
if (value.length === 1 || !(value[value.length - 2] & 0x80)) {
|
||||||
throw new ScriptError('Non-minimally encoded Script number.');
|
throw new ScriptError(
|
||||||
|
'UNKNOWN_ERROR',
|
||||||
|
'Non-minimally encoded Script number.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3232,20 +3205,19 @@ Script.isZero = function isZero(op) {
|
|||||||
* @param {Buffer} key
|
* @param {Buffer} key
|
||||||
* @param {VerifyFlags?} flags
|
* @param {VerifyFlags?} flags
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
|
* @throws {ScriptError}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Script.isValidKey = function isValidKey(key, flags) {
|
Script.validateKey = function validateKey(key, flags) {
|
||||||
if (flags == null)
|
if (flags == null)
|
||||||
flags = constants.flags.STANDARD_VERIFY_FLAGS;
|
flags = constants.flags.STANDARD_VERIFY_FLAGS;
|
||||||
|
|
||||||
if (!Script.isPush(key))
|
if (!Script.isPush(key))
|
||||||
return false;
|
throw new ScriptError('BAD_OPCODE');
|
||||||
|
|
||||||
if (flags & constants.flags.VERIFY_STRICTENC) {
|
if (flags & constants.flags.VERIFY_STRICTENC) {
|
||||||
if (!Script.isKeyEncoding(key)) {
|
if (!Script.isKeyEncoding(key))
|
||||||
bcoin.debug('Script failed key encoding test.');
|
throw new ScriptError('PUBKEYTYPE');
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -3286,14 +3258,15 @@ Script.isKeyEncoding = function isKeyEncoding(key) {
|
|||||||
* @param {Buffer} sig
|
* @param {Buffer} sig
|
||||||
* @param {VerifyFlags?} flags
|
* @param {VerifyFlags?} flags
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
|
* @throws {ScriptError}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Script.isValidSignature = function isValidSignature(sig, flags) {
|
Script.validateSignature = function validateSignature(sig, flags) {
|
||||||
if (flags == null)
|
if (flags == null)
|
||||||
flags = constants.flags.STANDARD_VERIFY_FLAGS;
|
flags = constants.flags.STANDARD_VERIFY_FLAGS;
|
||||||
|
|
||||||
if (!Script.isPush(sig))
|
if (!Script.isPush(sig))
|
||||||
return false;
|
throw new ScriptError('BAD_OPCODE');
|
||||||
|
|
||||||
// Allow empty sigs
|
// Allow empty sigs
|
||||||
if (sig.length === 0)
|
if (sig.length === 0)
|
||||||
@ -3302,24 +3275,18 @@ Script.isValidSignature = function isValidSignature(sig, flags) {
|
|||||||
if ((flags & constants.flags.VERIFY_DERSIG)
|
if ((flags & constants.flags.VERIFY_DERSIG)
|
||||||
|| (flags & constants.flags.VERIFY_LOW_S)
|
|| (flags & constants.flags.VERIFY_LOW_S)
|
||||||
|| (flags & constants.flags.VERIFY_STRICTENC)) {
|
|| (flags & constants.flags.VERIFY_STRICTENC)) {
|
||||||
if (!Script.isSignatureEncoding(sig)) {
|
if (!Script.isSignatureEncoding(sig))
|
||||||
bcoin.debug('Script does not have a proper signature encoding.');
|
throw new ScriptError('SIG_DER');
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & constants.flags.VERIFY_LOW_S) {
|
if (flags & constants.flags.VERIFY_LOW_S) {
|
||||||
if (!Script.isLowDER(sig)) {
|
if (!Script.isLowDER(sig))
|
||||||
bcoin.debug('Script does not have a low DER.');
|
throw new ScriptError('SIG_HIGH_S');
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flags & constants.flags.VERIFY_STRICTENC) {
|
if (flags & constants.flags.VERIFY_STRICTENC) {
|
||||||
if (!Script.isHashType(sig)) {
|
if (!Script.isHashType(sig))
|
||||||
bcoin.debug('Script does not have a valid hash type.');
|
throw new ScriptError('SIG_HASHTYPE');
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -3809,10 +3776,11 @@ Script.fromSymbolic = function fromSymbolic(items) {
|
|||||||
* @param {Number} i
|
* @param {Number} i
|
||||||
* @param {VerifyFlags} flags
|
* @param {VerifyFlags} flags
|
||||||
* @returns {Boolean}
|
* @returns {Boolean}
|
||||||
|
* @throws {ScriptError}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Script.verify = function verify(input, witness, output, tx, i, flags) {
|
Script.verify = function verify(input, witness, output, tx, i, flags) {
|
||||||
var copy, res, raw, redeem, hadWitness;
|
var copy, raw, redeem, hadWitness;
|
||||||
var stack = new Stack();
|
var stack = new Stack();
|
||||||
|
|
||||||
if (flags == null)
|
if (flags == null)
|
||||||
@ -3820,36 +3788,32 @@ Script.verify = function verify(input, witness, output, tx, i, flags) {
|
|||||||
|
|
||||||
if (flags & constants.flags.VERIFY_SIGPUSHONLY) {
|
if (flags & constants.flags.VERIFY_SIGPUSHONLY) {
|
||||||
if (!input.isPushOnly())
|
if (!input.isPushOnly())
|
||||||
return false;
|
throw new ScriptError('SIG_PUSHONLY');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the input script
|
// Execute the input script
|
||||||
res = input.execute(stack, flags, tx, i, 0);
|
input.execute(stack, flags, tx, i, 0);
|
||||||
|
|
||||||
if (!res)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Copy the stack for P2SH
|
// Copy the stack for P2SH
|
||||||
if (flags & constants.flags.VERIFY_P2SH)
|
if (flags & constants.flags.VERIFY_P2SH)
|
||||||
copy = stack.clone();
|
copy = stack.clone();
|
||||||
|
|
||||||
// Execute the previous output script
|
// Execute the previous output script
|
||||||
res = output.execute(stack, flags, tx, i, 0);
|
output.execute(stack, flags, tx, i, 0);
|
||||||
|
|
||||||
// Verify the script did not fail as well as the stack values
|
// Verify the script did not fail as well as the stack values
|
||||||
if (!res || stack.length === 0 || !Script.bool(stack.pop()))
|
if (stack.length === 0 || !Script.bool(stack.pop()))
|
||||||
return false;
|
throw new ScriptError('EVAL_FALSE');
|
||||||
|
|
||||||
if ((flags & constants.flags.VERIFY_WITNESS) && output.isWitnessProgram()) {
|
if ((flags & constants.flags.VERIFY_WITNESS) && output.isWitnessProgram()) {
|
||||||
hadWitness = true;
|
hadWitness = true;
|
||||||
|
|
||||||
// Input script must be empty.
|
// Input script must be empty.
|
||||||
if (input.code.length !== 0)
|
if (input.code.length !== 0)
|
||||||
return false;
|
throw new ScriptError('WITNESS_MALLEATED');
|
||||||
|
|
||||||
// Verify the program in the output script
|
// Verify the program in the output script
|
||||||
if (!Script.verifyProgram(witness, output, flags, tx, i))
|
Script.verifyProgram(witness, output, flags, tx, i);
|
||||||
return false;
|
|
||||||
|
|
||||||
// Force a cleanstack
|
// Force a cleanstack
|
||||||
stack.length = 0;
|
stack.length = 0;
|
||||||
@ -3859,40 +3823,35 @@ Script.verify = function verify(input, witness, output, tx, i, flags) {
|
|||||||
if ((flags & constants.flags.VERIFY_P2SH) && output.isScripthash()) {
|
if ((flags & constants.flags.VERIFY_P2SH) && output.isScripthash()) {
|
||||||
// P2SH can only have push ops in the scriptSig
|
// P2SH can only have push ops in the scriptSig
|
||||||
if (!input.isPushOnly())
|
if (!input.isPushOnly())
|
||||||
return false;
|
throw new ScriptError('SIG_PUSHONLY');
|
||||||
|
|
||||||
// Reset the stack
|
// Reset the stack
|
||||||
stack = copy;
|
stack = copy;
|
||||||
|
|
||||||
// Stack should not be empty at this point
|
// Stack should not be empty at this point
|
||||||
if (stack.length === 0)
|
if (stack.length === 0)
|
||||||
return false;
|
throw new ScriptError('EVAL_FALSE');
|
||||||
|
|
||||||
// Grab the real redeem script
|
// Grab the real redeem script
|
||||||
raw = stack.pop();
|
raw = stack.pop();
|
||||||
|
|
||||||
if (!Script.isPush(raw))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
redeem = new Script(raw);
|
redeem = new Script(raw);
|
||||||
|
|
||||||
// Execute the redeem script
|
// Execute the redeem script
|
||||||
res = redeem.execute(stack, flags, tx, i, 0);
|
redeem.execute(stack, flags, tx, i, 0);
|
||||||
|
|
||||||
// Verify the script did not fail as well as the stack values
|
// Verify the script did not fail as well as the stack values
|
||||||
if (!res || stack.length === 0 || !Script.bool(stack.pop()))
|
if (stack.length === 0 || !Script.bool(stack.pop()))
|
||||||
return false;
|
throw new ScriptError('EVAL_FALSE');
|
||||||
|
|
||||||
if ((flags & constants.flags.VERIFY_WITNESS) && redeem.isWitnessProgram()) {
|
if ((flags & constants.flags.VERIFY_WITNESS) && redeem.isWitnessProgram()) {
|
||||||
hadWitness = true;
|
hadWitness = true;
|
||||||
|
|
||||||
// Input script must be exactly one push of the redeem script.
|
// Input script must be exactly one push of the redeem script.
|
||||||
if (!(input.code.length === 1 && utils.equals(input.code[0], raw)))
|
if (!(input.code.length === 1 && utils.equals(input.code[0], raw)))
|
||||||
return false;
|
throw new ScriptError('WITNESS_MALLEATED');
|
||||||
|
|
||||||
// Verify the program in the redeem script
|
// Verify the program in the redeem script
|
||||||
if (!Script.verifyProgram(witness, redeem, flags, tx, i))
|
Script.verifyProgram(witness, redeem, flags, tx, i);
|
||||||
return false;
|
|
||||||
|
|
||||||
// Force a cleanstack
|
// Force a cleanstack
|
||||||
stack.length = 0;
|
stack.length = 0;
|
||||||
@ -3904,14 +3863,14 @@ Script.verify = function verify(input, witness, output, tx, i, flags) {
|
|||||||
assert((flags & constants.flags.VERIFY_P2SH) !== 0);
|
assert((flags & constants.flags.VERIFY_P2SH) !== 0);
|
||||||
// assert((flags & constants.flags.VERIFY_WITNESS) !== 0);
|
// assert((flags & constants.flags.VERIFY_WITNESS) !== 0);
|
||||||
if (stack.length !== 0)
|
if (stack.length !== 0)
|
||||||
return false;
|
throw new ScriptError('CLEANSTACK');
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we had a witness but no witness program, fail.
|
// If we had a witness but no witness program, fail.
|
||||||
if (flags & constants.flags.VERIFY_WITNESS) {
|
if (flags & constants.flags.VERIFY_WITNESS) {
|
||||||
assert((flags & constants.flags.VERIFY_P2SH) !== 0);
|
assert((flags & constants.flags.VERIFY_P2SH) !== 0);
|
||||||
if (!hadWitness && witness.length > 0)
|
if (!hadWitness && witness.length > 0)
|
||||||
return false;
|
throw new ScriptError('WITNESS_UNEXPECTED');
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -3926,23 +3885,38 @@ Script.verify = function verify(input, witness, output, tx, i, flags) {
|
|||||||
* @param {VerifyFlags} flags
|
* @param {VerifyFlags} flags
|
||||||
* @param {TX} tx
|
* @param {TX} tx
|
||||||
* @param {Number} i
|
* @param {Number} i
|
||||||
|
* @throws {ScriptError}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Script.verifyProgram = function verifyProgram(witness, output, flags, tx, i) {
|
Script.verifyProgram = function verifyProgram(witness, output, flags, tx, i) {
|
||||||
var program, witnessScript, redeem, stack, j, res;
|
var program = output.getWitnessProgram();
|
||||||
|
var stack = witness.toStack();
|
||||||
|
var witnessScript, redeem, j;
|
||||||
|
|
||||||
|
assert(program, 'verifyProgram called on non-witness-program.');
|
||||||
assert((flags & constants.flags.VERIFY_WITNESS) !== 0);
|
assert((flags & constants.flags.VERIFY_WITNESS) !== 0);
|
||||||
assert(output.isWitnessProgram());
|
|
||||||
|
|
||||||
program = output.getWitnessProgram();
|
if (program.version === 0) {
|
||||||
|
if (program.data.length === 32) {
|
||||||
|
if (stack.length === 0)
|
||||||
|
throw new ScriptError('WITNESS_PROGRAM_WITNESS_EMPTY');
|
||||||
|
|
||||||
// Failure on version=0 (bad program data length)
|
witnessScript = stack.pop();
|
||||||
if (!program.type) {
|
|
||||||
bcoin.debug('Malformed witness program.');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (program.version > 0) {
|
if (!utils.equals(utils.sha256(witnessScript), program.data))
|
||||||
|
throw new ScriptError('WITNESS_PROGRAM_MISMATCH');
|
||||||
|
|
||||||
|
redeem = new Script(witnessScript);
|
||||||
|
} else if (program.data.length === 20) {
|
||||||
|
if (stack.length !== 2)
|
||||||
|
throw new ScriptError('WITNESS_PROGRAM_MISMATCH');
|
||||||
|
|
||||||
|
redeem = Script.createPubkeyhash(program.data);
|
||||||
|
} else {
|
||||||
|
// Failure on version=0 (bad program data length)
|
||||||
|
throw new ScriptError('WITNESS_PROGRAM_WRONG_LENGTH');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
bcoin.debug('Unknown witness program version: %s', program.version);
|
bcoin.debug('Unknown witness program version: %s', program.version);
|
||||||
// Anyone can spend (we can return true here
|
// Anyone can spend (we can return true here
|
||||||
// if we want to always relay these transactions).
|
// if we want to always relay these transactions).
|
||||||
@ -3952,45 +3926,24 @@ Script.verifyProgram = function verifyProgram(witness, output, flags, tx, i) {
|
|||||||
// succeed in a block, but fail in the mempool
|
// succeed in a block, but fail in the mempool
|
||||||
// due to VERIFY_CLEANSTACK.
|
// due to VERIFY_CLEANSTACK.
|
||||||
if (flags & constants.flags.VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM)
|
if (flags & constants.flags.VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM)
|
||||||
return false;
|
throw new ScriptError('DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
stack = witness.toStack();
|
|
||||||
|
|
||||||
if (program.type === 'witnesspubkeyhash') {
|
|
||||||
if (stack.length !== 2)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
redeem = Script.createPubkeyhash(program.data);
|
|
||||||
} else if (program.type === 'witnessscripthash') {
|
|
||||||
if (stack.length === 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
witnessScript = stack.pop();
|
|
||||||
|
|
||||||
if (!utils.equals(utils.sha256(witnessScript), program.data))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
redeem = new Script(witnessScript);
|
|
||||||
} else {
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (j = 0; j < stack.length; j++) {
|
for (j = 0; j < stack.length; j++) {
|
||||||
if (stack.get(j).length > constants.script.MAX_PUSH)
|
if (stack.get(j).length > constants.script.MAX_PUSH)
|
||||||
return false;
|
throw new ScriptError('PUSH_SIZE');
|
||||||
}
|
}
|
||||||
|
|
||||||
res = redeem.execute(stack, flags, tx, i, 1);
|
redeem.execute(stack, flags, tx, i, 1);
|
||||||
|
|
||||||
// Verify the script did not fail as well as the stack values
|
// Verify the script did not fail as well as the stack values
|
||||||
if (!res || stack.length === 0 || !Script.bool(stack.pop()))
|
if (stack.length === 0 || !Script.bool(stack.pop()))
|
||||||
return false;
|
throw new ScriptError('EVAL_FALSE');
|
||||||
|
|
||||||
// Witnesses always require cleanstack
|
// Witnesses always require cleanstack
|
||||||
if (stack.length !== 0)
|
if (stack.length !== 0)
|
||||||
return false;
|
throw new ScriptError('EVAL_FALSE');
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@ -4310,25 +4263,31 @@ function ScriptError(code, op, ip) {
|
|||||||
this.type = 'ScriptError';
|
this.type = 'ScriptError';
|
||||||
this.code = code;
|
this.code = code;
|
||||||
|
|
||||||
if (Buffer.isBuffer(op))
|
if (typeof op !== 'string') {
|
||||||
op = 'PUSHDATA[' + op.length + ']';
|
if (Buffer.isBuffer(op))
|
||||||
|
op = 'PUSHDATA[' + op.length + ']';
|
||||||
|
|
||||||
if (op || ip != null) {
|
if (op || ip != null) {
|
||||||
code += '(';
|
code += ' (';
|
||||||
if (op) {
|
if (op) {
|
||||||
op = constants.opcodesByVal[op] || op;
|
op = constants.opcodesByVal[op] || op;
|
||||||
code += 'op=' + op;
|
code += 'op=' + op;
|
||||||
|
if (ip != null)
|
||||||
|
code += ', ';
|
||||||
|
}
|
||||||
if (ip != null)
|
if (ip != null)
|
||||||
code += ', ';
|
code += 'ip=' + ip;
|
||||||
|
code += ')';
|
||||||
}
|
}
|
||||||
if (ip != null)
|
|
||||||
code += 'ip=' + ip;
|
|
||||||
code + ')';
|
|
||||||
}
|
|
||||||
|
|
||||||
this.message = code;
|
this.message = code;
|
||||||
this.op = op;
|
this.op = op || '';
|
||||||
this.ip = ip;
|
this.ip = ip != null ? ip : -1;
|
||||||
|
} else {
|
||||||
|
this.message = op;
|
||||||
|
this.op = '';
|
||||||
|
this.ip = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.inherits(ScriptError, Error);
|
utils.inherits(ScriptError, Error);
|
||||||
|
|||||||
@ -515,7 +515,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TX.prototype.verify = function verify(index, force, flags) {
|
TX.prototype.verify = function verify(index, force, flags) {
|
||||||
var i, input, res;
|
var i, input;
|
||||||
|
|
||||||
// Valid if included in block
|
// Valid if included in block
|
||||||
if (!force && this.ts !== 0)
|
if (!force && this.ts !== 0)
|
||||||
@ -544,17 +544,23 @@ TX.prototype.verify = function verify(index, force, flags) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = Script.verify(
|
try {
|
||||||
input.script,
|
Script.verify(
|
||||||
input.witness,
|
input.script,
|
||||||
input.coin.script,
|
input.witness,
|
||||||
this,
|
input.coin.script,
|
||||||
i,
|
this,
|
||||||
flags
|
i,
|
||||||
);
|
flags
|
||||||
|
);
|
||||||
if (!res)
|
} catch (e) {
|
||||||
|
if (e.type === 'ScriptError') {
|
||||||
|
bcoin.debug('Script verification error: %s', e.message);
|
||||||
|
} else {
|
||||||
|
bcoin.debug('Script interpreter threw: %s', e.stack + '');
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -1133,7 +1139,7 @@ TX.prototype.isStandard = function isStandard(flags, ret) {
|
|||||||
TX.prototype.hasStandardInputs = function hasStandardInputs(flags) {
|
TX.prototype.hasStandardInputs = function hasStandardInputs(flags) {
|
||||||
var maxSigops = constants.script.MAX_SCRIPTHASH_SIGOPS;
|
var maxSigops = constants.script.MAX_SCRIPTHASH_SIGOPS;
|
||||||
var VERIFY_NONE = constants.flags.VERIFY_NONE;
|
var VERIFY_NONE = constants.flags.VERIFY_NONE;
|
||||||
var i, input, stack, res, redeem;
|
var i, input, stack, redeem;
|
||||||
|
|
||||||
if (flags == null)
|
if (flags == null)
|
||||||
flags = constants.flags.STANDARD_VERIFY_FLAGS;
|
flags = constants.flags.STANDARD_VERIFY_FLAGS;
|
||||||
@ -1160,10 +1166,11 @@ TX.prototype.hasStandardInputs = function hasStandardInputs(flags) {
|
|||||||
|
|
||||||
stack = new Stack();
|
stack = new Stack();
|
||||||
|
|
||||||
res = input.script.execute(stack, VERIFY_NONE, this, i, 0);
|
try {
|
||||||
|
input.script.execute(stack, VERIFY_NONE, this, i, 0);
|
||||||
if (!res)
|
} catch (e) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (stack.length === 0)
|
if (stack.length === 0)
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -254,11 +254,20 @@ describe('Script', function() {
|
|||||||
delete input.raw;
|
delete input.raw;
|
||||||
delete output.raw;
|
delete output.raw;
|
||||||
}
|
}
|
||||||
var res = Script.verify(input, witness, output, tx, 0, flags);
|
var err, res;
|
||||||
if (expected === 'OK')
|
try {
|
||||||
assert.ok(res);
|
res = Script.verify(input, witness, output, tx, 0, flags);
|
||||||
else
|
} catch (e) {
|
||||||
assert.ok(!res);
|
err = e;
|
||||||
|
}
|
||||||
|
if (expected !== 'OK') {
|
||||||
|
assert(!res);
|
||||||
|
assert(err);
|
||||||
|
assert.equal(err.code, expected);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
utils.assert.noError(err);
|
||||||
|
assert(res);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user