Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: bwty <[email protected]>
  • Loading branch information
whalelephant committed Feb 13, 2023
1 parent 64c9bd5 commit 3614df7
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 205 deletions.
59 changes: 58 additions & 1 deletion src/data_types/pres_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,59 @@ impl Serialize for PresentationRequest {
#[allow(unused)]
pub type PresentationRequestExtraQuery = HashMap<String, Query>;

#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[derive(Clone, Default, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub struct NonRevocedInterval {
pub from: Option<u64>,
pub to: Option<u64>,
}

impl NonRevocedInterval {
// Returns the most stringent interval,
// i.e. the latest from and the earliest to
pub fn compare_and_set(&mut self, to_compare: &NonRevocedInterval) {
// Update if
// - the new `from` value is later, smaller interval
// - the new `from` value is Some if previouly was None
match (self.from, to_compare.from) {
(Some(old_from), Some(new_from)) => {
if old_from.lt(&new_from) {
self.from = to_compare.from
}
}
(None, Some(_)) => self.from = to_compare.from,
_ => (),
}
// Update if
// - the new `to` value is earlier, smaller interval
// - the new `to` value is Some if previouly was None
match (self.to, to_compare.to) {
(Some(old_to), Some(new_to)) => {
if new_to.lt(&old_to) {
self.to = to_compare.to
}
}
(None, Some(_)) => self.to = to_compare.to,
_ => (),
}
}

pub fn update_with_override(&mut self, override_map: &HashMap<u64, u64>) {
self.from.map(|from| {
override_map
.get(&from)
.map(|&override_timestamp| self.from = Some(override_timestamp))
});
}

pub fn is_valid(&self, timestamp: u64) -> Result<(), ValidationError> {
if timestamp.lt(&self.from.unwrap_or(0)) || timestamp.gt(&self.to.unwrap_or(u64::MAX)) {
Err(invalid!("Invalid timestamp"))
} else {
Ok(())
}
}
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct AttributeInfo {
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -330,4 +377,14 @@ mod tests {
serde_json::from_str::<PresentationRequest>(&req_json).unwrap_err();
}
}

#[test]
fn override_works() {
let mut interval = NonRevocedInterval::default();
let override_map = HashMap::from([(10u64, 5u64)]);

interval.from = Some(10);
interval.update_with_override(&override_map);
assert_eq!(interval.from.unwrap(), 5u64);
}
}
5 changes: 4 additions & 1 deletion src/ffi/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,13 @@ pub extern "C" fn anoncreds_create_presentation(
/// E.g. if the ledger has Revocation Status List at timestamps [0, 100, 200],
/// let's call them List0, List100, List200. Then:
///
/// ```txt
///
/// List0 is valid List100 is valid
/// ______|_______ _______|_______
/// | | |
/// List 0 ----------- 100 ----------- 200
/// ```
///
/// A `nonrevoked_interval = {from: 50, to: 150}` should accept both List0 and
/// List100.
Expand Down Expand Up @@ -310,7 +313,7 @@ pub extern "C" fn anoncreds_verify_presentation(

let override_entries = {
let override_ffi_entries = nonrevoked_interval_override.as_slice();
override_ffi_entries.into_iter().try_fold(
override_ffi_entries.iter().try_fold(
Vec::with_capacity(override_ffi_entries.len()),
|mut v, entry| -> Result<Vec<(RevocationRegistryDefinitionId, u64, u64)>> {
v.push(entry.load()?);
Expand Down
51 changes: 1 addition & 50 deletions src/services/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
use crate::data_types::{
credential::AttributeValues,
nonce::Nonce,
pres_request::{AttributeInfo, NonRevocedInterval, PredicateInfo},
pres_request::{AttributeInfo, PredicateInfo},
};
use crate::utils::hash::SHA256;

Expand Down Expand Up @@ -134,55 +134,6 @@ pub fn build_sub_proof_request(
Ok(res)
}

pub fn get_non_revoc_interval(
global_interval: &Option<NonRevocedInterval>,
local_interval: &Option<NonRevocedInterval>,
) -> Option<NonRevocedInterval> {
trace!(
"get_non_revoc_interval >>> global_interval: {:?}, local_interval: {:?}",
global_interval,
local_interval
);

let interval = local_interval
.clone()
.or_else(|| global_interval.clone().or(None));

trace!("get_non_revoc_interval <<< interval: {:?}", interval);

interval
}

pub fn new_nonce() -> Result<Nonce> {
Nonce::new().map_err(err_map!(Unexpected))
}

#[cfg(test)]
mod tests {
use super::*;

fn _interval() -> NonRevocedInterval {
NonRevocedInterval {
from: None,
to: Some(123),
}
}

#[test]
fn get_non_revoc_interval_for_global() {
let res = get_non_revoc_interval(&Some(_interval()), &None).unwrap();
assert_eq!(_interval(), res);
}

#[test]
fn get_non_revoc_interval_for_local() {
let res = get_non_revoc_interval(&None, &Some(_interval())).unwrap();
assert_eq!(_interval(), res);
}

#[test]
fn get_non_revoc_interval_for_none() {
let res = get_non_revoc_interval(&None, &None);
assert_eq!(None, res);
}
}
Loading

0 comments on commit 3614df7

Please sign in to comment.