mirror of
https://github.com/chatmail/core.git
synced 2026-04-24 17:06:28 +03:00
fix: avoid blocking on expensive pgp operations
This commit is contained in:
@@ -1,46 +1,47 @@
|
||||
use std::borrow::Cow;
|
||||
use anyhow::Result;
|
||||
|
||||
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>>,
|
||||
pub struct Keyring {
|
||||
keys: Vec<Key>,
|
||||
}
|
||||
|
||||
impl<'a> Keyring<'a> {
|
||||
pub fn add_owned(&mut self, key: Key) {
|
||||
self.add(Cow::Owned(key))
|
||||
impl Keyring {
|
||||
pub fn add(&mut self, key: Key) {
|
||||
self.keys.push(key)
|
||||
}
|
||||
|
||||
pub fn add_ref(&mut self, key: &'a Key) {
|
||||
self.add(Cow::Borrowed(key))
|
||||
pub fn len(&self) -> usize {
|
||||
self.keys.len()
|
||||
}
|
||||
|
||||
fn add(&mut self, key: Cow<'a, Key>) {
|
||||
self.keys.push(key);
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.keys.is_empty()
|
||||
}
|
||||
|
||||
pub fn keys(&self) -> &[Cow<'a, Key>] {
|
||||
pub fn keys(&self) -> &[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;",
|
||||
paramsv![self_addr.as_ref().to_string()],
|
||||
)
|
||||
.await
|
||||
.and_then(|blob: Vec<u8>| Key::from_slice(&blob, KeyType::Private))
|
||||
.map(|key| self.add_owned(key))
|
||||
.is_some()
|
||||
) -> Result<Self> {
|
||||
let blob: Vec<u8> = context
|
||||
.sql
|
||||
.query_get_value_result(
|
||||
"SELECT private_key FROM keypairs ORDER BY addr=? DESC, is_default DESC;",
|
||||
paramsv![self_addr.as_ref().to_string()],
|
||||
)
|
||||
.await?
|
||||
.unwrap_or_default();
|
||||
|
||||
let key = async_std::task::spawn_blocking(move || Key::from_slice(&blob, KeyType::Private))
|
||||
.await?;
|
||||
|
||||
Ok(Self { keys: vec![key] })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user