briansmith / ring

Safe, fast, small crypto using Rust
Other
3.64k stars 683 forks source link

Enable js feature automatically on WASM platforms #2038

Closed Mubelotix closed 2 months ago

Mubelotix commented 2 months ago

As of today ring support the web platform by allowing users to enable the wasm32_unknown_unknown_js feature. The issue is that it might be difficult for users when ring is used by a dependency (ex: by rustls). The dependency you add to your project might not have included any way of a enabling ring's feature. As of today, you would have to fork rustls to enable the feature on ring.

The good thing is that all this feature does is enabling the js feature on getrandom. This can be done without a feature on ring ! Note that there is no scenario when you wouldn't want getrandom/js enabled when building on wasm32-unknown-unknown, because any build of getrandom without its js feature on wasm32-unknown-unknown will fail anyway.

Mubelotix commented 2 months ago

Note that I didn't remove the feature for backward compatibility, but that should happen eventually. I can add a warning if you had like to

briansmith commented 2 months ago

This is only the right thing to do when wasm32-unknown-unknown is used for a web target like WebAssembly in the web platform or in a browser extension. If wasm32-unknown-unknown is being used in a different environment then the "custom" feature of getrandom should be used instead, I guess.

briansmith commented 2 months ago

As of today, you would have to fork rustls to enable the feature on ring.

In your own project's Cargo.toml, you can add this:

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
ring = { version = "...", features = ["wasm32_unknown_unknown_js"]

or for non-web situations:

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
ring = { version = "...", features = ["less-safe-getrandom-custom-or-rdrand"] }

You can do this even if your project doesn't directly use any ring APIs.

Mubelotix commented 2 months ago

This is only the right thing to do when wasm32-unknown-unknown is used for a web target like WebAssembly in the web platform or in a browser extension. If wasm32-unknown-unknown is being used in a different environment then the "custom" feature of getrandom should be used instead, I guess.

Hm, I didn't know getrandom supported custom implementations.

You can do this even if your project doesn't directly use any ring APIs.

Oh, that works, I thought cargo would refuse to add a feature on a crate used by dependencies. Thank you