Fix RemovePattern issues
[NovacoinLibrary.git] / Novacoin / ScriptCode.cs
index 658a74c..19850c9 100644 (file)
@@ -1,17 +1,19 @@
 /**
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 using System;
@@ -23,7 +25,7 @@ using System.Text;
 namespace Novacoin
 {
     /// <summary>
-    /// Script opcodes
+    /// Script instructions
     /// </summary>
     public enum instruction
     {
@@ -223,10 +225,10 @@ namespace Novacoin
         }
 
         /// <summary>
-        /// Get the name of supplied opcode
+        /// Get the name of instruction
         /// </summary>
-        /// <param name="opcode">Opcode</param>
-        /// <returns>Opcode name</returns>
+        /// <param name="opcode">Instruction</param>
+        /// <returns>Instruction name</returns>
         public static string GetOpName(instruction opcode)
         {
             if (opcode == instruction.OP_0) // OP_0 and OP_FALSE are synonyms
@@ -238,10 +240,10 @@ namespace Novacoin
         }
 
         /// <summary>
-        /// Get next opcode from passed list of bytes and extract push arguments if there are some.
+        /// Get next instruction from list of bytes and extract push arguments if there are some.
         /// </summary>
         /// <param name="codeBytes">ByteQueue reference.</param>
-        /// <param name="opcodeRet">Found opcode.</param>
+        /// <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)
@@ -265,13 +267,13 @@ namespace Novacoin
             // Immediate operand
             if (opcode <= instruction.OP_PUSHDATA4)
             {
-                byte[] szBytes = new byte[4] { 0, 0, 0, 0 }; // Zero length
+                var szBytes = new byte[4] { 0, 0, 0, 0 }; // Zero length
 
                 try
                 {
                     if (opcode < instruction.OP_PUSHDATA1)
                     {
-                        // Zero value opcodes (OP_0, OP_FALSE)
+                        // Zero value instructions (OP_0, OP_FALSE)
                         szBytes[3] = (byte)opcode;
                     }
                     else if (opcode == instruction.OP_PUSHDATA1)
@@ -364,7 +366,7 @@ namespace Novacoin
         /// <summary>
         /// Decode instruction to integer value
         /// </summary>
-        /// <param name="opcode">Small integer opcode (OP_1_NEGATE and OP_0 - OP_16)</param>
+        /// <param name="opcode">Small integer instruction (OP_1_NEGATE and OP_0 - OP_16)</param>
         /// <returns>Small integer</returns>
         public static int DecodeOP_N(instruction opcode, bool AllowNegate = false)
         {
@@ -378,7 +380,7 @@ namespace Novacoin
                 return 0;
             }
 
-            // Only OP_n opcodes are supported, throw exception otherwise.
+            // Only OP_n instructions are supported, throw exception otherwise.
             if (opcode < instruction.OP_1 || opcode > instruction.OP_16)
             {
                 throw new ArgumentException("Invalid integer instruction.");
@@ -391,7 +393,7 @@ namespace Novacoin
         /// Converts integer into instruction
         /// </summary>
         /// <param name="n">Small integer from the range of -1 up to 16.</param>
-        /// <returns>Corresponding opcode.</returns>
+        /// <returns>Corresponding instruction.</returns>
         public static instruction EncodeOP_N(int n, bool allowNegate = false)
         {
             if (allowNegate && n == -1)
@@ -520,7 +522,7 @@ namespace Novacoin
 
             // Sender provides N pubkeys, receivers provides M signatures
             // N [pubkey1] [pubkey2] ... [pubkeyN] M OP_CHECKMULTISIG
-            // Where N and M are small integer opcodes (OP1 ... OP_16)
+            // Where N and M are small integer instructions (OP1 ... OP_16)
             templateTuples.Add(
                 new Tuple<txnouttype, byte[]>(
                     txnouttype.TX_MULTISIG,
@@ -591,7 +593,7 @@ namespace Novacoin
                         break;
                     }
 
-                    // Template matching opcodes:
+                    // Template matching instructions:
                     if (opcode2 == instruction.OP_PUBKEYS)
                     {
                         while (args1.Count() >= 33 && args1.Count() <= 120)
@@ -683,7 +685,7 @@ namespace Novacoin
 
             // In case concatenating two scripts ends up with two codeseparators,
             // or an extra one at the end, this prevents all those possible incompatibilities.
-            script.RemovePattern(new byte[] { (byte)instruction.OP_CODESEPARATOR });
+            script.RemoveInstruction(instruction.OP_CODESEPARATOR);
 
             // Blank out other inputs' signatures
             for (int i = 0; i < txTmp.vin.Length; i++)
@@ -741,12 +743,11 @@ namespace Novacoin
                 Array.Resize(ref txTmp.vin, 1);
             }
 
-            // Serialize and hash
-            var b = new List<byte>();
-            b.AddRange(txTmp.Bytes);
-            b.AddRange(BitConverter.GetBytes(nHashType));
+            // Concatenate and hash
+            var txBytes = txTmp.Bytes;
+            var nHashTypeBytes = BitConverter.GetBytes(nHashType);
 
-            return Hash256.Compute256(b);
+            return Hash256.Compute256(ref txBytes, ref nHashTypeBytes);
         }
 
         //
@@ -909,7 +910,7 @@ namespace Novacoin
                         switch (opcode)
                         {
                             //
-                            // Disabled opcodes
+                            // Disabled instructions
                             //
                             case instruction.OP_CAT:
                             case instruction.OP_SUBSTR:
@@ -1567,11 +1568,11 @@ namespace Novacoin
                                         return false;
                                     }
 
-                                    byte[] sigBytes = stacktop(ref stack, -2);
-                                    byte[] pubkeyBytes = stacktop(ref stack, -1);
+                                    var sigBytes = stacktop(ref stack, -2);
+                                    var pubkeyBytes = stacktop(ref stack, -1);
 
                                     // Subset of script starting at the most recent codeseparator
-                                    CScript scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin));
+                                    var scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin).ToArray());
 
                                     // There's no way for a signature to sign itself
                                     scriptCode.RemovePattern(sigBytes);
@@ -1638,7 +1639,7 @@ namespace Novacoin
                                     }
 
                                     // Subset of script starting at the most recent codeseparator
-                                    CScript scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin));
+                                    var scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin).ToArray());
 
                                     // There is no way for a signature to sign itself, so we need to drop the signatures
                                     for (int k = 0; k < nSigsCount; k++)