Skip to content

Commit

Permalink
Fix Wasm DID conversion error names (#651)
Browse files Browse the repository at this point in the history
* Fix DID conversion errors in resolve, resolve_history

* Change `ExplorerUrl::resolverUrl` parameter to union typed DID

* Remove example logExplorerUrl, logResolverUrl utility functions

* Remove example prettyPrintJson utility function

* Simplify UWasmDID conversion

* Remove unused From implementation

* Fix Wasm example log formatting in browser

* Fix JSON formatting in browser examples
  • Loading branch information
cycraig committed Feb 11, 2022
1 parent c517925 commit 6393be7
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 76 deletions.
2 changes: 1 addition & 1 deletion bindings/wasm/docs/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ E.g. https://explorer.iota.org/mainnet/identity-resolver/{did}

| Param | Type |
| --- | --- |
| did | <code>string</code> |
| did | [<code>DID</code>](#DID) \| <code>string</code> |

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

Expand Down
5 changes: 2 additions & 3 deletions bindings/wasm/examples/src/create_did.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

import {Client, Config, Document, KeyPair, KeyType} from '@iota/identity-wasm';
import {logExplorerUrl, logResolverUrl} from './utils';

/**
This example shows a basic introduction on how to create a basic DID Document and upload it to the Tangle.
Expand Down Expand Up @@ -32,8 +31,8 @@ async function createIdentity(clientConfig) {
const receipt = await client.publishDocument(doc);

// Log the results.
logExplorerUrl("DID Document Transaction:", clientConfig.explorer, receipt.messageId);
logResolverUrl("Explore the DID Document:", clientConfig.explorer, doc.id.toString());
console.log(`DID Document Transaction: ${clientConfig.explorer.messageUrl(receipt.messageId)}`);
console.log(`Explore the DID Document: ${clientConfig.explorer.resolverUrl(doc.id)}`);

// Return the results.
return {key, doc, receipt};
Expand Down
5 changes: 2 additions & 3 deletions bindings/wasm/examples/src/diff_chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import {Client, Config, Document, Service, Timestamp} from '@iota/identity-wasm';
import {createIdentity} from "./create_did";
import {logExplorerUrl, logResolverUrl} from "./utils";

/**
This example is a basic introduction to creating a diff message and publishing it to the tangle.
Expand Down Expand Up @@ -44,8 +43,8 @@ async function createDiff(clientConfig) {
// Publish diff to the Tangle
const diffReceipt = await client.publishDiff(receipt.messageId, diff);
console.log(diffReceipt);
logExplorerUrl("Diff Chain Transaction:", clientConfig.explorer, diffReceipt.messageId);
logResolverUrl("Explore the DID Document:", clientConfig.explorer, doc.id.toString());
console.log(`Diff Chain Transaction: ${clientConfig.explorer.messageUrl(diffReceipt.messageId)}`);
console.log(`Explore the DID Document: ${clientConfig.explorer.resolverUrl(doc.id)}`);

return {updatedDoc, key, diffMessageId: diffReceipt.messageId};
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/wasm/examples/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@
</button>

<h4 class="ma-md">Output:</h4>
<div id="content" class="content"></div>
<pre id="content" class="content"></pre>
</body>
</html>
</html>
6 changes: 3 additions & 3 deletions bindings/wasm/examples/src/manipulate_did.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
VerificationMethod
} from '@iota/identity-wasm';
import {createIdentity} from "./create_did";
import {logExplorerUrl, logResolverUrl} from "./utils";

/**
This example shows how to add more to an existing DID Document.
Expand Down Expand Up @@ -63,8 +62,9 @@ async function manipulateIdentity(clientConfig) {
const updateReceipt = await client.publishDocument(doc);

// Log the results.
logExplorerUrl("DID Document Update Transaction:", clientConfig.explorer, updateReceipt.messageId);
logResolverUrl("Explore the DID Document:", clientConfig.explorer, doc.id.toString());
console.log(`DID Document Update Transaction: ${clientConfig.explorer.messageUrl(updateReceipt.messageId)}`);
console.log(`Explore the DID Document: ${clientConfig.explorer.resolverUrl(doc.id)}`);

return {
key,
newKey,
Expand Down
5 changes: 2 additions & 3 deletions bindings/wasm/examples/src/merkle_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
VerifierOptions
} from '@iota/identity-wasm';
import {createIdentity} from './create_did';
import {logExplorerUrl} from './utils';

/**
This example shows how to sign/revoke verifiable credentials on scale.
Expand Down Expand Up @@ -50,7 +49,7 @@ async function merkleKey(clientConfig) {
// Publish the Identity to the IOTA Network and log the results.
// This may take a few seconds to complete proof-of-work.
const receipt = await client.publishDocument(issuer.doc);
logExplorerUrl("Identity Update:", clientConfig.explorer, receipt.messageId);
console.log(`Identity Update: ${clientConfig.explorer.messageUrl(receipt.messageId)}`);

// Prepare a credential subject indicating the degree earned by Alice
let credentialSubject = {
Expand Down Expand Up @@ -88,7 +87,7 @@ async function merkleKey(clientConfig) {
issuer.doc.metadataUpdated = Timestamp.nowUTC();
issuer.doc.signSelf(issuer.key, issuer.doc.defaultSigningMethod().id);
const nextReceipt = await client.publishDocument(issuer.doc);
logExplorerUrl("Identity Update:", clientConfig.explorer, nextReceipt.messageId);
console.log(`Identity Update: ${clientConfig.explorer.messageUrl(nextReceipt.messageId)}`);

// Check the verifiable credential is revoked
const newResult = await client.checkCredential(signedVc.toString(), VerifierOptions.default());
Expand Down
3 changes: 1 addition & 2 deletions bindings/wasm/examples/src/private_tangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
KeyType,
Network
} from '@iota/identity-wasm';
import {logResolverUrl} from "./utils";

/**
This example shows how a DID document can be created on a private tangle.
Expand Down Expand Up @@ -59,7 +58,7 @@ async function privateTangle(restURL, networkName) {

console.log(`Published the DID document to the private tangle:`);
console.log(resolved);
logResolverUrl("Explore the DID Document:", explorer, doc.id.toString());
console.log(`Explore the DID Document: ${explorer.resolverUrl(doc.id)}`);

// Return the results.
return {key, resolved, receipt};
Expand Down
15 changes: 7 additions & 8 deletions bindings/wasm/examples/src/resolve_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Timestamp,
VerificationMethod
} from '@iota/identity-wasm';
import {logExplorerUrl, prettyPrintJSON} from "./utils";
import {createIdentity} from "./create_did";

/**
Expand Down Expand Up @@ -75,7 +74,7 @@ async function resolveHistory(clientConfig) {
const intReceipt1 = await client.publishDocument(intDoc1);

// Log the results.
logExplorerUrl("Int. Chain Update (1):", clientConfig.explorer, intReceipt1.messageId);
console.log(`Int. Chain Update (1): ${clientConfig.explorer.messageUrl(intReceipt1.messageId)}`);

// ===========================================================================
// Diff Chain Update 1
Expand All @@ -101,7 +100,7 @@ async function resolveHistory(clientConfig) {

// Publish the diff to the Tangle, starting a diff chain.
const diffReceipt1 = await client.publishDiff(intReceipt1.messageId, diff1);
logExplorerUrl("Diff Chain Transaction (1):", clientConfig.explorer, diffReceipt1.messageId);
console.log(`Diff Chain Transaction (1): ${clientConfig.explorer.messageUrl(diffReceipt1.messageId)}`);

// ===========================================================================
// Diff Chain Update 2
Expand Down Expand Up @@ -129,7 +128,7 @@ async function resolveHistory(clientConfig) {
// Note that we still use the `messageId` from the last integration chain message here to link
// the current diff chain to that point on the integration chain.
const diffReceipt2 = await client.publishDiff(intReceipt1.messageId, diff2);
logExplorerUrl("Diff Chain Transaction (2):", clientConfig.explorer, diffReceipt2.messageId);
console.log(`Diff Chain Transaction (2): ${clientConfig.explorer.messageUrl(diffReceipt2.messageId)}`);

// ===========================================================================
// Diff Chain Spam
Expand All @@ -151,7 +150,7 @@ async function resolveHistory(clientConfig) {
const history1 = await client.resolveHistory(doc.id);

// The history shows two documents in the integration chain, and two diffs in the diff chain.
prettyPrintJSON(history1, "History (1):");
console.log(`History (1): ${JSON.stringify(history1, null, 2)}`);

// ===========================================================================
// Integration Chain Update 2
Expand Down Expand Up @@ -179,7 +178,7 @@ async function resolveHistory(clientConfig) {
const intReceipt2 = await client.publishDocument(intDoc2);

// Log the results.
logExplorerUrl("Int. Chain Update (2):", clientConfig.explorer, intReceipt2.messageId);
console.log(`Int. Chain Update (2): ${clientConfig.explorer.messageUrl(intReceipt2.messageId)}`);

// ===========================================================================
// DID History 2
Expand All @@ -191,7 +190,7 @@ async function resolveHistory(clientConfig) {
// The history now shows three documents in the integration chain, and no diffs in the diff chain.
// This is because each integration chain document has its own diff chain but only the last one
// is used during resolution.
prettyPrintJSON(history2, "History (2):");
console.log(`History (2): ${JSON.stringify(history2, null, 2)}`);

// ===========================================================================
// Diff Chain History
Expand All @@ -201,7 +200,7 @@ async function resolveHistory(clientConfig) {
// Old diff chains can be retrieved but they no longer affect DID resolution.
let previousIntegrationDocument = history2.integrationChainData()[1];
let previousDiffHistory = await client.resolveDiffHistory(previousIntegrationDocument);
prettyPrintJSON(previousDiffHistory, "Previous Diff History:");
console.log(`Previous Diff History: ${JSON.stringify(previousDiffHistory, null, 2)}`);
}

export {resolveHistory};
5 changes: 2 additions & 3 deletions bindings/wasm/examples/src/revoke_vc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import {Client, Config, Timestamp, VerifierOptions} from '@iota/identity-wasm';
import {createVC} from './create_vc';
import {logExplorerUrl, logResolverUrl} from './utils';

/**
This example shows how to revoke a verifiable credential.
Expand Down Expand Up @@ -40,8 +39,8 @@ async function revokeVC(clientConfig) {
const {messageId} = await client.publishDocument(issuer.doc);

// Log the resulting Identity update
logExplorerUrl("Issuer Update Transaction:", clientConfig.explorer, messageId);
logResolverUrl("Explore the Issuer DID Document:", clientConfig.explorer, issuer.doc.id.toString());
console.log(`Issuer Update Transaction: ${clientConfig.explorer.messageUrl(messageId)}`);
console.log(`Explore the Issuer DID Document: ${clientConfig.explorer.resolverUrl(issuer.doc.id)}`);

// Check the verifiable credential
const result = await client.checkCredential(signedVc.toString(), VerifierOptions.default());
Expand Down
40 changes: 0 additions & 40 deletions bindings/wasm/examples/src/utils.js

This file was deleted.

10 changes: 10 additions & 0 deletions bindings/wasm/src/did/wasm_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,13 @@ extern "C" {
#[wasm_bindgen(typescript_type = "DID | string")]
pub type UWasmDID;
}

impl TryFrom<UWasmDID> for IotaDID {
type Error = JsValue;

fn try_from(did: UWasmDID) -> std::result::Result<Self, Self::Error> {
// Parse rather than going through serde directly to return proper error types.
let json: String = did.into_serde().wasm_result()?;
IotaDID::parse(&json).wasm_result()
}
}
12 changes: 6 additions & 6 deletions bindings/wasm/src/tangle/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ impl Client {

/// Fetch the DID document specified by the given `DID`.
#[wasm_bindgen]
pub fn resolve(&self, did: &UWasmDID) -> Result<PromiseResolvedDocument> {
let client: Rc<IotaClient> = self.client.clone();
let did: IotaDID = did.into_serde().wasm_result()?;
pub fn resolve(&self, did: UWasmDID) -> Result<PromiseResolvedDocument> {
let did: IotaDID = IotaDID::try_from(did)?;

let client: Rc<IotaClient> = self.client.clone();
let promise: Promise = future_to_promise(async move {
client
.resolve(&did)
Expand All @@ -192,10 +192,10 @@ impl Client {

/// Returns the message history of the given DID.
#[wasm_bindgen(js_name = resolveHistory)]
pub fn resolve_history(&self, did: &UWasmDID) -> Result<PromiseDocumentHistory> {
let did: IotaDID = did.into_serde().wasm_result()?;
let client: Rc<IotaClient> = self.client.clone();
pub fn resolve_history(&self, did: UWasmDID) -> Result<PromiseDocumentHistory> {
let did: IotaDID = IotaDID::try_from(did)?;

let client: Rc<IotaClient> = self.client.clone();
let promise: Promise = future_to_promise(async move {
client
.resolve_history(&did)
Expand Down
5 changes: 3 additions & 2 deletions bindings/wasm/src/tangle/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use identity::iota::IotaDID;
use identity::iota::MessageId;
use wasm_bindgen::prelude::*;

use crate::did::UWasmDID;
use crate::error::Result;
use crate::error::WasmResult;

Expand Down Expand Up @@ -51,8 +52,8 @@ impl WasmExplorerUrl {
///
/// E.g. https://explorer.iota.org/mainnet/identity-resolver/{did}
#[wasm_bindgen(js_name = resolverUrl)]
pub fn resolver_url(&self, did: &str) -> Result<String> {
let did: IotaDID = IotaDID::parse(did).wasm_result()?;
pub fn resolver_url(&self, did: UWasmDID) -> Result<String> {
let did: IotaDID = IotaDID::try_from(did)?;
self.0.resolver_url(&did).map(|url| url.to_string()).wasm_result()
}

Expand Down

0 comments on commit 6393be7

Please sign in to comment.