Skip to content

Commit

Permalink
Add Wasm Stardust Client (#975)
Browse files Browse the repository at this point in the history
* Split StardustClientExt into StardustIdentityClient/Base traits

* Add network checks to StardustClientExt

* Add context to example errors, increase faucet timeout

* Fix compilation without `iota-client` feature, relax Send restriction

* Re-export bee_block as the block module

* Add IStardustIdentityClient, IStardustIdentityClientExt interfaces

* [WIP] TypeScript client extension attempt

* Remove expect, make error messages more verbose

* Fix AliasOutput serialization, update example

* Move deactivate_did_output to StardustIdentityClient

Add allow_empty to StardustDocument::unpack.

* Update Wasm bindings

* Implement publishDidOutput for Wasm bindings

* Clean Wasm ex0_create_did example

* Implement deleteDidOutput for Wasm bindings

* Add ex1_update_did Wasm example, add examples-stardust directory

* Add ex2_resolve_did example for Wasm

* Add metadata deactivated field Wasm bindings

* Fix input commitment hash calculation for multiple inputs

Sort inputs.
Add some block validation checks.
Fix unlocking logic.
Fix consumeAmount, remainderAmount calculations.

* Add ex4_delete_did Wasm example

* Fix StateMetadataDocument serialised fieldnames

Remove timestamps when unpacking an empty DID Document.

* Fix dust outputs when reclaiming Alias Output amount

* Update iota.js to latest version

* Start iota.rs Node.js client intergration

* Use unofficia iota-client Wasm bindings

* Fix formatting

* Add StardustDocument::unpack_from_block, simplify TypeScript

* Apply suggestions from code review

* Wait for faucet in Wasm Stardust examples

* Change TypeScript `import` to `import type`

* Improve Wasm TypeScript build (#976)

* Remove sleep in Wasm deactivate example

* Revert change to txm README.md example

Co-authored-by: Eike Haß <eike-hass@web.de>
  • Loading branch information
cycraig and eike-hass authored Aug 22, 2022
1 parent 2bdc4e5 commit 0d5ed61
Show file tree
Hide file tree
Showing 49 changed files with 3,981 additions and 4,314 deletions.
3 changes: 1 addition & 2 deletions bindings/wasm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ Cargo.lock
node_modules/

# Build artifacts
wasm-web/
wasm-node/
web/
node/
pkg/
**/dist/

# cypress
cypress/screenshots/
Expand Down
3 changes: 2 additions & 1 deletion bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
async-trait = { version = "0.1", default-features = false }
bee-block = { version = "1.0.0-beta.5", default-features = false, features = ["dto"] }
console_error_panic_hook = { version = "0.1" }
futures = { version = "0.3" }
js-sys = { version = "0.3" }
Expand All @@ -37,7 +38,7 @@ features = ["account", "storage-test-suite", "unstable-encryption", "revocation-
version = "=0.6.0"
path = "../../identity_stardust"
default-features = false
features = ["revocation-bitmap"]
features = ["client", "revocation-bitmap"]

[dev-dependencies]
wasm-bindgen-test = { version = "0.3" }
Expand Down
33 changes: 22 additions & 11 deletions bindings/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

## Install the library:

Latest Release: this version matches the main branch of this repository, is stable and will have changelogs.
Latest Release: this version matches the `main` branch of this repository, is stable and will have changelogs.
```bash
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.
Development Release: this version usually matches the latest code changes from the `dev` branch and may see frequent breaking changes.
```bash
npm install @iota/identity-wasm@dev
```

## Build

Alternatively, you can build the bindings if you have Rust installed. If not, refer to [rustup.rs](https://rustup.rs) for the installation.
Alternatively, you can build the bindings yourself if you have Rust installed. If not, refer to [rustup.rs](https://rustup.rs) for the installation.

Install [`wasm-bindgen-cli`](https://github.com/rustwasm/wasm-bindgen). A manual installation is required because we use the [Weak References](https://rustwasm.github.io/wasm-bindgen/reference/weak-references.html) feature, which [`wasm-pack` does not expose](https://github.com/rustwasm/wasm-pack/issues/930).

Expand All @@ -48,7 +48,7 @@ npm run build:web

## Minimum Requirements

The minimum supported version for node is: `v16.0.0`
The minimum supported version for node is: `v16`

## NodeJS Usage
<!--
Expand All @@ -62,7 +62,7 @@ cat \
-->
<!-- !test check Nodejs Example -->
```javascript
const identity = require('@iota/identity-wasm/node')
const identity = require('@iota/identity-wasm/node');

async function main() {

Expand All @@ -84,7 +84,7 @@ async function main() {
console.log(`Explorer Url:`, identity.ExplorerUrl.mainnet().resolverUrl(did));
}

main()
main();
```

## Web Setup
Expand All @@ -107,7 +107,13 @@ import copy from 'rollup-plugin-copy'

// Add the copy plugin to the `plugins` array of your rollup config:
copy({
targets: [{
targets: [
{
src: 'node_modules/@cycraig/iota-client-wasm/web/wasm/client_wasm_bg.wasm',
dest: 'public',
rename: 'client_wasm_bg.wasm'
},
{
src: 'node_modules/@iota/identity-wasm/web/identity_wasm_bg.wasm',
dest: 'public',
rename: 'identity_wasm_bg.wasm'
Expand All @@ -131,6 +137,10 @@ const CopyWebPlugin= require('copy-webpack-plugin');

new CopyWebPlugin({
patterns: [
{
from: 'node_modules/@cycraig/iota-client-wasm/web/wasm/client_wasm_bg.wasm',
to: 'client_wasm_bg.wasm'
},
{
from: 'node_modules/@iota/identity-wasm/web/identity_wasm_bg.wasm',
to: 'identity_wasm_bg.wasm'
Expand All @@ -142,9 +152,10 @@ new CopyWebPlugin({
### Web Usage

```js
import * as client from "@cycraig/iota-client-wasm/web";
import * as identity from "@iota/identity-wasm/web";

identity.init().then(() => {
client.init().then(() => identity.init()).then(() => {

// The creation step generates a keypair, builds an identity
// and publishes it to the IOTA mainnet.
Expand All @@ -165,8 +176,8 @@ identity.init().then(() => {
// or

(async () => {

await identity.init()
await client.init();
await identity.init();

// The creation step generates a keypair, builds an identity
// and publishes it to the IOTA mainnet.
Expand All @@ -188,7 +199,7 @@ identity.init().then(() => {
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)
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.

## Examples in the Wild

Expand Down
6 changes: 3 additions & 3 deletions bindings/wasm/build/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ fs.writeFileSync(
changedFileNode
);

// Generate `package.json`.
const newPackage = generatePackage({
main: 'identity_wasm.js',
types: 'identity_wasm.d.ts',
main: 'index.js',
types: 'index.d.ts',
});

fs.writeFileSync(path.join(RELEASE_FOLDER, 'package.json'), JSON.stringify(newPackage, null, 2));
6 changes: 3 additions & 3 deletions bindings/wasm/build/utils/generatePackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = (options) => {
description: rootPackage.description,
version: rootPackage.version,
license: rootPackage.license,
homepage: rootPackage.homepage,
repository: rootPackage.repository,
main: options.main,
module: options.module,
homepage: rootPackage.homepage,
main: options.main,
types: options.types,
}

Expand All @@ -19,4 +19,4 @@ module.exports = (options) => {

return newPackage;

}
}
16 changes: 8 additions & 8 deletions bindings/wasm/build/web.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path')
const fs = require('fs')
const { lintAll } = require('./lints')
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const { lintAll } = require('./lints');
const generatePackage = require('./utils/generatePackage');

const RELEASE_FOLDER = path.join(__dirname, '../web/');
Expand Down Expand Up @@ -47,10 +48,9 @@ fs.writeFileSync(
entryFilePathTs,
changedFileTs
);

// Generate `package.json`.
const newPackage = generatePackage({
module: 'identity_wasm.js',
types: 'identity_wasm.d.ts',
module: 'index.js',
types: 'index.d.ts',
});

fs.writeFileSync(path.join(RELEASE_FOLDER, 'package.json'), JSON.stringify(newPackage, null, 2));
fs.writeFileSync(path.join(RELEASE_FOLDER, 'package.json'), JSON.stringify(newPackage, null, 2));
Loading

0 comments on commit 0d5ed61

Please sign in to comment.