--- /dev/null
+\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;
+ }
+
+
+ }
+}
{
_hashBytes = pubKeyHash.hashBytes;
}
+
+ public override string ToString()
+ {
+ return (new CNovacoinAddress(this)).ToString();
+ }
}
}
--- /dev/null
+\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());
+ }
+ }
+}
{
public class CScriptID : Hash160
{
+ public CScriptID(Hash160 scriptHash)
+ {
+ _hashBytes = scriptHash.hashBytes;
+ }
+
+ public override string ToString()
+ {
+ return (new CNovacoinAddress(this)).ToString();
+ }
+
}
}
<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" />
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
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