Files
chatmail-core/src/keyring.rs
2020-03-14 15:17:23 +01:00

47 lines
1.0 KiB
Rust

use std::borrow::Cow;
use crate::constants::KeyType;
use crate::context::Context;
use crate::key::Key;
use crate::sql::Sql;
#[derive(Default, Clone, Debug)]
pub struct Keyring<'a> {
keys: Vec<Cow<'a, Key>>,
}
impl<'a> Keyring<'a> {
pub fn add_owned(&mut self, key: Key) {
self.add(Cow::Owned(key))
}
pub fn add_ref(&mut self, key: &'a Key) {
self.add(Cow::Borrowed(key))
}
fn add(&mut self, key: Cow<'a, Key>) {
self.keys.push(key);
}
pub fn keys(&self) -> &[Cow<'a, Key>] {
&self.keys
}
pub async fn load_self_private_for_decrypting(
&mut self,
context: &Context,
self_addr: impl AsRef<str>,
sql: &Sql,
) -> bool {
sql.query_get_value(
context,
"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;",
&[self_addr.as_ref()],
)
.await
.and_then(|blob: Vec<u8>| Key::from_slice(&blob, KeyType::Private))
.map(|key| self.add_owned(key))
.is_some()
}
}