eigensdk_signerv2/
lib.rs

1//! Signer utilities for Eigen SDK
2//! TODO: Alloy migration when alloy features are complete.
3
4use crate::error::SignerV2Error;
5use eth_keystore::decrypt_key;
6use ethers_core::types::{transaction::eip2718::TypedTransaction,Signature};
7use ethers_signers::{Wallet,Signer};
8
9pub mod error;
10
11pub trait SignerV2 {
12    async fn sign_transaction(&self, tx: &TypedTransaction) -> Result<Signature, SignerV2Error>;
13}
14
15#[derive(Clone, Debug)]
16pub struct KeyStoreSigner {
17    path: String,
18    password: String,
19}
20
21#[derive(Debug, Clone)]
22pub struct PrivateKeySigner {
23    private_key: Vec<u8>,
24}
25
26impl PrivateKeySigner {
27    async fn set_private_key(&mut self, pvt_key: Vec<u8>) {
28        self.private_key = pvt_key;
29    }
30}
31
32impl SignerV2 for PrivateKeySigner {
33    async fn sign_transaction(&self, tx: &TypedTransaction) -> Result<Signature, SignerV2Error> {
34        let wallet_result = Wallet::from_bytes(&self.private_key);
35
36        match wallet_result {
37            Ok(wallet) => {
38                let signer_tx_result = wallet.sign_transaction(tx).await;
39
40                match signer_tx_result {
41                    Ok(signer_tx) => {
42                        return Ok(signer_tx);
43                    }
44                    Err(_) => return Err(SignerV2Error::SignTransaction),
45                }
46            }
47            Err(_) => return Err(SignerV2Error::BuildWallet),
48        }
49    }
50}
51
52impl SignerV2 for KeyStoreSigner {
53    async fn sign_transaction(&self, tx: &TypedTransaction) -> Result<Signature, SignerV2Error> {
54        let private_key = decrypt_key(&self.path, &self.password).unwrap();
55
56        let wallet_result = Wallet::from_bytes(&private_key);
57
58        match wallet_result {
59            Ok(wallet) => {
60                let signer_tx_result = wallet.sign_transaction(tx).await;
61
62                match signer_tx_result {
63                    Ok(signer_tx) => {
64                        return Ok(signer_tx);
65                    }
66                    Err(_) => return Err(SignerV2Error::SignTransaction),
67                }
68            }
69            Err(_) => return Err(SignerV2Error::BuildWallet),
70        }
71    }
72}
73
74pub struct Config {
75    private_key: Vec<u8>,
76    keystore_path: String,
77    password: String,
78}
79
80impl Config {
81    fn is_private_key_signer(&self) -> bool {
82        !self.private_key.is_empty()
83    }
84
85    fn is_local_key_store_signer(&self) -> bool {
86        !self.keystore_path.is_empty()
87    }
88}
89
90pub struct SignerFromCOonfig {
91    c: Config,
92}
93
94impl SignerV2 for SignerFromCOonfig {
95    async fn sign_transaction(&self, tx: &TypedTransaction) -> Result<Signature, SignerV2Error> {
96        if self.c.is_local_key_store_signer() {
97            let private_key_result = decrypt_key(&self.c.keystore_path, &self.c.password);
98
99            match private_key_result {
100                Ok(private_key) => {
101                    let wallet_result = Wallet::from_bytes(&private_key);
102
103                    match wallet_result {
104                        Ok(wallet) => {
105                            let signer_tx_result = wallet.sign_transaction(tx).await;
106
107                            match signer_tx_result {
108                                Ok(signer_tx) => {
109                                    return Ok(signer_tx);
110                                }
111                                Err(_) => return Err(SignerV2Error::SignTransaction),
112                            }
113                        }
114                        Err(_) => return Err(SignerV2Error::BuildWallet),
115                    }
116                }
117                Err(_) => return Err(SignerV2Error::Decryptkey),
118            }
119        } else if self.c.is_private_key_signer() {
120            let private_key_result = decrypt_key(&self.c.keystore_path, &self.c.password);
121
122            match private_key_result {
123                Ok(private_key) => {
124                    let wallet_result = Wallet::from_bytes(&private_key);
125
126                    match wallet_result {
127                        Ok(wallet) => {
128                            let signer_tx_result = wallet.sign_transaction(tx).await;
129
130                            match signer_tx_result {
131                                Ok(signer_tx) => {
132                                    return Ok(signer_tx);
133                                }
134                                Err(_) => return Err(SignerV2Error::SignTransaction),
135                            }
136                        }
137                        Err(_) => return Err(SignerV2Error::BuildWallet),
138                    }
139                }
140                Err(_) => return Err(SignerV2Error::BuildWallet),
141            }
142        } else {
143            return Err(SignerV2Error::ConfigNotFound);
144        }
145    }
146}