From 291b5a456134a1d8fe70f0ee0e14ac5fa8c09ffb Mon Sep 17 00:00:00 2001 From: Gayan Date: Fri, 10 Sep 2021 10:16:52 +1200 Subject: [PATCH] Added DID meta data and DID resolution metadata --- Cargo.lock | 45 +++++-- pallets/dids/Cargo.toml | 1 + pallets/dids/src/lib.rs | 10 +- pallets/dids/src/structs.rs | 13 +- pallets/dids/src/tests.rs | 238 +++++++++++++++--------------------- 5 files changed, 154 insertions(+), 153 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c6a5c7..47af0a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2260,7 +2260,7 @@ dependencies = [ "itoa", "log", "net2", - "rustc_version", + "rustc_version 0.2.3", "time", "tokio 0.1.22", "tokio-buf", @@ -3874,6 +3874,7 @@ dependencies = [ "log", "pallet-timestamp", "parity-scale-codec", + "rstest", "serde", "sp-core", "sp-io", @@ -4192,7 +4193,7 @@ checksum = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" dependencies = [ "lock_api 0.3.4", "parking_lot_core 0.6.2", - "rustc_version", + "rustc_version 0.2.3", ] [[package]] @@ -4226,7 +4227,7 @@ dependencies = [ "cloudabi", "libc", "redox_syscall 0.1.57", - "rustc_version", + "rustc_version 0.2.3", "smallvec 0.6.14", "winapi 0.3.9", ] @@ -4530,9 +4531,9 @@ checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" [[package]] name = "proc-macro2" -version = "1.0.26" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" +checksum = "b9f5105d4fdaab20335ca9565e106a5d9b82b6219b5ba735731124ac6711d23d" dependencies = [ "unicode-xid", ] @@ -4806,7 +4807,7 @@ checksum = "1fdf7d9dbd43f3d81d94a49c1c3df73cc2b3827995147e6cf7f89d4ec5483e73" dependencies = [ "bitflags", "cc", - "rustc_version", + "rustc_version 0.2.3", ] [[package]] @@ -4994,6 +4995,19 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "rstest" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2288c66aeafe3b2ed227c981f364f9968fa952ef0b30e84ada4486e7ee24d00a" +dependencies = [ + "cfg-if 1.0.0", + "proc-macro2", + "quote", + "rustc_version 0.4.0", + "syn", +] + [[package]] name = "rustc-demangle" version = "0.1.19" @@ -5021,6 +5035,15 @@ dependencies = [ "semver 0.9.0", ] +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver 1.0.4", +] + [[package]] name = "rustls" version = "0.18.1" @@ -5082,7 +5105,7 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d3d055a2582e6b00ed7a31c1524040aa391092bf636328350813f3a0605215c" dependencies = [ - "rustc_version", + "rustc_version 0.2.3", ] [[package]] @@ -6140,6 +6163,12 @@ dependencies = [ "serde", ] +[[package]] +name = "semver" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" + [[package]] name = "semver-parser" version = "0.7.0" @@ -6333,7 +6362,7 @@ dependencies = [ "rand 0.7.3", "rand_core 0.5.1", "ring", - "rustc_version", + "rustc_version 0.2.3", "sha2 0.9.5", "subtle 2.4.0", "x25519-dalek", diff --git a/pallets/dids/Cargo.toml b/pallets/dids/Cargo.toml index 086a9e8..066f479 100644 --- a/pallets/dids/Cargo.toml +++ b/pallets/dids/Cargo.toml @@ -21,6 +21,7 @@ serde = { version = "1.0.119" } sp-core = { default-features = false, version = '3.0.0' } sp-io = { default-features = false, version = '3.0.0' } sp-runtime = { default-features = false, version = '3.0.0' } +rstest = { version = "0.11.0" } [features] default = ['std'] diff --git a/pallets/dids/src/lib.rs b/pallets/dids/src/lib.rs index e375a31..803045a 100644 --- a/pallets/dids/src/lib.rs +++ b/pallets/dids/src/lib.rs @@ -236,6 +236,9 @@ pub mod pallet { pub fn insert_did_document( origin: OriginFor, did_document: Vec, + did_document_metadata: Option>, + did_resolution_metadata: Option>, + sender_account_id: ::AccountId, did_hash: Vec, ) -> DispatchResultWithPostInfo { let origin_account = ensure_signed(origin)?; @@ -252,13 +255,12 @@ pub mod pallet { DIDDocument::::insert( did_hash.clone(), DID { - did_uri: None, - did_document, + did_document_metadata, + did_resolution_metadata, block_number, block_time_stamp: time, did_ref: None, - sender_account_id: origin_account.clone(), - active: Some(true), + sender_account_id, }, ); diff --git a/pallets/dids/src/structs.rs b/pallets/dids/src/structs.rs index 96af855..559fe90 100644 --- a/pallets/dids/src/structs.rs +++ b/pallets/dids/src/structs.rs @@ -17,11 +17,18 @@ pub struct DIDDetail { #[derive(Clone, Decode, Encode, Eq, PartialEq)] pub struct DID { // DID Document hash: Vec, - pub did_uri: Option>, + // pub did_uri: Option>, // DID Document - pub did_document: Vec, + // pub did_document: Vec, + // DID Resolution Metadata + pub did_resolution_metadata: Option>, + + // DID Document Metadata + pub did_document_metadata: Option>, + + // Block number pub block_number: ::BlockNumber, // Block time stamp in ISO 8601 format pub block_time_stamp: u64, @@ -33,7 +40,7 @@ pub struct DID { pub sender_account_id: ::AccountId, // Active status - pub active: Option, + // pub active: Option, } #[derive(Clone, Decode, Encode, Eq, PartialEq)] diff --git a/pallets/dids/src/tests.rs b/pallets/dids/src/tests.rs index 9c6afaa..14db6e9 100644 --- a/pallets/dids/src/tests.rs +++ b/pallets/dids/src/tests.rs @@ -2,63 +2,94 @@ use super::*; use crate::mock::new_test_ext; use crate::mock::Origin; +use rstest::*; + use crate::mock::DIDModule; use frame_support::pallet_prelude::DispatchError; use frame_support::sp_runtime::app_crypto::sp_core::Hasher; use frame_support::{assert_err, assert_ok}; use sp_core::Blake2Hasher; -#[test] -fn create_vc() { - let public_key = vec![ + +#[fixture] +pub fn did_document_metadata() -> Option> { + Some(r#"{ + "created": "2002-01-01T20:20:20Z", + "updated": "2002-02-01T20:20:20Z", + "deactivated": "2002-03-01T20:20:20Z", + "versionId": "1", + + }"#.as_bytes().to_vec()) +} + +#[fixture] +pub fn did_resolution_metadata() -> Option> { + Some(r#"{ + "accept": "application/did+ld+json" + }"#.as_bytes().to_vec()) +} + +#[fixture] +pub fn did_document() -> &'static str { + r#"{ + "@context": [ + "https://www.w3.org/ns/did/v1", + "https://w3id.org/security/suites/ed25519-2020/v1" + ] + "id": "did:trackback.dev:123456789abcdefghi", + "authentication": [{ + "id": "did:trackback.dev:123456789abcdefghi#keys-1", + "type": "Ed25519VerificationKey2020", + "controller": "did:trackback.dev:123456789abcdefghi", + "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV" + }] + }"# +} + +#[fixture] +pub fn did_hash(did_document: &'static str) -> Vec { + Blake2Hasher::hash(did_document.as_ref()).as_bytes().to_vec() +} + +#[fixture] +pub fn public_key() ->Vec { + vec![ 0, 1, 217, 200, 51, 244, 152, 125, 173, 92, 30, 224, 60, 141, 221, 44, 65, 132, 45, 94, 199, 150, 116, 108, 95, 18, 118, 246, 86, 167, 64, 132, 76, - ]; + ] +} + +#[fixture] +pub fn vc_hash() -> Vec { + "Hash".as_bytes().to_vec() +} - let hash = "Hash".as_bytes().to_vec(); +#[rstest] +fn create_vc(public_key: Vec,vc_hash: Vec) { new_test_ext().execute_with(|| { - let res = DIDModule::create_vc_fingerprint( + assert_ok!(DIDModule::create_vc_fingerprint( Origin::signed(1), public_key, - hash, + vc_hash, Some(true) - ); - let p = 0; - // assert_ok!(DIDModule::create_vc_fingerprint( - // Origin::signed(1), - // public_key, - // hash, - // Some(true) - // )); - // assert_ok!(DIDModule::create_vc_fingerprint( - // Origin::signed(1), - // public_key, - // hash, - // Some(true) - // )); + )); }); } -#[test] -fn create_vc_exists() { - let public_key = vec![ - 0, 1, 217, 200, 51, 244, 152, 125, 173, 92, 30, 224, 60, 141, 221, 44, 65, 132, 45, 94, - 199, 150, 116, 108, 95, 18, 118, 246, 86, 167, 64, 132, 76, - ]; - - let hash = "Hash".as_bytes().to_vec(); +#[rstest] +fn create_vc_exists(public_key: Vec, vc_hash: Vec) { new_test_ext().execute_with(|| { DIDModule::create_vc_fingerprint( Origin::signed(1), public_key.clone(), - hash.clone(), + vc_hash.clone(), Some(true), ).ok(); assert_err!( - DIDModule::create_vc_fingerprint(Origin::signed(1), public_key, hash, Some(true)), + DIDModule::create_vc_fingerprint(Origin::signed(1), public_key, vc_hash, Some(true)), DispatchError::Module { index: 1, error: 4, @@ -68,68 +99,43 @@ fn create_vc_exists() { }); } -#[test] -fn create_did() { - let did_document = r#"{ - "@context": [ - "https://www.w3.org/ns/did/v1", - "https://w3id.org/security/suites/ed25519-2020/v1" - ] - "id": "did:trackback.dev:123456789abcdefghi", - "authentication": [{ - "id": "did:trackback.dev:123456789abcdefghi#keys-1", - "type": "Ed25519VerificationKey2020", - "controller": "did:trackback.dev:123456789abcdefghi", - "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV" - }] - }"#; - - let did_hash = Blake2Hasher::hash(did_document.as_ref()) - .as_bytes() - .to_vec(); +#[rstest] +fn create_did(did_document_metadata: Option>, did_resolution_metadata: Option>, did_document: &'static str, did_hash: Vec) { new_test_ext().execute_with(|| { assert_ok!(DIDModule::insert_did_document( Origin::signed(1), did_document.as_bytes().to_vec(), + did_document_metadata, + did_resolution_metadata, + 10, did_hash )); }); } -#[test] -fn create_an_existing_did() { - let did_document = r#"{ - "@context": [ - "https://www.w3.org/ns/did/v1", - "https://w3id.org/security/suites/ed25519-2020/v1" - ] - "id": "did:trackback.dev:123456789abcdefghi", - "authentication": [{ - "id": "did:trackback.dev:123456789abcdefghi#keys-1", - "type": "Ed25519VerificationKey2020", - "controller": "did:trackback.dev:123456789abcdefghi", - "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV" - }] - }"#; - - let did_hash = Blake2Hasher::hash(did_document.as_ref()) - .as_bytes() - .to_vec(); +#[rstest] +fn create_an_existing_did(did_document_metadata: Option>, did_resolution_metadata: Option>, did_document: &'static str, did_hash: Vec) { new_test_ext().execute_with(|| { DIDModule::insert_did_document( Origin::signed(1), - did_document.clone().as_bytes().to_vec(), - did_hash.clone(), + did_document.as_bytes().to_vec(), + did_document_metadata.clone(), + did_resolution_metadata.clone(), + 10, + did_hash.clone() ).ok(); assert_err!( - DIDModule::insert_did_document( - Origin::signed(1), - did_document.as_bytes().to_vec(), - did_hash - ), + DIDModule::insert_did_document( + Origin::signed(1), + did_document.as_bytes().to_vec(), + did_document_metadata, + did_resolution_metadata, + 10, + did_hash.clone() + ), DispatchError::Module { index: 1, error: 0, @@ -139,31 +145,17 @@ fn create_an_existing_did() { }); } -#[test] -fn revoke_a_did() { - let did_document = r#"{ - "@context": [ - "https://www.w3.org/ns/did/v1", - "https://w3id.org/security/suites/ed25519-2020/v1" - ] - "id": "did:trackback.dev:123456789abcdefghi", - "authentication": [{ - "id": "did:trackback.dev:123456789abcdefghi#keys-1", - "type": "Ed25519VerificationKey2020", - "controller": "did:trackback.dev:123456789abcdefghi", - "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV" - }] - }"#; - - let did_hash = Blake2Hasher::hash(did_document.as_ref()) - .as_bytes() - .to_vec(); +#[rstest] +fn revoke_a_did(did_document_metadata: Option>, did_resolution_metadata: Option>, did_document: &'static str, did_hash: Vec) { new_test_ext().execute_with(|| { DIDModule::insert_did_document( Origin::signed(1), - did_document.clone().as_bytes().to_vec(), - did_hash.clone(), + did_document.as_bytes().to_vec(), + did_document_metadata.clone(), + did_resolution_metadata.clone(), + 10, + did_hash.clone() ).ok(); assert_ok!( @@ -175,32 +167,19 @@ fn revoke_a_did() { }); } -#[test] -fn revoke_non_existing_did() { - let did_document = r#"{ - "@context": [ - "https://www.w3.org/ns/did/v1", - "https://w3id.org/security/suites/ed25519-2020/v1" - ] - "id": "did:trackback.dev:123456789abcdefghi", - "authentication": [{ - "id": "did:trackback.dev:123456789abcdefghi#keys-1", - "type": "Ed25519VerificationKey2020", - "controller": "did:trackback.dev:123456789abcdefghi", - "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV" - }] - }"#; - - let did_hash = Blake2Hasher::hash(did_document.as_ref()) - .as_bytes() - .to_vec(); +#[rstest] +fn revoke_non_existing_did(did_document_metadata: Option>, did_resolution_metadata: Option>, did_document: &'static str, did_hash: Vec) { new_test_ext().execute_with(|| { DIDModule::insert_did_document( Origin::signed(1), - did_document.clone().as_bytes().to_vec(), - did_hash.clone(), + did_document.as_bytes().to_vec(), + did_document_metadata.clone(), + did_resolution_metadata.clone(), + 10, + did_hash.clone() ).ok(); + DIDModule::revoke_did( Origin::signed(1), did_hash.clone() @@ -219,26 +198,9 @@ fn revoke_non_existing_did() { }); } -#[test] -fn revoke_a_revoked_did() { - let did_document = r#"{ - "@context": [ - "https://www.w3.org/ns/did/v1", - "https://w3id.org/security/suites/ed25519-2020/v1" - ] - "id": "did:trackback.dev:123456789abcdefghi", - "authentication": [{ - "id": "did:trackback.dev:123456789abcdefghi#keys-1", - "type": "Ed25519VerificationKey2020", - "controller": "did:trackback.dev:123456789abcdefghi", - "publicKeyMultibase": "zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV" - }] - }"#; - - let did_hash = Blake2Hasher::hash(did_document.as_ref()) - .as_bytes() - .to_vec(); - +#[rstest] +fn revoke_a_revoked_did(did_hash: Vec) +{ new_test_ext().execute_with(|| { assert_err!( DIDModule::revoke_did(