npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@iota/identity-wasm

v1.2.0

Published

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

Downloads

2,808

Readme

IOTA Identity WASM

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

API Reference

Examples

Install the library:

Latest Release: this version matches the main branch of this repository.

npm install @iota/identity-wasm

Build

Alternatively, you can build the bindings yourself 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

NodeJS Usage

The following code creates a new IOTA DID Document suitable for publishing to a locally running private network. See the instructions on running your own private network.

const {
  Jwk,
  JwkType,
  EdCurve,
  MethodScope,
  IotaDocument,
  VerificationMethod,
  Service,
  MethodRelationship,
  IotaIdentityClient,
} = require('@iota/identity-wasm/node');
const { Client } = require('@iota/sdk-wasm/node');

const EXAMPLE_JWK = new Jwk({
  kty: JwkType.Okp,
  crv: EdCurve.Ed25519,
  x: "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo",
});

// The endpoint of the IOTA node to use.
const API_ENDPOINT = "http://localhost";

/** Demonstrate how to create a DID Document. */
async function main() {
  // Create a new client with the given network endpoint.
  const client = new Client({
    primaryNode: API_ENDPOINT,
    localPow: true,
  });

  const didClient = new IotaIdentityClient(client);

  // Get the Bech32 human-readable part (HRP) of the network.
  const networkHrp = await didClient.getNetworkHrp();

  // Create a new DID document with a placeholder DID.
  // The DID will be derived from the Alias Id of the Alias Output after publishing.
  const document = new IotaDocument(networkHrp);

  // Insert a new Ed25519 verification method in the DID document.
  const method = VerificationMethod.newFromJwk(
    document.id(),
    EXAMPLE_JWK,
    "#key-1"
  );
  document.insertMethod(method, MethodScope.VerificationMethod());

  // Attach a new method relationship to the existing method.
  document.attachMethodRelationship(
    document.id().join("#key-1"),
    MethodRelationship.Authentication
  );

  // Add a new Service.
  const service = new Service({
    id: document.id().join("#linked-domain"),
    type: "LinkedDomains",
    serviceEndpoint: "https://iota.org/",
  });
  document.insertService(service);

  console.log(`Created document `, JSON.stringify(document.toJSON(), null, 2));
}

main();

which prints

Created document  {
  "id": "did:iota:tst:0x0000000000000000000000000000000000000000000000000000000000000000",
  "verificationMethod": [
    {
      "id": "did:iota:tst:0x0000000000000000000000000000000000000000000000000000000000000000#key-1",
      "controller": "did:iota:tst:0x0000000000000000000000000000000000000000000000000000000000000000",
      "type": "JsonWebKey",
      "publicKeyJwk": {
        "kty": "OKP",
        "crv": "Ed25519",
        "x": "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"
      }
    }
  ],
  "authentication": [
    "did:iota:tst:0x0000000000000000000000000000000000000000000000000000000000000000#key-1"
  ],
  "service": [
    {
      "id": "did:iota:tst:0x0000000000000000000000000000000000000000000000000000000000000000#linked-domain",
      "type": "LinkedDomains",
      "serviceEndpoint": "https://iota.org/"
    }
  ]
}

NOTE: see the examples for how to publish an IOTA DID Document.

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/sdk-wasm/web/wasm/iota_sdk_wasm_bg.wasm",
      dest: "public",
      rename: "iota_sdk_wasm_bg.wasm",
    },
    {
      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/sdk-wasm/web/wasm/iota_sdk_wasm_bg.wasm',
      to: 'iota_sdk_wasm_bg.wasm'
    },
    {
      from: 'node_modules/@iota/identity-wasm/web/identity_wasm_bg.wasm',
      to: 'identity_wasm_bg.wasm'
    }
  ]
}),

Web Usage

import init, { Client } from "@iota/sdk-wasm/web";
import * as identity from "@iota/identity-wasm/web";

// The endpoint of the IOTA node to use.
const API_ENDPOINT = "http://localhost";

const EXAMPLE_JWK = new identity.Jwk({
  kty: identity.JwkType.Okp,
  crv: identity.EdCurve.Ed25519,
  x: "11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo",
});

/** Demonstrate how to create a DID Document. */
async function createDocument() {
  // Create a new client with the given network endpoint.
  const iotaClient = new Client({
    primaryNode: API_ENDPOINT,
    localPow: true,
  });

  const didClient = new identity.IotaIdentityClient(iotaClient);

  // Get the Bech32 human-readable part (HRP) of the network.
  const networkHrp = await didClient.getNetworkHrp();

  // Create a new DID document with a placeholder DID.
  // The DID will be derived from the Alias Id of the Alias Output after publishing.
  const document = new identity.IotaDocument(networkHrp);

  // Insert a new Ed25519 verification method in the DID document.
  let method = identity.VerificationMethod.newFromJwk(
    document.id(),
    EXAMPLE_JWK,
    "#key-1"
  );
  document.insertMethod(method, identity.MethodScope.VerificationMethod());

  // Attach a new method relationship to the existing method.
  document.attachMethodRelationship(
    document.id().join("#key-1"),
    identity.MethodRelationship.Authentication
  );

  // Add a new Service.
  const service = new identity.Service({
    id: document.id().join("#linked-domain"),
    type: "LinkedDomains",
    serviceEndpoint: "https://iota.org/",
  });
  document.insertService(service);

  console.log(`Created document `, JSON.stringify(document.toJSON(), null, 2));
}

init()
  .then(() => identity.init())
  .then(() => {
    await createDocument();
  });

// or

(async () => {
  await init();
  await identity.init();

  await createDocument();
})();

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

Calling 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.

NOTE: see the examples for how to publish an IOTA DID Document.

Examples in the Wild

You may find it useful to see how the WASM bindings are being used in existing applications: