Add license header.
[NovacoinLibrary.git] / Novacoin / CKeyPair.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 Org.BouncyCastle.Crypto;
20 using Org.BouncyCastle.Crypto.Generators;
21 using Org.BouncyCastle.Crypto.Parameters;
22 using Org.BouncyCastle.Math;
23 using Org.BouncyCastle.Math.EC;
24 using Org.BouncyCastle.Security;
25 using System;
26 using System.Collections.Generic;
27 using System.Linq;
28
29
30 namespace Novacoin
31 {
32     public class CKeyPair : CKey
33     {
34         private ECPrivateKeyParameters _Private;
35
36         /// <summary>
37         /// Initialize new CKeyPair instance with random secret.
38         /// </summary>
39         public CKeyPair(bool Compressed=true)
40         {
41             ECKeyGenerationParameters genParams = new ECKeyGenerationParameters(domain, new SecureRandom());
42             ECKeyPairGenerator generator = new ECKeyPairGenerator("ECDSA");
43             generator.Init(genParams);
44             AsymmetricCipherKeyPair ecKeyPair = generator.GenerateKeyPair();
45
46             _Private = (ECPrivateKeyParameters)ecKeyPair.Private;
47             _Public = (ECPublicKeyParameters)ecKeyPair.Public;
48
49             if (Compressed)
50             {
51                 _Public = Compress(_Public);
52             }
53         }
54
55         /// <summary>
56         /// Init key pair using secret sequence of bytes
57         /// </summary>
58         /// <param name="secretBytes">Byte sequence</param>
59         /// <param name="Compressed">Compression flag</param>
60         public CKeyPair(IEnumerable<byte> secretBytes, bool Compressed=true)
61         {
62             // Deserialize secret value
63             BigInteger D = new BigInteger(secretBytes.Take(32).ToArray());
64
65             if (D.SignValue == -1)
66             {
67                 List<byte> fixedKeyBytes = secretBytes.Take(32).ToList();
68                 fixedKeyBytes.Insert(0, 0x00); // prepend with sign byte
69
70                 D = new BigInteger(fixedKeyBytes.ToArray());
71             }
72
73             // Calculate public key
74             ECPoint Q = curve.G.Multiply(D);
75
76             _Private = new ECPrivateKeyParameters(D, domain);
77             _Public = new ECPublicKeyParameters(Q, domain);
78
79             if (Compressed)
80             {
81                 _Public = Compress(_Public);
82             }
83         }
84
85         /// <summary>
86         /// Init key pair using secret sequence of bytes
87         /// </summary>
88         /// <param name="secretBytes">Byte sequence</param>
89         public CKeyPair(IEnumerable<byte> secretBytes) : 
90             this (secretBytes.Take(32), (secretBytes.Count() == 33 && secretBytes.Last() == 0x01))
91         {
92         }
93
94         public CKeyPair(string strBase58)
95         {
96             List<byte> rawBytes = AddressTools.Base58DecodeCheck(strBase58).ToList();
97             rawBytes.RemoveAt(0); // Remove key version byte
98
99             // Deserialize secret value
100             BigInteger D = new BigInteger(rawBytes.Take(32).ToArray());
101
102             if (D.SignValue == -1)
103             {
104                 List<byte> secretbytes = rawBytes.Take(32).ToList(); // Copy secret
105                 secretbytes.Insert(0, 0x00); // Prepend with sign byte
106
107                 D = new BigInteger(secretbytes.ToArray()); // Try decoding again
108             }
109
110             // Calculate public key
111             ECPoint Q = curve.G.Multiply(D);
112
113             _Private = new ECPrivateKeyParameters(D, domain);
114             _Public = new ECPublicKeyParameters(Q, domain);
115
116             if (rawBytes.Count == 33 && rawBytes.Last() == 0x01) // Check compression tag
117             {
118                 _Public = Compress(_Public);
119             }
120         }
121
122         /// <summary>
123         /// Initialize a copy of CKeyPair instance
124         /// </summary>
125         /// <param name="pair">CKyPair instance</param>
126         public CKeyPair(CKeyPair pair)
127         {
128             _Public = pair._Public;
129             _Private = pair._Private;
130         }
131
132         /// <summary>
133         /// Create signature for supplied data
134         /// </summary>
135         /// <param name="data">Hash to sigh</param>
136         /// <returns>Signature bytes sequence</returns>
137         public IEnumerable<byte> Sign(Hash sigHash)
138         {
139             ISigner signer = SignerUtilities.GetSigner("NONEwithECDSA");
140             signer.Init(true, _Private);
141             signer.BlockUpdate(sigHash.hashBytes, 0, sigHash.hashSize);
142
143             return signer.GenerateSignature();
144         }
145
146         public CPubKey PubKey
147         {
148             get { return new CPubKey(PublicBytes); }
149         }
150
151         /// <summary>
152         /// SecretBytes part of key pair
153         /// </summary>
154         public IEnumerable<byte> SecretBytes
155         {
156             get
157             {
158                 List<byte> secretBytes = new List<byte>(_Private.D.ToByteArray());
159
160                 if (secretBytes[0] == 0x00)
161                 {
162                     // Remove sign
163                     secretBytes.RemoveAt(0);
164                 }
165
166                 if (IsCompressed)
167                 {
168                     // Set compression flag
169                     secretBytes.Add(0x01);
170                 }
171
172                 return secretBytes;
173             }
174         }
175
176         public string ToHex()
177         {
178             return Interop.ToHex(SecretBytes);
179         }
180
181         public override string ToString()
182         {
183             List<byte> r = new List<byte>();
184
185             r.Add((byte)(128 + AddrType.PUBKEY_ADDRESS)); // Key version
186             r.AddRange(SecretBytes); // Key data
187
188             return AddressTools.Base58EncodeCheck(r);
189         }
190     }
191 }