-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathUFRMPayloadDecoder.pas
More file actions
429 lines (390 loc) · 14.8 KB
/
UFRMPayloadDecoder.pas
File metadata and controls
429 lines (390 loc) · 14.8 KB
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
unit UFRMPayloadDecoder;
{ Copyright (c) 2016 by Albert Molina
Distributed under the MIT software license, see the accompanying file LICENSE
or visit http://www.opensource.org/licenses/mit-license.php.
This unit is a part of the PascalCoin Project, an infinitely scalable
cryptocurrency. Find us here:
Web: https://www.pascalcoin.org
Source: https://github.com/PascalCoin/PascalCoin
If you like it, consider a donation using Bitcoin:
16K3HCZRhFUtM8GdWRcfKeaa6KsuyxZaYk
THIS LICENSE HEADER MUST NOT BE REMOVED.
}
{$mode Delphi}
interface
{$I ../config.inc}
uses
LCLIntf, LCLType, LMessages,
Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, UCommon.UI,
UBlockChain, UCrypto, UWallet, Buttons, ComCtrls,UAppParams;
type
{ TFRMPayloadDecoder }
TFRMPayloadDecoder = class(TApplicationForm)
Label1: TLabel;
lblBlock: TLabel;
lblDateTime: TLabel;
Label6: TLabel;
Label2: TLabel;
lblOperationTxt: TLabel;
PageControl: TPageControl;
tsDecoded: TTabSheet;
tsDecodeMethods: TTabSheet;
cbMethodPublicPayload: TCheckBox;
cbUsingPrivateKeys: TCheckBox;
cbUsingPasswords: TCheckBox;
memoPasswords: TMemo;
Label7: TLabel;
lblDecodedMethod: TLabel;
Label3: TLabel;
bbSaveMethods: TBitBtn;
bbClose: TBitBtn;
memoDecoded: TMemo;
memoOriginalPayloadInHexa: TMemo;
lblPasswordsInfo: TLabel;
lblAmountCaption: TLabel;
lblAmount: TLabel;
lblFeeCaption: TLabel;
lblFee: TLabel;
Label4: TLabel;
bbFind: TBitBtn;
ebOphash: TEdit;
lblSenderCaption: TLabel;
lblSender: TLabel;
lblReceiverCaption: TLabel;
lblReceiver: TLabel;
lblReceiverInfo: TLabel;
cbShowAsHexadecimal: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure PageControlChanging(Sender: TObject; var AllowChange: Boolean);
procedure cbMethodPublicPayloadClick(Sender: TObject);
procedure bbSaveMethodsClick(Sender: TObject);
procedure memoDecodedKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure bbFindClick(Sender: TObject);
procedure ebOphashExit(Sender: TObject);
procedure ebOphashKeyPress(Sender: TObject; var Key: Char);
procedure cbShowAsHexadecimalClick(Sender: TObject);
private
FOpResume : TOperationResume;
FSavedDecodeMethods : boolean;
FSemaphor : Boolean;
{ Private declarations }
Procedure TryToDecode;
Procedure SaveMethods;
procedure SetOpResume(const Value: TOperationResume);
public
{ Public declarations }
Procedure Init(Const AOperationResume : TOperationResume; WalletKeys : TWalletKeys; AppParams : TAppParams); overload;
Procedure Init(Const AOperationResume : TOperationResume); overload;
Property OpResume : TOperationResume read FOpResume write SetOpResume;
Procedure DoFind(Const OpHash : String);
end;
implementation
{$R *.lfm}
Uses
UNode, UTime, UAccounts, UCommon, USettings, UBaseTypes,
UPCEncryption, UFRMMemoText;
{ TFRMPayloadDecoder }
procedure TFRMPayloadDecoder.bbSaveMethodsClick(Sender: TObject);
begin
SaveMethods;
PageControl.ActivePage := tsDecoded;
TryToDecode;
end;
procedure TFRMPayloadDecoder.bbFindClick(Sender: TObject);
Var oph : String;
begin
oph := TCrypto.ToHexaString( FOpResume.OperationHash );
if Not InputQuery('Search operation by OpHash','Insert Operation Hash value (OpHash)',oph) then exit;
DoFind(oph);
end;
procedure TFRMPayloadDecoder.cbMethodPublicPayloadClick(Sender: TObject);
begin
FSavedDecodeMethods := false;
lblPasswordsInfo.Caption := Format('Possible passwords: %d',[memoPasswords.Lines.Count]);
end;
procedure TFRMPayloadDecoder.cbShowAsHexadecimalClick(Sender: TObject);
begin
TryToDecode;
end;
procedure TFRMPayloadDecoder.DoFind(Const OpHash : String);
Var
r,md160 : TRawBytes;
pcops : TPCOperationsComp;
nBlock,nAccount,nN_Operation : Cardinal;
opbi : Integer;
opr : TOperationResume;
strings : TStrings;
FRM : TFRMMemoText;
begin
// Search for an operation based on "ophash"
if (trim(OpHash)='') then begin
OpResume := CT_TOperationResume_NUL;
exit;
end;
try
// Build 2.1.4 new decoder option: Check if OpHash is a posible double spend
r := TCrypto.HexaToRaw(trim(OpHash));
If not TPCOperation.TryParseOperationHash(OpHash,nBlock,nAccount,nN_Operation,md160) then begin
raise Exception.Create('Invalid OPHASH');
end;
Case TNode.Node.FindNOperation(nBlock,nAccount,nN_Operation,opr) of
OpHash_invalid_params : raise Exception.Create(Format('Not a valid OpHash searching at Block:%d Account:%d N_Operation:%d',[nBlock,nAccount,nN_Operation]));
OpHash_block_not_found : raise Exception.Create('Your blockchain file does not contain all blocks to find');
OpHash_found : ;
else raise Exception.Create('ERROR DEV 20171120-6');
end;
If (TPCOperation.EqualOperationHashes(opr.OperationHash,r)) Or
(TPCOperation.EqualOperationHashes(opr.OperationHash_OLD,r)) then begin
// Found!
OpResume := opr;
end else begin
// Not found!
strings := TStringList.Create;
try
strings.Add('Possible double spend detected!');
strings.Add(Format('OpHash: %s',[OpHash]));
strings.Add(Format('Decode OpHash info: Block:%d Account:%s N_Operation:%d',[nBlock,TAccountComp.AccountNumberToAccountTxtNumber(nAccount),nN_Operation]));
strings.Add('');
strings.Add('Real OpHash found in PascalCoin Blockchain:');
strings.Add(Format('OpHash: %s',[TCrypto.ToHexaString(opr.OperationHash)]));
strings.Add(Format('Decode OpHash info: Block:%d Account:%s N_Operation:%d',[opr.Block,TAccountComp.AccountNumberToAccountTxtNumber(opr.SignerAccount),opr.n_operation]));
If (opr.Block=0) then begin
strings.Add('* Note: This is a pending operation not included on Blockchain');
end;
OpResume := opr; // Do show operation resume!
FRM := TFRMMemoText.Create(Self);
try
FRM.InitData('Possible double spend detected',strings.Text);
FRM.ShowModal;
finally
FRM.Free;
end;
finally
strings.Free;
end;
end;
Except
OpResume := CT_TOperationResume_NUL;
try
FSemaphor := true;
ebOphash.Text := trim(ophash);
finally
FSemaphor := false;
end;
Raise;
end;
end;
procedure TFRMPayloadDecoder.ebOphashExit(Sender: TObject);
begin
DoFind(ebOphash.Text);
end;
procedure TFRMPayloadDecoder.ebOphashKeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then DoFind(ebOphash.Text);
end;
procedure TFRMPayloadDecoder.FormCreate(Sender: TObject);
begin
FSemaphor := true;
try
memoDecoded.Lines.Clear;
memoOriginalPayloadInHexa.Lines.Clear;
lblPasswordsInfo.Caption := '';
OpResume := CT_TOperationResume_NUL;
finally
FSemaphor := false;
end;
end;
// Needed for classic compat
Procedure TFRMPayloadDecoder.Init(Const AOperationResume : TOperationResume; WalletKeys : TWalletKeys; AppParams : TAppParams);
begin
Init(AOperationResume);
end;
procedure TFRMPayloadDecoder.Init(Const AOperationResume : TOperationResume);
begin
OpResume := AOperationResume;
FSavedDecodeMethods := true;
PageControl.ActivePage := tsDecoded;
TryToDecode;
end;
procedure TFRMPayloadDecoder.memoDecodedKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=VK_ESCAPE then Close;
end;
procedure TFRMPayloadDecoder.PageControlChanging(Sender: TObject; var AllowChange: Boolean);
begin
//
if PageControl.ActivePage=tsDecodeMethods then begin
If not FSavedDecodeMethods then begin
case Application.MessageBox(PChar('Save new decode methods?'),PChar(Application.Title),MB_YESNOCANCEL+MB_ICONQUESTION) of
IDYES : Begin
SaveMethods;
End;
IDCANCEL : begin
AllowChange := false;
end;
end;
end;
end else begin
FSavedDecodeMethods := true;
end;
end;
procedure TFRMPayloadDecoder.SaveMethods;
begin
TSettings.AppParams.ParamByName['PayloadDecoder.notencrypted'].SetAsBoolean(cbMethodPublicPayload.Checked);
TSettings.AppParams.ParamByName['PayloadDecoder.usingprivatekeys'].SetAsBoolean(cbUsingPrivateKeys.Checked);
TSettings.AppParams.ParamByName['PayloadDecoder.usingpasswords'].SetAsBoolean(cbUsingPasswords.Checked);
TSettings.AppParams.ParamByName['PayloadDecoder.passwords'].SetAsString(memoPasswords.Lines.Text);
TSettings.AppParams.ParamByName['PayloadDecoder.showashexadecimal'].SetAsBoolean(cbShowAsHexadecimal.Checked);
TSettings.Save;
FSavedDecodeMethods := true;
end;
procedure TFRMPayloadDecoder.SetOpResume(const Value: TOperationResume);
Var sem : Boolean;
begin
sem := FSemaphor;
Try
FSemaphor := false;
FOpResume := Value;
if Not Value.valid then begin
lblBlock.Caption := '';
lblDateTime.Caption := '';
lblOperationTxt.Caption := '';
lblDecodedMethod.Caption := '';
lblFee.Caption := '';
lblPasswordsInfo.Caption := '';
lblAmount.Caption := '';
lblSender.Caption := '';
lblReceiver.Caption := '';
lblReceiverInfo.Visible := false;
exit;
end;
If (Value.NOpInsideBlock>=0) then
lblBlock.Caption := inttostr(Value.Block)+'/'+inttostr(Value.NOpInsideBlock+1)+' '+IntToStr(Value.n_operation)
else lblBlock.Caption := inttostr(Value.Block)+' '+IntToStr(Value.n_operation);
if Value.time>10000 then begin
lblDateTime.Caption := DateTimeToStr(UnivDateTime2LocalDateTime(UnixToUnivDateTime(Value.time)));
lblDateTime.Font.Color := clBlack;
end else begin
lblDateTime.Caption := '(Pending block)';
lblDateTime.Font.Color := clRed;
end;
lblOperationTxt.Caption := Value.OperationTxt;
lblAmount.Caption := TAccountComp.FormatMoney(value.Amount);
if Value.Amount>0 then lblAmount.Font.Color := clGreen
else if Value.Amount=0 then lblAmount.Font.Color := clGray
else lblAmount.Font.Color := clRed;
If (Value.SignerAccount>=0) And (Value.DestAccount>=0) then begin
lblSenderCaption.Caption := 'Sender:';
lblSender.Caption := TAccountComp.AccountNumberToAccountTxtNumber(Value.SignerAccount);
lblReceiverCaption.Visible := true;
lblReceiver.Caption := TAccountComp.AccountNumberToAccountTxtNumber(Value.DestAccount);
lblReceiver.Visible := true;
lblFeeCaption.Visible := Value.AffectedAccount=Value.SignerAccount;
lblFee.Visible := lblFeeCaption.Visible;
lblReceiverInfo.Visible := Not lblFee.Visible;
end else begin
lblSenderCaption.Caption := 'Account:';
lblSender.caption := TAccountComp.AccountNumberToAccountTxtNumber(Value.AffectedAccount);
lblReceiverCaption.Visible := false;
lblReceiver.Visible := false;
lblFeeCaption.Visible := true;
lblFee.Visible := true;
lblReceiverInfo.Visible := false;
end;
lblFee.Caption := TAccountComp.FormatMoney(value.Fee);
if Value.Fee>0 then lblFee.Font.Color := clGreen
else if Value.Fee=0 then lblFee.Font.Color := clGray
else lblFee.Font.Color := clRed;
ebOpHash.text := TCrypto.ToHexaString(Value.OperationHash);
memoOriginalPayloadInHexa.Lines.Text := TCrypto.ToHexaString(Value.OriginalPayload.payload_raw);
cbMethodPublicPayload.Checked := TSettings.AppParams.ParamByName['PayloadDecoder.notencrypted'].GetAsBoolean(true);
cbUsingPrivateKeys.Checked := TSettings.AppParams.ParamByName['PayloadDecoder.usingprivatekeys'].GetAsBoolean(true);
cbUsingPasswords.Checked := TSettings.AppParams.ParamByName['PayloadDecoder.usingpasswords'].GetAsBoolean(true);
memoPasswords.Lines.Text := TSettings.AppParams.ParamByName['PayloadDecoder.passwords'].GetAsString('');
cbShowAsHexadecimal.Checked := TSettings.AppParams.ParamByName['PayloadDecoder.showashexadecimal'].GetAsBoolean(false);
FSavedDecodeMethods := true;
PageControl.ActivePage := tsDecoded;
TryToDecode;
Finally
FSemaphor := sem;
End;
end;
procedure TFRMPayloadDecoder.TryToDecode;
Function UseWallet(Const raw : TRawBytes; var Decrypted : TRawBytes; var WalletKey : TWalletKey) : Boolean;
Var i : Integer;
begin
Result := false;
if Not assigned(TWallet.Keys) then exit;
for i := 0 to TWallet.Keys.Count - 1 do begin
WalletKey := TWallet.Keys.Key[i];
If Assigned(WalletKey.PrivateKey) then begin
if TPCEncryption.DoPascalCoinECIESDecrypt(WalletKey.PrivateKey.PrivateKey,raw,Decrypted) then begin
Result := true;
exit;
end;
end;
end;
end;
Function UsePassword(const raw : TRawBytes; var Decrypted : TRawBytes; var PasswordUsed : AnsiString) : Boolean;
Var i : Integer;
Begin
Result := false;
for i := 0 to memoPasswords.Lines.Count - 1 do begin
if TPCEncryption.DoPascalCoinAESDecrypt(raw,TEncoding.ANSI.GetBytes(memoPasswords.Lines[i]),Decrypted) then begin
if (TCrypto.IsHumanReadable(Decrypted)) then begin
Result := true;
PasswordUsed := memoPasswords.Lines[i];
exit;
end;
end;
end;
End;
Var raw : TRawBytes;
WalletKey : TWalletKey;
Decrypted : TRawBytes;
PasswordUsed : AnsiString;
ok : boolean;
begin
ok := true;
if Assigned(TWallet.Keys) then begin
raw := FOpResume.OriginalPayload.payload_raw;
if Length(raw)>0 then begin
// First try to a human readable...
if (cbMethodPublicPayload.Checked) and (TCrypto.IsHumanReadable(raw)) then begin
if cbShowAsHexadecimal.Checked then memoDecoded.Lines.Text := TCrypto.ToHexaString(raw)
else memoDecoded.Lines.Text := TEncoding.ANSI.GetString(raw);
lblDecodedMethod.Caption := 'Not encrypted payload';
end else if (cbUsingPrivateKeys.Checked) And (UseWallet(raw,Decrypted,WalletKey)) then begin
if cbShowAsHexadecimal.Checked then memoDecoded.Lines.Text := TCrypto.ToHexaString(Decrypted)
else memoDecoded.Lines.Text := TEncoding.ANSI.GetString(Decrypted);
lblDecodedMethod.Caption := 'Encrypted with EC '+TAccountComp.GetECInfoTxt(WalletKey.PrivateKey.EC_OpenSSL_NID);
end else if (cbUsingPasswords.Checked) And (UsePassword(raw,Decrypted,PasswordUsed)) then begin
if cbShowAsHexadecimal.Checked then memoDecoded.Lines.Text := TCrypto.ToHexaString(Decrypted)
else memoDecoded.Lines.Text := TEncoding.ANSI.GetString(Decrypted);
lblDecodedMethod.Caption := 'Encrypted with pwd:"'+PasswordUsed+'"';
end else begin
memoDecoded.Lines.Text := 'CANNOT DECRYPT';
lblDecodedMethod.Caption := '';
ok := false;
end;
if ok then begin
memoDecoded.Font.Color := clBlack;
memoDecoded.Color := clWhite;
end else begin
memoDecoded.Font.Color := clRed;
memoDecoded.Color := clBtnFace;
end;
end else begin
memoDecoded.Lines.Text := '(No payload)';
memoDecoded.Font.Color := clDkGray;
memoDecoded.Color := clLtGray;
lblDecodedMethod.Caption := '';
end;
end else begin
memoDecoded.Lines.Text := '';
lblDecodedMethod.Caption := '';
end;
end;
end.