Skip to content

Commit

Permalink
More identifier checks in CoreDocument (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver E. Anderson committed Nov 15, 2022
1 parent 11d537b commit d4cdbeb
Show file tree
Hide file tree
Showing 28 changed files with 1,532 additions and 451 deletions.
2 changes: 1 addition & 1 deletion bindings/stronghold-nodejs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 18 additions & 12 deletions bindings/wasm/src/did/wasm_core_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ impl WasmCoreDocument {
}

/// Sets the DID of the document.
///
/// ### Warning
///
/// Changing the identifier can drastically alter the results of
/// [`Self::resolve_method`](CoreDocument::resolve_method()),
/// [`Self::resolve_service`](CoreDocument::resolve_service()) and the related [DID URL dereferencing](https://w3c-ccg.github.io/did-resolution/#dereferencing) algorithm.
#[wasm_bindgen(js_name = setId)]
pub fn set_id(&mut self, id: &WasmCoreDID) {
*self.0.id_mut() = id.0.clone();
*self.0.id_mut_unchecked() = id.0.clone();
}

/// Returns a copy of the document controllers.
Expand Down Expand Up @@ -227,16 +233,17 @@ impl WasmCoreDocument {
/// If the value is set to `null`, the custom property will be removed.
///
/// ### WARNING
///
/// This method can overwrite existing properties like `id` and result in an invalid document.
#[wasm_bindgen(js_name = setPropertyUnchecked)]
pub fn set_property_unchecked(&mut self, key: String, value: &JsValue) -> Result<()> {
let value: Option<serde_json::Value> = value.into_serde().wasm_result()?;
match value {
Some(value) => {
self.0.properties_mut().insert(key, value);
self.0.properties_mut_unchecked().insert(key, value);
}
None => {
self.0.properties_mut().remove(&key);
self.0.properties_mut_unchecked().remove(&key);
}
}
Ok(())
Expand All @@ -262,19 +269,19 @@ impl WasmCoreDocument {

/// Add a new {@link CoreService} to the document.
///
/// Returns `true` if the service was added.
/// Errors if there already exists a service or verification method with the same id.
#[wasm_bindgen(js_name = insertService)]
pub fn insert_service(&mut self, service: &WasmCoreService) -> bool {
self.0.service_mut().append(service.0.clone())
pub fn insert_service(&mut self, service: &WasmCoreService) -> Result<()> {
self.0.insert_service(service.0.clone()).wasm_result()
}

/// Remoce a {@link CoreService} identified by the given {@link CoreDIDUrl} from the document.
///
/// Returns `true` if the service was removed.
#[wasm_bindgen(js_name = removeService)]
#[allow(non_snake_case)]
pub fn remove_service(&mut self, didUrl: &WasmCoreDIDUrl) -> bool {
self.0.service_mut().remove(&didUrl.0.clone())
pub fn remove_service(&mut self, didUrl: &WasmCoreDIDUrl) -> Option<WasmCoreService> {
self.0.remove_service(&didUrl.0.clone()).map(Into::into)
}

/// Returns the first {@link CoreService} with an `id` property matching the provided `query`,
Expand Down Expand Up @@ -330,14 +337,13 @@ impl WasmCoreDocument {
/// Adds a new `method` to the document in the given `scope`.
#[wasm_bindgen(js_name = insertMethod)]
pub fn insert_method(&mut self, method: &WasmCoreVerificationMethod, scope: &WasmMethodScope) -> Result<()> {
self.0.insert_method(method.0.clone(), scope.0).wasm_result()?;
Ok(())
self.0.insert_method(method.0.clone(), scope.0).wasm_result()
}

/// Removes all references to the specified Verification Method.
#[wasm_bindgen(js_name = removeMethod)]
pub fn remove_method(&mut self, did: &WasmCoreDIDUrl) -> Result<()> {
self.0.remove_method(&did.0).wasm_result()
pub fn remove_method(&mut self, did: &WasmCoreDIDUrl) -> Option<WasmCoreVerificationMethod> {
self.0.remove_method(&did.0).map(Into::into)
}

/// Returns a copy of the first verification method with an `id` property
Expand Down
17 changes: 9 additions & 8 deletions bindings/wasm/src/iota/iota_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,17 @@ impl WasmIotaDocument {
/// If the value is set to `null`, the custom property will be removed.
///
/// ### WARNING
///
/// This method can overwrite existing properties like `id` and result in an invalid document.
#[wasm_bindgen(js_name = setPropertyUnchecked)]
pub fn set_property_unchecked(&mut self, key: String, value: &JsValue) -> Result<()> {
let value: Option<serde_json::Value> = value.into_serde().wasm_result()?;
match value {
Some(value) => {
self.0.properties_mut().insert(key, value);
self.0.properties_mut_unchecked().insert(key, value);
}
None => {
self.0.properties_mut().remove(&key);
self.0.properties_mut_unchecked().remove(&key);
}
}
Ok(())
Expand Down Expand Up @@ -177,16 +178,16 @@ impl WasmIotaDocument {
///
/// Returns `true` if the service was added.
#[wasm_bindgen(js_name = insertService)]
pub fn insert_service(&mut self, service: &WasmIotaService) -> bool {
self.0.insert_service(service.0.clone())
pub fn insert_service(&mut self, service: &WasmIotaService) -> Result<()> {
self.0.insert_service(service.0.clone()).wasm_result()
}

/// Remove a {@link IotaService} identified by the given {@link IotaDIDUrl} from the document.
///
/// Returns `true` if a service was removed.
#[wasm_bindgen(js_name = removeService)]
pub fn remove_service(&mut self, did: &WasmIotaDIDUrl) -> bool {
self.0.remove_service(&did.0)
pub fn remove_service(&mut self, did: &WasmIotaDIDUrl) -> Option<WasmIotaService> {
self.0.remove_service(&did.0).map(Into::into)
}

/// Returns the first {@link IotaService} with an `id` property matching the provided `query`,
Expand Down Expand Up @@ -233,8 +234,8 @@ impl WasmIotaDocument {

/// Removes all references to the specified Verification Method.
#[wasm_bindgen(js_name = removeMethod)]
pub fn remove_method(&mut self, did: &WasmIotaDIDUrl) -> Result<()> {
self.0.remove_method(&did.0).wasm_result()
pub fn remove_method(&mut self, did: &WasmIotaDIDUrl) -> Option<WasmIotaVerificationMethod> {
self.0.remove_method(&did.0).map(Into::into)
}

/// Returns a copy of the first verification method with an `id` property
Expand Down
2 changes: 1 addition & 1 deletion bindings/wasm/src/resolver/wasm_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl WasmResolver {
let handler = move |did: CoreDID| {
let rc_client_clone: Rc<WasmIotaIdentityClient> = rc_client.clone();
async move {
let iota_did: IotaDID = IotaDID::parse(did)?;
let iota_did: IotaDID = IotaDID::parse(did).map_err(identity_iota::iota::Error::DIDSyntaxError)?;
Self::client_as_handler(rc_client_clone.as_ref(), iota_did.into()).await
}
};
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/tests/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ describe("CoreDocument", function() {
assert.deepStrictEqual(list.length, 1);
assert.deepStrictEqual(list[0].toJSON(), resolved.toJSON());
// Remove
const remove = doc.removeService(resolved.id());
assert.deepStrictEqual(remove, true);
const removed = doc.removeService(resolved.id());
assert.deepStrictEqual(removed.toJSON(), resolved.toJSON());
assert.deepStrictEqual(doc.resolveService(fragment1), undefined);
assert.deepStrictEqual(doc.service().length, 0);
});
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/tests/iota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ describe("IotaDocument", function() {
assert.deepStrictEqual(list.length, 1);
assert.deepStrictEqual(list[0].toJSON(), resolved.toJSON());
// Remove
const remove = doc.removeService(resolved.id());
assert.deepStrictEqual(remove, true);
const removed = doc.removeService(resolved.id());
assert.deepStrictEqual(removed.toJSON(), resolved.toJSON());
assert.deepStrictEqual(doc.resolveService(fragment1), undefined);
assert.deepStrictEqual(doc.service().length, 0);
});
Expand Down
2 changes: 1 addition & 1 deletion examples/0_basic/1_update_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn main() -> anyhow::Result<()> {
"type": "LinkedDomains",
"serviceEndpoint": "https://iota.org/"
}))?;
assert!(document.insert_service(service));
assert!(document.insert_service(service).is_ok());
document.metadata.updated = Some(Timestamp::now_utc());

// Remove a verification method.
Expand Down
2 changes: 1 addition & 1 deletion examples/1_advanced/5_alias_output_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn main() -> anyhow::Result<()> {
];
for service in services {
let service: IotaService = Service::from_json_value(service)?;
assert!(document.insert_service(service));
assert!(document.insert_service(service).is_ok());
document.metadata.updated = Some(Timestamp::now_utc());

// Increase the storage deposit and publish the update.
Expand Down
2 changes: 1 addition & 1 deletion examples_legacy/account/unchecked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async fn main() -> Result<()> {

// Add a custom property to the document.
document
.properties_mut()
.properties_mut_unchecked()
.insert("myCustomPropertyKey".into(), "value".into());

// Override the updated field timestamp to 24 hours (= 86400 seconds) in the future,
Expand Down
8 changes: 4 additions & 4 deletions identity_account/src/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ async fn test_account_sync_integration_msg_update() {

let client: Client = Client::builder().network(network).build().await.unwrap();
let mut new_doc: IotaDocument = account.document().clone();
new_doc.properties_mut().insert("foo".into(), 123u32.into());
new_doc.properties_mut().insert("bar".into(), 456u32.into());
new_doc.properties_mut_unchecked().insert("foo".into(), 123u32.into());
new_doc.properties_mut_unchecked().insert("bar".into(), 456u32.into());
new_doc.metadata.previous_message_id = *account.chain_state().last_integration_message_id();
new_doc.metadata.updated = Some(Timestamp::now_utc());
account
Expand Down Expand Up @@ -521,8 +521,8 @@ async fn test_account_sync_diff_msg_update() {

let client: Client = Client::builder().network(network).build().await.unwrap();
let mut new_doc: IotaDocument = account.document().clone();
new_doc.properties_mut().insert("foo".into(), 123u32.into());
new_doc.properties_mut().insert("bar".into(), 456u32.into());
new_doc.properties_mut_unchecked().insert("foo".into(), 123u32.into());
new_doc.properties_mut_unchecked().insert("bar".into(), 456u32.into());
new_doc.metadata.updated = Some(Timestamp::now_utc());
let mut diff_msg: DiffMessage = DiffMessage::new(
account.document(),
Expand Down
2 changes: 1 addition & 1 deletion identity_account/src/updates/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Update {
// Check method identifier is not duplicated.
let method_url: IotaDIDUrl = did.to_url().join(fragment.identifier())?;
if document.resolve_method(method_url, None).is_some() {
return Err(crate::Error::DIDError(identity_did::Error::MethodAlreadyExists));
return Err(crate::Error::DIDError(identity_did::Error::MethodInsertionError));
}

// Generate or extract the private key and/or retrieve the public key.
Expand Down
Loading

0 comments on commit d4cdbeb

Please sign in to comment.