Add license header.
[NovacoinLibrary.git] / Novacoin / CNovacoinAddress.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
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.
9
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.
14
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/>.
17  */
18
19 using System.Collections.Generic;
20 using System.Linq;
21
22 namespace Novacoin
23 {
24     /// <summary>
25     /// Available address types
26     /// </summary>
27     public enum AddrType
28     {
29         PUBKEY_ADDRESS = 8,
30         SCRIPT_ADDRESS = 20,
31         PUBKEY_ADDRESS_TEST = 111,
32         SCRIPT_ADDRESS_TEST = 196
33     };
34
35     /// <summary>
36     /// Represents novacoin address
37     /// </summary>
38     public class CNovacoinAddress
39     {
40         private byte nVersion;
41         private List<byte> addrData;
42
43         /// <summary>
44         /// Initialize with custom data and version
45         /// </summary>
46         /// <param name="nVersionIn"></param>
47         /// <param name="addrDataIn"></param>
48         public CNovacoinAddress(byte nVersionIn, IEnumerable<byte> addrDataIn)
49         {
50             nVersion = nVersionIn;
51             addrData = addrDataIn.ToList();
52         }
53
54         /// <summary>
55         /// Initialize new instance of PUBKEY_ADDRESS
56         /// </summary>
57         /// <param name="keyID">CKeyID instance</param>
58         public CNovacoinAddress(CKeyID keyID)
59         {
60             nVersion = (byte)AddrType.PUBKEY_ADDRESS;
61             addrData = new List<byte>(keyID.hashBytes);
62         }
63
64         public CNovacoinAddress(string strNovacoinAddress)
65         {
66             addrData = AddressTools.Base58DecodeCheck(strNovacoinAddress).ToList();
67
68             nVersion = addrData[0];
69             addrData.RemoveAt(0);
70         }
71
72         /// <summary>
73         /// Initialize new instance of SCRIPT_ADDRESS
74         /// </summary>
75         /// <param name="keyID">CScriptID instance</param>
76         public CNovacoinAddress(CScriptID scriptID)
77         {
78             nVersion = (byte)AddrType.SCRIPT_ADDRESS;
79             addrData = new List<byte>(scriptID.hashBytes);
80         }
81
82         /// <summary>
83         /// Basic sanity checking
84         /// </summary>
85         public bool IsValid
86         {
87             get
88             {
89                 // Address data is normally generated by RIPEMD-160 hash function
90                 int nExpectedSize = 20;
91
92                 switch ((AddrType)nVersion)
93                 {
94                     case AddrType.PUBKEY_ADDRESS:
95                         nExpectedSize = 20; // Hash of public key
96                         break;
97                     case AddrType.SCRIPT_ADDRESS:
98                         nExpectedSize = 20; // Hash of CScript
99                         break;
100                     case AddrType.PUBKEY_ADDRESS_TEST:
101                         nExpectedSize = 20;
102                         break;
103                     case AddrType.SCRIPT_ADDRESS_TEST:
104                         nExpectedSize = 20;
105                         break;
106                     default:
107                         return false;
108                 }
109
110                 return addrData.Count == nExpectedSize;
111             }
112         }
113
114         /// <summary>
115         /// Generate base58 serialized representation of novacoin address
116         /// </summary>
117         /// <returns>Base58(data + checksum)</returns>
118         public override string ToString()
119         {
120             List<byte> r = new List<byte>();
121
122             r.Add(nVersion);
123             r.AddRange(addrData);
124
125             return AddressTools.Base58EncodeCheck(r);
126         }
127     }
128 }