Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable optional verification_method for DID Document #233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions crates/web5/src/dids/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct Document {
pub controller: Option<Vec<String>>,
#[serde(rename = "alsoKnownAs", skip_serializing_if = "Option::is_none")]
pub also_known_as: Option<Vec<String>>,
#[serde(rename = "verificationMethod")]
pub verification_method: Vec<VerificationMethod>,
#[serde(rename = "verificationMethod", skip_serializing_if = "Option::is_none")]
pub verification_method: Option<Vec<VerificationMethod>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub authentication: Option<Vec<String>>,
#[serde(rename = "assertionMethod", skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -132,6 +132,8 @@ impl Document {
}
VerificationMethodType::VerificationMethod => {
self.verification_method
.clone()
.ok_or(DocumentError::VerificationMethodNotFound)?
.first()
.cloned()
.ok_or(DocumentError::VerificationMethodNotFound)?
Expand All @@ -143,6 +145,8 @@ impl Document {

let verification_method = self
.verification_method
.clone()
.ok_or(DocumentError::VerificationMethodNotFound)?
.iter()
.find(|method| method.id == *key_id)
.cloned()
Expand Down Expand Up @@ -171,7 +175,7 @@ mod tests {
fn test_get_verification_method() {
let document = Document {
id: "did:example:123".to_string(),
verification_method: vec![
verification_method: Some(vec![
VerificationMethod {
id: "did:example:123#key1".to_string(),
r#type: "JsonWebKey2020".to_string(),
Expand All @@ -184,7 +188,7 @@ mod tests {
controller: "did:example:123".to_string(),
public_key_jwk: Jwk::default(),
},
],
]),
authentication: Some(vec!["did:example:123#key1".to_string()]),
..Default::default()
};
Expand Down
54 changes: 29 additions & 25 deletions crates/web5/src/dids/methods/dht/document_packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ impl Document {

// 1.1 Add identity key verification method
let identity_key_id = format!("{}#0", did_uri);
let id_key_vm = self
.verification_method
.iter()
.find(|vm| vm.id == identity_key_id);
let id_key_vm = match self.verification_method {
None => None,
Some(ref vms) => vms.iter().find(|vm| vm.id == identity_key_id),
};
match id_key_vm {
None => {
return Err(DocumentPacketError::DocumentError(
Expand All @@ -104,22 +104,26 @@ impl Document {

// 1.2 Add all other verification methods
let mut idx = 1;
self.verification_method
.iter()
.try_for_each(|vm| -> Result<(), DocumentPacketError> {
// skip identity key because we already added it
if vm.id == identity_key_id {
return Ok(());
}

let vm_record = vm.to_resource_record(did_uri, idx)?.to_owned();
answers.push(vm_record);
root_record.vm.push(idx);
vm_id_to_idx.insert(vm.id.clone(), idx);

idx += 1;
Ok(())
})?;
match self.verification_method {
None => {}
Some(ref vms) => {
vms.iter()
.try_for_each(|vm| -> Result<(), DocumentPacketError> {
// skip identity key because we already added it
if vm.id == identity_key_id {
return Ok(());
}

let vm_record = vm.to_resource_record(did_uri, idx)?.to_owned();
answers.push(vm_record);
root_record.vm.push(idx);
vm_id_to_idx.insert(vm.id.clone(), idx);

idx += 1;
Ok(())
})?;
}
}

// 2. Add verification relationships to root_record
// 2.1 Add assertion methods to root_record
Expand Down Expand Up @@ -310,7 +314,7 @@ impl Document {
context: None,
controller,
also_known_as,
verification_method: verification_methods,
verification_method: Some(verification_methods),
authentication,
assertion_method,
key_agreement,
Expand Down Expand Up @@ -369,7 +373,7 @@ mod tests {
fn test_to_and_from_packet_full_featured() {
let did_uri = "did:dht:123";

let verification_method1 = generate_identity_key_vm(&did_uri);
let verification_method1 = generate_identity_key_vm(did_uri);
let verification_method2 = generate_additional_vm(did_uri);

let service = Service {
Expand Down Expand Up @@ -403,7 +407,7 @@ mod tests {
verification_method2.id.clone(),
]),
service: Some(vec![service]),
verification_method: vec![verification_method1, verification_method2],
verification_method: Some(vec![verification_method1, verification_method2]),
};

let packet = document
Expand All @@ -420,10 +424,10 @@ mod tests {
fn test_to_and_from_packet() {
let did_uri = "did:dht:123";

let verification_method = generate_identity_key_vm(&did_uri);
let verification_method = generate_identity_key_vm(did_uri);
let document = Document {
id: did_uri.to_string(),
verification_method: vec![verification_method],
verification_method: Some(vec![verification_method]),
..Default::default()
};

Expand Down
8 changes: 5 additions & 3 deletions crates/web5/src/dids/methods/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Create<DidJwkCreateOptions> for DidJwk {

let document = Document {
id: uri.clone(),
verification_method: vec![verification_method.clone()],
verification_method: Some(vec![verification_method.clone()]),
authentication: Some(vec![verification_method_id.clone()]),
assertion_method: Some(vec![verification_method_id.clone()]),
capability_invocation: Some(vec![verification_method_id.clone()]),
Expand Down Expand Up @@ -123,7 +123,9 @@ mod tests {
fn create_produces_correct_did_document() {
let bearer_did = create_did_jwk();

let verification_method_id = bearer_did.document.verification_method[0].id.clone();
let verification_method_id = bearer_did.document.verification_method.unwrap()[0]
.id
.clone();
assert_eq!(
bearer_did.document.authentication.unwrap()[0],
verification_method_id
Expand Down Expand Up @@ -161,7 +163,7 @@ mod tests {
let did_document = result.did_document.unwrap();
assert_eq!(did_document.id, bearer_did.identifier.uri);

let verification_method_id = did_document.verification_method[0].id.clone();
let verification_method_id = did_document.verification_method.unwrap()[0].id.clone();
assert_eq!(
did_document.authentication.unwrap()[0],
verification_method_id
Expand Down
7 changes: 4 additions & 3 deletions crates/web5/src/dids/methods/spruce_mappers/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Document {
also_known_as: spruce_document
.also_known_as
.map(|aka| aka.into_iter().map(|uri| uri.to_string()).collect()),
verification_method: verification_methods,
verification_method: Some(verification_methods),
authentication,
assertion_method,
key_agreement,
Expand Down Expand Up @@ -241,11 +241,12 @@ mod tests {
"https://w3id.org/security/suites/jws-2020/v1".to_string()
])
);
let vm = &document.verification_method.unwrap();
assert_eq!(document.controller, None);
assert_eq!(document.also_known_as, None);
assert_eq!(document.verification_method.len(), 1);
assert_eq!(vm.len(), 1);

let vm = &document.verification_method[0];
let vm = &vm[0];
assert_eq!(vm.id, format!("{}#0", expected_did_uri));
assert_eq!(vm.r#type, "JsonWebKey2020".to_string());
assert_eq!(vm.controller, expected_did_uri);
Expand Down
42 changes: 32 additions & 10 deletions crates/web5/src/jws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "EdDSA".to_string(),
Expand Down Expand Up @@ -200,7 +202,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "EdDSA".to_string(),
Expand Down Expand Up @@ -251,7 +255,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "".to_string(),
Expand Down Expand Up @@ -292,7 +298,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "EdDSA".to_string(),
Expand Down Expand Up @@ -333,7 +341,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "UNSUPPORTED_ALG".to_string(),
Expand Down Expand Up @@ -374,7 +384,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "EdDSA".to_string(),
Expand Down Expand Up @@ -415,7 +427,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "EdDSA".to_string(),
Expand Down Expand Up @@ -456,7 +470,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "EdDSA".to_string(),
Expand Down Expand Up @@ -498,7 +514,9 @@ mod tests {
)
.expect("failed to create bearer did");

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();

let header = JwsHeader {
alg: "EdDSA".to_string(),
Expand All @@ -521,7 +539,11 @@ mod tests {
},
)
.expect("failed to create bearer did");
let invalid_key_id = invalid_bearer_did.document.verification_method[0]
let invalid_key_id = invalid_bearer_did
.document
.verification_method
.clone()
.unwrap()[0]
.id
.clone();

Expand Down
6 changes: 4 additions & 2 deletions crates/web5/src/jwt/jws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ mod tests {
..Default::default()
};

let key_id = bearer_did.document.verification_method[0].id.clone();
let key_id = bearer_did.document.verification_method.clone().unwrap()[0]
.id
.clone();
let jwt = Jwt::sign(
&bearer_did,
&KeySelector::KeyId {
Expand All @@ -115,7 +117,7 @@ mod tests {
assert_eq!("JWT".to_string(), jwt_decoded.header.typ);
assert_eq!(key_id, jwt_decoded.header.kid);
assert_eq!(
bearer_did.document.verification_method[0]
bearer_did.document.verification_method.clone().unwrap()[0]
.public_key_jwk
.alg,
jwt_decoded.header.alg
Expand Down
Loading