Skip to content

Commit fea2c00

Browse files
feat: add TxSigner support for Either (#2036)
* feat: add TxSigner support for Either * Self * Sig: Send * crate Either * pub use, rm redud
1 parent 5aa17d2 commit fea2c00

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

crates/signer/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ workspace = true
2424

2525
[dependencies]
2626
alloy-primitives = { workspace = true, features = ["std", "k256"] }
27+
either = "1.13.0"
2728

2829
async-trait.workspace = true
2930
auto_impl.workspace = true

crates/signer/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod error;
1010
pub use error::{Error, Result, UnsupportedSignerOperation};
1111

1212
mod signer;
13-
pub use signer::{Signer, SignerSync};
13+
pub use signer::{Either, Signer, SignerSync};
1414

1515
pub mod utils;
1616

crates/signer/src/signer.rs

+59
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloy_primitives::{
44
};
55
use async_trait::async_trait;
66
use auto_impl::auto_impl;
7+
pub use either::Either;
78

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

138+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
139+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
140+
#[async_trait]
141+
impl<A, B, Sig> Signer<Sig> for Either<A, B>
142+
where
143+
A: Signer<Sig> + Send + Sync,
144+
B: Signer<Sig> + Send + Sync,
145+
Sig: Send,
146+
{
147+
async fn sign_hash(&self, hash: &B256) -> Result<Sig> {
148+
match self {
149+
Self::Left(signer) => signer.sign_hash(hash).await,
150+
Self::Right(signer) => signer.sign_hash(hash).await,
151+
}
152+
}
153+
154+
fn address(&self) -> Address {
155+
match self {
156+
Self::Left(signer) => signer.address(),
157+
Self::Right(signer) => signer.address(),
158+
}
159+
}
160+
161+
fn chain_id(&self) -> Option<ChainId> {
162+
match self {
163+
Self::Left(signer) => signer.chain_id(),
164+
Self::Right(signer) => signer.chain_id(),
165+
}
166+
}
167+
168+
fn set_chain_id(&mut self, chain_id: Option<ChainId>) {
169+
match self {
170+
Self::Left(signer) => signer.set_chain_id(chain_id),
171+
Self::Right(signer) => signer.set_chain_id(chain_id),
172+
}
173+
}
174+
}
175+
176+
impl<A, B, Sig> SignerSync<Sig> for Either<A, B>
177+
where
178+
A: SignerSync<Sig>,
179+
B: SignerSync<Sig>,
180+
{
181+
fn sign_hash_sync(&self, hash: &B256) -> Result<Sig> {
182+
match self {
183+
Self::Left(signer) => signer.sign_hash_sync(hash),
184+
Self::Right(signer) => signer.sign_hash_sync(hash),
185+
}
186+
}
187+
188+
fn chain_id_sync(&self) -> Option<ChainId> {
189+
match self {
190+
Self::Left(signer) => signer.chain_id_sync(),
191+
Self::Right(signer) => signer.chain_id_sync(),
192+
}
193+
}
194+
}
195+
137196
#[cfg(test)]
138197
mod tests {
139198
use super::*;

0 commit comments

Comments
 (0)