-
Notifications
You must be signed in to change notification settings - Fork 20
/
PrivateKey.cs
357 lines (308 loc) · 13.5 KB
/
PrivateKey.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Autarkysoft.Bitcoin
// Copyright (c) 2020 Autarkysoft
// Distributed under the MIT software license, see the accompanying
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
using Autarkysoft.Bitcoin.Blockchain.Transactions;
using Autarkysoft.Bitcoin.Cryptography.Asymmetric.EllipticCurve;
using Autarkysoft.Bitcoin.Cryptography.Hashing;
using Autarkysoft.Bitcoin.Encoders;
using System;
using System.Numerics;
using System.Text;
namespace Autarkysoft.Bitcoin.Cryptography.Asymmetric.KeyPairs
{
/// <summary>
/// Implementation of bitcoin private keys.
/// Implements <see cref="IDisposable"/>.
/// </summary>
public class PrivateKey : IDisposable
{
/// <summary>
/// This constructor is only used by <see cref="MiniPrivateKey"/> as the derived class.
/// Derived classes have to call <see cref="SetBytes(byte[])"/> method with the appropriate bytes.
/// </summary>
protected PrivateKey()
{
}
/// <summary>
/// Initializes a new instance of <see cref="PrivateKey"/> using the given <see cref="BigInteger"/>.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"/>
/// <param name="big">Integer to use</param>
public PrivateKey(BigInteger big)
{
if (big.Sign < 0)
throw new ArgumentOutOfRangeException("Number value can not be negative.");
SetBytes(big.ToByteArray(true, true));
}
/// <summary>
/// Initializes a new instance of <see cref="PrivateKey"/> using the given byte array.
/// </summary>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentOutOfRangeException"/>
/// <param name="ba">Byte array to use</param>
public PrivateKey(byte[] ba)
{
SetBytes(ba);
}
/// <summary>
/// Initializes a new instance of <see cref="PrivateKey"/> using the given <see cref="IRandomNumberGenerator"/>.
/// </summary>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <param name="rng">The <see cref="IRandomNumberGenerator"/> instance to use</param>
public PrivateKey(IRandomNumberGenerator rng)
{
if (rng is null)
throw new ArgumentNullException(nameof(rng), "Random number generator can not be null.");
byte[] temp = new byte[KeyByteSize];
int count = 0;
while (count <= Constants.RngRetryCount)
{
try
{
rng.GetBytes(temp);
SetBytes(temp);
break;
}
catch (Exception)
{
count++;
// This should never happen:
if (count == Constants.RngRetryCount)
throw new ArgumentException(Err.BadRNG);
// TODO: maybe use a mod Curve.N in case bytes were bigger than N (out of range)
}
}
}
/// <summary>
/// Initializes a new instance of <see cref="PrivateKey"/> using the given WIF (Base-58 encoded private key
/// with a checksum)
/// </summary>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentOutOfRangeException"/>
/// <exception cref="FormatException"/>
/// <param name="wif">Wallet import format</param>
/// <param name="netType">
/// [Default value = <see cref="NetworkType.MainNet"/>]
/// Network type to check against (affects WIF's starting characters)
/// </param>
public PrivateKey(string wif, NetworkType netType = NetworkType.MainNet)
{
if (string.IsNullOrWhiteSpace(wif))
throw new ArgumentNullException(nameof(wif), "Input WIF can not be null or empty.");
byte[] ba = b58Encoder.DecodeWithCheckSum(wif);
if (ba[0] != GetWifFirstByte(netType))
{
throw new FormatException("Invalid first byte.");
}
if (ba.Length == KeyByteSize + 1) // Uncompressed
{
SetBytes(ba.SubArray(1));
}
else if (ba.Length == KeyByteSize + 2) // Compressed
{
if (ba[^1] != CompressedByte)
{
throw new FormatException("Invalid compressed byte.");
}
SetBytes(ba.SubArray(1, ba.Length - 2));
}
else
{
throw new FormatException("Invalid bytes length");
}
}
/// <summary>
/// Base-58 encoder to convert byte arrays to WIF.
/// </summary>
protected readonly Base58 b58Encoder = new Base58();
private readonly EllipticCurveCalculator calc = new EllipticCurveCalculator();
private readonly BigInteger minValue = BigInteger.One;
// Curve.N - 1
private readonly BigInteger maxValue =
BigInteger.Parse("115792089237316195423570985008687907852837564279074904382605163141518161494336");
private const int KeyByteSize = 32;
private const byte MainNetByte = 128;
private const byte TestNetByte = 239;
private const byte RegTestByte = 239;
private const byte CompressedByte = 1;
// Don't rename, used by tests with reflection.
private byte[] keyBytes;
/// <summary>
/// Checks the given byte array and sets the keyBytes or throws an exception.
/// </summary>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentOutOfRangeException"/>
/// <param name="key">Key bytes to use</param>
protected void SetBytes(byte[] key)
{
if (key == null)
throw new ArgumentNullException(nameof(key), "Key bytes can not be null.");
key = key.TrimStart(); // bytes are treated as big-endian
if (key.Length > 32)
{
throw new ArgumentOutOfRangeException(nameof(key), $"Given key value is bigger than 256 bits.");
}
BigInteger num = key.ToBigInt(true, true);
if (num < minValue || num > maxValue)
{
throw new ArgumentOutOfRangeException(nameof(key), "Given key value is outside the defined range by the curve.");
}
keyBytes = new byte[32];
// due to big-endianness byte array must be padded with initial zeros hence the dstOffset below
Buffer.BlockCopy(key, 0, keyBytes, dstOffset: 32 - key.Length, count: key.Length);
}
/// <exception cref="ArgumentException"/>
protected byte GetWifFirstByte(NetworkType netType)
{
return netType switch
{
NetworkType.MainNet => MainNetByte,
NetworkType.TestNet => TestNetByte,
NetworkType.RegTest => RegTestByte,
_ => throw new ArgumentException("Network type is not defined!"),
};
}
/// <exception cref="ObjectDisposedException"/>
protected virtual void CheckDisposed()
{
if (isDisposed)
throw new ObjectDisposedException("Private key instance was disposed.");
}
/// <summary>
/// Returns the byte sequence of this instance.
/// </summary>
/// <exception cref="ObjectDisposedException"/>
/// <returns>An array of bytes.</returns>
public byte[] ToBytes()
{
CheckDisposed();
byte[] ba = new byte[keyBytes.Length];
Buffer.BlockCopy(keyBytes, 0, ba, 0, keyBytes.Length);
return ba;
}
/// <summary>
/// Converts the bytes sequence of this instance to its equivalent <see cref="BigInteger"/> representation.
/// </summary>
/// <exception cref="ObjectDisposedException"/>
/// <returns>The <see cref="BigInteger"/> representation of this instance.</returns>
public BigInteger ToBigInt()
{
CheckDisposed();
return keyBytes.ToBigInt(true, true);
}
/// <summary>
/// Converts this instance to its equivalent <see cref="PublicKey"/>.
/// </summary>
/// <exception cref="ObjectDisposedException"/>
/// <returns>The derived <see cref="PublicKey"/>.</returns>
public PublicKey ToPublicKey()
{
CheckDisposed();
return new PublicKey(calc.MultiplyByG(ToBigInt()));
}
/// <summary>
/// Converts the bytes sequence of this instance to its <see cref="Base58"/> representation with a checksum.
/// Also known as Wallet Import Format (WIF).
/// </summary>
/// <exception cref="ObjectDisposedException"/>
/// <remarks>
/// For versioned WIFs check out <see cref="ImprovementProposals.BIP0178"/>
/// </remarks>
/// <param name="compressed">Indicating public key type</param>
/// <param name="netType">
/// [Default value = <see cref="NetworkType.MainNet"/>]
/// Network type to use when encoding the key (affects WIF's starting characters)
/// </param>
/// <returns>A <see cref="Base58"/> encoded string (WIF).</returns>
public string ToWif(bool compressed, NetworkType netType = NetworkType.MainNet)
{
CheckDisposed();
byte[] data = new byte[compressed ? keyBytes.Length + 2 : keyBytes.Length + 1];
Buffer.BlockCopy(keyBytes, 0, data, 1, keyBytes.Length);
data[0] = GetWifFirstByte(netType);
if (compressed)
{
data[^1] = CompressedByte;
}
return b58Encoder.EncodeWithCheckSum(data);
}
/// <summary>
/// Signs the given UTF-8 encoded message with this key.
/// </summary>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ObjectDisposedException"/>
/// <param name="message">Message to sign</param>
/// <returns>Signature</returns>
public Signature Sign(string message)
{
CheckDisposed();
// Empty string is OK but we don't accept it
if (string.IsNullOrEmpty(message))
throw new ArgumentNullException(nameof(message), "Message can not be null or empty.");
// TODO: check out https://github.com/bitcoin/bips/blob/master/bip-0137.mediawiki + 322
// TODO: convert Sha into a straem itself and directly write to it instead of another stream (similar to tx)
using Sha256 hash = new Sha256(true);
FastStream stream = new FastStream();
stream.Write((byte)Constants.MsgSignConst.Length);
stream.Write(Encoding.UTF8.GetBytes(Constants.MsgSignConst));
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
new CompactInt(messageBytes.Length).WriteToStream(stream);
stream.Write(messageBytes);
byte[] toSign = hash.ComputeHash(stream.ToByteArray());
return calc.Sign(toSign, keyBytes);
}
/// <summary>
/// Signs the given transaction's input at the given index with the given <see cref="SigHashType"/> and
/// sets its signature.
/// </summary>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ObjectDisposedException"/>
/// <param name="tx">The transaction to sign</param>
/// <param name="txToSpend">The previous tx that the transaction to sign is spending</param>
/// <param name="index">Index of the input to sign</param>
/// <param name="sht">Signature hash type</param>
public void Sign(ITransaction tx, ITransaction txToSpend, int index, SigHashType sht = SigHashType.All)
{
CheckDisposed();
if (tx == null)
throw new ArgumentNullException(nameof(tx), "Transaction can not be null!");
if (txToSpend == null)
throw new ArgumentNullException(nameof(txToSpend), "Previous out script can not be null!");
// TODO: change ITransaction.Sign so that it takes public key and does the initial checks
// such as correct txout being spent.
Signature sig = calc.Sign(tx.GetBytesToSign(txToSpend, index, sht), keyBytes);
sig.SigHash = sht;
//tx.WriteScriptSig(sig, pubKey, txToSpend, index);
}
private bool isDisposed = false;
/// <summary>
/// Releases the resources used by the <see cref="PrivateKey"/> class.
/// </summary>
/// <param name="disposing">
/// True to release both managed and unmanaged resources; false to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
if (keyBytes != null)
Array.Clear(keyBytes, 0, keyBytes.Length);
keyBytes = null;
}
isDisposed = true;
}
}
/// <summary>
/// Releases all resources used by the current instance of the <see cref="PrivateKey"/> class.
/// </summary>
public void Dispose()
{
Dispose(true);
}
}
}