Skip to content

Commit 268c579

Browse files
committed
Replace parse_address with FromStr::from_str
1 parent 1d39626 commit 268c579

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

codechain/config/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ impl Operating {
228228
impl Mining {
229229
pub fn overwrite_with(&mut self, matches: &clap::ArgMatches) -> Result<(), String> {
230230
if let Some(author) = matches.value_of("author") {
231-
self.author = Some(parse_address(author)?);
231+
self.author = Some(author.parse()?);
232232
}
233233
if let Some(engine_signer) = matches.value_of("engine-signer") {
234-
self.engine_signer = Some(parse_address(engine_signer)?);
234+
self.engine_signer = Some(engine_signer.parse()?);
235235
}
236236
if let Some(password_path) = matches.value_of("password-path") {
237237
self.password_path = Some(password_path.to_string());
@@ -361,7 +361,7 @@ impl ShardValidator {
361361
}
362362

363363
if let Some(account) = matches.value_of("shard-validator") {
364-
self.account = Some(parse_address(account)?)
364+
self.account = Some(account.parse()?)
365365
}
366366
if let Some(password_path) = matches.value_of("shard-validator-password-path") {
367367
self.password_path = Some(password_path.to_string());
@@ -388,11 +388,3 @@ pub fn load_config(matches: &clap::ArgMatches) -> Result<Config, String> {
388388

389389
Ok(config)
390390
}
391-
392-
fn parse_address(value: &str) -> Result<Address, String> {
393-
if value.starts_with("0x") {
394-
Address::from_str(&value[2..])
395-
} else {
396-
Address::from_str(value)
397-
}.map_err(|_| "Invalid address".to_string())
398-
}

key/src/address.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use bech32::Bech32;
2424
use heapsize::HeapSizeOf;
2525
use primitives::H160;
2626
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
27-
use rustc_hex::FromHexError;
2827
use serde::de::{Error as SerdeError, Visitor};
2928
use serde::{Deserialize, Deserializer, Serialize, Serializer};
3029

@@ -97,11 +96,22 @@ impl Decodable for Address {
9796
}
9897
}
9998

99+
/// Return `s` without the `0x` at the beginning of it, if any.
100+
pub fn clean_0x(s: &str) -> &str {
101+
if s.starts_with("0x") {
102+
&s[2..]
103+
} else {
104+
s
105+
}
106+
}
107+
100108
impl FromStr for Address {
101-
type Err = FromHexError;
109+
type Err = String;
102110

103111
fn from_str(s: &str) -> Result<Self, Self::Err> {
104-
Ok(Address(H160::from_str(s)?))
112+
let s = clean_0x(s);
113+
let a = H160::from_str(s).map_err(|_| format!("Invalid address {}", s))?;
114+
Ok(Address(a))
105115
}
106116
}
107117

0 commit comments

Comments
 (0)