Skip to content

Commit 5a9b649

Browse files
committed
primitives - remove eth_checksum and move the code to the crate instead
1 parent e2d1d59 commit 5a9b649

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

primitives/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ time = "0.1.42"
2525
hex = "0.3.2"
2626
merkletree = "0.10.0"
2727
tiny-keccak = "1.5"
28-
eth_checksum = { version = "0.1.1", git = "https://github.com/elpiel/rust-eth-checksum", branch = "clean-up-and-performance" }
28+
rust-crypto = "0.2"
2929
# Numbers - BigNum, Numbers, Traits and Derives
3030
num-bigint = { version = "0.2", features = ["serde"] }
3131
num = "0.2.0"

primitives/src/big_num.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::convert::TryFrom;
2-
use std::error::Error;
32
use std::iter::Sum;
43
use std::ops::{Add, AddAssign, Div, Mul, Sub};
54
use std::str::FromStr;

primitives/src/eth_checksum.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crypto::{digest::Digest, sha3::Sha3};
2+
3+
pub fn checksum(address: &str) -> String {
4+
let address = address.trim_start_matches("0x").to_lowercase();
5+
6+
let address_hash = {
7+
let mut hasher = Sha3::keccak256();
8+
hasher.input(address.as_bytes());
9+
hasher.result_str()
10+
};
11+
12+
address
13+
.char_indices()
14+
.fold(String::from("0x"), |mut acc, (index, address_char)| {
15+
// this cannot fail since it's Keccak256 hashed
16+
let n = u16::from_str_radix(&address_hash[index..index + 1], 16).unwrap();
17+
18+
if n > 7 {
19+
// make char uppercase if ith character is 9..f
20+
acc.push_str(&address_char.to_uppercase().to_string())
21+
} else {
22+
// already lowercased
23+
acc.push(address_char)
24+
}
25+
26+
acc
27+
})
28+
}

primitives/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod util {
2424
pub mod logging;
2525
}
2626
pub mod analytics;
27+
mod eth_checksum;
2728
pub mod validator;
2829

2930
pub use self::ad_unit::AdUnit;

0 commit comments

Comments
 (0)