Skip to content

Commit

Permalink
Add validation of DID Document (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar authored May 13, 2024
2 parents cf279f0 + 1210bc3 commit eb44d34
Show file tree
Hide file tree
Showing 12 changed files with 535 additions and 69 deletions.
11 changes: 9 additions & 2 deletions vdr/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 vdr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ web3 = { version = "0.20.0", optional = true }
web-sys = { version = "0.3.64", optional = true, features = ["Window"] }
web3-wasm = { package = "web3", version = "0.20.0", default-features = false, features = ["wasm", "http", "http-tls"], optional = true }
jsonrpc-core = "18.0.0"
regex-lite = "0.1.5"

[dev-dependencies]
rstest = "0.18.2"
Expand Down
30 changes: 15 additions & 15 deletions vdr/src/contracts/did/did_ethr_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,12 @@ pub mod test {
contracts::{
did::types::{
did::DID,
did_doc::test::{SERVICE_ENDPOINT, SERVICE_TYPE},
did_doc::test::SERVICE_ENDPOINT,
did_doc_attribute::{
PublicKeyAttribute, PublicKeyPurpose, PublicKeyType, ServiceAttribute,
},
},
ServiceEndpoint,
ServiceEndpoint, ServiceType,
},
};

Expand All @@ -656,7 +656,7 @@ pub mod test {

pub fn service() -> DidDocAttribute {
DidDocAttribute::Service(ServiceAttribute {
type_: SERVICE_TYPE.to_string(),
type_: ServiceType::LinkedDomains,
service_endpoint: ServiceEndpoint::String(SERVICE_ENDPOINT.to_string()),
})
}
Expand Down Expand Up @@ -826,13 +826,13 @@ pub mod test {
data: vec![
122, 212, 176, 164, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 226, 219, 108,
141, 198, 198, 129, 187, 93, 106, 209, 33, 161, 7, 243, 0, 233, 178, 181, 100,
105, 100, 47, 115, 118, 99, 47, 83, 101, 114, 118, 105, 99, 101, 0, 0, 0, 0, 0,
105, 100, 47, 115, 118, 99, 47, 76, 105, 110, 107, 101, 100, 68, 111, 109, 97,
105, 110, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 18, 104, 116, 116, 112, 58, 47, 47, 101, 120, 97, 109, 112, 108, 101,
46, 99, 111, 109, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3, 232, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 18, 104, 116, 116, 112, 58, 47, 47, 101, 120, 97, 109,
112, 108, 101, 46, 99, 111, 109, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
signature: None,
hash: None,
Expand Down Expand Up @@ -896,12 +896,12 @@ pub mod test {
data: vec![
0, 192, 35, 218, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 226, 219, 108, 141,
198, 198, 129, 187, 93, 106, 209, 33, 161, 7, 243, 0, 233, 178, 181, 100, 105,
100, 47, 115, 118, 99, 47, 83, 101, 114, 118, 105, 99, 101, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 104, 116,
116, 112, 58, 47, 47, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 47, 115, 118, 99, 47, 76, 105, 110, 107, 101, 100, 68, 111, 109, 97, 105,
110, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18,
104, 116, 116, 112, 58, 47, 47, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111,
109, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
signature: None,
hash: None,
Expand Down
59 changes: 33 additions & 26 deletions vdr/src/contracts/did/did_indy_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub async fn build_create_did_transaction(
did: &DID,
did_doc: &DidDocument,
) -> VdrResult<Transaction> {
did_doc.validate()?;
TransactionBuilder::new()
.set_contract(CONTRACT_NAME)
.set_method(METHOD_CREATE_DID)
Expand Down Expand Up @@ -70,6 +71,7 @@ pub async fn build_create_did_endorsing_data(
did: &DID,
did_doc: &DidDocument,
) -> VdrResult<TransactionEndorsingData> {
did_doc.validate()?;
TransactionEndorsingDataBuilder::new()
.set_contract(CONTRACT_NAME)
.set_identity(&Address::try_from(did)?)
Expand Down Expand Up @@ -98,6 +100,7 @@ pub async fn build_update_did_transaction(
did: &DID,
did_doc: &DidDocument,
) -> VdrResult<Transaction> {
did_doc.validate()?;
TransactionBuilder::new()
.set_contract(CONTRACT_NAME)
.set_method(METHOD_UPDATE_DID)
Expand Down Expand Up @@ -125,6 +128,7 @@ pub async fn build_update_did_endorsing_data(
did: &DID,
did_doc: &DidDocument,
) -> VdrResult<TransactionEndorsingData> {
did_doc.validate()?;
TransactionEndorsingDataBuilder::new()
.set_contract(CONTRACT_NAME)
.set_identity(&Address::try_from(did)?)
Expand Down Expand Up @@ -220,10 +224,14 @@ pub async fn build_resolve_did_transaction(
#[logfn(Info)]
#[logfn_inputs(Debug)]
pub fn parse_resolve_did_result(client: &LedgerClient, bytes: &[u8]) -> VdrResult<DidRecord> {
TransactionParser::new()
let did_record = TransactionParser::new()
.set_contract(CONTRACT_NAME)
.set_method(METHOD_RESOLVE_DID)
.parse::<DidRecord>(client, bytes)
.parse::<DidRecord>(client, bytes)?;

did_record.document.validate()?;

Ok(did_record)
}

#[cfg(test)]
Expand Down Expand Up @@ -260,7 +268,7 @@ pub mod test {
198, 198, 129, 187, 93, 106, 209, 33, 161, 7, 243, 0, 233, 178, 181, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2, 22, 123, 34, 64, 99, 111, 110, 116, 101, 120, 116, 34,
0, 0, 0, 0, 0, 0, 0, 2, 16, 123, 34, 64, 99, 111, 110, 116, 101, 120, 116, 34,
58, 91, 34, 104, 116, 116, 112, 115, 58, 47, 47, 119, 119, 119, 46, 119, 51,
46, 111, 114, 103, 47, 110, 115, 47, 100, 105, 100, 47, 118, 49, 34, 93, 44,
34, 105, 100, 34, 58, 34, 100, 105, 100, 58, 105, 110, 100, 121, 98, 101, 115,
Expand All @@ -280,18 +288,17 @@ pub mod test {
100, 121, 98, 101, 115, 117, 58, 100, 105, 100, 58, 101, 116, 104, 114, 58,
116, 101, 115, 116, 110, 101, 116, 58, 48, 120, 102, 48, 101, 50, 100, 98, 54,
99, 56, 100, 99, 54, 99, 54, 56, 49, 98, 98, 53, 100, 54, 97, 100, 49, 50, 49,
97, 49, 48, 55, 102, 51, 48, 48, 101, 57, 98, 50, 98, 53, 35, 75, 69, 89, 45,
49, 34, 44, 34, 112, 117, 98, 108, 105, 99, 75, 101, 121, 77, 117, 108, 116,
105, 98, 97, 115, 101, 34, 58, 34, 122, 65, 75, 74, 80, 51, 102, 55, 66, 68,
54, 87, 52, 105, 87, 69, 81, 57, 106, 119, 110, 100, 86, 84, 67, 66, 113, 56,
117, 97, 50, 85, 116, 116, 56, 69, 69, 106, 74, 54, 86, 120, 115, 102, 34, 125,
93, 44, 34, 97, 117, 116, 104, 101, 110, 116, 105, 99, 97, 116, 105, 111, 110,
34, 58, 91, 34, 100, 105, 100, 58, 105, 110, 100, 121, 98, 101, 115, 117, 58,
100, 105, 100, 58, 101, 116, 104, 114, 58, 116, 101, 115, 116, 110, 101, 116,
58, 48, 120, 102, 48, 101, 50, 100, 98, 54, 99, 56, 100, 99, 54, 99, 54, 56,
49, 98, 98, 53, 100, 54, 97, 100, 49, 50, 49, 97, 49, 48, 55, 102, 51, 48, 48,
101, 57, 98, 50, 98, 53, 35, 75, 69, 89, 45, 49, 34, 93, 125, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
97, 49, 48, 55, 102, 51, 48, 48, 101, 57, 98, 50, 98, 53, 34, 44, 34, 112, 117,
98, 108, 105, 99, 75, 101, 121, 77, 117, 108, 116, 105, 98, 97, 115, 101, 34,
58, 34, 122, 65, 75, 74, 80, 51, 102, 55, 66, 68, 54, 87, 52, 105, 87, 69, 81,
57, 106, 119, 110, 100, 86, 84, 67, 66, 113, 56, 117, 97, 50, 85, 116, 116, 56,
69, 69, 106, 74, 54, 86, 120, 115, 102, 34, 125, 93, 44, 34, 97, 117, 116, 104,
101, 110, 116, 105, 99, 97, 116, 105, 111, 110, 34, 58, 91, 34, 100, 105, 100,
58, 105, 110, 100, 121, 98, 101, 115, 117, 58, 100, 105, 100, 58, 101, 116,
104, 114, 58, 116, 101, 115, 116, 110, 101, 116, 58, 48, 120, 102, 48, 101, 50,
100, 98, 54, 99, 56, 100, 99, 54, 99, 54, 56, 49, 98, 98, 53, 100, 54, 97, 100,
49, 50, 49, 97, 49, 48, 55, 102, 51, 48, 48, 101, 57, 98, 50, 98, 53, 35, 75,
69, 89, 45, 49, 34, 93, 125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
signature: None,
hash: None,
Expand Down Expand Up @@ -343,7 +350,7 @@ pub mod test {
0, 0, 0, 0, 101, 207, 153, 152, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 210, 123, 34, 64,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 204, 123, 34, 64,
99, 111, 110, 116, 101, 120, 116, 34, 58, 91, 34, 104, 116, 116, 112, 115, 58, 47,
47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47, 110, 115, 47, 100, 105, 100,
47, 118, 49, 34, 93, 44, 34, 105, 100, 34, 58, 34, 100, 105, 100, 58, 105, 110,
Expand All @@ -360,16 +367,16 @@ pub mod test {
101, 114, 34, 58, 34, 100, 105, 100, 58, 105, 110, 100, 121, 98, 101, 115, 117, 58,
48, 120, 102, 48, 101, 50, 100, 98, 54, 99, 56, 100, 99, 54, 99, 54, 56, 49, 98,
98, 53, 100, 54, 97, 100, 49, 50, 49, 97, 49, 48, 55, 102, 51, 48, 48, 101, 57, 98,
50, 98, 53, 35, 75, 69, 89, 45, 49, 34, 44, 34, 112, 117, 98, 108, 105, 99, 75,
101, 121, 77, 117, 108, 116, 105, 98, 97, 115, 101, 34, 58, 34, 122, 65, 75, 74,
80, 51, 102, 55, 66, 68, 54, 87, 52, 105, 87, 69, 81, 57, 106, 119, 110, 100, 86,
84, 67, 66, 113, 56, 117, 97, 50, 85, 116, 116, 56, 69, 69, 106, 74, 54, 86, 120,
115, 102, 34, 125, 93, 44, 34, 97, 117, 116, 104, 101, 110, 116, 105, 99, 97, 116,
105, 111, 110, 34, 58, 91, 34, 100, 105, 100, 58, 105, 110, 100, 121, 98, 101, 115,
117, 58, 48, 120, 102, 48, 101, 50, 100, 98, 54, 99, 56, 100, 99, 54, 99, 54, 56,
49, 98, 98, 53, 100, 54, 97, 100, 49, 50, 49, 97, 49, 48, 55, 102, 51, 48, 48, 101,
57, 98, 50, 98, 53, 35, 75, 69, 89, 45, 49, 34, 93, 125, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
50, 98, 53, 34, 44, 34, 112, 117, 98, 108, 105, 99, 75, 101, 121, 77, 117, 108,
116, 105, 98, 97, 115, 101, 34, 58, 34, 122, 65, 75, 74, 80, 51, 102, 55, 66, 68,
54, 87, 52, 105, 87, 69, 81, 57, 106, 119, 110, 100, 86, 84, 67, 66, 113, 56, 117,
97, 50, 85, 116, 116, 56, 69, 69, 106, 74, 54, 86, 120, 115, 102, 34, 125, 93, 44,
34, 97, 117, 116, 104, 101, 110, 116, 105, 99, 97, 116, 105, 111, 110, 34, 58, 91,
34, 100, 105, 100, 58, 105, 110, 100, 121, 98, 101, 115, 117, 58, 48, 120, 102, 48,
101, 50, 100, 98, 54, 99, 56, 100, 99, 54, 99, 54, 56, 49, 98, 98, 53, 100, 54, 97,
100, 49, 50, 49, 97, 49, 48, 55, 102, 51, 48, 48, 101, 57, 98, 50, 98, 53, 35, 75,
69, 89, 45, 49, 34, 93, 125, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,
];
let parsed_did_doc = parse_resolve_did_result(&client, &data).unwrap();
assert_eq!(did_doc(TEST_ACCOUNT.as_ref()), parsed_did_doc.document);
Expand Down
2 changes: 2 additions & 0 deletions vdr/src/contracts/did/did_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ mod ethr {
None,
None,
None,
None,
);

match delegate_type {
Expand Down Expand Up @@ -343,6 +344,7 @@ mod ethr {
key.public_key_hex.as_deref(),
key.public_key_base58.as_deref(),
key.public_key_base64.as_deref(),
None,
);

match key.purpose {
Expand Down
30 changes: 30 additions & 0 deletions vdr/src/contracts/did/types/did.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
use crate::{types::ContractOutput, ContractParam, VdrError, VdrResult};
use once_cell::sync::Lazy;
use regex_lite::Regex;
use serde_derive::{Deserialize, Serialize};

pub const DID_PREFIX: &str = "did";

const DID_SYNTAX: &str = r"did:(?:indybesu|ethr):(?:[a-zA-Z0-9]+:)*0x[a-fA-F0-9]{40}";
const PATH: &str = r"\/[^#?]*";
const QUERY: &str = r"[?][^#]*";
const FRAGMENT: &str = r"[#].*";

static DID_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(&format!("^{DID_SYNTAX}$")).unwrap());

pub static DID_URL_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(&format!(
"^{DID_SYNTAX}(?:{PATH})?(?:{QUERY})?(?:{FRAGMENT})?$"
))
.unwrap()
});

pub static RELATIVE_DID_URL_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(&format!("^(?:{PATH})?(?:{QUERY})?(?:{FRAGMENT})?$")).unwrap());

/// Wrapper structure for DID
#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
pub struct DID(String);
Expand All @@ -19,6 +38,17 @@ impl DID {
pub fn without_network(&self) -> VdrResult<DID> {
Ok(ParsedDid::try_from(self)?.as_short_did())
}

pub(crate) fn validate(&self) -> VdrResult<()> {
if !DID_REGEX.is_match(&self.0) {
return Err(VdrError::InvalidDidDocument(format!(
"Incorrect DID: {}",
&self.0
)));
};

Ok(())
}
}

impl From<&str> for DID {
Expand Down
Loading

0 comments on commit eb44d34

Please sign in to comment.