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

Commit

Permalink
remove type constant and use typed sighash
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Apr 26, 2022
1 parent f1b31e9 commit 7170883
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
22 changes: 4 additions & 18 deletions ethers-core/src/types/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::{decode_to, eip2930::AccessList, normalize_v, rlp_opt};
use crate::{
types::{
Address, Bytes, NameOrAddress, Signature, SignatureError, Transaction, H256, U256, U64,
},
utils::keccak256,
use super::{decode_to, eip2718::TypedTransaction, eip2930::AccessList, normalize_v, rlp_opt};
use crate::types::{
Address, Bytes, NameOrAddress, Signature, SignatureError, Transaction, U256, U64,
};
use rlp::{Decodable, DecoderError, RlpStream};
use thiserror::Error;
Expand Down Expand Up @@ -80,9 +77,6 @@ pub struct Eip1559TransactionRequest {
}

impl Eip1559TransactionRequest {
/// EIP-2718 transaction type
const TX_TYPE: u8 = 0x02;

/// Creates an empty transaction request with all fields left empty
pub fn new() -> Self {
Self::default()
Expand Down Expand Up @@ -160,14 +154,6 @@ impl Eip1559TransactionRequest {
self
}

/// Hashes the transaction's data for signing
pub fn sighash(&self) -> H256 {
let mut encoded = vec![];
encoded.extend_from_slice(&[Self::TX_TYPE]);
encoded.extend_from_slice(self.rlp().as_ref());
keccak256(encoded).into()
}

/// Gets the unsigned transaction's RLP encoding
pub fn rlp(&self) -> Bytes {
let mut rlp = RlpStream::new();
Expand Down Expand Up @@ -247,7 +233,7 @@ impl Eip1559TransactionRequest {
let s = rlp.val_at(offset)?;

let sig = Signature { r, s, v };
txn.from = Some(sig.recover(txn.sighash())?);
txn.from = Some(sig.recover(TypedTransaction::Eip1559(txn.clone()).sighash())?);

Ok((txn, sig))
}
Expand Down
22 changes: 4 additions & 18 deletions ethers-core/src/types/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::normalize_v;
use crate::{
types::{
Address, Bytes, Signature, SignatureError, Transaction, TransactionRequest, H256, U256, U64,
},
utils::keccak256,
use super::{eip2718::TypedTransaction, normalize_v};
use crate::types::{
Address, Bytes, Signature, SignatureError, Transaction, TransactionRequest, H256, U256, U64,
};
use rlp::{Decodable, RlpStream};
use rlp_derive::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};
Expand Down Expand Up @@ -84,9 +81,6 @@ pub struct Eip2930TransactionRequest {
}

impl Eip2930TransactionRequest {
/// EIP-2718 transaction type
const TX_TYPE: u8 = 0x01;

pub fn new(tx: TransactionRequest, access_list: AccessList) -> Self {
Self { tx, access_list }
}
Expand Down Expand Up @@ -153,17 +147,9 @@ impl Eip2930TransactionRequest {
let s = rlp.val_at(offset)?;

let sig = Signature { r, s, v };
txn.tx.from = Some(sig.recover(txn.sighash())?);
txn.tx.from = Some(sig.recover(TypedTransaction::Eip2930(txn.clone()).sighash())?);
Ok((txn, sig))
}

/// Hashes the transaction's data for signing
pub fn sighash(&self) -> H256 {
let mut encoded = vec![];
encoded.extend_from_slice(&[Self::TX_TYPE]);
encoded.extend_from_slice(self.rlp().as_ref());
keccak256(encoded).into()
}
}

/// Get a Eip2930TransactionRequest from a rlp encoded byte stream
Expand Down

0 comments on commit 7170883

Please sign in to comment.