# Tauri + WASM Integration Fix ## ๐Ÿ” Problem WASM works in browser (`npm run dev`) but fails in Tauri webview (`npm run tauri:dev`) with error: ``` Cannot assign to read only property 'toString' of object '#' ``` ## โœ… Solutions Applied ### 1. **CSP (Content Security Policy) Update** **File:** `src-tauri/tauri.conf.json` Added required CSP directives for WASM: ```json { "security": { "csp": { "default-src": "'self' 'unsafe-inline' asset: https://asset.localhost", "script-src": "'self' 'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval'", // ... other directives with asset: protocol }, "assetProtocol": { "enable": true, "scope": ["**"] } } } ``` **Key changes:** - โœ… Added `'wasm-unsafe-eval'` to `script-src` - **REQUIRED for WASM execution in Tauri 2.x** - โœ… Added `asset:` and `https://asset.localhost` to relevant directives - โœ… Enabled `assetProtocol` to serve assets with proper MIME types ### 2. **Vite Config Update** **File:** `vite.config.ts` ```typescript export default defineConfig({ optimizeDeps: { exclude: ['@neptune/native'], // Only exclude native module // โŒ NOT excluding @neptune/wasm - it must be bundled! }, build: { assetsInlineLimit: 0, // Don't inline WASM as base64 }, assetsInclude: ['**/*.wasm'], // Serve WASM with correct MIME type }) ``` **Key changes:** - โœ… Removed `@neptune/wasm` from `exclude` and `external` - โœ… Added `assetsInlineLimit: 0` to prevent base64 encoding - โœ… Added `assetsInclude` for WASM MIME type ### 3. **Package.json Fix** ```json { "dependencies": { "axios": "^1.7.9" // Fixed from invalid "1.13.2" } } ``` ## ๐Ÿงช Testing ### 1. Browser (Should work) ```bash npm run dev ``` Open http://localhost:5173/auth โ†’ Create wallet โ†’ Should work ### 2. Tauri (Should work now) ```bash npm run tauri:dev ``` Create wallet โ†’ Should work without CSP/WASM errors ## ๐Ÿ› Additional Debugging ### If WASM still doesn't load: #### Check 1: WASM File in Dist ```bash npm run build ls dist/assets/*.wasm # Should see neptune_wasm_bg-*.wasm ``` #### Check 2: Browser DevTools (in Tauri) 1. Open Tauri app 2. Right-click โ†’ Inspect Element 3. Console tab โ†’ Check for errors 4. Network tab โ†’ Filter `.wasm` โ†’ Check if WASM loads (200 status) #### Check 3: CSP Errors In DevTools Console, look for: ``` Refused to execute WebAssembly script... ``` If you see this โ†’ CSP is still blocking WASM ### Temporary Debug: Disable CSP If nothing works, temporarily disable CSP to isolate the issue: ```json // src-tauri/tauri.conf.json { "security": { "dangerousDisableAssetCspModification": true, // Disable CSP temporarily "csp": null // Remove CSP entirely for testing } } ``` โš ๏ธ **WARNING:** Only use this for debugging! Re-enable CSP for production. ## ๐Ÿ“ Why This Happens ### Tauri vs Browser Differences | Feature | Browser | Tauri Webview | |---------|---------|---------------| | CSP | Permissive by default | Strict by default | | WASM | Always allowed | Needs `'wasm-unsafe-eval'` | | Asset loading | HTTP(S) | Custom `asset://` protocol | | MIME types | Auto-detected | Must be configured | ### WASM Loading in Tauri 1. Vite bundles WASM file โ†’ `dist/assets/neptune_wasm_bg-*.wasm` 2. Tauri serves it via `asset://localhost/assets/...` 3. CSP must allow: - `script-src 'wasm-unsafe-eval'` โ†’ Execute WASM - `connect-src asset:` โ†’ Fetch WASM file 4. AssetProtocol must serve with `Content-Type: application/wasm` ## ๐Ÿ”„ Next Steps After Fix ### 1. Test Full Wallet Flow - โœ… Generate wallet (WASM) - โœ… Display seed phrase - โœ… Confirm seed phrase - ๐Ÿšง Create keystore (needs Tauri commands) ### 2. Implement Tauri Commands See `TAURI_COMMANDS_TODO.md` (if it exists, otherwise create it) ### 3. Build & Test Production ```bash npm run tauri:build ``` ## ๐Ÿ“š References - [Tauri CSP Documentation](https://tauri.app/v2/reference/config/#securityconfig) - [Vite WASM Plugin](https://vitejs.dev/guide/features.html#webassembly) - [wasm-bindgen with Vite](https://rustwasm.github.io/wasm-bindgen/reference/deployment.html) ## ๐ŸŽฏ Summary **Problem:** Tauri CSP blocked WASM execution **Solution:** Add `'wasm-unsafe-eval'` + `asset:` protocol + proper Vite config **Status:** Should work now! ๐Ÿš€