Checkpoints valudation, PoW reward calculation and continue working on block index.
[NovacoinLibrary.git] / Novacoin / AddressTools.cs
index 9356305..b740987 100644 (file)
@@ -1,14 +1,12 @@
 \feffusing System;
-using System.Collections.Generic;
 using System.Linq;
 using System.Text;
-using System.Text.RegularExpressions;
-using System.Threading.Tasks;
 
-using System.Numerics;
+using Org.BouncyCastle.Math;
 
 namespace Novacoin
 {
+    [Serializable]
     public class Base58Exception : Exception
     {
         public Base58Exception()
@@ -26,56 +24,50 @@ namespace Novacoin
         }
     }
 
-
     public class AddressTools
     {
-        const string strDigits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
+        private const string strDigits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
+        private static readonly BigInteger _base = BigInteger.ValueOf(58);
 
         /// <summary>
         /// Encode a byte sequence as a base58-encoded string
         /// </summary>
         /// <param name="bytes">Byte sequence</param>
         /// <returns>Encoding result</returns>
-        public static string Base58Encode(byte[] bytes)
+        public static string Base58Encode(byte[] input)
         {
-            string strResult = "";
-
-            int nBytes = bytes.Length;
-            BigInteger arrayToInt = 0;
-            BigInteger encodeSize = strDigits.Length;
-
-            for (int i = 0; i < nBytes; ++i)
-            {
-                arrayToInt = arrayToInt * 256 + bytes[i];
-            }
-            while (arrayToInt > 0)
+            // TODO: This could be a lot more efficient.
+            var bi = new BigInteger(1, input);
+            var s = new StringBuilder();
+            while (bi.CompareTo(_base) >= 0)
             {
-                int rem = (int)(arrayToInt % encodeSize);
-                arrayToInt /= encodeSize;
-                strResult = strDigits[rem] + strResult;
+                var mod = bi.Mod(_base);
+                s.Insert(0, new[] { strDigits[mod.IntValue] });
+                bi = bi.Subtract(mod).Divide(_base);
             }
-
-            // Leading zeroes encoded as base58 zeros
-            for (int i = 0; i < nBytes && bytes[i] == 0; ++i)
+            s.Insert(0, new[] { strDigits[bi.IntValue] });
+            // Convert leading zeros too.
+            foreach (var anInput in input)
             {
-                strResult = strDigits[0] + strResult;
+                if (anInput == 0)
+                    s.Insert(0, new[] { strDigits[0] });
+                else
+                    break;
             }
-
-            return strResult;
+            return s.ToString();
         }
 
+
         /// <summary>
         /// Encode a byte sequence to a base58-encoded string, including checksum
         /// </summary>
         /// <param name="bytes">Byte sequence</param>
         /// <returns>Base58(data+checksum)</returns>
-        public static string Base58EncodeCheck(IEnumerable<byte> bytes)
+        public static string Base58EncodeCheck(byte[] bytes)
         {
-            byte[] dataBytes = bytes.ToArray();
-            Array.Resize(ref dataBytes, dataBytes.Length + 4);
-
-            byte[] checkSum = Hash256.Compute256(bytes).hashBytes.Take(4).ToArray();
-
+            var dataBytes = new byte[bytes.Length + 4];
+            bytes.CopyTo(dataBytes, 0);
+            var checkSum = CryptoUtils.ComputeHash256(bytes).Take(4).ToArray();
             checkSum.CopyTo(dataBytes, dataBytes.Length - 4); // add 4-byte hash check to the end
 
             return Base58Encode(dataBytes);
@@ -86,48 +78,57 @@ namespace Novacoin
         /// </summary>
         /// <param name="strBase58">Base58 data string</param>
         /// <returns>Byte array</returns>
-        public static IEnumerable<byte> Base58Decode(string strBase58)
+        public static byte[] Base58Decode(string input)
         {
-            // Remove whitespaces
-            strBase58 = Regex.Replace(strBase58, @"s", "");
-
-            BigInteger intData = 0;
-            for (int i = 0; i < strBase58.Length; i++)
+            var bytes = DecodeToBigInteger(input).ToByteArray();
+            // We may have got one more byte than we wanted, if the high bit of the next-to-last byte was not zero. This
+            // is because BigIntegers are represented with twos-compliment notation, thus if the high bit of the last
+            // byte happens to be 1 another 8 zero bits will be added to ensure the number parses as positive. Detect
+            // that case here and chop it off.
+            var stripSignByte = bytes.Length > 1 && bytes[0] == 0 && bytes[1] >= 0x80;
+            // Count the leading zeros, if any.
+            var leadingZeros = 0;
+            for (var i = 0; input[i] == strDigits[0]; i++)
             {
-                int digit = strDigits.IndexOf(strBase58[i]);
+                leadingZeros++;
+            }
+            var tmp = new byte[bytes.Length - (stripSignByte ? 1 : 0) + leadingZeros];
+            Array.Copy(bytes, stripSignByte ? 1 : 0, tmp, leadingZeros, tmp.Length - leadingZeros);
+            return tmp;
+        }
 
-                if (digit < 0)
+        public static BigInteger DecodeToBigInteger(string input)
+        {
+            var bi = BigInteger.ValueOf(0);
+            // Work backwards through the string.
+            for (var i = input.Length - 1; i >= 0; i--)
+            {
+                var alphaIndex = strDigits.IndexOf(input[i]);
+                if (alphaIndex == -1)
                 {
-                    throw new FormatException(string.Format("Invalid Base58 character `{0}` at position {1}", strBase58[i], i));
+                    throw new FormatException("Illegal character " + input[i] + " at " + i);
                 }
-
-                intData = intData * 58 + digit;
+                bi = bi.Add(BigInteger.ValueOf(alphaIndex).Multiply(_base.Pow(input.Length - 1 - i)));
             }
-
-            // Leading zero bytes get encoded as leading `1` characters
-            int leadingZeroCount = strBase58.TakeWhile(c => c == '1').Count();
-
-            IEnumerable<byte> leadingZeros = Enumerable.Repeat((byte)0, leadingZeroCount);
-            IEnumerable<byte> bytesWithoutLeadingZeros = intData.ToByteArray().Reverse().SkipWhile(b => b == 0);
-
-            return leadingZeros.Concat(bytesWithoutLeadingZeros);
+            return bi;
         }
-        public static IEnumerable<byte> Base58DecodeCheck(string strBase58Check)
+
+        public static byte[] Base58DecodeCheck(string strBase58Check)
         {
-            byte[] rawData = Base58Decode(strBase58Check).ToArray();
+            var rawData = Base58Decode(strBase58Check).ToArray();
 
             if (rawData.Length < 4)
             {
                 throw new Base58Exception("Data is too short.");
             }
 
-            byte[] result = new byte[rawData.Length - 4];
-            byte[] resultCheckSum = new byte[4];
+            var result = new byte[rawData.Length - 4];
+            var resultCheckSum = new byte[4];
 
             Array.Copy(rawData, result, result.Length);
             Array.Copy(rawData, result.Length, resultCheckSum, 0, 4);
 
-            byte[] checkSum = Hash256.Compute256(result).hashBytes.Take(4).ToArray();
+            var checkSum = CryptoUtils.ComputeHash256(result).Take(4).ToArray();
 
             if (!checkSum.SequenceEqual(resultCheckSum))
             {