forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.types.pas
598 lines (518 loc) · 16.3 KB
/
web3.eth.types.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under GNU AGPL v3.0 with Commons Clause }
{ }
{ This program is free software: you can redistribute it and/or modify }
{ it under the terms of the GNU Affero General Public License as published }
{ by the Free Software Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU Affero General Public License for more details. }
{ }
{ You should have received a copy of the GNU Affero General Public License }
{ along with this program. If not, see <https://www.gnu.org/licenses/> }
{ }
{******************************************************************************}
unit web3.eth.types;
{$I web3.inc}
interface
uses
// Delphi
System.Classes,
System.JSON,
System.Net.HttpClient,
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// CryptoLib4Pascal
ClpIECPrivateKeyParameters,
// web3
web3;
type
TBytes32 = array[0..31] of Byte;
TArg = record
Inner: TBytes32;
public
function toAddress: TAddress;
function toBytes32: TBytes32;
function toHex(const prefix: string): string;
function toInt: Integer;
function toInt64: Int64;
function toInt256: BigInteger;
function toUInt256: BigInteger;
function toBoolean: Boolean;
function toString: string;
function toDateTime: TUnixDateTime;
end;
PArg = ^TArg;
TTuple = TArray<TArg>;
TTopics = array[0..3] of TArg;
IBlock = interface
function ToString: string;
function baseFeePerGas: TWei;
end;
ITransaction = interface
function &type: Byte;
function ToString: string;
function blockNumber: BigInteger; // block number where this transaction was in. null when its pending.
function timeStamp: TUnixDateTime; // the unix timestamp for when the transaction got mined.
function from: TAddress; // address of the sender.
function gasLimit: BigInteger; // gas limit provided by the sender.
function gasPrice: TWei; // gas price provided by the sender in Wei.
function maxPriorityFeePerGas: TWei; // EIP-1559-only
function maxFeePerGas: TWei; // EIP-1559-only
function input: string; // the data send along with the transaction.
function &to: TAddress; // address of the receiver. null when its a contract creation transaction.
function value: TWei; // value transferred in Wei.
end;
ITxReceipt = interface
function ToString: string;
function txHash: TTxHash; // hash of the transaction.
function from: TAddress; // address of the sender.
function &to: TAddress; // address of the receiver. null when it's a contract creation transaction.
function gasUsed: BigInteger; // the amount of gas used by this specific transaction.
function status: Boolean; // success or failure.
function effectiveGasPrice: TWei; // eip-1559-only
end;
TAddressHelper = record helper for TAddress
class function Zero: TAddress; static;
function IsZero: Boolean;
constructor Create(const arg: TArg); overload;
constructor Create(const hex: string); overload;
class procedure FromName(const client: IWeb3; const name: string; const callback: TProc<TAddress, IError>); static;
procedure ToString(const client: IWeb3; const callback: TProc<string, IError>; const abbreviated: Boolean = False);
function ToChecksum: TAddress;
function Abbreviated: string;
procedure IsEOA(const client: IWeb3; const callback: TProc<Boolean, IError>);
function SameAs(const other: TAddress): Boolean;
end;
TPrivateKey = record
private
Inner: TBytes32;
public
class function Zero: TPrivateKey; static;
function IsZero: Boolean;
class operator Explicit(const hex: string): TPrivateKey;
function ToString: string;
class operator Implicit(const value: IECPrivateKeyParameters): TPrivateKey;
class operator Implicit(const value: TPrivateKey): IECPrivateKeyParameters;
class function Generate: TPrivateKey; static;
class function Prompt(const &public: TAddress): IResult<TPrivateKey>; static;
function GetAddress: IResult<TAddress>;
end;
TTupleHelper = record helper for TTuple
function Add: PArg;
function Last: PArg;
function Empty: Boolean;
function Strings: Boolean;
function ToArray: TArray<TArg>;
function ToString: string;
function ToStrings: TStrings;
class function From(const hex: string): TTuple;
procedure Enumerate(const callback: TProc<TArg, TProc>;const done: TProc);
end;
function byteArrayToBytes32(const value: TBytes): TBytes32;
function bytes32ToByteArray(const value: TBytes32): TBytes;
implementation
uses
{$IFDEF FMX}
FMX.Dialogs,
{$ELSE}
VCL.Dialogs,
{$ENDIF}
// web3
web3.bip39,
web3.bip44,
web3.crypto,
web3.error,
web3.eth,
web3.eth.crypto,
web3.eth.ens,
web3.http,
web3.json,
web3.utils;
function byteArrayToBytes32(const value: TBytes): TBytes32;
begin
FillChar(Result, 32, 0);
var input := value;
while Length(input) < 32 do input := [0] + input;
Move(input[0], Result[0], 32);
end;
function bytes32ToByteArray(const value: TBytes32): TBytes;
begin
SetLength(Result, 32);
Move(value[0], Result[0], 32);
end;
{ TArg }
function TArg.toAddress: TAddress;
begin
Result := TAddress.Create(Self);
end;
function TArg.toBytes32: TBytes32;
begin
Result := Self.Inner;
end;
function TArg.toHex(const prefix: string): string;
const
Digits = '0123456789ABCDEF';
begin
Result := StringOfChar('0', Length(Inner) * 2);
try
for var I := 0 to Length(Inner) - 1 do
begin
Result[2 * I + 1] := Digits[(Inner[I] shr 4) + 1];
Result[2 * I + 2] := Digits[(Inner[I] and $F) + 1];
end;
finally
Result := prefix + Result;
end;
end;
function TArg.toInt: Integer;
begin
Result := StrToInt(Self.toHex('$'));
end;
function TArg.toInt64: Int64;
begin
Result := StrToInt64(Self.toHex('$'));
end;
function TArg.toInt256: BigInteger;
begin
const S = Self.toHex('0x');
Result := S;
if Copy(S, System.Low(S), 4) = '0xFF' then
Result := BigInteger.Zero - (web3.Infinite - Result);
end;
function TArg.toUInt256: BigInteger;
begin
Result := Self.toHex('0x');
end;
function TArg.toBoolean: Boolean;
begin
Result := Self.toInt <> 0;
end;
function TArg.toString: string;
begin
Result := TEncoding.UTF8.GetString(Inner);
end;
function TArg.toDateTime: TUnixDateTime;
begin
Result := Self.toInt64;
end;
{ TAddressHelper }
constructor TAddressHelper.Create(const arg: TArg);
begin
Self := TAddress.Create(arg.toHex('0x'));
end;
constructor TAddressHelper.Create(const hex: string);
begin
if not web3.utils.isHex(hex) then
begin
Self := TAddress.Zero;
EXIT;
end;
var buf := web3.utils.fromHex(hex);
if Length(buf) = 20 then
Self := TAddress(hex).ToChecksum
else
if Length(buf) < 20 then
begin
repeat
buf := [0] + buf;
until Length(buf) = 20;
Self := TAddress(web3.utils.toHex(buf)).ToChecksum;
end
else
Self := TAddress(web3.utils.toHex(Copy(buf, Length(buf) - 20, 20))).ToChecksum;
end;
class procedure TAddressHelper.FromName(const client: IWeb3; const name: string; const callback: TProc<TAddress, IError>);
begin
if web3.utils.isHex(name) then
callback(TAddress.Create(name), nil)
else
web3.eth.ens.addr(client, name, callback);
end;
procedure TAddressHelper.ToString(const client: IWeb3; const callback: TProc<string, IError>; const abbreviated: Boolean);
begin
const addr: TAddress = Self;
web3.eth.ens.reverse(client, addr, procedure(name: string; err: IError)
begin
if Assigned(err) then
begin
callback('', err);
EXIT;
end;
var output := (function: string
begin
if (name <> '')
and (name.ToLower <> '0x')
and (name.ToLower <> '0x0000000000000000000000000000000000000000') then
Result := name
else
Result := string(addr);
end)();
if abbreviated then
if isHex(output) then
output := Copy(output, System.Low(output), 8);
callback(output, nil);
end);
end;
function TAddressHelper.ToChecksum: TAddress;
begin
Result := '0x';
// take lowercase hex address sans 0x as input
var input := string(Self).ToLower;
while Copy(input, System.Low(input), 2).ToLower = '0x' do
Delete(input, System.Low(input), 2);
// treat the hex address as UTF-8 for keccak256 hashing
const hash = web3.utils.toHex('', web3.utils.sha3(TEncoding.UTF8.GetBytes(input)));
// iterate over each character in the hex address
for var I := System.Low(input) to High(input) do
// check if the corresponding hex digit (nibble) in the hash is 8 or higher
if CharInSet(input[I], ['a', 'b', 'c', 'd', 'e', 'f']) and (StrToInt('$' + hash[I]) > 7) then
Result := TAddress(string(Result) + input.ToUpper[I])
else
Result := TAddress(string(Result) + input[I]);
end;
function TAddressHelper.Abbreviated: string;
begin
Result := string(Self);
Result := Copy(Result, System.Low(Result), 7) + '..' + Copy(Result, System.High(Result) - 4, MaxInt);
end;
procedure TAddressHelper.IsEOA(const client: IWeb3; const callback: TProc<Boolean, IError>);
begin
client.Call('eth_getCode', [Self.ToChecksum, BLOCK_LATEST], procedure(response: TJsonObject; err: IError)
begin
if not Assigned(response) then
begin
callback(True, err);
EXIT;
end;
const code = web3.json.getPropAsStr(response, 'result');
callback((code = '') or (code = '0x') or (code = '0x0'), err);
end);
end;
class function TAddressHelper.Zero: TAddress;
begin
Result := '0x0000000000000000000000000000000000000000';
end;
function TAddressHelper.IsZero: Boolean;
begin
Result := (Self = '')
or (string(Self).ToLower = '0x')
or (string(Self).ToLower = '0x0')
or (string(Self).ToLower = '0x0000000000000000000000000000000000000000');
end;
function TAddressHelper.SameAs(const other: TAddress): Boolean;
begin
Result := SameText(string(self), string(other));
end;
{ TPrivateKey }
class function TPrivateKey.Zero: TPrivateKey;
begin
FillChar(Result.Inner, 32, 0);
end;
function TPrivateKey.IsZero: Boolean;
begin
for var I := 0 to 31 do
if Self.Inner[I] <> 0 then
begin
Result := False;
EXIT;
end;
Result := True;
end;
class function TPrivateKey.Generate: TPrivateKey;
begin
Result := web3.crypto.generatePrivateKey('ECDSA', SECP256K1);
end;
class operator TPrivateKey.Explicit(const hex: string): TPrivateKey;
begin
Result.Inner := byteArrayToBytes32(web3.utils.fromHex(hex));
end;
function TPrivateKey.ToString: string;
begin
Result := web3.utils.toHex('', bytes32ToByteArray(Self.Inner));
end;
class operator TPrivateKey.Implicit(const value: IECPrivateKeyParameters): TPrivateKey;
begin
Result.Inner := byteArrayToBytes32(value.D.ToByteArrayUnsigned);
end;
class operator TPrivateKey.Implicit(const value: TPrivateKey): IECPrivateKeyParameters;
begin
Result := web3.crypto.privateKeyFromByteArray('ECDSA', SECP256K1, bytes32ToByteArray(value.Inner));
end;
class function TPrivateKey.Prompt(const &public: TAddress): IResult<TPrivateKey>;
begin
var input: string;
TThread.Synchronize(nil, procedure
begin
{$WARN SYMBOL_DEPRECATED OFF}
input := Trim(InputBox(string(&public), 'Please paste your private key or your secret recovery phrase', ''));
{$WARN SYMBOL_DEPRECATED DEFAULT}
end);
if input = '' then
begin
Result := TResult<TPrivateKey>.Err(TPrivateKey.Zero, TCancelled.Create);
EXIT;
end;
var &private: TPrivateKey;
if web3.utils.isHex('', input) then
begin
if Length(input) <> SizeOf(TPrivateKey) - 1 then
begin
Result := TResult<TPrivateKey>.Err(TPrivateKey.Zero, 'Private key is invalid');
EXIT;
end;
&private := TPrivateKey(input);
end else
begin
Result := web3.bip44.wallet(&public, web3.bip39.seed(input, ''));
if Result.isErr or Result.Value.IsZero then
begin
Result := TResult<TPrivateKey>.Err(TPrivateKey.Zero, 'Secret recovery phrase is invalid');
EXIT;
end;
&private := Result.Value;
end;
const address = &private.GetAddress;
if address.isErr then
begin
Result := TResult<TPrivateKey>.Err(TPrivateKey.Zero, address.Error);
EXIT;
end;
if address.Value.ToChecksum <> &public.ToChecksum then
begin
if web3.utils.isHex('', input) then
Result := TResult<TPrivateKey>.Err(TPrivateKey.Zero, 'Private key is invalid')
else
Result := TResult<TPrivateKey>.Err(TPrivateKey.Zero, 'Secret recovery phrase is invalid');
EXIT;
end;
Result := TResult<TPrivateKey>.Ok(&private);
end;
function TPrivateKey.GetAddress: IResult<TAddress>;
begin
try
Result := TResult<TAddress>.Ok(web3.eth.crypto.publicKeyToAddress(web3.crypto.publicKeyFromPrivateKey(Self)));
except
Result := TResult<TAddress>.Err(TAddress.Zero, 'Private key is invalid');
end;
end;
{ TTupleHelper }
function TTupleHelper.Add: PArg;
begin
SetLength(Self, Length(Self) + 1);
Result := Last;
end;
function TTupleHelper.Last: PArg;
begin
Result := nil;
if Length(Self) > 0 then
Result := @Self[High(Self)];
end;
function TTupleHelper.Empty: Boolean;
begin
Result := (Length(Self) < 2) or (Self[1].toInt64 = 0);
end;
function TTupleHelper.Strings: Boolean;
begin
Result := (not Self.Empty) and (Length(Self) > (Self[1].toInt + 2));
end;
function TTupleHelper.ToArray: TArray<TArg>;
begin
Result := [];
if Length(Self) < 3 then
EXIT;
const len = Self[1].toInt;
if len = 0 then
EXIT;
for var idx := 2 to High(Self) do
Result := Result + [Self[idx]];
SetLength(Result, len);
end;
function TTupleHelper.ToString: string;
begin
Result := '';
if Self.Empty then
EXIT;
if Self.Strings then
begin
const SL = Self.ToStrings;
if Assigned(SL) then
try
Result := TrimRight(SL.Text);
finally
SL.Free;
end;
EXIT;
end;
if Length(Self) < 3 then
EXIT;
const len = Self[1].toInt;
if len = 0 then
EXIT;
for var idx := 2 to High(Self) do
Result := Result + Self[idx].toString;
SetLength(Result, len);
end;
function TTupleHelper.ToStrings: TStrings;
begin
Result := nil;
if Length(Self) < 3 then
EXIT;
var len := Self[1].toInt;
if len = 0 then
EXIT;
Result := TStringList.Create;
for var idx := 2 to len + 1 do
begin
const ndx = Self[idx].toInt div SizeOf(TArg) + 2;
len := Self[ndx].toInt;
var str := Self[ndx + 1].toString;
SetLength(str, len);
Result.Add(str);
end;
end;
class function TTupleHelper.From(const hex: string): TTuple;
begin
var tup: TTuple;
var buf: TBytes := web3.utils.fromHex(hex);
while Length(buf) >= 32 do
begin
SetLength(tup, Length(tup) + 1);
Move(buf[0], tup[High(tup)].Inner[0], 32);
Delete(buf, 0, 32);
end;
Result := tup;
end;
procedure TTupleHelper.Enumerate(const callback: TProc<TArg, TProc>; const done: TProc);
begin
var Next: TProc<Integer, TArray<TArg>>;
Next := procedure(idx: Integer; arr: TArray<TArg>)
begin
if idx >= Length(arr) then
begin
if Assigned(done) then done;
EXIT;
end;
callback(arr[idx], procedure
begin
Next(idx + 1, arr);
end);
end;
if Self.Empty then
begin
if Assigned(done) then done;
EXIT;
end;
Next(0, Self.ToArray);
end;
end.