Skip to content

Commit

Permalink
Merge wasm bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed May 2, 2023
1 parent b47cf99 commit c41571d
Show file tree
Hide file tree
Showing 77 changed files with 948 additions and 5,597 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Wasm publish to NPM (wallet)
name: Wasm publish to NPM

on: workflow_dispatch

Expand All @@ -11,7 +11,7 @@ jobs:

defaults:
run:
working-directory: wallet/bindings/wasm
working-directory: bindings/wasm

steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Wasm bindings checks (wallet)

name: Wasm bindings checks
on:
push:
branches: [develop, production]
Expand All @@ -12,9 +11,9 @@ on:
- "!**/tests/**" # Exclude all tests
- "!cli/**" # Exclude CLI
- "!**/bindings/**" # Exclude all bindings
- "sdk/src/wallet/bindings/wasm/**"
- "sdk/src/wallet/bindings/nodejs/**"
- ".github/workflows/bindings-wallet-wasm.yml"
- "bindings/wasm/**"
- "bindings/nodejs/**"
- ".github/workflows/bindings-wasm.yml"
pull_request:
branches: [develop, production]
paths:
Expand All @@ -26,9 +25,9 @@ on:
- "!**/tests/**" # Exclude all tests
- "!cli/**" # Exclude CLI
- "!**/bindings/**" # Exclude all bindings
- "sdk/src/wallet/bindings/wasm/**"
- "sdk/src/wallet/bindings/nodejs/**"
- ".github/workflows/bindings-wallet-wasm.yml"
- "bindings/wasm/**"
- "bindings/nodejs/**"
- ".github/workflows/bindings-wasm.yml"
schedule:
- cron: "0 1 * * *"
workflow_dispatch:
Expand Down Expand Up @@ -60,7 +59,7 @@ jobs:
uses: ./.github/actions/setup-rust
with:
target: wasm32-unknown-unknown
cache-root: sdk/src/wallet/bindings/wasm
cache-root: bindings/wasm

# Download a pre-compiled wasm-bindgen binary.
- name: Install wasm-bindgen-cli
Expand All @@ -73,14 +72,14 @@ jobs:
with:
node-version: ${{ matrix.node }}
cache: yarn
cache-dependency-path: sdk/src/wallet/bindings/wasm/yarn.lock
cache-dependency-path: bindings/wasm/yarn.lock

- name: Run Yarn Install
working-directory: sdk/src/wallet/bindings/wasm/
working-directory: bindings/wasm/
run: yarn install

- name: Run Yarn Build
working-directory: sdk/src/wallet/bindings/wasm/
working-directory: bindings/wasm/
run: yarn build

- name: Start private tangle
Expand All @@ -89,12 +88,12 @@ jobs:

- name: Run Yarn Test
if: ${{ startsWith(matrix.os, 'ubuntu') }}
working-directory: sdk/src/wallet/bindings/wasm/
working-directory: bindings/wasm/
run: yarn test

- name: Run Yarn Test
if: ${{ !startsWith(matrix.os, 'ubuntu') }}
working-directory: sdk/src/wallet/bindings/wasm/
working-directory: bindings/wasm/
# Only run test that doesn't require a node because we can't use the private tangle script on macos and windows
run: yarn test -t 'Wallet methods'

Expand All @@ -115,15 +114,15 @@ jobs:
uses: actions/setup-node@v2
with:
cache: yarn
cache-dependency-path: sdk/src/wallet/bindings/wasm/yarn.lock
cache-dependency-path: bindings/wasm/yarn.lock
node-version: 18.x

- name: Install Dependencies
working-directory: sdk/src/wallet/bindings/wasm/
working-directory: bindings/wasm/
run: yarn install --ignore-scripts

- name: Lint
working-directory: sdk/src/wallet/bindings/wasm/
working-directory: bindings/wasm/
run: yarn lint

check-format:
Expand All @@ -139,13 +138,13 @@ jobs:
uses: actions/setup-node@v2
with:
cache: yarn
cache-dependency-path: sdk/src/wallet/bindings/wasm/yarn.lock
cache-dependency-path: bindings/wasm/yarn.lock
node-version: 18.x

- name: Install Dependencies
working-directory: sdk/src/wallet/bindings/wasm/
working-directory: bindings/wasm/
run: yarn install --ignore-scripts

- name: Format
working-directory: sdk/src/wallet/bindings/wasm/
working-directory: bindings/wasm/
run: yarn format-check
49 changes: 17 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ members = [
"bindings/core",
"bindings/nodejs",
"bindings/python",
"bindings/wasm",
"cli",
"sdk",
"sdk/src/client/bindings/java/lib/native",
"sdk/src/client/bindings/wasm",
"sdk/src/wallet/bindings/java/lib/native",
"sdk/src/wallet/bindings/nodejs",
"sdk/src/wallet/bindings/swift",
"sdk/src/wallet/bindings/wasm"
"sdk/src/wallet/bindings/swift"
]

[profile.dev]
Expand Down
6 changes: 3 additions & 3 deletions bindings/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use serde::Deserialize;

#[cfg(feature = "mqtt")]
pub use self::method_handler::listen_mqtt;
#[cfg(not(target_family = "wasm"))]
pub use self::method_handler::CallMethod;
pub use self::{
error::{Error, Result},
method::{AccountMethod, ClientMethod, SecretManagerMethod, UtilsMethod, WalletMethod},
method_handler::{
call_client_method, call_secret_manager_method, call_utils_method, call_wallet_method, CallMethod,
},
method_handler::{call_client_method, call_secret_manager_method, call_utils_method, call_wallet_method},
response::Response,
};

Expand Down
6 changes: 6 additions & 0 deletions bindings/core/src/method_handler/call_method.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

#[cfg(not(target_family = "wasm"))]
use std::pin::Pin;

#[cfg(not(target_family = "wasm"))]
use futures::Future;
use iota_sdk::{
client::{secret::SecretManager, Client},
Expand All @@ -20,13 +22,15 @@ use crate::{
UtilsMethod,
};

#[cfg(not(target_family = "wasm"))]
pub trait CallMethod {
type Method;

// This uses a manual async_trait-like impl because it's not worth it to import the lib for one trait
fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + Send + 'a>>;
}

#[cfg(not(target_family = "wasm"))]
impl CallMethod for Client {
type Method = ClientMethod;

Expand All @@ -35,6 +39,7 @@ impl CallMethod for Client {
}
}

#[cfg(not(target_family = "wasm"))]
impl CallMethod for Wallet {
type Method = WalletMethod;

Expand All @@ -43,6 +48,7 @@ impl CallMethod for Wallet {
}
}

#[cfg(not(target_family = "wasm"))]
impl CallMethod for SecretManager {
type Method = SecretManagerMethod;

Expand Down
6 changes: 3 additions & 3 deletions bindings/core/src/method_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mod secret_manager;
mod utils;
mod wallet;

pub use call_method::{
call_client_method, call_secret_manager_method, call_utils_method, call_wallet_method, CallMethod,
};
#[cfg(not(target_family = "wasm"))]
pub use call_method::CallMethod;
pub use call_method::{call_client_method, call_secret_manager_method, call_utils_method, call_wallet_method};
#[cfg(feature = "mqtt")]
pub use client::listen_mqtt;
105 changes: 105 additions & 0 deletions bindings/nodejs/lib/bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import type { EventType } from '../types/wallet';
import type { WalletMethodHandler } from './wallet/WalletMethodHandler';
import { __UtilsMethods__ } from '../types/utils';
import type { SecretManagerMethodHandler } from './secretManager/SecretManagerMethodHandler';
import type { ClientMethodHandler } from './client/ClientMethodHandler';

// @ts-ignore: path is set to match runtime transpiled js path
import addon = require('../../../build/Release/index.node');

const {
callUtilsMethodRust,
callSecretManagerMethod,
createSecretManager,
initLogger,
callClientMethod,
createClient,
listenMqtt,
callWalletMethod,
createWallet,
listenWallet,
destroyWallet,
getClientFromWallet,
} = addon;

const callClientMethodAsync = (
method: string,
handler: ClientMethodHandler,
): Promise<string> =>
new Promise((resolve, reject) => {
callClientMethod(method, handler, (error: Error, result: string) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});

const callSecretManagerMethodAsync = (
method: string,
handler: SecretManagerMethodHandler,
): Promise<string> =>
new Promise((resolve, reject) => {
callSecretManagerMethod(
method,
handler,
(error: Error, result: string) => {
if (error) {
reject(error);
} else {
resolve(result);
}
},
);
});

const callUtilsMethod = (method: __UtilsMethods__): any => {
const response = JSON.parse(callUtilsMethodRust(JSON.stringify(method)));
if (response.type == 'error' || response.type == 'panic') {
throw response;
} else {
return response.payload;
}
};

const listenWalletAsync = (
eventTypes: EventType[],
callback: (error: Error, result: string) => void,
handler: WalletMethodHandler,
): Promise<void> => {
listenWallet(eventTypes, callback, handler);
return Promise.resolve();
};

const callWalletMethodAsync = (
method: string,
handler: WalletMethodHandler,
): Promise<string> =>
new Promise((resolve, reject) => {
callWalletMethod(method, handler, (error: Error, result: string) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});

export {
initLogger,
createClient,
createSecretManager,
createWallet,
callClientMethodAsync,
callSecretManagerMethodAsync,
callUtilsMethod,
callWalletMethodAsync,
destroyWallet,
listenWalletAsync,
getClientFromWallet,
listenMqtt,
};
Loading

0 comments on commit c41571d

Please sign in to comment.