From 241856f1328f7d900260976c18c29f67c7cebc80 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Wed, 2 Sep 2015 11:32:45 +0300 Subject: [PATCH] Implementation of IEquatable and IEqualityComparer, derive KeyID and ScriptID form uint160. --- Novacoin/CKeyID.cs | 26 ++++++++++++------ Novacoin/CScript.cs | 8 +++--- Novacoin/CScriptID.cs | 26 ++++++++++++------ Novacoin/base_uint.cs | 67 +++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 95 insertions(+), 32 deletions(-) diff --git a/Novacoin/CKeyID.cs b/Novacoin/CKeyID.cs index d7130a8..62639a1 100644 --- a/Novacoin/CKeyID.cs +++ b/Novacoin/CKeyID.cs @@ -16,25 +16,33 @@ * along with this program. If not, see . */ -using System; -using System.Diagnostics.Contracts; - namespace Novacoin { - public class CKeyID : Hash160 + /// + /// Represents the key identifier. Internal state is calculated as Hash160(pubkey). + /// + 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(hashBytes.Length == 20, "Your data doesn't seem like a hash160 of some value."); + } - _hashBytes = hashBytes; + public CKeyID(byte[] hashBytes) : base(hashBytes) + { } + /// + /// Generate Pay-to-PubkeyHash address + /// + /// Base58 formatted novacoin address public override string ToString() { return (new CNovacoinAddress(this)).ToString(); diff --git a/Novacoin/CScript.cs b/Novacoin/CScript.cs index 1418a25..cd6d27a 100644 --- a/Novacoin/CScript.cs +++ b/Novacoin/CScript.cs @@ -74,9 +74,9 @@ namespace Novacoin /// hash_length_byte hash_bytes /// /// Hash160 instance - 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 /// /// Hash256 instance - public void AddHash(Hash256 hash) + public void AddHash(uint256 hash) { - codeBytes.Add((byte)hash.hashSize); + codeBytes.Add((byte)hash.Size); codeBytes.AddRange((byte[])hash); } diff --git a/Novacoin/CScriptID.cs b/Novacoin/CScriptID.cs index bd4dea5..b9ead5a 100644 --- a/Novacoin/CScriptID.cs +++ b/Novacoin/CScriptID.cs @@ -16,25 +16,33 @@ * along with this program. If not, see . */ -using System; -using System.Diagnostics.Contracts; - namespace Novacoin { - public class CScriptID : Hash160 + /// + /// Represents the script identifier. Internal value is calculated as Hash160(script). + /// + 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(hashBytes.Length == 20, "Your data doesn't seem like a hash160 of some value."); + } - _hashBytes = hashBytes; + public CScriptID(byte[] hashBytes) : base(hashBytes) + { } + /// + /// Generate Pay-to-ScriptHash address + /// + /// Base58 formatted novacoin address public override string ToString() { return (new CNovacoinAddress(this)).ToString(); diff --git a/Novacoin/base_uint.cs b/Novacoin/base_uint.cs index e5a36bf..072111f 100644 --- a/Novacoin/base_uint.cs +++ b/Novacoin/base_uint.cs @@ -18,6 +18,7 @@ using System; +using System.Collections.Generic; using System.Diagnostics.Contracts; namespace Novacoin @@ -25,7 +26,7 @@ namespace Novacoin /// /// Base class for uint256 and uint160. /// - public class base_uint : IComparable, IEquatable + public class base_uint : IComparable, IEquatable, IEqualityComparer { protected int nWidth; protected uint[] pn; @@ -54,11 +55,14 @@ namespace Novacoin return pn[0]; } + /// + /// Total size in bytes. + /// 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 + /// + /// True cast operator + /// + /// + /// public static bool operator true(base_uint a) { return (a != 0); } + /// + /// False cast operator. + /// + /// Value + /// Boolean result public static bool operator false(base_uint a) { return (a == 0); } + /// + /// Imlicit byte[] cast operator. + /// + /// Value 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() { -- 1.7.1