7.0 KiB
Modules
Modules extend Synapse without rebuilding the app. A module is one WebAssembly file that runs
sandboxed in the app (crates/synapse-modules, wasmi interpreter: no JIT, so it runs on
Linux, Windows and Android alike) and reaches the app only through host functions allowed by
the permissions the user accepted when installing it. Its UI is data: views and block macros
are JSON trees the app renders.
Installing
Settings → Modules: modules bundled with the app (app/assets/modules/, offered with an
Install button) or Install from file… (.wasm). The app reads the manifest and shows a
consent dialog with the permissions; installing an update with new permissions asks again.
Installed modules live in <graph>/modules/<id>.wasm with modules.json (enabled flag and
granted permissions), so installation is per device. A file swapped on disk cannot gain
permissions: they are checked against modules.json on every start. Removing a module keeps
its data unless "also delete its data" is ticked (that deletion syncs).
Writing a module
Use the SDK in modules/sdk (Rust, wasm32-unknown-unknown); modules/secrets is a complete
example. Build every bundled module with scripts/build-modules.sh (needs
rustup target add wasm32-unknown-unknown; it copies <id>.wasm into app/assets/modules/,
which is committed so the app and the host tests use the same file).
synapse_module_sdk::manifest!("../manifest.json");
synapse_module_sdk::module!(handle);
fn handle(req: Value) -> Value { json!({ "ui": ui::text("Hello") }) }
Manifest (custom section synapse.manifest, JSON)
| Field | Meaning |
|---|---|
api |
Module API version, 1 |
id |
Lowercase letters, digits, ., -, _ (≤ 64), e.g. synapse.secrets |
name, version, description, author |
Shown in Settings and the consent dialog |
permissions |
Any of storage, crypto, clipboard, pages:read, pages:write |
views |
[{id, title, icon?}]: sidebar entries (icon is an emoji) |
macros |
Names of {{name args}} block macros it renders |
tick_seconds |
Optional (≥ 5): the app sends {"op":"tick"} this often |
ABI (API v1)
The module exports memory, syn_alloc(len) -> ptr and syn_handle(ptr, len). The host
writes a JSON request into a buffer from syn_alloc and calls syn_handle, which answers by
calling the import synapse.respond(ptr, len). Host calls: synapse.call(ptr, len) -> n
with {"fn": name, ...}, then synapse.take(ptr) copies the n-byte reply
({"ok": value} or {"error": message}). The SDK wraps all of this.
Limits per request: 10⁹ fuel units (about a second of work; a module that runs out is stopped), 64 MiB of linear memory, 4 MiB per message. A module that traps loses its instance (and in-memory state); the next request starts a fresh one.
Requests from the app
| Request | Expected answer |
|---|---|
{"op":"render","view":id} |
{"ui": tree} |
{"op":"event","view":id,"action":a,"arg":s?,"values":{field id: text}} |
{"ui": tree, "effects": [...]} |
{"op":"macro","name":n,"args":s} |
{"inline": chip} |
{"op":"macro_action","name":n,"args":s,"action":a} |
{"inline": chip, "effects": [...]} |
{"op":"tick"} |
{"changed": bool} (true redraws views and macros) |
{"op":"suspend"} |
{"changed": bool}; sent when a phone app is paused |
Any answer may be {"error": message}.
UI tree nodes (type): column{children}, row{children} (fields and pairs stretch),
split{left,right} (side by side when wide, stacked when narrow), heading{text},
text{text, style: muted|error|mono}, divider, pair{label, value, mono?, link?} (links
only for http/https), field{id, label, value, secret?, multiline?, hint?, autofocus?, submit?: action, change?: action, icon?}, button{label, action, arg?, style: primary|danger|plain, icon?}, list{items, empty}, item{title, subtitle, action, arg, selected?}. A field keeps what the user typed until the module sends a different value.
Icon names: add, lock, unlock, copy, eye, eye_off, edit, delete, key, refresh, save, close,
search, link, person.
Chip (macro): {text, icon?, masked?, revealed?, error?, actions: [{id, label, icon}]}.
Effects: {"copy": {"text", "clear_after_ms"?}} (needs clipboard; the app clears the
clipboard later only if it still holds that text), {"toast": text},
{"open_view": view id}, {"refresh_macros": true}.
Host functions
fn |
Permission | Arguments → result |
|---|---|---|
log |
— | msg |
now |
— | → ms since epoch |
random |
— | len (≤ 4096) → base64 |
kv.get / kv.set / kv.list |
storage |
key, value (string or null to delete), prefix → [{key, value}] |
crypto.derive |
crypto |
password, salt (b64), memory_kib ≤ 256 MiB, iterations ≤ 10, lanes ≤ 4 → key handle (Argon2id) |
crypto.generate |
crypto |
→ random key handle |
crypto.seal / crypto.open |
crypto |
key, plaintext / data (b64), aad (XChaCha20-Poly1305, random nonce, nonce‖ct) |
crypto.wrap / crypto.unwrap |
crypto |
key (kek), target / data, aad → wrapped key / handle |
crypto.forget / crypto.forget_all |
crypto |
drop key handles (keys are zeroed) |
pages.get |
pages:read |
title → {title, markdown} or null |
pages.append |
pages:write |
title, text → adds a block (creates the page) |
Keys never enter the module: it holds handles; the host keeps the keys (zeroed on drop) and does the cryptography natively (Argon2 inside the interpreter would take seconds).
storage is the module's own key-value store in the graph (module_data table, schema v9),
synced with the op put_module_data (last writer wins per key, deletes are tombstones;
keys ≤ 256 bytes, values ≤ 1 MiB). Modules never see each other's data.
Secrets (synapse.secrets)
An encrypted vault: Settings → Modules → Secrets → Install, then the Secrets sidebar
view. Create the vault with a master password (≥ 8 characters), add entries (name, username,
password with a generator, URL, notes), and reference one in any block with
{{secret Name}}: it shows masked, with buttons to show it (hidden again after 20 s), copy
the password or the username (the clipboard clears after 30 s).
- Crypto: a random 256-bit data key encrypts every entry (XChaCha20-Poly1305, the entry key as associated data). The data key is wrapped by a key derived from the master password (Argon2id, 64 MiB, 3 iterations, random salt). Changing the password re-wraps the data key.
- Only ciphertext is stored and synced; the server and the op log never see names or values. Secrets are not part of history snapshots.
- Locks after 5 minutes without use, on request, when a phone app is paused, or when the module stops. Forgetting the master password means the secrets are lost.
- Create the vault on one device and unlock it on the others once synced: two vaults created offline on two devices would each have their own data key (entries from the other one show as "could not be decrypted").