-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from iotaledger/feat/verifiable-credentials
feat(vc): Create verifiable credentials
- Loading branch information
Showing
43 changed files
with
1,875 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
mod macros; | ||
mod object; | ||
mod one_or_many; | ||
mod timestamp; | ||
mod uri; | ||
mod value; | ||
|
||
pub use object::Object; | ||
pub use one_or_many::OneOrMany; | ||
pub use timestamp::Timestamp; | ||
pub use uri::Uri; | ||
pub use value::Value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt; | ||
|
||
/// A generic container that stores one or many values of a given type. | ||
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] | ||
#[serde(untagged)] | ||
pub enum OneOrMany<T> { | ||
One(T), | ||
Many(Vec<T>), | ||
} | ||
|
||
impl<T> OneOrMany<T> { | ||
/// Returns the number of elements in the collection | ||
pub fn len(&self) -> usize { | ||
match self { | ||
Self::One(_) => 1, | ||
Self::Many(inner) => inner.len(), | ||
} | ||
} | ||
|
||
/// Returns `true` if the collection is empty | ||
pub fn is_empty(&self) -> bool { | ||
match self { | ||
Self::One(_) => false, | ||
Self::Many(inner) => inner.is_empty(), | ||
} | ||
} | ||
|
||
/// Returns a reference to the element at the given index. | ||
pub fn get(&self, index: usize) -> Option<&T> { | ||
match self { | ||
Self::One(inner) if index == 0 => Some(inner), | ||
Self::One(_) => None, | ||
Self::Many(inner) => inner.get(index), | ||
} | ||
} | ||
|
||
/// Returns `true` if the given value is represented in the collection. | ||
pub fn contains(&self, value: &T) -> bool | ||
where | ||
T: PartialEq<T>, | ||
{ | ||
match self { | ||
Self::One(inner) => inner == value, | ||
Self::Many(inner) => inner.contains(value), | ||
} | ||
} | ||
|
||
pub fn iter(&self) -> impl Iterator<Item = &T> + '_ { | ||
OneOrManyIter::new(self) | ||
} | ||
|
||
/// Consumes the `OneOrMany`, returning the contents as a `Vec`. | ||
pub fn into_vec(self) -> Vec<T> { | ||
match self { | ||
Self::One(inner) => vec![inner], | ||
Self::Many(inner) => inner, | ||
} | ||
} | ||
} | ||
|
||
impl<T> fmt::Debug for OneOrMany<T> | ||
where | ||
T: fmt::Debug, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Self::One(inner) => fmt::Debug::fmt(inner, f), | ||
Self::Many(inner) => fmt::Debug::fmt(inner, f), | ||
} | ||
} | ||
} | ||
|
||
impl<T> Default for OneOrMany<T> { | ||
fn default() -> Self { | ||
Self::Many(Vec::new()) | ||
} | ||
} | ||
|
||
impl<T> From<T> for OneOrMany<T> { | ||
fn from(other: T) -> Self { | ||
Self::One(other) | ||
} | ||
} | ||
|
||
impl<T> From<Vec<T>> for OneOrMany<T> { | ||
fn from(mut other: Vec<T>) -> Self { | ||
if other.len() == 1 { | ||
Self::One(other.pop().expect("infallible")) | ||
} else { | ||
Self::Many(other) | ||
} | ||
} | ||
} | ||
|
||
impl<T> From<OneOrMany<T>> for Vec<T> { | ||
fn from(other: OneOrMany<T>) -> Self { | ||
other.into_vec() | ||
} | ||
} | ||
|
||
struct OneOrManyIter<'a, T> { | ||
inner: &'a OneOrMany<T>, | ||
index: usize, | ||
} | ||
|
||
impl<'a, T> OneOrManyIter<'a, T> { | ||
pub fn new(inner: &'a OneOrMany<T>) -> Self { | ||
Self { inner, index: 0 } | ||
} | ||
} | ||
|
||
impl<'a, T> Iterator for OneOrManyIter<'a, T> { | ||
type Item = &'a T; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.index += 1; | ||
self.inner.get(self.index - 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::{fmt, ops::Deref}; | ||
|
||
use crate::did::DID; | ||
|
||
/// A simple wrapper for URIs adhering to RFC 3986 | ||
/// | ||
/// TODO: Parse/Validate according to RFC 3986 | ||
/// TODO: impl From<DID> for Uri | ||
#[derive(Clone, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] | ||
#[repr(transparent)] | ||
#[serde(transparent)] | ||
pub struct Uri(pub(crate) String); | ||
|
||
impl Uri { | ||
pub fn into_inner(self) -> String { | ||
self.0 | ||
} | ||
} | ||
|
||
impl fmt::Debug for Uri { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "Uri({:?})", self.0) | ||
} | ||
} | ||
|
||
impl Deref for Uri { | ||
type Target = String; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl From<&'_ str> for Uri { | ||
fn from(other: &'_ str) -> Self { | ||
Self(other.into()) | ||
} | ||
} | ||
|
||
impl From<String> for Uri { | ||
fn from(other: String) -> Self { | ||
Self(other) | ||
} | ||
} | ||
|
||
impl From<DID> for Uri { | ||
fn from(other: DID) -> Uri { | ||
Self(other.to_string()) | ||
} | ||
} | ||
|
||
impl<T> PartialEq<T> for Uri | ||
where | ||
T: AsRef<str> + ?Sized, | ||
{ | ||
fn eq(&self, other: &T) -> bool { | ||
self.0.eq(other.as_ref()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use anyhow::Result; | ||
use identity_vc::prelude::*; | ||
use serde_json::{from_reader, to_string}; | ||
use std::{env::args, fs::File, path::Path}; | ||
|
||
fn main() -> Result<()> { | ||
let args: Vec<String> = args().collect(); | ||
|
||
match args[1].as_str() { | ||
"test-credential" => { | ||
let path: &Path = Path::new(&args[2]); | ||
let file: File = File::open(path)?; | ||
let data: VerifiableCredential = from_reader(file)?; | ||
|
||
data.validate()?; | ||
|
||
println!("{}", to_string(&data)?); | ||
} | ||
"test-presentation" => { | ||
let path: &Path = Path::new(&args[2]); | ||
let file: File = File::open(path)?; | ||
let data: VerifiablePresentation = from_reader(file)?; | ||
|
||
data.validate()?; | ||
|
||
println!("{}", to_string(&data)?); | ||
} | ||
test => { | ||
panic!("Unknown Test: {:?}", test); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use identity_core::common::{Object, Uri}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt; | ||
|
||
/// A reference to a JSON-LD context | ||
#[derive(Clone, PartialEq, Deserialize, Serialize)] | ||
#[serde(untagged)] | ||
pub enum Context { | ||
Uri(Uri), | ||
Obj(Object), | ||
} | ||
|
||
impl fmt::Debug for Context { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Self::Uri(inner) => fmt::Debug::fmt(inner, f), | ||
Self::Obj(inner) => fmt::Debug::fmt(inner, f), | ||
} | ||
} | ||
} | ||
|
||
impl From<Uri> for Context { | ||
fn from(other: Uri) -> Self { | ||
Self::Uri(other) | ||
} | ||
} | ||
|
||
impl From<&'_ str> for Context { | ||
fn from(other: &'_ str) -> Self { | ||
Self::Uri(other.into()) | ||
} | ||
} | ||
|
||
impl From<String> for Context { | ||
fn from(other: String) -> Self { | ||
Self::Uri(other.into()) | ||
} | ||
} | ||
|
||
impl From<Object> for Context { | ||
fn from(other: Object) -> Self { | ||
Self::Obj(other) | ||
} | ||
} |
Oops, something went wrong.