Make it work on WASM
I think the only issue is deal with rand
cargo check --target wasm32-unknown-unknown
error: the wasm*-unknown-unknown targets are not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
--> /home/casatta/.cargo/registry/src/index.crates.io-6f17d22bba15001f/getrandom-0.2.12/src/lib.rs:291:9
|
291 | / compile_error!("the wasm*-unknown-unknown targets are not supported by \
292 | | default, you may need to enable the \"js\" feature. \
293 | | For more information see: \
294 | | https://docs.rs/getrandom/#webassembly-support");
| |________________________________________________________________________^
error[E0433]: failed to resolve: use of undeclared crate or module `imp`
--> /home/casatta/.cargo/registry/src/index.crates.io-6f17d22bba15001f/getrandom-0.2.12/src/lib.rs:347:9
|
347 | imp::getrandom_inner(dest)?;
| ^^^ use of undeclared crate or module `imp`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
In rust-secp we have the following code in our Cargo.toml
[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"
getrandom = { version = "0.2", features = ["js"] }
I guess we need to copy that here. (Though I'm surprised that us having a rust-secp dependency here is insufficient.)
The rust-secp code seems it's there also in rust-secp-zkp https://github.com/BlockstreamResearch/rust-secp256k1-zkp/blob/master/Cargo.toml (it puzzles me it's a DEV-dependencies but anyway).
Not sure if some deps is taking it without the js feature?
$ cargo tree -i getrandom
getrandom v0.2.12
├── elements v0.24.0 (/home/casatta/git/rust-elements)
└── rand_core v0.6.4
├── rand v0.8.5
│ ├── secp256k1 v0.28.1
│ │ ├── bitcoin v0.31.1
│ │ │ └── elements v0.24.0 (/home/casatta/git/rust-elements)
│ │ └── secp256k1-zkp v0.10.1
│ │ └── elements v0.24.0 (/home/casatta/git/rust-elements)
│ └── secp256k1-zkp v0.10.1 (*)
│ [dev-dependencies]
│ └── elements v0.24.0 (/home/casatta/git/rust-elements)
└── rand_chacha v0.3.1
└── rand v0.8.5 (*)
[dev-dependencies]
└── elements v0.24.0 (/home/casatta/git/rust-elements)
If I add
[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"
getrandom = { version = "0.2", features = ["js"] }
to rust-elements the build succeeds but I am not sure it's the right way to do it
Ohh, I see. It's a dev-dependency because this avoids making it a dependency downstream. We don't want to force wasm users to have rand on. What we want to say is, if the user has rand on, and they are on WASM, then they should have the rand/js feature on ... but Cargo has no way to say this.
So what you're doing is the right thing, AFAICT.