VarInt class, TxIn/TXOut/Tx serializarion, some CKey and CPubKey methods
[NovacoinLibrary.git] / Novacoin / CPubKey.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Novacoin
8 {
9     /// <summary>
10     /// Representation of ECDSA public key
11     /// </summary>
12     public class CPubKey
13     {
14         /// <summary>
15         /// Public key bytes
16         /// </summary>
17         private List<byte> pubKeyBytes;
18
19         /// <summary>
20         /// Initializes a new instance of CPubKey class as the copy of another instance
21         /// </summary>
22         /// <param name="pubKey">Another CPubKey instance</param>
23         public CPubKey(CPubKey pubKey)
24         {
25             pubKeyBytes = pubKey.pubKeyBytes;
26         }
27
28         /// <summary>
29         /// Initializes a new instance of CPubKey class using supplied sequence of bytes
30         /// </summary>
31         /// <param name="bytesList"></param>
32         public CPubKey(IEnumerable<byte> bytesList)
33         {
34             pubKeyBytes = new List<byte>(bytesList);
35         }
36
37         /// <summary>
38         /// Quick validity test
39         /// </summary>
40         /// <returns>Validation result</returns>
41         public bool IsValid()
42         {
43             return pubKeyBytes.Count == 33 || pubKeyBytes.Count == 65;
44         }
45
46         /// <summary>
47         /// Is this a compressed public key?
48         /// </summary>
49         /// <returns></returns>
50         public bool IsCompressed()
51         {
52             // Compressed public keys are 33 bytes long
53             return pubKeyBytes.Count == 33;
54         }
55
56         /// <summary>
57         /// Calculate Hash160 and create new CKeyID instance.
58         /// </summary>
59         /// <returns>New key ID</returns>
60         public CKeyID GetKeyID()
61         {
62             return new CKeyID(Hash160.Compute160(this.Raw));
63         }
64
65         /// <summary>
66         /// Accessor for internal representation
67         /// </summary>
68         public IList<byte> Raw
69         {
70             get { return pubKeyBytes; }
71         }
72     }
73 }