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
67
68
69
use crate::{Error, Keystore, Message, PublicKey, SecretKey, Signature};
use borrown::Borrown;
pub trait Signer {
type Error: From<Error> + From<<Self::Keystore as Keystore>::Error>;
type Keystore: Keystore;
fn keystore(&self) -> Result<&Self::Keystore, Self::Error>;
fn id_secret(
&self,
id: &<Self::Keystore as Keystore>::KeyId,
) -> Result<Borrown<'_, SecretKey>, Self::Error> {
let keystore = self.keystore()?;
let secret = keystore.secret(id)?.ok_or(Error::KeyNotFound)?;
Ok(secret)
}
fn id_public(
&self,
id: &<Self::Keystore as Keystore>::KeyId,
) -> Result<Borrown<'_, PublicKey>, Self::Error> {
let keystore = self.keystore()?;
let public = keystore.public(id)?.ok_or(Error::KeyNotFound)?;
Ok(public)
}
fn sign(
&self,
id: &<Self::Keystore as Keystore>::KeyId,
message: &Message,
) -> Result<Signature, Self::Error> {
let secret = self.id_secret(id)?;
self.sign_with_key(secret.as_ref(), message)
}
#[cfg(not(feature = "std"))]
fn sign_with_key(
&self,
secret: &SecretKey,
message: &Message,
) -> Result<Signature, Self::Error>;
#[cfg(feature = "std")]
fn sign_with_key(
&self,
secret: &SecretKey,
message: &Message,
) -> Result<Signature, Self::Error> {
Ok(Signature::sign(secret, message))
}
}