Use implicit type casting operator for serialization.
[NovacoinLibrary.git] / Novacoin / CScript.cs
index bbb2a61..a9ca8ef 100644 (file)
@@ -59,7 +59,7 @@ namespace Novacoin
         /// Initializes new instance of CScript and fills it with supplied bytes
         /// </summary>
         /// <param name="bytes">Enumerator interface for byte sequence</param>
-        public CScript(IEnumerable<byte> bytes)
+        public CScript(byte[] bytes)
         {
             codeBytes = new List<byte>(bytes);
         }
@@ -96,7 +96,7 @@ namespace Novacoin
         public void AddHash(Hash160 hash)
         {
             codeBytes.Add((byte)hash.hashSize);
-            codeBytes.AddRange(hash.hashBytes);
+            codeBytes.AddRange((byte[])hash);
         }
 
         /// <summary>
@@ -108,7 +108,7 @@ namespace Novacoin
         public void AddHash(Hash256 hash)
         {
             codeBytes.Add((byte)hash.hashSize);
-            codeBytes.AddRange(hash.hashBytes);
+            codeBytes.AddRange((byte[])hash);
         }
 
         /// <summary>
@@ -132,18 +132,18 @@ namespace Novacoin
             }
             else if (nCount < 0xffff)
             {
-                // OP_PUSHDATA1 0x00 0x01 [0x5a]
+                // OP_PUSHDATA1 0x01 0x00 [0x5a]
                 codeBytes.Add((byte)instruction.OP_PUSHDATA2);
 
-                var szBytes = Interop.BEBytes((ushort)nCount);
+                var szBytes = BitConverter.GetBytes((ushort)nCount);
                 codeBytes.AddRange(szBytes);
             }
             else if (nCount < 0xffffffff)
             {
-                // OP_PUSHDATA1 0x00 0x00 0x00 0x01 [0x5a]
+                // OP_PUSHDATA1 0x01 0x00 0x00 0x00 [0x5a]
                 codeBytes.Add((byte)instruction.OP_PUSHDATA4);
 
-                var szBytes = Interop.BEBytes((uint)nCount);
+                var szBytes = BitConverter.GetBytes((uint)nCount);
                 codeBytes.AddRange(szBytes);
             }
 
@@ -152,39 +152,102 @@ namespace Novacoin
         }
 
         /// <summary>
-        /// Scan code bytes for pattern
+        /// Just insert data array without including any prefixes. Please make sure that you know what you're doing, 
+        ///    it is recommended to use AddInstruction, AddHash or PushData instead.
+        /// </summary>
+        /// <param name="dataBytes">Data bytes</param>
+        public void AddRawData(byte[] dataBytes)
+        {
+            // Add data bytes
+            codeBytes.AddRange(dataBytes);
+        }
+
+        /// <summary>
+        /// Scan pushed data bytes for pattern and, in case of exact match, remove it.
         /// </summary>
         /// <param name="pattern">Pattern sequence</param>
-        /// <returns>Matches enumerator</returns>
-        private IEnumerable<int> FindPattern(byte[] pattern)
+        /// <returns>Matches count</returns>
+        public int RemovePattern(byte[] pattern)
         {
-            for (int i = 0; i < codeBytes.Count; i++)
+            // There is no sense to continue if pattern is empty or longer than script itself
+            if (pattern.Length == 0 || pattern.Length > codeBytes.Count)
             {
-                if (codeBytes.Skip(i).Take(pattern.Length).SequenceEqual(pattern))
+                return 0;
+            }
+
+            var count = 0;
+            var bq1 = new ByteQueue(codeBytes);
+
+            byte[] pushData;
+            instruction opcode;
+
+            var newScript = new CScript();
+
+            while (ScriptCode.GetOp(ref bq1, out opcode, out pushData))
+            {
+                if (pushData.Length == 0)
+                {
+                    // No data, put instruction on its place
+                    newScript.AddInstruction(opcode);
+                }
+                else if (!pushData.SequenceEqual(pattern))
+                {
+                    // No match, create push operator
+                    newScript.PushData(pushData);
+                }
+                else
                 {
-                    yield return i;
+                    count++; // match
                 }
             }
+
+            if (count > 0)
+            {
+                // Replace current script if any matches were found
+                codeBytes = newScript.codeBytes;
+            }
+
+            return count;
         }
 
         /// <summary>
-        /// Scan code bytes for pattern and remove it
+        /// Scan script for specific instruction and remove it if there are some matches.
         /// </summary>
-        /// <param name="pattern">Pattern sequence</param>
-        /// <returns>Matches number</returns>
-        public int RemovePattern(byte[] pattern)
+        /// <param name="op">Instruction</param>
+        /// <returns>Matches count</returns>
+        public int RemoveInstruction(instruction op)
         {
-            var resultBytes = new List<byte>(codeBytes);
-            int count = 0;
-                        
-            foreach (int i in FindPattern(pattern))
+            byte[] pushData;
+            instruction opcode;
+
+            var count = 0;
+            var newScript = new CScript();
+            var bq1 = new ByteQueue(codeBytes);
+
+            while (ScriptCode.GetOp(ref bq1, out opcode, out pushData))
+            {
+                if (pushData.Length != 0 && op != opcode)
+                {
+                    // If instruction didn't match then push its data again
+                    newScript.PushData(pushData);
+                }
+                else if (Enum.IsDefined(typeof(instruction), op) && op != opcode)
+                {
+                    // Instruction didn't match
+                    newScript.AddInstruction(opcode);
+                }
+                else
+                {
+                    count++; // match
+                }
+            }
+
+            if (count > 0)
             {
-                resultBytes.RemoveRange(i - count * pattern.Length, pattern.Length);
-                count++;
+                // Replace current script if any matches were found
+                codeBytes = newScript.codeBytes;
             }
 
-            codeBytes = resultBytes;
-            
             return count;
         }
 
@@ -384,7 +447,7 @@ namespace Novacoin
         public void SetDestination(CPubKey pubKey)
         {
             codeBytes.Clear();
-            PushData(pubKey.PublicBytes);
+            PushData(pubKey);
             AddInstruction(instruction.OP_CHECKSIG);
         }
 
@@ -434,7 +497,7 @@ namespace Novacoin
 
             foreach (var key in keys)
             {
-                PushData(key.PublicBytes);
+                PushData(key);
             }
 
             AddInstruction(ScriptCode.EncodeOP_N(keys.Length));
@@ -444,9 +507,9 @@ namespace Novacoin
         /// <summary>
         /// Access to script code.
         /// </summary>
-        public byte[] Bytes
+        public static implicit operator byte[] (CScript script)
         {
-            get { return codeBytes.ToArray(); }
+            return script.codeBytes.ToArray();
         }
 
         public CScriptID ScriptID