#!/usr/bin/env bash # Copyright 2026 @quantus/crypto authors & contributors # SPDX-License-Identifier: Apache-2.0 # Builds packages/quantus-crypto. Deliberately NOT part of build-wasm.sh: that # script drives the nightly-2022-06-24 + xargo build that `wasm-crypto` needs, # and this crate needs a modern compiler (inline `const {}` blocks, Rust >= 1.79). # Keeping the two builds separate is what lets wasm-crypto stay byte-identical to # upstream. See quantus/wasm#1. # # There is no asm.js step here. wasm2js over ML-DSA would be enormous and slow, # and every context we ship into sets 'wasm-unsafe-eval', so wasm is always # available where this runs. set -e # Which package to build. Two now — quantus-crypto and quantus-codec — built # the same way from the same toolchain, so the script takes the name rather than # being copied. `quantus_codec` is the crate name for `quantus-codec`: cargo # wants underscores, npm wants hyphens. PKG=${1:-quantus-crypto} CRATE=$(echo "$PKG" | tr '-' '_') BINDGEN_VER=0.2.128 WASM=packages/$PKG/build-wasm/${CRATE}_bg.wasm OPT=packages/$PKG/build-wasm/${CRATE}_opt.wasm echo "*** Building Rust sources" # The toolchain comes from packages/quantus-crypto/rust-toolchain.toml, which # pins the same channel the chain builds its runtime with. (cd packages/$PKG && cargo build --target wasm32-unknown-unknown --release --locked) echo "*** Converting to WASM" ./bindgen-quantus/wasm-bindgen \ packages/$PKG/target/wasm32-unknown-unknown/release/$CRATE.wasm \ --out-dir packages/$PKG/build-wasm \ --target web # The glue is a build artifact but is checked in, so the package can be built # without a Rust toolchain — the same reasoning as upstream checking in an empty # bytes.js. Copy it back so the two never drift. # Must run before the glue is copied into src/generated — see the script's own # comment for why the fetch path cannot ship. node ./scripts/strip-wasm-fetch-init.mjs packages/$PKG/build-wasm/$CRATE.js echo "*** Updating checked-in bindings" cp packages/$PKG/build-wasm/$CRATE.js packages/$PKG/src/generated/$CRATE.js cp packages/$PKG/build-wasm/$CRATE.d.ts packages/$PKG/src/generated/$CRATE.d.ts # binaryen-quantus, not binaryen. Upstream pins version_105 (2021), which predates # the externref tables wasm-bindgen 0.2.128 emits: it "optimises" the table into # something that fails at instantiation with # `WebAssembly.Table.grow(): failed to grow table by 4`. The wasm is valid before # wasm-opt and broken after, and nothing in the build says so — it only surfaces # when a consumer tries to init. Same shape of problem as the two bindgens. echo "*** Optimising WASM output" ./binaryen-quantus/bin/wasm-opt $WASM -Oz -o $OPT # Must come before packing: tsc clears build/, which is where bytes.js lands. ./scripts/build-quantus-js.sh "$PKG" echo "*** Packing WASM into baseX" PKG_NAME=$PKG CRATE_NAME=$CRATE node ./scripts/pack-quantus-base.mjs