Skip to content

Commit

Permalink
feat: add TxSigner support for Either (#2036)
Browse files Browse the repository at this point in the history
* feat: add TxSigner support for Either

* Self

* Sig: Send

* crate Either

* pub use, rm redud
  • Loading branch information
stevencartavia authored Feb 11, 2025
1 parent 5aa17d2 commit fea2c00
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ workspace = true

[dependencies]
alloy-primitives = { workspace = true, features = ["std", "k256"] }
either = "1.13.0"

async-trait.workspace = true
auto_impl.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/signer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod error;
pub use error::{Error, Result, UnsupportedSignerOperation};

mod signer;
pub use signer::{Signer, SignerSync};
pub use signer::{Either, Signer, SignerSync};

pub mod utils;

Expand Down
59 changes: 59 additions & 0 deletions crates/signer/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use alloy_primitives::{
};
use async_trait::async_trait;
use auto_impl::auto_impl;
pub use either::Either;

#[cfg(feature = "eip712")]
use alloy_dyn_abi::eip712::TypedData;
Expand Down Expand Up @@ -134,6 +135,64 @@ pub trait SignerSync<Sig = Signature> {
fn chain_id_sync(&self) -> Option<ChainId>;
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[async_trait]
impl<A, B, Sig> Signer<Sig> for Either<A, B>
where
A: Signer<Sig> + Send + Sync,
B: Signer<Sig> + Send + Sync,
Sig: Send,
{
async fn sign_hash(&self, hash: &B256) -> Result<Sig> {
match self {
Self::Left(signer) => signer.sign_hash(hash).await,
Self::Right(signer) => signer.sign_hash(hash).await,
}
}

fn address(&self) -> Address {
match self {
Self::Left(signer) => signer.address(),
Self::Right(signer) => signer.address(),
}
}

fn chain_id(&self) -> Option<ChainId> {
match self {
Self::Left(signer) => signer.chain_id(),
Self::Right(signer) => signer.chain_id(),
}
}

fn set_chain_id(&mut self, chain_id: Option<ChainId>) {
match self {
Self::Left(signer) => signer.set_chain_id(chain_id),
Self::Right(signer) => signer.set_chain_id(chain_id),
}
}
}

impl<A, B, Sig> SignerSync<Sig> for Either<A, B>
where
A: SignerSync<Sig>,
B: SignerSync<Sig>,
{
fn sign_hash_sync(&self, hash: &B256) -> Result<Sig> {
match self {
Self::Left(signer) => signer.sign_hash_sync(hash),
Self::Right(signer) => signer.sign_hash_sync(hash),
}
}

fn chain_id_sync(&self) -> Option<ChainId> {
match self {
Self::Left(signer) => signer.chain_id_sync(),
Self::Right(signer) => signer.chain_id_sync(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit fea2c00

Please sign in to comment.