JSPM

  • Created
  • Published
  • Downloads 790
  • Score
    100M100P100Q101645F
  • License Apache-2.0

WASM bindings for IOTA Identity - A Self Sovereign Identity Framework implementing the DID and VC standards from W3C. To be used in Javascript/Typescript

Package Exports

  • @iota/identity-wasm/node
  • @iota/identity-wasm/node/identity_wasm.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@iota/identity-wasm) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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

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() {
    // Choose the Tangle network to publish on.
    const network = identity.Network.mainnet();
    // const network = identity.Network.devnet();

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

    // Create a new DID Document using the KeyPair for the default VerificationMethod.
    const doc = new identity.Document(key, network.name());

    // Sign the DID Document with the private key.
    doc.signSelf(key, doc.defaultSigningMethod().id());

    // Create a default client instance for the network.
    const client = await identity.Client.fromConfig({network: network});
    
    // Publish the DID Document to the IOTA Tangle.
    const receipt = await client.publishDocument(doc);

    // The message can be viewed at https://explorer.iota.org/<mainnet|devnet>/identity-resolver/<did>
    const explorer = identity.ExplorerUrl.mainnet();
    // const explorer = identity.ExplorerUrl.devnet(); // if using the devnet
    console.log("Tangle Message Receipt:", receipt);
    console.log("Tangle Explorer Url:", explorer.resolverUrl(doc.id()));
}

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(() => {
  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)