solana_keychain/traits.rs
1//! Core trait definitions for Solana signers
2
3use async_trait::async_trait;
4
5use crate::error::SignerError;
6use crate::sdk_adapter::{Pubkey, Signature, Transaction};
7
8pub type SignedTransaction = (String, Signature);
9#[derive(Debug)]
10pub enum SignTransactionResult {
11 Complete(SignedTransaction),
12 Partial(SignedTransaction),
13}
14
15impl SignTransactionResult {
16 pub fn into_signed_transaction(self) -> SignedTransaction {
17 match self {
18 Self::Complete(tx) | Self::Partial(tx) => tx,
19 }
20 }
21}
22
23/// Trait for signing Solana transactions
24///
25/// All signer implementations must implement this trait to provide
26/// a unified interface for transaction signing.
27#[async_trait]
28pub trait SolanaSigner: Send + Sync {
29 /// Get the public key of this signer
30 fn pubkey(&self) -> Pubkey;
31
32 /// Sign a Solana transaction
33 ///
34 /// # Arguments
35 ///
36 /// * `tx` - The transaction to sign (will be modified in place)
37 ///
38 /// # Returns
39 ///
40 /// The encoded transaction/signature tuple, explicitly marked as complete or partial.
41 async fn sign_transaction(
42 &self,
43 tx: &mut Transaction,
44 ) -> Result<SignTransactionResult, SignerError>;
45
46 /// Sign an arbitrary message
47 ///
48 /// # Arguments
49 ///
50 /// * `message` - The message bytes to sign
51 ///
52 /// # Returns
53 ///
54 /// The signature produced by signing the message
55 async fn sign_message(&self, message: &[u8]) -> Result<Signature, SignerError>;
56
57 /// Check if the signer is available and healthy
58 ///
59 /// # Returns
60 ///
61 /// `true` if the signer can be used, `false` otherwise
62 async fn is_available(&self) -> bool;
63}