Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Tx optional from #1075

Merged
merged 5 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethers-core/src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct Signature {
pub r: U256,
/// S Value
pub s: U256,
/// V value in 'Electrum' notation.
/// V value
pub v: u64,
}

Expand Down
50 changes: 49 additions & 1 deletion ethers-core/src/types/transaction/response.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Transaction types
use super::{decode_signature, eip2930::AccessList, normalize_v, rlp_opt};
use crate::{
types::{Address, Bloom, Bytes, Log, H256, U256, U64},
types::{Address, Bloom, Bytes, Log, Signature, SignatureError, H256, U256, U64},
utils::keccak256,
};
use rlp::{Decodable, DecoderError, RlpStream};
Expand Down Expand Up @@ -32,6 +32,7 @@ pub struct Transaction {
pub transaction_index: Option<U64>,

/// Sender
#[serde(default = "crate::types::Address::zero")]
pub from: Address,

/// Recipient (None when contract creation)
Expand Down Expand Up @@ -311,6 +312,11 @@ impl Transaction {
*offset += 1;
Ok(())
}

pub fn recover_from(&self) -> Result<Address, SignatureError> {
let signature = Signature { r: self.r, s: self.s, v: self.v.as_u64() };
signature.recover(self.hash)
}
meetmangukiya marked this conversation as resolved.
Show resolved Hide resolved
}

/// Get a TransactionReceipt directly from an rlp encoded byte stream
Expand Down Expand Up @@ -636,4 +642,46 @@ mod tests {
// we compare hash because the hash depends on the rlp encoding
assert_eq!(decoded_transaction.hash(), tx.hash());
}

#[test]
fn recover_from() {
let tx = Transaction {
hash: H256::from_str(
"5e2fc091e15119c97722e9b63d5d32b043d077d834f377b91f80d32872c78109",
)
.unwrap(),
nonce: 65.into(),
block_hash: Some(
H256::from_str("f43869e67c02c57d1f9a07bb897b54bec1cfa1feb704d91a2ee087566de5df2c")
.unwrap(),
),
block_number: Some(6203173.into()),
transaction_index: Some(10.into()),
from: Address::from_str("e66b278fa9fbb181522f6916ec2f6d66ab846e04").unwrap(),
to: Some(Address::from_str("11d7c2ab0d4aa26b7d8502f6a7ef6844908495c2").unwrap()),
value: 0.into(),
gas_price: Some(1500000007.into()),
gas: 106703.into(),
input: hex::decode("e5225381").unwrap().into(),
v: 1.into(),
r: U256::from_str_radix(
"12010114865104992543118914714169554862963471200433926679648874237672573604889",
10,
)
.unwrap(),
s: U256::from_str_radix(
"22830728216401371437656932733690354795366167672037272747970692473382669718804",
10,
)
meetmangukiya marked this conversation as resolved.
Show resolved Hide resolved
.unwrap(),
transaction_type: Some(2.into()),
access_list: Some(AccessList::default()),
max_priority_fee_per_gas: Some(1500000000.into()),
max_fee_per_gas: Some(1500000009.into()),
chain_id: Some(5.into()),
};

assert_eq!(tx.hash, tx.hash());
assert_eq!(tx.from, tx.recover_from().unwrap());
}
}