Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee committed Jun 6, 2023
1 parent d5ffa6c commit c6b6551
Show file tree
Hide file tree
Showing 19 changed files with 965 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ members = [
"aries_vcx_core",
"uniffi_aries_vcx/core",
"did_doc",
"did_doc_sov",
"did_parser",
"did_resolver",
"did_resolver_registry",
Expand Down
2 changes: 1 addition & 1 deletion did_doc/src/schema/did_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{
verification_method::{VerificationMethod, VerificationMethodKind},
};

type ControllerAlias = OneOrList<Did>;
pub type ControllerAlias = OneOrList<Did>;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
Expand Down
2 changes: 1 addition & 1 deletion did_doc/src/schema/mod.rs
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;
2 changes: 1 addition & 1 deletion did_doc/src/schema/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
utils::OneOrList,
};

type ServiceTypeAlias = OneOrList<String>;
pub type ServiceTypeAlias = OneOrList<String>;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down
12 changes: 12 additions & 0 deletions did_doc_sov/Cargo.toml
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"
13 changes: 13 additions & 0 deletions did_doc_sov/src/error.rs
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),
}
4 changes: 4 additions & 0 deletions did_doc_sov/src/extra_fields/aip1.rs
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 {}
71 changes: 71 additions & 0 deletions did_doc_sov/src/extra_fields/didcommv1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use serde::{Deserialize, Serialize};

use super::AcceptType;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ExtraFieldsDidCommV1 {
#[serde(default)]
priority: u32,
#[serde(default)]
recipient_keys: Vec<String>,
#[serde(default)]
routing_keys: Vec<String>,
#[serde(default)]
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],
}
}
}
45 changes: 45 additions & 0 deletions did_doc_sov/src/extra_fields/didcommv2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use serde::{Deserialize, Serialize};

use super::AcceptType;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ExtraFieldsDidCommV2 {
#[serde(default)]
accept: Vec<AcceptType>,
#[serde(default)]
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,
}
}
}
86 changes: 86 additions & 0 deletions did_doc_sov/src/extra_fields/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::error::DidDocumentSovError;

pub mod aip1;
pub mod didcommv1;
pub mod didcommv2;

#[derive(Serialize, Deserialize, 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),
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum ExtraFields {
AIP1(aip1::ExtraFieldsAIP1),
DIDCommV1(didcommv1::ExtraFieldsDidCommV1),
DIDCommV2(didcommv2::ExtraFieldsDidCommV2),
}

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")),
}
}
}
107 changes: 107 additions & 0 deletions did_doc_sov/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
pub mod error;
pub mod extra_fields;
pub mod service;

use did_doc::{
did_parser::Did,
schema::{
did_doc::{ControllerAlias, DidDocument, DidDocumentBuilder},
types::uri::Uri,
verification_method::{VerificationMethod, VerificationMethodKind},
},
};
use error::DidDocumentSovError;
use extra_fields::ExtraFields;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use service::{services_list::ServicesList, ServiceSov};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct DidDocumentSov {
#[serde(flatten)]
did_doc: DidDocument<ExtraFields>,
}

impl DidDocumentSov {
pub fn builder() -> DidDocumentSovBuilder {
DidDocumentSovBuilder::default()
}

pub fn id(&self) -> &Did {
self.did_doc.id()
}

pub fn also_known_as(&self) -> &[Uri] {
self.did_doc.also_known_as()
}

pub fn controller(&self) -> Option<&ControllerAlias> {
self.did_doc.controller()
}

pub fn verification_method(&self) -> &[VerificationMethod] {
self.did_doc.verification_method()
}

pub fn authentication(&self) -> &[VerificationMethodKind] {
self.did_doc.authentication()
}

pub fn service(&self) -> ServicesList {
ServicesList::new(self.did_doc.service())
}

pub fn assertion_method(&self) -> &[VerificationMethodKind] {
self.did_doc.assertion_method()
}

pub fn key_agreement(&self) -> &[VerificationMethodKind] {
self.did_doc.key_agreement()
}

pub fn capability_invocation(&self) -> &[VerificationMethodKind] {
self.did_doc.capability_invocation()
}

pub fn capability_delegation(&self) -> &[VerificationMethodKind] {
self.did_doc.capability_delegation()
}

pub fn extra_field(&self, key: &str) -> Option<&Value> {
self.did_doc.extra_field(key)
}
}

#[derive(Default)]
pub struct DidDocumentSovBuilder {
ddo_builder: DidDocumentBuilder<ExtraFields>,
}

impl DidDocumentSovBuilder {
pub fn new(id: Did) -> Self {
Self {
ddo_builder: DidDocumentBuilder::new(id),
}
}

pub fn add_controller(mut self, controller: Did) -> Self {
self.ddo_builder = self.ddo_builder.add_controller(controller);
self
}

pub fn add_verification_method(mut self, verification_method: VerificationMethod) -> Self {
self.ddo_builder = self.ddo_builder.add_verification_method(verification_method);
self
}

pub fn add_service(mut self, service: ServiceSov) -> Result<Self, DidDocumentSovError> {
self.ddo_builder = self.ddo_builder.add_service(service.try_into()?);
Ok(self)
}

pub fn build(self) -> DidDocumentSov {
DidDocumentSov {
did_doc: self.ddo_builder.build(),
}
}
}
Loading

0 comments on commit c6b6551

Please sign in to comment.