Implementation of IEquatable<base_uint> and IEqualityComparer<base_uint>, derive...
authorCryptoManiac <balthazar.ad@gmail.com>
Wed, 2 Sep 2015 08:32:45 +0000 (11:32 +0300)
committerCryptoManiac <balthazar.ad@gmail.com>
Wed, 2 Sep 2015 08:32:45 +0000 (11:32 +0300)
Novacoin/CKeyID.cs
Novacoin/CScript.cs
Novacoin/CScriptID.cs
Novacoin/base_uint.cs

index d7130a8..62639a1 100644 (file)
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-using System;
-using System.Diagnostics.Contracts;
-
 namespace Novacoin
 {
-    public class CKeyID : Hash160
+    /// <summary>
+    /// Represents the key identifier. Internal state is calculated as Hash160(pubkey).
+    /// </summary>
+    public class CKeyID : uint160
     {
-        public CKeyID(Hash160 pubKeyHash)
+        public CKeyID() : base()
+        {
+        }
+
+        public CKeyID(CKeyID KeyID) : base(KeyID)
         {
-            _hashBytes = pubKeyHash;
         }
 
-        internal CKeyID(byte[] hashBytes)
+        public CKeyID(uint160 pubKeyHash) : base(pubKeyHash)
         {
-            Contract.Requires<ArgumentException>(hashBytes.Length == 20, "Your data doesn't seem like a hash160 of some value.");
+        }
 
-            _hashBytes = hashBytes;
+        public CKeyID(byte[] hashBytes) : base(hashBytes)
+        {
         }
 
+        /// <summary>
+        /// Generate Pay-to-PubkeyHash address
+        /// </summary>
+        /// <returns>Base58 formatted novacoin address</returns>
         public override string ToString()
         {
             return (new CNovacoinAddress(this)).ToString();
index 1418a25..cd6d27a 100644 (file)
@@ -74,9 +74,9 @@ namespace Novacoin
         ///    hash_length_byte hash_bytes
         /// </summary>
         /// <param name="hash">Hash160 instance</param>
-        public void AddHash(Hash160 hash)
+        public void AddHash(uint160 hash)
         {
-            codeBytes.Add((byte)hash.hashSize);
+            codeBytes.Add((byte)hash.Size);
             codeBytes.AddRange((byte[])hash);
         }
 
@@ -86,9 +86,9 @@ namespace Novacoin
         ///    hash_length_byte hash_bytes
         /// </summary>
         /// <param name="hash">Hash256 instance</param>
-        public void AddHash(Hash256 hash)
+        public void AddHash(uint256 hash)
         {
-            codeBytes.Add((byte)hash.hashSize);
+            codeBytes.Add((byte)hash.Size);
             codeBytes.AddRange((byte[])hash);
         }
 
index bd4dea5..b9ead5a 100644 (file)
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-using System;
-using System.Diagnostics.Contracts;
-
 namespace Novacoin
 {
-    public class CScriptID : Hash160
+    /// <summary>
+    /// Represents the script identifier. Internal value is calculated as Hash160(script).
+    /// </summary>
+    public class CScriptID : uint160
     {
-        public CScriptID(Hash160 scriptHash)
+        public CScriptID() : base()
+        {
+        }
+
+        public CScriptID(CScriptID KeyID) : base(KeyID)
         {
-            _hashBytes = scriptHash;
         }
 
-        internal CScriptID(byte[] hashBytes)
+        public CScriptID(uint160 pubKeyHash) : base(pubKeyHash)
         {
-            Contract.Requires<ArgumentException>(hashBytes.Length == 20, "Your data doesn't seem like a hash160 of some value.");
+        }
 
-            _hashBytes = hashBytes;
+        public CScriptID(byte[] hashBytes) : base(hashBytes)
+        {
         }
 
+        /// <summary>
+        /// Generate Pay-to-ScriptHash address
+        /// </summary>
+        /// <returns>Base58 formatted novacoin address</returns>
         public override string ToString()
         {
             return (new CNovacoinAddress(this)).ToString();
index e5a36bf..072111f 100644 (file)
@@ -18,6 +18,7 @@
 
 
 using System;
+using System.Collections.Generic;
 using System.Diagnostics.Contracts;
 
 namespace Novacoin
@@ -25,7 +26,7 @@ namespace Novacoin
     /// <summary>
     /// Base class for uint256 and uint160.
     /// </summary>
-    public class base_uint : IComparable<base_uint>, IEquatable<base_uint>
+    public class base_uint : IComparable<base_uint>, IEquatable<base_uint>, IEqualityComparer<base_uint>
     {
         protected int nWidth;
         protected uint[] pn;
@@ -54,11 +55,14 @@ namespace Novacoin
             return pn[0];
         }
 
+        /// <summary>
+        /// Total size in bytes.
+        /// </summary>
         public int Size
         {
             get
             {
-                return nWidth;
+                return nWidth * sizeof(uint);
             }
         }
 
@@ -98,6 +102,7 @@ namespace Novacoin
         }
 
 
+        #region Comparison operations
         public static bool operator <(base_uint a, base_uint b)
         {
             for (int i = a.nWidth - 1; i >= 0; i--)
@@ -161,7 +166,9 @@ namespace Novacoin
             }
             return true;
         }
+        #endregion
 
+        #region Equality operators
         public static bool operator ==(base_uint a, base_uint b)
         {
             if (object.ReferenceEquals(a, b))
@@ -210,21 +217,38 @@ namespace Novacoin
         {
             return (!(a == b));
         }
+        #endregion
 
+        #region Cast oerations
+        /// <summary>
+        /// True cast operator
+        /// </summary>
+        /// <param name="a"></param>
+        /// <returns></returns>
         public static bool operator true(base_uint a)
         {
             return (a != 0);
         }
 
+        /// <summary>
+        /// False cast operator.
+        /// </summary>
+        /// <param name="a">Value</param>
+        /// <returns>Boolean result</returns>
         public static bool operator false(base_uint a)
         {
             return (a == 0);
         }
 
+        /// <summary>
+        /// Imlicit byte[] cast operator.
+        /// </summary>
+        /// <param name="a">Value</param>
         public static implicit operator byte[] (base_uint a)
         {
             return Interop.LEBytes(a.pn);
         }
+        #endregion
 
         private static bool ArraysEqual(uint[] a, uint[] b)
         {
@@ -240,19 +264,26 @@ namespace Novacoin
             return true;
         }
 
-        public override int GetHashCode()
+
+        #region IEqualityComparer
+        public bool Equals(base_uint a, base_uint b)
         {
-            int hash = 17;
-            unchecked
+            if (object.ReferenceEquals(a, b))
             {
-                foreach (var element in pn)
-                {
-                    hash = hash * 31 + element.GetHashCode();
-                }
+                return true;
             }
-            return hash;
+
+            return ArraysEqual(a.pn, b.pn);
         }
 
+        public int GetHashCode(base_uint a)
+        {
+            return a.GetHashCode();
+        }
+
+        #endregion
+
+        #region IComparable
         public int CompareTo(base_uint item)
         {
             if (this > item)
@@ -266,7 +297,9 @@ namespace Novacoin
 
             return 0;
         }
+        #endregion
 
+        #region IEquatable
         public bool Equals(base_uint a)
         {
             if (a == null)
@@ -277,10 +310,24 @@ namespace Novacoin
             return ArraysEqual(pn, a.pn);
         }
 
+        public override int GetHashCode()
+        {
+            int hash = 17;
+            unchecked
+            {
+                foreach (var element in pn)
+                {
+                    hash = hash * 31 + element.GetHashCode();
+                }
+            }
+            return hash;
+        }
+
         public override bool Equals(object o)
         {
             return Equals(o as base_uint);
         }
+        #endregion
 
         public override string ToString()
         {