TrimArray() simplification
[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 using System;
22 using System.Diagnostics.Contracts;
23
24 namespace Novacoin
25 {
26     /// <summary>
27     /// Available address types
28     /// </summary>
29     public enum AddrType
30     {
31         PUBKEY_ADDRESS = 8,
32         SCRIPT_ADDRESS = 20,
33         PUBKEY_ADDRESS_TEST = 111,
34         SCRIPT_ADDRESS_TEST = 196
35     };
36
37     /// <summary>
38     /// Represents novacoin address
39     /// </summary>
40     public class CNovacoinAddress
41     {
42         private byte nVersion;
43         private byte[] addrData = new byte[20];
44
45         /// <summary>
46         /// Initialize with custom data and version
47         /// </summary>
48         /// <param name="nVersionIn"></param>
49         /// <param name="addrDataIn"></param>
50         public CNovacoinAddress(byte nVersionIn, byte[] addrDataIn)
51         {
52             Contract.Requires<ArgumentException>(addrDataIn.Length == 20, "Your data doesn't look like a valid hash160 of some value.");
53
54             nVersion = nVersionIn;
55             addrDataIn.CopyTo(addrData, 0);
56         }
57
58         /// <summary>
59         /// Initialize new instance of PUBKEY_ADDRESS
60         /// </summary>
61         /// <param name="keyID">CKeyID instance</param>
62         public CNovacoinAddress(CKeyID keyID)
63         {
64             nVersion = (byte)AddrType.PUBKEY_ADDRESS;
65             addrData = keyID;
66         }
67
68         /// <summary>
69         /// Initialize new instance of SCRIPT_ADDRESS
70         /// </summary>
71         /// <param name="keyID">CScriptID instance</param>
72         public CNovacoinAddress(CScriptID scriptID)
73         {
74             nVersion = (byte)AddrType.SCRIPT_ADDRESS;
75             addrData = scriptID;
76         }
77
78         /// <summary>
79         /// Initialize new instance from base58 string
80         /// </summary>
81         /// <param name="strNovacoinAddress"></param>
82         public CNovacoinAddress(string strNovacoinAddress)
83         {
84             var RawAddrData = AddressTools.Base58DecodeCheck(strNovacoinAddress);
85
86             if (RawAddrData.Length != 21)
87             {
88                 throw new ArgumentException("Though you have provided a correct Base58 representation of some data, this data doesn't represent a valid Novacoin address.");
89             }
90
91             nVersion = RawAddrData[0];
92             Array.Copy(RawAddrData, 1, addrData, 0, 20);
93         }
94
95         /// <summary>
96         /// 20 bytes, Hash160 of script or public key
97         /// </summary>
98         public static implicit operator byte[](CNovacoinAddress addr)
99         {
100             return addr.addrData;
101         }
102
103         /// <summary>
104         /// Basic sanity checking
105         /// </summary>
106         public bool IsValid
107         {
108             get
109             {
110                 // Address data is normally generated by RIPEMD-160 hash function
111                 int nExpectedSize = 20;
112
113                 switch ((AddrType)nVersion)
114                 {
115                     case AddrType.PUBKEY_ADDRESS:
116                         nExpectedSize = 20; // Hash of public key
117                         break;
118                     case AddrType.SCRIPT_ADDRESS:
119                         nExpectedSize = 20; // Hash of CScript
120                         break;
121                     case AddrType.PUBKEY_ADDRESS_TEST:
122                         nExpectedSize = 20;
123                         break;
124                     case AddrType.SCRIPT_ADDRESS_TEST:
125                         nExpectedSize = 20;
126                         break;
127                     default:
128                         return false;
129                 }
130
131                 return addrData.Length == nExpectedSize;
132             }
133         }
134
135         /// <summary>
136         /// Generate base58 serialized representation of novacoin address
137         /// </summary>
138         /// <returns>Base58(data + checksum)</returns>
139         public override string ToString()
140         {
141             var r = new List<byte>();
142
143             r.Add(nVersion);
144             r.AddRange(addrData);
145
146             return AddressTools.Base58EncodeCheck(r.ToArray());
147         }
148     }
149 }