From: Alekos Filini Date: Mon, 20 Jul 2020 13:51:57 +0000 (+0200) Subject: Make the blockchain interface async again on wasm32-unknown-unknown X-Git-Tag: 0.1.0-beta.1~22 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/struct.SegwitCodeLengthError.html?a=commitdiff_plain;h=ceafbca6d36862a8c7acf840ff25dc24c5f0bbc8;p=bdk-cli Make the blockchain interface async again on wasm32-unknown-unknown The procedural macro `#[maybe_async]` makes a method or every method of a trait "async" whenever the target_arch is `wasm32`, and leaves them untouched on every other platform. The macro `maybe_await!($e:expr)` can be used to call `maybe_async` methods on multi-platform code: it expands to `$e` on non-wasm32 platforms and to `$e.await` on wasm32. The macro `await_or_block!($e:expr)` can be used to contain async code as much as possible: it expands to `tokio::runtime::Runtime::new().unwrap().block_on($e)` on non-wasm32 platforms, and to `$e.await` on wasm32. --- diff --git a/Cargo.toml b/Cargo.toml index 65be202..c2b6f29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2018" authors = ["Riccardo Casatta ", "Alekos Filini "] [dependencies] +magical-macros = { path = "./macros" } log = "^0.4" bitcoin = { version = "0.23", features = ["use-serde"] } miniscript = { version = "1.0" } @@ -15,17 +16,23 @@ serde_json = { version = "^1.0" } sled = { version = "0.31.0", optional = true } electrum-client = { version = "0.2.0-beta.1", optional = true } reqwest = { version = "0.10", optional = true, features = ["json"] } -tokio = { version = "0.2", optional = true, features = ["rt-core"] } futures = { version = "0.3", optional = true } clap = { version = "2.33", optional = true } base64 = { version = "^0.11", optional = true } +# Platform-specific dependencies +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +tokio = { version = "0.2", features = ["rt-core"] } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +async-trait = "0.1" + [features] minimal = [] compiler = ["miniscript/compiler"] default = ["key-value-db", "electrum"] electrum = ["electrum-client"] -esplora = ["reqwest", "futures", "tokio"] +esplora = ["reqwest", "futures"] key-value-db = ["sled"] cli-utils = ["clap", "base64"] diff --git a/src/cli.rs b/src/cli.rs index 90c0704..8ea39ed 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -276,6 +276,7 @@ pub fn add_global_flags<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { .subcommand(SubCommand::with_name("repl").about("Opens an interactive shell")) } +#[maybe_async] pub fn handle_matches( wallet: &Wallet, matches: ArgMatches<'_>, @@ -287,7 +288,7 @@ where if let Some(_sub_matches) = matches.subcommand_matches("get_new_address") { Ok(Some(format!("{}", wallet.get_new_address()?))) } else if let Some(_sub_matches) = matches.subcommand_matches("sync") { - wallet.sync(None, None)?; + maybe_await!(wallet.sync(None, None))?; Ok(None) } else if let Some(_sub_matches) = matches.subcommand_matches("list_unspent") { let mut res = String::new(); @@ -382,7 +383,7 @@ where panic!("Missing `psbt` and `tx` option"); }; - let txid = wallet.broadcast(tx)?; + let txid = maybe_await!(wallet.broadcast(tx))?; Ok(Some(format!("TXID: {}", txid))) } else if let Some(sub_matches) = matches.subcommand_matches("extract_psbt") {