-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: remove bitcoin_hashes * refactor: remove sha2 * chore: run rustfmt
- Loading branch information
1 parent
d4c03c5
commit 5c98e6f
Showing
11 changed files
with
155 additions
and
148 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod sha3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use sha3::{Digest, Keccak256}; | ||
|
||
/// Computes the Keccak-256 hash of the input data. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use walleth::utils::crypto::sha3::keccak256; | ||
/// | ||
/// let hash = keccak256(&"Hello, world!".as_bytes()); | ||
/// | ||
/// assert_eq!( | ||
/// hash, | ||
/// [ | ||
/// 182, 225, 109, 39, 172, | ||
/// 90, 180, 39, 167, 246, | ||
/// 137, 0, 172, 85, 89, | ||
/// 206, 39, 45, 198, 195, | ||
/// 124, 130, 179, 224, 82, | ||
/// 36, 108, 130, 36, 76, | ||
/// 80, 228 | ||
/// ] | ||
/// ); | ||
/// ``` | ||
/// | ||
pub fn keccak256(data: &[u8]) -> [u8; 32] { | ||
let mut hasher = Keccak256::new(); | ||
hasher.update(data); | ||
hasher.finalize().into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use hex; | ||
|
||
/// Encode a byte array into a hex string | ||
pub fn encode(data: &[u8]) -> String { | ||
hex::encode(data) | ||
} | ||
|
||
/// Decode a hex string into a byte array | ||
pub fn decode(data: &str) -> Result<Vec<u8>, String> { | ||
match hex::decode(data) { | ||
Ok(bytes) => Ok(bytes), | ||
Err(e) => Err(e.to_string()), | ||
} | ||
} | ||
|
||
/// Assert that a string is a valid hex address | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use walleth::utils::hex::assert_is_valid_hex_address; | ||
/// | ||
/// assert!(assert_is_valid_hex_address(&"0x00C08c440DbDC3A2a9C9D99b30077a53Ba7eDEAD".to_string()).is_ok()); | ||
/// ``` | ||
pub fn assert_is_valid_hex_address(value: &String) -> Result<(), String> { | ||
let unprefixed = remove0x(value); | ||
|
||
assert_is_hex(&unprefixed)?; | ||
|
||
if unprefixed.len() != 40 { | ||
return Err(format!( | ||
"String passed into assert_is_valid_hex_address is {} hex chars long instead of 40.", | ||
value.len() | ||
)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Assert that a string is a valid hex | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use walleth::utils::hex::assert_is_hex; | ||
/// | ||
/// let assertion = assert_is_hex(&"00C08c440DbDC3A2a9C9D99b30077a53Ba7eDEAD".to_string()); | ||
/// | ||
/// assert!(assertion.is_ok()); | ||
/// ``` | ||
pub fn assert_is_hex(value: &str) -> Result<(), String> { | ||
match decode(value) { | ||
Ok(_) => Ok(()), | ||
Err(e) => Err(e.to_string()), | ||
} | ||
} | ||
|
||
/// Remove the 0x prefix from a string | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use walleth::utils::hex::remove0x; | ||
/// | ||
/// let unprefixed = remove0x(&"0x00C08c440DbDC3A2a9C9D99b30077a53Ba7eDEAD".to_string()); | ||
/// assert_eq!( | ||
/// unprefixed, | ||
/// "00C08c440DbDC3A2a9C9D99b30077a53Ba7eDEAD".to_string(), | ||
/// ); | ||
/// ``` | ||
pub fn remove0x(value: &String) -> String { | ||
match value.starts_with("0x") { | ||
true => String::from(&value[2..]), | ||
_ => value.to_string(), | ||
} | ||
} | ||
|
||
/// Add the 0x prefix to a string | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use walleth::utils::hex::add0x; | ||
/// | ||
/// assert_eq!( | ||
/// add0x(&"00C08c440DbDC3A2a9C9D99b30077a53Ba7eDEAD".to_string()), | ||
/// "0x00C08c440DbDC3A2a9C9D99b30077a53Ba7eDEAD", | ||
/// ); | ||
/// ``` | ||
pub fn add0x(value: &String) -> String { | ||
match value.starts_with("0x") { | ||
true => value.to_string(), | ||
_ => format!("0x{}", value), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod hex; | ||
pub use hex::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub mod controller; | ||
pub mod crypto; | ||
pub mod hdwallet; | ||
pub mod hex; | ||
pub mod observable; | ||
pub mod safe; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.