-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
- Loading branch information
Showing
19 changed files
with
972 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,5 +1,5 @@ | ||
pub mod did_doc; | ||
pub mod service; | ||
pub mod types; | ||
pub(crate) mod utils; | ||
pub mod utils; | ||
pub mod verification_method; |
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,12 @@ | ||
[package] | ||
name = "did_doc_sov" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
did_doc = { path = "../did_doc" } | ||
serde = { version = "1.0.159", default-features = false, features = ["derive"] } | ||
serde_json = "1.0.95" | ||
thiserror = "1.0.40" |
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,13 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum DidDocumentSovError { | ||
#[error("Attempted to access empty collection: {0}")] | ||
EmptyCollection(&'static str), | ||
#[error("DID document builder error: {0}")] | ||
DidDocumentBuilderError(#[from] did_doc::error::DidDocumentBuilderError), | ||
#[error("Unexpected service type: {0}")] | ||
UnexpectedServiceType(String), | ||
#[error("Index out of bounds: {0}")] | ||
IndexOutOfBounds(usize), | ||
} |
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,4 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)] | ||
pub struct ExtraFieldsAIP1 {} |
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,67 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::AcceptType; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct ExtraFieldsDidCommV1 { | ||
priority: u32, | ||
recipient_keys: Vec<String>, | ||
routing_keys: Vec<String>, | ||
accept: Vec<AcceptType>, | ||
} | ||
|
||
impl ExtraFieldsDidCommV1 { | ||
pub fn builder() -> ExtraFieldsDidCommV1Builder { | ||
ExtraFieldsDidCommV1Builder::default() | ||
} | ||
|
||
pub fn priority(&self) -> u32 { | ||
self.priority | ||
} | ||
|
||
pub fn recipient_keys(&self) -> &[String] { | ||
self.recipient_keys.as_ref() | ||
} | ||
|
||
pub fn routing_keys(&self) -> &[String] { | ||
self.routing_keys.as_ref() | ||
} | ||
|
||
pub fn accept(&self) -> &[AcceptType] { | ||
self.accept.as_ref() | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct ExtraFieldsDidCommV1Builder { | ||
priority: u32, | ||
recipient_keys: Vec<String>, | ||
routing_keys: Vec<String>, | ||
} | ||
|
||
impl ExtraFieldsDidCommV1Builder { | ||
pub fn set_priority(mut self, priority: u32) -> Self { | ||
self.priority = priority; | ||
self | ||
} | ||
|
||
pub fn set_recipient_keys(mut self, recipient_keys: Vec<String>) -> Self { | ||
self.recipient_keys = recipient_keys; | ||
self | ||
} | ||
|
||
pub fn set_routing_keys(mut self, routing_keys: Vec<String>) -> Self { | ||
self.routing_keys = routing_keys; | ||
self | ||
} | ||
|
||
pub fn build(self) -> ExtraFieldsDidCommV1 { | ||
ExtraFieldsDidCommV1 { | ||
priority: self.priority, | ||
recipient_keys: self.recipient_keys, | ||
routing_keys: self.routing_keys, | ||
accept: vec![AcceptType::DIDCommV1], | ||
} | ||
} | ||
} |
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,43 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::AcceptType; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct ExtraFieldsDidCommV2 { | ||
accept: Vec<AcceptType>, | ||
routing_keys: Vec<String>, | ||
} | ||
|
||
impl ExtraFieldsDidCommV2 { | ||
pub fn builder() -> ExtraFieldsDidCommV2Builder { | ||
ExtraFieldsDidCommV2Builder::default() | ||
} | ||
|
||
pub fn accept(&self) -> &[AcceptType] { | ||
self.accept.as_ref() | ||
} | ||
|
||
pub fn routing_keys(&self) -> &[String] { | ||
self.routing_keys.as_ref() | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct ExtraFieldsDidCommV2Builder { | ||
routing_keys: Vec<String>, | ||
} | ||
|
||
impl ExtraFieldsDidCommV2Builder { | ||
pub fn set_routing_keys(mut self, routing_keys: Vec<String>) -> Self { | ||
self.routing_keys = routing_keys; | ||
self | ||
} | ||
|
||
pub fn build(self) -> ExtraFieldsDidCommV2 { | ||
ExtraFieldsDidCommV2 { | ||
accept: vec![AcceptType::DIDCommV2], | ||
routing_keys: self.routing_keys, | ||
} | ||
} | ||
} |
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,99 @@ | ||
use std::fmt::Display; | ||
|
||
use serde::{Deserialize, Deserializer, Serialize}; | ||
|
||
use crate::error::DidDocumentSovError; | ||
|
||
pub mod aip1; | ||
pub mod didcommv1; | ||
pub mod didcommv2; | ||
|
||
#[derive(Serialize, Clone, Debug, PartialEq)] | ||
pub enum AcceptType { | ||
DIDCommV1, | ||
DIDCommV2, | ||
Other(String), | ||
} | ||
|
||
impl Display for AcceptType { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
AcceptType::DIDCommV1 => write!(f, "didcomm/aip2;env=rfc19"), | ||
AcceptType::DIDCommV2 => write!(f, "didcomm/v2"), | ||
AcceptType::Other(other) => write!(f, "{}", other), | ||
} | ||
} | ||
} | ||
impl<'de> Deserialize<'de> for AcceptType { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
match s.as_str() { | ||
"didcomm/aip2;env=rfc19" => Ok(AcceptType::DIDCommV1), | ||
"didcomm/v2" => Ok(AcceptType::DIDCommV2), | ||
_ => Ok(AcceptType::Other(s)), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] | ||
#[serde(untagged)] | ||
pub enum ExtraFields { | ||
DIDCommV1(didcommv1::ExtraFieldsDidCommV1), | ||
DIDCommV2(didcommv2::ExtraFieldsDidCommV2), | ||
AIP1(aip1::ExtraFieldsAIP1), | ||
} | ||
|
||
impl Default for ExtraFields { | ||
fn default() -> Self { | ||
ExtraFields::AIP1(aip1::ExtraFieldsAIP1::default()) | ||
} | ||
} | ||
|
||
impl ExtraFields { | ||
pub fn recipient_keys(&self) -> Result<&[String], DidDocumentSovError> { | ||
match self { | ||
ExtraFields::DIDCommV1(extra) => Ok(extra.recipient_keys()), | ||
ExtraFields::AIP1(_) | ExtraFields::DIDCommV2(_) => { | ||
Err(DidDocumentSovError::EmptyCollection("recipient_keys")) | ||
} | ||
} | ||
} | ||
|
||
pub fn routing_keys(&self) -> Result<&[String], DidDocumentSovError> { | ||
match self { | ||
ExtraFields::DIDCommV1(extra) => Ok(extra.routing_keys()), | ||
ExtraFields::DIDCommV2(extra) => Ok(extra.routing_keys()), | ||
ExtraFields::AIP1(_) => Err(DidDocumentSovError::EmptyCollection("routing_keys")), | ||
} | ||
} | ||
|
||
pub fn first_recipient_key(&self) -> Result<&String, DidDocumentSovError> { | ||
self.recipient_keys()? | ||
.first() | ||
.ok_or(DidDocumentSovError::EmptyCollection("recipient_keys")) | ||
} | ||
|
||
pub fn first_routing_key(&self) -> Result<&String, DidDocumentSovError> { | ||
self.routing_keys()? | ||
.first() | ||
.ok_or(DidDocumentSovError::EmptyCollection("routing_keys")) | ||
} | ||
|
||
pub fn accept(&self) -> Result<&[AcceptType], DidDocumentSovError> { | ||
match self { | ||
ExtraFields::DIDCommV1(extra) => Ok(extra.accept()), | ||
ExtraFields::DIDCommV2(extra) => Ok(extra.accept()), | ||
ExtraFields::AIP1(_) => Err(DidDocumentSovError::EmptyCollection("accept")), | ||
} | ||
} | ||
|
||
pub fn priority(&self) -> Result<u32, DidDocumentSovError> { | ||
match self { | ||
ExtraFields::DIDCommV1(extra) => Ok(extra.priority()), | ||
_ => Err(DidDocumentSovError::EmptyCollection("priority")), | ||
} | ||
} | ||
} |
Oops, something went wrong.