From: CryptoManiac Date: Tue, 18 Aug 2015 18:18:24 +0000 (+0300) Subject: Novacoin address generation X-Git-Url: https://git.novaco.in/?p=NovacoinLibrary.git;a=commitdiff_plain;h=e57af964c9a6fbd14a9c4c7fc93121a4ccfac67d Novacoin address generation --- diff --git a/Novacoin/AddressTools.cs b/Novacoin/AddressTools.cs new file mode 100644 index 0000000..766ae5c --- /dev/null +++ b/Novacoin/AddressTools.cs @@ -0,0 +1,42 @@ +using 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; + } + + + } +} diff --git a/Novacoin/CKeyID.cs b/Novacoin/CKeyID.cs index ea09f65..a021d83 100644 --- a/Novacoin/CKeyID.cs +++ b/Novacoin/CKeyID.cs @@ -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 index 0000000..5ff3d28 --- /dev/null +++ b/Novacoin/CNovacoinAddress.cs @@ -0,0 +1,87 @@ +using 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 addrData; + + public CNovacoinAddress(byte nVersionIn, IEnumerable addrDataIn) + { + nVersion = nVersionIn; + addrData = addrDataIn.ToList(); + } + + public CNovacoinAddress(CKeyID keyID) + { + nVersion = (byte)AddrType.PUBKEY_ADDRESS; + addrData = new List(keyID.hashBytes); + } + + public CNovacoinAddress(CScriptID scriptID) + { + nVersion = (byte)AddrType.SCRIPT_ADDRESS; + addrData = new List(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 r = new List(); + + 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()); + } + } +} diff --git a/Novacoin/CScriptID.cs b/Novacoin/CScriptID.cs index 32030df..1f5314e 100644 --- a/Novacoin/CScriptID.cs +++ b/Novacoin/CScriptID.cs @@ -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(); + } + } } diff --git a/Novacoin/Novacoin.csproj b/Novacoin/Novacoin.csproj index 5e158c0..0053701 100644 --- a/Novacoin/Novacoin.csproj +++ b/Novacoin/Novacoin.csproj @@ -33,11 +33,14 @@ True + + + diff --git a/NovacoinTest/Program.cs b/NovacoinTest/Program.cs index fe54e2a..645c8e7 100644 --- a/NovacoinTest/Program.cs +++ b/NovacoinTest/Program.cs @@ -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