1#[cfg(not(feature = "std"))]
12use crate::no_std_prelude::*;
13use crate::{Error, ParamType, Token, Word};
14
15#[derive(Debug)]
16struct DecodeResult {
17 token: Token,
18 new_offset: usize,
19}
20
21fn as_usize(slice: &Word) -> Result<usize, Error> {
22 if !slice[..28].iter().all(|x| *x == 0) {
23 return Err(Error::InvalidData);
24 }
25
26 let result = ((slice[28] as usize) << 24)
27 + ((slice[29] as usize) << 16)
28 + ((slice[30] as usize) << 8)
29 + (slice[31] as usize);
30
31 Ok(result)
32}
33
34fn as_bool(slice: &Word) -> Result<bool, Error> {
35 check_zeroes(&slice[..31])?;
36 Ok(slice[31] == 1)
37}
38
39fn decode_impl(types: &[ParamType], data: &[u8], validate: bool) -> Result<(Vec<Token>, usize), Error> {
40 let is_empty_bytes_valid_encoding = types.iter().all(|t| t.is_empty_bytes_valid_encoding());
41 if !is_empty_bytes_valid_encoding && data.is_empty() {
42 return Err(Error::InvalidName(
43 "please ensure the contract and method you're calling exist! \
44 failed to decode empty bytes. if you're using jsonrpc this is \
45 likely due to jsonrpc returning `0x` in case contract or method \
46 don't exist"
47 .into(),
48 ));
49 }
50
51 let mut tokens = vec![];
52 let mut offset = 0;
53
54 for param in types {
55 let res = decode_param(param, data, offset, validate)?;
56 offset = res.new_offset;
57 tokens.push(res.token);
58 }
59 if validate && offset != data.len() {
60 return Err(Error::InvalidData);
61 }
62
63 Ok((tokens, offset))
64}
65
66pub fn decode_validate(types: &[ParamType], data: &[u8]) -> Result<Vec<Token>, Error> {
69 decode_impl(types, data, true).map(|(tokens, _)| tokens)
70}
71
72pub fn decode(types: &[ParamType], data: &[u8]) -> Result<Vec<Token>, Error> {
74 decode_impl(types, data, false).map(|(tokens, _)| tokens)
75}
76
77fn peek(data: &[u8], offset: usize, len: usize) -> Result<&[u8], Error> {
78 if offset + len > data.len() {
79 Err(Error::InvalidData)
80 } else {
81 Ok(&data[offset..(offset + len)])
82 }
83}
84
85fn peek_32_bytes(data: &[u8], offset: usize) -> Result<Word, Error> {
86 peek(data, offset, 32).map(|x| {
87 let mut out: Word = [0u8; 32];
88 out.copy_from_slice(&x[0..32]);
89 out
90 })
91}
92
93fn round_up_nearest_multiple(value: usize, padding: usize) -> usize {
94 (value + padding - 1) / padding * padding
95}
96
97fn take_bytes(data: &[u8], offset: usize, len: usize, validate: bool) -> Result<Vec<u8>, Error> {
98 if validate {
99 let padded_len = round_up_nearest_multiple(len, 32);
100 if offset + padded_len > data.len() {
101 return Err(Error::InvalidData);
102 }
103 check_zeroes(&data[(offset + len)..(offset + padded_len)])?;
104 } else if offset + len > data.len() {
105 return Err(Error::InvalidData);
106 }
107 Ok(data[offset..(offset + len)].to_vec())
108}
109
110fn check_zeroes(data: &[u8]) -> Result<(), Error> {
111 if data.iter().all(|b| *b == 0) {
112 Ok(())
113 } else {
114 Err(Error::InvalidData)
115 }
116}
117
118fn decode_param(param: &ParamType, data: &[u8], offset: usize, validate: bool) -> Result<DecodeResult, Error> {
119 match *param {
120 ParamType::Address => {
121 let slice = peek_32_bytes(data, offset)?;
122 if validate {
123 check_zeroes(&slice[..12])?;
124 }
125 let mut address = [0u8; 20];
126 address.copy_from_slice(&slice[12..]);
127 let result = DecodeResult { token: Token::Address(address.into()), new_offset: offset + 32 };
128 Ok(result)
129 }
130 ParamType::Int(_) => {
131 let slice = peek_32_bytes(data, offset)?;
132 let result = DecodeResult { token: Token::Int(slice.into()), new_offset: offset + 32 };
133 Ok(result)
134 }
135 ParamType::Uint(_) => {
136 let slice = peek_32_bytes(data, offset)?;
137 let result = DecodeResult { token: Token::Uint(slice.into()), new_offset: offset + 32 };
138 Ok(result)
139 }
140 ParamType::Bool => {
141 let b = as_bool(&peek_32_bytes(data, offset)?)?;
142 let result = DecodeResult { token: Token::Bool(b), new_offset: offset + 32 };
143 Ok(result)
144 }
145 ParamType::FixedBytes(len) => {
146 let bytes = take_bytes(data, offset, len, validate)?;
149 let result = DecodeResult { token: Token::FixedBytes(bytes), new_offset: offset + 32 };
150 Ok(result)
151 }
152 ParamType::Bytes => {
153 let dynamic_offset = as_usize(&peek_32_bytes(data, offset)?)?;
154 let len = as_usize(&peek_32_bytes(data, dynamic_offset)?)?;
155 let bytes = take_bytes(data, dynamic_offset + 32, len, validate)?;
156 let result = DecodeResult { token: Token::Bytes(bytes), new_offset: offset + 32 };
157 Ok(result)
158 }
159 ParamType::String => {
160 let dynamic_offset = as_usize(&peek_32_bytes(data, offset)?)?;
161 let len = as_usize(&peek_32_bytes(data, dynamic_offset)?)?;
162 let bytes = take_bytes(data, dynamic_offset + 32, len, validate)?;
163 let result = DecodeResult {
164 token: Token::String(String::from_utf8_lossy(&bytes).into()),
169 new_offset: offset + 32,
170 };
171 Ok(result)
172 }
173 ParamType::Array(ref t) => {
174 let len_offset = as_usize(&peek_32_bytes(data, offset)?)?;
175 let len = as_usize(&peek_32_bytes(data, len_offset)?)?;
176
177 let tail_offset = len_offset + 32;
178 let tail = &data[tail_offset..];
179
180 let mut tokens = vec![];
181 let mut new_offset = 0;
182
183 for _ in 0..len {
184 let res = decode_param(t, tail, new_offset, validate)?;
185 new_offset = res.new_offset;
186 tokens.push(res.token);
187 }
188
189 let result = DecodeResult { token: Token::Array(tokens), new_offset: offset + 32 };
190
191 Ok(result)
192 }
193 ParamType::FixedArray(ref t, len) => {
194 let is_dynamic = param.is_dynamic();
195
196 let (tail, mut new_offset) = if is_dynamic {
197 let offset = as_usize(&peek_32_bytes(data, offset)?)?;
198 if offset > data.len() {
199 return Err(Error::InvalidData);
200 }
201 (&data[offset..], 0)
202 } else {
203 (data, offset)
204 };
205
206 let mut tokens = vec![];
207
208 for _ in 0..len {
209 let res = decode_param(t, tail, new_offset, validate)?;
210 new_offset = res.new_offset;
211 tokens.push(res.token);
212 }
213
214 let result = DecodeResult {
215 token: Token::FixedArray(tokens),
216 new_offset: if is_dynamic { offset + 32 } else { new_offset },
217 };
218
219 Ok(result)
220 }
221 ParamType::Tuple(ref t) => {
222 let is_dynamic = param.is_dynamic();
223
224 let (tail, mut new_offset) = if is_dynamic {
227 let offset = as_usize(&peek_32_bytes(data, offset)?)?;
228 if offset > data.len() {
229 return Err(Error::InvalidData);
230 }
231 (&data[offset..], 0)
232 } else {
233 (data, offset)
234 };
235
236 let len = t.len();
237 let mut tokens = Vec::with_capacity(len);
238 for param in t {
239 let res = decode_param(param, tail, new_offset, validate)?;
240 new_offset = res.new_offset;
241 tokens.push(res.token);
242 }
243
244 let result = DecodeResult {
248 token: Token::Tuple(tokens),
249 new_offset: if is_dynamic { offset + 32 } else { new_offset },
250 };
251
252 Ok(result)
253 }
254 }
255}
256
257#[cfg(test)]
258mod tests {
259 use hex_literal::hex;
260
261 #[cfg(not(feature = "std"))]
262 use crate::no_std_prelude::*;
263 use crate::{decode, decode_validate, ParamType, Token, Uint};
264
265 #[test]
266 fn decode_from_empty_byte_slice() {
267 assert!(decode(&[ParamType::Address], &[]).is_err());
269 assert!(decode(&[ParamType::Bytes], &[]).is_err());
270 assert!(decode(&[ParamType::Int(0)], &[]).is_err());
271 assert!(decode(&[ParamType::Int(1)], &[]).is_err());
272 assert!(decode(&[ParamType::Int(0)], &[]).is_err());
273 assert!(decode(&[ParamType::Int(1)], &[]).is_err());
274 assert!(decode(&[ParamType::Bool], &[]).is_err());
275 assert!(decode(&[ParamType::String], &[]).is_err());
276 assert!(decode(&[ParamType::Array(Box::new(ParamType::Bool))], &[]).is_err());
277 assert!(decode(&[ParamType::FixedBytes(1)], &[]).is_err());
278 assert!(decode(&[ParamType::FixedArray(Box::new(ParamType::Bool), 1)], &[]).is_err());
279
280 assert!(decode(&[ParamType::FixedBytes(0)], &[]).is_ok());
282 assert!(decode(&[ParamType::FixedArray(Box::new(ParamType::Bool), 0)], &[]).is_ok());
283 }
284
285 #[test]
286 fn decode_static_tuple_of_addresses_and_uints() {
287 let encoded = hex!(
288 "
289 0000000000000000000000001111111111111111111111111111111111111111
290 0000000000000000000000002222222222222222222222222222222222222222
291 1111111111111111111111111111111111111111111111111111111111111111
292 "
293 );
294 let address1 = Token::Address([0x11u8; 20].into());
295 let address2 = Token::Address([0x22u8; 20].into());
296 let uint = Token::Uint([0x11u8; 32].into());
297 let tuple = Token::Tuple(vec![address1, address2, uint]);
298 let expected = vec![tuple];
299 let decoded =
300 decode(&[ParamType::Tuple(vec![ParamType::Address, ParamType::Address, ParamType::Uint(32)])], &encoded)
301 .unwrap();
302 assert_eq!(decoded, expected);
303 }
304
305 #[test]
306 fn decode_dynamic_tuple() {
307 let encoded = hex!(
308 "
309 0000000000000000000000000000000000000000000000000000000000000020
310 0000000000000000000000000000000000000000000000000000000000000040
311 0000000000000000000000000000000000000000000000000000000000000080
312 0000000000000000000000000000000000000000000000000000000000000009
313 6761766f66796f726b0000000000000000000000000000000000000000000000
314 0000000000000000000000000000000000000000000000000000000000000009
315 6761766f66796f726b0000000000000000000000000000000000000000000000
316 "
317 );
318 let string1 = Token::String("gavofyork".to_owned());
319 let string2 = Token::String("gavofyork".to_owned());
320 let tuple = Token::Tuple(vec![string1, string2]);
321 let decoded = decode(&[ParamType::Tuple(vec![ParamType::String, ParamType::String])], &encoded).unwrap();
322 let expected = vec![tuple];
323 assert_eq!(decoded, expected);
324 }
325
326 #[test]
327 fn decode_nested_tuple() {
328 let encoded = hex!(
329 "
330 0000000000000000000000000000000000000000000000000000000000000020
331 0000000000000000000000000000000000000000000000000000000000000080
332 0000000000000000000000000000000000000000000000000000000000000001
333 00000000000000000000000000000000000000000000000000000000000000c0
334 0000000000000000000000000000000000000000000000000000000000000100
335 0000000000000000000000000000000000000000000000000000000000000004
336 7465737400000000000000000000000000000000000000000000000000000000
337 0000000000000000000000000000000000000000000000000000000000000006
338 6379626f72670000000000000000000000000000000000000000000000000000
339 0000000000000000000000000000000000000000000000000000000000000060
340 00000000000000000000000000000000000000000000000000000000000000a0
341 00000000000000000000000000000000000000000000000000000000000000e0
342 0000000000000000000000000000000000000000000000000000000000000005
343 6e69676874000000000000000000000000000000000000000000000000000000
344 0000000000000000000000000000000000000000000000000000000000000003
345 6461790000000000000000000000000000000000000000000000000000000000
346 0000000000000000000000000000000000000000000000000000000000000040
347 0000000000000000000000000000000000000000000000000000000000000080
348 0000000000000000000000000000000000000000000000000000000000000004
349 7765656500000000000000000000000000000000000000000000000000000000
350 0000000000000000000000000000000000000000000000000000000000000008
351 66756e7465737473000000000000000000000000000000000000000000000000
352 "
353 );
354 let string1 = Token::String("test".to_owned());
355 let string2 = Token::String("cyborg".to_owned());
356 let string3 = Token::String("night".to_owned());
357 let string4 = Token::String("day".to_owned());
358 let string5 = Token::String("weee".to_owned());
359 let string6 = Token::String("funtests".to_owned());
360 let bool = Token::Bool(true);
361 let deep_tuple = Token::Tuple(vec![string5, string6]);
362 let inner_tuple = Token::Tuple(vec![string3, string4, deep_tuple]);
363 let outer_tuple = Token::Tuple(vec![string1, bool, string2, inner_tuple]);
364 let expected = vec![outer_tuple];
365 let decoded = decode(
366 &[ParamType::Tuple(vec![
367 ParamType::String,
368 ParamType::Bool,
369 ParamType::String,
370 ParamType::Tuple(vec![
371 ParamType::String,
372 ParamType::String,
373 ParamType::Tuple(vec![ParamType::String, ParamType::String]),
374 ]),
375 ])],
376 &encoded,
377 )
378 .unwrap();
379 assert_eq!(decoded, expected);
380 }
381
382 #[test]
383 fn decode_complex_tuple_of_dynamic_and_static_types() {
384 let encoded = hex!(
385 "
386 0000000000000000000000000000000000000000000000000000000000000020
387 1111111111111111111111111111111111111111111111111111111111111111
388 0000000000000000000000000000000000000000000000000000000000000080
389 0000000000000000000000001111111111111111111111111111111111111111
390 0000000000000000000000002222222222222222222222222222222222222222
391 0000000000000000000000000000000000000000000000000000000000000009
392 6761766f66796f726b0000000000000000000000000000000000000000000000
393 "
394 );
395 let uint = Token::Uint([0x11u8; 32].into());
396 let string = Token::String("gavofyork".to_owned());
397 let address1 = Token::Address([0x11u8; 20].into());
398 let address2 = Token::Address([0x22u8; 20].into());
399 let tuple = Token::Tuple(vec![uint, string, address1, address2]);
400 let expected = vec![tuple];
401 let decoded = decode(
402 &[ParamType::Tuple(vec![ParamType::Uint(32), ParamType::String, ParamType::Address, ParamType::Address])],
403 &encoded,
404 )
405 .unwrap();
406 assert_eq!(decoded, expected);
407 }
408
409 #[test]
410 fn decode_params_containing_dynamic_tuple() {
411 let encoded = hex!(
412 "
413 0000000000000000000000002222222222222222222222222222222222222222
414 00000000000000000000000000000000000000000000000000000000000000a0
415 0000000000000000000000003333333333333333333333333333333333333333
416 0000000000000000000000004444444444444444444444444444444444444444
417 0000000000000000000000000000000000000000000000000000000000000000
418 0000000000000000000000000000000000000000000000000000000000000001
419 0000000000000000000000000000000000000000000000000000000000000060
420 00000000000000000000000000000000000000000000000000000000000000a0
421 0000000000000000000000000000000000000000000000000000000000000009
422 7370616365736869700000000000000000000000000000000000000000000000
423 0000000000000000000000000000000000000000000000000000000000000006
424 6379626f72670000000000000000000000000000000000000000000000000000
425 "
426 );
427 let address1 = Token::Address([0x22u8; 20].into());
428 let bool1 = Token::Bool(true);
429 let string1 = Token::String("spaceship".to_owned());
430 let string2 = Token::String("cyborg".to_owned());
431 let tuple = Token::Tuple(vec![bool1, string1, string2]);
432 let address2 = Token::Address([0x33u8; 20].into());
433 let address3 = Token::Address([0x44u8; 20].into());
434 let bool2 = Token::Bool(false);
435 let expected = vec![address1, tuple, address2, address3, bool2];
436 let decoded = decode(
437 &[
438 ParamType::Address,
439 ParamType::Tuple(vec![ParamType::Bool, ParamType::String, ParamType::String]),
440 ParamType::Address,
441 ParamType::Address,
442 ParamType::Bool,
443 ],
444 &encoded,
445 )
446 .unwrap();
447 assert_eq!(decoded, expected);
448 }
449
450 #[test]
451 fn decode_params_containing_static_tuple() {
452 let encoded = hex!(
453 "
454 0000000000000000000000001111111111111111111111111111111111111111
455 0000000000000000000000002222222222222222222222222222222222222222
456 0000000000000000000000000000000000000000000000000000000000000001
457 0000000000000000000000000000000000000000000000000000000000000000
458 0000000000000000000000003333333333333333333333333333333333333333
459 0000000000000000000000004444444444444444444444444444444444444444
460 "
461 );
462 let address1 = Token::Address([0x11u8; 20].into());
463 let address2 = Token::Address([0x22u8; 20].into());
464 let bool1 = Token::Bool(true);
465 let bool2 = Token::Bool(false);
466 let tuple = Token::Tuple(vec![address2, bool1, bool2]);
467 let address3 = Token::Address([0x33u8; 20].into());
468 let address4 = Token::Address([0x44u8; 20].into());
469
470 let expected = vec![address1, tuple, address3, address4];
471 let decoded = decode(
472 &[
473 ParamType::Address,
474 ParamType::Tuple(vec![ParamType::Address, ParamType::Bool, ParamType::Bool]),
475 ParamType::Address,
476 ParamType::Address,
477 ],
478 &encoded,
479 )
480 .unwrap();
481 assert_eq!(decoded, expected);
482 }
483
484 #[test]
485 fn decode_data_with_size_that_is_not_a_multiple_of_32() {
486 let encoded = hex!(
487 "
488 0000000000000000000000000000000000000000000000000000000000000000
489 00000000000000000000000000000000000000000000000000000000000000a0
490 0000000000000000000000000000000000000000000000000000000000000152
491 0000000000000000000000000000000000000000000000000000000000000001
492 000000000000000000000000000000000000000000000000000000000054840d
493 0000000000000000000000000000000000000000000000000000000000000092
494 3132323033393637623533326130633134633938306235616566666231373034
495 3862646661656632633239336139353039663038656233633662306635663866
496 3039343265376239636337366361353163636132366365353436393230343438
497 6533303866646136383730623565326165313261323430396439343264653432
498 3831313350373230703330667073313678390000000000000000000000000000
499 0000000000000000000000000000000000103933633731376537633061363531
500 3761
501 "
502 );
503
504 assert_eq!(
505 decode(
506 &[
507 ParamType::Uint(256),
508 ParamType::String,
509 ParamType::String,
510 ParamType::Uint(256),
511 ParamType::Uint(256),
512 ],
513 &encoded,
514 ).unwrap(),
515 &[
516 Token::Uint(Uint::from(0)),
517 Token::String(String::from("12203967b532a0c14c980b5aeffb17048bdfaef2c293a9509f08eb3c6b0f5f8f0942e7b9cc76ca51cca26ce546920448e308fda6870b5e2ae12a2409d942de428113P720p30fps16x9")),
518 Token::String(String::from("93c717e7c0a6517a")),
519 Token::Uint(Uint::from(1)),
520 Token::Uint(Uint::from(5538829))
521 ]
522 );
523 }
524
525 #[test]
526 fn decode_after_fixed_bytes_with_less_than_32_bytes() {
527 let encoded = hex!(
528 "
529 0000000000000000000000008497afefdc5ac170a664a231f6efb25526ef813f
530 0000000000000000000000000000000000000000000000000000000000000000
531 0000000000000000000000000000000000000000000000000000000000000000
532 0000000000000000000000000000000000000000000000000000000000000080
533 000000000000000000000000000000000000000000000000000000000000000a
534 3078303030303030314600000000000000000000000000000000000000000000
535 "
536 );
537
538 assert_eq!(
539 decode(
540 &[ParamType::Address, ParamType::FixedBytes(32), ParamType::FixedBytes(4), ParamType::String,],
541 &encoded,
542 )
543 .unwrap(),
544 &[
545 Token::Address(hex!("8497afefdc5ac170a664a231f6efb25526ef813f").into()),
546 Token::FixedBytes([0u8; 32].to_vec()),
547 Token::FixedBytes([0u8; 4].to_vec()),
548 Token::String("0x0000001F".into()),
549 ]
550 )
551 }
552
553 #[test]
554 fn decode_broken_utf8() {
555 let encoded = hex!(
556 "
557 0000000000000000000000000000000000000000000000000000000000000020
558 0000000000000000000000000000000000000000000000000000000000000004
559 e4b88de500000000000000000000000000000000000000000000000000000000
560 "
561 );
562
563 assert_eq!(decode(&[ParamType::String,], &encoded).unwrap(), &[Token::String("不�".into())]);
564 }
565
566 #[test]
567 fn decode_corrupted_dynamic_array() {
568 let encoded = hex!(
573 "
574 0000000000000000000000000000000000000000000000000000000000000020
575 00000000000000000000000000000000000000000000000000000000ffffffff
576 0000000000000000000000000000000000000000000000000000000000000001
577 0000000000000000000000000000000000000000000000000000000000000002
578 "
579 );
580
581 assert!(decode(&[ParamType::Array(Box::new(ParamType::Uint(32)))], &encoded).is_err());
582 }
583
584 #[test]
585 fn decode_corrupted_nested_array_tuple() {
586 let input = hex!(
587 "
5880000000000000000000000000000000000000000000000000000000000000040
589
59000000000000000000000000000000000000000000000000000000000000002a0
5910000000000000000000000000000000000000000000000000000000000000009
592
59300000000000000000000000000000000fffffffffffffffffffffffffffffffe
5940000000000000000000000000000000000000000000000000000000000000000
595
5960000000000000000000000000000000000000000000000000000000000000000
5970000000000000000000000000000000000000000000000000000000000000000
598
5990000000000000000000000000000000000000000000000000000000000000000
600000000000000000000000000000000000000000000000000ffffffffffffffff
601
6020008000000000000000000000000000000000000000000000000000000000000
6030000000000000000000000000000000000000000000000020000000000000000
604
6050000000000000000000000000000000000000000000000000000000000000000
6060000000000000000000000000001000000000000000000000000000000000000
607
608000000000000000000000000000000000000000000000000000000000000053a
6090100000000000000000000000000000000000000000000000000000000000000
610
6110000000000000010000000000000000000000000000000000000000000000000
6120000000000000000000000000000000000000000000000000000000000000000
613
6140000000000000000000000000000000000000000000000000000000002000000
6150000000000000000000000000000000000000000000000000000000000100000
616
6170000000000000000000000000000000000000000000000000000000000000000
618ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
619
6200000000000000000000000000000000000000000000000000000000000000006
62100000000000000000000000000000000000000000000000000000000000000c0
622
6230000000000000000000000000000000000000000000000000000000000002ce0
6240000000000000000000000000000000000000000000000000000000000005880
625
6260000000000000000000000000000000000000000000000000000000000008280
627000000000000000000000000000000000000000000000000000000000000acc0
628
629000000000000000000000000000000000000000000000000000000000000d6e0
6300000000000000000000000000000000000000000020000000000000000000000
631
6320000000000000000000000000000000000000000000000000000000000000040
6330000000000000000000000000000000000000000000000000000000000000009
634
6350000000000000000000000000000000000000000000000000000000000000120
6360000000000000000000000000000000000000000000000000000000000000720
637
6380000000000000000000000000000000000000000000000000000000000000b80
6390000000000000000000000000000000000000000000000000000000000000fe0
640
641"
642 );
643
644 let func = {
645 use crate::{Function, Param};
646 use ParamType::*;
647 #[allow(deprecated)]
648 Function {
649 name: "f_tuple".to_string(),
650 inputs: vec![
651 Param {
652 name: "c".to_string(),
653 kind: Array(Box::new(Tuple(vec![Uint(256), Uint(256)]))),
654 internal_type: None,
655 },
656 Param {
657 name: "d".to_string(),
658 kind: Array(Box::new(Tuple(vec![
659 Uint(256),
660 Array(Box::new(Tuple(vec![Uint(256), Array(Box::new(ParamType::String))]))),
661 ]))),
662 internal_type: None,
663 },
664 ],
665 outputs: vec![],
666 constant: None,
667 state_mutability: crate::StateMutability::default(),
668 }
669 };
670 assert!(func.decode_input(&input).is_err());
671 }
672
673 #[test]
674 fn decode_corrupted_fixed_array_of_strings() {
675 let input = hex!(
676 "
6770000000000000000000000000000000000000000000000000000000000000001
6780000000000000000000000000000000000000000000000000000000001000040
6790000000000000000000000000000000000000000000000000000000000000040
6800000000000000000000000000000000000000000000000000000000000000080
6810000000000000000000000000000000000000000000000000000000000000008
6825445535454455354000000000000000000000000000000000000000000000000
6830000000000000000000000000000000000000000000000000000000000000008
6845445535454455354000000000000000000000000000000000000000000000000
685"
686 );
687
688 let func = {
689 use crate::{Function, Param};
690 use ParamType::*;
691 #[allow(deprecated)]
692 Function {
693 name: "f".to_string(),
694 inputs: vec![
695 Param { name: "i".to_string(), kind: Uint(256), internal_type: None },
696 Param {
697 name: "p".to_string(),
698 kind: FixedArray(Box::new(ParamType::String), 2),
699 internal_type: None,
700 },
701 ],
702 outputs: vec![],
703 constant: None,
704 state_mutability: crate::StateMutability::default(),
705 }
706 };
707 assert!(func.decode_input(&input).is_err());
708 }
709
710 #[test]
711 fn decode_verify_addresses() {
712 let input = hex!(
713 "
714 0000000000000000000000000000000000000000000000000000000000012345
715 0000000000000000000000000000000000000000000000000000000000054321
716 "
717 );
718 assert!(decode(&[ParamType::Address], &input).is_ok());
719 assert!(decode_validate(&[ParamType::Address], &input).is_err());
720 assert!(decode_validate(&[ParamType::Address, ParamType::Address], &input).is_ok());
721 }
722
723 #[test]
724 fn decode_verify_bytes() {
725 let input = hex!(
726 "
727 0000000000000000000000001234500000000000000000000000000000012345
728 0000000000000000000000005432100000000000000000000000000000054321
729 "
730 );
731 assert!(decode_validate(&[ParamType::Address, ParamType::FixedBytes(20)], &input).is_err());
732 assert!(decode_validate(&[ParamType::Address, ParamType::Address], &input).is_ok());
733 }
734}