Skip to content

Commit

Permalink
feat: fct and prf are now optional fields. Fixes #98 (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsantell authored May 25, 2023
1 parent c0bec08 commit 6802b5c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 24 deletions.
16 changes: 14 additions & 2 deletions ucan/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,27 @@ where
false => None,
};

let facts = if self.facts.is_empty() {
None
} else {
Some(self.facts.clone())
};

let proofs = if self.proofs.is_empty() {
None
} else {
Some(self.proofs.clone())
};

Ok(UcanPayload {
aud: self.audience.clone(),
iss: self.issuer.get_did().await?,
exp: self.expiration,
nbf: self.not_before,
nnc: nonce,
att: self.capabilities.clone(),
fct: self.facts.clone(),
prf: self.proofs.clone(),
fct: facts,
prf: proofs,
})
}

Expand Down
16 changes: 9 additions & 7 deletions ucan/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ impl ProofChain {

let mut proofs: Vec<ProofChain> = Vec::new();

for cid_string in ucan.proofs().iter() {
let cid = Cid::try_from(cid_string.as_str())?;
let ucan_token = store.require_token(&cid).await?;
let proof_chain =
Self::try_from_token_string(&ucan_token, now_time, did_parser, store).await?;
proof_chain.validate_link_to(&ucan)?;
proofs.push(proof_chain);
if let Some(ucan_proofs) = ucan.proofs() {
for cid_string in ucan_proofs.iter() {
let cid = Cid::try_from(cid_string.as_str())?;
let ucan_token = store.require_token(&cid).await?;
let proof_chain =
Self::try_from_token_string(&ucan_token, now_time, did_parser, store).await?;
proof_chain.validate_link_to(&ucan)?;
proofs.push(proof_chain);
}
}

let mut redelegations = BTreeSet::<usize>::new();
Expand Down
26 changes: 19 additions & 7 deletions ucan/src/ipld/ucan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub struct UcanIpld {
pub s: Signature,

pub att: Vec<CapabilityIpld>,
pub prf: Vec<Cid>,
pub prf: Option<Vec<Cid>>,
pub exp: u64,
pub fct: Vec<Value>,
pub fct: Option<Vec<Value>>,

pub nnc: Option<String>,
pub nbf: Option<u64>,
Expand All @@ -31,10 +31,19 @@ impl TryFrom<&Ucan> for UcanIpld {
type Error = anyhow::Error;

fn try_from(ucan: &Ucan) -> Result<Self, Self::Error> {
let mut prf = Vec::new();
for cid_string in ucan.proofs() {
prf.push(Cid::try_from(cid_string.as_str())?);
}
let prf = if let Some(proofs) = ucan.proofs() {
let mut prf = Vec::new();
for cid_string in proofs {
prf.push(Cid::try_from(cid_string.as_str())?);
}
if prf.is_empty() {
None
} else {
Some(prf)
}
} else {
None
};

Ok(UcanIpld {
v: ucan.version().to_string(),
Expand Down Expand Up @@ -74,7 +83,10 @@ impl TryFrom<&UcanIpld> for Ucan {
nnc: value.nnc.clone(),
att: value.att.clone(),
fct: value.fct.clone(),
prf: value.prf.iter().map(|cid| cid.to_string()).collect(),
prf: value
.prf
.clone()
.map(|prf| prf.iter().map(|cid| cid.to_string()).collect()),
};

let signed_data = format!(
Expand Down
4 changes: 2 additions & 2 deletions ucan/src/tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn it_builds_with_a_simple_example() {
assert_eq!(ucan.expires_at(), &expiration);
assert!(ucan.not_before().is_some());
assert_eq!(ucan.not_before().unwrap(), not_before);
assert_eq!(ucan.facts(), &vec![fact_1, fact_2]);
assert_eq!(ucan.facts(), &Some(vec![fact_1, fact_2]));

let expected_attenuations =
Vec::from([CapabilityIpld::from(&cap_1), CapabilityIpld::from(&cap_2)]);
Expand Down Expand Up @@ -132,6 +132,6 @@ async fn it_prevents_duplicate_proofs() {

assert_eq!(
next_ucan.proofs(),
&vec![Cid::try_from(ucan).unwrap().to_string()]
&Some(vec![Cid::try_from(ucan).unwrap().to_string()])
)
}
2 changes: 0 additions & 2 deletions ucan/src/tests/ucan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ mod validate {
"exp": ucan.expires_at(),
"nbf": ucan.not_before(),
"att": [],
"fct": [],
"prf": []
},
"signed_data": ucan.signed_data(),
"signature": ucan.signature()
Expand Down
10 changes: 6 additions & 4 deletions ucan/src/ucan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ pub struct UcanPayload {
#[serde(skip_serializing_if = "Option::is_none")]
pub nnc: Option<String>,
pub att: Vec<CapabilityIpld>,
pub fct: Vec<Value>,
pub prf: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fct: Option<Vec<Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prf: Option<Vec<String>>,
}

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -153,7 +155,7 @@ impl Ucan {
&self.payload.aud
}

pub fn proofs(&self) -> &Vec<String> {
pub fn proofs(&self) -> &Option<Vec<String>> {
&self.payload.prf
}

Expand All @@ -173,7 +175,7 @@ impl Ucan {
&self.payload.att
}

pub fn facts(&self) -> &Vec<Value> {
pub fn facts(&self) -> &Option<Vec<Value>> {
&self.payload.fct
}

Expand Down

0 comments on commit 6802b5c

Please sign in to comment.