Skip to content

Commit

Permalink
Add document merge to wasm bindings (#401)
Browse files Browse the repository at this point in the history
* Update document merge functions in wasm bindings

Add WasmDocumentDiff::merge()
Update parameter of WasmDocument::merge()

* Fix formatting

* Fix IntegrationChainHistory, DiffChainHistory bindings

* Remove redundant js_class, js_name attributes
  • Loading branch information
cycraig committed Sep 17, 2021
1 parent 2cc903a commit fa17597
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 114 deletions.
209 changes: 111 additions & 98 deletions bindings/wasm/docs/api-reference.md
Expand Up @@ -25,6 +25,8 @@
<dd></dd>
<dt><a href="#DidResponse">DidResponse</a></dt>
<dd></dd>
<dt><a href="#DiffChainHistory">DiffChainHistory</a></dt>
<dd></dd>
<dt><a href="#Document">Document</a></dt>
<dd></dd>
<dt><a href="#DocumentDiff">DocumentDiff</a></dt>
Expand All @@ -37,6 +39,8 @@
<dd></dd>
<dt><a href="#FeaturesResponse">FeaturesResponse</a></dt>
<dd></dd>
<dt><a href="#IntegrationChainHistory">IntegrationChainHistory</a></dt>
<dd></dd>
<dt><a href="#Introduction">Introduction</a></dt>
<dd></dd>
<dt><a href="#IntroductionProposal">IntroductionProposal</a></dt>
Expand Down Expand Up @@ -75,10 +79,6 @@
<dd></dd>
<dt><a href="#VerificationMethod">VerificationMethod</a></dt>
<dd></dd>
<dt><a href="#WasmDiffChainHistory">WasmDiffChainHistory</a></dt>
<dd></dd>
<dt><a href="#WasmIntegrationChainHistory">WasmIntegrationChainHistory</a></dt>
<dd></dd>
</dl>

## Members
Expand Down Expand Up @@ -700,6 +700,52 @@ Parses a `DID` from the input string.
| --- | --- |
| value | <code>any</code> |

<a name="DiffChainHistory"></a>

## DiffChainHistory
**Kind**: global class

* [DiffChainHistory](#DiffChainHistory)
* _instance_
* [.chainData()](#DiffChainHistory+chainData) ⇒ <code>Array.&lt;any&gt;</code>
* [.spam()](#DiffChainHistory+spam) ⇒ <code>Array.&lt;any&gt;</code>
* [.toJSON()](#DiffChainHistory+toJSON) ⇒ <code>any</code>
* _static_
* [.fromJSON(json)](#DiffChainHistory.fromJSON)[<code>DiffChainHistory</code>](#DiffChainHistory)

<a name="DiffChainHistory+chainData"></a>

### diffChainHistory.chainData() ⇒ <code>Array.&lt;any&gt;</code>
Returns a [`js_sys::Array`] of `$wasm_ty` as strings.

NOTE: this clones the field.

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

### diffChainHistory.spam() ⇒ <code>Array.&lt;any&gt;</code>
Returns a [`js_sys::Array`] of [`MessageIds`][MessageId] as strings.

NOTE: this clones the field.

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

### diffChainHistory.toJSON() ⇒ <code>any</code>
Serializes a `$ident` object as a JSON object.

**Kind**: instance method of [<code>DiffChainHistory</code>](#DiffChainHistory)
<a name="DiffChainHistory.fromJSON"></a>

### DiffChainHistory.fromJSON(json) ⇒ [<code>DiffChainHistory</code>](#DiffChainHistory)
Deserializes a `$ident` object from a JSON object.

**Kind**: static method of [<code>DiffChainHistory</code>](#DiffChainHistory)

| Param | Type |
| --- | --- |
| json | <code>any</code> |

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

## Document
Expand Down Expand Up @@ -971,13 +1017,13 @@ Generate the difference between two DID Documents and sign it
<a name="Document+merge"></a>

### document.merge(diff)
Verifies the `diff` signature and merges the changes into `self`.
Verifies a `DocumentDiff` signature and merges the changes into `self`.

**Kind**: instance method of [<code>Document</code>](#Document)

| Param | Type |
| --- | --- |
| diff | <code>string</code> |
| diff | [<code>DocumentDiff</code>](#DocumentDiff) |

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

Expand Down Expand Up @@ -1061,6 +1107,7 @@ Defines the difference between two DID [`Document`]s' JSON representations.
* [.previousMessageId](#DocumentDiff+previousMessageId) ⇒ <code>string</code>
* [.previousMessageId](#DocumentDiff+previousMessageId)
* [.id()](#DocumentDiff+id)[<code>DID</code>](#DID)
* [.merge(document)](#DocumentDiff+merge)[<code>Document</code>](#Document)

<a name="DocumentDiff+did"></a>

Expand Down Expand Up @@ -1118,6 +1165,18 @@ Returns the DID of the associated DID Document.
NOTE: clones the data.

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

### documentDiff.merge(document) ⇒ [<code>Document</code>](#Document)
Returns a new DID Document which is the result of merging `self`
with the given Document.

**Kind**: instance method of [<code>DocumentDiff</code>](#DocumentDiff)

| Param | Type |
| --- | --- |
| document | [<code>Document</code>](#Document) |

<a name="DocumentHistory"></a>

## DocumentHistory
Expand Down Expand Up @@ -1234,6 +1293,52 @@ Deserializes a [`WasmDocumentHistory`] object from a JSON object.
| --- | --- |
| value | <code>any</code> |

<a name="IntegrationChainHistory"></a>

## IntegrationChainHistory
**Kind**: global class

* [IntegrationChainHistory](#IntegrationChainHistory)
* _instance_
* [.chainData()](#IntegrationChainHistory+chainData) ⇒ <code>Array.&lt;any&gt;</code>
* [.spam()](#IntegrationChainHistory+spam) ⇒ <code>Array.&lt;any&gt;</code>
* [.toJSON()](#IntegrationChainHistory+toJSON) ⇒ <code>any</code>
* _static_
* [.fromJSON(json)](#IntegrationChainHistory.fromJSON)[<code>IntegrationChainHistory</code>](#IntegrationChainHistory)

<a name="IntegrationChainHistory+chainData"></a>

### integrationChainHistory.chainData() ⇒ <code>Array.&lt;any&gt;</code>
Returns a [`js_sys::Array`] of `$wasm_ty` as strings.

NOTE: this clones the field.

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

### integrationChainHistory.spam() ⇒ <code>Array.&lt;any&gt;</code>
Returns a [`js_sys::Array`] of [`MessageIds`][MessageId] as strings.

NOTE: this clones the field.

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

### integrationChainHistory.toJSON() ⇒ <code>any</code>
Serializes a `$ident` object as a JSON object.

**Kind**: instance method of [<code>IntegrationChainHistory</code>](#IntegrationChainHistory)
<a name="IntegrationChainHistory.fromJSON"></a>

### IntegrationChainHistory.fromJSON(json) ⇒ [<code>IntegrationChainHistory</code>](#IntegrationChainHistory)
Deserializes a `$ident` object from a JSON object.

**Kind**: static method of [<code>IntegrationChainHistory</code>](#IntegrationChainHistory)

| Param | Type |
| --- | --- |
| json | <code>any</code> |

<a name="Introduction"></a>

## Introduction
Expand Down Expand Up @@ -2072,98 +2177,6 @@ Deserializes a `VerificationMethod` object from a JSON object.
| --- | --- |
| value | <code>any</code> |

<a name="WasmDiffChainHistory"></a>

## WasmDiffChainHistory
**Kind**: global class

* [WasmDiffChainHistory](#WasmDiffChainHistory)
* _instance_
* [.chainData()](#WasmDiffChainHistory+chainData) ⇒ <code>Array.&lt;any&gt;</code>
* [.spam()](#WasmDiffChainHistory+spam) ⇒ <code>Array.&lt;any&gt;</code>
* [.toJSON()](#WasmDiffChainHistory+toJSON) ⇒ <code>any</code>
* _static_
* [.fromJSON(json)](#WasmDiffChainHistory.fromJSON)[<code>WasmDiffChainHistory</code>](#WasmDiffChainHistory)

<a name="WasmDiffChainHistory+chainData"></a>

### wasmDiffChainHistory.chainData() ⇒ <code>Array.&lt;any&gt;</code>
Returns a [`js_sys::Array`] of `$wasm_ty` as strings.

NOTE: this clones the field.

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

### wasmDiffChainHistory.spam() ⇒ <code>Array.&lt;any&gt;</code>
Returns a [`js_sys::Array`] of [`MessageIds`][MessageId] as strings.

NOTE: this clones the field.

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

### wasmDiffChainHistory.toJSON() ⇒ <code>any</code>
Serializes a `$ident` object as a JSON object.

**Kind**: instance method of [<code>WasmDiffChainHistory</code>](#WasmDiffChainHistory)
<a name="WasmDiffChainHistory.fromJSON"></a>

### WasmDiffChainHistory.fromJSON(json) ⇒ [<code>WasmDiffChainHistory</code>](#WasmDiffChainHistory)
Deserializes a `$ident` object from a JSON object.

**Kind**: static method of [<code>WasmDiffChainHistory</code>](#WasmDiffChainHistory)

| Param | Type |
| --- | --- |
| json | <code>any</code> |

<a name="WasmIntegrationChainHistory"></a>

## WasmIntegrationChainHistory
**Kind**: global class

* [WasmIntegrationChainHistory](#WasmIntegrationChainHistory)
* _instance_
* [.chainData()](#WasmIntegrationChainHistory+chainData) ⇒ <code>Array.&lt;any&gt;</code>
* [.spam()](#WasmIntegrationChainHistory+spam) ⇒ <code>Array.&lt;any&gt;</code>
* [.toJSON()](#WasmIntegrationChainHistory+toJSON) ⇒ <code>any</code>
* _static_
* [.fromJSON(json)](#WasmIntegrationChainHistory.fromJSON)[<code>WasmIntegrationChainHistory</code>](#WasmIntegrationChainHistory)

<a name="WasmIntegrationChainHistory+chainData"></a>

### wasmIntegrationChainHistory.chainData() ⇒ <code>Array.&lt;any&gt;</code>
Returns a [`js_sys::Array`] of `$wasm_ty` as strings.

NOTE: this clones the field.

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

### wasmIntegrationChainHistory.spam() ⇒ <code>Array.&lt;any&gt;</code>
Returns a [`js_sys::Array`] of [`MessageIds`][MessageId] as strings.

NOTE: this clones the field.

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

### wasmIntegrationChainHistory.toJSON() ⇒ <code>any</code>
Serializes a `$ident` object as a JSON object.

**Kind**: instance method of [<code>WasmIntegrationChainHistory</code>](#WasmIntegrationChainHistory)
<a name="WasmIntegrationChainHistory.fromJSON"></a>

### WasmIntegrationChainHistory.fromJSON(json) ⇒ [<code>WasmIntegrationChainHistory</code>](#WasmIntegrationChainHistory)
Deserializes a `$ident` object from a JSON object.

**Kind**: static method of [<code>WasmIntegrationChainHistory</code>](#WasmIntegrationChainHistory)

| Param | Type |
| --- | --- |
| json | <code>any</code> |

<a name="Digest"></a>

## Digest
Expand Down
8 changes: 4 additions & 4 deletions bindings/wasm/src/chain/document_history.rs
Expand Up @@ -97,11 +97,11 @@ impl From<DocumentHistory> for WasmDocumentHistory {

#[wasm_bindgen(inspectable)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct WasmIntegrationChainHistory(ChainHistory<IotaDocument>);
pub struct IntegrationChainHistory(ChainHistory<IotaDocument>);

#[wasm_bindgen(inspectable)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct WasmDiffChainHistory(ChainHistory<DocumentDiff>);
pub struct DiffChainHistory(ChainHistory<DocumentDiff>);

macro_rules! impl_wasm_chain_history {
($ident:ident, $ty:ty, $wasm_ty:ty) => {
Expand Down Expand Up @@ -158,5 +158,5 @@ macro_rules! impl_wasm_chain_history {
};
}

impl_wasm_chain_history!(WasmIntegrationChainHistory, IotaDocument, WasmDocument);
impl_wasm_chain_history!(WasmDiffChainHistory, DocumentDiff, WasmDocumentDiff);
impl_wasm_chain_history!(IntegrationChainHistory, IotaDocument, WasmDocument);
impl_wasm_chain_history!(DiffChainHistory, DocumentDiff, WasmDocumentDiff);
2 changes: 1 addition & 1 deletion bindings/wasm/src/chain/mod.rs
@@ -1,6 +1,6 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

pub use document_history::{WasmDiffChainHistory, WasmDocumentHistory, WasmIntegrationChainHistory};
pub use document_history::{DiffChainHistory, IntegrationChainHistory, WasmDocumentHistory};

mod document_history;
11 changes: 4 additions & 7 deletions bindings/wasm/src/did/wasm_document.rs
Expand Up @@ -4,7 +4,6 @@
use std::str::FromStr;

use identity::core::decode_b58;
use identity::core::FromJson;
use identity::crypto::merkle_key::MerkleDigestTag;
use identity::crypto::merkle_key::MerkleKey;
use identity::crypto::merkle_key::Sha256;
Expand All @@ -18,7 +17,7 @@ use identity::iota::IotaDID;
use identity::iota::IotaDocument;
use identity::iota::IotaVerificationMethod;
use identity::iota::MessageId;
use identity::iota::{DocumentDiff, TangleRef};
use identity::iota::TangleRef;
use wasm_bindgen::prelude::*;

use crate::common::WasmTimestamp;
Expand Down Expand Up @@ -338,12 +337,10 @@ impl WasmDocument {
.wasm_result()
}

/// Verifies the `diff` signature and merges the changes into `self`.
/// Verifies a `DocumentDiff` signature and merges the changes into `self`.
#[wasm_bindgen]
pub fn merge(&mut self, diff: &str) -> Result<()> {
let diff: DocumentDiff = DocumentDiff::from_json(diff).wasm_result()?;
self.0.merge(&diff).wasm_result()?;
Ok(())
pub fn merge(&mut self, diff: &WasmDocumentDiff) -> Result<()> {
self.0.merge(&diff.0).wasm_result()
}

// ===========================================================================
Expand Down
8 changes: 7 additions & 1 deletion bindings/wasm/src/did/wasm_document_diff.rs
Expand Up @@ -7,7 +7,7 @@ use std::str::FromStr;
use identity::iota::{DocumentDiff, MessageId, TangleRef};
use wasm_bindgen::prelude::*;

use crate::did::WasmDID;
use crate::did::{WasmDID, WasmDocument};
use crate::error::{Result, WasmResult};

/// Defines the difference between two DID [`Document`]s' JSON representations.
Expand Down Expand Up @@ -66,6 +66,12 @@ impl WasmDocumentDiff {
self.0.set_previous_message_id(previous_message_id);
Ok(())
}

/// Returns a new DID Document which is the result of merging `self`
/// with the given Document.
pub fn merge(&self, document: &WasmDocument) -> Result<WasmDocument> {
self.0.merge(&document.0).map(WasmDocument).wasm_result()
}
}

impl From<DocumentDiff> for WasmDocumentDiff {
Expand Down
6 changes: 3 additions & 3 deletions bindings/wasm/src/tangle/client.rs
Expand Up @@ -19,7 +19,7 @@ use js_sys::Promise;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::future_to_promise;

use crate::chain::{WasmDiffChainHistory, WasmDocumentHistory};
use crate::chain::{DiffChainHistory, WasmDocumentHistory};
use crate::did::{WasmDocument, WasmDocumentDiff};
use crate::error::{Result, WasmResult};
use crate::tangle::Config;
Expand Down Expand Up @@ -169,15 +169,15 @@ impl Client {
/// NOTE: the document must have been published to the tangle and have a valid message id and
/// authentication method.
#[wasm_bindgen(js_name = resolveDiffHistory)]
pub fn resolve_diffs(&self, document: &WasmDocument) -> Result<Promise> {
pub fn resolve_diff_history(&self, document: &WasmDocument) -> Result<Promise> {
let client: Rc<IotaClient> = self.client.clone();
let iota_document: IotaDocument = document.0.clone();

let promise: Promise = future_to_promise(async move {
client
.resolve_diff_history(&iota_document)
.await
.map(WasmDiffChainHistory::from)
.map(DiffChainHistory::from)
.map(JsValue::from)
.wasm_result()
});
Expand Down

0 comments on commit fa17597

Please sign in to comment.