-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathUEPasaDecoder.pas
More file actions
300 lines (272 loc) · 11.7 KB
/
Copy pathUEPasaDecoder.pas
File metadata and controls
300 lines (272 loc) · 11.7 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
unit UEPasaDecoder;
{ Copyright (c) PascalCoin Developers - Herman Schoenfeld - Albert Molina
PIP-0027: E-PASA Reference Implementation
See: https://github.com/PascalCoin/PascalCoin/blob/master/PIP/PIP-0027.md
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:
Source: https://github.com/PascalCoin/PascalCoin
THIS LICENSE HEADER MUST NOT BE REMOVED.
}
{$IFDEF FPC}
{$MODE DELPHI}
{$ZEROBASEDSTRINGS OFF}
{$ENDIF FPC}
interface
{$I ./../config.inc}
uses
SysUtils,
TypInfo,
{$IFNDEF FPC}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF},
UBlockChain, UNode, UBaseTypes, UPCDataTypes,
UAccounts,
UEncoding,
UEPasa,
UWallet,
URPC, UJSONFunctions;
type
TDecodeEPasaResult = (der_Decoded, der_Undefined, der_NonDeterministic, der_InvalidPayloadType, der_AccountNameNotFound, der_NotEnoughData, der_PrivateKeyNotFound, der_PasswordNotFound);
TEPasaDecoder = Class
public
class Function TryDecodeEPASA(AAccount : Cardinal; const APayload : TOperationPayload; const ANode : TNode; const AWalletKeys : TWalletKeys; const APasswords : TList<String>;
out ADecodeEPasaResult : TDecodeEPasaResult; out AEPasa : TEPasa) : Boolean; overload;
class Function TryDecodeEPASA(AAccount : Cardinal; const APayload : TOperationPayload; const ANode : TNode; const AWalletKeys : TWalletKeys; const APasswords : TList<String>;
out AEPasa : TEPasa) : Boolean; overload;
class Function DecodeEPASA(AAccount : Cardinal; const APayload : TOperationPayload; const ANode : TNode; const AWalletKeys : TWalletKeys; const APasswords : TList<String>) : String; overload;
class function CheckEPasa(const ASender : TRPCProcess; const AAccount_EPasa : String; AJSONResponse : TPCJSONObject; var AErrorNum : Integer; var AErrorDesc : String) : Boolean; overload;
class function CheckEPasa(const ASender : TRPCProcess; const AMethodName : String; AInputParams, AJSONResponse : TPCJSONObject; var AErrorNum : Integer; var AErrorDesc : String) : Boolean; overload;
class function ValidateEPasa(const ASender : TRPCProcess; const AMethodName : String; AInputParams, AJSONResponse : TPCJSONObject; var AErrorNum : Integer; var AErrorDesc : String) : Boolean;
End;
implementation
uses UPCEncryption, UCommon, UCrypto;
{ TEPasaDecoder }
class function TEPasaDecoder.TryDecodeEPASA(AAccount: Cardinal;
const APayload: TOperationPayload; const ANode : TNode; const AWalletKeys: TWalletKeys;
const APasswords: TList<String>; out ADecodeEPasaResult: TDecodeEPasaResult;
out AEPasa: TEPasa): Boolean;
var
LUnencryptedPayloadBytes, LPwd : TBytes;
LDone : Boolean;
i : Integer;
LAccount : TAccount;
begin
LUnencryptedPayloadBytes := Nil;
AEPasa.Clear;
Result := False;
ADecodeEPasaResult := der_Decoded;
AEPasa.Account := AAccount;
AEPasa.AccountChecksum := TEPasa.CalculateAccountChecksum(AAccount);
AEPasa.PayloadType := TEPasaComp.FromProtocolValue(APayload.payload_type);
if AEPasa.PayloadType.HasTrait(ptNonDeterministic) then begin
ADecodeEPasaResult := der_NonDeterministic;
Exit(False);
end;
if Not AEPasa.PayloadType.IsValid then begin
ADecodeEPasaResult := der_InvalidPayloadType;
Exit(False);
end;
if AEPasa.PayloadType.HasTrait(ptAddressedByName) then begin
if (AEPasa.PayloadType.HasTrait(ptPublic) and
AEPasa.PayloadType.HasTrait(ptBase58Formatted)) then begin
// PayToKey candidate...
AEPasa.AccountName := '@';
end else begin
if Assigned(ANode) then begin
LAccount := ANode.GetMempoolAccount(AAccount);
AEPasa.AccountName := LAccount.name.ToPrintable;
end;
if AEPasa.AccountName='' then begin
ADecodeEPasaResult := der_AccountNameNotFound; // Will continue processing
end;
end;
end;
// payload data
if (Length(APayload.payload_raw)=0) then begin
// Nothing to decode...
end else if (AEPasa.PayloadType.HasTrait(ptSenderKeyEncrypted)) or (AEPasa.PayloadType.HasTrait(ptRecipientKeyEncrypted)) then begin
if Assigned(AWalletKeys) then begin
LDone := False;
i := 0;
while (Not LDone) and (i < AWalletKeys.Count) do begin
if Assigned(AWalletKeys.Key[i].PrivateKey) then begin
if TPCEncryption.DoPascalCoinECIESDecrypt(AWalletKeys.Key[i].PrivateKey.PrivateKey,APayload.payload_raw,LUnencryptedPayloadBytes) then begin
LDone := True;
end;
end;
inc(i);
end;
if Not LDone then begin
ADecodeEPasaResult := der_PrivateKeyNotFound;
Exit(False);
end;
end else begin
ADecodeEPasaResult := der_NotEnoughData;
Exit(False);
end;
end else if (AEPasa.PayloadType.HasTrait(ptPasswordEncrypted)) then begin
if Assigned(APasswords) then begin
LDone := False;
i := 0;
while (Not LDone) and (i < APasswords.Count) do begin
LPwd.FromString(APasswords[i]);
if TPCEncryption.DoPascalCoinAESDecrypt(APayload.payload_raw,LPwd,LUnencryptedPayloadBytes) then begin
AEPasa.Password := APasswords[i];
LDone := True;
end;
inc(i);
end;
if Not LDone then begin
ADecodeEPasaResult := der_PasswordNotFound;
Exit(False);
end;
end else begin
ADecodeEPasaResult := der_NotEnoughData;
Exit(False);
end;
end else begin
if (Not AEPasa.PayloadType.HasTrait(ptPublic)) then begin
// Internal Error
ADecodeEPasaResult := der_Undefined;
Exit(False);
end;
LUnencryptedPayloadBytes := APayload.payload_raw;
end;
// LUnencryptedPayloadBytes Has Value in RAW
if (AEPasa.PayloadType.HasTrait(ptAsciiFormatted)) then begin
AEPasa.Payload := LUnencryptedPayloadBytes.ToString;
end else if (AEPasa.PayloadType.HasTrait(ptHexFormatted)) then begin
AEPasa.Payload := THexEncoding.Encode(LUnencryptedPayloadBytes,True);
end else if (AEPasa.PayloadType.HasTrait(ptBase58Formatted)) then begin
AEPasa.Payload := TPascalBase58Encoding.Encode(LUnencryptedPayloadBytes);
end else begin
// Internal error
ADecodeEPasaResult := der_Undefined;
Exit(False);
end;
Result := true;
end;
class function TEPasaDecoder.CheckEPasa(const ASender: TRPCProcess;
const AMethodName: String; AInputParams, AJSONResponse: TPCJSONObject;
var AErrorNum: Integer; var AErrorDesc: String): Boolean;
begin
Result := CheckEPAsa(ASender,AInputParams.AsString('account_epasa',''),AJSONResponse,AErrorNum,AErrorDesc);
end;
class function TEPasaDecoder.CheckEPasa(const ASender: TRPCProcess;
const AAccount_EPasa: String; AJSONResponse: TPCJSONObject;
var AErrorNum: Integer; var AErrorDesc: String): Boolean;
var LEPasa : TEPasa;
LResultObject : TPCJSONObject;
begin
if Not TEPasa.TryParse(AAccount_EPasa,LEPasa) then begin
AErrorNum := CT_RPC_ErrNum_InvalidEPASA;
AErrorDesc := 'Not a valid epasa: '+AAccount_EPasa;
Result := False;
Exit(False);
end else begin
Result := True;
LResultObject := AJSONResponse.GetAsObject('result');
LResultObject.GetAsVariant('account_epasa').Value := LEPasa.ToString(False);
LResultObject.GetAsVariant('account_epasa_classic').Value := LEPasa.ToClassicPASAString;
if LEPasa.PayloadType.HasTrait(ptAddressedByName) then begin
LResultObject.GetAsVariant('account').Value := LEPasa.AccountName;
end else begin
LResultObject.GetAsVariant('account').Value := LEPasa.Account.Value;
end;
if LEPasa.PayloadType.HasTrait(ptPublic) then begin
LResultObject.GetAsVariant('payload_method').Value := 'none';
end else if LEPasa.PayloadType.HasTrait(ptSenderKeyEncrypted) then begin
LResultObject.GetAsVariant('payload_method').Value := 'sender';
end else if LEPasa.PayloadType.HasTrait(ptRecipientKeyEncrypted) then begin
LResultObject.GetAsVariant('payload_method').Value := 'dest';
end else if LEPasa.PayloadType.HasTrait(ptPasswordEncrypted) then begin
LResultObject.GetAsVariant('payload_method').Value := 'aes';
LResultObject.GetAsVariant('pwd').Value := LEPasa.Password;
end;
if LEPasa.PayloadType.HasTrait(ptAsciiFormatted) then begin
LResultObject.GetAsVariant('payload_encode').Value := 'string';
end else if LEPasa.PayloadType.HasTrait(ptHexFormatted) then begin
LResultObject.GetAsVariant('payload_encode').Value := 'hexa';
end else if LEPasa.PayloadType.HasTrait(ptBase58Formatted) then begin
LResultObject.GetAsVariant('payload_encode').Value := 'base58';
end;
LResultObject.GetAsVariant('payload').Value := LEPasa.GetRawPayloadBytes.ToHexaString;
LResultObject.GetAsVariant('payload_type').Value := LEPasa.PayloadType.ToProtocolValue;
LResultObject.GetAsVariant('is_pay_to_key').Value := LEPasa.IsPayToKey;
end;
end;
class function TEPasaDecoder.DecodeEPASA(AAccount: Cardinal;
const APayload: TOperationPayload; const ANode: TNode;
const AWalletKeys: TWalletKeys; const APasswords: TList<String>): String;
var LEPasa : TEPasa;
begin
if TryDecodeEPASA(AAccount,APayload,ANode,AWalletKeys,APasswords,LEPasa) then begin
Result := LEPasa.ToClassicPASAString;
end else Result := '';
end;
class function TEPasaDecoder.TryDecodeEPASA(AAccount: Cardinal;
const APayload: TOperationPayload; const ANode : TNode; const AWalletKeys: TWalletKeys;
const APasswords: TList<String>; out AEPasa: TEPasa): Boolean;
var LDecodeEPasaResult: TDecodeEPasaResult;
begin
Result := TryDecodeEPASA(AAccount,APayload,ANode,AWalletKeys,APasswords,LDecodeEPasaResult,AEPasa);
end;
class function TEPasaDecoder.ValidateEPasa(const ASender: TRPCProcess;
const AMethodName: String; AInputParams, AJSONResponse: TPCJSONObject;
var AErrorNum: Integer; var AErrorDesc: String): Boolean;
var
s : String;
card : Cardinal;
LEPasaStr, LDelimStart,LDelimEnd, LPwdZone, LPayload : String;
LRawPayload : TRawBytes;
begin
LEPasaStr := '';
LPwdZone := '';
LEPasaStr := AInputParams.AsString('account','');
s := Trim(AInputParams.AsString('payload_method','none'));
if s='dest' then begin
LDelimStart := '(';
LDelimEnd := ')';
end else if s='sender' then begin
LDelimStart := '<';
LDelimEnd := '>';
end else if s='aes' then begin
LDelimStart := '{';
LDelimEnd := '}';
LPwdZone := ':' + AInputParams.AsString('pwd','');
end else if (s='none') or (trim(s)='') then begin
LDelimStart := '[';
LDelimEnd := ']';
end else begin
AErrorNum := CT_RPC_ErrNum_InvalidData;
AErrorDesc := Format('"payload_method" %s not valid',[s]);
Exit(False);
end;
s := Trim(AInputParams.AsString('payload',''));
if Not TCrypto.HexaToRaw(s,LRawPayload) then begin
AErrorNum := CT_RPC_ErrNum_InvalidData;
AErrorDesc := Format('"payload" is not an HEXASTRING: %s',[s]);
Exit(False);
end;
s := Trim(AInputParams.AsString('payload_encode','string'));
if s='hexa' then begin
LPayload := '0x'+LRawPayload.ToHexaString;
end else if s='base58' then begin
LPayload := TPascalBase58Encoding.Encode(LRawPayload);
end else if (s='string') or (Trim(s)='') then begin
LPayload := '"'+TPascalAsciiEncoding.Escape(LRawPayload.ToString)+'"';
end else begin
AErrorNum := CT_RPC_ErrNum_InvalidData;
AErrorDesc := Format('"payload_encode" %s not valid',[s]);
Exit(False);
end;
LEPasaStr := AInputParams.AsString('account','') + LDelimStart + LPayload + LPwdZone + LDelimEnd;
Result := CheckEPasa(ASender,LEPasaStr,AJSONResponse,AErrorNum,AErrorDesc);
end;
initialization
TRPCProcess.RegisterProcessMethod('validateepasa',TEPasaDecoder.ValidateEPasa);
TRPCProcess.RegisterProcessMethod('checkepasa',TEPasaDecoder.CheckEPasa);
finalization
TRPCProcess.UnregisterProcessMethod('validateepasa');
TRPCProcess.UnregisterProcessMethod('checkepasa');
end.