-
Notifications
You must be signed in to change notification settings - Fork 0
/
nosowallcon.pas
361 lines (327 loc) · 9.38 KB
/
nosowallcon.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
unit NosoWallCon;
{
nosowallcon 1.1
January 26th, 2024
Stand alone unit to control wallet addresses file and array
}
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, fileutil, nosodebug,nosocrypto,nosogeneral;
TYPE
WalletData = Packed Record
Hash : String[40]; // El hash publico o direccion
Custom : String[40]; // En caso de que la direccion este personalizada
PublicKey : String[255]; // clave publica
PrivateKey : String[255]; // clave privada
Balance : int64; // el ultimo saldo conocido de la direccion
Pending : int64; // el ultimo saldo de pagos pendientes
Score : int64; // estado del registro de la direccion.
LastOP : int64; // tiempo de la ultima operacion en UnixTime.
end;
Procedure ClearWalletArray();
function InsertToWallArr(LData:WalletData):boolean;
Function GetWallArrIndex(Index:integer):WalletData;
Function WallAddIndex(Address:String):integer;
Function LenWallArr():Integer;
Function ChangeWallArrPos(PosA,PosB:integer):boolean;
Procedure ClearWallPendings();
Procedure SetPendingForAddress(Index:integer;value:int64);
Function GetAddressFromFile(FileLocation:String;out WalletInfo:WalletData):Boolean;
Function ImportAddressesFromBackup(BakFolder:String):integer;
Function SaveAddresstoFile(FileName:string;LData:WalletData):boolean;
function CreateNewWallet():Boolean;
Function GetWalletAsStream(out LStream:TMemoryStream):int64;
Function SaveWalletToFile():boolean;
Function LoadWallet(wallet:String):Boolean;
Function VerifyAddressOnDisk(HashAddress:String):boolean;
var
WalletArray : array of walletData; // Wallet addresses
FileWallet : file of WalletData;
WalletFilename : string= 'NOSODATA'+DirectorySeparator+'wallet.pkw';
CS_WalletFile : TRTLCriticalSection;
CS_WalletArray : TRTLCriticalSection;
IMPLEMENTATION
Procedure ClearWalletArray();
Begin
EnterCriticalSection(CS_WalletArray);
setlength(WalletArray,0);
LeaveCriticalSection(CS_WalletArray);
End;
function InsertToWallArr(LData:WalletData):boolean;
Begin
result := false;
if WallAddIndex(LData.Hash)<0 then
begin
EnterCriticalSection(CS_WalletArray);
Insert(LData,WalletArray,length(WalletArray));
LeaveCriticalSection(CS_WalletArray);
Result := true;
end;
End;
Function GetWallArrIndex(Index:integer):WalletData;
Begin
EnterCriticalSection(CS_WalletArray);
if Index <= Length(WalletArray)-1 then
Result := WalletArray[Index]
else result := Default(WalletData);
LeaveCriticalSection(CS_WalletArray);
End;
Function WallAddIndex(Address:String):integer;
var
counter : integer;
Begin
Result := -1;
if ((Address ='') or (length(Address)<5)) then exit;
EnterCriticalSection(CS_WalletArray);
for counter := 0 to high(WalletArray) do
if ((WalletArray[counter].Hash = Address) or (WalletArray[counter].Custom = Address )) then
Begin
Result := counter;
break;
end;
LeaveCriticalSection(CS_WalletArray);
End;
Function LenWallArr():Integer;
Begin
EnterCriticalSection(CS_WalletArray);
Result := Length(WalletArray);
LeaveCriticalSection(CS_WalletArray);
End;
Function ChangeWallArrPos(PosA,PosB:integer):boolean;
var
oldData,NewData : WalletData;
Begin
Result := false;
if posA>LenWallArr-1 then exit;
if posB>LenWallArr-1 then exit;
if posA=posB then Exit;
OldData := GetWallArrIndex(posA);
NewData := GetWallArrIndex(posB);
EnterCriticalSection(CS_WalletArray);
WalletArray[posA] := NewData;
WalletArray[posB] := OldData;
LeaveCriticalSection(CS_WalletArray);
Result := true;
End;
Procedure ClearWallPendings();
var
counter : integer;
Begin
EnterCriticalSection(CS_WalletArray);
for counter := 0 to length(WalletArray)-1 do
WalletArray[counter].pending := 0;
LeaveCriticalSection(CS_WalletArray);
End;
Procedure SetPendingForAddress(Index:integer;value:int64);
Begin
if Index > LenWallArr-1 then exit;
EnterCriticalSection(CS_WalletArray);
WalletArray[Index].pending := value;
LeaveCriticalSection(CS_WalletArray);
End;
// Import an address data from a file
Function GetAddressFromFile(FileLocation:String;out WalletInfo:WalletData):Boolean;
var
TempFile : File of WalletData;
Opened : boolean = false;
Closed : boolean = false;
Begin
result := true;
AssignFile(TempFile,FileLocation);
TRY
Reset(TempFile);
Opened := true;
Read(TempFile,WalletInfo);
CloseFile(TempFile);
Closed := true;
EXCEPT on E:Exception do
begin
Result := false;
ToDeepDeb('NosoWallcon,GetAddressFromFile,'+E.Message);
end;
END;
If ( (opened) and (not Closed) ) then CloseFile(TempFile);
End;
// Verify if all baked up keys are present on active wallet
Function ImportAddressesFromBackup(BakFolder:String):integer;
Var
BakFiles : TStringList;
Counter : integer = 0;
ThisData : WalletData;
Begin
Result := 0;
BakFiles := TStringList.Create;
TRY
FindAllFiles(BakFiles, BakFolder, '*.pkw', true);
while Counter < BakFiles.Count do
begin
if GetAddressFromFile(BakFiles[Counter],ThisData) then
begin
if InsertToWallArr(ThisData) then inc(result);
end;
Inc(Counter);
end;
if result > 0 then ToDeepDeb(format('Imported %d addresses from backup files',[result]));
EXCEPT on E:Exception do
begin
ToDeepDeb('NosoWallcon,ImportAddressesFromBackup,'+E.Message);
end;
END;
BakFiles.free;
End;
// Saves an address info to a specific file
Function SaveAddresstoFile(FileName:string;LData:WalletData):boolean;
var
TempFile : File of WalletData;
opened : boolean = false;
Closed : boolean = false;
Begin
Result := true;
AssignFile(TempFile,FileName);
TRY
rewrite(TempFile);
opened := true;
write(TempFile,Ldata);
CloseFile(TempFile);
Closed := true;
EXCEPT on E:Exception do
begin
Result := false;
ToDeepDeb('NosoWallcon,SaveAddresstoFile,'+E.Message);
end;
END;
If ( (opened) and (not Closed) ) then CloseFile(TempFile);
End;
// Creates a new wallet file with a new generated address
function CreateNewWallet():Boolean;
var
NewAddress : WalletData;
PubKey,PriKey : string;
Begin
TRY
if not fileexists (WalletFilename) then // Check to avoid delete an existing file
begin
ClearWalletArray;
NewAddress := Default(WalletData);
NewAddress.Hash:=GenerateNewAddress(PubKey,PriKey);
NewAddress.PublicKey:=pubkey;
NewAddress.PrivateKey:=PriKey;
InsertToWallArr(NewAddress);
SaveWalletToFile;
end;
EXCEPT on E:Exception do
begin
ToDeepDeb('NosoWallcon,CreateNewWallet,'+E.Message);
end;
END; {TRY}
End;
// Load the wallet file into a memory stream
Function GetWalletAsStream(out LStream:TMemoryStream):int64;
Begin
Result := 0;
EnterCriticalSection(CS_WalletFile);
TRY
LStream.LoadFromFile(WalletFilename);
result:= LStream.Size;
LStream.Position:=0;
EXCEPT ON E:Exception do
begin
ToDeepDeb('NosoWallcon,GetWalletAsStream,'+E.Message);
end;
END{Try};
LeaveCriticalSection(CS_WalletFile);
End;
// Save the wallet array to the file
Function SaveWalletToFile():boolean;
var
MyStream : TMemoryStream;
Counter : integer;
Begin
Result := true;
TryCopyFile(WalletFilename,WalletFilename+'.bak');
MyStream:= TMemoryStream.Create;
MyStream.Position:=0;
EnterCriticalSection(CS_WalletFile);
EnterCriticalSection(CS_WalletArray);
for Counter := 0 to length(WalletArray)-1 do
begin
MyStream.Write(WalletArray[counter],SizeOf(WalletData));
end;
TRY
MyStream.SaveToFile(WalletFilename);
EXCEPT ON E:EXCEPTION DO
begin
ToDeepDeb('NosoWallcon,SaveWalletToFile,'+E.Message);
Result := false;
end;
END;
LeaveCriticalSection(CS_WalletArray);
LeaveCriticalSection(CS_WalletFile);
MyStream.Free;
If result = true then TryCopyFile(WalletFilename,WalletFilename+'.bak')
else TryCopyFile(WalletFilename+'.bak',WalletFilename);
End;
Function LoadWallet(wallet:String):Boolean;
var
MyStream : TMemoryStream;
ThisAddress : WalletData;
Counter : integer;
Records : integer;
Begin
Result := true;
MyStream := TMemoryStream.Create;
if fileExists(wallet) then
begin
Records := GetWalletAsStream(MyStream) div sizeof(WalletData);
if Records > 0 then
begin
ClearWalletArray;
For counter := 0 to records-1 do
begin
MyStream.Read(ThisAddress,Sizeof(WalletData));
InsertToWallArr(ThisAddress);
end;
end
else result := false;
end
else result := false;
MyStream.Free;
End;
Function VerifyAddressOnDisk(HashAddress:String):boolean;
var
MyStream : TMemoryStream;
ThisAddress : WalletData;
Counter : integer;
Records : integer;
Begin
Result := false;
MyStream := TMemoryStream.Create;
if fileExists(WalletFilename) then
begin
Records := GetWalletAsStream(MyStream) div sizeof(WalletData);
if Records > 0 then
begin
For counter := 0 to records-1 do
begin
MyStream.Read(ThisAddress,Sizeof(WalletData));
if ThisAddress.Hash=HashAddress then
begin
result := true;
break;
end;
end;
end
else result := false;
end
else result := false;
MyStream.Free;
End;
INITIALIZATION
InitCriticalSection(CS_WalletArray);
InitCriticalSection(CS_WalletFile);
SetLength(WalletArray,0);
FINALIZATION
DoneCriticalSection(CS_WalletArray);
DoneCriticalSection(CS_WalletFile);
END.