solders_keypair/
signer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use pyo3::prelude::*;

use crate::{null_signer::NullSigner, presigner::Presigner, Keypair};
use solana_sdk::{
    pubkey::Pubkey as PubkeyOriginal,
    signature::Signature as SignatureOriginal,
    signer::{signers::Signers, Signer as SignerTrait, SignerError as SignerErrorOriginal},
};
use solders_traits::{SignerTraitWrapper, ToSignerOriginal};

#[derive(FromPyObject, Debug)]
pub enum Signer {
    KeypairWrapper(Keypair),
    PresignerWrapper(Presigner),
    NullSignerWrapper(NullSigner),
}

impl ToSignerOriginal for Signer {
    fn to_inner(&self) -> Box<dyn SignerTrait> {
        match self {
            Signer::KeypairWrapper(x) => x.to_inner(),
            Signer::PresignerWrapper(x) => x.to_inner(),
            Signer::NullSignerWrapper(x) => x.to_inner(),
        }
    }
}

impl SignerTraitWrapper for Signer {}

pub struct SignerVec(pub Vec<Signer>);

impl Signers for SignerVec {
    fn pubkeys(&self) -> Vec<PubkeyOriginal> {
        self.0.iter().map(|keypair| keypair.pubkey()).collect()
    }

    fn try_pubkeys(&self) -> Result<Vec<PubkeyOriginal>, SignerErrorOriginal> {
        let mut pubkeys = Vec::new();
        for keypair in self.0.iter() {
            pubkeys.push(keypair.try_pubkey()?);
        }
        Ok(pubkeys)
    }

    fn sign_message(&self, message: &[u8]) -> Vec<SignatureOriginal> {
        self.0
            .iter()
            .map(|keypair| keypair.sign_message(message))
            .collect()
    }

    fn try_sign_message(
        &self,
        message: &[u8],
    ) -> Result<Vec<SignatureOriginal>, SignerErrorOriginal> {
        let mut signatures = Vec::new();
        for keypair in self.0.iter() {
            signatures.push(keypair.try_sign_message(message)?);
        }
        Ok(signatures)
    }

    fn is_interactive(&self) -> bool {
        self.0.iter().any(|s| s.is_interactive())
    }
}