Novacoin address generation
authorCryptoManiac <balthazar@yandex.ru>
Tue, 18 Aug 2015 18:18:24 +0000 (21:18 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Tue, 18 Aug 2015 18:18:24 +0000 (21:18 +0300)
Novacoin/AddressTools.cs [new file with mode: 0644]
Novacoin/CKeyID.cs
Novacoin/CNovacoinAddress.cs [new file with mode: 0644]
Novacoin/CScriptID.cs
Novacoin/Novacoin.csproj
NovacoinTest/Program.cs

diff --git a/Novacoin/AddressTools.cs b/Novacoin/AddressTools.cs
new file mode 100644 (file)
index 0000000..766ae5c
--- /dev/null
@@ -0,0 +1,42 @@
+\feffusing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using System.Numerics;
+
+namespace Novacoin
+{
+    public class AddressTools
+    {
+        public static string Base58Encode(byte[] bytes)
+        {
+            const string strDigits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
+            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)
+            {
+                int rem = (int)(arrayToInt % encodeSize);
+                arrayToInt /= encodeSize;
+                strResult = strDigits[rem] + strResult;
+            }
+            for (int i = 0; i < nBytes && bytes[i] == 0; ++i)
+            {
+                strResult = strDigits[0] + strResult;
+            }
+
+            return strResult;
+        }
+
+
+    }
+}
index ea09f65..a021d83 100644 (file)
@@ -12,5 +12,10 @@ namespace Novacoin
         {
             _hashBytes = pubKeyHash.hashBytes;
         }
+
+        public override string ToString()
+        {
+            return (new CNovacoinAddress(this)).ToString();
+        }
     }
 }
diff --git a/Novacoin/CNovacoinAddress.cs b/Novacoin/CNovacoinAddress.cs
new file mode 100644 (file)
index 0000000..5ff3d28
--- /dev/null
@@ -0,0 +1,87 @@
+\feffusing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Novacoin
+{
+    public enum AddrType
+    {
+        PUBKEY_ADDRESS = 8,
+        SCRIPT_ADDRESS = 20,
+        PUBKEY_ADDRESS_TEST = 111,
+        SCRIPT_ADDRESS_TEST = 196
+    };
+
+    public class CNovacoinAddress
+    {
+        private byte nVersion;
+        private List<byte> addrData;
+
+        public CNovacoinAddress(byte nVersionIn, IEnumerable<byte> addrDataIn)
+        {
+            nVersion = nVersionIn;
+            addrData = addrDataIn.ToList();
+        }
+
+        public CNovacoinAddress(CKeyID keyID)
+        {
+            nVersion = (byte)AddrType.PUBKEY_ADDRESS;
+            addrData = new List<byte>(keyID.hashBytes);
+        }
+
+        public CNovacoinAddress(CScriptID scriptID)
+        {
+            nVersion = (byte)AddrType.SCRIPT_ADDRESS;
+            addrData = new List<byte>(scriptID.hashBytes);
+        }
+
+        public static byte[] ConcatAddress(byte[] RipeHash, byte[] Checksum)
+        {
+            byte[] ret = new byte[RipeHash.Length + 4];
+            Array.Copy(RipeHash, ret, RipeHash.Length);
+            Array.Copy(Checksum, 0, ret, RipeHash.Length, 4);
+            return ret;
+        }
+
+        public bool IsValid()
+        {
+            int nExpectedSize = 20;
+
+            switch ((AddrType) nVersion)
+            {
+                case AddrType.PUBKEY_ADDRESS:
+                    nExpectedSize = 20; // Hash of public key
+                    break;
+                case AddrType.SCRIPT_ADDRESS:
+                    nExpectedSize = 20; // Hash of CScript
+                    break;
+                case AddrType.PUBKEY_ADDRESS_TEST:
+                    nExpectedSize = 20;
+                    break;
+                case AddrType.SCRIPT_ADDRESS_TEST:
+                    nExpectedSize = 20;
+                    break;
+                default:
+                    return false;
+            }
+
+            return addrData.Count == nExpectedSize;
+        }
+
+        public override string ToString()
+        {
+            List<byte> r = new List<byte>();
+
+            byte[] checkSum = Hash256.Compute256(addrData).hashBytes;
+            Array.Resize(ref checkSum, 4);
+
+            r.Add(nVersion);
+            r.AddRange(addrData);
+            r.AddRange(checkSum);
+
+            return AddressTools.Base58Encode(r.ToArray());
+        }
+    }
+}
index 32030df..1f5314e 100644 (file)
@@ -8,5 +8,15 @@ namespace Novacoin
 {
     public class CScriptID : Hash160
     {
+        public CScriptID(Hash160 scriptHash)
+        {
+            _hashBytes = scriptHash.hashBytes;
+        }
+
+        public override string ToString()
+        {
+            return (new CNovacoinAddress(this)).ToString();
+        }
+
     }
 }
index 5e158c0..0053701 100644 (file)
       <Private>True</Private>
     </Reference>
     <Reference Include="System" />
+    <Reference Include="System.Numerics" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="AddressTools.cs" />
     <Compile Include="CKey.cs" />
     <Compile Include="CKeyID.cs" />
     <Compile Include="CKeyPair.cs" />
+    <Compile Include="CNovacoinAddress.cs" />
     <Compile Include="CPubKey.cs" />
     <Compile Include="CScriptID.cs" />
     <Compile Include="Hash.cs" />
index fe54e2a..645c8e7 100644 (file)
@@ -37,7 +37,7 @@ namespace NovacoinTest
             Console.WriteLine("OK: {0}\n", strBlock1 == strBlock1Bytes);
 
             Console.WriteLine(b2.ToString());
-            Console.WriteLine("OK: {0}\n", strBlock2 == strBlock2Bytes);
+            Console.WriteLine("Reserialization is OK: {0}\n", strBlock2 == strBlock2Bytes);
 
             /// ECDSA keypair generation test
 
@@ -47,7 +47,13 @@ namespace NovacoinTest
        
             Console.WriteLine(keyPair1.ToString());
             Console.WriteLine("PubKey: {0}", pubKey.ToString());
-            Console.WriteLine("OK: {0}\n", keyPair1.ToString() == keyPair2.ToString());
+            Console.WriteLine("Reinitialization is OK: {0}\n", keyPair1.ToString() == keyPair2.ToString());
+
+            /// Address generation test
+
+            CKeyID keyID = keyPair1.GetKeyID();
+            Console.WriteLine("Key ID: {0}", Interop.ToHex(keyID.hashBytes));
+            Console.WriteLine("Novacoin address: {0}\n", keyID.ToString());
 
             /// ECDSA keypair signing test