forked from svanas/delphereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.eth.abi.pas
369 lines (337 loc) · 11.6 KB
/
web3.eth.abi.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
{******************************************************************************}
{ }
{ 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.abi;
{$I web3.inc}
interface
uses
// Delphi
System.Generics.Collections,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3;
type
TContractArray = TList<Variant>;
IContractStruct = interface
['{CA0C794D-6280-4AB1-9E91-4DE3443DFD1B}']
function Tuple: TArray<Variant>;
end;
function tuple(const args: array of Variant): Variant;
function &array(const args: array of Variant): TContractArray; overload;
function &array(const args: array of TAddress): TContractArray; overload;
function &array(const args: array of BigInteger): TContractArray; overload;
function encode(const func: string; const args: array of const): string;
implementation
uses
// Delphi
System.SysUtils,
System.Variants,
// web3
web3.utils;
function tuple(const args: array of Variant): Variant;
begin
Result := VarArrayCreate([0, High(args)], varVariant);
for var I := 0 to High(args) do Result[I] := args[I];
end;
function &array(const args: array of Variant): TContractArray;
begin
Result := TContractArray.Create;
for var arg in args do Result.Add(arg);
end;
function &array(const args: array of TAddress): TContractArray;
begin
Result := TContractArray.Create;
for var arg in args do Result.Add(arg);
end;
function &array(const args: array of BigInteger): TContractArray;
begin
Result := TContractArray.Create;
for var arg in args do Result.Add(web3.utils.toHex(arg));
end;
function encode(const func: string; const args: array of const): string;
// https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#argument-encoding
function encodeArgs(const args: array of const): TBytes;
function encodeArg(const bool: Boolean): TBytes; overload;
begin
Result := web3.utils.fromHex('0x' + IntToHex(Ord(bool), 64));
end;
function encodeArg(const int: Integer): TBytes; overload;
begin
Result := web3.utils.fromHex('0x' + IntToHex(int, 64));
end;
function encodeArg(const int: Int64): TBytes; overload;
begin
Result := web3.utils.fromHex('0x' + IntToHex(int, 64));
end;
function encodeArg(const str: string): TBytes; overload;
var
buf: TBytes;
hex: string;
begin
const prefix = Copy(str, System.Low(str), 2).ToLower;
if prefix <> '0x' then
begin
if prefix = '0b' then // bytes
begin
hex := web3.utils.toHex(BigInteger.Create(str), [padToEven, noPrefix]);
buf := web3.utils.fromHex(hex);
end
else // string literal
begin
buf := TEncoding.UTF8.GetBytes(str);
hex := web3.utils.toHex('', buf);
end;
while (hex = '') or (Length(hex) mod 64 <> 0) do hex := hex + '0';
if Length(buf) > 0 then hex := IntToHex(Length(buf), 64) + hex;
hex := '0x' + hex;
end
else // number or address
begin
buf := web3.utils.fromHex(str);
if Length(buf) = 32 then
hex := str
else
hex := web3.utils.toHex(buf, 32 - Length(buf), 32);
end;
Result := web3.utils.fromHex(hex);
end;
function isDynamic(const arg: Variant): Boolean; overload;
begin
Result := False;
case FindVarData(arg)^.VType of
varUnknown:
begin
var struct: IContractStruct;
if Supports(arg, IContractStruct, struct) then
Result := isDynamic(tuple(struct.Tuple));
end;
varOleStr,
varStrArg,
varUStrArg,
varString,
varUString:
Result := Copy(string(arg), System.Low(string(arg)), 2).ToLower <> '0x';
else
if VarIsArray(arg) then // tuple is dynamic if any of the elements is dynamic
for var I := VarArrayLowBound(arg, 1) to VarArrayHighBound(arg, 1) do
begin
Result := isDynamic(VarArrayGet(arg, [I]));
if Result then
EXIT;
end;
end;
end;
function varArrayCount(const arg: Variant): Integer;
begin
Result := 0;
if VarIsArray(arg) then
begin
Result := VarArrayHighBound(arg, 1) - VarArrayLowBound(arg, 1) + 1;
for var I := VarArrayLowBound(arg, 1) to VarArrayHighBound(arg, 1) do
begin
const itm = VarArrayGet(arg, [I]);
if VarIsArray(itm) then
Result := Result + varArrayCount(itm) - 1;
end;
end;
end;
function encodeArg(const arg: Variant): TBytes; overload;
begin
Result := [];
case FindVarData(arg)^.VType of
varSmallint,
varShortInt,
varInteger:
Result := encodeArg(Integer(arg));
varByte,
varWord,
varUInt32:
Result := encodeArg(Cardinal(arg));
varInt64:
Result := encodeArg(Int64(arg));
varUInt64:
Result := encodeArg(UInt64(arg));
varOleStr,
varStrArg,
varUStrArg,
varString,
varUString:
Result := encodeArg(string(arg));
varBoolean:
Result := encodeArg(Boolean(arg));
varUnknown:
begin
var struct: IContractStruct;
if Supports(arg, IContractStruct, struct) then
Result := encodeArg(tuple(struct.Tuple));
end;
else
if VarIsArray(arg) then // tuple
begin
var suffix: TBytes;
var offset: Integer := varArrayCount(arg) * 32;
for var idx := VarArrayLowBound(arg, 1) to VarArrayHighBound(arg, 1) do
begin
const elem = VarArrayGet(arg, [idx]);
const curr = encodeArg(elem);
if isDynamic(elem) then
begin
Result := Result + encodeArg(offset);
suffix := suffix + curr;
offset := offset + Length(curr);
end
else
Result := Result + curr;
end;
Result := Result + suffix;
end;
end;
end;
function encodeArg(const arg: TVarRec): TBytes; overload;
begin
case arg.VType of
vtBoolean:
Result := encodeArg(arg.VBoolean);
vtInteger:
Result := encodeArg(arg.VInteger);
vtString:
Result := encodeArg(UnicodeString(PShortString(arg.VAnsiString)^));
vtWideString:
Result := encodeArg(WideString(arg.VWideString^));
vtInt64:
Result := encodeArg(arg.VInt64^);
vtUnicodeString:
Result := encodeArg(string(arg.VUnicodeString));
vtVariant:
Result := encodeArg(arg.VVariant^);
vtInterface:
begin
var struct: IContractStruct;
if Supports(IInterface(arg.VInterface), IContractStruct, struct) then
Result := encodeArg(tuple(struct.Tuple));
end;
vtObject:
if arg.VObject is TContractArray then // array
begin
var suffix: TBytes;
var offset: Integer := (arg.VObject as TContractArray).Count * 32;
Result := encodeArg((arg.VObject as TContractArray).Count);
for var elem in arg.VObject as TContractArray do
begin
const curr = encodeArg(elem);
if isDynamic(elem) then
begin
Result := Result + encodeArg(offset);
suffix := suffix + curr;
offset := offset + Length(curr);
end
else
Result := Result + curr;
end;
Result := Result + suffix;
end;
end;
end;
function isDynamic(const arg: TVarRec): Boolean; overload;
begin
Result := False;
case arg.VType of
vtVariant:
Result := isDynamic(arg.VVariant^);
vtObject:
Result := arg.VObject is TContractArray;
vtInterface:
begin
var struct: IContractStruct;
if Supports(IInterface(arg.VInterface), IContractStruct, struct) then
Result := isDynamic(tuple(struct.Tuple));
end;
vtString, vtWideString, vtUnicodeString:
begin
var S: string;
case arg.VType of
vtString:
S := UnicodeString(PShortString(arg.VAnsiString)^);
vtWideString:
S := WideString(arg.VWideString^);
vtUnicodeString:
S := string(arg.VUnicodeString);
end;
Result := Copy(S, System.Low(S), 2).ToLower <> '0x';
end;
end;
end;
function len(const args: array of const): Integer;
function len(const arg: TVarRec): Integer;
begin
Result := 1;
if arg.VType = vtInterface then
begin
var struct: IContractStruct;
if Supports(IInterface(arg.VInterface), IContractStruct, struct) then
begin
const T = tuple(struct.Tuple);
if not isDynamic(T) then Result := varArrayCount(T);
end;
end;
end;
begin
Result := 0;
for var arg in args do Result := Result + len(arg);
end;
begin
Result := [];
var suffix: TBytes;
var offset: Integer := len(args) * 32;
for var arg in args do
begin
const curr = encodeArg(arg);
if isDynamic(arg) then
begin
Result := Result + encodeArg(offset);
suffix := suffix + curr;
offset := offset + Length(curr);
end
else
Result := Result + curr;
end;
Result := Result + suffix;
end;
begin
// step #1: encode the args into a byte array
var data: TBytes;
try
data := encodeArgs(args);
finally
for var arg in args do
if (arg.VType = vtObject) and (arg.VObject is TContractArray) then
arg.VObject.Free;
end;
// step #2: the first four bytes specify the function to be called
const hash = web3.utils.sha3(web3.utils.toHex(func));
data := Copy(hash, 0, 4) + data;
// step #3: hex-encode the data
Result := web3.utils.toHex(data);
end;
end.