-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathweb3.utils.pas
305 lines (265 loc) · 8.91 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
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
{******************************************************************************}
{ }
{ 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.utils;
{$I web3.inc}
interface
uses
// Delphi
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3;
type
TToHex = set of (padToEven, zeroAs0x0, noPrefix);
function toHex(const buf: TBytes): string; overload;
function toHex(const prefix: string; const buf: TBytes): string; overload;
function toHex(const buf: TBytes; const offset, len: Integer): string; overload;
function toHex(const prefix: string; const buf: TBytes; const 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; const offset, len: Integer): string; overload;
function toHex(const prefix, str: string; const offset, len: Integer): string; overload;
function toHex(const val: TVarRec): string; overload;
function toHex(const int: BigInteger; const options: TToHex = []): string; overload;
function toBin(const int: BigInteger): string;
function isHex(const str: string): Boolean; overload;
function isHex(const prefix, str: string): Boolean; overload;
function fromHex(hex: string): TBytes;
function scale(const amount: Double; const decimals: Byte): BigInteger;
function unscale(const amount: BigInteger; const decimals: Byte): Double;
function sha3(const hex: string): TBytes; overload;
function sha3(const buf: TBytes): TBytes; overload;
procedure sha3(const client: IWeb3; const hex: string; const callback: TProc<string, IError>); overload;
function sha256(const input: TBytes): TBytes;
function hash160(const input: TBytes): TBytes;
function hmac_sha512(const msg, key: TBytes): TBytes;
implementation
uses
// Delphi
System.Classes,
System.Hash,
System.JSON,
System.Math,
// HashLib4Pascal
HlpIHash,
HlpIHashInfo,
HlpSHA3,
// CryptoLib4Pascal
ClpDigestUtilities,
ClpIDigest,
ClpHMac,
ClpKeyParameter,
// web3
web3.json;
function toHex(const buf: TBytes): string;
begin
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; const offset, len: Integer): string;
begin
Result := toHex('0x', buf, offset, len);
end;
function toHex(const prefix: string; const buf: TBytes; const offset, len: Integer): string;
const
Digits = '0123456789ABCDEF';
begin
Result := StringOfChar('0', len * 2);
try
for var 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; const offset, len: Integer): string;
begin
Result := toHex('0x', str, offset, len);
end;
function toHex(const prefix, str: string; const offset, len: Integer): string;
begin
Result := toHex(TEncoding.UTF8.GetBytes(str), offset, len);
end;
function toHex(const 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(const int: BigInteger; const options: TToHex): string;
begin
if int.IsZero then
begin
if zeroAs0x0 in options then
Result := '0'
else
Result := ''
end
else
begin
if int.IsNegative then
Result := (web3.Infinite - int.Abs).ToHexString
else
Result := int.ToHexString;
if padToEven in options then
if Result.Length mod 2 > 0 then
Result := '0' + Result; // pad to even
end;
if not(noPrefix in options) then
Result := '0x' + Result;
end;
function toBin(const int: BigInteger): string;
begin
Result := '0b' + int.ToBinaryString;
end;
function isHex(const str: string): Boolean;
begin
Result := isHex('0x', str);
end;
function isHex(const prefix, str: string): Boolean;
begin
Result := Length(str) > 1;
if Result then
begin
Result := SameText(Copy(str, System.Low(str), Length(prefix)), prefix);
if Result then
for var I := System.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;
begin
hex := Trim(hex);
while Copy(hex, System.Low(hex), 2).ToLower = '0x' do
Delete(hex, System.Low(hex), 2);
if hex.Length mod 2 > 0 then
hex := '0' + hex; // pad to even
SetLength(Result, Length(hex) div 2);
for var I := System.Low(hex) to Length(hex) div 2 do
Result[I - 1] := StrToInt('$' + Copy(hex, (I - 1) * 2 + 1, 2));
end;
function scale(const amount: Double; const decimals: Byte): BigInteger;
begin
Result := BigInteger.Create(amount * Round(Power(10, decimals)));
end;
function unscale(const amount: BigInteger; const decimals: Byte): Double;
begin
Result := amount.AsDouble / Round(Power(10, decimals));
end;
function sha3(const hex: string): TBytes;
begin
Result := sha3(fromHex(hex));
end;
function sha3(const buf: TBytes): TBytes;
begin
const keccak256 = TKeccak_256.Create;
try
Result := keccak256.ComputeBytes(buf).GetBytes;
finally
keccak256.Free;
end;
end;
procedure sha3(const client: IWeb3; const hex: string; const callback: TProc<string, IError>);
begin
client.Call('web3_sha3', [hex], procedure(response: TJsonObject; err: IError)
begin
if Assigned(err) then
callback('', err)
else
callback(web3.json.getPropAsStr(response, 'result'), nil);
end);
end;
function sha256(const input: TBytes): TBytes;
begin
const stream = TBytesStream.Create;
try
stream.Write(input, Length(input));
stream.Position := 0;
Result := THashSHA2.GetHashBytes(stream, THashSHA2.TSHA2Version.SHA256);
finally
stream.Free;
end;
end;
function hash160(const input: TBytes): TBytes;
begin
Result := sha256(input);
const digest = TDigestUtilities.GetDigest('RIPEMD160');
digest.BlockUpdate(Result, 0, System.Length(Result));
Result := TDigestUtilities.DoFinal(digest);
end;
function hmac_sha512(const msg, key: TBytes): TBytes;
begin
const hmac = THMac.Create(TDigestUtilities.GetDigest('SHA-512'));
try
hmac.Init(TKeyParameter.Create(key));
hmac.BlockUpdate(msg, 0, System.Length(msg));
System.SetLength(Result, hmac.GetMacSize());
hmac.DoFinal(Result, 0);
finally
hmac.Free;
end;
end;
end.