tomusdrw / rust-web3

Ethereum JSON-RPC multi-transport client. Rust implementation of web3 library. ENS address: rust-web3.eth
MIT License
1.45k stars 465 forks source link

Why the Address type is H160? How should I convert from a &str? #607

Closed hzqd closed 2 years ago

hzqd commented 2 years ago

I try to convert a &str address to Address type using Address::from_slice(<&str>.as_bytes()).

Getting panic message:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `42`,
 right: `20`'

How should I convert a &str to an Address?

tomusdrw commented 2 years ago

H160 implements FromStr trait (here), so you can simply do:

let str_addr: &str = "...";
let addr: Address = str_addr.parse().unwrap()