Skip to content

Commit

Permalink
move Accounts::hash_message and make it static (#588)
Browse files Browse the repository at this point in the history
* move Accounts::hash_message and make it static

* fix formatting

* move hash_message to crate::signing
  • Loading branch information
blackghost1987 authored Feb 4, 2022
1 parent 11b686e commit 2c0286d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/api/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ impl<T: Transport> Accounts<T> {
where
S: AsRef<[u8]>,
{
let message = message.as_ref();

let mut eth_message = format!("\x19Ethereum Signed Message:\n{}", message.len()).into_bytes();
eth_message.extend_from_slice(message);

signing::keccak256(&eth_message).into()
signing::hash_message(message)
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ pub fn namehash(name: &str) -> NameHash {
node
}

/// Hash a message according to EIP-191.
///
/// The data is a UTF-8 encoded string and will enveloped as follows:
/// `"\x19Ethereum Signed Message:\n" + message.length + message` and hashed
/// using keccak256.
pub fn hash_message<S>(message: S) -> H256
where
S: AsRef<[u8]>,
{
let message = message.as_ref();

let mut eth_message = format!("\x19Ethereum Signed Message:\n{}", message.len()).into_bytes();
eth_message.extend_from_slice(message);

keccak256(&eth_message).into()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 2c0286d

Please sign in to comment.