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, IEnumerable<byte> addrDataIn)
50 nVersion = nVersionIn;
51 addrData = addrDataIn.ToList();
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>(keyID.hashBytes);
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>(scriptID.hashBytes);
83 /// Basic sanity checking
89 // Address data is normally generated by RIPEMD-160 hash function
90 int nExpectedSize = 20;
92 switch ((AddrType)nVersion)
94 case AddrType.PUBKEY_ADDRESS:
95 nExpectedSize = 20; // Hash of public key
97 case AddrType.SCRIPT_ADDRESS:
98 nExpectedSize = 20; // Hash of CScript
100 case AddrType.PUBKEY_ADDRESS_TEST:
103 case AddrType.SCRIPT_ADDRESS_TEST:
110 return addrData.Count == nExpectedSize;
115 /// Generate base58 serialized representation of novacoin address
117 /// <returns>Base58(data + checksum)</returns>
118 public override string ToString()
120 List<byte> r = new List<byte>();
123 r.AddRange(addrData);
125 return AddressTools.Base58EncodeCheck(r);