Complete implementation of uint256 and uint160 classes.
[NovacoinLibrary.git] / Novacoin / Interop.cs
index 9f1b425..0cb7e75 100644 (file)
@@ -22,36 +22,16 @@ using System.Text;
 
 namespace Novacoin
 {
-    public class InteropException : Exception
-    {
-        public InteropException()
-        {
-        }
-
-        public InteropException(string message)
-            : base(message)
-        {
-        }
-
-        public InteropException(string message, Exception inner)
-            : base(message, inner)
-        {
-        }
-    }
-
+    /// <summary>
+    /// Miscellaneous functions
+    /// </summary>
     public class Interop
     {
-        public static byte[] ReverseBytes(byte[] source)
-        {
-            var b = new byte[source.Length];
-
-            source.CopyTo(b, 0);
-
-            Array.Reverse(b);
-
-            return b;
-        }
-
+        /// <summary>
+        /// Convert array of unsigned integers to array of bytes.
+        /// </summary>
+        /// <param name="values">Array of unsigned integer values.</param>
+        /// <returns>Byte array</returns>
         public static byte[] LEBytes(uint[] values)
         {
             var result = new byte[values.Length * sizeof(uint)];
@@ -60,6 +40,11 @@ namespace Novacoin
             return result;
         }
 
+        /// <summary>
+        /// Convert byte array to array of unsigned integers.
+        /// </summary>
+        /// <param name="bytes">Byte array.</param>
+        /// <returns>Array of integers</returns>
         public static uint[] ToUInt32Array(byte[] bytes)
         {
             var result = new uint[bytes.Length / sizeof(uint)];
@@ -68,68 +53,20 @@ namespace Novacoin
             return result;
         }
 
-        public static byte[] BEBytes(ushort n)
-        {
-            var resultBytes = BitConverter.GetBytes(n);
-
-            Array.Reverse(resultBytes);
-
-            return resultBytes;
-        }
-
-        public static byte[] BEBytes(uint n)
-        {
-            var resultBytes = BitConverter.GetBytes(n);
-
-            Array.Reverse(resultBytes);
-
-            return resultBytes;
-        }
-
-        public static byte[] BEBytes(ulong n)
-        {
-            var resultBytes = BitConverter.GetBytes(n);
-
-            Array.Reverse(resultBytes);
-
-            return resultBytes;
-        }
-
-
-        public static ushort BEBytesToUInt16(byte[] bytes)
-        {
-            if (bytes.Length != sizeof(ushort))
-            {
-                throw new InteropException("Array size doesn't match the ushort data type.");
-            }
-
-            Array.Reverse(bytes);
-
-            return BitConverter.ToUInt16(bytes, 0);
-        }
-
-        public static uint BEBytesToUInt32(byte[] bytes)
+        /// <summary>
+        /// Reverse byte array
+        /// </summary>
+        /// <param name="source">Source array</param>
+        /// <returns>Result array</returns>
+        public static byte[] ReverseBytes(byte[] source)
         {
-            if (bytes.Length != sizeof(uint))
-            {
-                throw new InteropException("Array size doesn't match the uint data type.");
-            }
-
-            Array.Reverse(bytes);
-
-            return BitConverter.ToUInt32(bytes, 0);
-        }
+            var b = new byte[source.Length];
 
-        public static ulong BEBytesToUInt64(byte[] bytes)
-        {
-            if (bytes.Length != sizeof(ulong))
-            {
-                throw new InteropException("Array size doesn't match the ulong data type.");
-            }
+            source.CopyTo(b, 0);
 
-            Array.Reverse(bytes);
+            Array.Reverse(b);
 
-            return BitConverter.ToUInt64(bytes, 0);
+            return b;
         }
 
         public static byte[] HexToArray(string hex)
@@ -145,7 +82,7 @@ namespace Novacoin
             return bytes;
         }
 
-        public static string ToHex(IEnumerable<byte> bytes)
+        public static string ToHex(byte[] bytes)
         {
             var sb = new StringBuilder();
             foreach (var b in bytes)
@@ -154,5 +91,10 @@ namespace Novacoin
             }
             return sb.ToString();
         }
+
+        public static uint GetTime()
+        {
+            return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
+        }
     }
 }