Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Change document metadata created, updated to be optional #753

Merged
merged 8 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions bindings/wasm/docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1398,9 +1398,9 @@ Deserializes a `DiffMessage` from a JSON object.
* ~~[.mergeDiff(diff)](#Document+mergeDiff)~~
* [.integrationIndex()](#Document+integrationIndex) ⇒ <code>string</code>
* [.metadata()](#Document+metadata) ⇒ [<code>DocumentMetadata</code>](#DocumentMetadata)
* [.metadataCreated()](#Document+metadataCreated) ⇒ [<code>Timestamp</code>](#Timestamp)
* [.metadataCreated()](#Document+metadataCreated) ⇒ [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code>
* [.setMetadataCreated(timestamp)](#Document+setMetadataCreated)
* [.metadataUpdated()](#Document+metadataUpdated) ⇒ [<code>Timestamp</code>](#Timestamp)
* [.metadataUpdated()](#Document+metadataUpdated) ⇒ [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code>
* [.setMetadataUpdated(timestamp)](#Document+setMetadataUpdated)
* [.metadataPreviousMessageId()](#Document+metadataPreviousMessageId) ⇒ <code>string</code>
* [.setMetadataPreviousMessageId(value)](#Document+setMetadataPreviousMessageId)
Expand Down Expand Up @@ -1812,7 +1812,7 @@ NOTE: Copies all the metadata. See also `metadataCreated`, `metadataUpdated`,
**Kind**: instance method of [<code>Document</code>](#Document)
<a name="Document+metadataCreated"></a>

### document.metadataCreated() ⇒ [<code>Timestamp</code>](#Timestamp)
### document.metadataCreated() ⇒ [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code>
Returns a copy of the timestamp of when the DID document was created.

**Kind**: instance method of [<code>Document</code>](#Document)
Expand All @@ -1825,11 +1825,11 @@ Sets the timestamp of when the DID document was created.

| Param | Type |
| --- | --- |
| timestamp | [<code>Timestamp</code>](#Timestamp) |
| timestamp | [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code> |

<a name="Document+metadataUpdated"></a>

### document.metadataUpdated() ⇒ [<code>Timestamp</code>](#Timestamp)
### document.metadataUpdated() ⇒ [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code>
Returns a copy of the timestamp of the last DID document update.

**Kind**: instance method of [<code>Document</code>](#Document)
Expand All @@ -1842,7 +1842,7 @@ Sets the timestamp of the last DID document update.

| Param | Type |
| --- | --- |
| timestamp | [<code>Timestamp</code>](#Timestamp) |
| timestamp | [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code> |

<a name="Document+metadataPreviousMessageId"></a>

Expand Down Expand Up @@ -2033,8 +2033,8 @@ Additional attributes related to an IOTA DID Document.

* [DocumentMetadata](#DocumentMetadata)
* [.previousMessageId](#DocumentMetadata+previousMessageId) ⇒ <code>string</code>
* [.created()](#DocumentMetadata+created) ⇒ [<code>Timestamp</code>](#Timestamp)
* [.updated()](#DocumentMetadata+updated) ⇒ [<code>Timestamp</code>](#Timestamp)
* [.created()](#DocumentMetadata+created) ⇒ [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code>
* [.updated()](#DocumentMetadata+updated) ⇒ [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code>
* [.clone()](#DocumentMetadata+clone) ⇒ [<code>DocumentMetadata</code>](#DocumentMetadata)

<a name="DocumentMetadata+previousMessageId"></a>
Expand All @@ -2043,13 +2043,13 @@ Additional attributes related to an IOTA DID Document.
**Kind**: instance property of [<code>DocumentMetadata</code>](#DocumentMetadata)
<a name="DocumentMetadata+created"></a>

### documentMetadata.created() ⇒ [<code>Timestamp</code>](#Timestamp)
### documentMetadata.created() ⇒ [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code>
Returns a copy of the timestamp of when the DID document was created.

**Kind**: instance method of [<code>DocumentMetadata</code>](#DocumentMetadata)
<a name="DocumentMetadata+updated"></a>

### documentMetadata.updated() ⇒ [<code>Timestamp</code>](#Timestamp)
### documentMetadata.updated() ⇒ [<code>Timestamp</code>](#Timestamp) \| <code>undefined</code>
Returns a copy of the timestamp of the last DID document update.

**Kind**: instance method of [<code>DocumentMetadata</code>](#DocumentMetadata)
Expand Down
24 changes: 16 additions & 8 deletions bindings/wasm/src/did/wasm_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::str::FromStr;
use identity::core::OneOrMany;
use identity::core::OneOrSet;
use identity::core::OrderedSet;
use identity::core::Timestamp;
use identity::core::Url;
use identity::crypto::PrivateKey;
use identity::crypto::SignatureOptions;
Expand Down Expand Up @@ -559,26 +560,30 @@ impl WasmDocument {

/// Returns a copy of the timestamp of when the DID document was created.
#[wasm_bindgen(js_name = metadataCreated)]
pub fn metadata_created(&self) -> WasmTimestamp {
WasmTimestamp::from(self.0.metadata.created)
pub fn metadata_created(&self) -> Option<WasmTimestamp> {
self.0.metadata.created.map(WasmTimestamp::from)
}

/// Sets the timestamp of when the DID document was created.
#[wasm_bindgen(js_name = setMetadataCreated)]
pub fn set_metadata_created(&mut self, timestamp: &WasmTimestamp) {
self.0.metadata.created = timestamp.0;
pub fn set_metadata_created(&mut self, timestamp: OptionTimestamp) -> Result<()> {
let timestamp: Option<Timestamp> = timestamp.into_serde().wasm_result()?;
self.0.metadata.created = timestamp;
Ok(())
}

/// Returns a copy of the timestamp of the last DID document update.
#[wasm_bindgen(js_name = metadataUpdated)]
pub fn metadata_updated(&self) -> WasmTimestamp {
WasmTimestamp::from(self.0.metadata.updated)
pub fn metadata_updated(&self) -> Option<WasmTimestamp> {
self.0.metadata.updated.map(WasmTimestamp::from)
}

/// Sets the timestamp of the last DID document update.
#[wasm_bindgen(js_name = setMetadataUpdated)]
pub fn set_metadata_updated(&mut self, timestamp: &WasmTimestamp) {
self.0.metadata.updated = timestamp.0;
pub fn set_metadata_updated(&mut self, timestamp: OptionTimestamp) -> Result<()> {
let timestamp: Option<Timestamp> = timestamp.into_serde().wasm_result()?;
self.0.metadata.updated = timestamp;
Ok(())
}

/// Returns a copy of the previous integration chain message id.
Expand Down Expand Up @@ -663,4 +668,7 @@ extern "C" {

#[wasm_bindgen(typescript_type = "Map<string, any>")]
pub type MapStringAny;

#[wasm_bindgen(typescript_type = "Timestamp | undefined")]
pub type OptionTimestamp;
}
8 changes: 4 additions & 4 deletions bindings/wasm/src/did/wasm_document_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ pub struct WasmDocumentMetadata(pub(crate) IotaDocumentMetadata);
impl WasmDocumentMetadata {
/// Returns a copy of the timestamp of when the DID document was created.
#[wasm_bindgen]
pub fn created(&self) -> WasmTimestamp {
WasmTimestamp::from(self.0.created)
pub fn created(&self) -> Option<WasmTimestamp> {
self.0.created.map(WasmTimestamp::from)
}

/// Returns a copy of the timestamp of the last DID document update.
#[wasm_bindgen]
pub fn updated(&self) -> WasmTimestamp {
WasmTimestamp::from(self.0.updated)
pub fn updated(&self) -> Option<WasmTimestamp> {
self.0.updated.map(WasmTimestamp::from)
}

#[wasm_bindgen(getter = previousMessageId)]
Expand Down
2 changes: 1 addition & 1 deletion examples/account/unchecked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn main() -> Result<()> {
// Override the updated field timestamp to 24 hours (= 86400 seconds) in the future,
// because we can. This is usually set automatically by Account::update_identity.
let timestamp: Timestamp = Timestamp::from_unix(Timestamp::now_utc().to_unix() + 86400)?;
document.metadata.updated = timestamp;
document.metadata.updated = Some(timestamp);

// Update the identity without validation and publish the result to the Tangle
// (depending on the account's autopublish setting).
Expand Down
2 changes: 1 addition & 1 deletion examples/low-level-api/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub async fn add_new_key(

// Prepare the update
updated_doc.metadata.previous_message_id = *receipt.message_id();
updated_doc.metadata.updated = Timestamp::now_utc();
updated_doc.metadata.updated = Some(Timestamp::now_utc());
updated_doc.sign_self(key.private(), updated_doc.default_signing_method()?.id().clone())?;

// Publish the update to the Tangle
Expand Down
2 changes: 1 addition & 1 deletion examples/low-level-api/manipulate_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn run() -> Result<(IotaDocument, KeyPair, KeyPair, Receipt, Receipt)>
// This is REQUIRED in order for the messages to form a chain.
// Skipping / forgetting this will render the publication useless.
document.metadata.previous_message_id = *receipt.message_id();
document.metadata.updated = Timestamp::now_utc();
document.metadata.updated = Some(Timestamp::now_utc());

// Sign the DID Document with the original private key.
document.sign_self(keypair.private(), document.default_signing_method()?.id().clone())?;
Expand Down
4 changes: 2 additions & 2 deletions examples/low-level-api/resolve_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async fn main() -> Result<()> {
// This is REQUIRED in order for the messages to form a chain.
// Skipping / forgetting this will render the publication useless.
int_doc_1.metadata.previous_message_id = *original_receipt.message_id();
int_doc_1.metadata.updated = Timestamp::now_utc();
int_doc_1.metadata.updated = Some(Timestamp::now_utc());

// Sign the DID Document with the original private key.
int_doc_1.sign_self(keypair.private(), int_doc_1.default_signing_method()?.id().clone())?;
Expand Down Expand Up @@ -130,7 +130,7 @@ async fn main() -> Result<()> {
// Note: the `previous_message_id` points to the `message_id` of the last integration chain
// update.
int_doc_2.metadata.previous_message_id = *int_receipt_1.message_id();
int_doc_2.metadata.updated = Timestamp::now_utc();
int_doc_2.metadata.updated = Some(Timestamp::now_utc());

int_doc_2.sign_self(keypair.private(), int_doc_2.default_signing_method()?.id().clone())?;
int_doc_2
Expand Down
4 changes: 2 additions & 2 deletions examples/low-level-api/revoke_vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn main() -> Result<()> {
let (mut issuer_doc, issuer_key, issuer_receipt) = issuer;
issuer_doc.remove_method(&issuer_doc.id().to_url().join("#newKey")?)?;
issuer_doc.metadata.previous_message_id = *issuer_receipt.message_id();
issuer_doc.metadata.updated = Timestamp::now_utc();
issuer_doc.metadata.updated = Some(Timestamp::now_utc());
issuer_doc.sign_self(issuer_key.private(), issuer_doc.default_signing_method()?.id().clone())?;
// This is an integration chain update, so we publish the full document.
let update_receipt = client.publish_document(&issuer_doc).await?;
Expand Down Expand Up @@ -135,7 +135,7 @@ pub async fn add_new_key(

// Prepare the update
updated_doc.metadata.previous_message_id = *receipt.message_id();
updated_doc.metadata.updated = Timestamp::now_utc();
updated_doc.metadata.updated = Some(Timestamp::now_utc());
updated_doc.sign_self(key.private(), updated_doc.default_signing_method()?.id().clone())?;

// Publish the update to the Tangle
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 @@ -417,7 +417,7 @@ async fn test_account_sync_integration_msg_update() {
new_doc.properties_mut().insert("foo".into(), 123.into());
new_doc.properties_mut().insert("bar".into(), 456.into());
new_doc.metadata.previous_message_id = *account.chain_state().last_integration_message_id();
new_doc.metadata.updated = Timestamp::now_utc();
new_doc.metadata.updated = Some(Timestamp::now_utc());
account
.sign(
IotaDocument::DEFAULT_METHOD_FRAGMENT,
Expand Down Expand Up @@ -459,7 +459,7 @@ async fn test_account_sync_diff_msg_update() {
let mut new_doc: IotaDocument = account.document().clone();
new_doc.properties_mut().insert("foo".into(), 123.into());
new_doc.properties_mut().insert("bar".into(), 456.into());
new_doc.metadata.updated = Timestamp::now_utc();
new_doc.metadata.updated = Some(Timestamp::now_utc());
let mut diff_msg: DiffMessage = DiffMessage::new(
account.document(),
&new_doc,
Expand Down
16 changes: 12 additions & 4 deletions identity-account/src/tests/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ async fn test_create_identity() -> Result<()> {
assert!(account.load_state().await.is_ok());

// Ensure timestamps were recently set.
assert!(state.document().metadata.created > Timestamp::from_unix(Timestamp::now_utc().to_unix() - 15).unwrap());
assert!(state.document().metadata.updated > Timestamp::from_unix(Timestamp::now_utc().to_unix() - 15).unwrap());
assert!(
state.document().metadata.created.unwrap() > Timestamp::from_unix(Timestamp::now_utc().to_unix() - 15).unwrap()
);
assert!(
state.document().metadata.updated.unwrap() > Timestamp::from_unix(Timestamp::now_utc().to_unix() - 15).unwrap()
);

Ok(())
}
Expand Down Expand Up @@ -217,7 +221,9 @@ async fn test_create_method_content_generate() -> Result<()> {
state.document().metadata.created
);
// Ensure `updated` was recently set.
assert!(state.document().metadata.updated > Timestamp::from_unix(Timestamp::now_utc().to_unix() - 15).unwrap());
assert!(
state.document().metadata.updated.unwrap() > Timestamp::from_unix(Timestamp::now_utc().to_unix() - 15).unwrap()
);
}
Ok(())
}
Expand Down Expand Up @@ -609,7 +615,9 @@ async fn test_delete_method() -> Result<()> {
state.document().metadata.created
);
// Ensure `updated` was recently set.
assert!(state.document().metadata.updated > Timestamp::from_unix(Timestamp::now_utc().to_unix() - 15).unwrap());
assert!(
state.document().metadata.updated.unwrap() > Timestamp::from_unix(Timestamp::now_utc().to_unix() - 15).unwrap()
);

// Deleting a non-existing methods fails.
let output = account.process_update(update).await;
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 @@ -290,7 +290,7 @@ impl Update {
}
}

state.document_mut().metadata.updated = Timestamp::now_utc();
state.document_mut().metadata.updated = Some(Timestamp::now_utc());

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion identity-diff/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
fn merge(&self, diff: Self::Type) -> crate::Result<Self> {
match (self, diff) {
(None, DiffOption::None) => Ok(None),
(Some(_), DiffOption::None) => Ok(self.clone()),
(Some(_), DiffOption::None) => Ok(None),
(None, DiffOption::Some(ref d)) => Ok(Some(<T>::from_diff(d.clone())?)),
(Some(t), DiffOption::Some(ref d)) => Ok(Some(t.merge(d.clone())?)),
}
Expand Down
Loading