From: CryptoManiac Date: Sat, 15 Aug 2015 19:16:21 +0000 (+0300) Subject: IsPushOnly & HashOnlyCanonicalPushes X-Git-Url: https://git.novaco.in/?a=commitdiff_plain;h=5cdad770376d2e941aad03ce2fa683b06e0b4a8a;p=NovacoinLibrary.git IsPushOnly & HashOnlyCanonicalPushes --- diff --git a/Novacoin/CScript.cs b/Novacoin/CScript.cs index 47ad01c..45381f9 100644 --- a/Novacoin/CScript.cs +++ b/Novacoin/CScript.cs @@ -180,6 +180,64 @@ namespace Novacoin } /// + /// Is it true that script doesn't contain anything except push value operations? + /// + /// Checking result + public bool IsPushonly() + { + WrappedList wCodeBytes = new WrappedList(codeBytes); + + opcodetype opcode; // Current opcode + IEnumerable pushArgs; // OP_PUSHDATAn argument + + // Scan opcodes sequence + while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs)) + { + if (opcode > opcodetype.OP_16) + { + // We don't allow control opcodes here + return false; + } + } + + return true; + } + + /// + /// Is it true that script doesn't contain non-canonical push operations? + /// + /// Checking result + public bool HashOnlyCanonicalPushes() + { + WrappedList wCodeBytes = new WrappedList(codeBytes); + + opcodetype opcode; // Current opcode + IEnumerable pushArgs; // OP_PUSHDATAn argument + + // Scan opcodes sequence + while (ScriptOpcode.GetOp(ref wCodeBytes, out opcode, out pushArgs)) + { + byte[] data = pushArgs.ToArray(); + + if (opcode < opcodetype.OP_PUSHDATA1 && opcode > opcodetype.OP_0 && (data.Length == 1 && data[0] <= 16)) + // Could have used an OP_n code, rather than a 1-byte push. + return false; + if (opcode == opcodetype.OP_PUSHDATA1 && data.Length < (int)opcodetype.OP_PUSHDATA1) + // Could have used a normal n-byte push, rather than OP_PUSHDATA1. + return false; + if (opcode == opcodetype.OP_PUSHDATA2 && data.Length <= 0xFF) + // Could have used an OP_PUSHDATA1. + return false; + if (opcode == opcodetype.OP_PUSHDATA4 && data.Length <= 0xFFFF) + // Could have used an OP_PUSHDATA2. + return false; + } + + return true; + } + + + /// /// Disassemble current script code /// /// Code listing