Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test delegation store #12

Merged
merged 4 commits into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/delegation/agent.rs
Expand Up @@ -20,29 +20,34 @@ use web_time::SystemTime;
/// This is helpful for sessions where more than one delegation will be made.
#[derive(Debug)]
pub struct Agent<
S: Store<DID, V, Enc>,
DID: Did = did::preset::Verifier,
V: varsig::Header<Enc> + Clone = varsig::header::Preset,
Enc: Codec + Into<u64> + TryFrom<u64> = varsig::encoding::Preset,
> {
S: Store<DID, V, C>,
DID: Did + Clone = did::preset::Verifier,
V: varsig::Header<C> + Clone = varsig::header::Preset,
C: Codec + Into<u64> + TryFrom<u64> = varsig::encoding::Preset,
> where
Delegation<DID, V, C>: Encode<C>,
Payload<DID>: TryFrom<Named<Ipld>>,
Named<Ipld>: From<Payload<DID>>,
expede marked this conversation as resolved.
Show resolved Hide resolved
{
/// The [`Did`][Did] of the agent.
pub did: DID,

/// The attached [`deleagtion::Store`][super::store::Store].
pub store: S,

signer: <DID as Did>::Signer,
_marker: PhantomData<(V, Enc)>,
_marker: PhantomData<(V, C)>,
}

impl<
S: Store<DID, V, Enc> + Clone,
S: Store<DID, V, C> + Clone,
DID: Did + Clone,
V: varsig::Header<Enc> + Clone,
Enc: Codec + TryFrom<u64> + Into<u64>,
> Agent<S, DID, V, Enc>
V: varsig::Header<C> + Clone,
C: Codec + TryFrom<u64> + Into<u64>,
> Agent<S, DID, V, C>
where
Ipld: Encode<Enc>,
Ipld: Encode<C>,
Delegation<DID, V, C>: Encode<C>,
Payload<DID>: TryFrom<Named<Ipld>>,
Named<Ipld>: From<Payload<DID>>,
{
Expand All @@ -67,7 +72,7 @@ where
not_before: Option<Timestamp>,
now: SystemTime,
varsig_header: V,
) -> Result<Delegation<DID, V, Enc>, DelegateError<S::DelegationStoreError>> {
) -> Result<Delegation<DID, V, C>, DelegateError<S::DelegationStoreError>> {
let mut salt = self.did.clone().to_string().into_bytes();
let nonce = Nonce::generate_12(&mut salt);

Expand Down Expand Up @@ -121,7 +126,7 @@ where
pub fn receive(
&self,
cid: Cid, // FIXME remove and generate from the capsule header?
delegation: Delegation<DID, V, Enc>,
delegation: Delegation<DID, V, C>,
) -> Result<(), ReceiveError<S::DelegationStoreError, DID>> {
if self.store.get(&cid).is_ok() {
return Ok(());
Expand All @@ -135,7 +140,7 @@ where
.validate_signature()
.map_err(|_| ReceiveError::InvalidSignature(cid))?;

self.store.insert(cid, delegation).map_err(Into::into)
self.store.insert_keyed(cid, delegation).map_err(Into::into)
}
}

Expand Down
32 changes: 14 additions & 18 deletions src/delegation/policy/predicate.rs
Expand Up @@ -79,24 +79,20 @@ impl Predicate {
Predicate::Not(inner) => !inner.run(data)?,
Predicate::And(lhs, rhs) => lhs.run(data)? && rhs.run(data)?,
Predicate::Or(lhs, rhs) => lhs.run(data)? || rhs.run(data)?,
Predicate::Every(xs, p) => {
xs.get(data)?
.to_vec()
.iter()
.try_fold(true, |acc, each_datum| {
dbg!("every", &p, acc, each_datum);
Ok(acc && p.clone().run(&each_datum.0)?)
})?
}
Predicate::Some(xs, p) => {
xs.get(data)?
.to_vec()
.iter()
.try_fold(false, |acc, each_datum| {
dbg!("some", &p, acc, each_datum);
Ok(acc || p.clone().run(&each_datum.0)?)
})?
}
Predicate::Every(xs, p) => xs
.get(data)?
.to_vec()
.iter()
.try_fold(true, |acc, each_datum| {
Ok(acc && p.clone().run(&each_datum.0)?)
})?,
Predicate::Some(xs, p) => xs
.get(data)?
.to_vec()
.iter()
.try_fold(false, |acc, each_datum| {
Ok(acc || p.clone().run(&each_datum.0)?)
})?,
})
}

Expand Down