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
use crate::{
    Error,
    PublicKey,
    SecretKey,
};

use borrown::Borrown;

/// Keys container
pub trait Keystore {
    /// Keystore error implementation
    type Error: From<Error>;

    /// Identifier for the keypair
    type KeyId;

    /// Secret key for a given id
    fn secret(
        &self,
        id: &Self::KeyId,
    ) -> Result<Option<Borrown<'_, SecretKey>>, Self::Error>;

    /// Public key for a given id
    #[cfg(not(feature = "std"))]
    fn public(
        &self,
        id: &Self::KeyId,
    ) -> Result<Option<Borrown<'_, PublicKey>>, Self::Error>;

    /// Public key for a given id
    #[cfg(feature = "std")]
    fn public(
        &self,
        id: &Self::KeyId,
    ) -> Result<Option<Borrown<'_, PublicKey>>, Self::Error> {
        let secret = self.secret(id)?;
        let public = secret
            .map(|s| PublicKey::from(s.as_ref()))
            .map(Borrown::Owned);

        Ok(public)
    }
}