# c2pa-wasm

The WebAssembly wrapper for [`c2pa-rs`](https://github.com/contentauth/c2pa-rs) that powers `c2pa-web`, built with [wasm-bindgen](https://github.com/wasm-bindgen/wasm-bindgen). While these bindings can be used directly, most users will prefer the convenience of `c2pa-web`.

## Installation

```sh
npm install @contentauth/c2pa-wasm
```

## Development

### Prerequisites

Ensure the repo-wide prerequisites are installed:

- [Node.js](https://nodejs.org/) v22.22+
- [Nx](https://nx.dev/getting-started/intro)
- [pnpm](https://pnpm.io/)

See the [c2pa-js README](../../README.md#prerequisites) for details.

Then, install all of the following prerequisites:

- [Rust](#rust)
- [wasm-bindgen-cli](#wasm-bindgen-cli)
- [wasm-pack](#wasm-pack)

#### Rust

The minimum supported Rust version is **1.96.0**.

To install Rust via [rustup](https://rustup.rs/):

```sh
curl https://sh.rustup.rs -sSf | sh
```

For more information, see [Install Rust](https://www.rust-lang.org/tools/install).

Additionally, you must install the `wasm32-unknown-unknown` target:

```sh
rustup target add wasm32-unknown-unknown
```

#### wasm-bindgen-cli

The `wasm-bindgen-cli` provides options to tweak JavaScript generated by `wasm-bindgen`.

For more information, see the [wasm-bindgen-cli documentation](https://wasm-bindgen.github.io/wasm-bindgen/reference/cli).

```sh
cargo install wasm-bindgen-cli@0.2.129
```

> [!IMPORTANT]
> The version of `wasm-bindgen-cli` **must** match the version of the `wasm-bindgen` dependency in [`Cargo.toml`](https://github.com/contentauth/c2pa-js/blob/main/packages/c2pa-wasm/Cargo.toml). A version mismatch will cause build failures with errors like _"rust Wasm file schema version: X.X.X, this binary schema version: Y.Y.Y"_. If you encounter this, check `Cargo.toml` for the correct version and reinstall the CLI accordingly.

#### wasm-pack

Wasm-pack is a tool for building and working with Rust-generated WebAssembly that interoperates with JavaScript.

```sh
cargo install wasm-pack@0.13.1
```

For more information, see the [wasm-pack documentation](https://rustwasm.github.io/docs/wasm-pack/).

### Building

To build the library:

```sh
nx build c2pa-wasm
```

This will execute the commands defined in the [nx-wasm-bindgen executor](https://github.com/contentauth/c2pa-js-v2/blob/main/tools/nx-wasm-bindgen/src/executors/build/executor.ts) to produce the final output in the `pkg/` directory.

### Testing

The library relies on [`wasm-pack`](https://rustwasm.github.io/docs/wasm-pack/) to run its tests. As a general principle, the majority of test coverage will come from `c2pa-web`'s test suite.

To run the tests:

```sh
nx test c2pa-wasm
```
