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.
use crate::{
account::{Address, Signature},
hash,
};
use ethnum::U256;
/// An Ethereum transaction.
#[derive(Clone, Debug)]
pub struct Transaction {
/// The nonce for the transaction.
pub nonce: U256,
/// The gas price in Wei for the transaction.
pub gas_price: U256,
/// The gas limit for the transaction.
pub gas: U256,
/// The target address for the transaction. This can also be `None` to
/// indicate a contract creation transaction.
pub to: Option<Address>,
/// The amount of Ether to send with the transaction.
pub value: U256,
/// The calldata to use for the transaction.
pub data: Vec<u8>,
}
impl Transaction {
/// Returns the RLP encoded transaction with an optional signature.
pub fn encode(&self, chain_id: U256, signature: Option<Signature>) -> Vec<u8> {
// NOTE: This is currently not at all optimal in terms of memory
// allocations, but we don't really care.
let (v, r, s) = signature.map_or((chain_id, U256::ZERO, U256::ZERO), |signature| {
(
signature.v_replay_protected(chain_id),
U256::from_be_bytes(signature.r),
U256::from_be_bytes(signature.s),
)
});
rlp::list(&[
&rlp::uint(self.nonce)[..],
&rlp::uint(self.gas_price),
&rlp::uint(self.gas),
&self
.to
.map_or_else(|| rlp::bytes(b""), |to| rlp::bytes(&*to)),
&rlp::uint(self.value),
&rlp::bytes(&self.data),
&rlp::uint(v),
&rlp::uint(r),
&rlp::uint(s),
])
}
/// Returns the 32-byte message used for signing.
pub fn signing_message(&self, chain_id: U256) -> [u8; 32] {
hash::keccak256(self.encode(chain_id, None))
}
}
/// Tiny RLP encoding implementation.
mod rlp {
use ethnum::U256;
/// RLP encode some bytes.
pub fn bytes(bytes: &[u8]) -> Vec<u8> {
match bytes {
[x] if *x < 0x80 => vec![*x],
_ => {
let mut buf = len(bytes.len(), 0x80);
buf.extend_from_slice(bytes);
buf
}
}
}
/// RLP encode a list.
pub fn list(items: &[&[u8]]) -> Vec<u8> {
let total_len = items.iter().map(|item| item.len()).sum();
let mut buf = len(total_len, 0xc0);
for item in items {
buf.extend_from_slice(item);
}
buf
}
/// RLP encode a length.
pub fn len(len: usize, offset: u8) -> Vec<u8> {
if len < 56 {
vec![len as u8 + offset]
} else {
let bl_buf = len.to_be_bytes();
let bl = {
let start = len.leading_zeros() / 8;
&bl_buf[start as usize..]
};
let mut buf = vec![bl.len() as u8 + offset + 55];
buf.extend_from_slice(bl);
buf
}
}
/// RLP encode a unsigned integer. This ensures that it is shortned to its
/// shortest little endian byte representation.
pub fn uint(value: U256) -> Vec<u8> {
let start = value.leading_zeros() / 8;
bytes(&value.to_be_bytes()[start as usize..])
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{account::PrivateKey, ganache::DETERMINISTIC_PRIVATE_KEY};
use hex_literal::hex;
const CHAIN_ID: U256 = U256::new(0x1337);
fn transaction() -> Transaction {
Transaction {
nonce: U256::new(7777),
gas_price: U256::new(100_000_000_000),
gas: U256::new(150_000),
to: Some(Address(hex!("7070707070707070707070707070707070707070"))),
value: U256::new(42_000_000_000_000_000_000),
data: hex!("01020304").into(),
}
}
#[test]
fn rlp_encode_for_signing() {
assert_eq!(
transaction().encode(CHAIN_ID, None),
hex!(
"f6821e6185174876e800830249f0947070707070707070707070707070707070
707070890246ddf9797668000084010203048213378080"
),
)
}
#[test]
fn rlp_encode_with_signature() {
let key = PrivateKey::new(DETERMINISTIC_PRIVATE_KEY).unwrap();
let signature = key.sign(transaction().signing_message(CHAIN_ID));
assert_eq!(
transaction().encode(CHAIN_ID, Some(signature)),
hex!(
"f876821e6185174876e800830249f09470707070707070707070707070707070
70707070890246ddf979766800008401020304822691a076382953503398303f
e8c3e3d235e8f71adc39fb90fcda99514e324d96bad253a044351903896d0b6f
8a3ee9543c1e23ec3bab29a4ae5cae1a6b7b2c811105264f"
),
)
}
}
|