-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathweb3.rlp.pas
328 lines (294 loc) · 10.2 KB
/
web3.rlp.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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <svanas@runbox.com> }
{ 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.rlp;
{$I web3.inc}
interface
uses
// Delphi
System.SysUtils,
// web3
web3;
type
TDataType = (dtString, dtList);
TItem = record
Bytes : TBytes;
DataType: TDataType;
constructor Create(const aBytes: TBytes; const aDataType: TDataType);
end;
function encode(const item: Integer): IResult<TBytes>; overload;
function encode(const item: string): IResult<TBytes>; overload;
function encode(const item: TVarRec): IResult<TBytes>; overload;
function encode(const items: array of const): IResult<TBytes>; overload;
function recode(const items: array of TItem): IResult<TBytes>;
function decode(const input: TBytes): IResult<TArray<TItem>>;
implementation
uses
// Delphi
System.Math,
System.Variants,
// web3
web3.error,
web3.utils;
function encodeLength(const len, offset: Integer): IResult<TBytes>;
function toBinary(const x: Integer): TBytes;
begin
if x = 0 then
Result := []
else
begin
var i, r: Word;
DivMod(x, 256, i, r);
Result := toBinary(i) + [r];
end;
end;
begin
if len < 56 then
Result := TResult<TBytes>.Ok([len + offset])
else
if len < Power(256, 8) then
begin
const bin = toBinary(len);
Result := TResult<TBytes>.Ok([Length(bin) + offset + 55] + bin);
end
else
Result := TResult<TBytes>.Err('RLP input is too long');
end;
function encodeItem(const item: TBytes): IResult<TBytes>; overload;
begin
const len = Length(item);
if (len = 1) and (item[0] < $80) then
begin
Result := TResult<TBytes>.Ok(item);
EXIT;
end;
Result := encodeLength(len, $80);
if Result.isOk then
Result := TResult<TBytes>.Ok(Result.Value + item);
end;
function encodeItem(const item: Variant): IResult<TBytes>; overload;
begin
case FindVarData(item)^.VType of
varEmpty:
Result := TResult<TBytes>.Ok([]);
varSmallint,
varShortInt,
varInteger:
Result := encode(Integer(item));
varOleStr,
varStrArg,
varUStrArg,
varString,
varUString:
Result := encode(string(item));
else
if VarIsArray(item) then
begin
var output: TBytes := [];
for var I := VarArrayLowBound(item, 1) to VarArrayHighBound(item, 1) do
begin
Result := encodeItem(VarArrayGet(item, [I]));
if Result.isErr then
EXIT;
output := output + Result.Value;
end;
Result := encodeLength(Length(output), $c0);
if Result.isOk then
Result := TResult<TBytes>.Ok(Result.Value + output);
end
else
Result := TResult<TBytes>.Err('Cannot RLP-encode item');
end;
end;
function encode(const item: Integer): IResult<TBytes>;
begin
var arg: TVarRec;
arg.VType := vtInteger;
arg.VInteger := item;
Result := encode(arg);
end;
function encode(const item: string): IResult<TBytes>;
begin
var arg: TVarRec;
arg.VType := vtUnicodeString;
arg.VUnicodeString := Pointer(item);
Result := encode(arg);
end;
function encode(const item: TVarRec): IResult<TBytes>;
begin
if item.VType = vtVariant then
Result := encodeItem(item.VVariant^)
else
Result := encodeItem(web3.utils.fromHex(web3.utils.toHex(item)));
end;
function encode(const items: array of const): IResult<TBytes>;
begin
var output: TBytes := [];
for var item in items do
begin
Result := encode(item);
if Result.isErr then
EXIT;
output := output + Result.Value;
end;
Result := encodeLength(Length(output), $c0);
if Result.isOk then
Result := TResult<TBytes>.Ok(Result.Value + output);
end;
function recode(const items: array of TItem): IResult<TBytes>;
begin
var output: TBytes := [];
for var item in items do
begin
if item.DataType = dtString then
Result := encodeItem(item.Bytes)
else
if Length(item.Bytes) = 0 then
Result := encodeItem(VarArrayCreate([0, 0], varVariant))
else
Result := TResult<TBytes>.Err(TNotImplemented.Create);
if Result.isErr then
EXIT;
output := output + Result.Value;
end;
Result := encodeLength(Length(output), $c0);
if Result.isOk then
Result := TResult<TBytes>.Ok(Result.Value + output);
end;
type
TLength = record
Offset : Integer;
Length : Integer;
DataType: TDataType;
constructor Create(const aOffset, aLength: Integer; const aDataType: TDataType);
end;
constructor TLength.Create(const aOffset, aLength: Integer; const aDataType: TDataType);
begin
Self.Offset := aOffset;
Self.Length := aLength;
Self.DataType := aDataType;
end;
function decodeLength(const input: TBytes): IResult<TLength>;
function toInt(input: TBytes): IResult<Integer>;
begin
const len = Length(input);
if len = 0 then
Result := TResult<Integer>.Err('RLP input is null')
else
if len = 1 then
Result := TResult<Integer>.Ok(input[0])
else
begin
const I = toInt(Copy(input, 0, Length(input) - 1));
if I.isErr then
Result := TResult<Integer>.Err(I.Error)
else
Result := TResult<Integer>.Ok(input[High(input)] + I.Value * 256);
end;
end;
begin
const len = Length(input);
if len = 0 then
begin
Result := TResult<TLength>.Err('RLP input is null');
EXIT;
end;
const prefix = input[0];
// For a single byte whose value is in the [$00, $7F] range, that byte is its own RLP encoding.
if prefix <= $7F then
begin
Result := TResult<TLength>.Ok(TLength.Create(0, 1, dtString));
EXIT;
end;
// If a string is 0-55 bytes long, the RLP encoding consists of a single byte with value $80 plus
// the length of the string followed by the string. The range of the first byte is thus [$80, $B7].
if (prefix <= $B7) and (len > prefix - $80) then
begin
Result := TResult<TLength>.Ok(TLength.Create(1, prefix - $80, dtString));
EXIT;
end;
// If a string is more than 55 bytes long, the RLP encoding consists of a single byte with value
// $B7 plus the length of the length of the string in binary form, followed by the length of the
// string, followed by the string. The range of the first byte is thus [$B8, $BF].
if prefix <= $BF then
begin
const len_of_len = prefix - $B7;
if len > len_of_len then
begin
const len_of_payload = toInt(Copy(input, 1, len_of_len));
if len_of_payload.isOk and (len > len_of_len + len_of_payload.Value) then
begin
Result := TResult<TLength>.Ok(TLength.Create(1 + len_of_len, len_of_payload.Value, dtString));
EXIT;
end;
end;
end;
// If the total payload of a list (i.e. the combined length of all its items) is 0-55 bytes long, the
// RLP encoding consists of a single byte with value $C0 plus the length of the list followed by the
// concatenation of the RLP encodings of the items. The range of the first byte is thus [$C0, $F7].
if (prefix <= $F7) and (len > prefix - $C0) then
begin
Result := TResult<TLength>.Ok(TLength.Create(1, prefix - $C0, dtList));
EXIT;
end;
// If the total payload of a list is more than 55 bytes long, the RLP encoding consists of a
// single byte with value $F7 plus the length of the length of the payload in binary form,
// followed by the length of the payload, followed by the concatenation of the RLP encodings
// of the items. The range of the first byte is thus [0xF8, 0xFF].
const len_of_len = prefix - $F7;
if len > len_of_len then
begin
const len_of_payload = toInt(Copy(input, 1, len_of_len));
if len_of_payload.isOk and (len > len_of_len + len_of_payload.Value) then
begin
Result := TResult<TLength>.Ok(TLength.Create(1 + len_of_len, len_of_payload.Value, dtList));
EXIT;
end;
end;
Result := TResult<TLength>.Err('input does not conform to RLP encoding');
end;
constructor TItem.Create(const aBytes: TBytes; const aDataType: TDataType);
begin
Self.Bytes := aBytes;
Self.DataType := aDataType;
end;
function decode(const input: TBytes): IResult<TArray<TItem>>;
begin
if Length(input) = 0 then
begin
Result := TResult<TArray<TItem>>.Ok([]);
EXIT;
end;
const this = decodeLength(input);
if this.isErr then
begin
Result := TResult<TArray<TItem>>.Err(this.Error);
EXIT;
end;
var output: TArray<TItem> := [TItem.Create(Copy(input, this.Value.Offset, this.Value.Length), this.Value.DataType)];
const next = decode(Copy(input, this.Value.offset + this.Value.length));
if next.isOk then
for var I := 0 to High(next.Value) do output := output + [next.Value[I]];
Result := TResult<TArray<TItem>>.Ok(output);
end;
end.