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

Example Cleanup #292

Merged
merged 15 commits into from
Jul 13, 2021
56 changes: 16 additions & 40 deletions bindings/wasm/docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,7 @@
* [Client](#Client)
* [new Client()](#new_Client_new)
* _instance_
* [.defaultNodeURL](#Client+defaultNodeURL) ⇒ <code>string</code>
* [.explorerURL](#Client+explorerURL) ⇒ <code>string</code>
* [.network()](#Client+network) ⇒ [<code>Network</code>](#Network)
* [.transactionURL(message_id)](#Client+transactionURL) ⇒ <code>string</code>
* [.publishDocument(document)](#Client+publishDocument) ⇒ <code>Promise.&lt;any&gt;</code>
* [.publishDiff(message_id, value)](#Client+publishDiff) ⇒ <code>Promise.&lt;any&gt;</code>
* [.resolve(did)](#Client+resolve) ⇒ <code>Promise.&lt;any&gt;</code>
Expand All @@ -160,35 +157,12 @@
### new Client()
Creates a new `Client` with default settings.

<a name="Client+defaultNodeURL"></a>

### client.defaultNodeURL ⇒ <code>string</code>
Returns the default node URL of the `Client` network.

**Kind**: instance property of [<code>Client</code>](#Client)
<a name="Client+explorerURL"></a>

### client.explorerURL ⇒ <code>string</code>
Returns the web explorer URL of the `Client` network.

**Kind**: instance property of [<code>Client</code>](#Client)
<a name="Client+network"></a>

### client.network() ⇒ [<code>Network</code>](#Network)
Returns the `Client` Tangle network.

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

### client.transactionURL(message_id) ⇒ <code>string</code>
Returns the web explorer URL of the given `transaction`.

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

| Param | Type |
| --- | --- |
| message_id | <code>string</code> |

<a name="Client+publishDocument"></a>

### client.publishDocument(document) ⇒ <code>Promise.&lt;any&gt;</code>
Expand Down Expand Up @@ -562,8 +536,6 @@ Creates a new `Client` with default settings for the given `Network`.
* [.network](#DID+network) ⇒ [<code>Network</code>](#Network)
* [.networkName](#DID+networkName) ⇒ <code>string</code>
* [.tag](#DID+tag) ⇒ <code>string</code>
* [.tangleNode](#DID+tangleNode) ⇒ <code>string</code>
* [.tangleExplorer](#DID+tangleExplorer) ⇒ <code>string</code>
* [.toString()](#DID+toString) ⇒ <code>string</code>
* _static_
* [.fromBase58(key, network)](#DID.fromBase58) ⇒ [<code>DID</code>](#DID)
Expand Down Expand Up @@ -597,18 +569,6 @@ Returns the IOTA tangle network of the `DID`.
### did.tag ⇒ <code>string</code>
Returns the unique tag of the `DID`.

**Kind**: instance property of [<code>DID</code>](#DID)
<a name="DID+tangleNode"></a>

### did.tangleNode ⇒ <code>string</code>
Returns the node URL of the target Tangle network.

**Kind**: instance property of [<code>DID</code>](#DID)
<a name="DID+tangleExplorer"></a>

### did.tangleExplorer ⇒ <code>string</code>
Returns the web explorer URL of the target Tangle network.

**Kind**: instance property of [<code>DID</code>](#DID)
<a name="DID+toString"></a>

Expand Down Expand Up @@ -1249,6 +1209,7 @@ Deserializes a `KeyPair` object from a JSON object.
* _instance_
* [.defaultNodeURL](#Network+defaultNodeURL) ⇒ <code>string</code>
* [.explorerURL](#Network+explorerURL) ⇒ <code>string</code>
* [.messageURL(message_id)](#Network+messageURL) ⇒ <code>string</code>
* [.toString()](#Network+toString) ⇒ <code>string</code>
* _static_
* [.mainnet()](#Network.mainnet) ⇒ [<code>Network</code>](#Network)
Expand All @@ -1257,11 +1218,26 @@ Deserializes a `KeyPair` object from a JSON object.
<a name="Network+defaultNodeURL"></a>

### network.defaultNodeURL ⇒ <code>string</code>
Returns the node URL of the Tangle network.

**Kind**: instance property of [<code>Network</code>](#Network)
<a name="Network+explorerURL"></a>

### network.explorerURL ⇒ <code>string</code>
Returns the web explorer URL of the Tangle network.

**Kind**: instance property of [<code>Network</code>](#Network)
<a name="Network+messageURL"></a>

### network.messageURL(message_id) ⇒ <code>string</code>
Returns the web explorer URL of the given `message`.

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

| Param | Type |
| --- | --- |
| message_id | <code>string</code> |

<a name="Network+toString"></a>

### network.toString() ⇒ <code>string</code>
Expand Down
53 changes: 16 additions & 37 deletions bindings/wasm/src/tangle/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use futures::executor;
use identity::core::FromJson;
use identity::credential::Credential;
use identity::credential::Presentation;
use identity::iota::Client as IotaClient;
use identity::iota::CredentialValidator;
use identity::iota::DocumentDiff;
use identity::iota::IotaDID;
use identity::iota::IotaDocument;
use identity::iota::MessageId;
use identity::iota::{Client as IotaClient, TangleRef};
use identity::iota::TangleRef;
use identity::iota::TangleResolve;
use js_sys::Promise;
use std::cell::RefCell;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::future_to_promise;
Expand All @@ -22,19 +23,17 @@ use crate::tangle::Config;
use crate::tangle::WasmNetwork;
use crate::utils::err;

type Shared<T> = Rc<RefCell<T>>;

#[wasm_bindgen]
#[derive(Debug)]
pub struct Client {
client: Shared<IotaClient>,
pub(crate) client: Rc<IotaClient>,
}

#[wasm_bindgen]
impl Client {
fn from_client(client: IotaClient) -> Client {
Self {
client: Rc::new(RefCell::new(client)),
client: Rc::new(client),
}
}

Expand Down Expand Up @@ -65,40 +64,21 @@ impl Client {
/// Returns the `Client` Tangle network.
#[wasm_bindgen]
pub fn network(&self) -> WasmNetwork {
self.client.borrow().network().into()
}

/// Returns the default node URL of the `Client` network.
#[wasm_bindgen(getter = defaultNodeURL)]
pub fn default_node_url(&self) -> String {
self.client.borrow().default_node_url().to_string()
}

/// Returns the web explorer URL of the `Client` network.
#[wasm_bindgen(getter = explorerURL)]
pub fn explorer_url(&self) -> String {
self.client.borrow().explorer_url().to_string()
}

/// Returns the web explorer URL of the given `transaction`.
#[wasm_bindgen(js_name = transactionURL)]
pub fn transaction_url(&self, message_id: &str) -> String {
self.client.borrow().transaction_url(message_id).to_string()
self.client.network().into()
}

/// Publishes an `IotaDocument` to the Tangle.
#[wasm_bindgen(js_name = publishDocument)]
pub fn publish_document(&self, document: &JsValue) -> Result<Promise, JsValue> {
let document: IotaDocument = document.into_serde().map_err(err)?;
let client: Shared<IotaClient> = self.client.clone();
let client: Rc<IotaClient> = self.client.clone();

let promise: Promise = future_to_promise(async move {
client
.borrow()
.publish_document(&document)
.await
.map_err(err)
.map(|message| message.to_string().into())
.and_then(|receipt| JsValue::from_serde(&receipt).map_err(err))
});

Ok(promise)
Expand All @@ -109,15 +89,14 @@ impl Client {
pub fn publish_diff(&self, message_id: &str, value: &JsValue) -> Result<Promise, JsValue> {
let diff: DocumentDiff = value.into_serde().map_err(err)?;
let message: MessageId = MessageId::from_str(message_id).map_err(err)?;
let client: Shared<IotaClient> = self.client.clone();
let client: Rc<IotaClient> = self.client.clone();

let promise: Promise = future_to_promise(async move {
client
.borrow()
.publish_diff(&message, &diff)
.await
.map_err(err)
.map(|message| message.to_string().into())
.and_then(|receipt| JsValue::from_serde(&receipt).map_err(err))
});

Ok(promise)
Expand All @@ -132,11 +111,11 @@ impl Client {
message_id: &'a MessageId,
}

let client: Shared<IotaClient> = self.client.clone();
let client: Rc<IotaClient> = self.client.clone();
let did: IotaDID = did.parse().map_err(err)?;

let promise: Promise = future_to_promise(async move {
client.borrow().resolve(&did).await.map_err(err).and_then(|document| {
client.resolve(&did).await.map_err(err).and_then(|document| {
let wrapper = DocWrapper {
document: &document,
message_id: document.message_id(),
Expand All @@ -152,11 +131,11 @@ impl Client {
/// Validates a credential with the DID Document from the Tangle.
#[wasm_bindgen(js_name = checkCredential)]
pub fn check_credential(&self, data: &str) -> Result<Promise, JsValue> {
let client: Shared<IotaClient> = self.client.clone();
let client: Rc<IotaClient> = self.client.clone();
let data: Credential = Credential::from_json(&data).map_err(err)?;

let promise: Promise = future_to_promise(async move {
CredentialValidator::new(&client.borrow())
CredentialValidator::new(&*client)
.validate_credential(data)
.await
.map_err(err)
Expand All @@ -169,11 +148,11 @@ impl Client {
/// Validates a presentation with the DID Document from the Tangle.
#[wasm_bindgen(js_name = checkPresentation)]
pub fn check_presentation(&self, data: &str) -> Result<Promise, JsValue> {
let client: Shared<IotaClient> = self.client.clone();
let client: Rc<IotaClient> = self.client.clone();
let data: Presentation = Presentation::from_json(&data).map_err(err)?;

let promise: Promise = future_to_promise(async move {
CredentialValidator::new(&client.borrow())
CredentialValidator::new(&*client)
.validate_presentation(data)
.await
.map_err(err)
Expand Down
10 changes: 9 additions & 1 deletion bindings/wasm/src/tangle/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ impl WasmNetwork {
Self(IotaNetwork::Testnet)
}

/// Returns the node URL of the Tangle network.
#[wasm_bindgen(getter = defaultNodeURL)]
pub fn default_node_url(&self) -> String {
self.0.node_url().to_string()
self.0.default_node_url().to_string()
}

/// Returns the web explorer URL of the Tangle network.
#[wasm_bindgen(getter = explorerURL)]
pub fn explorer_url(&self) -> String {
self.0.explorer_url().to_string()
}

/// Returns the web explorer URL of the given `message`.
#[wasm_bindgen(js_name = messageURL)]
pub fn message_url(&self, message_id: &str) -> String {
self.0.message_url(message_id).to_string()
}

#[allow(clippy::inherent_to_string, clippy::wrong_self_convention)]
#[wasm_bindgen(js_name = toString)]
pub fn to_string(&self) -> String {
Expand Down
13 changes: 0 additions & 13 deletions bindings/wasm/src/wasm_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use identity::core::decode_b58;
use identity::iota::IotaDID;
use identity::iota::Network;
use wasm_bindgen::prelude::*;

use crate::crypto::KeyPair;
Expand Down Expand Up @@ -59,18 +58,6 @@ impl WasmDID {
self.0.tag().into()
}

/// Returns the node URL of the target Tangle network.
#[wasm_bindgen(getter = tangleNode)]
pub fn tangle_node(&self) -> String {
Network::from_did(&self.0).node_url().to_string()
}

/// Returns the web explorer URL of the target Tangle network.
#[wasm_bindgen(getter = tangleExplorer)]
pub fn tangle_explorer(&self) -> String {
Network::from_did(&self.0).explorer_url().to_string()
}

/// Returns the `DID` object as a string.
#[allow(clippy::inherent_to_string)]
#[wasm_bindgen(js_name = toString)]
Expand Down
52 changes: 24 additions & 28 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,6 @@ tokio = { version = "1.5", features = ["full"] }
name = "getting_started"
path = "getting_started.rs"

[[example]]
name = "low_create_did"
path = "low-level-api/create_did_document.rs"

[[example]]
name = "low_manipulate_did"
path = "low-level-api/manipulate_did_document.rs"

[[example]]
name = "low_verifiable_credential"
path = "low-level-api/verifiable_credential.rs"

[[example]]
name = "low_verifiable_presentation"
path = "low-level-api/verifiable_presentation.rs"

[[example]]
name = "low_resolution"
path = "low-level-api/resolution.rs"

[[example]]
name = "low_diff_chain"
path = "low-level-api/diff_chain.rs"

[[example]]
name = "low_merkle_key"
path = "low-level-api/merkle_key.rs"

[[example]]
name = "account_basic"
path = "account/basic.rs"
Expand All @@ -65,3 +37,27 @@ path = "account/signing.rs"
[[example]]
name = "account_stronghold"
path = "account/stronghold.rs"

[[example]]
name = "create_did"
path = "low-level-api/create_did.rs"

[[example]]
name = "create_vc"
path = "low-level-api/create_vc.rs"

[[example]]
name = "create_vp"
path = "low-level-api/create_vp.rs"

[[example]]
name = "manipulate_did"
path = "low-level-api/manipulate_did.rs"

[[example]]
name = "merkle_key"
path = "low-level-api/merkle_key.rs"

[[example]]
name = "resolution"
path = "low-level-api/resolution.rs"
9 changes: 9 additions & 0 deletions examples/account/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use identity::account::Account;
use identity::account::AccountStorage;
use identity::account::AutoSave;
use identity::account::Result;
use identity::iota::Network;

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -20,6 +21,14 @@ async fn main() -> Result<()> {
.dropsave(false) // save the account state on drop
.milestone(4) // save a snapshot every 4 actions
.storage(AccountStorage::Memory) // use the default in-memory storage adapter
// configure the mainnet Tangle client
.client(Network::Mainnet, |builder| {
builder
.node("https://chrysalis-nodes.iota.org")
.unwrap() // unwrap is safe, we provided a valid node URL
.permanode("https://chrysalis-chronicle.iota.org/api/mainnet/", None, None)
.unwrap() // unwrap is safe, we provided a valid permanode URL
})
.build()
.await?;

Expand Down
Loading