New constructor for block header class
[NovacoinLibrary.git] / Novacoin / CPubKey.cs
1 \feffusing System.Collections.Generic;
2 using System.Linq;
3 using Org.BouncyCastle.Math.EC;
4 using Org.BouncyCastle.Crypto.Parameters;
5
6 namespace Novacoin
7 {
8     /// <summary>
9     /// Representation of ECDSA public key
10     /// </summary>
11     public class CPubKey : CKey
12     {
13         /// <summary>
14         /// Initializes a new instance of CPubKey class as the copy of another instance
15         /// </summary>
16         /// <param name="pubKey">Another CPubKey instance</param>
17         public CPubKey(CPubKey pubKey)
18         {
19             _Public = pubKey._Public;
20         }
21
22         /// <summary>
23         /// Initializes a new instance of CPubKey class using supplied sequence of bytes
24         /// </summary>
25         /// <param name="bytes">Byte sequence</param>
26         public CPubKey(IEnumerable<byte> bytes)
27         {
28             ECPoint pQ = curve.Curve.DecodePoint(bytes.ToArray());
29             _Public = new ECPublicKeyParameters(pQ, domain);
30         }
31
32         /// <summary>
33         /// Init with base58 encoded sequence of bytes
34         /// </summary>
35         /// <param name="strBase58"></param>
36         public CPubKey(string strBase58)
37         {
38             ECPoint pQ = curve.Curve.DecodePoint(AddressTools.Base58DecodeCheck(strBase58).ToArray());
39             _Public = new ECPublicKeyParameters(pQ, domain);
40         }
41
42         /// <summary>
43         /// Quick validity test
44         /// </summary>
45         /// <returns>Validation result</returns>
46         public bool IsValid
47         {
48             get { return !_Public.Q.IsInfinity; }
49         }
50
51         public string ToHex()
52         {
53             return Interop.ToHex(PublicBytes);
54         }
55
56         public override string ToString()
57         {
58             List<byte> r = new List<byte>();
59
60             r.Add((byte)(AddrType.PUBKEY_ADDRESS));
61             r.AddRange(PublicBytes);
62
63             return AddressTools.Base58EncodeCheck(r);
64         }
65     }
66 }