Skip to content

Commit

Permalink
Correctly sign non legacy transaction without EIP155 (alloy-rs#647)
Browse files Browse the repository at this point in the history
* Add tx_type method to transaction

* Remove chain_id from sign_hash_async

* refactor: remove txtype from interface

---------

Co-authored-by: James <[email protected]>
  • Loading branch information
2 people authored and ben186 committed Jul 27, 2024
1 parent 2006ae1 commit 5238556
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
4 changes: 4 additions & 0 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ impl Transaction for TxLegacy {
}

impl SignableTransaction<Signature> for TxLegacy {
fn use_eip155(&self) -> bool {
self.chain_id.is_some()
}

fn set_chain_id(&mut self, chain_id: ChainId) {
self.chain_id = Some(chain_id);
}
Expand Down
5 changes: 5 additions & 0 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ pub trait Transaction: any::Any + Send + Sync + 'static {
/// types, or in other networks. For example, in Optimism, the deposit transaction signature is the
/// unit type `()`.
pub trait SignableTransaction<Signature>: Transaction {
/// True if the transaction uses EIP-155 signatures.
fn use_eip155(&self) -> bool {
false
}

/// Sets `chain_id`.
///
/// Prefer [`set_chain_id_checked`](Self::set_chain_id_checked).
Expand Down
6 changes: 1 addition & 5 deletions crates/signer-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ impl<D: PrehashSigner<(ecdsa::Signature, RecoveryId)>> SignerSync for Wallet<D>
#[inline]
fn sign_hash_sync(&self, hash: &B256) -> Result<Signature> {
let (recoverable_sig, recovery_id) = self.signer.sign_prehash(hash.as_ref())?;
let mut sig = Signature::from_signature_and_parity(recoverable_sig, recovery_id)?;
if let Some(chain_id) = self.chain_id {
sig = sig.with_chain_id(chain_id);
}
Ok(sig)
Ok(Signature::from_signature_and_parity(recoverable_sig, recovery_id)?)
}

#[inline]
Expand Down
6 changes: 4 additions & 2 deletions crates/signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ macro_rules! sign_transaction_with_chain_id {

let mut sig = $sign.map_err(alloy_signer::Error::other)?;

if let Some(chain_id) = $signer.chain_id().or_else(|| $tx.chain_id()) {
sig = sig.with_chain_id(chain_id);
if $tx.use_eip155() {
if let Some(chain_id) = $signer.chain_id().or_else(|| $tx.chain_id()) {
sig = sig.with_chain_id(chain_id);
}
}

Ok(sig)
Expand Down

0 comments on commit 5238556

Please sign in to comment.