File tree Expand file tree Collapse file tree 1 file changed +22
-3
lines changed
crates/script-sequence/src Expand file tree Collapse file tree 1 file changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -228,9 +228,13 @@ pub fn sig_to_file_name(sig: &str) -> String {
228
228
return name. to_string ( ) ;
229
229
}
230
230
// 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 ( ) ;
234
238
}
235
239
236
240
// return sig as is
@@ -255,5 +259,20 @@ mod tests {
255
259
. as_str( ) ,
256
260
"522bb704"
257
261
) ;
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" ) ;
258
277
}
259
278
}
You can’t perform that action at this time.
0 commit comments