From 98ebcd55eb4a3881ef6153387f5d000c85f3404e Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Sun, 23 Aug 2015 02:27:32 +0300 Subject: [PATCH] VerifyScript --- Novacoin/CScript.cs | 2 +- Novacoin/ScriptCode.cs | 68 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/Novacoin/CScript.cs b/Novacoin/CScript.cs index d1165a8..077111f 100644 --- a/Novacoin/CScript.cs +++ b/Novacoin/CScript.cs @@ -174,7 +174,7 @@ namespace Novacoin /// /// Is it true that script doesn't contain anything except push value operations? /// - public bool IsPushonly + public bool IsPushOnly { get { diff --git a/Novacoin/ScriptCode.cs b/Novacoin/ScriptCode.cs index 0d83bd0..ff8bb40 100644 --- a/Novacoin/ScriptCode.cs +++ b/Novacoin/ScriptCode.cs @@ -2020,7 +2020,7 @@ namespace Novacoin /// Input number /// Hashing type flag /// Signature checking flags - /// + /// Checking result public static bool CheckSig(IList sigBytes, IList pubkeyBytes, CScript script, CTransaction txTo, int nIn, int nHashType, int flags) { CPubKey pubkey; @@ -2070,5 +2070,71 @@ namespace Novacoin return true; } + + /// + /// Evaluates the both scriptSig and scriptPubKey. + /// + /// + /// + /// Transaction + /// Input number + /// Script validation flags + /// Hash type flag + /// + public static bool VerifyScript(CScript scriptSig, CScript scriptPubKey, CTransaction txTo, int nIn, int flags, int nHashType) + { + List> stack = new List>(); + List> stackCopy = null; + + if (!EvalScript(ref stack, scriptSig, txTo, nIn, flags, nHashType)) + { + return false; + } + + if ((flags & (int)scriptflag.SCRIPT_VERIFY_P2SH) != 0) + { + stackCopy = new List> (stack); + } + + if (!EvalScript(ref stack, scriptPubKey, txTo, nIn, flags, nHashType)) + { + return false; + } + + if (stack.Count == 0 || CastToBool(stack.Last()) == false) + { + return false; + } + + // Additional validation for spend-to-script-hash transactions: + if ((flags & (int)scriptflag.SCRIPT_VERIFY_P2SH) != 0 && scriptPubKey.IsPayToScriptHash) + { + if (!scriptSig.IsPushOnly) // scriptSig must be literals-only + { + return false; + } + + // stackCopy cannot be empty here, because if it was the + // P2SH HASH <> EQUAL scriptPubKey would be evaluated with + // an empty stack and the EvalScript above would return false. + + if (stackCopy.Count == 0) + { + throw new StackMachineException("Fatal script validation error."); + } + + CScript pubKey2 = new CScript(stackCopy.Last()); + popstack(ref stackCopy); + + if (!EvalScript(ref stackCopy, pubKey2, txTo, nIn, flags, nHashType)) + return false; + if (stackCopy.Count == 0) + return false; + + return CastToBool(stackCopy.Last()); + } + + return true; + } }; } -- 1.7.1