From 63dd30393cde7e3a53ac8734ee594affe321748b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 30 Sep 2016 11:20:58 -0700 Subject: [PATCH] script: implement minimalif. --- lib/protocol/constants.js | 3 ++- lib/script/script.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/protocol/constants.js b/lib/protocol/constants.js index dadbb30f..c49f4697 100644 --- a/lib/protocol/constants.js +++ b/lib/protocol/constants.js @@ -775,8 +775,9 @@ exports.flags = { VERIFY_CHECKSEQUENCEVERIFY: (1 << 10), VERIFY_WITNESS: (1 << 11), VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM: (1 << 12), - VERIFY_MAST: (1 << 13), + VERIFY_MINIMALIF: (1 << 13), VERIFY_NULLFAIL: (1 << 14), + VERIFY_MAST: (1 << 15), // should be 1 << 13 VERIFY_SEQUENCE: (1 << 0), MEDIAN_TIME_PAST: (1 << 1) }; diff --git a/lib/script/script.js b/lib/script/script.js index f8421663..ec9a0c38 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -362,33 +362,52 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) { case opcodes.OP_IF: case opcodes.OP_NOTIF: { val = false; + if (negate === 0) { if (stack.length < 1) throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip); + val = Script.bool(stack.pop()); + + if (version == 1 && (flags & constants.flags.VERIFY_MINIMALIF)) { + if (val.length > 1) + throw new ScriptError('MINIMALIF'); + + if (val.length === 1 && val[0] !== 1) + throw new ScriptError('MINIMALIF'); + } + if (op === opcodes.OP_NOTIF) val = !val; } + state.push(val); + if (!val) negate++; + break; } case opcodes.OP_ELSE: { if (state.length === 0) throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip); + state[state.length - 1] = !state[state.length - 1]; + if (!state[state.length - 1]) negate++; else negate--; + break; } case opcodes.OP_ENDIF: { if (state.length === 0) throw new ScriptError('UNBALANCED_CONDITIONAL', op, ip); + if (!state.pop()) negate--; + break; } case opcodes.OP_VERIF: