-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathsigned.rs
317 lines (275 loc) · 10.3 KB
/
signed.rs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::transaction::{RlpEcdsaDecodableTx, RlpEcdsaEncodableTx, SignableTransaction};
use alloy_eips::eip2718::Eip2718Result;
use alloy_primitives::{Signature, B256};
use alloy_rlp::BufMut;
use core::hash::{Hash, Hasher};
#[cfg(not(feature = "std"))]
use once_cell::race::OnceBox as OnceLock;
#[cfg(feature = "std")]
use std::sync::OnceLock;
/// A transaction with a signature and hash seal.
#[derive(Debug, Clone)]
pub struct Signed<T, Sig = Signature> {
#[doc(alias = "transaction")]
tx: T,
signature: Sig,
#[doc(alias = "tx_hash", alias = "transaction_hash")]
hash: OnceLock<B256>,
}
impl<T, Sig> Signed<T, Sig> {
/// Instantiate from a transaction and signature. Does not verify the signature.
pub fn new_unchecked(tx: T, signature: Sig, hash: B256) -> Self {
let value = OnceLock::new();
#[allow(clippy::useless_conversion)]
value.get_or_init(|| hash.into());
Self { tx, signature, hash: value }
}
/// Instantiate from a transaction and signature. Does not verify the signature.
pub const fn new_unhashed(tx: T, signature: Sig) -> Self {
Self { tx, signature, hash: OnceLock::new() }
}
/// Returns a reference to the transaction.
#[doc(alias = "transaction")]
pub const fn tx(&self) -> &T {
&self.tx
}
/// Returns a mutable reference to the transaction.
pub fn tx_mut(&mut self) -> &mut T {
&mut self.tx
}
/// Returns a reference to the signature.
pub const fn signature(&self) -> &Sig {
&self.signature
}
/// Returns the transaction without signature.
pub fn strip_signature(self) -> T {
self.tx
}
/// Converts the transaction type to the given alternative that is `From<T>`
///
/// Caution: This is only intended for converting transaction types that are structurally
/// equivalent (produce the same hash).
pub fn convert<U>(self) -> Signed<U, Sig>
where
U: From<T>,
{
self.map(U::from)
}
/// Converts the transaction to the given alternative that is `TryFrom<T>`
///
/// Returns the transaction with the new transaction type if all conversions were successful.
///
/// Caution: This is only intended for converting transaction types that are structurally
/// equivalent (produce the same hash).
pub fn try_convert<U>(self) -> Result<Signed<U, Sig>, U::Error>
where
U: TryFrom<T>,
{
self.try_map(U::try_from)
}
/// Applies the given closure to the inner transaction type.
///
/// Caution: This is only intended for converting transaction types that are structurally
/// equivalent (produce the same hash).
pub fn map<Tx>(self, f: impl FnOnce(T) -> Tx) -> Signed<Tx, Sig> {
let Self { tx, signature, hash } = self;
Signed { tx: f(tx), signature, hash }
}
/// Applies the given fallible closure to the inner transactions.
///
/// Caution: This is only intended for converting transaction types that are structurally
/// equivalent (produce the same hash).
pub fn try_map<Tx, E>(self, f: impl FnOnce(T) -> Result<Tx, E>) -> Result<Signed<Tx, Sig>, E> {
let Self { tx, signature, hash } = self;
Ok(Signed { tx: f(tx)?, signature, hash })
}
}
impl<T: SignableTransaction<Sig>, Sig> Signed<T, Sig> {
/// Calculate the signing hash for the transaction.
pub fn signature_hash(&self) -> B256 {
self.tx.signature_hash()
}
}
impl<T> Signed<T>
where
T: RlpEcdsaEncodableTx,
{
/// Returns a reference to the transaction hash.
#[doc(alias = "tx_hash", alias = "transaction_hash")]
pub fn hash(&self) -> &B256 {
#[allow(clippy::useless_conversion)]
self.hash.get_or_init(|| self.tx.tx_hash(&self.signature).into())
}
/// Splits the transaction into parts.
pub fn into_parts(self) -> (T, Signature, B256) {
let hash = *self.hash();
(self.tx, self.signature, hash)
}
/// Get the length of the transaction when RLP encoded.
pub fn rlp_encoded_length(&self) -> usize {
self.tx.rlp_encoded_length_with_signature(&self.signature)
}
/// RLP encode the signed transaction.
pub fn rlp_encode(&self, out: &mut dyn BufMut) {
self.tx.rlp_encode_signed(&self.signature, out);
}
/// Get the length of the transaction when EIP-2718 encoded.
pub fn eip2718_encoded_length(&self) -> usize {
self.tx.eip2718_encoded_length(&self.signature)
}
/// EIP-2718 encode the signed transaction with a specified type flag.
pub fn eip2718_encode_with_type(&self, ty: u8, out: &mut dyn BufMut) {
self.tx.eip2718_encode_with_type(&self.signature, ty, out);
}
/// EIP-2718 encode the signed transaction.
pub fn eip2718_encode(&self, out: &mut dyn BufMut) {
self.tx.eip2718_encode(&self.signature, out);
}
/// Get the length of the transaction when network encoded.
pub fn network_encoded_length(&self) -> usize {
self.tx.network_encoded_length(&self.signature)
}
/// Network encode the signed transaction with a specified type flag.
pub fn network_encode_with_type(&self, ty: u8, out: &mut dyn BufMut) {
self.tx.network_encode_with_type(&self.signature, ty, out);
}
/// Network encode the signed transaction.
pub fn network_encode(&self, out: &mut dyn BufMut) {
self.tx.network_encode(&self.signature, out);
}
}
impl<T> Signed<T>
where
T: RlpEcdsaDecodableTx,
{
/// RLP decode the signed transaction.
pub fn rlp_decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
T::rlp_decode_signed(buf)
}
/// EIP-2718 decode the signed transaction with a specified type flag.
pub fn eip2718_decode_with_type(buf: &mut &[u8], ty: u8) -> Eip2718Result<Self> {
T::eip2718_decode_with_type(buf, ty)
}
/// EIP-2718 decode the signed transaction.
pub fn eip2718_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
T::eip2718_decode(buf)
}
/// Network decode the signed transaction with a specified type flag.
pub fn network_decode_with_type(buf: &mut &[u8], ty: u8) -> Eip2718Result<Self> {
T::network_decode_with_type(buf, ty)
}
/// Network decode the signed transaction.
pub fn network_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
T::network_decode(buf)
}
}
impl<T> Hash for Signed<T>
where
T: RlpEcdsaDecodableTx + Hash,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.hash().hash(state);
self.tx.hash(state);
self.signature.hash(state);
}
}
impl<T: RlpEcdsaEncodableTx + PartialEq> PartialEq for Signed<T> {
fn eq(&self, other: &Self) -> bool {
self.hash() == other.hash() && self.tx == other.tx && self.signature == other.signature
}
}
impl<T: RlpEcdsaEncodableTx + PartialEq> Eq for Signed<T> {}
#[cfg(feature = "k256")]
impl<T: SignableTransaction<Signature>> Signed<T, Signature> {
/// Recover the signer of the transaction
pub fn recover_signer(
&self,
) -> Result<alloy_primitives::Address, alloy_primitives::SignatureError> {
let sighash = self.tx.signature_hash();
self.signature.recover_address_from_prehash(&sighash)
}
/// Attempts to recover signer and constructs a [`crate::transaction::Recovered`] object.
pub fn try_into_recovered(
self,
) -> Result<crate::transaction::Recovered<T>, alloy_primitives::SignatureError> {
let signer = self.recover_signer()?;
Ok(crate::transaction::Recovered::new_unchecked(self.tx, signer))
}
/// Attempts to recover signer and constructs a [`crate::transaction::Recovered`] with a
/// reference to the transaction `Recovered<&T>`
pub fn try_to_recovered_ref(
&self,
) -> Result<crate::transaction::Recovered<&T>, alloy_primitives::SignatureError> {
let signer = self.recover_signer()?;
Ok(crate::transaction::Recovered::new_unchecked(&self.tx, signer))
}
}
#[cfg(all(any(test, feature = "arbitrary"), feature = "k256"))]
impl<'a, T: SignableTransaction<Signature> + arbitrary::Arbitrary<'a>> arbitrary::Arbitrary<'a>
for Signed<T, Signature>
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
use k256::{
ecdsa::{signature::hazmat::PrehashSigner, SigningKey},
NonZeroScalar,
};
use rand::{rngs::StdRng, SeedableRng};
let rng_seed = u.arbitrary::<[u8; 32]>()?;
let mut rand_gen = StdRng::from_seed(rng_seed);
let signing_key: SigningKey = NonZeroScalar::random(&mut rand_gen).into();
let tx = T::arbitrary(u)?;
let (recoverable_sig, recovery_id) =
signing_key.sign_prehash(tx.signature_hash().as_ref()).unwrap();
let signature: Signature = (recoverable_sig, recovery_id).into();
Ok(tx.into_signed(signature))
}
}
#[cfg(feature = "serde")]
mod serde {
use crate::transaction::RlpEcdsaEncodableTx;
use alloc::borrow::Cow;
use alloy_primitives::B256;
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize, Serializer};
#[derive(Serialize, Deserialize)]
struct Signed<'a, T: Clone, Sig: Clone> {
#[serde(flatten)]
tx: Cow<'a, T>,
#[serde(flatten)]
signature: Cow<'a, Sig>,
hash: Cow<'a, B256>,
}
impl<T> Serialize for super::Signed<T>
where
T: Clone + RlpEcdsaEncodableTx + Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Signed {
tx: Cow::Borrowed(&self.tx),
signature: Cow::Borrowed(&self.signature),
hash: Cow::Borrowed(self.hash()),
}
.serialize(serializer)
}
}
impl<'de, T, Sig> Deserialize<'de> for super::Signed<T, Sig>
where
T: Clone + DeserializeOwned,
Sig: Clone + DeserializeOwned,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Signed::<T, Sig>::deserialize(deserializer).map(|value| {
Self::new_unchecked(
value.tx.into_owned(),
value.signature.into_owned(),
value.hash.into_owned(),
)
})
}
}
}