Skip to content

Commit

Permalink
Expose iteration over verification relationship fields (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulmth authored Sep 20, 2022
1 parent 190b7b6 commit f0b69a3
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 50 deletions.
16 changes: 11 additions & 5 deletions bindings/wasm/src/did/wasm_core_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,23 @@ impl WasmCoreDocument {
// Verification Methods
// ===========================================================================

/// Returns a list of all {@link CoreVerificationMethod} in the DID Document.
/// Returns a list of all {@link CoreVerificationMethod} in the DID Document,
/// whose verification relationship matches `scope`.
///
/// If `scope` is not set, a list over the **embedded** methods is returned.
#[wasm_bindgen]
pub fn methods(&self) -> ArrayCoreVerificationMethod {
self
pub fn methods(&self, scope: Option<RefMethodScope>) -> Result<ArrayCoreVerificationMethod> {
let scope: Option<MethodScope> = scope.map(|js| js.into_serde().wasm_result()).transpose()?;
let methods = self
.0
.methods()
.methods(scope)
.into_iter()
.cloned()
.map(WasmCoreVerificationMethod::from)
.map(JsValue::from)
.collect::<js_sys::Array>()
.unchecked_into::<ArrayCoreVerificationMethod>()
.unchecked_into::<ArrayCoreVerificationMethod>();
Ok(methods)
}

/// Returns an array of all verification relationships.
Expand Down
16 changes: 11 additions & 5 deletions bindings/wasm/src/iota/iota_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,23 @@ impl WasmIotaDocument {
// Verification Methods
// ===========================================================================

/// Returns a list of all {@link IotaVerificationMethod} in the DID Document.
/// Returns a list of all {@link IotaVerificationMethod} in the DID Document,
/// whose verification relationship matches `scope`.
///
/// If `scope` is not set, a list over the **embedded** methods is returned.
#[wasm_bindgen]
pub fn methods(&self) -> ArrayIotaVerificationMethods {
self
pub fn methods(&self, scope: Option<RefMethodScope>) -> Result<ArrayIotaVerificationMethods> {
let scope: Option<MethodScope> = scope.map(|js| js.into_serde().wasm_result()).transpose()?;
let methods = self
.0
.methods()
.methods(scope)
.into_iter()
.cloned()
.map(WasmIotaVerificationMethod::from)
.map(JsValue::from)
.collect::<js_sys::Array>()
.unchecked_into::<ArrayIotaVerificationMethods>()
.unchecked_into::<ArrayIotaVerificationMethods>();
Ok(methods)
}

/// Adds a new `method` to the document in the given `scope`.
Expand Down
4 changes: 2 additions & 2 deletions identity_account/src/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async fn test_account_autopublish() {

let doc = account.document();

assert_eq!(doc.methods().count(), 1);
assert_eq!(doc.methods().len(), 1);
assert_eq!(doc.service().len(), 2);

for service in ["my-service", "my-other-service"] {
Expand Down Expand Up @@ -213,7 +213,7 @@ async fn test_account_autopublish() {
let doc = account.document();

assert_eq!(doc.service().len(), 0);
assert_eq!(doc.methods().count(), 2);
assert_eq!(doc.methods().len(), 2);

for method in ["sign-0", "new-method"] {
assert!(doc.resolve_method(method, None).is_some());
Expand Down
8 changes: 4 additions & 4 deletions identity_account/src/tests/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn test_create_identity() -> Result<()> {
let method: &IotaVerificationMethod = document.resolve_method(expected_fragment, None).unwrap();

assert_eq!(document.core_document().verification_relationships().count(), 1);
assert_eq!(document.core_document().methods().count(), 1);
assert_eq!(document.core_document().methods(None).len(), 1);

let location: KeyLocation = KeyLocation::from_verification_method(method).unwrap();

Expand Down Expand Up @@ -204,7 +204,7 @@ async fn test_create_method_content_generate() -> Result<()> {

// Still only the default relationship.
assert_eq!(document.core_document().verification_relationships().count(), 1);
assert_eq!(document.core_document().methods().count(), 2);
assert_eq!(document.core_document().methods(None).len(), 2);

let location: KeyLocation = KeyLocation::from_verification_method(method).unwrap();

Expand Down Expand Up @@ -291,7 +291,7 @@ async fn test_create_scoped_method() -> Result<()> {

assert_eq!(document.core_document().verification_relationships().count(), 2);

assert_eq!(document.core_document().methods().count(), 2);
assert_eq!(document.core_document().methods(None).len(), 2);

let core_doc = document.core_document();

Expand Down Expand Up @@ -601,7 +601,7 @@ async fn test_delete_method() -> Result<()> {
// Still only the default relationship.
assert_eq!(document.core_document().verification_relationships().count(), 1);

assert_eq!(document.core_document().methods().count(), 1);
assert_eq!(document.core_document().methods(None).len(), 1);

// Ensure the key still exists in storage.
assert!(account
Expand Down
16 changes: 8 additions & 8 deletions identity_credential/src/validator/credential_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ mod tests {
issuer_doc
.signer(issuer_key.private())
.options(ProofOptions::default())
.method(issuer_doc.methods().next().unwrap().id())
.method(issuer_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential)
.unwrap();

Expand Down Expand Up @@ -478,7 +478,7 @@ mod tests {
issuer_doc
.signer(issuer_key.private())
.options(ProofOptions::default())
.method(issuer_doc.methods().next().unwrap().id())
.method(issuer_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential)
.unwrap();

Expand Down Expand Up @@ -516,7 +516,7 @@ mod tests {
issuer_doc
.signer(issuer_key.private())
.options(ProofOptions::default())
.method(issuer_doc.methods().next().unwrap().id())
.method(issuer_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential)
.unwrap();

Expand All @@ -542,7 +542,7 @@ mod tests {
issuer_doc
.signer(issuer_key.private())
.options(ProofOptions::default())
.method(issuer_doc.methods().next().unwrap().id())
.method(issuer_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential)
.unwrap();

Expand Down Expand Up @@ -589,7 +589,7 @@ mod tests {
issuer_doc
.signer(other_keys.private())
.options(ProofOptions::default())
.method(issuer_doc.methods().next().unwrap().id())
.method(issuer_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential)
.unwrap();

Expand Down Expand Up @@ -815,7 +815,7 @@ mod tests {
issuer_doc
.signer(issuer_key.private())
.options(ProofOptions::default())
.method(issuer_doc.methods().next().unwrap().id())
.method(issuer_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential)
.unwrap();
// the credential now has no credential subjects which is not semantically correct
Expand Down Expand Up @@ -855,7 +855,7 @@ mod tests {
issuer_doc
.signer(issuer_key.private())
.options(ProofOptions::default())
.method(issuer_doc.methods().next().unwrap().id())
.method(issuer_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential)
.unwrap();
// the credential now has no credential subjects which is not semantically correct
Expand Down Expand Up @@ -894,7 +894,7 @@ mod tests {
issuer_doc
.signer(issuer_key.private())
.options(ProofOptions::default())
.method(issuer_doc.methods().next().unwrap().id())
.method(issuer_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential)
.unwrap();
// the credential now has no credential subjects which is not semantically correct
Expand Down
18 changes: 9 additions & 9 deletions identity_credential/src/validator/presentation_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,14 @@ mod tests {
issuer_foo_doc
.signer(issuer_foo_key.private())
.options(ProofOptions::default())
.method(issuer_foo_doc.methods().next().unwrap().id())
.method(issuer_foo_doc.methods(None).get(0).unwrap().id())
.sign(credential_foo)
.unwrap();

issuer_bar_doc
.signer(issuer_bar_key.private())
.options(ProofOptions::default())
.method(issuer_bar_doc.methods().next().unwrap().id())
.method(issuer_bar_doc.methods(None).get(0).unwrap().id())
.sign(credential_bar)
.unwrap();
setup
Expand All @@ -350,7 +350,7 @@ mod tests {
subject_foo_doc
.signer(subject_foo_key.private())
.options(ProofOptions::new().challenge("475a7984-1bb5-4c4c-a56f-822bccd46440".to_owned()))
.method(subject_foo_doc.methods().next().unwrap().id())
.method(subject_foo_doc.methods(None).get(0).unwrap().id())
.sign(&mut presentation)
.unwrap();

Expand Down Expand Up @@ -396,7 +396,7 @@ mod tests {
subject_foo_doc
.signer(subject_foo_key.private())
.options(ProofOptions::new().challenge("some challenge".to_owned()))
.method(subject_foo_doc.methods().next().unwrap().id())
.method(subject_foo_doc.methods(None).get(0).unwrap().id())
.sign(&mut presentation)
.unwrap();

Expand Down Expand Up @@ -461,7 +461,7 @@ mod tests {
subject_foo_doc
.signer(subject_foo_key.private())
.options(ProofOptions::new().challenge("some challenge".to_owned()))
.method(subject_foo_doc.methods().next().unwrap().id())
.method(subject_foo_doc.methods(None).get(0).unwrap().id())
.sign(&mut presentation)
.unwrap();

Expand Down Expand Up @@ -513,7 +513,7 @@ mod tests {
issuer_bar_doc
.signer(issuer_bar_key.private())
.options(ProofOptions::default())
.method(issuer_bar_doc.methods().next().unwrap().id())
.method(issuer_bar_doc.methods(None).get(0).unwrap().id())
.sign(&mut credential_bar)
.unwrap();

Expand All @@ -525,7 +525,7 @@ mod tests {
subject_foo_doc
.signer(subject_foo_key.private())
.options(ProofOptions::new().challenge("some challenge".to_owned()))
.method(subject_foo_doc.methods().next().unwrap().id())
.method(subject_foo_doc.methods(None).get(0).unwrap().id())
.sign(&mut presentation)
.unwrap();

Expand Down Expand Up @@ -619,7 +619,7 @@ mod tests {
subject_foo_doc
.signer(subject_foo_key.private())
.options(ProofOptions::new().challenge("some challenge".to_owned()))
.method(subject_foo_doc.methods().next().unwrap().id())
.method(subject_foo_doc.methods(None).get(0).unwrap().id())
.sign(&mut presentation)
.unwrap();

Expand Down Expand Up @@ -692,7 +692,7 @@ mod tests {
subject_foo_doc
.signer(subject_foo_key.private())
.options(ProofOptions::new().challenge("some challenge".to_owned()))
.method(subject_foo_doc.methods().next().unwrap().id())
.method(subject_foo_doc.methods(None).get(0).unwrap().id())
.sign(&mut presentation)
.unwrap();

Expand Down
80 changes: 77 additions & 3 deletions identity_did/src/document/core_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,51 @@ where
}
}

/// Returns a `Vec` of verification method references whose verification relationship matches `scope`.
///
/// If `scope` is `None`, an iterator over all **embedded** methods is returned.
pub fn methods(&self, scope: Option<MethodScope>) -> Vec<&VerificationMethod<D, U>>
where
D: DID,
{
if let Some(scope) = scope {
match scope {
MethodScope::VerificationMethod => self.verification_method().iter().collect(),
MethodScope::VerificationRelationship(MethodRelationship::AssertionMethod) => self
.assertion_method()
.iter()
.filter_map(|method_ref| self.resolve_method_ref(method_ref))
.collect(),
MethodScope::VerificationRelationship(MethodRelationship::Authentication) => self
.authentication()
.iter()
.filter_map(|method_ref| self.resolve_method_ref(method_ref))
.collect(),
MethodScope::VerificationRelationship(MethodRelationship::CapabilityDelegation) => self
.capability_delegation()
.iter()
.filter_map(|method_ref| self.resolve_method_ref(method_ref))
.collect(),
MethodScope::VerificationRelationship(MethodRelationship::CapabilityInvocation) => self
.capability_invocation()
.iter()
.filter_map(|method_ref| self.resolve_method_ref(method_ref))
.collect(),
MethodScope::VerificationRelationship(MethodRelationship::KeyAgreement) => self
.key_agreement()
.iter()
.filter_map(|method_ref| self.resolve_method_ref(method_ref))
.collect(),
}
} else {
self.all_methods().collect()
}
}

/// Returns an iterator over all embedded verification methods in the DID Document.
///
/// This excludes verification methods that are referenced by the DID Document.
pub fn methods(&self) -> impl Iterator<Item = &VerificationMethod<D, U>> {
fn all_methods(&self) -> impl Iterator<Item = &VerificationMethod<D, U>> {
fn __filter_ref<D, T>(method: &MethodRef<D, T>) -> Option<&VerificationMethod<D, T>>
where
D: DID,
Expand Down Expand Up @@ -1053,8 +1094,41 @@ mod tests {
let document: CoreDocument = document();

// Access methods by index.
assert_eq!(document.methods().next().unwrap().id().to_string(), "did:example:1234#key-1");
assert_eq!(document.methods().nth(2).unwrap().id().to_string(), "did:example:1234#key-3");
assert_eq!(document.methods(None).get(0).unwrap().id().to_string(), "did:example:1234#key-1");
assert_eq!(document.methods(None).get(2).unwrap().id().to_string(), "did:example:1234#key-3");
}

#[test]
fn test_methods_scope() {
let document: CoreDocument = document();

// VerificationMethod
let verification_methods: Vec<&VerificationMethod> = document.methods(Some(MethodScope::VerificationMethod));
assert_eq!(
verification_methods.get(0).unwrap().id().to_string(),
"did:example:1234#key-1"
);
assert_eq!(
verification_methods.get(1).unwrap().id().to_string(),
"did:example:1234#key-2"
);
assert_eq!(
verification_methods.get(2).unwrap().id().to_string(),
"did:example:1234#key-3"
);
assert_eq!(verification_methods.len(), 3);

// Authentication
let authentication: Vec<&VerificationMethod> = document.methods(Some(MethodScope::authentication()));
assert_eq!(
authentication.get(0).unwrap().id().to_string(),
"did:example:1234#auth-key"
);
assert_eq!(
authentication.get(1).unwrap().id().to_string(),
"did:example:1234#key-3"
);
assert_eq!(authentication.len(), 2);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions identity_iota_client_legacy/src/tangle/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ mod tests {
issuer_core_doc
.signer(issuer_core_key.private())
.options(ProofOptions::default())
.method(issuer_core_doc.methods().next().unwrap().id())
.method(issuer_core_doc.methods(None).get(0).unwrap().id())
.sign(credential_core)
.unwrap();
setup
Expand All @@ -436,7 +436,7 @@ mod tests {
subject_doc
.signer(subject_key.private())
.options(ProofOptions::new().challenge(challenge.clone()))
.method(subject_doc.methods().next().unwrap().id())
.method(subject_doc.methods(None).get(0).unwrap().id())
.sign(&mut presentation)
.unwrap();

Expand Down Expand Up @@ -479,7 +479,7 @@ mod tests {
subject_doc
.signer(subject_key.private())
.options(ProofOptions::new().challenge(challenge.clone()))
.method(subject_doc.methods().next().unwrap().id())
.method(subject_doc.methods(None).get(0).unwrap().id())
.sign(&mut presentation)
.unwrap();

Expand Down
Loading

0 comments on commit f0b69a3

Please sign in to comment.