Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge wasm bindings #352

Merged
merged 3 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions bindings/core/src/method_handler/call_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ 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>>;
fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + 'a>>;
}

impl CallMethod for Client {
type Method = ClientMethod;

fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + Send + 'a>> {
fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + 'a>> {
Box::pin(call_client_method(self, method))
}
}

impl CallMethod for Wallet {
type Method = WalletMethod;

fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + Send + 'a>> {
fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + 'a>> {
Box::pin(call_wallet_method(self, method))
}
}

impl CallMethod for SecretManager {
type Method = SecretManagerMethod;

fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + Send + 'a>> {
fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + 'a>> {
Box::pin(call_secret_manager_method(self, method))
}
}
Expand Down
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,
};
7 changes: 5 additions & 2 deletions bindings/nodejs/lib/client/ClientMethodHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { callMethodAsync, createClient, listenMqtt } from './bindings';
import { callClientMethodAsync, createClient, listenMqtt } from '../bindings';
import type { IClientOptions, __ClientMethods__ } from '../../types/client';

/** The MethodHandler which sends the commands to the Rust side. */
Expand All @@ -18,7 +18,10 @@ export class ClientMethodHandler {
}

async callMethod(method: __ClientMethods__): Promise<string> {
return callMethodAsync(JSON.stringify(method), this.methodHandler);
return callClientMethodAsync(
JSON.stringify(method),
this.methodHandler,
);
}

// MQTT
Expand Down
Loading