@@ -169,10 +169,12 @@ fn msg_from_data(data: Data) -> Result<Message> {
169
169
}
170
170
171
171
fn bytes_from_hex_str ( mut hex_str : & str ) -> Result < Vec < u8 > > {
172
- // Strip the optional prefix.
173
- const OPTIONAL_PREFIX : & str = "0x" ;
174
- if hex_str. starts_with ( OPTIONAL_PREFIX ) {
175
- hex_str = & hex_str[ OPTIONAL_PREFIX . len ( ) ..] ;
172
+ // Check for the prefix.
173
+ const PREFIX : & str = "0x" ;
174
+ if hex_str. starts_with ( PREFIX ) {
175
+ hex_str = & hex_str[ PREFIX . len ( ) ..] ;
176
+ } else {
177
+ bail ! ( "missing 0x at the beginning of hex string" )
176
178
}
177
179
hex:: decode ( hex_str) . context ( "failed to decode bytes from hex string" )
178
180
}
@@ -228,16 +230,12 @@ mod tests {
228
230
#[ test]
229
231
fn sign_hex ( ) {
230
232
with_tmp_dir_and_wallet ( |_dir, wallet_path| {
231
- let hex_encoded = hex:: encode ( TEST_STR ) ;
233
+ let hex_encoded = format ! ( "0x{}" , hex:: encode( TEST_STR ) ) ;
232
234
let msg = msg_from_hex_str ( & hex_encoded) . unwrap ( ) ;
233
235
let account_ix = 0 ;
234
236
let sig =
235
237
sign_msg_with_wallet_account ( wallet_path, account_ix, & msg, TEST_PASSWORD ) . unwrap ( ) ;
236
238
assert_eq ! ( sig. to_string( ) , EXPECTED_SIG ) ;
237
- // The hex prefix should be ignored if it exists.
238
- let prefixed = format ! ( "0x{hex_encoded}" ) ;
239
- let prefixed_msg = msg_from_hex_str ( & prefixed) . unwrap ( ) ;
240
- assert_eq ! ( msg, prefixed_msg) ;
241
239
} ) ;
242
240
}
243
241
}
0 commit comments