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