aboutsummaryrefslogtreecommitdiff
path: root/src/account.rs
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2024-06-28 13:38:09 +0200
committerNicholas Rodrigues Lordello <[email protected]>2024-06-28 13:38:13 +0200
commitbdb47ee691f0302c05daaf0278b606fbaf319f61 (patch)
tree3c414c0adbcfa8a76255502e9d5d38bdbb31d5d0 /src/account.rs
parent0f523a185ff67848e890156c04b95f765969051d (diff)
Update dependency version
Additionally, we start using `ethdigest` instead of the `sha3` crate for doing Keccak-256 hashing and representing digest types in code.
Diffstat (limited to 'src/account.rs')
-rw-r--r--src/account.rs25
1 files changed, 13 insertions, 12 deletions
diff --git a/src/account.rs b/src/account.rs
index f862853..f3340aa 100644
--- a/src/account.rs
+++ b/src/account.rs
@@ -4,9 +4,9 @@ mod public;
mod signature;
pub use self::{public::PublicKey, signature::Signature};
-use crate::hash;
use anyhow::Result;
use ethaddr::Address;
+use ethdigest::Digest;
use k256::{
ecdsa::{hazmat::SignPrimitive, SigningKey},
SecretKey,
@@ -40,9 +40,9 @@ impl PrivateKey {
// an uncompressed point) and the subsequent bytes are the coordinates
// we want. So discard the first byte for the address calculation.
debug_assert_eq!(encoded[0], 0x04);
- let hash = hash::keccak256(&encoded[1..]);
+ let digest = Digest::of(&encoded[1..]);
- Address::from_slice(&hash[12..])
+ Address::from_slice(&digest[12..])
}
/// Returns the private key's 32 byte secret.
@@ -51,15 +51,15 @@ impl PrivateKey {
}
/// Generate a signature for the specified message.
- pub fn sign(&self, message: [u8; 32]) -> Signature {
+ pub fn sign(&self, message: Digest) -> Signature {
self.try_sign(message).expect("signature operation failed")
}
/// Generate a signature for the specified message.
- pub fn try_sign(&self, message: [u8; 32]) -> Result<Signature> {
+ pub fn try_sign(&self, message: Digest) -> Result<Signature> {
let (signature, recovery_id) = SigningKey::from(&self.0)
.as_nonzero_scalar()
- .try_sign_prehashed_rfc6979::<Sha256>(&message.into(), b"")?;
+ .try_sign_prehashed_rfc6979::<Sha256>(&message.0.into(), b"")?;
Ok(Signature(signature, recovery_id.unwrap()))
}
}
@@ -74,26 +74,27 @@ impl Debug for PrivateKey {
mod tests {
use super::*;
use crate::ganache::DETERMINISTIC_PRIVATE_KEY;
- use hex_literal::hex;
+ use ethaddr::address;
+ use ethnum::uint;
#[test]
fn ganache_determinitic_address() {
let key = PrivateKey::new(DETERMINISTIC_PRIVATE_KEY).unwrap();
assert_eq!(
- *key.address(),
- hex!("90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"),
+ key.address(),
+ address!("90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"),
);
}
#[test]
fn ganache_deterministic_signature() {
let key = PrivateKey::new(DETERMINISTIC_PRIVATE_KEY).unwrap();
- let message = hash::keccak256(b"\x19Ethereum Signed Message:\n12Hello World!");
+ let message = Digest::of(b"\x19Ethereum Signed Message:\n12Hello World!");
assert_eq!(
key.sign(message),
Signature::from_parts(
- hex!("408790f153cbfa2722fc708a57d97a43b24429724cf060df7c915d468c43bd84"),
- hex!("61c96aac95ce37d7a31087b6634f4a3ea439a9f704b5c818584fa2a32fa83859"),
+ uint!("0x408790f153cbfa2722fc708a57d97a43b24429724cf060df7c915d468c43bd84"),
+ uint!("0x61c96aac95ce37d7a31087b6634f4a3ea439a9f704b5c818584fa2a32fa83859"),
1,
),
);