Chain trust score computation.
[NovacoinLibrary.git] / Novacoin / Interop.cs
index 9f1b425..55979d7 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,84 +53,50 @@ 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)
+        /// <summary>
+        /// Reverse byte array
+        /// </summary>
+        /// <param name="source">Source array</param>
+        /// <returns>Result array</returns>
+        public static byte[] ReverseBytes(byte[] source)
         {
-            var resultBytes = BitConverter.GetBytes(n);
-
-            Array.Reverse(resultBytes);
-
-            return resultBytes;
-        }
+            var b = new byte[source.Length];
 
-        public static byte[] BEBytes(ulong n)
-        {
-            var resultBytes = BitConverter.GetBytes(n);
+            source.CopyTo(b, 0);
 
-            Array.Reverse(resultBytes);
+            Array.Reverse(b);
 
-            return resultBytes;
+            return b;
         }
 
-
-        public static ushort BEBytesToUInt16(byte[] bytes)
+        public static byte[] HexToArray(string hex)
         {
-            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);
-        }
+            int nChars = hex.Length;
+            var bytes = new byte[nChars / 2];
 
-        public static uint BEBytesToUInt32(byte[] bytes)
-        {
-            if (bytes.Length != sizeof(uint))
+            for (int i = 0; i < nChars; i += 2)
             {
-                throw new InteropException("Array size doesn't match the uint data type.");
+                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
             }
 
-            Array.Reverse(bytes);
-
-            return BitConverter.ToUInt32(bytes, 0);
+            return bytes;
         }
 
-        public static ulong BEBytesToUInt64(byte[] bytes)
+        public static byte[] TrimArray(byte[] bytes)
         {
-            if (bytes.Length != sizeof(ulong))
+            int trimStart = bytes.Length - 1;
+            while (trimStart >= 0 && bytes[trimStart] == 0)
             {
-                throw new InteropException("Array size doesn't match the ulong data type.");
+                trimStart--;
             }
 
-            Array.Reverse(bytes);
+            byte[] result = new byte[trimStart + 1];
+            Array.Copy(bytes, 0, result, 0, trimStart + 1);
 
-            return BitConverter.ToUInt64(bytes, 0);
-        }
-
-        public static byte[] HexToArray(string hex)
-        {
-            int nChars = hex.Length;
-            var bytes = new byte[nChars / 2];
-
-            for (int i = 0; i < nChars; i += 2)
-            {
-                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
-            }
-
-            return bytes;
+            return result;
         }
 
-        public static string ToHex(IEnumerable<byte> bytes)
+        public static string ToHex(byte[] bytes)
         {
             var sb = new StringBuilder();
             foreach (var b in bytes)
@@ -154,5 +105,10 @@ namespace Novacoin
             }
             return sb.ToString();
         }
+
+        public static uint GetTime()
+        {
+            return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
+        }
     }
 }