-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfrmmainunit.pas
388 lines (286 loc) · 9.25 KB
/
frmmainunit.pas
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
unit frmMainunit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, ExtCtrls,
StdCtrls, btckeyfunctions, ClpEncoders, Clipbrd, MaskEdit, Spin, EditBtn,
USha256;
type
{ TfrmMain }
TfrmMain = class(TForm)
btnBrainpvtKey: TButton;
btnRNDpvtKey: TButton;
btnComputePrvKeyDet: TButton;
btnComputePubKeyDet: TButton;
btnBulkGen: TButton;
btnCancel: TButton;
btnSaveBulk: TButton;
edtBulkAmount: TEdit;
edtBTCaddrCompr: TLabeledEdit;
edtBTCaddress: TLabeledEdit;
edtPubKey: TLabeledEdit;
edtPubKeyComp: TLabeledEdit;
edtPassPhrase: TLabeledEdit;
edtPvtKeyB64: TLabeledEdit;
edtPvtKeyHex: TLabeledEdit;
edtPvtKeyWIF: TLabeledEdit;
edtPvtKey: TLabeledEdit;
edtPvtKeyWIFComp: TLabeledEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label6: TLabel;
memBulk: TMemo;
PageControl1: TPageControl;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
dlgSave: TSaveDialog;
StatusBar: TStatusBar;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
tmrKeyPerSec: TTimer;
procedure btnBrainpvtKeyClick(Sender: TObject);
procedure btnComputePrvKeyDetClick(Sender: TObject);
procedure btnComputePubKeyDetClick(Sender: TObject);
procedure btnRNDpvtKeyClick(Sender: TObject);
procedure btnBulkGenClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnSaveBulkClick(Sender: TObject);
procedure edtBulkAmountKeyPress(Sender: TObject; var Key: char);
procedure FormCreate(Sender: TObject);
procedure DblClick2CLPBRD(Sender: TObject);
procedure edtDbgHintLength(Sender: TObject);
procedure memBulkChange(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure tmrKeyPerSecTimer(Sender: TObject);
private
public
end;
function ByteToString(const Value: TBytes): string;
function StrToByte(const Value: string): TBytes;
function ByteToHexString(input: TBytes): string;
var
frmMain: TfrmMain;
IsPRVkeyInputValid: boolean;
KeyPair: TKeyPair;
forcedCancel: boolean;
keysGenPerSec: integer;
implementation
{$R *.lfm}
{ TfrmMain }
procedure TfrmMain.btnBrainpvtKeyClick(Sender: TObject);
var
sbt: TBytes;
astr: string;
begin
edtPvtKey.Text := SHA256ToStr(CalcSHA256(edtPassPhrase.Text));
IsPRVkeyInputValid := True;
// i will need to create some function or procedure to actually validate input
// for now i will just tell it that the private key is valid
if IsPRVkeyInputValid = True then
begin
// write some code to get public key from this generated private key (brain wallet)
// KeyPair := TbtcKeyFunctions.GenPubKeyFromPvtInput(TKeyType.SECP256K1);
// now I am stuck... how do I tell this function what my input private key is?
//TbtcKeyFunctions.GenPubKeyFromPvtInput();
btnBrainpvtKey.Enabled := False;
btnRNDpvtKey.Enabled := False;
btnComputePrvKeyDet.Enabled := True;
btnComputePubKeyDet.Enabled := True;
end;
end;
procedure TfrmMain.btnComputePrvKeyDetClick(Sender: TObject);
begin
edtPvtKeyHex.Text := edtPvtKey.Text;
edtPvtKeyB64.Text := TBase64.Encode(THex.Decode(edtPvtKeyHex.Text));
edtPvtKeyWIF.Text := TbtcKeyFunctions.GetPrivateKeyWIF(edtPvtKeyHex.Text, False);
edtPvtKeyWIFComp.Text := TbtcKeyFunctions.GetPrivateKeyWIF(edtPvtKeyHex.Text, True);
btnComputePrvKeyDet.Enabled := False;
if (btnComputePubKeyDet.Enabled = False) and (btnComputePrvKeyDet.Enabled = False) then
begin
btnRNDpvtKey.Enabled := True;
btnBrainpvtKey.Enabled := True;
end;
end;
procedure TfrmMain.btnComputePubKeyDetClick(Sender: TObject);
var
Address, PubKey: String;
begin
TbtcKeyFunctions.GetPublicKeyDetails(edtPvtKey.Text, False, Address, PubKey);
edtBTCaddress.Text := Address;
edtPubKey.Text := PubKey;
TbtcKeyFunctions.GetPublicKeyDetails(edtPvtKey.Text, True, Address, PubKey);
edtBTCaddrCompr.Text := Address;
edtPubKeyComp.Text := PubKey;
btnComputePubKeyDet.Enabled := False;
if (btnComputePubKeyDet.Enabled = False) and
(btnComputePrvKeyDet.Enabled = False) then
begin
btnRNDpvtKey.Enabled := True;
btnBrainpvtKey.Enabled := True;
end;
end;
procedure TfrmMain.btnRNDpvtKeyClick(Sender: TObject);
begin
edtPvtKey.Text := TbtcKeyFunctions.GenerateValidRandomBytesForPrivateKey();
edtPvtKey.Hint := IntToStr(length(edtPvtKey.Text)) +
' Characters Counted (debuging hint)';
IsPRVkeyInputValid := True;
// add checks to see if valid private key is generated
// then enable "compute below" buttons
// will obviously need some validation code written in future
if IsPRVkeyInputValid = True then
begin
btnComputePrvKeyDet.Enabled := True;
btnComputePubKeyDet.Enabled := True;
btnRNDpvtKey.Enabled := False;
btnBrainpvtKey.Enabled := False;
btnRNDpvtKey.Caption := 'Generate New Random Private Key (More Secure)';
end;
end;
procedure TfrmMain.btnBulkGenClick(Sender: TObject);
var
qty: integer;
i: integer;
begin
// playing with idea of counting keys per second
keysGenPerSec := 0;
tmrKeyPerSec.Enabled := True;
qty := StrToInt(edtBulkAmount.Text);
memBulk.Lines.Clear;
forcedCancel := False;
edtBulkAmount.Enabled:=false;
btnBulkGen.Visible := False; // so user doesnt keep clicking if generating large list
for i := 1 to qty do
begin
if forcedCancel = True then
exit;
KeyPair := TbtcKeyFunctions.GenerateECKeyPair(TKeyType.SECP256K1);
memBulk.Lines.Add(ByteToHexString(KeyPair.PrivateKey) + ' , ' +
ByteToHexString(KeyPair.PublicKey));
// make the memo scroll as it is populated... just looks cool i guess
//
memBulk.SelStart := memBulk.GetTextLen;
memBulk.SelLength := 0;
memBulk.ScrollBy(0, memBulk.Lines.Count);
memBulk.Refresh;
// just toying with generating mass keys and counting how many per second using a timer
Inc(keysGenPerSec, 1);
application.ProcessMessages;
// so if user does a ridculous amount of keys form will stay responsive and can click cancel
end;
keysGenPerSec := 0;
StatusBar.Panels.Items[0].Text := '0/Keys Per Second';
tmrKeyPerSec.Enabled := False; // stop timer... no point in keeping it enabled
btnBulkGen.Visible := True; // re-enables so user can smash all day long... whatever
edtBulkAmount.Enabled:=true;
end;
procedure TfrmMain.btnCancelClick(Sender: TObject);
begin
forcedCancel := True;
btnBulkGen.Visible := True; // re-enables so user can smash all day long... whatever
keysGenPerSec := 0;
StatusBar.Panels.Items[0].Text := '0/Keys Per Second';
tmrKeyPerSec.Enabled := False;
edtBulkAmount.Enabled:=true;
end;
procedure TfrmMain.btnSaveBulkClick(Sender: TObject);
var
fileout: TFileName;
begin
if dlgSave.Execute then begin
dlgSave.Options:=[ofOverwritePrompt];
memBulk.Lines.SaveToFile(dlgSave.FileName);
end;
end;
procedure TfrmMain.edtBulkAmountKeyPress(Sender: TObject; var Key: char);
begin
if (key = #13) and (StrToInt(edtBulkAmount.Text) > 0) then
begin
btnBulkGen.Click;
abort;
end;
// #8 is Backspace
if not (Key in [#8, '0'..'9']) then begin
ShowMessage('Invalid key');
// Discard the key
Key := #0;
end;
end;
procedure TfrmMain.DblClick2CLPBRD(Sender: TObject);
var
theText: TLabeledEdit;
begin
theText := Sender as TLabeledEdit;
clipboard.AsText := theText.Text;
end;
procedure TfrmMain.edtDbgHintLength(Sender: TObject);
var
theDbgHint: TLabeledEdit;
begin
theDbgHint := Sender as TLabeledEdit;
theDbgHint.Hint := IntToStr(length(theDbgHint.Text)) +
' Characters Counted (debuging hint)';
end;
procedure TfrmMain.memBulkChange(Sender: TObject);
begin
btnSaveBulk.Enabled:=true;
end;
procedure TfrmMain.PageControl1Change(Sender: TObject);
begin
if PageControl1.ActivePageIndex = 1 then
StatusBar.Panels.Items[0].Text := '0/Keys Per Second';
if PageControl1.ActivePageIndex = 0 then
StatusBar.Panels.Items[0].Text :=
'Double click any field to copy to clip board. Hover over addresses for QR Code.';
if PageControl1.ActivePageIndex = 2 then
StatusBar.Panels.Items[0].Text :=
'Yeah... its a pointless tool so far... Maybe some day it will be useful and cool!';
end;
procedure TfrmMain.tmrKeyPerSecTimer(Sender: TObject);
begin
StatusBar.Panels.Items[0].Text := IntToStr(keysGenPerSec) + '/Keys Per Second';
keysGenPerSec := 0;
// set back to 0 so the next time this executes in 1000ms it should be how many times the keygen looped.... lets try
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
PageControl1Change(self);
Application.ShowHint := True;
end;
function ByteToHexString(input: TBytes): string;
var
index: Int32;
begin
Result := '';
for index := System.Low(input) to System.High(input) do
begin
Result := Result + IntToHex(input[index], 2);
end;
end;
function ByteToString(const Value: TBytes): string;
var
I: integer;
S: string;
Letra: char;
begin
S := '';
for I := Length(Value) - 1 downto 0 do
begin
letra := Chr(Value[I]);
S := letra + S;
end;
Result := S;
end;
function StrToByte(const Value: string): TBytes;
var
I: integer;
begin
SetLength(Result, Length(Value));
for I := 0 to Length(Value) - 1 do
Result[I] := Ord(Value[I + 1]);
end;
end.