-
Notifications
You must be signed in to change notification settings - Fork 231
/
signed.rs
55 lines (47 loc) · 1.53 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
use crate::transaction::SignableTransaction;
use alloy_primitives::{Signature, B256};
/// A transaction with a signature and hash seal.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Signed<T, Sig = Signature> {
tx: T,
signature: Sig,
hash: B256,
}
impl<T, Sig> Signed<T, Sig> {
/// Returns a reference to the transaction.
pub const fn tx(&self) -> &T {
&self.tx
}
/// Returns a reference to the signature.
pub const fn signature(&self) -> &Sig {
&self.signature
}
/// Returns a reference to the transaction hash.
pub const fn hash(&self) -> &B256 {
&self.hash
}
/// Splits the transaction into parts.
pub fn into_parts(self) -> (T, Sig, B256) {
(self.tx, self.signature, self.hash)
}
}
impl<T: SignableTransaction<Sig>, Sig> Signed<T, Sig> {
/// Instantiate from a transaction and signature. Does not verify the signature.
pub const fn new_unchecked(tx: T, signature: Sig, hash: B256) -> Self {
Self { tx, signature, hash }
}
/// Calculate the signing hash for the transaction.
pub fn signature_hash(&self) -> B256 {
self.tx.signature_hash()
}
}
#[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)
}
}