Skip to main content

m4a_matrix_core/
signer.rs

1use ruma_signatures::{KeyPair, Signature};
2
3/// An ed25519 signer: server name, key id (`ed25519:<version>`) and a signing closure.
4/// The closure signs the exact bytes it is given and returns the 64-byte signature.
5pub struct Signer {
6    server: String,
7    key_id: String,
8    sign: Box<dyn Fn(&[u8]) -> [u8; 64] + Send + Sync>,
9}
10
11impl Signer {
12    pub fn new(server: impl Into<String>, key_id: impl Into<String>, sign: impl Fn(&[u8]) -> [u8; 64] + Send + Sync + 'static) -> Self {
13        Self { server: server.into(), key_id: key_id.into(), sign: Box::new(sign) }
14    }
15    pub fn server(&self) -> &str {
16        &self.server
17    }
18}
19
20impl KeyPair for Signer {
21    fn sign(&self, message: &[u8]) -> Signature {
22        let key_id = self.key_id.as_str().try_into().expect("key id is `ed25519:<version>`");
23        Signature::new(key_id, (self.sign)(message).to_vec())
24    }
25}