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;
22 using System.Diagnostics.Contracts;
27 /// Available address types
33 PUBKEY_ADDRESS_TEST = 111,
34 SCRIPT_ADDRESS_TEST = 196
38 /// Represents novacoin address
40 public class CNovacoinAddress
42 private byte nVersion;
43 private byte[] addrData = new byte[20];
46 /// Initialize with custom data and version
48 /// <param name="nVersionIn"></param>
49 /// <param name="addrDataIn"></param>
50 public CNovacoinAddress(byte nVersionIn, byte[] addrDataIn)
52 Contract.Requires<ArgumentException>(addrDataIn.Length == 20, "Your data doesn't look like a valid hash160 of some value.");
54 nVersion = nVersionIn;
55 addrDataIn.CopyTo(addrData, 0);
59 /// Initialize new instance of PUBKEY_ADDRESS
61 /// <param name="keyID">CKeyID instance</param>
62 public CNovacoinAddress(CKeyID keyID)
64 nVersion = (byte)AddrType.PUBKEY_ADDRESS;
69 /// Initialize new instance of SCRIPT_ADDRESS
71 /// <param name="keyID">CScriptID instance</param>
72 public CNovacoinAddress(CScriptID scriptID)
74 nVersion = (byte)AddrType.SCRIPT_ADDRESS;
79 /// Initialize new instance from base58 string
81 /// <param name="strNovacoinAddress"></param>
82 public CNovacoinAddress(string strNovacoinAddress)
84 var RawAddrData = AddressTools.Base58DecodeCheck(strNovacoinAddress);
86 if (RawAddrData.Length != 21)
88 throw new ArgumentException("Though you have provided a correct Base58 representation of some data, this data doesn't represent a valid Novacoin address.");
91 nVersion = RawAddrData[0];
92 Array.Copy(RawAddrData, 1, addrData, 0, 20);
96 /// 20 bytes, Hash160 of script or public key
98 public static implicit operator byte[](CNovacoinAddress addr)
100 return addr.addrData;
104 /// Basic sanity checking
110 // Address data is normally generated by RIPEMD-160 hash function
111 int nExpectedSize = 20;
113 switch ((AddrType)nVersion)
115 case AddrType.PUBKEY_ADDRESS:
116 nExpectedSize = 20; // Hash of public key
118 case AddrType.SCRIPT_ADDRESS:
119 nExpectedSize = 20; // Hash of CScript
121 case AddrType.PUBKEY_ADDRESS_TEST:
124 case AddrType.SCRIPT_ADDRESS_TEST:
131 return addrData.Length == nExpectedSize;
136 /// Generate base58 serialized representation of novacoin address
138 /// <returns>Base58(data + checksum)</returns>
139 public override string ToString()
141 var r = new List<byte>();
144 r.AddRange(addrData);
146 return AddressTools.Base58EncodeCheck(r.ToArray());