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 22, 2023
1 parent 4d89351 commit 71f3055
Show file tree
Hide file tree
Showing 46 changed files with 2,035 additions and 107 deletions.
47 changes: 37 additions & 10 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_peer",
"did_doc_sov",
"did_parser",
"did_resolver",
Expand Down
8 changes: 7 additions & 1 deletion did_doc/src/schema/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,15 @@ impl<E> ServiceBuilderWithServiceType<E> {
}

pub fn build(self) -> Service<E> {
let service_type = if self.service_type.len() == 1 {
// SAFETY: we know that the length is non-zero
OneOrList::One(self.service_type.into_iter().next().unwrap())
} else {
OneOrList::List(self.service_type.into_iter().collect())
};
Service {
id: self.id,
service_type: OneOrList::List(self.service_type.into_iter().collect()),
service_type,
service_endpoint: self.service_endpoint,
extra: self.extra,
}
Expand Down
10 changes: 10 additions & 0 deletions did_doc/src/schema/verification_method/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod public_key;
mod verification_method;
mod verification_method_kind;
mod verification_method_type;

pub use verification_method::{
CompleteVerificationMethodBuilder, IncompleteVerificationMethodBuilder, VerificationMethod,
};
pub use verification_method_kind::VerificationMethodKind;
pub use verification_method_type::VerificationMethodType;
112 changes: 112 additions & 0 deletions did_doc/src/schema/verification_method/public_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use std::str::FromStr;

use base64::{engine::general_purpose, Engine};
use serde::{Deserialize, Serialize};

use crate::{
error::DidDocumentBuilderError,
schema::types::{jsonwebkey::JsonWebKey, multibase::Multibase},
};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
#[serde(deny_unknown_fields)]
pub enum PublicKeyField {
#[serde(rename_all = "camelCase")]
Multibase { public_key_multibase: String },
#[serde(rename_all = "camelCase")]
Jwk { public_key_jwk: JsonWebKey },
#[serde(rename_all = "camelCase")]
Base58 { public_key_base58: String },
#[serde(rename_all = "camelCase")]
Base64 { public_key_base64: String },
#[serde(rename_all = "camelCase")]
Hex { public_key_hex: String },
#[serde(rename_all = "camelCase")]
Pem { public_key_pem: String },
#[serde(rename_all = "camelCase")]
Pgp { public_key_pgp: String },
}

impl PublicKeyField {
pub fn key_decoded(&self) -> Result<Vec<u8>, DidDocumentBuilderError> {
match self {
PublicKeyField::Multibase {
public_key_multibase,
} => {
let multibase = Multibase::from_str(public_key_multibase)?;
Ok(multibase.as_ref().to_vec())
}
PublicKeyField::Jwk { public_key_jwk } => public_key_jwk.to_vec(),
PublicKeyField::Base58 { public_key_base58 } => {
Ok(bs58::decode(public_key_base58).into_vec()?)
}
PublicKeyField::Base64 { public_key_base64 } => {
Ok(general_purpose::STANDARD.decode(public_key_base64.as_bytes())?)
}
PublicKeyField::Hex { public_key_hex } => Ok(hex::decode(public_key_hex)?),
PublicKeyField::Pem { public_key_pem } => {
Ok(pem::parse(public_key_pem.as_bytes())?.contents().to_vec())
}
PublicKeyField::Pgp { public_key_pgp: _ } => Err(
DidDocumentBuilderError::UnsupportedPublicKeyField("publicKeyPgp"),
),
}
}
}

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

static PUBLIC_KEY_MULTIBASE: &str = "z6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc";
static PUBLIC_KEY_BASE58: &str = "6LSbysY2xFMRpGMhb7tFTLMpeuPRaqaWM1yECx2AtzE3KCc";
static PUBLIC_KEY_BASE64: &str = "7AEEiIVxASfd1+8HamOWE5BCi6vqNfL13mzYUoQk1M4mKQ==";
static PUBLIC_KEY_HEX: &str =
"ec01048885710127ddd7ef076a63961390428babea35f2f5de6cd8528424d4ce2629";
static PUBLIC_KEY_BYTES: [u8; 34] = [
236, 1, 4, 136, 133, 113, 1, 39, 221, 215, 239, 7, 106, 99, 150, 19, 144, 66, 139, 171,
234, 53, 242, 245, 222, 108, 216, 82, 132, 36, 212, 206, 38, 41,
];

#[test]
fn test_multibase() {
let public_key_field = PublicKeyField::Multibase {
public_key_multibase: PUBLIC_KEY_MULTIBASE.to_string(),
};
assert_eq!(public_key_field.key_decoded().unwrap(), PUBLIC_KEY_BYTES);
}

#[test]
fn test_base58() {
let public_key_field = PublicKeyField::Base58 {
public_key_base58: PUBLIC_KEY_BASE58.to_string(),
};
assert_eq!(
public_key_field.key_decoded().unwrap(),
PUBLIC_KEY_BYTES.to_vec()
);
}

#[test]
fn test_base64() {
let public_key_field = PublicKeyField::Base64 {
public_key_base64: PUBLIC_KEY_BASE64.to_string(),
};
assert_eq!(
public_key_field.key_decoded().unwrap(),
PUBLIC_KEY_BYTES.to_vec()
);
}

#[test]
fn test_hex() {
let public_key_field = PublicKeyField::Hex {
public_key_hex: PUBLIC_KEY_HEX.to_string(),
};
assert_eq!(
public_key_field.key_decoded().unwrap(),
PUBLIC_KEY_BYTES.to_vec()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,76 +1,9 @@
use base64::{engine::general_purpose, Engine};
use did_parser::{Did, DidUrl};
use serde::{Deserialize, Serialize};

use crate::error::DidDocumentBuilderError;
use crate::schema::types::jsonwebkey::JsonWebKey;

use super::types::{jsonwebkey::JsonWebKey, multibase::Multibase};

// Either a set of verification methods maps or DID URLs
// https://www.w3.org/TR/did-core/#did-document-properties
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum VerificationMethodKind {
Resolved(VerificationMethod),
Resolvable(DidUrl),
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
#[serde(deny_unknown_fields)]
pub enum PublicKeyField {
#[serde(rename_all = "camelCase")]
Multibase { public_key_multibase: Multibase },
#[serde(rename_all = "camelCase")]
Jwk { public_key_jwk: JsonWebKey },
#[serde(rename_all = "camelCase")]
Base58 { public_key_base58: String },
#[serde(rename_all = "camelCase")]
Base64 { public_key_base64: String },
#[serde(rename_all = "camelCase")]
Hex { public_key_hex: String },
#[serde(rename_all = "camelCase")]
Pem { public_key_pem: String },
#[serde(rename_all = "camelCase")]
Pgp { public_key_pgp: String },
}

impl PublicKeyField {
pub fn key_decoded(&self) -> Result<Vec<u8>, DidDocumentBuilderError> {
match self {
PublicKeyField::Multibase {
public_key_multibase,
} => Ok(public_key_multibase.as_ref().to_vec()),
PublicKeyField::Jwk { public_key_jwk } => public_key_jwk.to_vec(),
PublicKeyField::Base58 { public_key_base58 } => {
Ok(bs58::decode(public_key_base58).into_vec()?)
}
PublicKeyField::Base64 { public_key_base64 } => {
Ok(general_purpose::STANDARD.decode(public_key_base64.as_bytes())?)
}
PublicKeyField::Hex { public_key_hex } => Ok(hex::decode(public_key_hex)?),
PublicKeyField::Pem { public_key_pem } => {
Ok(pem::parse(public_key_pem.as_bytes())?.contents().to_vec())
}
PublicKeyField::Pgp { public_key_pgp: _ } => Err(
DidDocumentBuilderError::UnsupportedPublicKeyField("publicKeyPgp"),
),
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum VerificationMethodType {
JsonWebKey2020,
EcdsaSecp256k1VerificationKey2019,
Ed25519VerificationKey2018,
Bls12381G1Key2020,
Bls12381G2Key2020,
PgpVerificationKey2021,
RsaVerificationKey2018,
X25519KeyAgreementKey2019,
EcdsaSecp256k1RecoveryMethod2020,
}
use super::{public_key::PublicKeyField, VerificationMethodType};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -109,14 +42,14 @@ impl VerificationMethod {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct IncompleteVerificationMethodBuilder {
id: DidUrl,
controller: Did,
verification_method_type: VerificationMethodType,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CompleteVerificationMethodBuilder {
id: DidUrl,
controller: Did,
Expand All @@ -139,7 +72,7 @@ impl IncompleteVerificationMethodBuilder {

pub fn add_public_key_multibase(
self,
public_key_multibase: Multibase,
public_key_multibase: String,
) -> CompleteVerificationMethodBuilder {
CompleteVerificationMethodBuilder {
id: self.id,
Expand Down Expand Up @@ -230,8 +163,8 @@ mod tests {
DidUrl::parse("did:example:123456789abcdefghi#fragment".to_string()).unwrap()
}

fn create_valid_multibase() -> Multibase {
Multibase::new("zQmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e".to_string()).unwrap()
fn create_valid_multibase() -> String {
"zQmWvQxTqbG2Z9HPJgG57jjwR154cKhbtJenbyYTWkjgF3e".to_string()
}

fn create_valid_verification_key_type() -> VerificationMethodType {
Expand Down
13 changes: 13 additions & 0 deletions did_doc/src/schema/verification_method/verification_method_kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use did_parser::DidUrl;
use serde::{Deserialize, Serialize};

use super::VerificationMethod;

// Either a set of verification methods maps or DID URLs
// https://www.w3.org/TR/did-core/#did-document-properties
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum VerificationMethodKind {
Resolved(VerificationMethod),
Resolvable(DidUrl),
}
Loading

0 comments on commit 71f3055

Please sign in to comment.