Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename chain id #300

Merged
merged 3 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions src/core/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,30 @@ mod tests {

#[test]
fn should_catch_wrong_address_encoding() {
assert!(
"0x___c045110b8dbf29765047380898919c5cb56f4"
.parse::<Address>()
.is_err()
);
assert!("0x___c045110b8dbf29765047380898919c5cb56f4"
.parse::<Address>()
.is_err());
}

#[test]
fn should_catch_wrong_address_insufficient_length() {
assert!(
"0x0e7c045110b8dbf297650473808989"
.parse::<Address>()
.is_err()
);
assert!("0x0e7c045110b8dbf297650473808989"
.parse::<Address>()
.is_err());
}

#[test]
fn should_catch_wrong_address_excess_length() {
assert!(
"0x0e7c045110b8dbf29765047380898919c5cb56f400000000"
.parse::<Address>()
.is_err()
);
assert!("0x0e7c045110b8dbf29765047380898919c5cb56f400000000"
.parse::<Address>()
.is_err());
}

#[test]
fn should_catch_wrong_address_prefix() {
assert!(
"0_0e7c045110b8dbf29765047380898919c5cb56f4"
.parse::<Address>()
.is_err()
);
assert!("0_0e7c045110b8dbf29765047380898919c5cb56f4"
.parse::<Address>()
.is_err());
}

#[test]
Expand Down
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
4 changes: 2 additions & 2 deletions src/storage/storage_ctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
74 changes: 52 additions & 22 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-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,
}
}
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-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,
}
}
Expand Down Expand Up @@ -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);
}
}