forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.utils.pas
237 lines (206 loc) · 6.16 KB
/
web3.utils.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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under Creative Commons NonCommercial (aka CC BY-NC) license. }
{ }
{******************************************************************************}
unit web3.utils;
{$I web3.inc}
interface
uses
// Delphi
System.JSON,
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// HashLib4Pascal
HlpSHA3,
// web3
web3,
web3.eth.types,
web3.json,
web3.json.rpc;
type
TToHex = set of (padToEven, zeroAs0x0);
function toHex(const buf: TBytes): string; overload;
function toHex(const bytes32: TBytes32): string; overload;
function toHex(const prefix: string; const buf: TBytes): string; overload;
function toHex(const buf: TBytes; offset, len: Integer): string; overload;
function toHex(const prefix: string; const buf: TBytes; offset, len: Integer): string; overload;
function toHex(const str: string): string; overload;
function toHex(const prefix, str: string): string; overload;
function toHex(const str: string; offset, len: Integer): string; overload;
function toHex(const prefix, str: string; offset, len: Integer): string; overload;
function toHex(val: TVarRec): string; overload;
function toHex(int: BigInteger; options: TToHex = []): string; overload;
function isHex(const str: string): Boolean; overload;
function isHex(const prefix, str: string): Boolean; overload;
function fromHex(hex: string): TBytes;
function sha3(const hex: string): TBytes; overload;
function sha3(const buf: TBytes): TBytes; overload;
procedure sha3(client: TWeb3; const hex: string; callback: TAsyncString); overload;
implementation
function toHex(const buf: TBytes): string;
begin
Result := toHex('0x', buf);
end;
function toHex(const bytes32: TBytes32): string;
var
buf: TBytes;
begin
SetLength(buf, 32);
Move(bytes32, buf[0], 32);
Result := toHex('0x', buf);
end;
function toHex(const prefix: string; const buf: TBytes): string;
begin
Result := toHex(prefix, buf, 0, Length(buf));
end;
function toHex(const buf: TBytes; offset, len: Integer): string;
begin
Result := toHex('0x', buf, offset, len);
end;
function toHex(const prefix: string; const buf: TBytes; offset, len: Integer): string;
const
Digits = '0123456789ABCDEF';
var
I: Integer;
begin
Result := StringOfChar('0', len * 2);
try
for I := 0 to Length(buf) - 1 do
begin
Result[2 * (I + offset) + 1] := Digits[(buf[I] shr 4) + 1];
Result[2 * (I + offset) + 2] := Digits[(buf[I] and $F) + 1];
end;
finally
Result := prefix + Result;
end;
end;
function toHex(const str: string): string;
begin
Result := toHex('0x', str);
end;
function toHex(const prefix, str: string): string;
begin
if isHex(prefix, str) then
Result := str
else
Result := toHex(prefix, TEncoding.UTF8.GetBytes(str));
end;
function toHex(const str: string; offset, len: Integer): string;
begin
Result := toHex('0x', str, offset, len);
end;
function toHex(const prefix, str: string; offset, len: Integer): string;
begin
Result := toHex(TEncoding.UTF8.GetBytes(str), offset, len);
end;
function toHex(val: TVarRec): string;
// if the length of the string is not even, then pad with a leading zero.
function pad(const str: string): string;
begin
if str = '0' then
Result := ''
else
if str.Length mod 2 = 0 then
Result := str
else
Result := '0' + str;
end;
begin
case val.VType of
vtInteger:
Result := '0x' + pad(IntToHex(val.VInteger, 0));
vtString:
Result := toHex(UnicodeString(PShortString(val.VAnsiString)^));
vtWideString:
Result := toHex(WideString(val.VWideString^));
vtInt64:
Result := '0x' + pad(IntToHex(val.VInt64^, 0));
vtUnicodeString:
Result := toHex(string(val.VUnicodeString));
end;
end;
function toHex(int: BigInteger; options: TToHex): string;
begin
if int.IsZero then
begin
if zeroAs0x0 in options then
Result := '0'
else
Result := ''
end
else
begin
Result := int.ToHexString;
if padToEven in options then
if Result.Length mod 2 > 0 then
Result := '0' + Result; // pad to even
end;
Result := '0x' + Result;
end;
function isHex(const str: string): Boolean;
begin
Result := isHex('0x', str);
end;
function isHex(const prefix, str: string): Boolean;
var
I: Integer;
begin
Result := Length(str) > 1;
if Result then
begin
Result := Copy(str, Low(str), Length(prefix)) = prefix;
if Result then
for I := Low(str) + Length(prefix) to High(str) do
begin
Result := CharInSet(str[I], ['0'..'9', 'a'..'f', 'A'..'F']);
if not Result then
EXIT;
end;
end;
end;
function fromHex(hex: string): TBytes;
var
I: Integer;
begin
hex := Trim(hex);
while Copy(hex, Low(hex), 2) = '0x' do
Delete(hex, Low(hex), 2);
if hex.Length mod 2 > 0 then
hex := '0' + hex; // pad to even
SetLength(Result, Length(hex) div 2);
for I := Low(hex) to Length(hex) div 2 do
Result[I - 1] := StrToInt('$' + Copy(hex, (I - 1) * 2 + 1, 2));
end;
function sha3(const hex: string): TBytes;
begin
Result := sha3(fromHex(hex));
end;
function sha3(const buf: TBytes): TBytes;
var
keccak256: TKeccak_256;
begin
keccak256 := TKeccak_256.Create;
try
Result := keccak256.ComputeBytes(buf).GetBytes;
finally
keccak256.Free;
end;
end;
procedure sha3(client: TWeb3; const hex: string; callback: TAsyncString);
begin
client.JsonRpc.Send(client.URL, client.Security, 'web3_sha3', [hex], procedure(resp: TJsonObject; err: IError)
begin
if Assigned(err) then
callback('', err)
else
callback(web3.json.getPropAsStr(resp, 'result'), nil);
end);
end;
end.