bdk icon indicating copy to clipboard operation
bdk copied to clipboard

Problem at use bdk 0.19 with wasm

Open josediegorobles opened this issue 2 years ago • 30 comments

Describe the bug
Bdk fails on the web. I think must be some thing related to rand crate.

To Reproduce
Create a wallet and call synchronize

Expected behavior
Working as in the console, without problems (and as in 0.18 and previous versions).

Build environment

  • BDK tag/commit: 0.19
  • OS+version: 20.04.01
  • Rust/Cargo version: 1.61.0
  • Rust/Cargo target: wasm32-unknown-unknown

Additional context
That's the error: Uncaught (in promise) RuntimeError: unreachable at __rust_start_panic (67d29ce8a8f7d806078e.module.wasm:0xc6d5d8) at rust_panic (67d29ce8a8f7d806078e.module.wasm:0xc55f4c) at std::panicking::rust_panic_with_hook::he2a025723e105e28 (67d29ce8a8f7d806078e.module.wasm:0x960d29) at std::panicking::begin_panic_handler::{{closure}}::hd9f8c213ec91b9d5 (67d29ce8a8f7d806078e.module.wasm:0xa6ac56) at std::sys_common::backtrace::__rust_end_short_backtrace::h6efd730283875809 (67d29ce8a8f7d806078e.module.wasm:0xc6c185) at rust_begin_unwind (67d29ce8a8f7d806078e.module.wasm:0xc16d18) at core::panicking::panic_fmt::hb02133958c1e7d35 (67d29ce8a8f7d806078e.module.wasm:0xc24531) at rand::rngs::thread::THREAD_RNG_KEY::__init::{{closure}}::ha2ad45ff6630e75f (67d29ce8a8f7d806078e.module.wasm:0xa55aef) at core::result::Result<T,E>::unwrap_or_else::hdc727ae752f97c76 (67d29ce8a8f7d806078e.module.wasm:0x73eae4) at rand::rngs::thread::THREAD_RNG_KEY::__init::hfb75fbbba2c214fb (67d29ce8a8f7d806078e.module.wasm:0x80efe3)

josediegorobles avatar Jun 30 '22 16:06 josediegorobles

If anyone wants to repro, check out this commit: https://github.com/diba-io/bitmask-core/pull/53/commits/76e83c953b0c7ecd5006b1c9d32c01e4cb644c6d

cryptoquick avatar Jun 30 '22 16:06 cryptoquick

Apparentely is an error of rust-bitcoin/secp256k1 https://github.com/rust-bitcoin/rust-secp256k1/issues/470 https://github.com/rust-bitcoin/rust-bitcoin/issues/1086

josediegorobles avatar Jul 04 '22 16:07 josediegorobles

Thanks for getting to the bottom of this with the rust-bitcoin team. Please keep us updated on when bdk can update to a new rust-bitcoin or secp256k1 library to support WASM.

notmandatory avatar Jul 04 '22 18:07 notmandatory

Hi, yeah it will be necessary to update for what I see. Would you think better to keep open this issue?

josediegorobles avatar Jul 05 '22 07:07 josediegorobles

Something of interest was said about that in the call?

josediegorobles avatar Jul 05 '22 14:07 josediegorobles

I think it's good to keep this issue open until what ever changes we need in rust-bitcoin and rust-secp256k1 are released. I just made the rest of the team on the call aware of the research you did on this for anyone else working with bdk and WASM.

notmandatory avatar Jul 05 '22 21:07 notmandatory

rust-seccp256k1 was corrected (https://github.com/rust-bitcoin/rust-secp256k1/issues/470#event-7018099093) and is realeased with new changes, version 0.24.

rust-bitcoin is updated to new rust-secp256k1 version on master branch but it's not released a new version yet, so we must to wait until that.

josediegorobles avatar Jul 21 '22 07:07 josediegorobles

rust-bitcoin new version is going to be released soon: https://github.com/rust-bitcoin/rust-bitcoin/issues/1117#issuecomment-1191189467

josediegorobles avatar Jul 21 '22 09:07 josediegorobles

Let us know once a release candidate is out, I'll update bdk asap

afilini avatar Jul 21 '22 10:07 afilini

They have this milestone: https://github.com/rust-bitcoin/rust-bitcoin/milestone/13 appears that it's going to be soon.

josediegorobles avatar Jul 29 '22 09:07 josediegorobles

rust-bitcoin 0.29 is released yet

josediegorobles avatar Aug 08 '22 20:08 josediegorobles

With rust-bitcoin 0.29 ready, here's a list of PRs / issues currently at play in order to update BDK to rust-bitcoin 0.29:

  • https://github.com/rust-bitcoin/rust-miniscript/issues/452
  • https://github.com/rust-bitcoin/rust-bitcoincore-rpc/pull/234
  • https://github.com/rust-bitcoin/rust-bitcoincore-rpc/pull/222
  • https://github.com/RCasatta/bitcoind/issues/82

cryptoquick avatar Aug 15 '22 15:08 cryptoquick

rust-bicoincore-rpc part is done yet, now is time for https://github.com/RCasatta/bitcoind/issues/82 and https://github.com/rust-bitcoin/rust-miniscript/issues/452, but they need a new release of rust-bitcoin-rpc: https://github.com/rust-bitcoin/rust-bitcoincore-rpc/issues/239

josediegorobles avatar Aug 22 '22 08:08 josediegorobles

What I'm doing for the BDK playground is using a patched version of BDK that doesn't use rand:

# TODO: somehow apply use this only on wasm?
# See https://github.com/rust-lang/cargo/issues/1197
bdk = { git = "https://github.com/bitcoindevkit/bdk.git", branch = "patch_without_rand_for_wasm" }

Since the secp documentation states:

To generate random keys or to re-randomize a context object, compile with the rand-std feature. If you are willing to use the rand-std feature, we have enabled an additional defense-in-depth sidechannel protection for our context objects, which re-blinds certain operations on secret key data.

I suppose this quick fix is not really safe to use in mainnet - the playground is testnet only, so good enough.

Consider this only if your application is still in a testing phase and not being used by anyone in production.

danielabrozzoni avatar Aug 24 '22 14:08 danielabrozzoni

Thanks, I think is probable that we try something like this while we are testing.

josediegorobles avatar Aug 24 '22 16:08 josediegorobles

New release of rust-bitcoincore-rpc https://github.com/rust-bitcoin/rust-bitcoincore-rpc/releases/tag/v0.16.0

josediegorobles avatar Aug 26 '22 07:08 josediegorobles

To help me understand, the dependency PRs that need to be released to fix this issue:

  • https://github.com/rust-bitcoin/rust-secp256k1/issues/474, released in 0.23.4
  • https://github.com/rust-bitcoin/rust-bitcoin/pull/1066, released in 0.29.0
  • https://github.com/rust-bitcoin/rust-bitcoincore-rpc/pull/234, released in 0.16.0
  • https://github.com/RCasatta/bitcoind/pull/84, released in 0.27.0
  • https://github.com/rust-bitcoin/rust-miniscript/pull/450, released 8.0.0
  • https://github.com/bitcoindevkit/rust-electrum-client/pull/81, released in 0.12.0
  • https://github.com/RCasatta/electrsd/pull/42, released in 0.21.0
  • #770 , bdk release 0.24.0

I'll update this comment as PRs get added, merged, and released. Please leave a comment below if I missed anything and I will add.

notmandatory avatar Aug 26 '22 15:08 notmandatory

TBD electrsd, bump bitcoind dependency to 0.27.0

Tried to bump the dep but at the moment I've got https://github.com/RCasatta/electrsd/issues/39

Do we need to update with bitcoin 0.29.0 also rust-electrum-client?

RCasatta avatar Aug 26 '22 17:08 RCasatta

Do we need to update with bitcoin 0.29.0 also rust-electrum-client?

Yeah, it seems like that from cargo tree

danielabrozzoni avatar Aug 26 '22 17:08 danielabrozzoni

I added rust-electrum-client to above TODO list.

notmandatory avatar Aug 26 '22 22:08 notmandatory

Re-opening as https://github.com/bitcoindevkit/rust-electrum-client/commit/3adfbd783ff68e3f668b4bd6e1d7d0654e2bf662 moved towards the fix, but didn't really fix this :)

danielabrozzoni avatar Sep 05 '22 09:09 danielabrozzoni

In rust-miniscript this PR is needed to finnnish: https://github.com/rust-bitcoin/rust-miniscript/pull/450

josediegorobles avatar Sep 12 '22 06:09 josediegorobles

Hi, rust-miniscript master branch is with rust-bitcoin 0.29 yet; but they don't have a release for now, they have this draft PR: https://github.com/rust-bitcoin/rust-miniscript/pull/462

josediegorobles avatar Sep 20 '22 11:09 josediegorobles

I was able to find a workaround for https://github.com/bitcoindevkit/bdk-cli/pull/117, try adding these lines to the Cargo.toml:

[target.'cfg(target_arch = "wasm32")'.dependencies]
rand = { version = "^0.6", features = ["wasm-bindgen"] }

Let me know if it works for you, so that we can close the issue

danielabrozzoni avatar Sep 22 '22 15:09 danielabrozzoni

We're another step closer to being able to upgrade to rust-bitcoin 0.29, the rust-miniscript PR (https://github.com/rust-bitcoin/rust-miniscript/pull/450) was merged 🎉 we just need it to be published in their next release, 8.0.0

notmandatory avatar Sep 25 '22 20:09 notmandatory

I'll start working on the update but I don't think we'll make it in time for this release unfortunately

afilini avatar Sep 26 '22 12:09 afilini

@afilini That's fine now that we have @danielabrozzoni 's workaround. It's much more important for us at DIBA to get #757 in.

cryptoquick avatar Sep 26 '22 17:09 cryptoquick

@danielabrozzoni Found a problem with the rand 0.6 workaround: https://github.com/rustwasm/wasm-bindgen/issues/3098#issuecomment-1270783733 ngl, i'm ded :skull:

cryptoquick avatar Oct 07 '22 02:10 cryptoquick

There's not much we can do about that, BDK itself depends on rand 0.7 so we are not the issue. secp256k1 has been updated to use rand 0.8, but the new secp is only used in rust-bitcoin 0.29, which we can't update to because miniscript hasn't been released yet. I was hoping we could do it in time for our 0.23 release, but it turns out there's still some work left to do there.

I have a PR already open, in draft, if you want to follow the process: #770

afilini avatar Oct 10 '22 09:10 afilini

I've subscribed to the discussion there, thanks!

cryptoquick avatar Oct 10 '22 18:10 cryptoquick