Skip to main content

Getting Started with WASM

IOTA Identity WASM

This is the beta version of the official WASM bindings for IOTA Identity.

API Reference

Account Examples

Low-Level Examples

Install the library:

Latest Release: this version matches the main branch of this repository, is stable and will have changelogs.

npm install @iota/identity-wasm

Development Release: this version matches the dev branch of this repository, may see frequent breaking changes and has the latest code changes.

npm install @iota/[email protected]

Build

Alternatively, you can build the bindings if you have Rust installed. If not, refer to rustup.rs for the installation.

Install wasm-bindgen-cli. A manual installation is required because we use the Weak References feature, which wasm-pack does not expose.

cargo install --force wasm-bindgen-cli

Then, install the necessary dependencies using:

npm install

and build the bindings for node.js with

npm run build:nodejs

or for the web with

npm run build:web

Minimum Requirements

The minimum supported version for node is: v16.0.0

NodeJS Usage

const identity = require('@iota/identity-wasm/node')

async function main() {

// The creation step generates a keypair, builds an identity
// and publishes it to the IOTA mainnet.
const builder = new identity.AccountBuilder();
const account = await builder.createIdentity();

// Retrieve the DID of the newly created identity.
const did = account.did();

// Print the DID of the created Identity.
console.log(did.toString())

// Print the local state of the DID Document
console.log(account.document());

// Print the Explorer URL for the DID.
console.log(`Explorer Url:`, identity.ExplorerUrl.mainnet().resolverUrl(did));
}

main()

Web Setup

The library loads the WASM file with an HTTP GET request, so the .wasm file must be copied to the root of the dist folder.

Rollup

  • Install rollup-plugin-copy:
$ npm install rollup-plugin-copy --save-dev
  • Add the copy plugin usage to the plugins array under rollup.config.js:
// Include the copy plugin
import copy from 'rollup-plugin-copy'

// Add the copy plugin to the `plugins` array of your rollup config:
copy({
targets: [{
src: 'node_modules/@iota/identity-wasm/web/identity_wasm_bg.wasm',
dest: 'public',
rename: 'identity_wasm_bg.wasm'
}]
})

Webpack

  • Install copy-webpack-plugin:
$ npm install copy-webpack-plugin --save-dev
// Include the copy plugin
const CopyWebPlugin= require('copy-webpack-plugin');

// Add the copy plugin to the `plugins` array of your webpack config:

new CopyWebPlugin({
patterns: [
{
from: 'node_modules/@iota/identity-wasm/web/identity_wasm_bg.wasm',
to: 'identity_wasm_bg.wasm'
}
]
}),

Web Usage

import * as identity from "@iota/identity-wasm/web";

identity.init().then(() => {

// The creation step generates a keypair, builds an identity
// and publishes it to the IOTA mainnet.
let builder = new identity.AccountBuilder();
let account = await builder.createIdentity();

// Retrieve the DID of the newly created identity.
const did = account.did();

// Print the DID of the created Identity.
console.log(did.toString())

// Print the local state of the DID Document
console.log(account.document());

});

// or

(async () => {

await identity.init()

// The creation step generates a keypair, builds an identity
// and publishes it to the IOTA mainnet.
let builder = new identity.AccountBuilder();
let account = await builder.createIdentity();

// Retrieve the DID of the newly created identity.
const did = account.did();

// Print the DID of the created Identity.
console.log(did.toString())

// Print the local state of the DID Document
console.log(account.document());

})()

// Default path is "identity_wasm_bg.wasm", but you can override it like this
await identity.init("./static/identity_wasm_bg.wasm");

identity.init().then(<callback>) or await identity.init() is required to load the wasm file (from the server if not available, because of that it will only be slow for the first time)