-
Notifications
You must be signed in to change notification settings - Fork 22
/
Utilities.cs
175 lines (138 loc) · 6.7 KB
/
Utilities.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
using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Security.Cryptography;
using System.Text;
namespace DotNetStratumMiner
{
public static class Utilities
{
public static byte[] HexStringToByteArray(string hexString)
{
if (hexString.Length % 2 != 0)
{
throw new ArgumentException(String.Format("The binary key cannot have an odd number of digits: {0}", hexString));
}
byte[] HexAsBytes = new byte[hexString.Length / 2];
for (int index = 0; index < HexAsBytes.Length; index++)
{
string byteValue = hexString.Substring(index * 2, 2);
//HexAsBytes[index] = Convert.ToByte(byteValue);
HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return HexAsBytes;
}
public static string ByteArrayToHexString(byte[] byteArray)
{
string result = "";
foreach (byte b in byteArray)
result += string.Format("{0:x2}", b);
return result;
}
public static byte[] ReverseByteArrayByFours(byte[] byteArray)
{
byte temp;
if (byteArray.Length % 4 != 0)
{
throw new ArgumentException(String.Format("The byte array length must be a multiple of 4"));
}
for (int index = 0; index < byteArray.Length; index += 4)
{
temp = byteArray[index];
byteArray[index] = byteArray[index + 3];
byteArray[index + 3] = byteArray[index + 2];
byteArray[index + 2] = byteArray[index + 1];
byteArray[index + 1] = byteArray[index + 3];
byteArray[index + 3] = temp;
}
return byteArray;
}
// For testing: Coinbase = Coinb1 + Extranonce1 + Extranonce2 + Coinb2
//Coinb1 = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff2703d72707062f503253482f049a53985208";
//Coinb2 = "0d2f7374726174756d506f6f6c2f0000000001923f6f2e010000001976a9145b771921a9b47ee8104da7e4710b5f633d95fa7388ac00000000";
//ExtraNonce1 = "f8025672";
//ExtraNonce2 = "00000001";
//MerkleNumbers = new string[5];
//MerkleNumbers[0] = "a7f97d9ec804c539b375f2997bc12d32269d105a5281f2403c14fb1833b70f0c";
//MerkleNumbers[1] = "04bd5a1f74d2beece0c893d742c51dd9b9f2e08e2686008d258f23461288a2cd";
//MerkleNumbers[2] = "303ec9c3092b926f862ee2bd70b629e270e836eba86df9b55f9d795439163b13";
//MerkleNumbers[3] = "2d43210a4fb228cf18001b630d0d45ca509b3db8ec3a40586b022546822b0dea";
//MerkleNumbers[4] = "caf00c196faef37f97da33e8c6ef3cbc233f172a9a062d72f0dfd3ee17af291c";
// Should generate a Merkle Root of 3c43e4bf024c2900181bd2f94bd7ebd5d3298f6d47f4f899b952acfd6aa6d94e
public static string GenerateMerkleRoot(string Coinb1, string Coinb2, string ExtraNonce1, string ExtraNonce2, string[] MerkleNumbers)
{
string Coinbase = Coinb1 + ExtraNonce1 + ExtraNonce2 + Coinb2;
byte[] Coinbasebytes = Utilities.HexStringToByteArray(Coinbase);
SHA256 mySHA256 = SHA256.Create();
mySHA256.Initialize();
byte[] hashValue, hashValue2;
// Create Coinbase hash by DoubleSHA of Coinbase
hashValue = mySHA256.ComputeHash(Coinbasebytes);
hashValue2 = mySHA256.ComputeHash(hashValue);
// Calculate Merkle Root by double-hashing the Coinbase hash with each Merkle number in turn
foreach (string s in MerkleNumbers)
{
hashValue = mySHA256.ComputeHash(Utilities.HexStringToByteArray(Utilities.ByteArrayToHexString(hashValue2) + s));
hashValue2 = mySHA256.ComputeHash(hashValue);
}
string MerkleRoot = Utilities.ByteArrayToHexString(Utilities.ReverseByteArrayByFours(hashValue2));
return MerkleRoot;
}
public static string GenerateTarget(int Difficulty)
{
// Calculate Target (which is the reverse of 0x 0000ffff 00000000 00000000 00000000 00000000 00000000 00000000 00000000 / difficulty
byte[] ba = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int index = 0;
int d = Difficulty;
int n = ba[0];
byte[] result = new byte[ba.Length];
do
{
int r = n / d;
result[index] = (byte)r;
int x = n - r * d;
if (++index == ba.Length)
break;
n = (x << 8) + ba[index];
}
while (true);
Array.Reverse((Array)result);
string Target = Utilities.ByteArrayToHexString(result);
return Target;
}
/// <summary>
/// Serializes an object to a UTF-8 encoded JSON string.
/// </summary>
/// <param name="obj">object to serialize</param>
/// <returns>JSON string result</returns>
public static string JsonSerialize(object obj)
{
// Serialize to a memory stream....
MemoryStream ms = new MemoryStream();
// Serialize to memory stream with DataContractJsonSerializer
DataContractJsonSerializer s = new DataContractJsonSerializer(obj.GetType());
s.WriteObject(ms, obj);
byte[] json = ms.ToArray();
ms.Close();
// Return utf8 encoded json string
return Encoding.UTF8.GetString(json, 0, json.Length);
}
/// <summary>
/// Deserializes an object from a UTF-8 encoded JSON string.
/// </summary>
/// <typeparam name="T">type of object to deserialize as</typeparam>
/// <param name="json">UTF-8 encoded JSON string</param>
/// <returns>deserialized object</returns>
public static T JsonDeserialize<T>(string json)
{
T result = default(T);
// Load json into memorystream and deserialize
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
result = (T)s.ReadObject(ms);
ms.Close();
return result;
}
}
}