-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTCAddress.cs
473 lines (448 loc) · 20.1 KB
/
BTCAddress.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//___________________________________________________________________________________
//date: 03apr2008
//function: process BitCoin
//last update: 17Apr2021
//author: luis g. valera
//02 March 1998 - Proyectos Huell@ - Colmena Salud / Send@ - Colmena ARP
//global routines - bitcoin & file management - lgv21feb2008
//update: 08 Febrero 2008 - C++ - VB.NET 2.0 Internal Project
//03abr2012 - Luis Villa - se reestructuran las clases para utilizar metadatos generico -- cualquier db
//08abr2013 - Luis Villa - se integran funciones de otras librerias
//datasource, CalErrorControl, session_management
//___________________________________________________________________________________
//Project list luis g. valera
//date: 07mar2008
//last update: 17Apr2021
//author: Luis Guillermo Valera - Juan Carlos Gonzalez Rodriguez - Hernan Felipe Fonseca Garzon
//04ago2008 - Microsoft Corporation - SHA256
//08sep2010 - BankVision Software Ltda - Occidental de Colombia
//02mar2011 - ADA S.A. - Secretaria Salud Soacha - Aseguramiento en Salud Y Salud Publica
//02ene2012 - Banco Finandina - Otorgamiento y Administracion de Tarjetas de Credito
//05feb2013 - Aciel Colombia SAS - Outsourcing Operativo
//02ene2014 - Aciel Colombia SAS - Outsourcing Operativo
//10feb2015 - Adaptacion para factory version 5.0
//___________________________________________________________________________________
using BitCoin_Encrypt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BitCoin_Address
{
public partial class BTCAddress : Form
{
public BTCAddress()
{
InitializeComponent();
}
public string SeedToSHA256 = "";
public string StrTXID = "";
public string StrPrivHex = "";
// function: btnGenPri_Click
// last update: 17Apr2021
// author: luis g. valera
// 0. _______________________________________________________________________
private void btnGenPri_Click(object sender, EventArgs e)
{
txtPrivHex.Text = EncryptFunctions.RandomPrixHex();
txtPrivHex.Text = EncryptFunctions.HashToPrivHex(txtSeedToSHA256.Text);
txtPrivWIF.Text = EncryptFunctions.PrivHexToWIF(txtPrivHex.Text);
txtPubHex.Text = EncryptFunctions.PrivHexToPubHex(txtPrivHex.Text);
txtPubSha256.Text = EncryptFunctions.HexHashSha256(txtPubHex.Text);
txtPubRipemd160.Text = EncryptFunctions.PubHexToRipemd160Hash(txtPubHex.Text);
txtBtcAddress.Text = EncryptFunctions.PubHashToAddress(txtPubRipemd160.Text, cboCoinType.Text);
displayPubHexPoint();
}
// function: btnSeedToSHA256_Click
// last update: 17Apr2021
// author: luis g. valera
// 1. _______________________________________________________________________
private void btnSeedToSHA256_Click(object sender, EventArgs e)
{
txtPrivHex.Text = EncryptFunctions.HashToPrivHex(txtSeedToSHA256.Text);
txtPrivWIF.Text = EncryptFunctions.PrivHexToWIF(txtPrivHex.Text);
txtPubHex.Text = EncryptFunctions.PrivHexToPubHex(txtPrivHex.Text);
txtPubSha256.Text = EncryptFunctions.HexHashSha256(txtPubHex.Text);
txtPubRipemd160.Text = EncryptFunctions.PubHexToRipemd160Hash(txtPubHex.Text);
txtBtcAddress.Text = EncryptFunctions.PubHashToAddress(txtPubRipemd160.Text, cboCoinType.Text);
displayPubHexPoint();
}
// function: btnPrivToWIF_Click
// last update: 17Apr2021
// author: luis g. valera
// 2. _______________________________________________________________________
private void btnPrivToWIF_Click(object sender, EventArgs e)
{
txtPrivWIF.Text = EncryptFunctions.PrivHexToWIF(txtPrivHex.Text);
txtPubHex.Text = EncryptFunctions.PrivHexToPubHex(txtPrivHex.Text);
txtPubSha256.Text = EncryptFunctions.HexHashSha256(txtPubHex.Text);
txtPubRipemd160.Text = EncryptFunctions.PubHexToRipemd160Hash(txtPubHex.Text);
txtBtcAddress.Text = EncryptFunctions.PubHashToAddress(txtPubRipemd160.Text, cboCoinType.Text);
displayPubHexPoint();
}
// function: btnPrivWIFToHex_Click
// last update: 17Apr2021
// author: luis g. valera
// 3. _______________________________________________________________________
private void btnPrivWIFToHex_Click(object sender, EventArgs e)
{
txtPrivHex.Text = EncryptFunctions.PrivWIFToHex(txtPrivWIF.Text);
txtPubHex.Text = EncryptFunctions.PrivHexToPubHex(txtPrivHex.Text);
txtPubSha256.Text = EncryptFunctions.HexHashSha256(txtPubHex.Text);
txtPubRipemd160.Text = EncryptFunctions.PubHexToRipemd160Hash(txtPubHex.Text);
txtBtcAddress.Text = EncryptFunctions.PubHashToAddress(txtPubRipemd160.Text, cboCoinType.Text);
displayPubHexPoint();
}
// function: btnPrivToPub_Click
// last update: 17Apr2021
// author: luis g. valera
// 4. _______________________________________________________________________
private void btnPrivToPub_Click(object sender, EventArgs e)
{
txtPubHex.Text = EncryptFunctions.PrivHexToPubHex(txtPrivHex.Text);
txtPubSha256.Text = EncryptFunctions.HexHashSha256(txtPubHex.Text);
txtPubRipemd160.Text = EncryptFunctions.PubHexToRipemd160Hash(txtPubHex.Text);
txtBtcAddress.Text = EncryptFunctions.PubHashToAddress(txtPubRipemd160.Text, cboCoinType.Text);
displayPubHexPoint();
}
// function: btnPubHexToSha_Click
// last update: 17Apr2021
// author: luis g. valera
// 7. _______________________________________________________________________
private void btnPubHexToSha_Click(object sender, EventArgs e)
{
txtPubSha256.Text = EncryptFunctions.HexHashSha256(txtPubHex.Text);
txtPubRipemd160.Text = EncryptFunctions.PubHexToRipemd160Hash(txtPubHex.Text);
txtBtcAddress.Text = EncryptFunctions.PubHashToAddress(txtPubRipemd160.Text, cboCoinType.Text);
displayPubHexPoint();
}
// function: btnPubShaToRipemd_Click
// last update: 17Apr2021
// author: luis g. valera
// 8. _______________________________________________________________________
private void btnPubShaToRipemd_Click(object sender, EventArgs e)
{
txtPubRipemd160.Text = EncryptFunctions.PubHexToRipemd160Hash(txtPubHex.Text);
txtBtcAddress.Text = EncryptFunctions.PubHashToAddress(txtPubRipemd160.Text, cboCoinType.Text);
displayPubHexPoint();
}
// function: btnPubRipToAdd_Click
// last update: 17Apr2021
// author: luis g. valera
// 9. _______________________________________________________________________
private void btnPubRipToAdd_Click(object sender, EventArgs e)
{
txtBtcAddress.Text = EncryptFunctions.PubHashToAddress(txtPubRipemd160.Text, cboCoinType.Text);
displayPubHexPoint();
}
// function: btnAddToPubRip_Click
// last update: 17Apr2021
// author: luis g. valera
// 10. ______________________________________________________________________
private void btnAddToPubRip_Click(object sender, EventArgs e)
{
txtPubRipemd160.Text = EncryptFunctions.AddressToPubHash(txtBtcAddress.Text);
displayPubHexPoint();
}
// function: btnPubHexPoint_Click
// last update: 17Apr2021
// author: luis g. valera
// 11. ______________________________________________________________________
private void btnPubHexPoint_Click(object sender, EventArgs e)
{
displayPubDecPoint();
}
// function: btnPubHexPoint_Click
// last update: 17Apr2021
// author: luis g. valera
// 12. ______________________________________________________________________
private void btnECDSAHex_Click(object sender, EventArgs e)
{
displayPubHexPoint();
}
// function: btnAddToPubRip_Click
// last update: 17Apr2021
// author: luis g. valera
// 13. ______________________________________________________________________
private void btnBlockExplorer_Click(object sender, EventArgs e)
{
try
{
if (cboCoinType.Text == "Testnet")
{
Process.Start("http://www.blockexplorer.com/testnet/address/" + txtBtcAddress.Text);
}
else if (cboCoinType.Text == "Namecoin")
{
Process.Start("http://explorer.dot-bit.org/a/" + txtBtcAddress.Text);
}
else
{
Process.Start("http://www.blockexplorer.com/address/" + txtBtcAddress.Text);
}
}
catch { }
}
// function: btnPubToPriv_Click
// last update: 17Apr2021
// author: luis g. valera
// 14. ______________________________________________________________________
private void btnPubToPriv_Click(object sender, EventArgs e)
{
txtPrivHex.Text = EncryptFunctions.PubToPriv_Click(txtPubHex.Text);
}
// function: btnTxID_Click
// last update: 17Apr2021
// author: luis g. valera
// 14. ______________________________________________________________________
//private void btnTxID_Click(object sender, EventArgs e)
//{
// string pubkey = EncryptFunctions.ParceTxId(txtSeedToSHA256.Text).Hex;
// int PubHexLength = (pubkey.Length - 2);
// if (PubHexLength > 130)
// {
// pubkey = pubkey.Substring(2, 130);
// }
// else
// {
// pubkey = "";
// }
// txtPubHex.Text = pubkey;
// btnPubHexToSha_Click(null, null);
// btnPubShaToRipemd_Click(null, null);
//}
// function: btnDecodeTxIdKeys_Click
// last update: 17Apr2021
// author: luis g. valera
// 6. ______________________________________________________________________
private void btnDecodeTxIdKeys_Click(object sender, EventArgs e)
{
if (btnDecodeTxIdKeys.Text == "Decode TxID")
{
if (SeedToSHA256.Length == 0)
{
SeedToSHA256 = txtSeedToSHA256.Text;
if (StrPrivHex.Length == 0)
{
StrPrivHex = txtPrivHex.Text;
};
ClearTextBoxes();
}
else if (StrTXID.Length > 0)
{
SeedToSHA256 = txtSeedToSHA256.Text;
ClearTextBoxes();
txtSeedToSHA256.Text = StrTXID;
//btnTxID_Click(null, null);
};
// DECODE KEYS
// phrase hash
btnDecodeTxIdKeys.Text = "Decode Btc Keys";
lblSeedToSHA256.Text = "TxID or Hash";
btnGenPri.Visible = false;
btnSeedToSHA256.Visible = false;
btnTxID.Visible = true;
// ECDSA hash
lblBtclNetwork.Text = "R Value";
txtBtclNetwork.Width = 561;
txtBtclNetwork.Text = "";
lblBtcNetWork.Visible = false;
cboCoinType.Visible = false;
btnBlockExplorer.Visible = false;
btnECDSAHex.Visible = false;
}
else
{
if (StrTXID.Length == 0)
{
StrTXID = txtSeedToSHA256.Text;
ClearTextBoxes();
if (SeedToSHA256.Length > 0)
{
txtSeedToSHA256.Text = SeedToSHA256;
btnSeedToSHA256_Click(null, null);
}
}
else if (SeedToSHA256.Length > 0)
{
StrTXID = txtSeedToSHA256.Text;
ClearTextBoxes();
txtSeedToSHA256.Text = SeedToSHA256;
btnSeedToSHA256_Click(null, null);
}
else if (StrPrivHex.Length > 0)
{
StrTXID = txtSeedToSHA256.Text;
ClearTextBoxes();
txtPrivHex.Text = StrPrivHex;
btnPrivToWIF_Click(null, null);
};
// DECODE TRANSACTION txid
// phrase hash
btnDecodeTxIdKeys.Text = "Decode TxID";
lblSeedToSHA256.Text = "Text to Hash (SHA256)";
btnGenPri.Visible = true;
btnSeedToSHA256.Visible = true;
btnTxID.Visible = false;
// ECDSA hash
lblBtclNetwork.Text = "Network (Hex)";
txtBtclNetwork.Width = 58;
txtBtclNetwork.Text = "";
lblBtcNetWork.Visible = true;
cboCoinType.Visible = true;
btnBlockExplorer.Visible = true;
btnECDSAHex.Visible = true;
}
}
// function: txtPrivHex_TextChanged
// last update: 17Apr2021
// author: luis g. valera
// 1. ______________________________________________________________________
private void txtPrivHex_TextChanged(object sender, EventArgs e)
{
byte[] StrToByteArray = EncryptFunctions.StringToByteArray(txtPrivHex.Text);
txtPrivHex.Text = EncryptFunctions.ByteArrayToString(StrToByteArray);
}
// function: txtPubHex_TextChanged
// last update: 17Apr2021
// author: luis g. valera
// 2. ______________________________________________________________________
private void txtPubHex_TextChanged(object sender, EventArgs e)
{
byte[] StrToByteArray = EncryptFunctions.StringToByteArray(txtPubHex.Text);
txtPubHex.Text = EncryptFunctions.ByteArrayToString(StrToByteArray);
}
// function: txtPubSha256_TextChanged
// last update: 17Apr2021
// author: luis g. valera
// 3. ______________________________________________________________________
private void txtPubSha256_TextChanged(object sender, EventArgs e)
{
byte[] StrToByteArray = EncryptFunctions.StringToByteArray(txtPubSha256.Text);
txtPubSha256.Text = EncryptFunctions.ByteArrayToString(StrToByteArray);
}
// function: txtPubRipemd160_TextChanged
// last update: 17Apr2021
// author: luis g. valera
// 4. ______________________________________________________________________
private void txtPubRipemd160_TextChanged(object sender, EventArgs e)
{
byte[] StrToByteArray = EncryptFunctions.StringToByteArray(txtPubRipemd160.Text);
txtPubRipemd160.Text = EncryptFunctions.ByteArrayToString(StrToByteArray);
}
// function: displayPubHexPoint
// last update: 17Apr2021
// author: luis g. valera
// 5. ______________________________________________________________________
private void displayPubHexPoint()
{
try
{
lblPubKeyPointX.Text = "Pub Key [Hex] X:";
lblPubKeyPointY.Text = "Pub Key [Hex] Y:";
lblPriKeyDec.Text = "Pri Key (Hex):";
// NetWork = 2 Characters
// PubHex = PubKeyPointX + PubKeyPointY
string source = txtPubHex.Text;
source = source.Replace(" ", "");
int PubHexLength = (source.Length - 2) / 2;
if (PubHexLength < 67)
{
// NetWork
txtBtclNetwork.Text = source.Substring(0, 2);
source = source.Substring(2, PubHexLength);
// PubKeyPointX
string PubKeyPointX = "";
PubKeyPointX = source.Substring(0, PubHexLength);
byte[] StrToByteArray = EncryptFunctions.StringToByteArray(PubKeyPointX);
txtPubKeyPointX.Text = EncryptFunctions.ByteArrayToString(StrToByteArray);
// redim source
source = txtPubHex.Text;
source = source.Replace(" ", "");
source = source.Replace(txtBtclNetwork.Text + PubKeyPointX, "");
// PubKeyPointY
if (PubHexLength > source.Length)
{
PubHexLength = source.Length;
}
string PubKeyPointY = source.Substring(0, PubHexLength);
StrToByteArray = EncryptFunctions.StringToByteArray(PubKeyPointY);
txtPubKeyPointY.Text = EncryptFunctions.ByteArrayToString(StrToByteArray);
}
txtPriKeyDec.Text = txtPrivHex.Text;
}
catch {
}
}
// function: displayPubDecPoint
// last update: 17Apr2021
// author: luis g. valera
// 6. ______________________________________________________________________
private void displayPubDecPoint()
{
try
{
lblPubKeyPointX.Text = "Pub Key [Dec] X:";
lblPubKeyPointY.Text = "Pub Key [Dec] Y:";
lblPriKeyDec.Text = "Pri Key (Dec):";
// NetWork = 2 Characters
// PubHex = PubKeyPointX + PubKeyPointY
string source = txtPubHex.Text;
source = source.Replace(" ", "");
int PubHexLength = (source.Length - 2) / 2;
if (PubHexLength < 67)
{
// NetWork
txtBtclNetwork.Text = source.Substring(0, 2);
source = source.Substring(2, PubHexLength);
// PubKeyPointX
string PubKeyPointX = "";
PubKeyPointX = source.Substring(0, PubHexLength);
//byte[] StrToByteArray = EncryptFunctions.StringToByteArray(PubKeyPointX);
//txtPubKeyPointX.Text = EncryptFunctions.HexToDec(StrToByteArray);
txtPubKeyPointX.Text = EncryptFunctions.HexToDec(PubKeyPointX);
// redim source
source = txtPubHex.Text;
source = source.Replace(" ", "");
source = source.Replace(txtBtclNetwork.Text + PubKeyPointX, "");
// PubKeyPointY
if (PubHexLength > source.Length)
{
PubHexLength = source.Length;
}
string PubKeyPointY = source.Substring(0, PubHexLength);
//StrToByteArray = EncryptFunctions.StringToByteArray(PubKeyPointY);
//txtPubKeyPointY.Text = EncryptFunctions.HexToDec(StrToByteArray);
txtPubKeyPointY.Text = EncryptFunctions.HexToDec(PubKeyPointY);
}
txtPriKeyDec.Text = EncryptFunctions.HexToDec(txtPrivHex.Text);
}
catch
{
}
}
// function: ClearTextBoxes
// last update: 17Apr2021
// author: luis g. valera
// 6. ______________________________________________________________________
private void ClearTextBoxes()
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
}
}
}