Package Exports
- @iota/identity-wasm/node
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
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. 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 = identity.Document.fromKeyPair(key)
// const doc = identity.Document.fromKeyPair(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 underrollup.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 = identity.Document.fromKeyPair(key)
// Or, if using the devnet:
// const doc = identity.Document.fromKeyPair(key, "dev")
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 = identity.Document.fromKeyPair(key)
// Or, if using the devnet:
// const doc = identity.Document.fromKeyPair(key, "dev")
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)