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
#[cfg(feature = "full-serde")]
mod lenient;
#[cfg(feature = "full-serde")]
pub use lenient::LenientTokenizer;
#[cfg(feature = "full-serde")]
mod strict;
#[cfg(feature = "full-serde")]
pub use strict::StrictTokenizer;
mod token;
pub use token::Token;
#[cfg(all(feature = "serde", not(feature = "std")))]
use crate::no_std_prelude::*;
#[cfg(feature = "serde")]
use core::cmp::Ordering::{Equal, Less};
#[cfg(feature = "serde")]
use crate::{Error, ParamType};
#[cfg(feature = "serde")]
pub trait Tokenizer {
fn tokenize(param: &ParamType, value: &str) -> Result<Token, Error> {
match *param {
ParamType::Address => {
Self::tokenize_address(value.strip_prefix("0x").unwrap_or(value)).map(|a| Token::Address(a.into()))
}
ParamType::String => Self::tokenize_string(value).map(Token::String),
ParamType::Bool => Self::tokenize_bool(value).map(Token::Bool),
ParamType::Bytes => Self::tokenize_bytes(value.strip_prefix("0x").unwrap_or(value)).map(Token::Bytes),
ParamType::FixedBytes(len) => {
Self::tokenize_fixed_bytes(value.strip_prefix("0x").unwrap_or(value), len).map(Token::FixedBytes)
}
ParamType::Uint(_) => Self::tokenize_uint(value).map(Into::into).map(Token::Uint),
ParamType::Int(_) => Self::tokenize_int(value).map(Into::into).map(Token::Int),
ParamType::Array(ref p) => Self::tokenize_array(value, p).map(Token::Array),
ParamType::FixedArray(ref p, len) => Self::tokenize_fixed_array(value, p, len).map(Token::FixedArray),
ParamType::Tuple(ref p) => Self::tokenize_struct(value, p).map(Token::Tuple),
}
}
fn tokenize_fixed_array(value: &str, param: &ParamType, len: usize) -> Result<Vec<Token>, Error> {
let result = Self::tokenize_array(value, param)?;
match result.len() == len {
true => Ok(result),
false => Err(Error::InvalidData),
}
}
fn tokenize_struct(value: &str, param: &[ParamType]) -> Result<Vec<Token>, Error> {
if !value.starts_with('(') || !value.ends_with(')') {
return Err(Error::InvalidData);
}
if value.chars().count() == 2 {
return Ok(vec![]);
}
let mut result = vec![];
let mut nested = 0isize;
let mut ignore = false;
let mut last_item = 1;
let mut array_nested = 0isize;
let mut array_item_start = 1;
let mut last_is_array = false;
let mut params = param.iter();
for (pos, ch) in value.chars().enumerate() {
match ch {
'[' if !ignore => {
if array_nested == 0 {
array_item_start = pos;
}
array_nested += 1;
}
']' if !ignore => {
array_nested -= 1;
if nested > 0 {
continue;
}
match array_nested.cmp(&0) {
Less => {
return Err(Error::InvalidData);
}
Equal => {
let sub = &value[array_item_start..pos + 1];
let token = Self::tokenize(params.next().ok_or(Error::InvalidData)?, sub)?;
result.push(token);
last_is_array = !last_is_array;
}
_ => {}
}
}
_ if array_nested != 0 => continue,
'(' if !ignore => {
nested += 1;
}
')' if !ignore && last_is_array => {
nested -= 1;
last_is_array = !last_is_array;
}
')' if !ignore => {
nested -= 1;
match nested.cmp(&0) {
Less => {
return Err(Error::InvalidData);
}
Equal => {
if last_is_array {
last_is_array = !last_is_array;
} else {
let sub = &value[last_item..pos];
let token = Self::tokenize(params.next().ok_or(Error::InvalidData)?, sub)?;
result.push(token);
last_item = pos + 1;
}
}
_ => {}
}
}
'"' => {
ignore = !ignore;
}
',' if array_nested == 0 && nested == 1 && !ignore && last_is_array => {
last_is_array = !last_is_array;
}
',' if nested == 1 && !ignore => {
let sub = &value[last_item..pos];
let token = Self::tokenize(params.next().ok_or(Error::InvalidData)?, sub)?;
result.push(token);
last_item = pos + 1;
}
_ => (),
}
}
if ignore {
return Err(Error::InvalidData);
}
Ok(result)
}
fn tokenize_array(value: &str, param: &ParamType) -> Result<Vec<Token>, Error> {
if !value.starts_with('[') || !value.ends_with(']') {
return Err(Error::InvalidData);
}
if value.chars().count() == 2 {
return Ok(vec![]);
}
let mut result = vec![];
let mut nested = 0isize;
let mut ignore = false;
let mut last_item = 1;
let mut tuple_nested = 0isize;
let mut tuple_item_start = 1;
let mut last_is_tuple = false;
for (i, ch) in value.chars().enumerate() {
match ch {
'(' if !ignore => {
if tuple_nested == 0 {
tuple_item_start = i;
}
tuple_nested += 1;
}
')' if !ignore => {
tuple_nested -= 1;
match tuple_nested.cmp(&0) {
Less => {
return Err(Error::InvalidData);
}
Equal => {
let sub = &value[tuple_item_start..i + 1];
let token = Self::tokenize(param, sub)?;
result.push(token);
last_is_tuple = !last_is_tuple;
}
_ => {}
}
}
_ if tuple_nested != 0 => continue,
'[' if !ignore => {
nested += 1;
}
']' if !ignore && last_is_tuple => {
nested -= 1;
last_is_tuple = !last_is_tuple;
}
']' if !ignore => {
nested -= 1;
match nested.cmp(&0) {
Less => {
return Err(Error::InvalidData);
}
Equal => {
if last_is_tuple {
last_is_tuple = !last_is_tuple;
} else {
let sub = &value[last_item..i];
let token = Self::tokenize(param, sub)?;
result.push(token);
last_item = i + 1;
}
}
_ => {}
}
}
'"' => {
ignore = !ignore;
}
',' if tuple_nested == 0 && nested == 1 && !ignore && last_is_tuple => {
last_is_tuple = !last_is_tuple;
}
',' if tuple_nested == 0 && nested == 1 && !ignore => {
let sub = &value[last_item..i];
let token = Self::tokenize(param, sub)?;
result.push(token);
last_item = i + 1;
}
_ => (),
}
}
if ignore {
return Err(Error::InvalidData);
}
Ok(result)
}
fn tokenize_address(value: &str) -> Result<[u8; 20], Error>;
fn tokenize_string(value: &str) -> Result<String, Error>;
fn tokenize_bool(value: &str) -> Result<bool, Error>;
fn tokenize_bytes(value: &str) -> Result<Vec<u8>, Error>;
fn tokenize_fixed_bytes(value: &str, len: usize) -> Result<Vec<u8>, Error>;
fn tokenize_uint(value: &str) -> Result<[u8; 32], Error>;
fn tokenize_int(value: &str) -> Result<[u8; 32], Error>;
}
#[cfg(all(test, feature = "full-serde"))]
mod test {
use super::{LenientTokenizer, ParamType, Tokenizer};
use crate::Token;
#[test]
fn single_quoted_in_array_must_error() {
assert!(LenientTokenizer::tokenize_array("[1,\"0,false]", &ParamType::Bool).is_err());
assert!(LenientTokenizer::tokenize_array("[false\"]", &ParamType::Bool).is_err());
assert!(LenientTokenizer::tokenize_array("[1,false\"]", &ParamType::Bool).is_err());
assert!(LenientTokenizer::tokenize_array("[1,\"0\",false]", &ParamType::Bool).is_err());
assert!(LenientTokenizer::tokenize_array("[1,0]", &ParamType::Bool).is_ok());
}
#[test]
fn tuples_arrays_mixed() {
assert_eq!(
LenientTokenizer::tokenize_array(
"[([(true)],[(false,true)])]",
&ParamType::Tuple(vec![
ParamType::Array(Box::new(ParamType::Tuple(vec![ParamType::Bool]))),
ParamType::Array(Box::new(ParamType::Tuple(vec![ParamType::Bool, ParamType::Bool]))),
]),
)
.unwrap(),
vec![Token::Tuple(vec![
Token::Array(vec![Token::Tuple(vec![Token::Bool(true)])]),
Token::Array(vec![Token::Tuple(vec![Token::Bool(false), Token::Bool(true)])]),
])]
);
assert_eq!(
LenientTokenizer::tokenize_struct(
"([(true)],[(false,true)])",
&[
ParamType::Array(Box::new(ParamType::Tuple(vec![ParamType::Bool]))),
ParamType::Array(Box::new(ParamType::Tuple(vec![ParamType::Bool, ParamType::Bool]))),
]
)
.unwrap(),
vec![
Token::Array(vec![Token::Tuple(vec![Token::Bool(true)])]),
Token::Array(vec![Token::Tuple(vec![Token::Bool(false), Token::Bool(true)])]),
]
);
}
#[test]
fn tuple_array_nested() {
assert_eq!(
LenientTokenizer::tokenize_struct(
"([(5c9d55b78febcc2061715ba4f57ecf8ea2711f2c)],2)",
&[ParamType::Array(Box::new(ParamType::Tuple(vec![ParamType::Address,],)),), ParamType::Uint(256,),]
)
.unwrap(),
vec![
Token::Array(vec![Token::Tuple(vec![Token::Address(
"0x5c9d55b78febcc2061715ba4f57ecf8ea2711f2c".parse().unwrap(),
),])]),
Token::Uint(2u64.into()),
]
);
}
}