use crate::{
error::CryptError,
private::{PrivateNode, TemporalKey},
};
use anyhow::Result;
use async_stream::stream;
use futures::Future;
use std::collections::BTreeSet;
use wnfs_common::{
BlockStore, Cid, HashOutput,
utils::{BoxStream, CondSend, CondSync},
};
use wnfs_hamt::Pair;
use wnfs_nameaccumulator::{AccumulatorSetup, ElementsProof, Name, NameAccumulator};
pub trait PrivateForest: CondSync {
fn empty_name(&self) -> Name {
Name::empty(self.get_accumulator_setup())
}
fn get_accumulator_setup(&self) -> &AccumulatorSetup;
fn get_proven_name(&self, name: &Name) -> (NameAccumulator, ElementsProof);
fn get_accumulated_name(&self, name: &Name) -> NameAccumulator {
self.get_proven_name(name).0
}
fn has_by_hash(
&self,
name_hash: &HashOutput,
store: &impl BlockStore,
) -> impl Future<Output = Result<bool>> + CondSend;
fn has(
&self,
name: &Name,
store: &impl BlockStore,
) -> impl Future<Output = Result<bool>> + CondSend;
fn put_encrypted<I>(
&mut self,
name: &Name,
values: I,
store: &impl BlockStore,
) -> impl Future<Output = Result<NameAccumulator>> + CondSend
where
I: IntoIterator<Item = Cid> + CondSend,
I::IntoIter: CondSend;
fn get_encrypted_by_hash<'b>(
&'b self,
name_hash: &HashOutput,
store: &impl BlockStore,
) -> impl Future<Output = Result<Option<&'b BTreeSet<Cid>>>> + CondSend;
fn get_encrypted(
&self,
name: &Name,
store: &impl BlockStore,
) -> impl Future<Output = Result<Option<&BTreeSet<Cid>>>> + CondSend;
fn remove_encrypted(
&mut self,
name: &Name,
store: &impl BlockStore,
) -> impl Future<Output = Result<Option<Pair<NameAccumulator, BTreeSet<Cid>>>>> + CondSend;
fn get_multivalue_by_hash<'a>(
&'a self,
label: &'a HashOutput,
temporal_key: &'a TemporalKey,
store: &'a impl BlockStore,
parent_name: Option<Name>,
) -> BoxStream<'a, Result<(Cid, PrivateNode)>>
where
Self: Sized,
{
Box::pin(stream! {
match self
.get_encrypted_by_hash(label, store)
.await
{
Ok(Some(cids)) => {
for cid in cids {
match PrivateNode::from_cid(*cid, temporal_key, self, store, parent_name.clone()).await {
Ok(node) => yield Ok((*cid, node)),
Err(e) if e.downcast_ref::<CryptError>().is_some() => {
}
Err(e) => yield Err(e)
}
}
}
Ok(None) => {},
Err(e) => yield Err(e),
}
})
}
}