Skip to content

Commit 5f8c855

Browse files
authored
fix(script): safely derive filename from calldata (#11291)
Update sequence.rs
1 parent f258f66 commit 5f8c855

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

crates/script-sequence/src/sequence.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,13 @@ pub fn sig_to_file_name(sig: &str) -> String {
228228
return name.to_string();
229229
}
230230
// assume calldata if `sig` is hex
231-
if let Ok(calldata) = hex::decode(sig) {
232-
// in which case we return the function signature
233-
return hex::encode(&calldata[..SELECTOR_LEN]);
231+
if let Ok(calldata) = hex::decode(sig.strip_prefix("0x").unwrap_or(sig)) {
232+
// in which case we return the function selector if available
233+
if let Some(selector) = calldata.get(..SELECTOR_LEN) {
234+
return hex::encode(selector);
235+
}
236+
// fallback to original string if calldata is too short to contain selector
237+
return sig.to_string();
234238
}
235239

236240
// return sig as is
@@ -255,5 +259,20 @@ mod tests {
255259
.as_str(),
256260
"522bb704"
257261
);
262+
// valid calldata with 0x prefix
263+
assert_eq!(
264+
sig_to_file_name(
265+
"0x522bb704000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfFFb92266"
266+
)
267+
.as_str(),
268+
"522bb704"
269+
);
270+
// short calldata: should not panic and should return input as-is
271+
assert_eq!(sig_to_file_name("0x1234").as_str(), "0x1234");
272+
assert_eq!(sig_to_file_name("123").as_str(), "123");
273+
// invalid hex: should return input as-is
274+
assert_eq!(sig_to_file_name("0xnotahex").as_str(), "0xnotahex");
275+
// non-hex non-signature: should return input as-is
276+
assert_eq!(sig_to_file_name("not_a_sig_or_hex").as_str(), "not_a_sig_or_hex");
258277
}
259278
}

0 commit comments

Comments
 (0)