1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
//! Module defining Ethereum transaction data as well as an RLP encoding
//! implementation.
pub mod accesslist;
mod eip1559;
mod eip2930;
mod legacy;
mod rlp;
pub use self::{
eip1559::Eip1559Transaction, eip2930::Eip2930Transaction, legacy::LegacyTransaction,
};
use crate::{account::Signature, hash};
use serde::{
de::{self, Deserializer},
Deserialize,
};
use serde_json::Map as JsonObject;
/// An Ethereum transaction.
#[derive(Clone, Debug)]
pub enum Transaction {
Legacy(LegacyTransaction),
Eip2930(Eip2930Transaction),
Eip1559(Eip1559Transaction),
}
impl Transaction {
/// Returns the RLP encoded transaction with an optional signature.
pub fn signing_message(&self) -> [u8; 32] {
hash::keccak256(self.rlp_encode(None))
}
/// Returns the 32-byte message used for signing.
pub fn encode(&self, signature: Signature) -> Vec<u8> {
self.rlp_encode(Some(signature))
}
/// Returns the RLP encoded transaction with an optional signature.
fn rlp_encode(&self, signature: Option<Signature>) -> Vec<u8> {
match self {
Transaction::Legacy(tx) => tx.rlp_encode(signature),
Transaction::Eip2930(tx) => tx.rlp_encode(signature),
Transaction::Eip1559(tx) => tx.rlp_encode(signature),
}
}
}
impl<'de> Deserialize<'de> for Transaction {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let json = JsonObject::deserialize(deserializer)?;
if json.contains_key("maxPriorityFeePerGas") || json.contains_key("maxFeePerGas") {
Ok(Transaction::Eip1559(
serde_json::from_value(json.into()).map_err(de::Error::custom)?,
))
} else if json.contains_key("accessList") {
Ok(Transaction::Eip2930(
serde_json::from_value(json.into()).map_err(de::Error::custom)?,
))
} else {
Ok(Transaction::Legacy(
serde_json::from_value(json.into()).map_err(de::Error::custom)?,
))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{account::PrivateKey, ganache::DETERMINISTIC_PRIVATE_KEY};
use hex_literal::hex;
use serde_json::{json, Value};
fn sign_encode(tx: Value) -> Vec<u8> {
let tx = serde_json::from_value::<Transaction>(tx).unwrap();
let key = PrivateKey::new(DETERMINISTIC_PRIVATE_KEY).unwrap();
let signature = key.sign(tx.signing_message());
tx.encode(signature)
}
#[test]
fn encode_signed_transaction() {
assert_eq!(
sign_encode(json!({
"nonce": 0,
"gasPrice": 0,
"gasLimit": 21000,
"to": "0x0000000000000000000000000000000000000000",
"value": 0,
"data": "0x",
})),
hex!(
"f85f808082520894000000000000000000000000000000000000000080801ca0
0f1c0e95b7050ac3df5ac3b69a7d41e0b815da462fcd30954b1c37b58ca71c16
a068dab467ad79359967a3df1bcfc17292a3839288d05274d0e3e391f8b50841
0b"
),
);
assert_eq!(
sign_encode(json!({
"chainId": 1,
"nonce": 0,
"gasPrice": 0,
"gasLimit": 21000,
"to": "0x0000000000000000000000000000000000000000",
"value": 0,
"data": "0x",
})),
hex!(
"f85f8080825208940000000000000000000000000000000000000000808025a0
c97442e361bf3940bec722b240c699de22302469756436bbcc5a150a93309b08
a02fd3e68ed327dea3d085ec16a8589ebf7871e5a990669f67be82a70cd9dfb4
f7"
),
);
assert_eq!(
sign_encode(json!({
"chainId": 1,
"nonce": 0,
"gasPrice": 0,
"gasLimit": 21000,
"to": "0x0000000000000000000000000000000000000000",
"value": 0,
"data": "0x",
"accessList": [],
})),
hex!(
"01f8610180808252089400000000000000000000000000000000000000008080
c080a04366d11301b0a233d0f311f93083583ed316c2ebd7246ccd93f1a320b2
57fd65a02e3df28ccda84b829403a04f2d142416f01bdf7036dba12b66e4add6
4d59455e"
),
);
assert_eq!(
sign_encode(json!({
"chainId": 1,
"nonce": 0,
"maxPriorityFeePerGas": 0,
"maxFeePerGas": 0,
"gasLimit": 21000,
"to": "0x0000000000000000000000000000000000000000",
"value": 0,
"data": "0x",
})),
hex!(
"02f8620180808082520894000000000000000000000000000000000000000080
80c001a0290dbdecbc884b4cb827015fe0cd7ac90df1a5634d52a2845c21afac
ca14b803a03e848dd1a342e5528beff99c42876cf091a68e2090dbbced5a5f7f
392d3abcda"
),
);
}
}
|