Add license header.
[NovacoinLibrary.git] / Novacoin / CPubKey.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 Org.BouncyCastle.Math.EC;
22 using Org.BouncyCastle.Crypto.Parameters;
23
24 namespace Novacoin
25 {
26     /// <summary>
27     /// Representation of ECDSA public key
28     /// </summary>
29     public class CPubKey : CKey
30     {
31         /// <summary>
32         /// Initializes a new instance of CPubKey class as the copy of another instance
33         /// </summary>
34         /// <param name="pubKey">Another CPubKey instance</param>
35         public CPubKey(CPubKey pubKey)
36         {
37             _Public = pubKey._Public;
38         }
39
40         /// <summary>
41         /// Initializes a new instance of CPubKey class using supplied sequence of bytes
42         /// </summary>
43         /// <param name="bytes">Byte sequence</param>
44         public CPubKey(IEnumerable<byte> bytes)
45         {
46             ECPoint pQ = curve.Curve.DecodePoint(bytes.ToArray());
47             _Public = new ECPublicKeyParameters(pQ, domain);
48         }
49
50         /// <summary>
51         /// Init with base58 encoded sequence of bytes
52         /// </summary>
53         /// <param name="strBase58"></param>
54         public CPubKey(string strBase58)
55         {
56             ECPoint pQ = curve.Curve.DecodePoint(AddressTools.Base58DecodeCheck(strBase58).ToArray());
57             _Public = new ECPublicKeyParameters(pQ, domain);
58         }
59
60         /// <summary>
61         /// Quick validity test
62         /// </summary>
63         /// <returns>Validation result</returns>
64         public bool IsValid
65         {
66             get { return !_Public.Q.IsInfinity; }
67         }
68
69         public string ToHex()
70         {
71             return Interop.ToHex(PublicBytes);
72         }
73
74         public override string ToString()
75         {
76             List<byte> r = new List<byte>();
77
78             r.Add((byte)(AddrType.PUBKEY_ADDRESS));
79             r.AddRange(PublicBytes);
80
81             return AddressTools.Base58EncodeCheck(r);
82         }
83     }
84 }