rust-bitcoin / rust-wallet

*archived* Library for a Bitcoin Wallet
Apache License 2.0
182 stars 86 forks source link

Can't use library for WASM target compilation #22

Closed thunderbiscuit closed 3 years ago

thunderbiscuit commented 4 years ago

Hi guys,

Sorry if this is not quite well structured of an issue; I'm just getting started with WASM in Rust.

So here is what is happening in my tests:

  1. I managed to use the library very easily in a test binary crate, producing mnemonics and such (so I know I'm using the crate correctly)
  2. I can produce a test WASM package (no bitcoin at this point) that I then use in a React app and it works fine as well (so I also know that I can target WASM and make it work in a web app)
  3. But as soon as I add bitcoin-wallet = "1.0.1" to my dependencies for the WASM library, I get this error when compiling:
wasm-pack build
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
   Compiling rustc-serialize v0.3.24
   Compiling secp256k1 v0.15.5
error: failed to run custom build command for `secp256k1 v0.15.5`

Caused by:
  process didn't exit successfully: `/home/username/sandbox/hello-wasm/lib/target/release/build/secp256k1-19305e1eee13d225/build-script-build` (exit code: 1)
--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("3")
HOST = Some("x86_64-unknown-linux-gnu")
CC_wasm32-unknown-unknown = None
CC_wasm32_unknown_unknown = None
TARGET_CC = None
CC = None
CFLAGS_wasm32-unknown-unknown = None
CFLAGS_wasm32_unknown_unknown = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
CC_wasm32-unknown-unknown = None
CC_wasm32_unknown_unknown = None
TARGET_CC = None
CC = None
CFLAGS_wasm32-unknown-unknown = None
CFLAGS_wasm32_unknown_unknown = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
running: "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-Wall" "-Wextra" "-DSECP256K1_BUILD=1" "-DUSE_NUM_NONE=1" "-DUSE_FIELD_INV_BUILTIN=1" "-DUSE_SCALAR_INV_BUILTIN=1" "-DENABLE_MODULE_ECDH=1" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-DECMULT_WINDOW_SIZE=15" "-DUSE_FIELD_10X26=1" "-DUSE_SCALAR_8X32=1" "-o" "/home/grizzly/sandbox/hello-wasm/lib/target/wasm32-unknown-unknown/release/build/secp256k1-08b329539d811319/out/depend/secp256k1/contrib/lax_der_parsing.o" "-c" "depend/secp256k1/contrib/lax_der_parsing.c"

--- stderr

error occurred: Failed to find tool. Is `clang` installed?

warning: build failed, waiting for other jobs to finish...
error[E0046]: not all trait items implemented, missing: `encode`
    --> /home/username/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-serialize-0.3.24/src/serialize.rs:1358:1
     |
853  |     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error>;
     |     ---------------------------------------------------------------- `encode` from trait
...
1358 | impl Encodable for path::Path {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `encode` in implementation

error[E0046]: not all trait items implemented, missing: `decode`
    --> /home/username/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-serialize-0.3.24/src/serialize.rs:1382:1
     |
904  |     fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error>;
     |     ----------------------------------------------------------- `decode` from trait
...
1382 | impl Decodable for path::PathBuf {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `decode` in implementation

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0046`.
error: build failed
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit code: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"

Any ideas what I might be doing wrong or what is happening here?

thunderbiscuit commented 3 years ago

After speaking with a few folks from the bitcoindevkit, I think the problem is related to how WASM doesn't support most random number generators, breaking a lot of libraries that normally use the OS for this type of thing.

Thank you! I'll close this now.