Skip to content

Commit

Permalink
wip: more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed Feb 13, 2024
1 parent 317282b commit 3300b0c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
5 changes: 3 additions & 2 deletions crates/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ alloy-network.workspace = true
alloy-primitives = { workspace = true, features = ["rlp"] }
alloy-rlp.workspace = true
alloy-eips.workspace = true
alloy-rpc-types.workspace = true
alloy-signer.workspace = true

# arbitrary
arbitrary = { workspace = true, features = ["derive"], optional = true }
alloy-rpc-types.workspace = true
async-trait.workspace = true
alloy-signer.workspace = true
serde.workspace = true

[dev-dependencies]
alloy-signer.workspace = true
Expand Down
29 changes: 23 additions & 6 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
use alloy_network::{BuilderError, NetworkSigner, SignableTransaction, TxSigner};
use alloy_primitives::Signature;
use alloy_signer::Signer;

use async_trait::async_trait;

mod builder;
pub use builder::EthereumTxBuilder;

Expand All @@ -25,8 +19,30 @@ pub use legacy::TxLegacy;
mod typed;
pub use typed::TypedTransaction;

/*
use alloy_network::{NetworkSigner, SignableTransaction, TxSigner};
use alloy_primitives::Signature;
use async_trait::async_trait;
use crate::Ethereum;
todo: this doesn't work
error[E0210]: type parameter `S` must be covered by another type when it appears before the first local type (`Ethereum`)
--> crates/consensus/src/transaction/mod.rs:32:6
|
32 | impl<S> NetworkSigner<Ethereum> for S
| ^ type parameter `S` must be covered by another type when it appears before the first local type (`Ethereum`)
|
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local, and no uncovered type parameters appear before that first local type
= note: in this case, 'before' refers to the following order: `impl<..> ForeignTrait<T1, ..., Tn> for T0`, where `T0` is the first and `Tn` is the last
a solution would be to move this to alloy-network, but then we'd have a circular dep, since alloy-consensus depends on alloy-network for the tx builder traits.
if we move e.g. `Ethereum` to alloy-network, we'd still have a circular dep. the only way to not have a circular dep is to move more
stuff from alloy-network into alloy-consensus or vice versa
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl<S> NetworkSigner<Ethereum> for S
Expand Down Expand Up @@ -138,3 +154,4 @@ mod test {
assert_eq!(error.to_string(), expected_error.to_string());
}
}
*/
1 change: 1 addition & 0 deletions crates/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ alloy-dyn-abi.workspace = true
alloy-json-abi.workspace = true
alloy-primitives.workspace = true
alloy-sol-types.workspace = true
alloy-network.workspace = true

thiserror.workspace = true

Expand Down
17 changes: 16 additions & 1 deletion crates/network/src/transaction/signer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{BuilderError, Network, Signed, Transaction};
use crate::{Network, Signed, Transaction};
use alloy_primitives::{keccak256, B256};
use alloy_rlp::BufMut;
use async_trait::async_trait;
Expand Down Expand Up @@ -49,17 +49,31 @@ pub trait SignableTransaction<Signature>: Transaction {
Self: Sized;
}

// TODO: Remove in favor of dyn trait upcasting (TBD, see https://github.com/rust-lang/rust/issues/65991#issuecomment-1903120162)
#[doc(hidden)]
impl<S: 'static> dyn SignableTransaction<S> {
pub fn __downcast_ref<T: std::any::Any>(&self) -> Option<&T> {
if std::any::Any::type_id(self) == std::any::TypeId::of::<T>() {
unsafe { Some(&*(self as *const _ as *const T)) }
} else {
None
}
}
}

// todo: move
/// A signer capable of signing any transaction for the given network.
#[async_trait]
pub trait NetworkSigner<N: Network> {
/// Asynchronously sign an unsigned transaction.
async fn sign(&self, tx: N::UnsignedTx) -> alloy_signer::Result<N::TxEnvelope>;
}

// todo: move
/// An async signer capable of signing any [SignableTransaction] for the given [Signature] type.
#[async_trait]
pub trait TxSigner<Signature> {
/// Asynchronously sign an unsigned transaction.
async fn sign_transaction(
&self,
tx: &mut dyn SignableTransaction<Signature>,
Expand All @@ -69,6 +83,7 @@ pub trait TxSigner<Signature> {
// todo: move
/// A sync signer capable of signing any [SignableTransaction] for the given [Signature] type.
pub trait TxSignerSync<Signature> {
/// Synchronously sign an unsigned transaction.
fn sign_transaction_sync(
&self,
tx: &mut dyn SignableTransaction<Signature>,
Expand Down
2 changes: 1 addition & 1 deletion crates/signer-aws/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn sig_from_digest_bytes_trial_recovery(

/// Makes a trial recovery to check whether an RSig corresponds to a known `VerifyingKey`.
fn check_candidate(signature: &Signature, hash: B256, pubkey: &VerifyingKey) -> bool {
signature.recover_from_prehash(&hash).map(|key| key == *pubkey).unwrap_or(false)
signature.recover_from_prehash(hash).map(|key| key == *pubkey).unwrap_or(false)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion crates/signer-gcp/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ fn sig_from_digest_bytes_trial_recovery(

/// Makes a trial recovery to check whether an RSig corresponds to a known `VerifyingKey`.
fn check_candidate(signature: &Signature, hash: &B256, pubkey: &VerifyingKey) -> bool {
signature.recover_from_prehash(hash).map(|key| key == *pubkey).unwrap_or(false)
signature.recover_from_prehash(*hash).map(|key| key == *pubkey).unwrap_or(false)
}

#[cfg(test)]
Expand Down

0 comments on commit 3300b0c

Please sign in to comment.