paritytech / libsecp256k1

Pure Rust Implementation of secp256k1.
Apache License 2.0
176 stars 84 forks source link

It can't build to wasm #134

Open aewc opened 2 years ago

aewc commented 2 years ago

When I use this dependency in my wasm project, it will report error when compiling:

error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
   --> /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.7/src/lib.rs:235:9
    |
235 | /         compile_error!("the wasm32-unknown-unknown target is not supported by \
236 | |                         default, you may need to enable the \"js\" feature. \
237 | |                         For more information see: \
238 | |                         https://docs.rs/getrandom/#webassembly-support");
    | |________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.7/src/lib.rs:262:5
    |
262 |     imp::getrandom_inner(dest)
    |     ^^^ use of undeclared crate or module `imp`

error: failed to run custom build command for `gmp-mpfr-sys v1.4.10`

Caused by:
  process didn't exit successfully: `/Users/xxx/workspace/github-aewc/infinity-swap/evm-canister/target/release/build/gmp-mpfr-sys-d8a467da99dcac09/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at 'Cross compilation from aarch64-apple-darwin to wasm32-unknown-unknown not supported! Use the `force-cross` feature to cross compile anyway.', /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/gmp-mpfr-sys-1.4.10/build.rs:111:9
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors

is there any features that without using the getrandom?

AurevoirXavier commented 1 year ago

Same. It can still be reproduced in 2023.

devinpearson commented 1 year ago

You need to add getrandom with the feature flag of JS. This is not libsecp256k1, but its dependency rand which depends on getrandom. https://docs.rs/getrandom/latest/getrandom/#webassembly-support

getrandom = { version = "0.2", features = ["js"]}

AurevoirXavier commented 1 year ago

Fixed by upgrading Substrate to polkadot-v0.9.43.

This issue may also be related to cargo workspace dependencies. I suspect there was a potential bug when handling the individual default-feature = false.