use std::future::Future;
use std::sync::Arc;
use alloy_consensus::TxEip1559;
use alloy_primitives::{Address, FixedBytes, Signature};
use alloy_signer_local::PrivateKeySigner;
pub trait SignerLike: Send + Sync {
fn address(&self) -> Address;
fn sign_hash(
&self,
hash: &FixedBytes<32>,
) -> impl Future<Output = Result<Signature, alloy_signer::Error>> + Send;
fn signs_eip1559(&self) -> bool {
false
}
fn sign_eip1559(
&self,
tx: &mut TxEip1559,
) -> impl Future<Output = Result<Option<Signature>, alloy_signer::Error>> + Send {
async {
let _ = tx;
Ok(None)
}
}
}
impl SignerLike for PrivateKeySigner {
fn address(&self) -> Address {
Self::address(self)
}
async fn sign_hash(&self, hash: &FixedBytes<32>) -> Result<Signature, alloy_signer::Error> {
alloy_signer::Signer::sign_hash(self, hash).await
}
fn signs_eip1559(&self) -> bool {
true
}
async fn sign_eip1559(
&self,
tx: &mut TxEip1559,
) -> Result<Option<Signature>, alloy_signer::Error> {
let sig = alloy_network::TxSigner::sign_transaction(self, tx).await?;
Ok(Some(sig))
}
}
impl<T: SignerLike + Send + Sync> SignerLike for Arc<T> {
fn address(&self) -> Address {
(**self).address()
}
async fn sign_hash(&self, hash: &FixedBytes<32>) -> Result<Signature, alloy_signer::Error> {
(**self).sign_hash(hash).await
}
fn signs_eip1559(&self) -> bool {
(**self).signs_eip1559()
}
async fn sign_eip1559(
&self,
tx: &mut TxEip1559,
) -> Result<Option<Signature>, alloy_signer::Error> {
(**self).sign_eip1559(tx).await
}
}