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

refactor: Make Agent take DID by value #10

Merged
merged 3 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 13 additions & 14 deletions src/delegation/agent.rs
@@ -1,5 +1,6 @@
use super::{payload::Payload, policy::Predicate, store::Store, Delegation};
use crate::ability::arguments::Named;
use crate::did;
use crate::{
crypto::{signature::Envelope, varsig, Nonce},
did::Did,
Expand All @@ -19,35 +20,33 @@ use web_time::SystemTime;
/// This is helpful for sessions where more than one delegation will be made.
#[derive(Debug)]
pub struct Agent<
'a,
DID: Did,
S: Store<DID, V, Enc>,
V: varsig::Header<Enc>,
Enc: Codec + TryFrom<u64> + Into<u64>,
DID: Did = did::preset::Verifier,
V: varsig::Header<Enc> + Clone = varsig::header::Preset,
Enc: Codec + Into<u64> + TryFrom<u64> = varsig::encoding::Preset,
> {
/// The [`Did`][Did] of the agent.
pub did: &'a DID,
pub did: DID,

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

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

impl<
'a,
DID: Did + Clone,
S: Store<DID, V, Enc> + Clone,
DID: Did + Clone,
V: varsig::Header<Enc> + Clone,
Enc: Codec + TryFrom<u64> + Into<u64>,
> Agent<'a, DID, S, V, Enc>
> Agent<S, DID, V, Enc>
where
Ipld: Encode<Enc>,
Payload<DID>: TryFrom<Named<Ipld>>,
Named<Ipld>: From<Payload<DID>>,
{
pub fn new(did: &'a DID, signer: &'a <DID as Did>::Signer, store: S) -> Self {
pub fn new(did: DID, signer: <DID as Did>::Signer, store: S) -> Self {
Self {
did,
store,
Expand All @@ -73,7 +72,7 @@ where
let nonce = Nonce::generate_12(&mut salt);

if let Some(ref sub) = subject {
if sub == self.did {
if sub == &self.did {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor question about this, because I run into it a lot. There's two options:

if sub == &self.did
// vs
if *sub == self.did

I assume that when you compare with &s, it has to deref these (which is extra indirection). The compiler can probably get rid of this, but do we know?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Good question.
In the error message the compiler suggests &, but * totally works here, too.

let payload: Payload<DID> = Payload {
issuer: self.did.clone(),
audience,
Expand All @@ -88,7 +87,7 @@ where
};

return Ok(
Delegation::try_sign(self.signer, varsig_header, payload).expect("FIXME")
Delegation::try_sign(&self.signer, varsig_header, payload).expect("FIXME")
);
}
}
Expand Down Expand Up @@ -116,7 +115,7 @@ where
not_before: not_before.map(Into::into),
};

Ok(Delegation::try_sign(self.signer, varsig_header, payload).expect("FIXME"))
Ok(Delegation::try_sign(&self.signer, varsig_header, payload).expect("FIXME"))
}

pub fn receive(
Expand All @@ -128,7 +127,7 @@ where
return Ok(());
}

if delegation.audience() != self.did {
if delegation.audience() != &self.did {
return Err(ReceiveError::WrongAudience(delegation.audience().clone()));
}

Expand Down
59 changes: 21 additions & 38 deletions src/invocation/agent.rs
Expand Up @@ -34,7 +34,6 @@ use web_time::SystemTime;

#[derive(Debug)]
pub struct Agent<
'a,
S: Store<T, DID, V, C>,
D: delegation::store::Store<DID, V, C>,
T: ToCommand = ability::preset::Preset,
Expand All @@ -43,19 +42,19 @@ pub struct Agent<
C: Codec + Into<u64> + TryFrom<u64> = varsig::encoding::Preset,
> {
/// The agent's [`DID`].
pub did: &'a DID,
pub did: DID,

/// A [`delegation::Store`][delegation::store::Store].
pub delegation_store: D,

/// A [`Store`][Store] for the agent's [`Invocation`]s.
pub invocation_store: S,

signer: &'a <DID as Did>::Signer,
signer: <DID as Did>::Signer,
marker: PhantomData<(T, V, C)>,
}

impl<'a, T, DID, S, D, V, C> Agent<'a, S, D, T, DID, V, C>
impl<T, DID, S, D, V, C> Agent<S, D, T, DID, V, C>
where
Ipld: Encode<C>,
T: ToCommand + Clone + ParseAbility,
Expand All @@ -71,8 +70,8 @@ where
<D as delegation::store::Store<DID, V, C>>::DelegationStoreError: fmt::Debug,
{
pub fn new(
did: &'a DID,
signer: &'a <DID as Did>::Signer,
did: DID,
signer: <DID as Did>::Signer,
invocation_store: S,
delegation_store: D,
) -> Self {
Expand Down Expand Up @@ -225,7 +224,7 @@ where
.check(proof_payloads, now)
.map_err(ReceiveError::ValidationError)?;

Ok(if invocation.normalized_audience() != self.did {
Ok(if invocation.normalized_audience() != &self.did {
Recipient::Other(invocation.payload)
} else {
Recipient::You(invocation.payload)
Expand Down Expand Up @@ -408,11 +407,9 @@ mod tests {
(verifier, signer)
}

fn setup_agent<'a>(
did: &'a crate::did::preset::Verifier,
signer: &'a crate::did::preset::Signer,
) -> Agent<'a, crate::invocation::store::MemoryStore, crate::delegation::store::MemoryStore>
{
fn setup_agent(
) -> Agent<crate::invocation::store::MemoryStore, crate::delegation::store::MemoryStore> {
let (did, signer) = gen_did();
let inv_store = crate::invocation::store::MemoryStore::default();
let del_store = crate::delegation::store::MemoryStore::default();

Expand All @@ -438,8 +435,7 @@ mod tests {
#[test_log::test]
fn test_invoker_is_sub_implicit_aud() -> TestResult {
let (_nbf, now, exp) = setup_valid_time();
let (server, server_signer) = gen_did();
let mut agent = setup_agent(&server, &server_signer);
let mut agent = setup_agent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm yeah this is a nice effect of this change


let invocation = agent.invoke(
None,
Expand Down Expand Up @@ -467,8 +463,7 @@ mod tests {
#[test_log::test]
fn test_invoker_is_sub_and_aud() -> TestResult {
let (_nbf, now, exp) = setup_valid_time();
let (server, server_signer) = gen_did();
let mut agent = setup_agent(&server, &server_signer);
let mut agent = setup_agent();

let invocation = agent.invoke(
Some(agent.did.clone()),
Expand Down Expand Up @@ -497,8 +492,7 @@ mod tests {
#[test_log::test]
fn test_other_recipient() -> TestResult {
let (_nbf, now, exp) = setup_valid_time();
let (server, server_signer) = gen_did();
let mut agent = setup_agent(&server, &server_signer);
let mut agent = setup_agent();

let (not_server, _) = gen_did();

Expand Down Expand Up @@ -528,8 +522,7 @@ mod tests {
#[test_log::test]
fn test_expired() -> TestResult {
let (past, now, _exp) = setup_valid_time();
let (server, server_signer) = gen_did();
let mut agent = setup_agent(&server, &server_signer);
let mut agent = setup_agent();

let invocation = agent.invoke(
None,
Expand Down Expand Up @@ -564,8 +557,8 @@ mod tests {
#[test_log::test]
fn test_invalid_sig() -> TestResult {
let (_past, now, _exp) = setup_valid_time();
let (server, server_signer) = gen_did();
let mut agent = setup_agent(&server, &server_signer);
let mut agent = setup_agent();
let server = &agent.did;

let mut invocation = agent.invoke(
None,
Expand Down Expand Up @@ -752,14 +745,9 @@ mod tests {
fn test_chain_ok() -> TestResult {
let ctx = setup_test_chain()?;

let mut agent: Agent<
'_,
&crate::invocation::store::MemoryStore<AccountManage>,
&crate::delegation::store::MemoryStore,
AccountManage,
> = Agent::new(
&ctx.server,
&ctx.server_signer,
let mut agent = Agent::new(
ctx.server.clone(),
ctx.server_signer.clone(),
&ctx.inv_store,
&ctx.del_store,
);
Expand All @@ -773,14 +761,9 @@ mod tests {
fn test_chain_wrong_sub() -> TestResult {
let ctx = setup_test_chain()?;

let mut agent: Agent<
'_,
&crate::invocation::store::MemoryStore<AccountManage>,
&crate::delegation::store::MemoryStore,
AccountManage,
> = Agent::new(
&ctx.server,
&ctx.server_signer,
let mut agent = Agent::new(
ctx.server.clone(),
ctx.server_signer.clone(),
&ctx.inv_store,
&ctx.del_store,
);
Expand Down