Improve CryptoUtils with wrappers for managed implementations of standard hashing...
[NovacoinLibrary.git] / Novacoin / ScriptCode.cs
index 458445a..14bc2e5 100644 (file)
@@ -247,23 +247,18 @@ namespace Novacoin
         /// <param name="opcodeRet">Found instruction.</param>
         /// <param name="bytesRet">IEnumerable out param which is used to get the push arguments.</param>
         /// <returns>Result of operation</returns>
-        public static bool GetOp(ref ByteQueue codeBytes, out instruction opcodeRet, out byte[] bytesRet)
+        public static bool GetOp(ref InstructionQueue codeBytes, out instruction opcodeRet, out byte[] bytesRet)
         {
             bytesRet = new byte[0];
-            opcodeRet = instruction.OP_INVALIDOPCODE;
+            instruction opcode = opcodeRet = instruction.OP_INVALIDOPCODE;
 
-            instruction opcode;
-
-            try
+            // Read instruction
+            byte opVal = 0xff;
+            if (!codeBytes.TryGet(ref opVal))
             {
-                // Read instruction
-                opcode = (instruction)codeBytes.Get();
-            }
-            catch (ByteQueueException)
-            {
-                // No instruction found there
                 return false;
             }
+            opcode = (instruction)opVal;
 
             // Immediate operand
             if (opcode <= instruction.OP_PUSHDATA4)
@@ -297,7 +292,7 @@ namespace Novacoin
                         nSize = BitConverter.ToInt32(codeBytes.Get(4), 0);
                     }
                 }
-                catch (ByteQueueException)
+                catch (InstructionQueueException)
                 {
                     // Unable to read operand length
                     return false;
@@ -305,13 +300,8 @@ namespace Novacoin
 
                 if (nSize > 0)
                 {
-                    // If nSize is greater than zero then there is some data available
-                    try
-                    {
-                        // Read found number of bytes into list of OP_PUSHDATAn arguments.
-                        bytesRet = codeBytes.Get(nSize);
-                    }
-                    catch (ByteQueueException)
+                    // Trying to read found number of bytes into list of OP_PUSHDATAn arguments.
+                    if (!codeBytes.TryGet(nSize, ref bytesRet))
                     {
                         // Unable to read data
                         return false;
@@ -551,8 +541,8 @@ namespace Novacoin
                 instruction opcode1, opcode2;
 
                 // Compare
-                var bq1 = script1.GetByteQueue();
-                var bq2 = script2.GetByteQueue();
+                var bq1 = script1.GetInstructionQueue();
+                var bq2 = script2.GetInstructionQueue();
 
                 byte[] args1, args2;
 
@@ -668,12 +658,7 @@ namespace Novacoin
         /// <returns></returns>
         public static Hash256 SignatureHash(CScript script, CTransaction txTo, int nIn, int nHashType)
         {
-            if (nIn >= txTo.vin.Length)
-            {
-                var sb = new StringBuilder();
-                sb.AppendFormat("ERROR: SignatureHash() : nIn={0} out of range\n", nIn);
-                throw new ArgumentOutOfRangeException("nIn", sb.ToString());
-            }
+            Contract.Requires<ArgumentOutOfRangeException>(nIn < txTo.vin.Length, "nIn out of range.");
 
             // Init a copy of transaction
             var txTmp = new CTransaction(txTo);
@@ -777,13 +762,9 @@ namespace Novacoin
         /// <param name="stack">Stack reference</param>
         private static void popstack(ref List<byte[]> stack)
         {
-            int nCount = stack.Count;
-            if (nCount == 0)
-            {
-                throw new StackMachineException("Stack is empty");
-            }
+            Contract.Requires<StackMachineException>(stack.Count > 0, "Stack is empty.");
 
-            stack.RemoveAt(nCount - 1);
+            stack.RemoveAt(stack.Count - 1);
         }
 
         /// <summary>
@@ -794,8 +775,8 @@ namespace Novacoin
         /// <returns>Byte sequence</returns>
         private static byte[] stacktop(ref List<byte[]> stack, int nDepth)
         {
-            Contract.Requires<StackMachineException>(nDepth < 0, "Positive stack depth makes no sense.");
-            Contract.Requires<StackMachineException>(stack.Count + nDepth > 0, "Value exceeds real stack depth.");
+            Contract.Requires<StackMachineException>(nDepth < 0, "Positive or zero stack depth makes no sense.");
+            Contract.Requires<StackMachineException>(stack.Count + nDepth >= 0, "Value exceeds real stack depth.");
 
             return stack[stack.Count + nDepth];
         }
@@ -863,11 +844,13 @@ namespace Novacoin
             var falseBytes = new byte[0];
             var trueBytes = new byte[] { 0x01 };
 
-            var CodeQueue = script.GetByteQueue();
+            var CodeQueue = script.GetInstructionQueue();
             var altStack = new List<byte[]>();
 
+#if !DEBUG
             try
             {
+#endif
                 instruction opcode;
                 byte[] pushArg;
 
@@ -1509,25 +1492,25 @@ namespace Novacoin
                                     {
                                         return false;
                                     }
-                                    Hash hash = null;
+                                    byte[] hash = null;
                                     var data = stacktop(ref stack, -1);
 
                                     switch (opcode)
                                     {
                                         case instruction.OP_HASH160:
-                                            hash = Hash160.Compute160(data);
+                                            hash = CryptoUtils.ComputeHash160(data);
                                             break;
                                         case instruction.OP_HASH256:
-                                            hash = Hash256.Compute256(data);
+                                            hash = CryptoUtils.ComputeHash256(data);
                                             break;
                                         case instruction.OP_SHA1:
-                                            hash = SHA1.Compute1(data);
+                                            hash = CryptoUtils.ComputeSha1(data);
                                             break;
                                         case instruction.OP_SHA256:
-                                            hash = SHA256.Compute256(data);
+                                            hash = CryptoUtils.ComputeSha256(data);
                                             break;
                                         case instruction.OP_RIPEMD160:
-                                            hash = RIPEMD160.Compute160(data);
+                                            hash = CryptoUtils.ComputeRipeMD160(data);
                                             break;
                                     }
                                     popstack(ref stack);
@@ -1703,12 +1686,14 @@ namespace Novacoin
                         return false;
                     }
                 }
+#if !DEBUG
             }
             catch (Exception)
             {
                 // If there are any exceptions then just return false.
                 return false;
             }
+#endif
 
             if (vfExec.Count() != 0)
             {