From 51392ce03baf9189099da89ec42db936d16d3589 Mon Sep 17 00:00:00 2001 From: Miroslav Kovar Date: Fri, 9 Jun 2023 16:03:30 +0200 Subject: [PATCH] Change key type Signed-off-by: Miroslav Kovar --- did_doc_sov/src/extra_fields/didcommv1.rs | 18 +++++++++--------- did_doc_sov/src/extra_fields/didcommv2.rs | 10 +++++----- did_doc_sov/src/extra_fields/mod.rs | 23 +++++++++++++++++++---- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/did_doc_sov/src/extra_fields/didcommv1.rs b/did_doc_sov/src/extra_fields/didcommv1.rs index 39c917df26..54e0f57d3c 100644 --- a/did_doc_sov/src/extra_fields/didcommv1.rs +++ b/did_doc_sov/src/extra_fields/didcommv1.rs @@ -1,13 +1,13 @@ use serde::{Deserialize, Serialize}; -use super::AcceptType; +use super::{AcceptType, KeyKind}; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)] #[serde(rename_all = "camelCase")] pub struct ExtraFieldsDidCommV1 { priority: u32, - recipient_keys: Vec, - routing_keys: Vec, + recipient_keys: Vec, + routing_keys: Vec, accept: Vec, } @@ -20,11 +20,11 @@ impl ExtraFieldsDidCommV1 { self.priority } - pub fn recipient_keys(&self) -> &[String] { + pub fn recipient_keys(&self) -> &[KeyKind] { self.recipient_keys.as_ref() } - pub fn routing_keys(&self) -> &[String] { + pub fn routing_keys(&self) -> &[KeyKind] { self.routing_keys.as_ref() } @@ -36,8 +36,8 @@ impl ExtraFieldsDidCommV1 { #[derive(Default)] pub struct ExtraFieldsDidCommV1Builder { priority: u32, - recipient_keys: Vec, - routing_keys: Vec, + recipient_keys: Vec, + routing_keys: Vec, } impl ExtraFieldsDidCommV1Builder { @@ -46,12 +46,12 @@ impl ExtraFieldsDidCommV1Builder { self } - pub fn set_recipient_keys(mut self, recipient_keys: Vec) -> Self { + pub fn set_recipient_keys(mut self, recipient_keys: Vec) -> Self { self.recipient_keys = recipient_keys; self } - pub fn set_routing_keys(mut self, routing_keys: Vec) -> Self { + pub fn set_routing_keys(mut self, routing_keys: Vec) -> Self { self.routing_keys = routing_keys; self } diff --git a/did_doc_sov/src/extra_fields/didcommv2.rs b/did_doc_sov/src/extra_fields/didcommv2.rs index 664295d693..85c9c34299 100644 --- a/did_doc_sov/src/extra_fields/didcommv2.rs +++ b/did_doc_sov/src/extra_fields/didcommv2.rs @@ -1,12 +1,12 @@ use serde::{Deserialize, Serialize}; -use super::AcceptType; +use super::{AcceptType, KeyKind}; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)] #[serde(rename_all = "camelCase")] pub struct ExtraFieldsDidCommV2 { accept: Vec, - routing_keys: Vec, + routing_keys: Vec, } impl ExtraFieldsDidCommV2 { @@ -18,18 +18,18 @@ impl ExtraFieldsDidCommV2 { self.accept.as_ref() } - pub fn routing_keys(&self) -> &[String] { + pub fn routing_keys(&self) -> &[KeyKind] { self.routing_keys.as_ref() } } #[derive(Default)] pub struct ExtraFieldsDidCommV2Builder { - routing_keys: Vec, + routing_keys: Vec, } impl ExtraFieldsDidCommV2Builder { - pub fn set_routing_keys(mut self, routing_keys: Vec) -> Self { + pub fn set_routing_keys(mut self, routing_keys: Vec) -> Self { self.routing_keys = routing_keys; self } diff --git a/did_doc_sov/src/extra_fields/mod.rs b/did_doc_sov/src/extra_fields/mod.rs index df7b74b5a3..57fbb42fea 100644 --- a/did_doc_sov/src/extra_fields/mod.rs +++ b/did_doc_sov/src/extra_fields/mod.rs @@ -1,5 +1,6 @@ use std::fmt::Display; +use did_doc::did_parser::DidUrl; use serde::{Deserialize, Deserializer, Serialize}; use crate::error::DidDocumentSovError; @@ -24,6 +25,7 @@ impl Display for AcceptType { } } } + impl<'de> Deserialize<'de> for AcceptType { fn deserialize(deserializer: D) -> Result where @@ -38,6 +40,19 @@ impl<'de> Deserialize<'de> for AcceptType { } } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[serde(untagged)] +pub enum KeyKind { + Reference(DidUrl), + Value(String), +} + +impl Default for KeyKind { + fn default() -> Self { + KeyKind::Value(String::default()) + } +} + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] #[serde(untagged)] pub enum ExtraFields { @@ -53,7 +68,7 @@ impl Default for ExtraFields { } impl ExtraFields { - pub fn recipient_keys(&self) -> Result<&[String], DidDocumentSovError> { + pub fn recipient_keys(&self) -> Result<&[KeyKind], DidDocumentSovError> { match self { ExtraFields::DIDCommV1(extra) => Ok(extra.recipient_keys()), ExtraFields::AIP1(_) | ExtraFields::DIDCommV2(_) => { @@ -62,7 +77,7 @@ impl ExtraFields { } } - pub fn routing_keys(&self) -> Result<&[String], DidDocumentSovError> { + pub fn routing_keys(&self) -> Result<&[KeyKind], DidDocumentSovError> { match self { ExtraFields::DIDCommV1(extra) => Ok(extra.routing_keys()), ExtraFields::DIDCommV2(extra) => Ok(extra.routing_keys()), @@ -70,13 +85,13 @@ impl ExtraFields { } } - pub fn first_recipient_key(&self) -> Result<&String, DidDocumentSovError> { + pub fn first_recipient_key(&self) -> Result<&KeyKind, DidDocumentSovError> { self.recipient_keys()? .first() .ok_or(DidDocumentSovError::EmptyCollection("recipient_keys")) } - pub fn first_routing_key(&self) -> Result<&String, DidDocumentSovError> { + pub fn first_routing_key(&self) -> Result<&KeyKind, DidDocumentSovError> { self.routing_keys()? .first() .ok_or(DidDocumentSovError::EmptyCollection("routing_keys"))