aboutsummaryrefslogtreecommitdiff
path: root/src/transaction.rs
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2021-03-18 09:28:50 +0100
committerNicholas Rodrigues Lordello <[email protected]>2021-03-18 09:28:50 +0100
commit789f4585504119de8e490aa12a8c02b649f24ee0 (patch)
treeef2df8f30af6f06102b49555a308d77d9eae36ef /src/transaction.rs
parent1ce0c196c10dc92977114279180ea714056c587d (diff)
added hashing command
Diffstat (limited to 'src/transaction.rs')
-rw-r--r--src/transaction.rs15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/transaction.rs b/src/transaction.rs
index 9aa95ea..fe4eb35 100644
--- a/src/transaction.rs
+++ b/src/transaction.rs
@@ -1,7 +1,10 @@
//! Module defining Ethereum transaction data as well as an RLP encoding
//! implementation.
-use crate::account::{Address, Signature};
+use crate::{
+ account::{Address, Signature},
+ hash,
+};
use ethnum::U256;
/// An Ethereum transaction.
@@ -53,6 +56,11 @@ impl Transaction {
&rlp::uint(s),
])
}
+
+ /// Returns the 32-byte message used for signing.
+ pub fn signing_message(&self, chain_id: u64) -> [u8; 32] {
+ hash::keccak256(self.encode(chain_id, None))
+ }
}
/// Tiny RLP encoding implementation.
@@ -116,7 +124,7 @@ fn replay_protection(chain_id: u64, v: u8) -> u64 {
#[cfg(test)]
mod tests {
use super::*;
- use crate::{account::PrivateKey, ganache::DETERMINISTIC_PRIVATE_KEY, hash};
+ use crate::{account::PrivateKey, ganache::DETERMINISTIC_PRIVATE_KEY};
use hex_literal::hex;
const CHAIN_ID: u64 = 0x1337;
@@ -146,8 +154,7 @@ mod tests {
#[test]
fn rlp_encode_with_signature() {
let key = PrivateKey::new(DETERMINISTIC_PRIVATE_KEY).unwrap();
- let message = hash::keccak256(transaction().encode(CHAIN_ID, None));
- let signature = key.sign(message);
+ let signature = key.sign(transaction().signing_message(CHAIN_ID));
assert_eq!(
transaction().encode(CHAIN_ID, Some(signature)),