ethabi/
decoder.rs

1// Copyright 2015-2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! ABI decoder.
10
11#[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	if !slice[..31].iter().all(|x| *x == 0) {
36		return Err(Error::InvalidData);
37	}
38
39	Ok(slice[31] == 1)
40}
41
42fn decode_offset(types: &[ParamType], data: &[u8]) -> Result<(Vec<Token>, usize), Error> {
43	let is_empty_bytes_valid_encoding = types.iter().all(|t| t.is_empty_bytes_valid_encoding());
44	if !is_empty_bytes_valid_encoding && data.is_empty() {
45		return Err(Error::InvalidName(
46			"please ensure the contract and method you're calling exist! \
47			 failed to decode empty bytes. if you're using jsonrpc this is \
48			 likely due to jsonrpc returning `0x` in case contract or method \
49			 don't exist"
50				.into(),
51		));
52	}
53
54	let mut tokens = vec![];
55	let mut offset = 0;
56
57	for param in types {
58		let res = decode_param(param, data, offset)?;
59		offset = res.new_offset;
60		tokens.push(res.token);
61	}
62
63	Ok((tokens, offset))
64}
65
66/// Decodes ABI compliant vector of bytes into vector of tokens described by types param.
67/// Fails, if some data left to decode
68pub fn decode_whole(types: &[ParamType], data: &[u8]) -> Result<Vec<Token>, Error> {
69	decode_offset(types, data).and_then(
70		|(tokens, offset)| {
71			if offset != data.len() {
72				Err(Error::InvalidData)
73			} else {
74				Ok(tokens)
75			}
76		},
77	)
78}
79
80/// Decodes ABI compliant vector of bytes into vector of tokens described by types param.
81/// Returns ok, even if some data left to decode
82pub fn decode(types: &[ParamType], data: &[u8]) -> Result<Vec<Token>, Error> {
83	decode_offset(types, data).map(|(tokens, _)| tokens)
84}
85
86fn peek(data: &[u8], offset: usize, len: usize) -> Result<&[u8], Error> {
87	if offset + len > data.len() {
88		Err(Error::InvalidData)
89	} else {
90		Ok(&data[offset..(offset + len)])
91	}
92}
93
94fn peek_32_bytes(data: &[u8], offset: usize) -> Result<Word, Error> {
95	peek(data, offset, 32).map(|x| {
96		let mut out: Word = [0u8; 32];
97		out.copy_from_slice(&x[0..32]);
98		out
99	})
100}
101
102fn take_bytes(data: &[u8], offset: usize, len: usize) -> Result<Vec<u8>, Error> {
103	if offset + len > data.len() {
104		Err(Error::InvalidData)
105	} else {
106		Ok(data[offset..(offset + len)].to_vec())
107	}
108}
109
110fn decode_param(param: &ParamType, data: &[u8], offset: usize) -> Result<DecodeResult, Error> {
111	match *param {
112		ParamType::Address => {
113			let slice = peek_32_bytes(data, offset)?;
114			let mut address = [0u8; 20];
115			address.copy_from_slice(&slice[12..]);
116			let result = DecodeResult { token: Token::Address(address.into()), new_offset: offset + 32 };
117			Ok(result)
118		}
119		ParamType::Int(_) => {
120			let slice = peek_32_bytes(data, offset)?;
121			let result = DecodeResult { token: Token::Int(slice.into()), new_offset: offset + 32 };
122			Ok(result)
123		}
124		ParamType::Uint(_) => {
125			let slice = peek_32_bytes(data, offset)?;
126			let result = DecodeResult { token: Token::Uint(slice.into()), new_offset: offset + 32 };
127			Ok(result)
128		}
129		ParamType::Bool => {
130			let b = as_bool(&peek_32_bytes(data, offset)?)?;
131			let result = DecodeResult { token: Token::Bool(b), new_offset: offset + 32 };
132			Ok(result)
133		}
134		ParamType::FixedBytes(len) => {
135			// FixedBytes is anything from bytes1 to bytes32. These values
136			// are padded with trailing zeros to fill 32 bytes.
137			let bytes = take_bytes(data, offset, len)?;
138			let result = DecodeResult { token: Token::FixedBytes(bytes), new_offset: offset + 32 };
139			Ok(result)
140		}
141		ParamType::Bytes => {
142			let dynamic_offset = as_usize(&peek_32_bytes(data, offset)?)?;
143			let len = as_usize(&peek_32_bytes(data, dynamic_offset)?)?;
144			let bytes = take_bytes(data, dynamic_offset + 32, len)?;
145			let result = DecodeResult { token: Token::Bytes(bytes), new_offset: offset + 32 };
146			Ok(result)
147		}
148		ParamType::String => {
149			let dynamic_offset = as_usize(&peek_32_bytes(data, offset)?)?;
150			let len = as_usize(&peek_32_bytes(data, dynamic_offset)?)?;
151			let bytes = take_bytes(data, dynamic_offset + 32, len)?;
152			let result = DecodeResult {
153				// NOTE: We're decoding strings using lossy UTF-8 decoding to
154				// prevent invalid strings written into contracts by either users or
155				// Solidity bugs from causing graph-node to fail decoding event
156				// data.
157				token: Token::String(String::from_utf8_lossy(&bytes).into()),
158				new_offset: offset + 32,
159			};
160			Ok(result)
161		}
162		ParamType::Array(ref t) => {
163			let len_offset = as_usize(&peek_32_bytes(data, offset)?)?;
164			let len = as_usize(&peek_32_bytes(data, len_offset)?)?;
165
166			let tail_offset = len_offset + 32;
167			let tail = &data[tail_offset..];
168
169			let mut tokens = vec![];
170			let mut new_offset = 0;
171
172			for _ in 0..len {
173				let res = decode_param(t, tail, new_offset)?;
174				new_offset = res.new_offset;
175				tokens.push(res.token);
176			}
177
178			let result = DecodeResult { token: Token::Array(tokens), new_offset: offset + 32 };
179
180			Ok(result)
181		}
182		ParamType::FixedArray(ref t, len) => {
183			let is_dynamic = param.is_dynamic();
184
185			let (tail, mut new_offset) = if is_dynamic {
186				let offset = as_usize(&peek_32_bytes(data, offset)?)?;
187				if offset > data.len() {
188					return Err(Error::InvalidData);
189				}
190				(&data[offset..], 0)
191			} else {
192				(data, offset)
193			};
194
195			let mut tokens = vec![];
196
197			for _ in 0..len {
198				let res = decode_param(t, tail, new_offset)?;
199				new_offset = res.new_offset;
200				tokens.push(res.token);
201			}
202
203			let result = DecodeResult {
204				token: Token::FixedArray(tokens),
205				new_offset: if is_dynamic { offset + 32 } else { new_offset },
206			};
207
208			Ok(result)
209		}
210		ParamType::Tuple(ref t) => {
211			let is_dynamic = param.is_dynamic();
212
213			// The first element in a dynamic Tuple is an offset to the Tuple's data
214			// For a static Tuple the data begins right away
215			let (tail, mut new_offset) = if is_dynamic {
216				let offset = as_usize(&peek_32_bytes(data, offset)?)?;
217				if offset > data.len() {
218					return Err(Error::InvalidData);
219				}
220				(&data[offset..], 0)
221			} else {
222				(data, offset)
223			};
224
225			let len = t.len();
226			let mut tokens = Vec::with_capacity(len);
227			for param in t {
228				let res = decode_param(param, tail, new_offset)?;
229				new_offset = res.new_offset;
230				tokens.push(res.token);
231			}
232
233			// The returned new_offset depends on whether the Tuple is dynamic
234			// dynamic Tuple -> follows the prefixed Tuple data offset element
235			// static Tuple  -> follows the last data element
236			let result = DecodeResult {
237				token: Token::Tuple(tokens),
238				new_offset: if is_dynamic { offset + 32 } else { new_offset },
239			};
240
241			Ok(result)
242		}
243	}
244}
245
246#[cfg(test)]
247mod tests {
248	use hex_literal::hex;
249
250	#[cfg(not(feature = "std"))]
251	use crate::no_std_prelude::*;
252	use crate::{decode, decode_whole, ParamType, Token, Uint};
253
254	#[test]
255	fn decode_from_empty_byte_slice() {
256		// these can NOT be decoded from empty byte slice
257		assert!(decode(&[ParamType::Address], &[]).is_err());
258		assert!(decode(&[ParamType::Bytes], &[]).is_err());
259		assert!(decode(&[ParamType::Int(0)], &[]).is_err());
260		assert!(decode(&[ParamType::Int(1)], &[]).is_err());
261		assert!(decode(&[ParamType::Int(0)], &[]).is_err());
262		assert!(decode(&[ParamType::Int(1)], &[]).is_err());
263		assert!(decode(&[ParamType::Bool], &[]).is_err());
264		assert!(decode(&[ParamType::String], &[]).is_err());
265		assert!(decode(&[ParamType::Array(Box::new(ParamType::Bool))], &[]).is_err());
266		assert!(decode(&[ParamType::FixedBytes(1)], &[]).is_err());
267		assert!(decode(&[ParamType::FixedArray(Box::new(ParamType::Bool), 1)], &[]).is_err());
268
269		// these are the only ones that can be decoded from empty byte slice
270		assert!(decode(&[ParamType::FixedBytes(0)], &[]).is_ok());
271		assert!(decode(&[ParamType::FixedArray(Box::new(ParamType::Bool), 0)], &[]).is_ok());
272	}
273
274	#[test]
275	fn decode_static_tuple_of_addresses_and_uints() {
276		let encoded = hex!(
277			"
278			0000000000000000000000001111111111111111111111111111111111111111
279			0000000000000000000000002222222222222222222222222222222222222222
280			1111111111111111111111111111111111111111111111111111111111111111
281		"
282		);
283		let address1 = Token::Address([0x11u8; 20].into());
284		let address2 = Token::Address([0x22u8; 20].into());
285		let uint = Token::Uint([0x11u8; 32].into());
286		let tuple = Token::Tuple(vec![address1, address2, uint]);
287		let expected = vec![tuple];
288		let decoded =
289			decode(&[ParamType::Tuple(vec![ParamType::Address, ParamType::Address, ParamType::Uint(32)])], &encoded)
290				.unwrap();
291		assert_eq!(decoded, expected);
292	}
293
294	#[test]
295	fn decode_dynamic_tuple() {
296		let encoded = hex!(
297			"
298			0000000000000000000000000000000000000000000000000000000000000020
299			0000000000000000000000000000000000000000000000000000000000000040
300			0000000000000000000000000000000000000000000000000000000000000080
301			0000000000000000000000000000000000000000000000000000000000000009
302			6761766f66796f726b0000000000000000000000000000000000000000000000
303			0000000000000000000000000000000000000000000000000000000000000009
304			6761766f66796f726b0000000000000000000000000000000000000000000000
305		"
306		);
307		let string1 = Token::String("gavofyork".to_owned());
308		let string2 = Token::String("gavofyork".to_owned());
309		let tuple = Token::Tuple(vec![string1, string2]);
310		let decoded = decode(&[ParamType::Tuple(vec![ParamType::String, ParamType::String])], &encoded).unwrap();
311		let expected = vec![tuple];
312		assert_eq!(decoded, expected);
313	}
314
315	#[test]
316	fn decode_nested_tuple() {
317		let encoded = hex!(
318			"
319			0000000000000000000000000000000000000000000000000000000000000020
320			0000000000000000000000000000000000000000000000000000000000000080
321			0000000000000000000000000000000000000000000000000000000000000001
322			00000000000000000000000000000000000000000000000000000000000000c0
323			0000000000000000000000000000000000000000000000000000000000000100
324			0000000000000000000000000000000000000000000000000000000000000004
325			7465737400000000000000000000000000000000000000000000000000000000
326			0000000000000000000000000000000000000000000000000000000000000006
327			6379626f72670000000000000000000000000000000000000000000000000000
328			0000000000000000000000000000000000000000000000000000000000000060
329			00000000000000000000000000000000000000000000000000000000000000a0
330			00000000000000000000000000000000000000000000000000000000000000e0
331			0000000000000000000000000000000000000000000000000000000000000005
332			6e69676874000000000000000000000000000000000000000000000000000000
333			0000000000000000000000000000000000000000000000000000000000000003
334			6461790000000000000000000000000000000000000000000000000000000000
335			0000000000000000000000000000000000000000000000000000000000000040
336			0000000000000000000000000000000000000000000000000000000000000080
337			0000000000000000000000000000000000000000000000000000000000000004
338			7765656500000000000000000000000000000000000000000000000000000000
339			0000000000000000000000000000000000000000000000000000000000000008
340			66756e7465737473000000000000000000000000000000000000000000000000
341		"
342		);
343		let string1 = Token::String("test".to_owned());
344		let string2 = Token::String("cyborg".to_owned());
345		let string3 = Token::String("night".to_owned());
346		let string4 = Token::String("day".to_owned());
347		let string5 = Token::String("weee".to_owned());
348		let string6 = Token::String("funtests".to_owned());
349		let bool = Token::Bool(true);
350		let deep_tuple = Token::Tuple(vec![string5, string6]);
351		let inner_tuple = Token::Tuple(vec![string3, string4, deep_tuple]);
352		let outer_tuple = Token::Tuple(vec![string1, bool, string2, inner_tuple]);
353		let expected = vec![outer_tuple];
354		let decoded = decode(
355			&[ParamType::Tuple(vec![
356				ParamType::String,
357				ParamType::Bool,
358				ParamType::String,
359				ParamType::Tuple(vec![
360					ParamType::String,
361					ParamType::String,
362					ParamType::Tuple(vec![ParamType::String, ParamType::String]),
363				]),
364			])],
365			&encoded,
366		)
367		.unwrap();
368		assert_eq!(decoded, expected);
369	}
370
371	#[test]
372	fn decode_complex_tuple_of_dynamic_and_static_types() {
373		let encoded = hex!(
374			"
375			0000000000000000000000000000000000000000000000000000000000000020
376			1111111111111111111111111111111111111111111111111111111111111111
377			0000000000000000000000000000000000000000000000000000000000000080
378			0000000000000000000000001111111111111111111111111111111111111111
379			0000000000000000000000002222222222222222222222222222222222222222
380			0000000000000000000000000000000000000000000000000000000000000009
381			6761766f66796f726b0000000000000000000000000000000000000000000000
382		"
383		);
384		let uint = Token::Uint([0x11u8; 32].into());
385		let string = Token::String("gavofyork".to_owned());
386		let address1 = Token::Address([0x11u8; 20].into());
387		let address2 = Token::Address([0x22u8; 20].into());
388		let tuple = Token::Tuple(vec![uint, string, address1, address2]);
389		let expected = vec![tuple];
390		let decoded = decode(
391			&[ParamType::Tuple(vec![ParamType::Uint(32), ParamType::String, ParamType::Address, ParamType::Address])],
392			&encoded,
393		)
394		.unwrap();
395		assert_eq!(decoded, expected);
396	}
397
398	#[test]
399	fn decode_params_containing_dynamic_tuple() {
400		let encoded = hex!(
401			"
402			0000000000000000000000002222222222222222222222222222222222222222
403			00000000000000000000000000000000000000000000000000000000000000a0
404			0000000000000000000000003333333333333333333333333333333333333333
405			0000000000000000000000004444444444444444444444444444444444444444
406			0000000000000000000000000000000000000000000000000000000000000000
407			0000000000000000000000000000000000000000000000000000000000000001
408			0000000000000000000000000000000000000000000000000000000000000060
409			00000000000000000000000000000000000000000000000000000000000000a0
410			0000000000000000000000000000000000000000000000000000000000000009
411			7370616365736869700000000000000000000000000000000000000000000000
412			0000000000000000000000000000000000000000000000000000000000000006
413			6379626f72670000000000000000000000000000000000000000000000000000
414		"
415		);
416		let address1 = Token::Address([0x22u8; 20].into());
417		let bool1 = Token::Bool(true);
418		let string1 = Token::String("spaceship".to_owned());
419		let string2 = Token::String("cyborg".to_owned());
420		let tuple = Token::Tuple(vec![bool1, string1, string2]);
421		let address2 = Token::Address([0x33u8; 20].into());
422		let address3 = Token::Address([0x44u8; 20].into());
423		let bool2 = Token::Bool(false);
424		let expected = vec![address1, tuple, address2, address3, bool2];
425		let decoded = decode(
426			&[
427				ParamType::Address,
428				ParamType::Tuple(vec![ParamType::Bool, ParamType::String, ParamType::String]),
429				ParamType::Address,
430				ParamType::Address,
431				ParamType::Bool,
432			],
433			&encoded,
434		)
435		.unwrap();
436		assert_eq!(decoded, expected);
437	}
438
439	#[test]
440	fn decode_params_containing_static_tuple() {
441		let encoded = hex!(
442			"
443			0000000000000000000000001111111111111111111111111111111111111111
444			0000000000000000000000002222222222222222222222222222222222222222
445			0000000000000000000000000000000000000000000000000000000000000001
446			0000000000000000000000000000000000000000000000000000000000000000
447			0000000000000000000000003333333333333333333333333333333333333333
448			0000000000000000000000004444444444444444444444444444444444444444
449		"
450		);
451		let address1 = Token::Address([0x11u8; 20].into());
452		let address2 = Token::Address([0x22u8; 20].into());
453		let bool1 = Token::Bool(true);
454		let bool2 = Token::Bool(false);
455		let tuple = Token::Tuple(vec![address2, bool1, bool2]);
456		let address3 = Token::Address([0x33u8; 20].into());
457		let address4 = Token::Address([0x44u8; 20].into());
458
459		let expected = vec![address1, tuple, address3, address4];
460		let decoded = decode(
461			&[
462				ParamType::Address,
463				ParamType::Tuple(vec![ParamType::Address, ParamType::Bool, ParamType::Bool]),
464				ParamType::Address,
465				ParamType::Address,
466			],
467			&encoded,
468		)
469		.unwrap();
470		assert_eq!(decoded, expected);
471	}
472
473	#[test]
474	fn decode_data_with_size_that_is_not_a_multiple_of_32() {
475		let encoded = hex!(
476			"
477            0000000000000000000000000000000000000000000000000000000000000000
478            00000000000000000000000000000000000000000000000000000000000000a0
479            0000000000000000000000000000000000000000000000000000000000000152
480            0000000000000000000000000000000000000000000000000000000000000001
481            000000000000000000000000000000000000000000000000000000000054840d
482            0000000000000000000000000000000000000000000000000000000000000092
483            3132323033393637623533326130633134633938306235616566666231373034
484            3862646661656632633239336139353039663038656233633662306635663866
485            3039343265376239636337366361353163636132366365353436393230343438
486            6533303866646136383730623565326165313261323430396439343264653432
487            3831313350373230703330667073313678390000000000000000000000000000
488            0000000000000000000000000000000000103933633731376537633061363531
489            3761
490        "
491		);
492
493		assert_eq!(
494			decode(
495				&[
496					ParamType::Uint(256),
497					ParamType::String,
498					ParamType::String,
499					ParamType::Uint(256),
500					ParamType::Uint(256),
501				],
502				&encoded,
503			).unwrap(),
504			&[
505				Token::Uint(Uint::from(0)),
506				Token::String(String::from("12203967b532a0c14c980b5aeffb17048bdfaef2c293a9509f08eb3c6b0f5f8f0942e7b9cc76ca51cca26ce546920448e308fda6870b5e2ae12a2409d942de428113P720p30fps16x9")),
507				Token::String(String::from("93c717e7c0a6517a")),
508				Token::Uint(Uint::from(1)),
509				Token::Uint(Uint::from(5538829))
510			]
511		);
512	}
513
514	#[test]
515	fn decode_after_fixed_bytes_with_less_than_32_bytes() {
516		let encoded = hex!(
517			"
518			0000000000000000000000008497afefdc5ac170a664a231f6efb25526ef813f
519			0000000000000000000000000000000000000000000000000000000000000000
520			0000000000000000000000000000000000000000000000000000000000000000
521			0000000000000000000000000000000000000000000000000000000000000080
522			000000000000000000000000000000000000000000000000000000000000000a
523			3078303030303030314600000000000000000000000000000000000000000000
524		"
525		);
526
527		assert_eq!(
528			decode(
529				&[ParamType::Address, ParamType::FixedBytes(32), ParamType::FixedBytes(4), ParamType::String,],
530				&encoded,
531			)
532			.unwrap(),
533			&[
534				Token::Address(hex!("8497afefdc5ac170a664a231f6efb25526ef813f").into()),
535				Token::FixedBytes([0u8; 32].to_vec()),
536				Token::FixedBytes([0u8; 4].to_vec()),
537				Token::String("0x0000001F".into()),
538			]
539		)
540	}
541
542	#[test]
543	fn decode_broken_utf8() {
544		let encoded = hex!(
545			"
546			0000000000000000000000000000000000000000000000000000000000000020
547			0000000000000000000000000000000000000000000000000000000000000004
548			e4b88de500000000000000000000000000000000000000000000000000000000
549        "
550		);
551
552		assert_eq!(decode(&[ParamType::String,], &encoded).unwrap(), &[Token::String("不�".into())]);
553	}
554
555	#[test]
556	fn decode_corrupted_dynamic_array() {
557		// line 1 at 0x00 =   0: tail offset of array
558		// line 2 at 0x20 =  32: length of array
559		// line 3 at 0x40 =  64: first word
560		// line 4 at 0x60 =  96: second word
561		let encoded = hex!(
562			"
563		0000000000000000000000000000000000000000000000000000000000000020
564		00000000000000000000000000000000000000000000000000000000ffffffff
565		0000000000000000000000000000000000000000000000000000000000000001
566		0000000000000000000000000000000000000000000000000000000000000002
567        "
568		);
569
570		assert!(decode(&[ParamType::Array(Box::new(ParamType::Uint(32)))], &encoded).is_err());
571	}
572
573	#[test]
574	fn decode_corrupted_nested_array_tuple() {
575		let input = hex!(
576			"
5770000000000000000000000000000000000000000000000000000000000000040
578
57900000000000000000000000000000000000000000000000000000000000002a0
5800000000000000000000000000000000000000000000000000000000000000009
581
58200000000000000000000000000000000fffffffffffffffffffffffffffffffe
5830000000000000000000000000000000000000000000000000000000000000000
584
5850000000000000000000000000000000000000000000000000000000000000000
5860000000000000000000000000000000000000000000000000000000000000000
587
5880000000000000000000000000000000000000000000000000000000000000000
589000000000000000000000000000000000000000000000000ffffffffffffffff
590
5910008000000000000000000000000000000000000000000000000000000000000
5920000000000000000000000000000000000000000000000020000000000000000
593
5940000000000000000000000000000000000000000000000000000000000000000
5950000000000000000000000000001000000000000000000000000000000000000
596
597000000000000000000000000000000000000000000000000000000000000053a
5980100000000000000000000000000000000000000000000000000000000000000
599
6000000000000000010000000000000000000000000000000000000000000000000
6010000000000000000000000000000000000000000000000000000000000000000
602
6030000000000000000000000000000000000000000000000000000000002000000
6040000000000000000000000000000000000000000000000000000000000100000
605
6060000000000000000000000000000000000000000000000000000000000000000
607ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
608
6090000000000000000000000000000000000000000000000000000000000000006
61000000000000000000000000000000000000000000000000000000000000000c0
611
6120000000000000000000000000000000000000000000000000000000000002ce0
6130000000000000000000000000000000000000000000000000000000000005880
614
6150000000000000000000000000000000000000000000000000000000000008280
616000000000000000000000000000000000000000000000000000000000000acc0
617
618000000000000000000000000000000000000000000000000000000000000d6e0
6190000000000000000000000000000000000000000020000000000000000000000
620
6210000000000000000000000000000000000000000000000000000000000000040
6220000000000000000000000000000000000000000000000000000000000000009
623
6240000000000000000000000000000000000000000000000000000000000000120
6250000000000000000000000000000000000000000000000000000000000000720
626
6270000000000000000000000000000000000000000000000000000000000000b80
6280000000000000000000000000000000000000000000000000000000000000fe0
629
630"
631		);
632
633		let func = {
634			use crate::{Function, Param};
635			use ParamType::*;
636			#[allow(deprecated)]
637			Function {
638				name: "f_tuple".to_string(),
639				inputs: vec![
640					Param {
641						name: "c".to_string(),
642						kind: Array(Box::new(Tuple(vec![Uint(256), Uint(256)]))),
643						internal_type: None,
644					},
645					Param {
646						name: "d".to_string(),
647						kind: Array(Box::new(Tuple(vec![
648							Uint(256),
649							Array(Box::new(Tuple(vec![Uint(256), Array(Box::new(ParamType::String))]))),
650						]))),
651						internal_type: None,
652					},
653				],
654				outputs: vec![],
655				constant: None,
656				state_mutability: crate::StateMutability::default(),
657			}
658		};
659		assert!(func.decode_input(&input).is_err());
660	}
661
662	#[test]
663	fn decode_corrupted_fixed_array_of_strings() {
664		let input = hex!(
665			"
6660000000000000000000000000000000000000000000000000000000000000001
6670000000000000000000000000000000000000000000000000000000001000040
6680000000000000000000000000000000000000000000000000000000000000040
6690000000000000000000000000000000000000000000000000000000000000080
6700000000000000000000000000000000000000000000000000000000000000008
6715445535454455354000000000000000000000000000000000000000000000000
6720000000000000000000000000000000000000000000000000000000000000008
6735445535454455354000000000000000000000000000000000000000000000000
674"
675		);
676
677		let func = {
678			use crate::{Function, Param};
679			use ParamType::*;
680			#[allow(deprecated)]
681			Function {
682				name: "f".to_string(),
683				inputs: vec![
684					Param { name: "i".to_string(), kind: Uint(256), internal_type: None },
685					Param {
686						name: "p".to_string(),
687						kind: FixedArray(Box::new(ParamType::String), 2),
688						internal_type: None,
689					},
690				],
691				outputs: vec![],
692				constant: None,
693				state_mutability: crate::StateMutability::default(),
694			}
695		};
696		assert!(func.decode_input(&input).is_err());
697	}
698
699	#[test]
700	fn decode_whole_addresses() {
701		let input = hex!(
702			"
703		0000000000000000000000000000000000000000000000000000000000012345
704		0000000000000000000000000000000000000000000000000000000000054321
705		"
706		);
707		assert!(decode(&[ParamType::Address], &input).is_ok());
708		assert!(decode_whole(&[ParamType::Address], &input).is_err());
709		assert!(decode_whole(&[ParamType::Address, ParamType::Address], &input).is_ok());
710	}
711}