2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 using System.Collections.Generic;
25 /// Available address types
31 PUBKEY_ADDRESS_TEST = 111,
32 SCRIPT_ADDRESS_TEST = 196
36 /// Represents novacoin address
38 public class CNovacoinAddress
40 private byte nVersion;
41 private List<byte> addrData;
44 /// Initialize with custom data and version
46 /// <param name="nVersionIn"></param>
47 /// <param name="addrDataIn"></param>
48 public CNovacoinAddress(byte nVersionIn, byte[] addrDataIn)
50 nVersion = nVersionIn;
51 addrData = new List<byte>(addrDataIn);
55 /// Initialize new instance of PUBKEY_ADDRESS
57 /// <param name="keyID">CKeyID instance</param>
58 public CNovacoinAddress(CKeyID keyID)
60 nVersion = (byte)AddrType.PUBKEY_ADDRESS;
61 addrData = new List<byte>((byte[])keyID);
64 public CNovacoinAddress(string strNovacoinAddress)
66 addrData = AddressTools.Base58DecodeCheck(strNovacoinAddress).ToList();
68 nVersion = addrData[0];
73 /// Initialize new instance of SCRIPT_ADDRESS
75 /// <param name="keyID">CScriptID instance</param>
76 public CNovacoinAddress(CScriptID scriptID)
78 nVersion = (byte)AddrType.SCRIPT_ADDRESS;
79 addrData = new List<byte>((byte[])scriptID);
83 /// 20 bytes, Hash160 of script or public key
85 public byte[] HashBytes
87 get { return addrData.ToArray(); }
91 /// Basic sanity checking
97 // Address data is normally generated by RIPEMD-160 hash function
98 int nExpectedSize = 20;
100 switch ((AddrType)nVersion)
102 case AddrType.PUBKEY_ADDRESS:
103 nExpectedSize = 20; // Hash of public key
105 case AddrType.SCRIPT_ADDRESS:
106 nExpectedSize = 20; // Hash of CScript
108 case AddrType.PUBKEY_ADDRESS_TEST:
111 case AddrType.SCRIPT_ADDRESS_TEST:
118 return addrData.Count == nExpectedSize;
123 /// Generate base58 serialized representation of novacoin address
125 /// <returns>Base58(data + checksum)</returns>
126 public override string ToString()
128 var r = new List<byte>();
131 r.AddRange(addrData);
133 return AddressTools.Base58EncodeCheck(r.ToArray());