diff --git a/bindings/nodejs/README.md b/bindings/nodejs/README.md index abac727e55..7f4ae693a1 100644 --- a/bindings/nodejs/README.md +++ b/bindings/nodejs/README.md @@ -129,7 +129,7 @@ const walletOptions: WalletOptions = { }, }, }; -const wallet = new Wallet(walletOptions); +const wallet = await Wallet.create(walletOptions); ``` ## Examples diff --git a/bindings/nodejs/examples/exchange/1-create-wallet.ts b/bindings/nodejs/examples/exchange/1-create-wallet.ts index a7266456f9..8f76ef8ed9 100644 --- a/bindings/nodejs/examples/exchange/1-create-wallet.ts +++ b/bindings/nodejs/examples/exchange/1-create-wallet.ts @@ -61,7 +61,7 @@ async function run() { secretManager: strongholdSecretManager, }; - const wallet = new Wallet(walletOptions); + const wallet = await Wallet.create(walletOptions); // Set syncOnlyMostBasicOutputs to true if not interested in outputs that are timelocked, // have a storage deposit return, expiration or are nft/account/foundry outputs. diff --git a/bindings/nodejs/examples/exchange/3-check-balance.ts b/bindings/nodejs/examples/exchange/3-check-balance.ts index ed9346243a..f2df4d1df3 100644 --- a/bindings/nodejs/examples/exchange/3-check-balance.ts +++ b/bindings/nodejs/examples/exchange/3-check-balance.ts @@ -18,7 +18,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); const address = await wallet.address(); diff --git a/bindings/nodejs/examples/exchange/4-listen-events.ts b/bindings/nodejs/examples/exchange/4-listen-events.ts index 88a6a58d7a..82bcc5d03c 100644 --- a/bindings/nodejs/examples/exchange/4-listen-events.ts +++ b/bindings/nodejs/examples/exchange/4-listen-events.ts @@ -18,7 +18,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/exchange/5-send-amount.ts b/bindings/nodejs/examples/exchange/5-send-amount.ts index e387f1587a..bbe9665779 100644 --- a/bindings/nodejs/examples/exchange/5-send-amount.ts +++ b/bindings/nodejs/examples/exchange/5-send-amount.ts @@ -23,7 +23,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/account_output/create.ts b/bindings/nodejs/examples/how_tos/account_output/create.ts index 335b3884ba..d3534017fb 100644 --- a/bindings/nodejs/examples/how_tos/account_output/create.ts +++ b/bindings/nodejs/examples/how_tos/account_output/create.ts @@ -27,7 +27,7 @@ async function run() { try { // Create the wallet - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/account_output/destroy.ts b/bindings/nodejs/examples/how_tos/account_output/destroy.ts index b5f6eb23d9..584e647739 100644 --- a/bindings/nodejs/examples/how_tos/account_output/destroy.ts +++ b/bindings/nodejs/examples/how_tos/account_output/destroy.ts @@ -23,7 +23,7 @@ async function run() { try { // Create the wallet - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/account_wallet/request-funds.ts b/bindings/nodejs/examples/how_tos/account_wallet/request-funds.ts index 3e1ec12583..df04d04591 100644 --- a/bindings/nodejs/examples/how_tos/account_wallet/request-funds.ts +++ b/bindings/nodejs/examples/how_tos/account_wallet/request-funds.ts @@ -23,7 +23,7 @@ async function run() { const faucetUrl = process.env.FAUCET_URL; // Create the wallet - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/account_wallet/transaction.ts b/bindings/nodejs/examples/how_tos/account_wallet/transaction.ts index 8fe6b32a4b..40ac515869 100644 --- a/bindings/nodejs/examples/how_tos/account_wallet/transaction.ts +++ b/bindings/nodejs/examples/how_tos/account_wallet/transaction.ts @@ -29,7 +29,7 @@ async function run() { }, }; - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/check-balance.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/check-balance.ts index af11654fb0..c31e7e0861 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/check-balance.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/check-balance.ts @@ -16,7 +16,7 @@ async function run() { throw new Error('.env WALLET_DB_PATH is undefined, see .env.example'); } try { - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts index e7619c3288..70df3b9842 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts @@ -25,7 +25,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-wallet.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-wallet.ts index e713c26fb3..a873310b18 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-wallet.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-wallet.ts @@ -66,7 +66,7 @@ async function run() { secretManager: strongholdSecretManager, }; - const wallet = new Wallet(walletOptions); + const wallet = await Wallet.create(walletOptions); console.log( 'Generated wallet with address: ' + (await wallet.address()), diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-accounts.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-accounts.ts index 2ba9ac7942..6bc973fb22 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-accounts.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-accounts.ts @@ -16,7 +16,7 @@ async function run() { throw new Error('.env WALLET_DB_PATH is undefined, see .env.example'); } try { - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-outputs.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-outputs.ts index a25192d276..4d7d1a426e 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-outputs.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-outputs.ts @@ -16,7 +16,7 @@ async function run() { throw new Error('.env WALLET_DB_PATH is undefined, see .env.example'); } try { - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-transactions.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-transactions.ts index feb53b0898..b5a813bae0 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-transactions.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-transactions.ts @@ -16,7 +16,7 @@ async function run() { throw new Error('.env WALLET_DB_PATH is undefined, see .env.example'); } try { - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); await wallet.sync({ syncIncomingTransactions: true }); diff --git a/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts b/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts index 199fab2471..7896a11a67 100644 --- a/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts +++ b/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts @@ -24,7 +24,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/advanced_transactions/claim_transaction.ts b/bindings/nodejs/examples/how_tos/advanced_transactions/claim_transaction.ts index 67a1fb7b53..c6b93e6602 100644 --- a/bindings/nodejs/examples/how_tos/advanced_transactions/claim_transaction.ts +++ b/bindings/nodejs/examples/how_tos/advanced_transactions/claim_transaction.ts @@ -17,7 +17,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/advanced_transactions/send_micro_transaction.ts b/bindings/nodejs/examples/how_tos/advanced_transactions/send_micro_transaction.ts index 6cb5e31db1..61a5cb1cdc 100644 --- a/bindings/nodejs/examples/how_tos/advanced_transactions/send_micro_transaction.ts +++ b/bindings/nodejs/examples/how_tos/advanced_transactions/send_micro_transaction.ts @@ -17,7 +17,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/nft_collection/00_mint_issuer_nft.ts b/bindings/nodejs/examples/how_tos/nft_collection/00_mint_issuer_nft.ts index 1106c6ecbd..de18dda1b5 100644 --- a/bindings/nodejs/examples/how_tos/nft_collection/00_mint_issuer_nft.ts +++ b/bindings/nodejs/examples/how_tos/nft_collection/00_mint_issuer_nft.ts @@ -27,7 +27,7 @@ async function run() { } // Create the wallet - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts b/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts index 6f7a6034ea..ab6ed7dc9e 100644 --- a/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts +++ b/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts @@ -25,7 +25,7 @@ async function run() { } // Create the wallet - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/nfts/burn_nft.ts b/bindings/nodejs/examples/how_tos/nfts/burn_nft.ts index d72d9dba05..81d12dc2f2 100644 --- a/bindings/nodejs/examples/how_tos/nfts/burn_nft.ts +++ b/bindings/nodejs/examples/how_tos/nfts/burn_nft.ts @@ -20,7 +20,7 @@ async function run() { } // Create the wallet - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts b/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts index 98378daacf..23f0cb66ab 100644 --- a/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts +++ b/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts @@ -39,7 +39,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/nfts/send_nft.ts b/bindings/nodejs/examples/how_tos/nfts/send_nft.ts index 907efaa23c..fcec68e571 100644 --- a/bindings/nodejs/examples/how_tos/nfts/send_nft.ts +++ b/bindings/nodejs/examples/how_tos/nfts/send_nft.ts @@ -23,7 +23,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/simple_transaction/request-funds.ts b/bindings/nodejs/examples/how_tos/simple_transaction/request-funds.ts index 8236e5d3dc..7a3866dcf0 100644 --- a/bindings/nodejs/examples/how_tos/simple_transaction/request-funds.ts +++ b/bindings/nodejs/examples/how_tos/simple_transaction/request-funds.ts @@ -19,7 +19,7 @@ async function run() { const faucetUrl = process.env.FAUCET_URL; // Create the wallet - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/how_tos/simple_transaction/simple-transaction.ts b/bindings/nodejs/examples/how_tos/simple_transaction/simple-transaction.ts index ec1249840b..ea366d7677 100644 --- a/bindings/nodejs/examples/how_tos/simple_transaction/simple-transaction.ts +++ b/bindings/nodejs/examples/how_tos/simple_transaction/simple-transaction.ts @@ -19,7 +19,7 @@ async function run() { ); } - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: process.env.WALLET_DB_PATH, }); diff --git a/bindings/nodejs/examples/wallet/common.ts b/bindings/nodejs/examples/wallet/common.ts index 4479a2a00f..38cedd495d 100644 --- a/bindings/nodejs/examples/wallet/common.ts +++ b/bindings/nodejs/examples/wallet/common.ts @@ -22,7 +22,7 @@ async function getUnlockedWallet() { const walletOptions: WalletOptions = { storagePath: process.env.WALLET_DB_PATH, }; - const wallet = new Wallet(walletOptions); + const wallet = await Wallet.create(walletOptions); return wallet; } diff --git a/bindings/nodejs/examples/wallet/getting-started.ts b/bindings/nodejs/examples/wallet/getting-started.ts index 541020e59b..4343021912 100644 --- a/bindings/nodejs/examples/wallet/getting-started.ts +++ b/bindings/nodejs/examples/wallet/getting-started.ts @@ -66,7 +66,7 @@ async function main() { secretManager: strongholdSecretManager, }; - const wallet = new Wallet(walletOptions); + const wallet = await Wallet.create(walletOptions); console.log('Generated wallet with address: ' + (await wallet.address())); diff --git a/bindings/nodejs/lib/wallet/wallet-method-handler.ts b/bindings/nodejs/lib/wallet/wallet-method-handler.ts index 1e937b778b..ecaab49c9f 100644 --- a/bindings/nodejs/lib/wallet/wallet-method-handler.ts +++ b/bindings/nodejs/lib/wallet/wallet-method-handler.ts @@ -22,11 +22,19 @@ import { SecretManager } from '../secret_manager'; export class WalletMethodHandler { methodHandler: any; + /** + * @param methodHandler The Rust method handler created in `WalletMethodHandler.create()`. + */ + constructor(methodHandler: any) { + this.methodHandler = methodHandler; + } + /** * @param options The wallet options. */ - constructor(options?: WalletOptions) { - this.methodHandler = createWallet(JSON.stringify(options)); + static async create(options: WalletOptions): Promise { + const methodHandler = await createWallet(JSON.stringify(options)); + return new WalletMethodHandler(methodHandler); } /** diff --git a/bindings/nodejs/lib/wallet/wallet.ts b/bindings/nodejs/lib/wallet/wallet.ts index 545d7e7fa3..29e3fdb0dc 100644 --- a/bindings/nodejs/lib/wallet/wallet.ts +++ b/bindings/nodejs/lib/wallet/wallet.ts @@ -62,10 +62,17 @@ export class Wallet { private methodHandler: WalletMethodHandler; /** - * @param options Wallet options. + * @param methodHandler The Rust method handler created in `WalletMethodHandler.create()`. */ - constructor(options: WalletOptions) { - this.methodHandler = new WalletMethodHandler(options); + constructor(methodHandler: WalletMethodHandler) { + this.methodHandler = methodHandler; + } + + /** + * @param options The wallet options. + */ + static async create(options: WalletOptions): Promise { + return new Wallet(await WalletMethodHandler.create(options)); } /** diff --git a/bindings/nodejs/src/wallet.rs b/bindings/nodejs/src/wallet.rs index 6beb831491..140c9ca306 100644 --- a/bindings/nodejs/src/wallet.rs +++ b/bindings/nodejs/src/wallet.rs @@ -17,10 +17,9 @@ use crate::{client::ClientMethodHandler, secret_manager::SecretManagerMethodHand pub type WalletMethodHandler = Arc>>; #[napi(js_name = "createWallet")] -pub fn create_wallet(options: String) -> Result> { +pub async fn create_wallet(options: String) -> Result> { let wallet_options = serde_json::from_str::(&options).map_err(NodejsError::from)?; - let runtime = tokio::runtime::Runtime::new().map_err(NodejsError::from)?; - let wallet = runtime.block_on(wallet_options.build()).map_err(NodejsError::from)?; + let wallet = wallet_options.build().await.map_err(NodejsError::from)?; Ok(External::new(Arc::new(RwLock::new(Some(wallet))))) } diff --git a/bindings/nodejs/tests/wallet/wallet.spec.ts b/bindings/nodejs/tests/wallet/wallet.spec.ts index 427d374559..5f77b5352b 100644 --- a/bindings/nodejs/tests/wallet/wallet.spec.ts +++ b/bindings/nodejs/tests/wallet/wallet.spec.ts @@ -45,7 +45,7 @@ describe('Wallet', () => { }; - const wallet = new Wallet(walletOptions); + const wallet = await Wallet.create(walletOptions); await wallet.destroy() removeDir(storagePath) @@ -90,7 +90,7 @@ describe('Wallet', () => { }; - const wallet = new Wallet(walletOptions); + const wallet = await Wallet.create(walletOptions); const client = await wallet.getClient(); const hrp = await client.getBech32Hrp(); @@ -98,7 +98,7 @@ describe('Wallet', () => { await wallet.destroy(); - const recreatedWallet = new Wallet({ storagePath: './test-recreate-wallet' }); + const recreatedWallet = await Wallet.create({ storagePath: './test-recreate-wallet' }); await recreatedWallet.destroy() removeDir(storagePath) diff --git a/bindings/wasm/README.md b/bindings/wasm/README.md index 548376a195..97b0ba2269 100644 --- a/bindings/wasm/README.md +++ b/bindings/wasm/README.md @@ -185,7 +185,7 @@ const { Wallet, CoinType } = require('@iota/sdk-wasm/node'); async function run() { try { - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: './my-database', coinType: CoinType.Shimmer, clientOptions: { @@ -214,7 +214,7 @@ run().then(() => process.exit()); import init, {Wallet, CoinType} from "@iota/sdk-wasm/web"; init().then(async () => { - const wallet = new Wallet({ + const wallet = await Wallet.create({ storagePath: './my-database', coinType: CoinType.Shimmer, clientOptions: { diff --git a/bindings/wasm/examples/node.js b/bindings/wasm/examples/node.js index 2c09226993..8ef73904bf 100644 --- a/bindings/wasm/examples/node.js +++ b/bindings/wasm/examples/node.js @@ -29,7 +29,7 @@ async function run() { bech32Hrp: 'tst', }); - const wallet = new Wallet({ + const wallet = await Wallet.create({ address: walletAddress[0], storagePath: './alice-database', bipPath: { diff --git a/bindings/wasm/src/wallet.rs b/bindings/wasm/src/wallet.rs index a9d4750cbc..39dea74fcf 100644 --- a/bindings/wasm/src/wallet.rs +++ b/bindings/wasm/src/wallet.rs @@ -28,13 +28,10 @@ pub struct WalletMethodHandler { /// Creates a method handler with the given options. #[wasm_bindgen(js_name = createWallet)] #[allow(non_snake_case)] -pub fn create_wallet(options: String) -> Result { +pub async fn create_wallet(options: String) -> Result { let wallet_options = serde_json::from_str::(&options).map_err(|e| e.to_string())?; - let wallet_method_handler = tokio::runtime::Builder::new_current_thread() - .build() - .map_err(|err| err.to_string())? - .block_on(async move { wallet_options.build().await.map_err(|e| e.to_string()) })?; + let wallet_method_handler = wallet_options.build().await.map_err(|e| e.to_string())?; Ok(WalletMethodHandler { wallet: Arc::new(Mutex::new(Some(wallet_method_handler))), diff --git a/bindings/wasm/test/wallet.spec.ts b/bindings/wasm/test/wallet.spec.ts index 37aad41730..2a612be685 100644 --- a/bindings/wasm/test/wallet.spec.ts +++ b/bindings/wasm/test/wallet.spec.ts @@ -21,7 +21,7 @@ async function run() { bech32Hrp: 'tst', }); - const wallet = new Wallet({ + const wallet = await Wallet.create({ address: walletAddress[0], bipPath: { coinType: CoinType.IOTA, diff --git a/sdk/src/wallet/core/builder.rs b/sdk/src/wallet/core/builder.rs index c5aaa7d70c..06a24ebb16 100644 --- a/sdk/src/wallet/core/builder.rs +++ b/sdk/src/wallet/core/builder.rs @@ -270,9 +270,6 @@ where // If the wallet builder is not set, it means the user provided it and we need to update the addresses. // In the other case it was loaded from the database and addresses are up to date. if provided_client_options { - // Can't call it in wasm, because it will panic with `condvar wait not supported`. Updating the bech32 hrp - // is still possible in wasm when setting the client options on the wallet. - #[cfg(not(target_family = "wasm"))] wallet.update_bech32_hrp().await?; }