Chain trust score computation.
[NovacoinLibrary.git] / Novacoin / Interop.cs
index 5c6d317..55979d7 100644 (file)
@@ -28,6 +28,32 @@ namespace Novacoin
     public class Interop
     {
         /// <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)];
+            Buffer.BlockCopy(values, 0, result, 0, result.Length);
+
+            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)];
+            Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
+
+            return result;
+        }
+
+        /// <summary>
         /// Reverse byte array
         /// </summary>
         /// <param name="source">Source array</param>
@@ -56,6 +82,20 @@ namespace Novacoin
             return bytes;
         }
 
+        public static byte[] TrimArray(byte[] bytes)
+        {
+            int trimStart = bytes.Length - 1;
+            while (trimStart >= 0 && bytes[trimStart] == 0)
+            {
+                trimStart--;
+            }
+
+            byte[] result = new byte[trimStart + 1];
+            Array.Copy(bytes, 0, result, 0, trimStart + 1);
+
+            return result;
+        }
+
         public static string ToHex(byte[] bytes)
         {
             var sb = new StringBuilder();
@@ -66,9 +106,9 @@ namespace Novacoin
             return sb.ToString();
         }
 
-        public static int GetTime()
+        public static uint GetTime()
         {
-            return (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
+            return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
         }
     }
 }