Skip to content

Commit

Permalink
Refactor chain name<->id convertion.
Browse files Browse the repository at this point in the history
Added alias
  • Loading branch information
r8d8 committed Nov 6, 2018
1 parent d3d026d commit 743c856
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
1 change: 0 additions & 1 deletion src/mnemonic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/util/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! # Crypto util functions
use hmac::digest::FixedOutput;
use sha3::Digest;
use sha3::Keccak256;
/// Keccak-256 crypto hash length in bytes
Expand Down
69 changes: 49 additions & 20 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,17 +53,17 @@ impl ToHex for u64 {
/// # Arguments:
/// * `id` - target chain id
///
pub fn to_chain_name(id: u8) -> Option<String> {
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".to_string()),
62 => Some("etc-morden".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,
}
}
Expand All @@ -63,16 +74,16 @@ pub fn to_chain_name(id: u8) -> Option<String> {
/// * `name` - target chain name
///
pub fn to_chain_id(name: &str) -> Option<u8> {
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" | "mainnet" => Some(61),
"etc-morden" => 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,
}
}
Expand Down Expand Up @@ -411,5 +422,23 @@ mod tests {
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);
}
}

0 comments on commit 743c856

Please sign in to comment.