Skip to main content

hyper_exchange/
adapter.rs

1//! Adapter that bridges [`crate::signer::Signer`] to [`motosan_wallet_core::HlSigner`].
2//!
3//! [`SingleAddressSigner`] wraps a hyper-exchange `Signer` and a fixed Ethereum
4//! address so it can be passed to motosan-wallet-core signing functions that
5//! expect the `HlSigner` trait.
6
7use crate::signer::Signer;
8use motosan_wallet_core::{HlSigner, WalletError};
9
10/// Bridges a hyper-exchange [`Signer`] (multi-address, recovery-id v=0|1) to
11/// motosan-wallet-core's [`HlSigner`] (single-address, Ethereum v=27|28).
12pub struct SingleAddressSigner<'a, S: Signer + ?Sized> {
13    signer: &'a S,
14    address: String,
15}
16
17impl<'a, S: Signer + ?Sized> SingleAddressSigner<'a, S> {
18    pub fn new(signer: &'a S, address: String) -> Self {
19        Self { signer, address }
20    }
21}
22
23impl<S: Signer + ?Sized> HlSigner for SingleAddressSigner<'_, S> {
24    fn address(&self) -> Result<String, WalletError> {
25        Ok(self.address.clone())
26    }
27
28    fn sign_prehash(&self, hash: &[u8; 32]) -> Result<[u8; 65], WalletError> {
29        let mut sig = self
30            .signer
31            .sign_hash(&self.address, hash)
32            .map_err(|e| WalletError::SigningError(e.to_string()))?;
33        // Signer returns recovery id (0 or 1); HlSigner expects Ethereum v (27 or 28).
34        sig[64] += 27;
35        Ok(sig)
36    }
37}