diff --git a/src/core/address.rs b/src/core/address.rs index 186d1293..9ceafbcb 100644 --- a/src/core/address.rs +++ b/src/core/address.rs @@ -130,38 +130,30 @@ mod tests { #[test] fn should_catch_wrong_address_encoding() { - assert!( - "0x___c045110b8dbf29765047380898919c5cb56f4" - .parse::
() - .is_err() - ); + assert!("0x___c045110b8dbf29765047380898919c5cb56f4" + .parse::
() + .is_err()); } #[test] fn should_catch_wrong_address_insufficient_length() { - assert!( - "0x0e7c045110b8dbf297650473808989" - .parse::
() - .is_err() - ); + assert!("0x0e7c045110b8dbf297650473808989" + .parse::
() + .is_err()); } #[test] fn should_catch_wrong_address_excess_length() { - assert!( - "0x0e7c045110b8dbf29765047380898919c5cb56f400000000" - .parse::
() - .is_err() - ); + assert!("0x0e7c045110b8dbf29765047380898919c5cb56f400000000" + .parse::
() + .is_err()); } #[test] fn should_catch_wrong_address_prefix() { - assert!( - "0_0e7c045110b8dbf29765047380898919c5cb56f4" - .parse::
() - .is_err() - ); + assert!("0_0e7c045110b8dbf29765047380898919c5cb56f4" + .parse::
() + .is_err()); } #[test] diff --git a/src/mnemonic/mod.rs b/src/mnemonic/mod.rs index c4312b36..d61192b8 100644 --- a/src/mnemonic/mod.rs +++ b/src/mnemonic/mod.rs @@ -14,7 +14,6 @@ use num::bigint::BigUint; use num::{FromPrimitive, ToPrimitive}; use rand::{OsRng, Rng}; use sha2::{self, Digest}; -use std::iter::repeat; use std::ops::{BitAnd, Shr}; /// Size of entropy in bytes diff --git a/src/storage/storage_ctrl.rs b/src/storage/storage_ctrl.rs index 51614c6b..f8d0c5dc 100644 --- a/src/storage/storage_ctrl.rs +++ b/src/storage/storage_ctrl.rs @@ -16,8 +16,8 @@ const CHAIN_NAMES: &'static [&'static str; 9] = &[ "rootstock-main", "rootstock-test", "kovan", - "etc-main", - "etc-test", + "etc", + "etc-morden", ]; /// Controller to switch storage according to specified chain diff --git a/src/util/crypto.rs b/src/util/crypto.rs index e9f852b7..af7dd348 100644 --- a/src/util/crypto.rs +++ b/src/util/crypto.rs @@ -1,6 +1,5 @@ //! # Crypto util functions -use hmac::digest::FixedOutput; use sha3::Digest; use sha3::Keccak256; /// Keccak-256 crypto hash length in bytes diff --git a/src/util/mod.rs b/src/util/mod.rs index 6854ccdf..913f032e 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -12,6 +12,17 @@ use std::mem::transmute; static HEX_CHARS: &'static [u8] = b"0123456789abcdef"; +const ETH: &'static str = "eth"; +const MORDEN: &'static str = "morden"; +const ROPSTEN: &'static str = "ropsten"; +const RINKEBY: &'static str = "rinkeby"; +const ROOTSTOCK_MAINNET: &'static str = "rootstock-main"; +const ROOTSTOCK_TESTNET: &'static str = "rootstock-test"; +const KOVAN: &'static str = "kovan"; +const ETC: &'static str = "etc"; +const MAINNET: &'static str = "mainnet"; +const ETC_MORDEN: &'static str = "etc-morden"; + /// Convert `self` into hex string pub trait ToHex { /// converts to hex @@ -42,17 +53,17 @@ impl ToHex for u64 { /// # Arguments: /// * `id` - target chain id /// -pub fn to_chain_name(id: u8) -> Option { +pub fn to_chain_name(id: u8) -> Option<&'static str> { match id { - 1 => Some("eth".to_string()), - 2 => Some("morden".to_string()), - 3 => Some("ropsten".to_string()), - 4 => Some("rinkeby".to_string()), - 30 => Some("rootstock-main".to_string()), - 31 => Some("rootstock-test".to_string()), - 42 => Some("kovan".to_string()), - 61 => Some("etc-main".to_string()), - 62 => Some("etc-test".to_string()), + 1 => Some(ETH), + 2 => Some(MORDEN), + 3 => Some(ROPSTEN), + 4 => Some(RINKEBY), + 30 => Some(ROOTSTOCK_MAINNET), + 31 => Some(ROOTSTOCK_TESTNET), + 42 => Some(KOVAN), + 61 => Some(ETC), + 62 => Some(ETC_MORDEN), _ => None, } } @@ -63,16 +74,16 @@ pub fn to_chain_name(id: u8) -> Option { /// * `name` - target chain name /// pub fn to_chain_id(name: &str) -> Option { - match name { - "eth" => Some(1), - "morden" => Some(2), - "ropsten" => Some(3), - "rinkeby" => Some(4), - "rootstock-main" => Some(30), - "rootstock-test" => Some(31), - "kovan" => Some(42), - "etc-main" => Some(61), - "etc-test" => Some(62), + match name.to_lowercase().as_str() { + ETH => Some(1), + MORDEN => Some(2), + ROPSTEN => Some(3), + RINKEBY => Some(4), + ROOTSTOCK_MAINNET => Some(30), + ROOTSTOCK_TESTNET => Some(31), + KOVAN => Some(42), + ETC | MAINNET => Some(61), + ETC_MORDEN => Some(62), _ => None, } } @@ -408,7 +419,26 @@ mod tests { assert_eq!(to_chain_id("rootstock-main"), Some(30)); assert_eq!(to_chain_id("rootstock-test"), Some(31)); assert_eq!(to_chain_id("kovan"), Some(42)); - assert_eq!(to_chain_id("etc-main"), Some(61)); - assert_eq!(to_chain_id("etc-test"), Some(62)); + assert_eq!(to_chain_id("etc"), Some(61)); + assert_eq!(to_chain_id("mainnet"), Some(61)); + assert_eq!(to_chain_id("etc-morden"), Some(62)); + + assert_eq!(to_chain_id("eTc"), Some(61)); + assert_eq!(to_chain_id("ecccc"), None); + } + + #[test] + fn should_convert_to_chain_name() { + assert_eq!(to_chain_name(1), Some(ETH)); + assert_eq!(to_chain_name(2), Some(MORDEN)); + assert_eq!(to_chain_name(3), Some(ROPSTEN)); + assert_eq!(to_chain_name(4), Some(RINKEBY)); + assert_eq!(to_chain_name(30), Some(ROOTSTOCK_MAINNET)); + assert_eq!(to_chain_name(31), Some(ROOTSTOCK_TESTNET)); + assert_eq!(to_chain_name(42), Some(KOVAN)); + assert_eq!(to_chain_name(61), Some(ETC)); + assert_eq!(to_chain_name(62), Some(ETC_MORDEN)); + + assert_eq!(to_chain_name(100), None); } }