Skip to content

Latest commit

 

History

History
 
 

wasm

IOTA Identity WASM

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

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/identity-wasm@dev

Build

Alternatively, you can build the bindings if you have Rust installed. If not, refer to rustup.rs for the installation. Then install the necessary dependencies using:

npm install

and then build the bindings for node.js with

npm run build:nodejs

or for the web with

npm run build:web

NodeJS Usage

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

// Generate a new KeyPair
const key = new identity.KeyPair(identity.KeyType.Ed25519)

// Create a new DID Document with the KeyPair as the default authentication method
const doc = new identity.Document(key)
// const doc = new identity.Document(key, "dev") // if using the devnet

// Sign the DID Document with the private key
doc.sign(key)

// Create a default client instance for the mainnet
const config = identity.Config.fromNetwork(identity.Network.mainnet())
// const config = identity.Config.fromNetwork(identity.Network.devnet()); // if using the devnet
const client = identity.Client.fromConfig(config)

// Publish the DID Document to the IOTA Tangle
// The message can be viewed at https://explorer.iota.org/<mainnet|devnet>/transaction/<messageId>
client.publishDocument(doc.toJSON())
    .then((receipt) => {
        console.log("Tangle Message Receipt: ", receipt)
        console.log("Tangle Message Url:", doc.id.network.messageURL(receipt.messageId))
    })
    .catch((error) => {
        console.error("Error: ", error)
        throw error
    })

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(() => {
  const key = new identity.KeyPair(identity.KeyType.Ed25519)
  const doc = new identity.Document(key)
  // const doc = new identity.Document(key, "dev") // if using the devnet
  console.log("Key Pair", key)
  console.log("DID Document: ", doc)
});

// or

(async () => {
  await identity.init()
  const key = new identity.KeyPair(identity.KeyType.Ed25519)
  const doc = new identity.Document(key)
  // const doc = new identity.Document(key, "dev") // if using the devnet
  console.log("Key Pair", key)
  console.log("DID Document: ", doc)
})()

// 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)