@iota/wallet-wasm
v1.0.0-alpha.1
Published
WebAssembly bindings for the IOTA wallet library
Downloads
5
Readme
IOTA Wallet Library - WebAssembly bindings
WebAssembly (Wasm) bindings for TypeScript/JavaScript to the wallet.rs
Wallet library.
Which bindings to choose?
The wallet.rs
Wallet 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 | ❌ | ✔️ |
*Node.js bindings only need to be compiled during npm install
if a pre-compiled binary is not available for your platform.
tl;dr: Use the Node.js bindings if you can. The Wasm bindings are just more portable and support browser environments.
Requirements
- One of the following Node.js versions: '16.x', '18.x';
wasm-bindgen
(cargo install wasm-bindgen-cli
);
Installation
- Using npm:
$ npm i @iota/wallet-wasm
- Using yarn:
$ yarn add @iota/wallet-wasm
Getting Started
After installing the library, you can create a Client
instance and interface with it.
Node.js Usage
const { AccountManager, CoinType } = require('@iota/wallet-wasm/node');
const manager = new AccountManager({
storagePath: './my-database',
coinType: CoinType.Shimmer,
clientOptions: {
nodes: ['https://api.testnet.shimmer.network'],
},
secretManager: {
mnemonic: "my development mnemonic",
},
});
const account = await manager.createAccount({
alias: 'Alice',
});
account.getNodeInfo().then((nodeInfo) => {
console.log(nodeInfo);
});
See the Node.js examples for more demonstrations, the only change needed is to import @iota/wallet-wasm/node
instead of @iota/wallet
.
Web Setup
Unlike Node.js, a few more steps are required to use this in the browser.
The library loads the compiled Wasm file with an HTTP GET request, so the wallet_wasm_bg.wasm
file must be copied to the root of the distribution folder.
A bundler such as webpack or rollup is recommended.
Rollup
- 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/wallet-wasm/web/wasm/wallet_wasm_bg.wasm',
dest: 'public',
rename: 'wallet_wasm_bg.wasm'
}]
})
Webpack
- 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/wallet-wasm/web/wasm/wallet_wasm_bg.wasm',
to: 'wallet_wasm_bg.wasm'
}
]
}),
// other plugins...
]
Web Usage
import init, { AccountManager, CoinType } from "@iota/wallet-wasm/web";
init().then(() => {
const manager = new AccountManager({
storagePath: './my-database',
coinType: CoinType.Shimmer,
clientOptions: {
nodes: ['https://api.testnet.shimmer.network'],
},
secretManager: {
mnemonic: "my development mnemonic",
},
});
const account = await manager.createAccount({
alias: 'Alice',
});
account.getNodeInfo().then((nodeInfo) => {
console.log(nodeInfo);
});
}).catch(console.error);
// Default path to load is "wallet_wasm_bg.wasm",
// but you can override it by passing a path explicitly.
//
// init("./static/wallet_wasm_bg.wasm").then(...)