signatory/key/
handle.rs

1//! Handle to a particular key.
2
3#[cfg(feature = "ecdsa")]
4#[allow(unused_imports)]
5use crate::ecdsa;
6
7#[cfg(feature = "ed25519")]
8use crate::ed25519;
9
10/// Handle to a particular key.
11///
12/// Uniquely identifies a particular key in the keyring.
13#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
14#[non_exhaustive]
15pub enum KeyHandle {
16    /// ECDSA/P-256.
17    #[cfg(feature = "nistp256")]
18    EcdsaNistP256(ecdsa::nistp256::VerifyingKey),
19
20    /// ECDSA/P-384.
21    #[cfg(feature = "nistp384")]
22    EcdsaNistP384(ecdsa::nistp384::VerifyingKey),
23
24    /// ECDSA/secp256k1.
25    #[cfg(feature = "secp256k1")]
26    EcdsaSecp256k1(ecdsa::secp256k1::VerifyingKey),
27
28    /// Ed25519.
29    #[cfg(feature = "ed25519")]
30    Ed25519(ed25519::VerifyingKey),
31}
32
33impl KeyHandle {
34    /// Get ECDSA/P-256 verifying key, if this is an ECDSA/P-256 key.
35    #[cfg(feature = "nistp256")]
36    pub fn ecdsa_nistp256(&self) -> Option<ecdsa::nistp256::VerifyingKey> {
37        match self {
38            KeyHandle::EcdsaNistP256(pk) => Some(*pk),
39            #[allow(unreachable_patterns)]
40            _ => None,
41        }
42    }
43
44    /// Get ECDSA/P-384 verifying key, if this is an ECDSA/P-384 key.
45    #[cfg(feature = "nistp384")]
46    pub fn ecdsa_nistp384(&self) -> Option<ecdsa::nistp384::VerifyingKey> {
47        match self {
48            KeyHandle::EcdsaNistP384(pk) => Some(*pk),
49            #[allow(unreachable_patterns)]
50            _ => None,
51        }
52    }
53
54    /// Get ECDSA/secp256k1 verifying key, if this is an ECDSA/secp256k1 key.
55    #[cfg(feature = "secp256k1")]
56    pub fn ecdsa_secp256k1(&self) -> Option<ecdsa::secp256k1::VerifyingKey> {
57        match self {
58            KeyHandle::EcdsaSecp256k1(pk) => Some(*pk),
59            #[allow(unreachable_patterns)]
60            _ => None,
61        }
62    }
63
64    /// Get Ed25519 verifying key, if this is an Ed25519 key.
65    #[cfg(feature = "ed25519")]
66    pub fn ed25519(&self) -> Option<ed25519::VerifyingKey> {
67        match self {
68            KeyHandle::Ed25519(pk) => Some(*pk),
69            #[allow(unreachable_patterns)]
70            _ => None,
71        }
72    }
73}