Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dyn-abi): abi-encode-packed always pads arrays #519

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions crates/dyn-abi/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,16 +1184,16 @@ expect: {expected}",
1111111111111111111111111111111111111111,\
2222222222222222222222222222222222222222\
]", "
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
"),

fixed_array_of_addresses("address[2]", "[\
1111111111111111111111111111111111111111,\
2222222222222222222222222222222222222222\
]", "
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
"),

two_addresses("(address,address)", "(\
Expand All @@ -1208,48 +1208,48 @@ expect: {expected}",
[1111111111111111111111111111111111111111, 2222222222222222222222222222222222222222],\
[3333333333333333333333333333333333333333, 4444444444444444444444444444444444444444]\
]", "
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
3333333333333333333333333333333333333333
4444444444444444444444444444444444444444
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
0000000000000000000000003333333333333333333333333333333333333333
0000000000000000000000004444444444444444444444444444444444444444
"),

dynamic_array_of_fixed_arrays_of_addresses("address[2][]", "[\
[1111111111111111111111111111111111111111, 2222222222222222222222222222222222222222],\
[3333333333333333333333333333333333333333, 4444444444444444444444444444444444444444]\
]", "
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
3333333333333333333333333333333333333333
4444444444444444444444444444444444444444
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
0000000000000000000000003333333333333333333333333333333333333333
0000000000000000000000004444444444444444444444444444444444444444
"),

dynamic_array_of_dynamic_arrays("address[][]", "[\
[1111111111111111111111111111111111111111],\
[2222222222222222222222222222222222222222]\
]", "
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
"),

dynamic_array_of_dynamic_arrays2("address[][]", "[\
[1111111111111111111111111111111111111111, 2222222222222222222222222222222222222222],\
[3333333333333333333333333333333333333333, 4444444444444444444444444444444444444444]\
]", "
1111111111111111111111111111111111111111
2222222222222222222222222222222222222222
3333333333333333333333333333333333333333
4444444444444444444444444444444444444444
0000000000000000000000001111111111111111111111111111111111111111
0000000000000000000000002222222222222222222222222222222222222222
0000000000000000000000003333333333333333333333333333333333333333
0000000000000000000000004444444444444444444444444444444444444444
"),

dynamic_array_of_dynamic_arrays3("uint32[][]", "[\
[1, 2],\
[3, 4]\
]", "
00000001
00000002
00000003
00000004
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000004
"),
}
}
27 changes: 26 additions & 1 deletion crates/dyn-abi/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,33 @@ impl DynSolValue {
let start = 32usize.saturating_sub(byte_size);
buf.extend_from_slice(&num.to_be_bytes::<32>()[start..]);
}
as_fixed_seq!(inner) | Self::Array(inner) => {
Self::FixedArray(inner) | Self::Array(inner) => {
for val in inner {
let mut buf_inner = Vec::new();
val.abi_encode_packed_to(&mut buf_inner);

// Array elements are always padded
if buf_inner.len() < 32usize {
// Calculate the number of padding elements needed
let padding_needed = 32usize.saturating_sub(buf_inner.len());

// Extend the vector with the padding elements
buf_inner.resize(32usize, 0);

// Rotate the vector left by the number of padding elements added
buf_inner.rotate_right(padding_needed);
}
buf.extend_from_slice(&buf_inner);
}
}
Self::Tuple(inner) => {
for val in inner {
val.abi_encode_packed_to(buf);
}
}
#[cfg(feature = "eip712")]
Self::CustomStruct { tuple, .. } => {
for val in tuple {
val.abi_encode_packed_to(buf);
}
}
Expand Down