# SDN-JS WASM Modules

This directory contains WebAssembly modules used by the SDN JavaScript library.

## Required WASM Files

The following WASM files should be placed in this directory:

### flatc-crypto.wasm

FlatBuffer encryption and signing module providing:
- AES-GCM encryption/decryption
- Ed25519 signing/verification
- Memory management functions

Build from the flatbuffers repository:
```bash
cd ../flatbuffers
emcc src/crypto.cpp -o ../sdn-js/wasm/flatc-crypto.wasm \
    -s EXPORTED_FUNCTIONS="['_encrypt_bytes', '_decrypt_bytes', '_ed25519_sign', '_ed25519_verify', '_malloc', '_free']" \
    -s EXPORTED_RUNTIME_METHODS="['HEAPU8']" \
    -O3
```

### edge-relays.wasm

Encrypted edge relay registry module (generated by build-edge-registry.ts):
```bash
npx ts-node scripts/build-edge-registry.ts /path/to/relays.json
```

### flatsql.wasm

FlatBuffer SQL virtual table module for browser-based SQLite:
```bash
cd ../flatbuffers-sqlite
emcc src/flatsql.cpp -o ../sdn-js/wasm/flatsql.wasm \
    -s EXPORTED_FUNCTIONS="['_create_virtual_table', '_query_flatbuffer', '_malloc', '_free']" \
    -O3
```

## Loading WASM Modules

The SDN library attempts to load WASM from multiple locations:
1. `./[module].wasm` - Same directory as the application
2. `/[module].wasm` - Root of the web server
3. `https://digitalarsenal.github.io/space-data-network/cdn/[module].wasm` - CDN fallback

## CORS Requirements

When serving WASM files, ensure proper CORS headers:
```
Access-Control-Allow-Origin: *
Content-Type: application/wasm
```

## SRI Verification

Each WASM file has an accompanying `.sri` file containing its Subresource Integrity hash:
```html
<script src="sdn.js"
        integrity="sha384-xxxxx"
        crossorigin="anonymous"></script>
```

## Building All WASM Modules

Use the build script to compile all WASM modules:
```bash
npm run build:wasm
```

This requires Emscripten to be installed:
```bash
brew install emscripten  # macOS
apt install emscripten   # Ubuntu
```
