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

Optimize wasm for explorer #2225

Draft
wants to merge 4 commits into
base: 2.0
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ codegen-units = 1
inherits = "release"
lto = true
strip = "symbols"
incremental = false

[profile.wasm]
codegen-units = 1
inherits = "release"
lto = true
strip = "symbols"
opt-level = "z"
incremental = false
3 changes: 2 additions & 1 deletion bindings/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ publish = false

[dependencies]
iota-sdk = { path = "../../sdk", default-features = false, features = [
"wallet",
"client",
"protocol_parameters_samples",
"tls",
] }
Expand Down Expand Up @@ -51,3 +51,4 @@ rocksdb = ["iota-sdk/rocksdb"]
storage = ["iota-sdk/storage"]
stronghold = ["iota-sdk/stronghold"]
private_key_secret_manager = ["iota-sdk/private_key_secret_manager"]
wallet = ["iota-sdk/wallet"]
2 changes: 2 additions & 0 deletions bindings/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum Error {
/// Client errors.
#[error("{0}")]
Client(#[from] iota_sdk::client::ClientError),
#[cfg(feature = "wallet")]
/// Wallet errors.
#[error("{0}")]
Wallet(#[from] iota_sdk::wallet::WalletError),
Expand Down Expand Up @@ -80,6 +81,7 @@ impl Serialize for Error {
error: match &self {
// Only Client and wallet have a proper serde impl
Self::Client(e) => serde_json::to_value(e).map_err(serde::ser::Error::custom)?,
#[cfg(feature = "wallet")]
Self::Wallet(e) => serde_json::to_value(e).map_err(serde::ser::Error::custom)?,
_ => serde_json::Value::String(self.to_string()),
},
Expand Down
15 changes: 12 additions & 3 deletions bindings/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ mod response;

use std::fmt::{Formatter, Result as FmtResult};

#[cfg(feature = "wallet")]
use crypto::keys::bip44::Bip44;
#[cfg(feature = "wallet")]
use derivative::Derivative;
use fern_logger::{logger_init, LoggerConfig, LoggerOutputConfigBuilder};
pub use iota_sdk;
use iota_sdk::client::secret::SecretManagerDto;
#[cfg(feature = "wallet")]
use iota_sdk::{
client::secret::{SecretManager, SecretManagerDto},
client::secret::SecretManager,
types::block::address::Bech32Address,
utils::serde::bip44::option_bip44,
wallet::{ClientOptions, Wallet, WalletError},
};
#[cfg(feature = "wallet")]
use serde::Deserialize;

#[cfg(feature = "mqtt")]
Expand All @@ -29,17 +34,20 @@ pub use self::method_handler::listen_mqtt;
pub use self::method_handler::CallMethod;
pub use self::{
error::Error,
method::{ClientMethod, SecretManagerMethod, UtilsMethod, WalletMethod},
method_handler::{call_client_method, call_secret_manager_method, call_utils_method, call_wallet_method},
method::{ClientMethod, SecretManagerMethod, UtilsMethod},
method_handler::{call_client_method, call_secret_manager_method, call_utils_method},
response::Response,
};
#[cfg(feature = "wallet")]
pub use self::{method::WalletMethod, method_handler::call_wallet_method};

pub fn init_logger(config: String) -> std::result::Result<(), fern_logger::Error> {
let output_config: LoggerOutputConfigBuilder = serde_json::from_str(&config).expect("invalid logger config");
let config = LoggerConfig::build().with_output(output_config).finish();
logger_init(config)
}

#[cfg(feature = "wallet")]
#[derive(Derivative, Deserialize, Default)]
#[derivative(Debug)]
#[serde(rename_all = "camelCase")]
Expand All @@ -54,6 +62,7 @@ pub struct WalletOptions {
pub storage_path: Option<String>,
}

#[cfg(feature = "wallet")]
impl WalletOptions {
pub fn with_address(mut self, address: impl Into<Option<Bech32Address>>) -> Self {
self.address = address.into();
Expand Down
5 changes: 4 additions & 1 deletion bindings/core/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
mod client;
mod secret_manager;
mod utils;
#[cfg(feature = "wallet")]
mod wallet;

pub use self::{client::ClientMethod, secret_manager::SecretManagerMethod, utils::UtilsMethod, wallet::WalletMethod};
#[cfg(feature = "wallet")]
pub use self::wallet::WalletMethod;
pub use self::{client::ClientMethod, secret_manager::SecretManagerMethod, utils::UtilsMethod};
13 changes: 8 additions & 5 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ use std::path::PathBuf;

use crypto::keys::bip44::Bip44;
use derivative::Derivative;
use iota_sdk::client::api::options::TransactionOptions;
#[cfg(feature = "events")]
use iota_sdk::wallet::events::types::{WalletEvent, WalletEventType};
// #[cfg(feature = "participation")]
// use iota_sdk::{
// client::node_manager::node::Node,
// types::api::plugins::participation::types::{ParticipationEventId, ParticipationEventType},
// wallet::types::participation::ParticipationEventRegistrationOptions,
// };
#[cfg(feature = "stronghold")]
use iota_sdk::types::block::address::Hrp;
#[cfg(feature = "events")]
use iota_sdk::wallet::events::types::{WalletEvent, WalletEventType};
use iota_sdk::{
client::{
api::{transaction_builder::Burn, PreparedTransactionDataDto, SignedTransactionDataDto},
api::{
options::TransactionOptions, transaction_builder::Burn, PreparedTransactionDataDto,
SignedTransactionDataDto,
},
node_manager::node::NodeAuth,
},
types::block::{
address::Hrp,
output::{AccountId, DelegationId, Output, OutputId, TokenId},
payload::signed_transaction::TransactionId,
},
Expand Down
19 changes: 11 additions & 8 deletions bindings/core/src/method_handler/call_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
use std::pin::Pin;

use futures::Future;
use iota_sdk::{
client::{
secret::{DowncastSecretManager, SecretManage},
Client,
},
wallet::Wallet,
use iota_sdk::client::{
secret::{DowncastSecretManager, SecretManage},
Client,
};
#[cfg(feature = "wallet")]
use iota_sdk::wallet::Wallet;

#[cfg(feature = "wallet")]
use crate::{method::WalletMethod, method_handler::wallet::call_wallet_method_internal};
use crate::{
method::{ClientMethod, SecretManagerMethod, WalletMethod},
method::{ClientMethod, SecretManagerMethod},
method_handler::{
client::call_client_method_internal, secret_manager::call_secret_manager_method_internal,
utils::call_utils_method_internal, wallet::call_wallet_method_internal,
utils::call_utils_method_internal,
},
panic::{convert_async_panics, convert_panics},
response::Response,
Expand All @@ -38,6 +39,7 @@ impl CallMethod for Client {
}
}

#[cfg(feature = "wallet")]
impl CallMethod for Wallet {
type Method = WalletMethod;

Expand All @@ -57,6 +59,7 @@ pub async fn call_client_method(client: &Client, method: ClientMethod) -> Respon
response
}

#[cfg(feature = "wallet")]
/// Call a wallet method.
pub async fn call_wallet_method(wallet: &Wallet, method: WalletMethod) -> Response {
log::debug!("Wallet method: {method:?}");
Expand Down
7 changes: 4 additions & 3 deletions bindings/core/src/method_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ mod call_method;
mod client;
mod secret_manager;
mod utils;
#[cfg(feature = "wallet")]
mod wallet;

pub use call_method::{
call_client_method, call_secret_manager_method, call_utils_method, call_wallet_method, CallMethod,
};
#[cfg(feature = "wallet")]
pub use call_method::call_wallet_method;
pub use call_method::{call_client_method, call_secret_manager_method, call_utils_method, CallMethod};
#[cfg(feature = "mqtt")]
pub use client::listen_mqtt;
18 changes: 14 additions & 4 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use std::collections::HashSet;
use derivative::Derivative;
#[cfg(feature = "ledger_nano")]
use iota_sdk::client::secret::LedgerNanoStatus;
#[cfg(feature = "wallet")]
use iota_sdk::wallet::{
types::{Balance, OutputData, TransactionWithMetadataDto},
PreparedCreateDelegationTransaction, PreparedCreateNativeTokenTransaction,
};
use iota_sdk::{
client::{
api::{PreparedTransactionData, SignedTransactionDataDto},
Expand Down Expand Up @@ -36,10 +41,6 @@ use iota_sdk::{
},
},
utils::serde::string,
wallet::{
types::{Balance, OutputData, TransactionWithMetadataDto},
PreparedCreateDelegationTransaction, PreparedCreateNativeTokenTransaction,
},
};
use serde::Serialize;
#[cfg(feature = "participation")]
Expand Down Expand Up @@ -287,6 +288,7 @@ pub enum Response {
Panic(String),

// wallet responses
#[cfg(feature = "wallet")]
/// Response for:
/// - [`GetAddress`](crate::method::WalletMethod::GetAddress)
Address(Bech32Address),
Expand All @@ -302,9 +304,11 @@ pub enum Response {
/// Response for:
/// - [`ClaimableOutputs`](crate::method::WalletMethod::ClaimableOutputs)
OutputIds(Vec<OutputId>),
#[cfg(feature = "wallet")]
/// Response for:
/// - [`GetOutput`](crate::method::WalletMethod::GetOutput)
OutputData(Option<Box<OutputData>>),
#[cfg(feature = "wallet")]
/// Response for:
/// - [`Outputs`](crate::method::WalletMethod::Outputs),
/// - [`UnspentOutputs`](crate::method::WalletMethod::UnspentOutputs)
Expand All @@ -328,16 +332,20 @@ pub enum Response {
/// - [`PrepareVote`](crate::method::WalletMethod::PrepareVote)
/// - [`PrepareImplicitAccountTransition`](crate::method::WalletMethod::PrepareImplicitAccountTransition)
PreparedTransaction(PreparedTransactionData),
#[cfg(feature = "wallet")]
/// Response for:
/// - [`PrepareCreateNativeToken`](crate::method::WalletMethod::PrepareCreateNativeToken),
PreparedCreateNativeTokenTransaction(PreparedCreateNativeTokenTransaction),
#[cfg(feature = "wallet")]
/// Response for:
/// - [`PrepareCreateDelegation`](crate::method::WalletMethod::PrepareCreateDelegation),
PreparedCreateDelegationTransaction(PreparedCreateDelegationTransaction),
#[cfg(feature = "wallet")]
/// Response for:
/// - [`GetIncomingTransaction`](crate::method::WalletMethod::GetIncomingTransaction)
/// - [`GetTransaction`](crate::method::WalletMethod::GetTransaction),
Transaction(Option<Box<TransactionWithMetadataDto>>),
#[cfg(feature = "wallet")]
/// Response for:
/// - [`IncomingTransactions`](crate::method::WalletMethod::IncomingTransactions)
/// - [`PendingTransactions`](crate::method::WalletMethod::PendingTransactions),
Expand All @@ -346,10 +354,12 @@ pub enum Response {
/// Response for:
/// - [`SignTransaction`](crate::method::WalletMethod::SignTransaction)
SignedTransactionData(SignedTransactionDataDto),
#[cfg(feature = "wallet")]
/// Response for:
/// - [`GetBalance`](crate::method::WalletMethod::GetBalance),
/// - [`Sync`](crate::method::WalletMethod::Sync)
Balance(Balance),
#[cfg(feature = "wallet")]
/// Response for:
/// - [`SignAndSubmitTransaction`](crate::method::WalletMethod::SignAndSubmitTransaction)
/// - [`SubmitAndStoreTransaction`](crate::method::WalletMethod::SubmitAndStoreTransaction)
Expand Down
1 change: 1 addition & 0 deletions bindings/nodejs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ iota-sdk-bindings-core = { path = "../core", default-features = false, features
"rocksdb",
"mqtt",
"private_key_secret_manager",
"wallet",
] }
log = { version = "0.4.21", default-features = false }
napi = { version = "2.16.1", default-features = false, features = ["async"] }
Expand Down
2 changes: 2 additions & 0 deletions bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ iota-sdk-bindings-core = { path = "../core", default-features = false, features
"storage",
"stronghold",
"mqtt",
"private_key_secret_manager",
"wallet",
] }

futures = { version = "0.3.30", default-features = false }
Expand Down
12 changes: 7 additions & 5 deletions bindings/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ crate-type = ["cdylib"]
doc = false

[dependencies]
iota-sdk-bindings-core = { path = "../core", default-features = false, features = [
"events",
"storage",
"private_key_secret_manager",
] }
iota-sdk-bindings-core = { path = "../core", default-features = false }

console_error_panic_hook = { version = "0.1.7", default-features = false }
js-sys = { version = "0.3.69", default-features = false, features = [] }
Expand All @@ -41,3 +37,9 @@ getrandom = { version = "0.2.12", default-features = false, features = ["js"] }
instant = { version = "0.1.12", default-features = false, features = [
"wasm-bindgen",
] }

[features]
default = ["private_key_secret_manager", "wallet"]

private_key_secret_manager = [ "iota-sdk-bindings-core/private_key_secret_manager" ]
wallet = [ "iota-sdk-bindings-core/events", "iota-sdk-bindings-core/storage", "iota-sdk-bindings-core/wallet" ]
3 changes: 3 additions & 0 deletions bindings/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"build:web": "node ./build_scripts/copyNodejsDefs.js && yarn run build:src && yarn run bundle:web && wasm-opt -O web/wasm/iota_sdk_wasm_bg.wasm -o web/wasm/iota_sdk_wasm_bg.wasm",
"bundle:nodejs": "wasm-bindgen ../../target/wasm32-unknown-unknown/production/iota_sdk_wasm.wasm --typescript --weak-refs --target nodejs --out-dir node/wasm && node ./build_scripts/node && tsc --project tsconfig.node.json --outDir node/lib",
"bundle:web": "wasm-bindgen ../../target/wasm32-unknown-unknown/production/iota_sdk_wasm.wasm --typescript --weak-refs --target web --out-dir web/wasm && node ./build_scripts/web && tsc --project tsconfig.web.json --outDir web/lib",
"build:src-client-only": "cargo build --lib --profile=wasm --target wasm32-unknown-unknown --no-default-features",
"build:web-client-only": "node ./build_scripts/copyNodejsDefs.js && yarn run build:src-client-only && yarn run bundle:web-client-only && wasm-opt -Oz web/wasm/iota_sdk_wasm_bg.wasm -o web/wasm/iota_sdk_wasm_bg.wasm",
"bundle:web-client-only": "wasm-bindgen ../../target/wasm32-unknown-unknown/wasm/iota_sdk_wasm.wasm --typescript --weak-refs --target web --out-dir web/wasm && node ./build_scripts/web && tsc --project tsconfig.web.json --outDir web/lib",
"copy-nodejs-defs": "node ./build_scripts/copyNodejsDefs.js",
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts .",
"format": "prettier --ignore-path .eslintignore -w \"{,*/**/}*.{ts,js,json}\"",
Expand Down
1 change: 1 addition & 0 deletions bindings/wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{build_js_error, destroyed_err, map_err, ArrayString};
#[wasm_bindgen(js_name = ClientMethodHandler)]
pub struct ClientMethodHandler(Arc<RwLock<Option<Client>>>);

#[cfg(feature = "wallet")]
impl ClientMethodHandler {
pub(crate) fn new(client: Client) -> Self {
Self(Arc::new(RwLock::new(Some(client))))
Expand Down
1 change: 1 addition & 0 deletions bindings/wasm/src/secret_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{build_js_error, map_err};
#[wasm_bindgen(js_name = SecretManagerMethodHandler)]
pub struct SecretManagerMethodHandler(Arc<RwLock<SecretManager>>);

#[cfg(feature = "wallet")]
impl SecretManagerMethodHandler {
pub(crate) fn new(secret_manager: Arc<RwLock<SecretManager>>) -> Self {
Self(secret_manager)
Expand Down
Loading
Loading