pub(in crate::protocol) mod discovery;
pub(in crate::protocol) mod recovery;
pub(in crate::protocol) mod sharing;
pub(in crate::protocol) mod unpairing;
use crate::protocol::context::Local;
use crate::protocol::stores::{StoreSet, Stores};
use crate::protocol::types::{ChannelRecord, ChannelStatus, ReplicaMember};
use crate::protocol::{
DeRecChannelStore, DeRecSecretStore, MissingPolicy, SecretKind, SecretValue,
};
use crate::types::{ChannelId, SharedKey};
use crate::{Error, Result};
use std::collections::HashMap;
async fn load_channel_key<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
channel_id: ChannelId,
) -> Result<SharedKey> {
match stores
.secrets
.load(local.secret_id, channel_id, SecretKind::SharedKey)
.await?
{
Some(SecretValue::SharedKey(key)) => Ok(key),
_ => Err(Error::InvalidInput(
"channel has no shared key — not yet paired",
)),
}
}
async fn load_member_keys<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
members: &[ReplicaMember],
) -> Result<HashMap<ChannelId, SharedKey>> {
let mut channel_ids: Vec<ChannelId> = Vec::new();
for member in members {
if !channel_ids.contains(&member.channel_id) {
channel_ids.push(member.channel_id);
}
}
Ok(stores
.secrets
.load_many(
local.secret_id,
&channel_ids,
SecretKind::SharedKey,
MissingPolicy::Fail,
)
.await?
.into_iter()
.filter_map(|(channel_id, value)| match value {
SecretValue::SharedKey(key) => Some((channel_id, key)),
_ => None,
})
.collect())
}
async fn flag_unpairing<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
member: &ReplicaMember,
) -> Result<()> {
stores
.channels
.save(
local.secret_id,
ChannelRecord::Replica(ReplicaMember {
status: ChannelStatus::Unpairing,
..member.clone()
}),
)
.await?;
Ok(())
}