Getting Started with Wasm
In a production setup, do not store passwords in the host's environment variables or in the source code. See our backup and security recommendations for production setups.
Before You Start
The IOTA SDK library also offers dedicated Node.js bindings. The differences with this package are outlined below.
Wasm bindings | Node.js bindings | |
---|---|---|
Environment | Node.js, browsers | Node.js |
Installation | - | Rust, Cargo required* |
Performance | ✔️ | ✔️✔️ |
Ledger Nano | ❌ | ✔️ |
Rocksdb | ❌ | ✔️ |
Stronghold | ❌ | ✔️ |
- The Node.js bindings only needs to be compiled during
npm install
if a pre-compiled binary is not available for your platform.
Use the Node.js bindings if you can. The Wasm bindings are just more portable and support browser environments.
Install Using a Package Manager
To start using the IOTA SDK in your Wasm project, you can install the IOTA SDK from your package manager of choice:
- npm
- Yarn
npm i @iota/sdk-wasm
yarn add @iota/sdk-wasm
Web Setup
Unlike Node.js, a few more steps are required to use this binding in a browser.
The library loads the compiled Wasm file with an HTTP GET request, so the iota_sdk_wasm_bg.wasm
file must be copied to
the root of the distribution folder.
A bundler such as webpack or rollup is recommended.
- Rollup
- Webpack
Install
rollup-plugin-copy
:npm install rollup-plugin-copy --save-dev
Add the plugin to your
rollup.config.js
:// Include the copy plugin.
import copy from 'rollup-plugin-copy';
// ...
// Add the copy plugin to the `plugins` array:
copy({
targets: [
{
src: 'node_modules/@iota/sdk-wasm/web/wasm/iota_sdk_wasm_bg.wasm',
dest: 'public',
rename: 'iota_sdk_wasm_bg.wasm',
},
],
});
Install
copy-webpack-plugin
:npm install copy-webpack-plugin --save-dev
Add the plugin to your
webpack.config.js
:// Include the copy plugin.
const CopyWebPlugin = require('copy-webpack-plugin');
// ...
experiments: {
// futureDefaults: true, // includes asyncWebAssembly, topLevelAwait etc.
asyncWebAssembly: true;
}
// Add the copy plugin to the `plugins` array:
plugins: [
new CopyWebPlugin({
patterns: [
{
from: 'node_modules/@iota/sdk-wasm/web/wasm/iota_sdk_wasm_bg.wasm',
to: 'iota_sdk_wasm_bg.wasm',
},
],
}),
// other plugins...
];
Usage
Client
After you installed the library, you can create a Client
instance and interface with it.
const { Client, initLogger } = require('@iota/sdk');
async function run() {
initLogger();
const client = new Client({
nodes: ['https://api.testnet.shimmer.network'],
localPow: true,
});
try {
const nodeInfo = await client.getInfo();
console.log('Node info: ', nodeInfo);
} catch (error) {
console.error('Error: ', error);
}
}
run().then(() => process.exit());
Wallet
After you installed the library, you can create a Wallet
instance and interact with it.
import { Wallet, CoinType, WalletOptions } from '@iota/sdk';
const walletOptions: WalletOptions = {
storagePath: `Alice`, // A name to associate with the created account.
clientOptions: {
nodes: ['https://api.testnet.shimmer.network'], // The node to connect to.
},
coinType: CoinType.Shimmer,
secretManager: {
// Setup Stronghold secret manager
stronghold: {
snapshotPath: 'vault.stronghold', // The path to store the account snapshot.
password: 'a-secure-password', // A password to encrypt the stored data. WARNING: Never hardcode passwords in production code.
},
},
};
const wallet = new Wallet(walletOptions);
Web Usage
import init, {Wallet, CoinType} from "@iota/sdk-wasm/web";
init().then(() => {
const wallet = new Wallet({
storagePath: './my-database',
coinType: CoinType.Shimmer,
clientOptions: {
nodes: ['https://api.testnet.shimmer.network'],
},
secretManager: {
mnemonic: "my development mnemonic",
},
});
const account = await wallet.createAccount({
alias: 'Alice',
});
account.addresses().then((addresses) => {
console.log(addresses);
});
}).catch(console.error);
// Default path to load is "iota_sdk_wasm_bg.wasm",
// but you can override it by passing a path explicitly.
//
// init("./static/iota_sdk_wasm_bg.wasm").then(...)
What's next?
How-To Guides
Once you have installed the IOTA SDK, you can start building your application. You can find usage examples in this Wiki's how-to guides.
More Examples
You can use the provided Node.js code examples to get acquainted with the IOTA SDK. You can use the following commands to run any example:
cd examples
yarn
yarn run-example ./[example folder]/[example file]
- Where
[example file]
is the file name from the example folder. For example:
yarn run-example examples/client/00_get_info.ts
API Reference
The IOTA SDK Rust API Reference is in the crate documentation.