From 6ff932f335a981529a1293a29f7f922f1e893bbf Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Tue, 1 Mar 2022 11:26:06 +0100 Subject: [PATCH 01/19] complete wasm bindings for `IotaDocument` --- bindings/wasm/Cargo.toml | 1 + bindings/wasm/docs/api-reference.md | 115 ++++++++- bindings/wasm/src/did/mod.rs | 1 + bindings/wasm/src/did/wasm_document.rs | 225 +++++++++++++++++- .../wasm/src/did/wasm_method_relationship.rs | 30 +++ bindings/wasm/tests/wasm.rs | 12 +- 6 files changed, 375 insertions(+), 9 deletions(-) create mode 100644 bindings/wasm/src/did/wasm_method_relationship.rs diff --git a/bindings/wasm/Cargo.toml b/bindings/wasm/Cargo.toml index 42e892160a..233eaaca92 100644 --- a/bindings/wasm/Cargo.toml +++ b/bindings/wasm/Cargo.toml @@ -20,6 +20,7 @@ futures = { version = "0.3" } js-sys = { version = "0.3" } serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0", default-features = false } +serde_repr = { version = "0.1", default-features = false } wasm-bindgen = { version = "0.2", features = ["serde-serialize"] } wasm-bindgen-futures = { version = "0.4", default-features = false } diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index 62b290e225..cdb6c40306 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -80,6 +80,8 @@ See IVerifierOptions.

KeyType
+
MethodRelationship
+
Digest
@@ -876,13 +878,22 @@ with the given Document. * [.metadataPreviousMessageId](#Document+metadataPreviousMessageId) ⇒ string * [.metadataPreviousMessageId](#Document+metadataPreviousMessageId) * [.metadataProof](#Document+metadataProof) ⇒ any + * [.controller()](#Document+controller) ⇒ [Array.<DID>](#DID) + * [.alsoKnownAs()](#Document+alsoKnownAs) ⇒ [Array.<DID>](#DID) + * [.setPropertyUnchecked(key, value)](#Document+setPropertyUnchecked) + * [.properties()](#Document+properties) ⇒ Map.<string, any> + * [.service()](#Document+service) ⇒ [Array.<Service>](#Service) * [.insertService(service)](#Document+insertService) ⇒ boolean * [.removeService(did)](#Document+removeService) + * [.methods()](#Document+methods) ⇒ [Array.<VerificationMethod>](#VerificationMethod) * [.insertMethod(method, scope)](#Document+insertMethod) * [.removeMethod(did)](#Document+removeMethod) * [.defaultSigningMethod()](#Document+defaultSigningMethod) ⇒ [VerificationMethod](#VerificationMethod) - * [.resolveMethod(query)](#Document+resolveMethod) ⇒ [VerificationMethod](#VerificationMethod) + * [.resolveMethod(query, scope)](#Document+resolveMethod) ⇒ [VerificationMethod](#VerificationMethod) + * [.resolveSigningMethod(query)](#Document+resolveSigningMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.revokeMerkleKey(query, index)](#Document+revokeMerkleKey) ⇒ boolean + * [.attachMethodRelationShips(options)](#Document+attachMethodRelationShips) + * [.detachMethodRelationShips(options)](#Document+detachMethodRelationShips) * [.signSelf(key_pair, method_query)](#Document+signSelf) * [.signDocument(document, key_pair, method_query)](#Document+signDocument) * [.signCredential(data, args, options)](#Document+signCredential) ⇒ [Credential](#Credential) @@ -897,6 +908,7 @@ with the given Document. * [.toJSON()](#Document+toJSON) ⇒ any * _static_ * [.fromVerificationMethod(method)](#Document.fromVerificationMethod) ⇒ [Document](#Document) + * [.isSigningMethodType(method_type)](#Document.isSigningMethodType) ⇒ boolean * [.verifyRootDocument(document)](#Document.verifyRootDocument) * [.diffIndex(message_id)](#Document.diffIndex) ⇒ string * [.fromJSON(json)](#Document.fromJSON) ⇒ [Document](#Document) @@ -999,6 +1011,47 @@ Sets the previous integration chain message id. Returns the `proof` object. **Kind**: instance property of [Document](#Document) + + +### document.controller() ⇒ [Array.<DID>](#DID) +Returns a list of the document controllers. + +**Kind**: instance method of [Document](#Document) + + +### document.alsoKnownAs() ⇒ [Array.<DID>](#DID) +Returns a set of the document's `alsoKnownAs`. + +**Kind**: instance method of [Document](#Document) + + +### document.setPropertyUnchecked(key, value) +Adds a custom property to the DID Document. +If the Document already has this key, the value will be overwritten. +If the value is set to `null`, the property will be removed. + +### WARNING +This method can overwrite existing properties like `id`. + +**Kind**: instance method of [Document](#Document) + +| Param | Type | +| --- | --- | +| key | string | +| value | any | + + + +### document.properties() ⇒ Map.<string, any> +Returns the custom DID Document properties. + +**Kind**: instance method of [Document](#Document) + + +### document.service() ⇒ [Array.<Service>](#Service) +Return a set of all `Service`s in the document. + +**Kind**: instance method of [Document](#Document) ### document.insertService(service) ⇒ boolean @@ -1021,6 +1074,12 @@ Remove a `Service` identified by the given `DIDUrl` from the document. | --- | --- | | did | [DIDUrl](#DIDUrl) | + + +### document.methods() ⇒ [Array.<VerificationMethod>](#VerificationMethod) +Returns a list of all [VerificationMethod](#VerificationMethod) in the DID Document. + +**Kind**: instance method of [Document](#Document) ### document.insertMethod(method, scope) @@ -1055,7 +1114,7 @@ Throws an error if no signing method is present. **Kind**: instance method of [Document](#Document) -### document.resolveMethod(query) ⇒ [VerificationMethod](#VerificationMethod) +### document.resolveMethod(query, scope) ⇒ [VerificationMethod](#VerificationMethod) Returns a copy of the first `VerificationMethod` with an `id` property matching the provided `query`. @@ -1063,6 +1122,18 @@ Throws an error if the method is not found. **Kind**: instance method of [Document](#Document) +| Param | Type | +| --- | --- | +| query | [DIDUrl](#DIDUrl) \| string | +| scope | [MethodScope](#MethodScope) \| undefined | + + + +### document.resolveSigningMethod(query) ⇒ [VerificationMethod](#VerificationMethod) +Attempts to resolve the given method query into a method capable of signing a document update. + +**Kind**: instance method of [Document](#Document) + | Param | Type | | --- | --- | | query | [DIDUrl](#DIDUrl) \| string | @@ -1077,6 +1148,31 @@ Throws an error if the method is not found. | query | [DIDUrl](#DIDUrl) \| string | | index | number | + + +### document.attachMethodRelationShips(options) +Attaches the relationship to the given method, if the method exists. + +Note: The method needs to be in the set of verification methods, +so it cannot be an embedded one. + +**Kind**: instance method of [Document](#Document) + +| Param | Type | +| --- | --- | +| options | AttachRelationshipOptions | + + + +### document.detachMethodRelationShips(options) +Detaches the given relationship from the given method, if the method exists. + +**Kind**: instance method of [Document](#Document) + +| Param | Type | +| --- | --- | +| options | AttachRelationshipOptions | + ### document.signSelf(key_pair, method_query) @@ -1261,6 +1357,17 @@ NOTE: the generated document is unsigned, see `Document::signSelf`. | --- | --- | | method | [VerificationMethod](#VerificationMethod) | + + +### Document.isSigningMethodType(method_type) ⇒ boolean +Returns whether the given [MethodType](#MethodType) can be used to sign document updates. + +**Kind**: static method of [Document](#Document) + +| Param | Type | +| --- | --- | +| method_type | [MethodType](#MethodType) | + ### Document.verifyRootDocument(document) @@ -2507,6 +2614,10 @@ Creates a new `VerifierOptions` with default options. ## KeyType **Kind**: global variable + + +## MethodRelationship +**Kind**: global variable ## Digest diff --git a/bindings/wasm/src/did/mod.rs b/bindings/wasm/src/did/mod.rs index 39a40d85d5..29ec39c3da 100644 --- a/bindings/wasm/src/did/mod.rs +++ b/bindings/wasm/src/did/mod.rs @@ -20,6 +20,7 @@ mod wasm_did_url; mod wasm_diff_message; mod wasm_document; mod wasm_document_metadata; +mod wasm_method_relationship; mod wasm_method_scope; mod wasm_method_type; mod wasm_resolved_document; diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 7fd32d73af..a32c77aa80 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -4,6 +4,7 @@ use std::str::FromStr; use identity::core::decode_b58; +use identity::core::OneOrMany; use identity::crypto::merkle_key::MerkleDigestTag; use identity::crypto::merkle_key::MerkleKey; use identity::crypto::merkle_key::Sha256; @@ -12,6 +13,7 @@ use identity::crypto::PrivateKey; use identity::crypto::PublicKey; use identity::crypto::SignatureOptions; use identity::did::verifiable::VerifiableProperties; +use identity::did::MethodRelationship; use identity::iota::Error; use identity::iota::IotaDocument; use identity::iota::IotaVerificationMethod; @@ -24,16 +26,19 @@ use crate::credential::WasmCredential; use crate::credential::WasmPresentation; use crate::crypto::KeyPair; use crate::crypto::WasmSignatureOptions; +use crate::did::wasm_method_relationship::WasmMethodRelationship; use crate::did::WasmDID; use crate::did::WasmDIDUrl; use crate::did::WasmDiffMessage; use crate::did::WasmDocumentMetadata; use crate::did::WasmMethodScope; +use crate::did::WasmMethodType; use crate::did::WasmVerificationMethod; use crate::did::WasmVerifierOptions; use crate::error::Result; use crate::error::WasmResult; use crate::service::Service; +use wasm_bindgen::JsCast; // ============================================================================= // ============================================================================= @@ -77,6 +82,12 @@ impl WasmDocument { .wasm_result() } + /// Returns whether the given {@link MethodType} can be used to sign document updates. + #[wasm_bindgen(js_name = isSigningMethodType)] + pub fn is_signing_method_type(method_type: &WasmMethodType) -> bool { + IotaDocument::is_signing_method_type(method_type.0) + } + // =========================================================================== // Properties // =========================================================================== @@ -87,10 +98,84 @@ impl WasmDocument { WasmDID(self.0.id().clone()) } + /// Returns a list of the document controllers. + #[wasm_bindgen(js_name = controller)] + pub fn controller(&self) -> ControllerArray { + match self.0.controller() { + Some(controllers) => controllers + .iter() + .cloned() + .map(WasmDID::from) + .map(JsValue::from) + .collect::() + .unchecked_into::(), + None => js_sys::Array::new().unchecked_into::(), + } + } + + /// Returns a set of the document's `alsoKnownAs`. + #[wasm_bindgen(js_name = alsoKnownAs)] + pub fn also_known_as(&self) -> ArrayString { + self + .0 + .also_known_as() + .iter() + .cloned() + .map(|url| url.to_string()) + .map(JsValue::from) + .collect::() + .unchecked_into::() + } + + /// Adds a custom property to the DID Document. + /// If the Document already has this key, the value will be overwritten. + /// If the value is set to `null`, the property will be removed. + /// + /// ### WARNING + /// This method can overwrite existing properties like `id`. + #[wasm_bindgen(js_name = setPropertyUnchecked)] + pub fn set_property(&mut self, key: String, value: &JsValue) -> Result<()> { + let value: Option = value.into_serde().wasm_result()?; + match value { + Some(value) => { + self.0.properties_mut().insert(key, value); //todo + } + None => { + self.0.properties_mut().remove(&key); + } + } + Ok(()) + } + + /// Returns the custom DID Document properties. + #[wasm_bindgen(js_name = properties)] + pub fn properties(&mut self) -> Result { + let properties = self.0.properties(); + let properties_map = js_sys::Map::new(); + for (key, value) in properties.iter() { + properties_map.set(&JsValue::from(key), &JsValue::from_serde(&value).wasm_result()?); + } + Ok(properties_map.unchecked_into::()) + } + // =========================================================================== // Services // =========================================================================== + /// Return a set of all `Service`s in the document. + #[wasm_bindgen(js_name = service)] + pub fn service(&self) -> ArrayService { + self + .0 + .service() + .iter() + .cloned() + .map(Service) + .map(JsValue::from) + .collect::() + .unchecked_into::() + } + /// Add a new `Service` to the document. #[wasm_bindgen(js_name = insertService)] pub fn insert_service(&mut self, service: &Service) -> Result { @@ -107,6 +192,20 @@ impl WasmDocument { // Verification Methods // =========================================================================== + /// Returns a list of all {@link VerificationMethod} in the DID Document. + #[wasm_bindgen(js_name = methods)] + pub fn methods(&self) -> ArrayMethods { + self + .0 + .methods() + .collect::>() + .into_iter() + .cloned() + .map(WasmVerificationMethod::from) + .map(JsValue::from) + .collect::() + .unchecked_into::() + } /// Adds a new Verification Method to the DID Document. #[wasm_bindgen(js_name = insertMethod)] pub fn insert_method(&mut self, method: &WasmVerificationMethod, scope: WasmMethodScope) -> Result<()> { @@ -139,13 +238,32 @@ impl WasmDocument { /// /// Throws an error if the method is not found. #[wasm_bindgen(js_name = resolveMethod)] - pub fn resolve_method(&self, query: &UDIDUrlQuery) -> Result { + pub fn resolve_method(&self, query: &UDIDUrlQuery, scope: Option) -> Result { let method_query: String = query.into_serde().wasm_result()?; + if let Some(scope) = scope { + return Ok(WasmVerificationMethod( + self + .0 + .resolve_method_with_scope(&method_query, scope.0) + .ok_or(identity::did::Error::MethodNotFound) + .wasm_result()? + .clone(), + )); + } Ok(WasmVerificationMethod( self.0.try_resolve_method(&method_query).wasm_result()?.clone(), )) } + /// Attempts to resolve the given method query into a method capable of signing a document update. + #[wasm_bindgen(js_name = resolveSigningMethod)] + pub fn resolve_signing_method(&mut self, query: &UDIDUrlQuery) -> Result { + let method_query: String = query.into_serde().wasm_result()?; + Ok(WasmVerificationMethod( + self.0.try_resolve_signing_method(&method_query).wasm_result()?.clone(), + )) + } + #[wasm_bindgen(js_name = revokeMerkleKey)] pub fn revoke_merkle_key(&mut self, query: &UDIDUrlQuery, index: u32) -> Result { let method_query: String = query.into_serde().wasm_result()?; @@ -153,6 +271,51 @@ impl WasmDocument { method.revoke_merkle_key(index).wasm_result() } + /// Attaches the relationship to the given method, if the method exists. + /// + /// Note: The method needs to be in the set of verification methods, + /// so it cannot be an embedded one. + #[wasm_bindgen(js_name = attachMethodRelationShips)] + pub fn attach_method_relationships(&mut self, options: &MethodRelationshipOptions) -> Result<()> { + let relationships: Vec = options + .relationships() + .into_serde::>() + .map(OneOrMany::into_vec) + .wasm_result()? + .into_iter() + .map(MethodRelationship::from) + .collect(); + + for relationship in relationships { + self + .0 + .attach_method_relationship(&options.did_url().0, relationship) + .wasm_result()?; + } + Ok(()) + } + + /// Detaches the given relationship from the given method, if the method exists. + #[wasm_bindgen(js_name = detachMethodRelationShips)] + pub fn detach_method_relationships(&mut self, options: &MethodRelationshipOptions) -> Result<()> { + let relationships: Vec = options + .relationships() + .into_serde::>() + .map(OneOrMany::into_vec) + .wasm_result()? + .into_iter() + .map(MethodRelationship::from) + .collect(); + + for relationship in relationships { + self + .0 + .detach_method_relationship(&options.did_url().0, relationship) + .wasm_result()?; + } + Ok(()) + } + // =========================================================================== // Signatures // =========================================================================== @@ -497,3 +660,63 @@ extern "C" { #[wasm_bindgen(typescript_type = "DIDUrl | string")] pub type UDIDUrlQuery; } + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "DID[]")] + pub type ControllerArray; +} + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "DID[]")] + pub type ArrayString; +} + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "Service[]")] + pub type ArrayService; +} + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "VerificationMethod[]")] + pub type ArrayMethods; +} + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "Map")] + pub type JSMap; +} + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "AttachRelationshipOptions")] + pub type MethodRelationshipOptions; + + #[wasm_bindgen(getter, method)] + pub fn did_url(this: &MethodRelationshipOptions) -> WasmDIDUrl; + + #[wasm_bindgen(getter, method)] + pub fn relationships(this: &MethodRelationshipOptions) -> JsValue; +} + +#[wasm_bindgen(typescript_custom_section)] +const TS_ATTACH_METHOD_RELATIONSHIP_OPTIONS: &'static str = r#" +/** + * Options for attaching one or more verification relationships to a method on an identity. + */ +export type AttachRelationshipOptions = { + /** + * The identifier of the method in the document. + */ + did_url: DIDUrl, + + /** + * The relationships to add; + */ + relationships: MethodRelationship | MethodRelationship[] +}; +"#; diff --git a/bindings/wasm/src/did/wasm_method_relationship.rs b/bindings/wasm/src/did/wasm_method_relationship.rs new file mode 100644 index 0000000000..245c5ecc99 --- /dev/null +++ b/bindings/wasm/src/did/wasm_method_relationship.rs @@ -0,0 +1,30 @@ +// Copyright 2020-2022 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +use identity::did::MethodRelationship; +use serde_repr::Deserialize_repr; +use serde_repr::Serialize_repr; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen (js_name = MethodRelationship)] +#[derive(Serialize_repr, Deserialize_repr)] +#[repr(u8)] +pub enum WasmMethodRelationship { + Authentication = 0, + AssertionMethod = 1, + KeyAgreement = 2, + CapabilityDelegation = 3, + CapabilityInvocation = 4, +} + +impl From for MethodRelationship { + fn from(relationship: WasmMethodRelationship) -> Self { + match relationship { + WasmMethodRelationship::Authentication => MethodRelationship::Authentication, + WasmMethodRelationship::AssertionMethod => MethodRelationship::AssertionMethod, + WasmMethodRelationship::KeyAgreement => MethodRelationship::KeyAgreement, + WasmMethodRelationship::CapabilityDelegation => MethodRelationship::CapabilityDelegation, + WasmMethodRelationship::CapabilityInvocation => MethodRelationship::CapabilityInvocation, + } + } +} diff --git a/bindings/wasm/tests/wasm.rs b/bindings/wasm/tests/wasm.rs index bd9e91f208..83a8c38407 100644 --- a/bindings/wasm/tests/wasm.rs +++ b/bindings/wasm/tests/wasm.rs @@ -178,7 +178,7 @@ fn test_document_resolve_method() { // Resolve with DIDUrl method query. assert_eq!( document - .resolve_method(&JsValue::from(default_method.id()).unchecked_into()) + .resolve_method(&JsValue::from(default_method.id()).unchecked_into(), None) .unwrap() .id() .to_string(), @@ -186,7 +186,7 @@ fn test_document_resolve_method() { ); assert_eq!( document - .resolve_method(&JsValue::from(method_new.id()).unchecked_into()) + .resolve_method(&JsValue::from(method_new.id()).unchecked_into(), None) .unwrap() .id() .to_string(), @@ -196,7 +196,7 @@ fn test_document_resolve_method() { // Resolve with string method query. assert_eq!( document - .resolve_method(&JsValue::from_str(&default_method.id().to_string()).unchecked_into()) + .resolve_method(&JsValue::from_str(&default_method.id().to_string()).unchecked_into(), None) .unwrap() .id() .to_string(), @@ -204,7 +204,7 @@ fn test_document_resolve_method() { ); assert_eq!( document - .resolve_method(&JsValue::from_str(&method_new.id().to_string()).unchecked_into()) + .resolve_method(&JsValue::from_str(&method_new.id().to_string()).unchecked_into(), None) .unwrap() .id() .to_string(), @@ -214,7 +214,7 @@ fn test_document_resolve_method() { // Resolve with string fragment method query. assert_eq!( document - .resolve_method(&JsValue::from_str(&default_method.id().fragment().unwrap()).unchecked_into()) + .resolve_method(&JsValue::from_str(&default_method.id().fragment().unwrap()).unchecked_into(), None) .unwrap() .id() .to_string(), @@ -222,7 +222,7 @@ fn test_document_resolve_method() { ); assert_eq!( document - .resolve_method(&JsValue::from_str(&method_new.id().fragment().unwrap()).unchecked_into()) + .resolve_method(&JsValue::from_str(&method_new.id().fragment().unwrap()).unchecked_into(), None) .unwrap() .id() .to_string(), From c899c0215cfc8d716b03f731f124d19b3e9f3848 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Tue, 1 Mar 2022 11:44:43 +0100 Subject: [PATCH 02/19] fix clippy issue --- bindings/wasm/src/tangle/resolver.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bindings/wasm/src/tangle/resolver.rs b/bindings/wasm/src/tangle/resolver.rs index 4a1c623a09..d600afd0db 100644 --- a/bindings/wasm/src/tangle/resolver.rs +++ b/bindings/wasm/src/tangle/resolver.rs @@ -236,6 +236,7 @@ impl WasmResolverBuilder { /// /// NOTE: replaces any previous `Client` or `Config` with the same network name. #[wasm_bindgen] + #[must_use] pub fn client(mut self, client: &WasmClient) -> WasmResolverBuilder { self.0 = self.0.client(Rc::clone(&client.client)); self From 8db226412cbea817cf897bb6409660e5b65e4668 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Tue, 1 Mar 2022 11:45:05 +0100 Subject: [PATCH 03/19] cargo fmt --- bindings/wasm/tests/wasm.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/bindings/wasm/tests/wasm.rs b/bindings/wasm/tests/wasm.rs index 83a8c38407..76e46ac23a 100644 --- a/bindings/wasm/tests/wasm.rs +++ b/bindings/wasm/tests/wasm.rs @@ -196,7 +196,10 @@ fn test_document_resolve_method() { // Resolve with string method query. assert_eq!( document - .resolve_method(&JsValue::from_str(&default_method.id().to_string()).unchecked_into(), None) + .resolve_method( + &JsValue::from_str(&default_method.id().to_string()).unchecked_into(), + None + ) .unwrap() .id() .to_string(), @@ -214,7 +217,10 @@ fn test_document_resolve_method() { // Resolve with string fragment method query. assert_eq!( document - .resolve_method(&JsValue::from_str(&default_method.id().fragment().unwrap()).unchecked_into(), None) + .resolve_method( + &JsValue::from_str(&default_method.id().fragment().unwrap()).unchecked_into(), + None + ) .unwrap() .id() .to_string(), @@ -222,7 +228,10 @@ fn test_document_resolve_method() { ); assert_eq!( document - .resolve_method(&JsValue::from_str(&method_new.id().fragment().unwrap()).unchecked_into(), None) + .resolve_method( + &JsValue::from_str(&method_new.id().fragment().unwrap()).unchecked_into(), + None + ) .unwrap() .id() .to_string(), From ed1335c3ad440bd7b2b240e73fe74cc8b15b9d70 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Tue, 1 Mar 2022 12:39:55 +0100 Subject: [PATCH 04/19] remove unnecessary `ControllerArray` --- bindings/wasm/src/did/wasm_document.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index a32c77aa80..54501bf2d5 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -100,7 +100,7 @@ impl WasmDocument { /// Returns a list of the document controllers. #[wasm_bindgen(js_name = controller)] - pub fn controller(&self) -> ControllerArray { + pub fn controller(&self) -> DIDArray { match self.0.controller() { Some(controllers) => controllers .iter() @@ -108,14 +108,14 @@ impl WasmDocument { .map(WasmDID::from) .map(JsValue::from) .collect::() - .unchecked_into::(), - None => js_sys::Array::new().unchecked_into::(), + .unchecked_into::(), + None => js_sys::Array::new().unchecked_into::(), } } /// Returns a set of the document's `alsoKnownAs`. #[wasm_bindgen(js_name = alsoKnownAs)] - pub fn also_known_as(&self) -> ArrayString { + pub fn also_known_as(&self) -> DIDArray { self .0 .also_known_as() @@ -124,7 +124,7 @@ impl WasmDocument { .map(|url| url.to_string()) .map(JsValue::from) .collect::() - .unchecked_into::() + .unchecked_into::() } /// Adds a custom property to the DID Document. @@ -138,7 +138,7 @@ impl WasmDocument { let value: Option = value.into_serde().wasm_result()?; match value { Some(value) => { - self.0.properties_mut().insert(key, value); //todo + self.0.properties_mut().insert(key, value); } None => { self.0.properties_mut().remove(&key); @@ -664,13 +664,7 @@ extern "C" { #[wasm_bindgen] extern "C" { #[wasm_bindgen(typescript_type = "DID[]")] - pub type ControllerArray; -} - -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(typescript_type = "DID[]")] - pub type ArrayString; + pub type DIDArray; } #[wasm_bindgen] From 8a3149695ae888ce6bdde6097143ab88cc74b930 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab <31316147+abdulmth@users.noreply.github.com> Date: Tue, 1 Mar 2022 22:59:19 +0100 Subject: [PATCH 05/19] Apply suggestions from code review Co-authored-by: Craig Bester --- bindings/wasm/src/did/wasm_document.rs | 41 +++++++++++--------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 54501bf2d5..e6e91a11da 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -120,7 +120,6 @@ impl WasmDocument { .0 .also_known_as() .iter() - .cloned() .map(|url| url.to_string()) .map(JsValue::from) .collect::() @@ -128,13 +127,12 @@ impl WasmDocument { } /// Adds a custom property to the DID Document. - /// If the Document already has this key, the value will be overwritten. - /// If the value is set to `null`, the property will be removed. + /// If the value is set to `null`, the custom property will be removed. /// /// ### WARNING - /// This method can overwrite existing properties like `id`. + /// This method can overwrite existing properties like `id` and result in an invalid document. #[wasm_bindgen(js_name = setPropertyUnchecked)] - pub fn set_property(&mut self, key: String, value: &JsValue) -> Result<()> { + pub fn set_property_unchecked(&mut self, key: String, value: &JsValue) -> Result<()> { let value: Option = value.into_serde().wasm_result()?; match value { Some(value) => { @@ -163,7 +161,7 @@ impl WasmDocument { // =========================================================================== /// Return a set of all `Service`s in the document. - #[wasm_bindgen(js_name = service)] + #[wasm_bindgen] pub fn service(&self) -> ArrayService { self .0 @@ -198,8 +196,6 @@ impl WasmDocument { self .0 .methods() - .collect::>() - .into_iter() .cloned() .map(WasmVerificationMethod::from) .map(JsValue::from) @@ -240,19 +236,16 @@ impl WasmDocument { #[wasm_bindgen(js_name = resolveMethod)] pub fn resolve_method(&self, query: &UDIDUrlQuery, scope: Option) -> Result { let method_query: String = query.into_serde().wasm_result()?; - if let Some(scope) = scope { - return Ok(WasmVerificationMethod( - self - .0 - .resolve_method_with_scope(&method_query, scope.0) - .ok_or(identity::did::Error::MethodNotFound) - .wasm_result()? - .clone(), - )); - } - Ok(WasmVerificationMethod( - self.0.try_resolve_method(&method_query).wasm_result()?.clone(), - )) + let method: &IotaVerificationMethod = if let Some(scope) = scope { + self + .0 + .resolve_method_with_scope(&method_query, scope.0) + .ok_or(identity::did::Error::MethodNotFound) + .wasm_result()? + } else { + self.0.try_resolve_method(&method_query).wasm_result()? + }; + Ok(WasmVerificationMethod(method.clone())) } /// Attempts to resolve the given method query into a method capable of signing a document update. @@ -275,7 +268,7 @@ impl WasmDocument { /// /// Note: The method needs to be in the set of verification methods, /// so it cannot be an embedded one. - #[wasm_bindgen(js_name = attachMethodRelationShips)] + #[wasm_bindgen(js_name = attachMethodRelationships)] pub fn attach_method_relationships(&mut self, options: &MethodRelationshipOptions) -> Result<()> { let relationships: Vec = options .relationships() @@ -664,7 +657,7 @@ extern "C" { #[wasm_bindgen] extern "C" { #[wasm_bindgen(typescript_type = "DID[]")] - pub type DIDArray; + pub type ArrayDID; } #[wasm_bindgen] @@ -682,7 +675,7 @@ extern "C" { #[wasm_bindgen] extern "C" { #[wasm_bindgen(typescript_type = "Map")] - pub type JSMap; + pub type MapStringAny; } #[wasm_bindgen] From 97855be78b2dfb135dcc02cf7e413dd1f8d48ed6 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 11:17:13 +0100 Subject: [PATCH 06/19] add `setAlsoKnownAs` and `setController` --- bindings/wasm/docs/api-reference.md | 51 +++++++--- bindings/wasm/src/did/wasm_document.rs | 133 ++++++++++++++++--------- 2 files changed, 124 insertions(+), 60 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index cdb6c40306..b7dd16b09a 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -878,8 +878,10 @@ with the given Document. * [.metadataPreviousMessageId](#Document+metadataPreviousMessageId) ⇒ string * [.metadataPreviousMessageId](#Document+metadataPreviousMessageId) * [.metadataProof](#Document+metadataProof) ⇒ any + * [.setController(controllers)](#Document+setController) * [.controller()](#Document+controller) ⇒ [Array.<DID>](#DID) - * [.alsoKnownAs()](#Document+alsoKnownAs) ⇒ [Array.<DID>](#DID) + * [.setAlsoKnownAs(urls)](#Document+setAlsoKnownAs) + * [.alsoKnownAs()](#Document+alsoKnownAs) ⇒ Array.<string> * [.setPropertyUnchecked(key, value)](#Document+setPropertyUnchecked) * [.properties()](#Document+properties) ⇒ Map.<string, any> * [.service()](#Document+service) ⇒ [Array.<Service>](#Service) @@ -892,8 +894,8 @@ with the given Document. * [.resolveMethod(query, scope)](#Document+resolveMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.resolveSigningMethod(query)](#Document+resolveSigningMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.revokeMerkleKey(query, index)](#Document+revokeMerkleKey) ⇒ boolean - * [.attachMethodRelationShips(options)](#Document+attachMethodRelationShips) - * [.detachMethodRelationShips(options)](#Document+detachMethodRelationShips) + * [.attachMethodRelationships(did_url, relationships)](#Document+attachMethodRelationships) + * [.detachMethodRelationships(did_url, relationships)](#Document+detachMethodRelationships) * [.signSelf(key_pair, method_query)](#Document+signSelf) * [.signDocument(document, key_pair, method_query)](#Document+signDocument) * [.signCredential(data, args, options)](#Document+signCredential) ⇒ [Credential](#Credential) @@ -1011,15 +1013,37 @@ Sets the previous integration chain message id. Returns the `proof` object. **Kind**: instance property of [Document](#Document) + + +### document.setController(controllers) +Sets the controllers of the DID Document. + +**Kind**: instance method of [Document](#Document) + +| Param | Type | +| --- | --- | +| controllers | [DID](#DID) \| [Array.<DID>](#DID) \| null | + ### document.controller() ⇒ [Array.<DID>](#DID) Returns a list of the document controllers. **Kind**: instance method of [Document](#Document) + + +### document.setAlsoKnownAs(urls) +Sets the `alsoKnownAs` property in the DID document. + +**Kind**: instance method of [Document](#Document) + +| Param | Type | +| --- | --- | +| urls | string \| Array.<string> \| null | + -### document.alsoKnownAs() ⇒ [Array.<DID>](#DID) +### document.alsoKnownAs() ⇒ Array.<string> Returns a set of the document's `alsoKnownAs`. **Kind**: instance method of [Document](#Document) @@ -1027,11 +1051,10 @@ Returns a set of the document's `alsoKnownAs`. ### document.setPropertyUnchecked(key, value) Adds a custom property to the DID Document. -If the Document already has this key, the value will be overwritten. -If the value is set to `null`, the property will be removed. +If the value is set to `null`, the custom property will be removed. ### WARNING -This method can overwrite existing properties like `id`. +This method can overwrite existing properties like `id` and result in an invalid document. **Kind**: instance method of [Document](#Document) @@ -1148,9 +1171,9 @@ Attempts to resolve the given method query into a method capable of signing a do | query | [DIDUrl](#DIDUrl) \| string | | index | number | - + -### document.attachMethodRelationShips(options) +### document.attachMethodRelationships(did_url, relationships) Attaches the relationship to the given method, if the method exists. Note: The method needs to be in the set of verification methods, @@ -1160,18 +1183,20 @@ so it cannot be an embedded one. | Param | Type | | --- | --- | -| options | AttachRelationshipOptions | +| did_url | [DIDUrl](#DIDUrl) | +| relationships | [MethodRelationship](#MethodRelationship) \| [Array.<MethodRelationship>](#MethodRelationship) | - + -### document.detachMethodRelationShips(options) +### document.detachMethodRelationships(did_url, relationships) Detaches the given relationship from the given method, if the method exists. **Kind**: instance method of [Document](#Document) | Param | Type | | --- | --- | -| options | AttachRelationshipOptions | +| did_url | [DIDUrl](#DIDUrl) | +| relationships | [MethodRelationship](#MethodRelationship) \| [Array.<MethodRelationship>](#MethodRelationship) | diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index e6e91a11da..d5731e8208 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -3,8 +3,8 @@ use std::str::FromStr; -use identity::core::decode_b58; -use identity::core::OneOrMany; +use identity::core::{decode_b58, OrderedSet, Url}; +use identity::core::{OneOrMany, OneOrSet}; use identity::crypto::merkle_key::MerkleDigestTag; use identity::crypto::merkle_key::MerkleKey; use identity::crypto::merkle_key::Sha256; @@ -14,11 +14,11 @@ use identity::crypto::PublicKey; use identity::crypto::SignatureOptions; use identity::did::verifiable::VerifiableProperties; use identity::did::MethodRelationship; -use identity::iota::Error; use identity::iota::IotaDocument; use identity::iota::IotaVerificationMethod; use identity::iota::MessageId; use identity::iota::NetworkName; +use identity::iota::{Error, IotaDID}; use wasm_bindgen::prelude::*; use crate::common::WasmTimestamp; @@ -98,9 +98,36 @@ impl WasmDocument { WasmDID(self.0.id().clone()) } + /// Sets the controllers of the DID Document. + #[wasm_bindgen(js_name = setController)] + pub fn set_controller(&mut self, controllers: &UDID) -> Result<()> { + let controllers: Option> = controllers.into_serde().wasm_result()?; + + let controller_set: Option> = if let Some(controllers) = controllers { + match controllers { + OneOrMany::One(controller) => Some(OneOrSet::new_one(controller)), + OneOrMany::Many(controllers) => { + if controllers.is_empty() { + None + } else { + let mut set: OrderedSet = OrderedSet::new(); + for controller in controllers { + set.append(controller); + } + Some(OneOrSet::new_set(set).wasm_result()?) + } + } + } + } else { + None + }; + *self.0.controller_mut() = controller_set; + Ok(()) + } + /// Returns a list of the document controllers. #[wasm_bindgen(js_name = controller)] - pub fn controller(&self) -> DIDArray { + pub fn controller(&self) -> ArrayDID { match self.0.controller() { Some(controllers) => controllers .iter() @@ -108,14 +135,28 @@ impl WasmDocument { .map(WasmDID::from) .map(JsValue::from) .collect::() - .unchecked_into::(), - None => js_sys::Array::new().unchecked_into::(), + .unchecked_into::(), + None => js_sys::Array::new().unchecked_into::(), } } + /// Sets the `alsoKnownAs` property in the DID document. + #[wasm_bindgen(js_name = setAlsoKnownAs)] + pub fn set_also_known_as(&mut self, urls: &UUrl) -> Result<()> { + let urls: Option> = urls.into_serde().wasm_result()?; + let mut urls_set: OrderedSet = OrderedSet::new(); + if let Some(urls) = urls { + for url in urls.into_vec() { + urls_set.append(Url::parse(url).wasm_result()?); + } + } + *self.0.also_known_as_mut() = urls_set; + Ok(()) + } + /// Returns a set of the document's `alsoKnownAs`. #[wasm_bindgen(js_name = alsoKnownAs)] - pub fn also_known_as(&self) -> DIDArray { + pub fn also_known_as(&self) -> ArrayString { self .0 .also_known_as() @@ -123,7 +164,7 @@ impl WasmDocument { .map(|url| url.to_string()) .map(JsValue::from) .collect::() - .unchecked_into::() + .unchecked_into::() } /// Adds a custom property to the DID Document. @@ -147,13 +188,12 @@ impl WasmDocument { /// Returns the custom DID Document properties. #[wasm_bindgen(js_name = properties)] - pub fn properties(&mut self) -> Result { - let properties = self.0.properties(); + pub fn properties(&mut self) -> Result { let properties_map = js_sys::Map::new(); - for (key, value) in properties.iter() { + for (key, value) in self.0.properties().iter() { properties_map.set(&JsValue::from(key), &JsValue::from_serde(&value).wasm_result()?); } - Ok(properties_map.unchecked_into::()) + Ok(properties_map.unchecked_into::()) } // =========================================================================== @@ -269,9 +309,12 @@ impl WasmDocument { /// Note: The method needs to be in the set of verification methods, /// so it cannot be an embedded one. #[wasm_bindgen(js_name = attachMethodRelationships)] - pub fn attach_method_relationships(&mut self, options: &MethodRelationshipOptions) -> Result<()> { - let relationships: Vec = options - .relationships() + pub fn attach_method_relationships( + &mut self, + did_url: &WasmDIDUrl, + relationships: &UMethodRelationships, + ) -> Result<()> { + let relationships: Vec = relationships .into_serde::>() .map(OneOrMany::into_vec) .wasm_result()? @@ -282,17 +325,20 @@ impl WasmDocument { for relationship in relationships { self .0 - .attach_method_relationship(&options.did_url().0, relationship) + .attach_method_relationship(&did_url.0, relationship) .wasm_result()?; } Ok(()) } /// Detaches the given relationship from the given method, if the method exists. - #[wasm_bindgen(js_name = detachMethodRelationShips)] - pub fn detach_method_relationships(&mut self, options: &MethodRelationshipOptions) -> Result<()> { - let relationships: Vec = options - .relationships() + #[wasm_bindgen(js_name = detachMethodRelationships)] + pub fn detach_method_relationships( + &mut self, + did_url: &WasmDIDUrl, + relationships: &UMethodRelationships, + ) -> Result<()> { + let relationships: Vec = relationships .into_serde::>() .map(OneOrMany::into_vec) .wasm_result()? @@ -303,7 +349,7 @@ impl WasmDocument { for relationship in relationships { self .0 - .detach_method_relationship(&options.did_url().0, relationship) + .detach_method_relationship(&did_url.0, relationship) .wasm_result()?; } Ok(()) @@ -654,6 +700,18 @@ extern "C" { pub type UDIDUrlQuery; } +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "string | string[] | null")] + pub type UUrl; +} + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "DID | DID[] | null")] + pub type UDID; +} + #[wasm_bindgen] extern "C" { #[wasm_bindgen(typescript_type = "DID[]")] @@ -666,6 +724,11 @@ extern "C" { pub type ArrayService; } +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(typescript_type = "Array")] + pub type ArrayString; +} #[wasm_bindgen] extern "C" { #[wasm_bindgen(typescript_type = "VerificationMethod[]")] @@ -680,30 +743,6 @@ extern "C" { #[wasm_bindgen] extern "C" { - #[wasm_bindgen(typescript_type = "AttachRelationshipOptions")] - pub type MethodRelationshipOptions; - - #[wasm_bindgen(getter, method)] - pub fn did_url(this: &MethodRelationshipOptions) -> WasmDIDUrl; - - #[wasm_bindgen(getter, method)] - pub fn relationships(this: &MethodRelationshipOptions) -> JsValue; + #[wasm_bindgen(typescript_type = "MethodRelationship | MethodRelationship[]")] + pub type UMethodRelationships; } - -#[wasm_bindgen(typescript_custom_section)] -const TS_ATTACH_METHOD_RELATIONSHIP_OPTIONS: &'static str = r#" -/** - * Options for attaching one or more verification relationships to a method on an identity. - */ -export type AttachRelationshipOptions = { - /** - * The identifier of the method in the document. - */ - did_url: DIDUrl, - - /** - * The relationships to add; - */ - relationships: MethodRelationship | MethodRelationship[] -}; -"#; From c9ed6ca1fce543c240d224449d2917cc0c144a0a Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 11:32:51 +0100 Subject: [PATCH 07/19] improve docs --- bindings/wasm/src/did/wasm_document.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index d5731e8208..6ff8e291c3 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -99,6 +99,9 @@ impl WasmDocument { } /// Sets the controllers of the DID Document. + /// + /// Note: Duplicates will be ignored. + /// Use `null` to remove all controllers. #[wasm_bindgen(js_name = setController)] pub fn set_controller(&mut self, controllers: &UDID) -> Result<()> { let controllers: Option> = controllers.into_serde().wasm_result()?; @@ -125,7 +128,10 @@ impl WasmDocument { Ok(()) } - /// Returns a list of the document controllers. + /// Returns a list of document controllers. + /// + /// Note: Duplicates will be ignored. + /// Use `null` to remove all controllers. #[wasm_bindgen(js_name = controller)] pub fn controller(&self) -> ArrayDID { match self.0.controller() { From 0d737878b394d0786cab537fae3519607594b2d3 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 11:51:05 +0100 Subject: [PATCH 08/19] fix imports --- bindings/wasm/src/did/wasm_document.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 25a89a929c..a37d79b737 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -3,8 +3,11 @@ use std::str::FromStr; -use identity::core::{decode_b58, OrderedSet, Url}; -use identity::core::{OneOrMany, OneOrSet}; +use identity::core::decode_b58; +use identity::core::OneOrMany; +use identity::core::OneOrSet; +use identity::core::OrderedSet; +use identity::core::Url; use identity::crypto::merkle_key::MerkleDigestTag; use identity::crypto::merkle_key::MerkleKey; use identity::crypto::merkle_key::Sha256; @@ -14,6 +17,7 @@ use identity::crypto::PublicKey; use identity::crypto::SignatureOptions; use identity::did::verifiable::VerifiableProperties; use identity::did::MethodRelationship; +use identity::did::Service; use identity::iota::IotaDocument; use identity::iota::IotaVerificationMethod; use identity::iota::MessageId; @@ -27,7 +31,6 @@ use crate::credential::WasmPresentation; use crate::crypto::KeyPair; use crate::crypto::WasmSignatureOptions; use crate::did::wasm_method_relationship::WasmMethodRelationship; -use crate::did::WasmDID; use crate::did::WasmDIDUrl; use crate::did::WasmDiffMessage; use crate::did::WasmDocumentMetadata; @@ -35,6 +38,7 @@ use crate::did::WasmMethodScope; use crate::did::WasmMethodType; use crate::did::WasmVerificationMethod; use crate::did::WasmVerifierOptions; +use crate::did::{WasmDID, WasmService}; use crate::error::Result; use crate::error::WasmResult; use crate::service::Service; From f06e847f7d6485ba9224d2166792a725d6c00425 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 12:04:17 +0100 Subject: [PATCH 09/19] small fix --- bindings/wasm/docs/api-reference.md | 67 +++++++++++++++++++++++--- bindings/wasm/src/did/wasm_document.rs | 6 +-- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index b7dd16b09a..7b2ab6feed 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -58,7 +58,10 @@ merged with one or more DiffMessages.

Builder for configuring [Clients][Client] when constructing a [Resolver].

Service
-
+

A DID Document Service used to enable trusted interactions associated +with a DID subject.

+

See: https://www.w3.org/TR/did-core/#services

+
SignatureOptions

Holds additional options for creating signatures. See ISignatureOptions.

@@ -76,10 +79,10 @@ See IVerifierOptions.

## Members
-
DIDMessageEncoding
-
KeyType
+
DIDMessageEncoding
+
MethodRelationship
Digest
@@ -1018,6 +1021,9 @@ Returns the `proof` object. ### document.setController(controllers) Sets the controllers of the DID Document. +Note: Duplicates will be ignored. +Use `null` to remove all controllers. + **Kind**: instance method of [Document](#Document) | Param | Type | @@ -1027,7 +1033,10 @@ Sets the controllers of the DID Document. ### document.controller() ⇒ [Array.<DID>](#DID) -Returns a list of the document controllers. +Returns a list of document controllers. + +Note: Duplicates will be ignored. +Use `null` to remove all controllers. **Kind**: instance method of [Document](#Document) @@ -2413,14 +2422,56 @@ Constructs a new [`Resolver`] based on the builder configuration. ## Service +A DID Document Service used to enable trusted interactions associated +with a DID subject. + +See: https://www.w3.org/TR/did-core/#services + **Kind**: global class * [Service](#Service) + * [new Service(service)](#new_Service_new) * _instance_ + * [.id](#Service+id) ⇒ [DIDUrl](#DIDUrl) + * [.type](#Service+type) ⇒ string + * [.serviceEndpoint](#Service+serviceEndpoint) ⇒ string \| Array.<string> \| Map.<string, Array.<string>> + * [.properties()](#Service+properties) ⇒ Map.<string, any> * [.toJSON()](#Service+toJSON) ⇒ any * _static_ * [.fromJSON(value)](#Service.fromJSON) ⇒ [Service](#Service) + + +### new Service(service) + +| Param | Type | +| --- | --- | +| service | IService | + + + +### service.id ⇒ [DIDUrl](#DIDUrl) +Returns a copy of the `Service` id. + +**Kind**: instance property of [Service](#Service) + + +### service.type ⇒ string +Returns a copy of the `Service` type. + +**Kind**: instance property of [Service](#Service) + + +### service.serviceEndpoint ⇒ string \| Array.<string> \| Map.<string, Array.<string>> +Returns a copy of the `Service` endpoint. + +**Kind**: instance property of [Service](#Service) + + +### service.properties() ⇒ Map.<string, any> +Returns a copy of the custom properties on the `Service`. + +**Kind**: instance method of [Service](#Service) ### service.toJSON() ⇒ any @@ -2631,14 +2682,14 @@ Throws an error if any of the options are invalid. Creates a new `VerifierOptions` with default options. **Kind**: static method of [VerifierOptions](#VerifierOptions) - - -## DIDMessageEncoding -**Kind**: global variable ## KeyType **Kind**: global variable + + +## DIDMessageEncoding +**Kind**: global variable ## MethodRelationship diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index a37d79b737..8b529924cd 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -16,8 +16,7 @@ use identity::crypto::PrivateKey; use identity::crypto::PublicKey; use identity::crypto::SignatureOptions; use identity::did::verifiable::VerifiableProperties; -use identity::did::MethodRelationship; -use identity::did::Service; +use identity::did::{MethodRelationship}; use identity::iota::IotaDocument; use identity::iota::IotaVerificationMethod; use identity::iota::MessageId; @@ -41,7 +40,6 @@ use crate::did::WasmVerifierOptions; use crate::did::{WasmDID, WasmService}; use crate::error::Result; use crate::error::WasmResult; -use crate::service::Service; use wasm_bindgen::JsCast; // ============================================================================= @@ -218,7 +216,7 @@ impl WasmDocument { .service() .iter() .cloned() - .map(Service) + .map(WasmService) .map(JsValue::from) .collect::() .unchecked_into::() From aa9896516f23322f0f3306610bdfa8cd65f064da Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 12:07:12 +0100 Subject: [PATCH 10/19] cargo fmt --- bindings/wasm/src/did/wasm_document.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 8b529924cd..df10c07605 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -16,12 +16,13 @@ use identity::crypto::PrivateKey; use identity::crypto::PublicKey; use identity::crypto::SignatureOptions; use identity::did::verifiable::VerifiableProperties; -use identity::did::{MethodRelationship}; +use identity::did::MethodRelationship; +use identity::iota::Error; +use identity::iota::IotaDID; use identity::iota::IotaDocument; use identity::iota::IotaVerificationMethod; use identity::iota::MessageId; use identity::iota::NetworkName; -use identity::iota::{Error, IotaDID}; use wasm_bindgen::prelude::*; use crate::common::WasmTimestamp; @@ -30,14 +31,15 @@ use crate::credential::WasmPresentation; use crate::crypto::KeyPair; use crate::crypto::WasmSignatureOptions; use crate::did::wasm_method_relationship::WasmMethodRelationship; +use crate::did::WasmDID; use crate::did::WasmDIDUrl; use crate::did::WasmDiffMessage; use crate::did::WasmDocumentMetadata; use crate::did::WasmMethodScope; use crate::did::WasmMethodType; +use crate::did::WasmService; use crate::did::WasmVerificationMethod; use crate::did::WasmVerifierOptions; -use crate::did::{WasmDID, WasmService}; use crate::error::Result; use crate::error::WasmResult; use wasm_bindgen::JsCast; From 041debaf3c43709586060490a5bcfa9457897bae Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 18:12:36 +0100 Subject: [PATCH 11/19] only allow one relationship to be added --- bindings/wasm/docs/api-reference.md | 16 ++++----- bindings/wasm/src/did/wasm_document.rs | 46 ++++++++------------------ 2 files changed, 22 insertions(+), 40 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index 7b2ab6feed..118d995b9b 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -81,10 +81,10 @@ See IVerifierOptions.

KeyType
-
DIDMessageEncoding
-
MethodRelationship
+
DIDMessageEncoding
+
Digest
@@ -1193,7 +1193,7 @@ so it cannot be an embedded one. | Param | Type | | --- | --- | | did_url | [DIDUrl](#DIDUrl) | -| relationships | [MethodRelationship](#MethodRelationship) \| [Array.<MethodRelationship>](#MethodRelationship) | +| relationships | [MethodRelationship](#MethodRelationship) | @@ -1205,7 +1205,7 @@ Detaches the given relationship from the given method, if the method exists. | Param | Type | | --- | --- | | did_url | [DIDUrl](#DIDUrl) | -| relationships | [MethodRelationship](#MethodRelationship) \| [Array.<MethodRelationship>](#MethodRelationship) | +| relationships | [MethodRelationship](#MethodRelationship) | @@ -2686,14 +2686,14 @@ Creates a new `VerifierOptions` with default options. ## KeyType **Kind**: global variable - - -## DIDMessageEncoding -**Kind**: global variable ## MethodRelationship **Kind**: global variable + + +## DIDMessageEncoding +**Kind**: global variable ## Digest diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index df10c07605..6f4e2afbea 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -322,22 +322,13 @@ impl WasmDocument { pub fn attach_method_relationships( &mut self, did_url: &WasmDIDUrl, - relationships: &UMethodRelationships, + relationships: &MethodRelationshipType, ) -> Result<()> { - let relationships: Vec = relationships - .into_serde::>() - .map(OneOrMany::into_vec) - .wasm_result()? - .into_iter() - .map(MethodRelationship::from) - .collect(); - - for relationship in relationships { - self - .0 - .attach_method_relationship(&did_url.0, relationship) - .wasm_result()?; - } + let relationship: WasmMethodRelationship = relationships.into_serde().wasm_result()?; + self + .0 + .attach_method_relationship(&did_url.0, relationship.into()) + .wasm_result()?; Ok(()) } @@ -346,22 +337,13 @@ impl WasmDocument { pub fn detach_method_relationships( &mut self, did_url: &WasmDIDUrl, - relationships: &UMethodRelationships, + relationships: &MethodRelationshipType, ) -> Result<()> { - let relationships: Vec = relationships - .into_serde::>() - .map(OneOrMany::into_vec) - .wasm_result()? - .into_iter() - .map(MethodRelationship::from) - .collect(); - - for relationship in relationships { - self - .0 - .detach_method_relationship(&did_url.0, relationship) - .wasm_result()?; - } + let relationship: WasmMethodRelationship = relationships.into_serde().wasm_result()?; + self + .0 + .attach_method_relationship(&did_url.0, relationship.into()) + .wasm_result()?; Ok(()) } @@ -753,6 +735,6 @@ extern "C" { #[wasm_bindgen] extern "C" { - #[wasm_bindgen(typescript_type = "MethodRelationship | MethodRelationship[]")] - pub type UMethodRelationships; + #[wasm_bindgen(typescript_type = "MethodRelationship")] + pub type MethodRelationshipType; } From a11fa866f1f31af345719086bf10fec42d2e3acc Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 18:20:25 +0100 Subject: [PATCH 12/19] remove unused import --- bindings/wasm/src/did/wasm_document.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 6f4e2afbea..ee56556dda 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -16,7 +16,6 @@ use identity::crypto::PrivateKey; use identity::crypto::PublicKey; use identity::crypto::SignatureOptions; use identity::did::verifiable::VerifiableProperties; -use identity::did::MethodRelationship; use identity::iota::Error; use identity::iota::IotaDID; use identity::iota::IotaDocument; From f8b000c354d52f53c4a673250b74314e2cb6416b Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 18:24:49 +0100 Subject: [PATCH 13/19] small fix --- bindings/wasm/docs/api-reference.md | 12 ++++++------ bindings/wasm/src/did/wasm_document.rs | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index 118d995b9b..8788760e2a 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -897,8 +897,8 @@ with the given Document. * [.resolveMethod(query, scope)](#Document+resolveMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.resolveSigningMethod(query)](#Document+resolveSigningMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.revokeMerkleKey(query, index)](#Document+revokeMerkleKey) ⇒ boolean - * [.attachMethodRelationships(did_url, relationships)](#Document+attachMethodRelationships) - * [.detachMethodRelationships(did_url, relationships)](#Document+detachMethodRelationships) + * [.attachMethodRelationships(did_url, relationship)](#Document+attachMethodRelationships) + * [.detachMethodRelationships(did_url, relationship)](#Document+detachMethodRelationships) * [.signSelf(key_pair, method_query)](#Document+signSelf) * [.signDocument(document, key_pair, method_query)](#Document+signDocument) * [.signCredential(data, args, options)](#Document+signCredential) ⇒ [Credential](#Credential) @@ -1182,7 +1182,7 @@ Attempts to resolve the given method query into a method capable of signing a do -### document.attachMethodRelationships(did_url, relationships) +### document.attachMethodRelationships(did_url, relationship) Attaches the relationship to the given method, if the method exists. Note: The method needs to be in the set of verification methods, @@ -1193,11 +1193,11 @@ so it cannot be an embedded one. | Param | Type | | --- | --- | | did_url | [DIDUrl](#DIDUrl) | -| relationships | [MethodRelationship](#MethodRelationship) | +| relationship | [MethodRelationship](#MethodRelationship) | -### document.detachMethodRelationships(did_url, relationships) +### document.detachMethodRelationships(did_url, relationship) Detaches the given relationship from the given method, if the method exists. **Kind**: instance method of [Document](#Document) @@ -1205,7 +1205,7 @@ Detaches the given relationship from the given method, if the method exists. | Param | Type | | --- | --- | | did_url | [DIDUrl](#DIDUrl) | -| relationships | [MethodRelationship](#MethodRelationship) | +| relationship | [MethodRelationship](#MethodRelationship) | diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index ee56556dda..1f2e564e00 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -321,9 +321,9 @@ impl WasmDocument { pub fn attach_method_relationships( &mut self, did_url: &WasmDIDUrl, - relationships: &MethodRelationshipType, + relationship: &MethodRelationshipType, ) -> Result<()> { - let relationship: WasmMethodRelationship = relationships.into_serde().wasm_result()?; + let relationship: WasmMethodRelationship = relationship.into_serde().wasm_result()?; self .0 .attach_method_relationship(&did_url.0, relationship.into()) @@ -336,9 +336,9 @@ impl WasmDocument { pub fn detach_method_relationships( &mut self, did_url: &WasmDIDUrl, - relationships: &MethodRelationshipType, + relationship: &MethodRelationshipType, ) -> Result<()> { - let relationship: WasmMethodRelationship = relationships.into_serde().wasm_result()?; + let relationship: WasmMethodRelationship = relationship.into_serde().wasm_result()?; self .0 .attach_method_relationship(&did_url.0, relationship.into()) From bf19c86b3686a553288ff26523627ac9e3bdd471 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab <31316147+abdulmth@users.noreply.github.com> Date: Wed, 2 Mar 2022 22:06:06 +0100 Subject: [PATCH 14/19] Apply suggestions from code review Co-authored-by: Craig Bester --- bindings/wasm/src/did/wasm_document.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 1f2e564e00..b388c70db0 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -196,7 +196,7 @@ impl WasmDocument { } /// Returns the custom DID Document properties. - #[wasm_bindgen(js_name = properties)] + #[wasm_bindgen] pub fn properties(&mut self) -> Result { let properties_map = js_sys::Map::new(); for (key, value) in self.0.properties().iter() { @@ -240,7 +240,7 @@ impl WasmDocument { // =========================================================================== /// Returns a list of all {@link VerificationMethod} in the DID Document. - #[wasm_bindgen(js_name = methods)] + #[wasm_bindgen] pub fn methods(&self) -> ArrayMethods { self .0 @@ -317,8 +317,8 @@ impl WasmDocument { /// /// Note: The method needs to be in the set of verification methods, /// so it cannot be an embedded one. - #[wasm_bindgen(js_name = attachMethodRelationships)] - pub fn attach_method_relationships( + #[wasm_bindgen(js_name = attachMethodRelationship)] + pub fn attach_method_relationship( &mut self, did_url: &WasmDIDUrl, relationship: &MethodRelationshipType, @@ -332,8 +332,8 @@ impl WasmDocument { } /// Detaches the given relationship from the given method, if the method exists. - #[wasm_bindgen(js_name = detachMethodRelationships)] - pub fn detach_method_relationships( + #[wasm_bindgen(js_name = detachMethodRelationship)] + pub fn detach_method_relationship( &mut self, did_url: &WasmDIDUrl, relationship: &MethodRelationshipType, @@ -694,13 +694,13 @@ extern "C" { #[wasm_bindgen] extern "C" { #[wasm_bindgen(typescript_type = "string | string[] | null")] - pub type UUrl; + pub type UOneOrManyUrl; } #[wasm_bindgen] extern "C" { #[wasm_bindgen(typescript_type = "DID | DID[] | null")] - pub type UDID; + pub type UOneOrManyDID; } #[wasm_bindgen] From 9539fc055309796740a6ad384f4e09ab07075aa2 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 22:29:49 +0100 Subject: [PATCH 15/19] code improvements --- bindings/wasm/docs/api-reference.md | 12 +++--- bindings/wasm/src/did/wasm_document.rs | 51 +++++--------------------- 2 files changed, 15 insertions(+), 48 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index 8788760e2a..bacdddb968 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -897,8 +897,8 @@ with the given Document. * [.resolveMethod(query, scope)](#Document+resolveMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.resolveSigningMethod(query)](#Document+resolveSigningMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.revokeMerkleKey(query, index)](#Document+revokeMerkleKey) ⇒ boolean - * [.attachMethodRelationships(did_url, relationship)](#Document+attachMethodRelationships) - * [.detachMethodRelationships(did_url, relationship)](#Document+detachMethodRelationships) + * [.attachMethodRelationship(did_url, relationship)](#Document+attachMethodRelationship) + * [.detachMethodRelationship(did_url, relationship)](#Document+detachMethodRelationship) * [.signSelf(key_pair, method_query)](#Document+signSelf) * [.signDocument(document, key_pair, method_query)](#Document+signDocument) * [.signCredential(data, args, options)](#Document+signCredential) ⇒ [Credential](#Credential) @@ -1180,9 +1180,9 @@ Attempts to resolve the given method query into a method capable of signing a do | query | [DIDUrl](#DIDUrl) \| string | | index | number | - + -### document.attachMethodRelationships(did_url, relationship) +### document.attachMethodRelationship(did_url, relationship) Attaches the relationship to the given method, if the method exists. Note: The method needs to be in the set of verification methods, @@ -1195,9 +1195,9 @@ so it cannot be an embedded one. | did_url | [DIDUrl](#DIDUrl) | | relationship | [MethodRelationship](#MethodRelationship) | - + -### document.detachMethodRelationships(did_url, relationship) +### document.detachMethodRelationship(did_url, relationship) Detaches the given relationship from the given method, if the method exists. **Kind**: instance method of [Document](#Document) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index b388c70db0..cfd6fc8dbb 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -106,23 +106,13 @@ impl WasmDocument { /// Note: Duplicates will be ignored. /// Use `null` to remove all controllers. #[wasm_bindgen(js_name = setController)] - pub fn set_controller(&mut self, controllers: &UDID) -> Result<()> { + pub fn set_controller(&mut self, controllers: &UOneOrManyDID) -> Result<()> { let controllers: Option> = controllers.into_serde().wasm_result()?; - - let controller_set: Option> = if let Some(controllers) = controllers { - match controllers { - OneOrMany::One(controller) => Some(OneOrSet::new_one(controller)), - OneOrMany::Many(controllers) => { - if controllers.is_empty() { - None - } else { - let mut set: OrderedSet = OrderedSet::new(); - for controller in controllers { - set.append(controller); - } - Some(OneOrSet::new_set(set).wasm_result()?) - } - } + let controller_set: Option> = if let Some(controllers) = controllers.map(OneOrMany::into_vec) { + if controllers.is_empty() { + None + } else { + Some(OneOrSet::try_from(OrderedSet::from_iter(controllers)).wasm_result()?) } } else { None @@ -135,7 +125,7 @@ impl WasmDocument { /// /// Note: Duplicates will be ignored. /// Use `null` to remove all controllers. - #[wasm_bindgen(js_name = controller)] + #[wasm_bindgen] pub fn controller(&self) -> ArrayDID { match self.0.controller() { Some(controllers) => controllers @@ -151,7 +141,7 @@ impl WasmDocument { /// Sets the `alsoKnownAs` property in the DID document. #[wasm_bindgen(js_name = setAlsoKnownAs)] - pub fn set_also_known_as(&mut self, urls: &UUrl) -> Result<()> { + pub fn set_also_known_as(&mut self, urls: &UOneOrManyUrl) -> Result<()> { let urls: Option> = urls.into_serde().wasm_result()?; let mut urls_set: OrderedSet = OrderedSet::new(); if let Some(urls) = urls { @@ -689,51 +679,28 @@ impl From for WasmDocument { extern "C" { #[wasm_bindgen(typescript_type = "DIDUrl | string")] pub type UDIDUrlQuery; -} -#[wasm_bindgen] -extern "C" { #[wasm_bindgen(typescript_type = "string | string[] | null")] pub type UOneOrManyUrl; -} -#[wasm_bindgen] -extern "C" { #[wasm_bindgen(typescript_type = "DID | DID[] | null")] pub type UOneOrManyDID; -} -#[wasm_bindgen] -extern "C" { #[wasm_bindgen(typescript_type = "DID[]")] pub type ArrayDID; -} -#[wasm_bindgen] -extern "C" { #[wasm_bindgen(typescript_type = "Service[]")] pub type ArrayService; -} -#[wasm_bindgen] -extern "C" { #[wasm_bindgen(typescript_type = "Array")] pub type ArrayString; -} -#[wasm_bindgen] -extern "C" { + #[wasm_bindgen(typescript_type = "VerificationMethod[]")] pub type ArrayMethods; -} -#[wasm_bindgen] -extern "C" { #[wasm_bindgen(typescript_type = "Map")] pub type MapStringAny; -} -#[wasm_bindgen] -extern "C" { #[wasm_bindgen(typescript_type = "MethodRelationship")] pub type MethodRelationshipType; } From d7c0b7d71dee01b3e1b1a718ebb1dc21350db267 Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 22:48:02 +0100 Subject: [PATCH 16/19] remove unnecessary type --- bindings/wasm/docs/api-reference.md | 16 ++++++++-------- bindings/wasm/src/did/wasm_document.rs | 9 ++------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index bacdddb968..94f26a76c7 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -79,10 +79,10 @@ See IVerifierOptions.

## Members
-
KeyType
-
MethodRelationship
+
KeyType
+
DIDMessageEncoding
Digest
@@ -1193,7 +1193,7 @@ so it cannot be an embedded one. | Param | Type | | --- | --- | | did_url | [DIDUrl](#DIDUrl) | -| relationship | [MethodRelationship](#MethodRelationship) | +| relationship | number | @@ -1205,7 +1205,7 @@ Detaches the given relationship from the given method, if the method exists. | Param | Type | | --- | --- | | did_url | [DIDUrl](#DIDUrl) | -| relationship | [MethodRelationship](#MethodRelationship) | +| relationship | number | @@ -2682,14 +2682,14 @@ Throws an error if any of the options are invalid. Creates a new `VerifierOptions` with default options. **Kind**: static method of [VerifierOptions](#VerifierOptions) - - -## KeyType -**Kind**: global variable ## MethodRelationship **Kind**: global variable + + +## KeyType +**Kind**: global variable ## DIDMessageEncoding diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index cfd6fc8dbb..026c3bc72d 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -311,9 +311,8 @@ impl WasmDocument { pub fn attach_method_relationship( &mut self, did_url: &WasmDIDUrl, - relationship: &MethodRelationshipType, + relationship: WasmMethodRelationship, ) -> Result<()> { - let relationship: WasmMethodRelationship = relationship.into_serde().wasm_result()?; self .0 .attach_method_relationship(&did_url.0, relationship.into()) @@ -326,9 +325,8 @@ impl WasmDocument { pub fn detach_method_relationship( &mut self, did_url: &WasmDIDUrl, - relationship: &MethodRelationshipType, + relationship: WasmMethodRelationship, ) -> Result<()> { - let relationship: WasmMethodRelationship = relationship.into_serde().wasm_result()?; self .0 .attach_method_relationship(&did_url.0, relationship.into()) @@ -700,7 +698,4 @@ extern "C" { #[wasm_bindgen(typescript_type = "Map")] pub type MapStringAny; - - #[wasm_bindgen(typescript_type = "MethodRelationship")] - pub type MethodRelationshipType; } From 9081f5a5529f19954f532d705c0e34e4db770feb Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 2 Mar 2022 22:56:57 +0100 Subject: [PATCH 17/19] fix return type and detach method --- bindings/wasm/docs/api-reference.md | 8 ++++---- bindings/wasm/src/did/wasm_document.rs | 12 +++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index 94f26a76c7..841b11a720 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -897,8 +897,8 @@ with the given Document. * [.resolveMethod(query, scope)](#Document+resolveMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.resolveSigningMethod(query)](#Document+resolveSigningMethod) ⇒ [VerificationMethod](#VerificationMethod) * [.revokeMerkleKey(query, index)](#Document+revokeMerkleKey) ⇒ boolean - * [.attachMethodRelationship(did_url, relationship)](#Document+attachMethodRelationship) - * [.detachMethodRelationship(did_url, relationship)](#Document+detachMethodRelationship) + * [.attachMethodRelationship(did_url, relationship)](#Document+attachMethodRelationship) ⇒ boolean + * [.detachMethodRelationship(did_url, relationship)](#Document+detachMethodRelationship) ⇒ boolean * [.signSelf(key_pair, method_query)](#Document+signSelf) * [.signDocument(document, key_pair, method_query)](#Document+signDocument) * [.signCredential(data, args, options)](#Document+signCredential) ⇒ [Credential](#Credential) @@ -1182,7 +1182,7 @@ Attempts to resolve the given method query into a method capable of signing a do -### document.attachMethodRelationship(did_url, relationship) +### document.attachMethodRelationship(did_url, relationship) ⇒ boolean Attaches the relationship to the given method, if the method exists. Note: The method needs to be in the set of verification methods, @@ -1197,7 +1197,7 @@ so it cannot be an embedded one. -### document.detachMethodRelationship(did_url, relationship) +### document.detachMethodRelationship(did_url, relationship) ⇒ boolean Detaches the given relationship from the given method, if the method exists. **Kind**: instance method of [Document](#Document) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 026c3bc72d..8b88d25dad 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -312,12 +312,11 @@ impl WasmDocument { &mut self, did_url: &WasmDIDUrl, relationship: WasmMethodRelationship, - ) -> Result<()> { + ) -> Result { self .0 .attach_method_relationship(&did_url.0, relationship.into()) - .wasm_result()?; - Ok(()) + .wasm_result() } /// Detaches the given relationship from the given method, if the method exists. @@ -326,12 +325,11 @@ impl WasmDocument { &mut self, did_url: &WasmDIDUrl, relationship: WasmMethodRelationship, - ) -> Result<()> { + ) -> Result { self .0 - .attach_method_relationship(&did_url.0, relationship.into()) - .wasm_result()?; - Ok(()) + .detach_method_relationship(&did_url.0, relationship.into()) + .wasm_result() } // =========================================================================== From e34f38d4c5dcc823900b45549532f2128daf535b Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Mon, 7 Mar 2022 14:29:07 +0100 Subject: [PATCH 18/19] improvements --- bindings/wasm/docs/api-reference.md | 11 ++++------- bindings/wasm/src/did/mod.rs | 1 + bindings/wasm/src/did/wasm_document.rs | 17 +++++++---------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index 841b11a720..b4a43a5f1c 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -1035,9 +1035,6 @@ Use `null` to remove all controllers. ### document.controller() ⇒ [Array.<DID>](#DID) Returns a list of document controllers. -Note: Duplicates will be ignored. -Use `null` to remove all controllers. - **Kind**: instance method of [Document](#Document) @@ -1075,19 +1072,19 @@ This method can overwrite existing properties like `id` and result in an invalid ### document.properties() ⇒ Map.<string, any> -Returns the custom DID Document properties. +Returns a copy of the custom DID Document properties. **Kind**: instance method of [Document](#Document) ### document.service() ⇒ [Array.<Service>](#Service) -Return a set of all `Service`s in the document. +Return a set of all [Service](#Service)s in the document. **Kind**: instance method of [Document](#Document) ### document.insertService(service) ⇒ boolean -Add a new `Service` to the document. +Add a new [Service](#Service) to the document. **Kind**: instance method of [Document](#Document) @@ -1098,7 +1095,7 @@ Add a new `Service` to the document. ### document.removeService(did) -Remove a `Service` identified by the given `DIDUrl` from the document. +Remove a [Service](#Service) identified by the given [DIDUrl](#DIDUrl) from the document. **Kind**: instance method of [Document](#Document) diff --git a/bindings/wasm/src/did/mod.rs b/bindings/wasm/src/did/mod.rs index 202a667e31..15ea5e1cf8 100644 --- a/bindings/wasm/src/did/mod.rs +++ b/bindings/wasm/src/did/mod.rs @@ -7,6 +7,7 @@ pub use self::wasm_did_url::WasmDIDUrl; pub use self::wasm_diff_message::WasmDiffMessage; pub use self::wasm_document::WasmDocument; pub use self::wasm_document_metadata::WasmDocumentMetadata; +pub use self::wasm_method_relationship::WasmMethodRelationship; pub use self::wasm_method_scope::WasmMethodScope; pub use self::wasm_method_type::WasmMethodType; pub use self::wasm_resolved_document::PromiseArrayResolvedDocument; diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 8b88d25dad..14dcf9b71e 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -122,9 +122,6 @@ impl WasmDocument { } /// Returns a list of document controllers. - /// - /// Note: Duplicates will be ignored. - /// Use `null` to remove all controllers. #[wasm_bindgen] pub fn controller(&self) -> ArrayDID { match self.0.controller() { @@ -185,7 +182,7 @@ impl WasmDocument { Ok(()) } - /// Returns the custom DID Document properties. + /// Returns a copy of the custom DID Document properties. #[wasm_bindgen] pub fn properties(&mut self) -> Result { let properties_map = js_sys::Map::new(); @@ -199,7 +196,7 @@ impl WasmDocument { // Services // =========================================================================== - /// Return a set of all `Service`s in the document. + /// Return a set of all {@link Service}s in the document. #[wasm_bindgen] pub fn service(&self) -> ArrayService { self @@ -213,13 +210,13 @@ impl WasmDocument { .unchecked_into::() } - /// Add a new `Service` to the document. + /// Add a new {@link Service} to the document. #[wasm_bindgen(js_name = insertService)] pub fn insert_service(&mut self, service: &WasmService) -> Result { Ok(self.0.insert_service(service.0.clone())) } - /// Remove a `Service` identified by the given `DIDUrl` from the document. + /// Remove a {@link Service} identified by the given {@link DIDUrl} from the document. #[wasm_bindgen(js_name = removeService)] pub fn remove_service(&mut self, did: &WasmDIDUrl) -> Result<()> { self.0.remove_service(&did.0).wasm_result() @@ -231,7 +228,7 @@ impl WasmDocument { /// Returns a list of all {@link VerificationMethod} in the DID Document. #[wasm_bindgen] - pub fn methods(&self) -> ArrayMethods { + pub fn methods(&self) -> ArrayVerificationMethods { self .0 .methods() @@ -239,7 +236,7 @@ impl WasmDocument { .map(WasmVerificationMethod::from) .map(JsValue::from) .collect::() - .unchecked_into::() + .unchecked_into::() } /// Adds a new Verification Method to the DID Document. #[wasm_bindgen(js_name = insertMethod)] @@ -692,7 +689,7 @@ extern "C" { pub type ArrayString; #[wasm_bindgen(typescript_type = "VerificationMethod[]")] - pub type ArrayMethods; + pub type ArrayVerificationMethods; #[wasm_bindgen(typescript_type = "Map")] pub type MapStringAny; From 7597d8ad7165f24bb28c16e767c018b65ac7504e Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Mon, 7 Mar 2022 14:49:52 +0100 Subject: [PATCH 19/19] small improvement to @link docs --- bindings/wasm/docs/api-reference.md | 2 +- bindings/wasm/src/did/wasm_document.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/wasm/docs/api-reference.md b/bindings/wasm/docs/api-reference.md index b4a43a5f1c..360dd867f5 100644 --- a/bindings/wasm/docs/api-reference.md +++ b/bindings/wasm/docs/api-reference.md @@ -1078,7 +1078,7 @@ Returns a copy of the custom DID Document properties. ### document.service() ⇒ [Array.<Service>](#Service) -Return a set of all [Service](#Service)s in the document. +Return a set of all [Services](#Service) in the document. **Kind**: instance method of [Document](#Document) diff --git a/bindings/wasm/src/did/wasm_document.rs b/bindings/wasm/src/did/wasm_document.rs index 14dcf9b71e..e2a0f36a69 100644 --- a/bindings/wasm/src/did/wasm_document.rs +++ b/bindings/wasm/src/did/wasm_document.rs @@ -196,7 +196,7 @@ impl WasmDocument { // Services // =========================================================================== - /// Return a set of all {@link Service}s in the document. + /// Return a set of all {@link Service Services} in the document. #[wasm_bindgen] pub fn service(&self) -> ArrayService { self