Skip to content

Commit

Permalink
move Accounts::hash_message and make it static
Browse files Browse the repository at this point in the history
  • Loading branch information
blackghost1987 committed Jan 18, 2022
1 parent 81c41ae commit a2a71d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/api/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Partial implementation of the `Accounts` namespace.

use crate::{
api::Namespace,
api::{hash_message, Namespace},
signing,
types::{AccessList, H256, U64},
Transport,
Expand Down 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()
hash_message(message)
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub use self::{
};

use crate::{
confirm, error,
types::{Bytes, TransactionReceipt, TransactionRequest, U64},
confirm, error, signing,
types::{Bytes, TransactionReceipt, TransactionRequest, U64, H256},
DuplexTransport, Transport,
};
use futures::Future;
Expand Down Expand Up @@ -163,3 +163,20 @@ impl<T: DuplexTransport> Web3<T> {
self.api()
}
}

/// 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);

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

0 comments on commit a2a71d0

Please sign in to comment.