1
votes

I am trying to port a wrapper for this crate into wasm. My current toolchain is:

  • wasm-pack
  • webpack
  • wasm-bindgen

A bit more info on the build system:

In my rust crate (which is a --lib crate), I only call wasm-pack build --target browser. This will create a pkg folder containing wasm blobs and associated js files. No complaints from the compiler here.

Then, inside pkg, I run npm link (just once).

Then, cargo generate --git https://github.com/rustwasm/wasm-pack-template creates the required web-app boilerplate. Inside the folder generated by this command, I run npm link schnorrkel-js to make it visible. npm run start runs the webpack dev server.

initial tests like binding simple calculation functions and alert work fine.

Everything breaks when I start using some (not any) functions from the mentioned crate (example). Unfortunately, the error message that I get is quite un-informative and does not help at all:

Entrypoint main = bootstrap.js
[../pkg/schnorrkel_js.js] 3.53 KiB {0} [built]
[../pkg/schnorrkel_js_bg.wasm] 93.7 KiB {0} [built]
    + 27 hidden modules

ERROR in ../pkg/schnorrkel_js_bg.wasm
Module not found: Error: Can't resolve 'env' in '.../schnorrkel-js/pkg'
 @ ../pkg/schnorrkel_js_bg.wasm
 @ ../pkg/schnorrkel_js.js
 @ ./index.js
 @ ./bootstrap.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./bootstrap.js

What is the cause of this? I have a strong guess that some underlying functionality is not portable to wasm but which one exactly, why, and what is the error message trying to say?


notes:

  • I am using the latest rust nightly version (rustc 1.34.0-nightly (d17318011 2019-02-07))

  • rest of the setup steps are taken directly from the Rust and Webassembly book.

  • Cargo.toml

[package]
name = "schnorrkel-js"
edition = "2018"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
schnorrkel = { git = "https://github.com/w3f/schnorrkel.git" }

Example code:

#[wasm_bindgen] 
pub fn verify(signature: &[u8], message: &[u8], pubkey: &[u8]) -> bool {
    let sig = match Signature::from_bytes(signature) {
        Ok(some_sig) => some_sig,
        Err(_) => return false
    };
    let pk = match PublicKey::from_bytes(pubkey) {
        Ok(some_pk) => some_pk,
        Err(_) => return false
    };

    // works up until here if I return a boolean. 
    // calling this breaks everything.
    pk.verify_simple(SIGNING_CTX, message, &sig)
}

1

1 Answers

2
votes

It looks like this could be an issue with the C code used by the clear_on_drop dependency, as mentioned here.

Try enabling the nightly feature on the schnorrkel crate, which in turn enables the nightly feature on clear_on_drop that disables its usage of C. On your Cargo.toml:

[dependencies]
wasm-bindgen = "0.2"
schnorrkel = { git = "https://github.com/w3f/schnorrkel.git", features = ["nightly"] }

Alternatively, you can use wasm2wat (there's an online version) on your compiled .wasm binary to inspect which env import is failing.