Skip to content

Commit

Permalink
Upgrade client-wasm to sdk-wasm (#1202)
Browse files Browse the repository at this point in the history
* Upgrade client to sdk

* Replace remaining dependencies on iota.js

* Remove `~client-wasm` from tsconfig

* Remove deprecated `importsNotUsedAsValues` ts conf

* Update foundry counter increment

* Update IOTA Identity Client Ext

* Update update did example

* Update examples with deposit changes

* Fix output ID calculation

* Fix remaining issues and TODOs

* Bump sdk-wasm to 1.0.2

* Import client from sdk-wasm

* Update cypress config

* Fix `init` import

* Bump sdk-wasm to `1.0.4`

* Update package-lock.json

* Use `let` over `var`

* use bigint instead of number

* Use default import from sdk

---------

Co-authored-by: Abdulrahim Al Methiab <[email protected]>
  • Loading branch information
PhilippGackstatter and abdulmth authored Aug 2, 2023
1 parent ec1b8ad commit d666834
Show file tree
Hide file tree
Showing 29 changed files with 702 additions and 541 deletions.
19 changes: 9 additions & 10 deletions bindings/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const {
MethodRelationship,
IotaIdentityClient,
} = require('@iota/identity-wasm/node');
const { Client } = require('@iota/client-wasm/node');
const { Client } = require('@iota/sdk-wasm/node');

const EXAMPLE_JWK = new Jwk({
kty: JwkType.Okp,
Expand Down Expand Up @@ -182,9 +182,9 @@ import copy from "rollup-plugin-copy";
copy({
targets: [
{
src: "node_modules/@iota/client-wasm/web/wasm/client_wasm_bg.wasm",
src: "node_modules/@iota/sdk-wasm/web/wasm/iota_sdk_wasm_bg.wasm",
dest: "public",
rename: "client_wasm_bg.wasm",
rename: "iota_sdk_wasm_bg.wasm",
},
{
src: "node_modules/@iota/identity-wasm/web/identity_wasm_bg.wasm",
Expand Down Expand Up @@ -212,8 +212,8 @@ const CopyWebPlugin= require('copy-webpack-plugin');
new CopyWebPlugin({
patterns: [
{
from: 'node_modules/@iota/client-wasm/web/wasm/client_wasm_bg.wasm',
to: 'client_wasm_bg.wasm'
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',
Expand All @@ -226,7 +226,7 @@ new CopyWebPlugin({
### Web Usage

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

// The endpoint of the IOTA node to use.
Expand All @@ -241,7 +241,7 @@ const EXAMPLE_JWK = new identity.Jwk({
/** Demonstrate how to create a DID Document. */
async function createDocument() {
// Create a new client with the given network endpoint.
const iotaClient = new client.Client({
const iotaClient = new Client({
primaryNode: API_ENDPOINT,
localPow: true,
});
Expand Down Expand Up @@ -280,8 +280,7 @@ async function createDocument() {
console.log(`Created document `, JSON.stringify(document.toJSON(), null, 2));
}

client
.init()
init()
.then(() => identity.init())
.then(() => {
await createDocument();
Expand All @@ -290,7 +289,7 @@ client
// or

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

await createDocument();
Expand Down
5 changes: 2 additions & 3 deletions bindings/wasm/cypress/support/setup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as client from "@iota/client-wasm/web";
import init from "@iota/sdk-wasm/web";
import * as identity from "../../web";

export const setup = async (func) => {
await client
.init("../../../node_modules/@iota/client-wasm/web/wasm/client_wasm_bg.wasm")
await init("../../../node_modules/@iota/sdk-wasm/web/wasm/iota_sdk_wasm_bg.wasm")
.then(async () => await identity.init("../../../web/identity_wasm_bg.wasm"))
.then(func);
};
22 changes: 12 additions & 10 deletions bindings/wasm/examples/src/0_basic/0_create_did.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, MnemonicSecretManager, SecretManager } from "@iota/client-wasm/node";
import { Bip39 } from "@iota/crypto.js";
import {
IotaDID,
IotaDocument,
Expand All @@ -13,7 +11,7 @@ import {
MethodScope,
Storage,
} from "@iota/identity-wasm/node";
import { Bech32Helper, IAliasOutput } from "@iota/iota.js";
import { AliasOutput, Client, MnemonicSecretManager, SecretManager, Utils } from "@iota/sdk-wasm/node";
import { API_ENDPOINT, ensureAddressHasFunds } from "../util";

/** Demonstrate how to create a DID Document and publish it in a new Alias Output. */
Expand All @@ -32,16 +30,20 @@ export async function createIdentity(): Promise<{
// Get the Bech32 human-readable part (HRP) of the network.
const networkHrp: string = await didClient.getNetworkHrp();

// Generate a random mnemonic for our wallet.
const secretManager: MnemonicSecretManager = {
mnemonic: Bip39.randomMnemonic(),
const mnemonicSecretManager: MnemonicSecretManager = {
mnemonic: Utils.generateMnemonic(),
};
const walletAddressBech32 = (await client.generateAddresses(secretManager, {

// Generate a random mnemonic for our wallet.
const secretManager: SecretManager = new SecretManager(mnemonicSecretManager);

const walletAddressBech32 = (await secretManager.generateEd25519Addresses({
accountIndex: 0,
range: {
start: 0,
end: 1,
},
bech32Hrp: networkHrp,
}))[0];
console.log("Wallet address Bech32:", walletAddressBech32);

Expand All @@ -64,12 +66,12 @@ export async function createIdentity(): Promise<{

// Construct an Alias Output containing the DID document, with the wallet address
// set as both the state controller and governor.
const address = Bech32Helper.addressFromBech32(walletAddressBech32, networkHrp);
const aliasOutput: IAliasOutput = await didClient.newDidOutput(address, document);
const address = Utils.parseBech32Address(walletAddressBech32);
const aliasOutput: AliasOutput = await didClient.newDidOutput(address, document);
console.log("Alias Output:", JSON.stringify(aliasOutput, null, 2));

// Publish the Alias Output and get the published DID document.
const published = await didClient.publishDidOutput(secretManager, aliasOutput);
const published = await didClient.publishDidOutput(mnemonicSecretManager, aliasOutput);
console.log("Published DID document:", JSON.stringify(published, null, 2));

return {
Expand Down
16 changes: 10 additions & 6 deletions bindings/wasm/examples/src/0_basic/1_update_did.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, MnemonicSecretManager } from "@iota/client-wasm/node";
import { Bip39 } from "@iota/crypto.js";
import {
IotaDocument,
IotaIdentityClient,
Expand All @@ -16,7 +14,7 @@ import {
Timestamp,
VerificationMethod,
} from "@iota/identity-wasm/node";
import { IAliasOutput, IRent, TransactionHelper } from "@iota/iota.js";
import { AliasOutput, Client, IRent, MnemonicSecretManager, Utils } from "@iota/sdk-wasm/node";
import { API_ENDPOINT, createDid } from "../util";

/** Demonstrates how to update a DID document in an existing Alias Output. */
Expand All @@ -29,7 +27,7 @@ export async function updateIdentity() {

// Generate a random mnemonic for our wallet.
const secretManager: MnemonicSecretManager = {
mnemonic: Bip39.randomMnemonic(),
mnemonic: Utils.generateMnemonic(),
};

// Creates a new wallet and identity (see "0_create_did" example).
Expand Down Expand Up @@ -71,12 +69,18 @@ export async function updateIdentity() {
await document.purgeMethod(storage, originalMethod?.id());

// Resolve the latest output and update it with the given document.
const aliasOutput: IAliasOutput = await didClient.updateDidOutput(document);
let aliasOutput: AliasOutput = await didClient.updateDidOutput(document);

// Because the size of the DID document increased, we have to increase the allocated storage deposit.
// This increases the deposit amount to the new minimum.
const rentStructure: IRent = await didClient.getRentStructure();
aliasOutput.amount = TransactionHelper.getStorageDeposit(aliasOutput, rentStructure).toString();

aliasOutput = await client.buildAliasOutput({
...aliasOutput,
amount: Utils.computeStorageDeposit(aliasOutput, rentStructure),
aliasId: aliasOutput.getAliasId(),
unlockConditions: aliasOutput.getUnlockConditions(),
});

// Publish the output.
const updated: IotaDocument = await didClient.publishDidOutput(secretManager, aliasOutput);
Expand Down
10 changes: 4 additions & 6 deletions bindings/wasm/examples/src/0_basic/2_resolve_did.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, MnemonicSecretManager } from "@iota/client-wasm/node";
import { Bip39 } from "@iota/crypto.js";
import { IotaDocument, IotaIdentityClient, JwkMemStore, KeyIdMemStore, Storage } from "@iota/identity-wasm/node";
import type { IAliasOutput } from "@iota/iota.js";
import { AliasOutput, Client, MnemonicSecretManager, Utils } from "@iota/sdk-wasm/node";
import { API_ENDPOINT, createDid } from "../util";

/** Demonstrates how to resolve an existing DID in an Alias Output. */
Expand All @@ -17,7 +15,7 @@ export async function resolveIdentity() {

// Generate a random mnemonic for our wallet.
const secretManager: MnemonicSecretManager = {
mnemonic: Bip39.randomMnemonic(),
mnemonic: Utils.generateMnemonic(),
};

// Creates a new wallet and identity (see "0_create_did" example).
Expand All @@ -34,6 +32,6 @@ export async function resolveIdentity() {
console.log("Resolved DID document:", JSON.stringify(resolved, null, 2));

// We can also resolve the Alias Output directly.
const aliasOutput: IAliasOutput = await didClient.resolveDidOutput(did);
console.log("The Alias Output holds " + aliasOutput.amount + " tokens");
const aliasOutput: AliasOutput = await didClient.resolveDidOutput(did);
console.log("The Alias Output holds " + aliasOutput.getAmount() + " tokens");
}
26 changes: 18 additions & 8 deletions bindings/wasm/examples/src/0_basic/3_deactivate_did.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, MnemonicSecretManager } from "@iota/client-wasm/node";
import { Bip39 } from "@iota/crypto.js";
import { IotaDocument, IotaIdentityClient, JwkMemStore, KeyIdMemStore, Storage } from "@iota/identity-wasm/node";
import { IAliasOutput, IRent, TransactionHelper } from "@iota/iota.js";
import { AliasOutput, Client, IRent, MnemonicSecretManager, Utils } from "@iota/sdk-wasm/node";
import { API_ENDPOINT, createDid } from "../util";

/** Demonstrates how to deactivate a DID in an Alias Output. */
Expand All @@ -17,7 +15,7 @@ export async function deactivateIdentity() {

// Generate a random mnemonic for our wallet.
const secretManager: MnemonicSecretManager = {
mnemonic: Bip39.randomMnemonic(),
mnemonic: Utils.generateMnemonic(),
};

// Creates a new wallet and identity (see "0_create_did" example).
Expand All @@ -36,11 +34,17 @@ export async function deactivateIdentity() {
// Deactivate the DID by publishing an empty document.
// This process can be reversed since the Alias Output is not destroyed.
// Deactivation may only be performed by the state controller of the Alias Output.
let deactivatedOutput: IAliasOutput = await didClient.deactivateDidOutput(did);
let deactivatedOutput: AliasOutput = await didClient.deactivateDidOutput(did);

// Optional: reduce and reclaim the storage deposit, sending the tokens to the state controller.
const rentStructure: IRent = await didClient.getRentStructure();
deactivatedOutput.amount = TransactionHelper.getStorageDeposit(deactivatedOutput, rentStructure).toString();

deactivatedOutput = await client.buildAliasOutput({
...deactivatedOutput,
amount: Utils.computeStorageDeposit(deactivatedOutput, rentStructure),
aliasId: deactivatedOutput.getAliasId(),
unlockConditions: deactivatedOutput.getUnlockConditions(),
});

// Publish the deactivated DID document.
await didClient.publishDidOutput(secretManager, deactivatedOutput);
Expand All @@ -54,10 +58,16 @@ export async function deactivateIdentity() {
}

// Re-activate the DID by publishing a valid DID document.
let reactivatedOutput: IAliasOutput = await didClient.updateDidOutput(document);
let reactivatedOutput: AliasOutput = await didClient.updateDidOutput(document);

// Increase the storage deposit to the minimum again, if it was reclaimed during deactivation.
reactivatedOutput.amount = TransactionHelper.getStorageDeposit(reactivatedOutput, rentStructure).toString();
reactivatedOutput = await client.buildAliasOutput({
...reactivatedOutput,
amount: Utils.computeStorageDeposit(reactivatedOutput, rentStructure),
aliasId: reactivatedOutput.getAliasId(),
unlockConditions: reactivatedOutput.getUnlockConditions(),
});

await didClient.publishDidOutput(secretManager, reactivatedOutput);

// Resolve the reactivated DID document.
Expand Down
5 changes: 2 additions & 3 deletions bindings/wasm/examples/src/0_basic/4_delete_did.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, MnemonicSecretManager } from "@iota/client-wasm/node";
import { Bip39 } from "@iota/crypto.js";
import { IotaIdentityClient, JwkMemStore, KeyIdMemStore, Storage } from "@iota/identity-wasm/node";
import { Client, MnemonicSecretManager, Utils } from "@iota/sdk-wasm/node";
import { API_ENDPOINT, createDid } from "../util";

/** Demonstrates how to delete a DID in an Alias Output, reclaiming the storage deposit. */
Expand All @@ -16,7 +15,7 @@ export async function deleteIdentity() {

// Generate a random mnemonic for our wallet.
const secretManager: MnemonicSecretManager = {
mnemonic: Bip39.randomMnemonic(),
mnemonic: Utils.generateMnemonic(),
};

// Creates a new wallet and identity (see "0_create_did" example).
Expand Down
5 changes: 2 additions & 3 deletions bindings/wasm/examples/src/0_basic/5_create_vc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, MnemonicSecretManager } from "@iota/client-wasm/node";
import { Bip39 } from "@iota/crypto.js";
import {
Credential,
FailFast,
Expand All @@ -13,6 +11,7 @@ import {
KeyIdMemStore,
Storage,
} from "@iota/identity-wasm/node";
import { Client, MnemonicSecretManager, Utils } from "@iota/sdk-wasm/node";
import { API_ENDPOINT, createDid } from "../util";

/**
Expand All @@ -29,7 +28,7 @@ export async function createVC() {

// Generate a random mnemonic for our wallet.
const secretManager: MnemonicSecretManager = {
mnemonic: Bip39.randomMnemonic(),
mnemonic: Utils.generateMnemonic(),
};

// Create an identity for the issuer with one verification method `key-1`.
Expand Down
7 changes: 3 additions & 4 deletions bindings/wasm/examples/src/0_basic/6_create_vp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Client, MnemonicSecretManager } from "@iota/client-wasm/node";
import { Bip39 } from "@iota/crypto.js";
import {
CoreDID,
Credential,
Expand All @@ -25,6 +23,7 @@ import {
SubjectHolderRelationship,
Timestamp,
} from "@iota/identity-wasm/node";
import { Client, MnemonicSecretManager, Utils } from "@iota/sdk-wasm/node";
import { API_ENDPOINT, createDid } from "../util";

/**
Expand All @@ -45,7 +44,7 @@ export async function createVP() {

// Creates a new wallet and identity (see "0_create_did" example).
const issuerSecretManager: MnemonicSecretManager = {
mnemonic: Bip39.randomMnemonic(),
mnemonic: Utils.generateMnemonic(),
};
const issuerStorage: Storage = new Storage(
new JwkMemStore(),
Expand All @@ -59,7 +58,7 @@ export async function createVP() {

// Create an identity for the holder, in this case also the subject.
const aliceSecretManager: MnemonicSecretManager = {
mnemonic: Bip39.randomMnemonic(),
mnemonic: Utils.generateMnemonic(),
};
const aliceStorage: Storage = new Storage(
new JwkMemStore(),
Expand Down
Loading

0 comments on commit d666834

Please sign in to comment.