Use BouncyCastle hashing functions
[NovacoinLibrary.git] / Novacoin / CScript.cs
index d1165a8..9149d34 100644 (file)
@@ -1,4 +1,22 @@
-\feffusing System;
+\feff/**
+ *  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;
 using System.Linq;
 using System.Text;
 using System.Collections.Generic;
@@ -97,9 +115,9 @@ namespace Novacoin
         /// Create new OP_PUSHDATAn operator and add it to opcode bytes list
         /// </summary>
         /// <param name="dataBytes">Set of data bytes</param>
-        public void PushData(IEnumerable<byte> dataBytes)
+        public void PushData(byte[] dataBytes)
         {
-            long nCount = dataBytes.LongCount();
+            var nCount = dataBytes.LongLength;
 
             if (nCount < (int)instruction.OP_PUSHDATA1)
             {
@@ -138,11 +156,11 @@ namespace Novacoin
         /// </summary>
         /// <param name="pattern">Pattern sequence</param>
         /// <returns>Matches enumerator</returns>
-        private IEnumerable<int> FindPattern(IList<byte> pattern)
+        private IEnumerable<int> FindPattern(byte[] pattern)
         {
             for (int i = 0; i < codeBytes.Count; i++)
             {
-                if (codeBytes.Skip(i).Take(pattern.Count).SequenceEqual(pattern))
+                if (codeBytes.Skip(i).Take(pattern.Length).SequenceEqual(pattern))
                 {
                     yield return i;
                 }
@@ -154,15 +172,14 @@ namespace Novacoin
         /// </summary>
         /// <param name="pattern">Pattern sequence</param>
         /// <returns>Matches number</returns>
-        public int RemovePattern(IList<byte> pattern)
+        public int RemovePattern(byte[] pattern)
         {
-            List<byte> resultBytes = new List<byte>(codeBytes);
+            var resultBytes = new List<byte>(codeBytes);
             int count = 0;
-            int patternLen = pattern.Count;
                         
             foreach (int i in FindPattern(pattern))
             {
-                resultBytes.RemoveRange(i - count * patternLen, patternLen);
+                resultBytes.RemoveRange(i - count * pattern.Length, pattern.Length);
                 count++;
             }
 
@@ -174,14 +191,14 @@ namespace Novacoin
         /// <summary>
         /// Is it true that script doesn't contain anything except push value operations?
         /// </summary>
-        public bool IsPushonly
+        public bool IsPushOnly
         {
             get
             {
-                ByteQueue wCodeBytes = new ByteQueue(codeBytes);
+                var wCodeBytes = new ByteQueue(codeBytes);
 
                 instruction opcode; // Current opcode
-                IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
+                byte[] pushArgs; // OP_PUSHDATAn argument
 
                 // Scan opcodes sequence
                 while (ScriptCode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
@@ -204,15 +221,15 @@ namespace Novacoin
         {
             get
             {
-                ByteQueue wCodeBytes = new ByteQueue(codeBytes);
+                var wCodeBytes = new ByteQueue(codeBytes);
 
+                byte[] pushArgs; // OP_PUSHDATAn argument
                 instruction opcode; // Current opcode
-                IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
 
                 // Scan opcodes sequence
                 while (ScriptCode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
                 {
-                    byte[] data = pushArgs.ToArray();
+                    var data = pushArgs;
 
                     if (opcode < instruction.OP_PUSHDATA1 && opcode > instruction.OP_0 && (data.Length == 1 && data[0] <= 16))
                     {
@@ -293,13 +310,13 @@ namespace Novacoin
         /// <returns>Amount of sigops</returns>
         public int GetSigOpCount(bool fAccurate)
         {
-            ByteQueue wCodeBytes = new ByteQueue(codeBytes);
+            var wCodeBytes = new ByteQueue(codeBytes);
 
             instruction opcode; // Current opcode
-            IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
+            byte[] pushArgs; // OP_PUSHDATAn argument
 
             int nCount = 0;
-            instruction lastOpcode = instruction.OP_INVALIDOPCODE;
+            var lastOpcode = instruction.OP_INVALIDOPCODE;
 
             // Scan opcodes sequence
             while (ScriptCode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
@@ -343,7 +360,7 @@ namespace Novacoin
             ByteQueue wScriptSig = scriptSig.GetByteQUeue();
 
             instruction opcode; // Current opcode
-            IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
+            byte[] pushArgs; // OP_PUSHDATAn argument
 
             while (ScriptCode.GetOp(ref wScriptSig, out opcode, out pushArgs))
             {
@@ -354,7 +371,7 @@ namespace Novacoin
             }
 
             /// ... and return its opcount:
-            CScript subScript = new CScript(pushArgs);
+            var subScript = new CScript(pushArgs);
 
             return subScript.GetSigOpCount(true);
 
@@ -410,31 +427,31 @@ namespace Novacoin
         /// </summary>
         /// <param name="nRequired">Amount of required signatures.</param>
         /// <param name="keys">Set of public keys.</param>
-        public void SetMultiSig(int nRequired, IEnumerable<CPubKey> keys)
+        public void SetMultiSig(int nRequired, CPubKey[] keys)
         {
             codeBytes.Clear();
             AddOp(ScriptCode.EncodeOP_N(nRequired));
 
-            foreach (CPubKey key in keys)
+            foreach (var key in keys)
             {
-                PushData(key.PublicBytes.ToList());
+                PushData(key.PublicBytes);
             }
 
-            AddOp(ScriptCode.EncodeOP_N(keys.Count()));
+            AddOp(ScriptCode.EncodeOP_N(keys.Length));
             AddOp(instruction.OP_CHECKMULTISIG);
         }
 
         /// <summary>
         /// Access to script code.
         /// </summary>
-        public IEnumerable<byte> Bytes
+        public byte[] Bytes
         {
-            get { return codeBytes; }
+            get { return codeBytes.ToArray(); }
         }
 
         public CScriptID ScriptID
         {
-            get { return new CScriptID(Hash160.Compute160(codeBytes)); }
+            get { return new CScriptID(Hash160.Compute160(codeBytes.ToArray())); }
         }
 
         /// <summary>
@@ -443,11 +460,11 @@ namespace Novacoin
         /// <returns>Code listing</returns>
                public override string ToString()
                {
-                       StringBuilder sb = new StringBuilder();
-            ByteQueue wCodeBytes = new ByteQueue(codeBytes);
+                       var sb = new StringBuilder();
+            var wCodeBytes = new ByteQueue(codeBytes);
 
             instruction opcode; // Current opcode
-            IEnumerable<byte> pushArgs; // OP_PUSHDATAn argument
+            byte[] pushArgs; // OP_PUSHDATAn argument
             while (ScriptCode.GetOp(ref wCodeBytes, out opcode, out pushArgs))
             {
                 if (sb.Length != 0)
@@ -469,4 +486,3 @@ namespace Novacoin
                }
        }
 }
-