rig_onchain_kit/signer/
mod.rs

1#[cfg(feature = "evm")]
2pub mod evm;
3#[cfg(feature = "http")]
4pub mod privy;
5#[cfg(feature = "solana")]
6pub mod solana;
7
8use std::future::Future;
9use std::sync::Arc;
10
11use anyhow::Result;
12use async_trait::async_trait;
13
14#[cfg(feature = "evm")]
15use self::evm::LocalEvmSigner;
16#[cfg(feature = "http")]
17use self::privy::PrivySigner;
18#[cfg(feature = "solana")]
19use self::solana::LocalSolanaSigner;
20
21pub enum Transaction {
22    #[cfg(feature = "solana")]
23    Solana(solana_sdk::transaction::Transaction),
24    #[cfg(feature = "evm")]
25    Evm(),
26}
27
28pub enum SignerType {
29    #[cfg(feature = "solana")]
30    LocalSolana(LocalSolanaSigner),
31    #[cfg(feature = "evm")]
32    LocalEvm(LocalEvmSigner),
33    #[cfg(feature = "http")]
34    Privy(PrivySigner),
35}
36
37#[async_trait]
38pub trait TransactionSigner: Send + Sync {
39    fn address(&self) -> String {
40        unimplemented!()
41    }
42
43    fn pubkey(&self) -> String {
44        unimplemented!()
45    }
46
47    #[cfg(feature = "solana")]
48    async fn sign_and_send_solana_transaction(
49        &self,
50        _tx: &mut solana_sdk::transaction::Transaction,
51    ) -> Result<String> {
52        Err(anyhow::anyhow!(
53            "Solana transactions not supported by this signer"
54        ))
55    }
56
57    #[cfg(feature = "evm")]
58    async fn sign_and_send_evm_transaction(
59        &self,
60        _tx: alloy::rpc::types::TransactionRequest,
61    ) -> Result<String> {
62        Err(anyhow::anyhow!(
63            "EVM transactions not supported by this signer"
64        ))
65    }
66
67    async fn sign_and_send_encoded_solana_transaction(
68        &self,
69        _tx: String,
70    ) -> Result<String> {
71        Err(anyhow::anyhow!(
72            "Solana transactions not supported by this signer"
73        ))
74    }
75
76    async fn sign_and_send_json_evm_transaction(
77        &self,
78        _tx: serde_json::Value,
79    ) -> Result<String> {
80        Err(anyhow::anyhow!(
81            "EVM transactions not supported by this signer"
82        ))
83    }
84}
85
86tokio::task_local! {
87    static CURRENT_SIGNER: Arc<dyn TransactionSigner>;
88}
89
90pub struct SignerContext;
91
92impl SignerContext {
93    pub async fn with_signer<T>(
94        signer: Arc<dyn TransactionSigner>,
95        f: impl Future<Output = Result<T>> + Send,
96    ) -> Result<T> {
97        CURRENT_SIGNER.scope(signer, f).await
98    }
99
100    pub async fn current() -> Arc<dyn TransactionSigner> {
101        CURRENT_SIGNER.get().clone()
102    }
103}