sal-modular/evm_client/src/signer.rs
Sameh Abouelsaad 85a15edaec feat: Upgrade dependencies and refactor client
- Upgrade several dependencies to their latest versions.
- Refactor the EVM client for improved modularity and clarity.
- Simplify transaction signing and sending logic.
2025-05-16 03:07:42 +03:00

81 lines
2.6 KiB
Rust

// Signing should be done using ethers-core utilities directly. This file is now empty.
// Native: Only compile for non-WASM
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
pub trait Signer: Send + Sync {
async fn sign(&self, message: &[u8]) -> Result<Vec<u8>, EvmError>;
fn address(&self) -> String;
}
// WASM: Only compile for WASM
#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
pub trait Signer {
async fn sign(&self, message: &[u8]) -> Result<Vec<u8>, EvmError>;
fn address(&self) -> String;
}
// --- Implementation for vault::SessionManager ---
#[cfg(target_arch = "wasm32")]
#[async_trait::async_trait(?Send)]
impl<S: vault::KVStore> Signer for vault::SessionManager<S> {
async fn sign(&self, message: &[u8]) -> Result<Vec<u8>, EvmError> {
log::debug!("SessionManager::sign called");
self.sign(message)
.await
.map_err(|e| {
log::error!("Vault signing error: {}", e);
EvmError::Vault(e.to_string())
})
}
fn address(&self) -> String {
log::debug!("SessionManager::address called");
self.current_keypair()
.map(|k| {
if k.key_type == vault::KeyType::Secp256k1 {
let pubkey = &k.public_key;
use alloy_primitives::keccak256;
let hash = keccak256(&pubkey[1..]);
format!("0x{}", hex::encode(&hash[12..]))
} else {
format!("0x{}", hex::encode(&k.public_key))
}
})
.unwrap_or_default()
}
}
#[cfg(not(target_arch = "wasm32"))]
#[async_trait::async_trait]
impl<S: vault::KVStore + Send + Sync> Signer for vault::SessionManager<S> {
async fn sign(&self, message: &[u8]) -> Result<Vec<u8>, EvmError> {
log::debug!("SessionManager::sign called");
self.sign(message)
.await
.map_err(|e| {
log::error!("Vault signing error: {}", e);
EvmError::Vault(e.to_string())
})
}
fn address(&self) -> String {
log::debug!("SessionManager::address called");
self.current_keypair()
.map(|k| {
if k.key_type == vault::KeyType::Secp256k1 {
let pubkey = &k.public_key;
use alloy_primitives::keccak256;
let hash = keccak256(&pubkey[1..]);
format!("0x{}", hex::encode(&hash[12..]))
} else {
format!("0x{}", hex::encode(&k.public_key))
}
})
.unwrap_or_default()
}
}