Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(license): main-process license module + offline entitlement verify

enes8fd18a80ebfa
diff --git a/src/main/device-id.ts b/src/main/device-id.ts
new file mode 100644
index 000000000..8473e1e76
--- /dev/null
+++ b/src/main/device-id.ts
@@ -0,0 +1,18 @@
+import { promises as fs, readFileSync } from 'fs'
+import path from 'path'
+import { randomUUID } from 'crypto'
+import { app } from 'electron'
+
+// Stable anonymous machine id, persisted in userData (shared with telemetry).
+export function getDeviceId(): string {
+ const file = path.join(app.getPath('userData'), 'device-id')
+ try {
+ const existing = readFileSync(file, 'utf-8').trim()
+ if (existing) return existing
+ } catch {
+ // not created yet
+ }
+ const id = randomUUID()
+ void fs.writeFile(file, id, 'utf-8').catch(() => {})
+ return id
+}
diff --git a/src/main/entitlement-key.ts b/src/main/entitlement-key.ts
new file mode 100644
index 000000000..41279ed53
--- /dev/null
+++ b/src/main/entitlement-key.ts
@@ -0,0 +1,5 @@
+// Ed25519 PUBLIC key (SPKI PEM) used to verify entitlement tokens offline. Replace this with
+// the public key printed when you generate the server's ENTITLEMENT_PRIVATE_KEY (see
+// server/README.md). An empty value disables premium (offline verification always fails),
+// which is the safe default for dev/unsigned builds.
+export const ENTITLEMENT_PUBLIC_KEY = ''
diff --git a/src/main/index.ts b/src/main/index.ts
index ef3bdae5d..afa302252 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -18,6 +18,7 @@ import { createContextTail } from './context-tail'
import { initBridge } from './bridge'
import { initTelemetry } from './telemetry'
import { initClaudeUsage } from './claude-usage'
+import { initLicense } from './license'
const settingsStore = new SettingsStore()
const ptyManager = new PtyManager()
@@ -271,6 +272,7 @@ app.whenReady().then(async () => {
initBridge(win, ptyManager)
initClaudeUsage(win)
initTelemetry(() => settingsStore.get())
+ initLicense(win)
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
diff --git a/src/main/license.ts b/src/main/license.ts
new file mode 100644
index 000000000..569b4f2bf
--- /dev/null
+++ b/src/main/license.ts
@@ -0,0 +1,123 @@
+// License/premium client. Runs in the main process: stores the key + last entitlement token,
+// activates/refreshes against our API, and verifies the token OFFLINE with the embedded
+// Ed25519 public key. Offline grace: a still-unexpired stored token keeps premium alive when
+// a refresh can't reach the server.
+import { promises as fs, readFileSync } from 'fs'
+import path from 'path'
+import crypto from 'node:crypto'
+import { app, ipcMain, type BrowserWindow } from 'electron'
+import { IPC } from '../shared/ipc'
+import type { LicenseStatus } from '../shared/types'
+import { getDeviceId } from './device-id'
+import { ENTITLEMENT_PUBLIC_KEY } from './entitlement-key'
+
+const API_BASE = process.env.NODETERM_API_BASE || 'https://api.nodeterm.dev'
+
+interface Stored {
+ key?: string
+ token?: string
+}
+
+function file(): string {
+ return path.join(app.getPath('userData'), 'license.json')
+}
+function load(): Stored {
+ try {
+ return JSON.parse(readFileSync(file(), 'utf-8')) as Stored
+ } catch {
+ return {}
+ }
+}
+async function save(s: Stored): Promise<void> {
+ await fs.writeFile(file(), JSON.stringify(s), 'utf-8').catch(() => {})
+}
+
+interface Payload {
+ deviceId: string
+ tier: string
+ licenseId: string
+ exp: number
+}
+
+// Offline verification of our compact Ed25519 token: base64url(payload).base64url(sig).
+function verify(token: string | undefined): Payload | null {
+ if (!token || !ENTITLEMENT_PUBLIC_KEY) return null
+ const dot = token.indexOf('.')
+ if (dot < 1) return null
+ const p = token.slice(0, dot)
+ const s = token.slice(dot + 1)
+ try {
+ const key = crypto.createPublicKey(ENTITLEMENT_PUBLIC_KEY)
+ if (!crypto.verify(null, Buffer.from(p), key, Buffer.from(s, 'base64url'))) return null
+ const payload = JSON.parse(Buffer.from(p, 'base64url').toString('utf-8')) as Payload
+ if (typeof payload.exp !== 'number' || payload.exp * 1000 <= Date.now()) return null
+ return payload
+ } catch {
+ return null
+ }
+}
+
+function statusFrom(token: string | undefined, error: string | null = null): LicenseStatus {
+ const p = verify(token)
+ return p
+ ? { tier: p.tier, active: true, expiresAt: p.exp, error: null }
+ : { tier: null, active: false, expiresAt: null, error }
+}
+
+async function call(path: string, body: unknown): Promise<{ token?: string; error?: string }> {
+ try {
+ const ctrl = new AbortController()
+ const t = setTimeout(() => ctrl.abort(), 8000)
+ const res = await fetch(`${API_BASE}${path}`, {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify(body),
+ signal: ctrl.signal
+ }).finally(() => clearTimeout(t))
+ if (res.status === 204) return {}
+ const json = (await res.json().catch(() => ({}))) as { token?: string; error?: string }
+ if (!res.ok) return { error: json.error ?? 'network' }
+ return json
+ } catch {
+ return { error: 'offline' }
+ }
+}
+
+export function initLicense(win: BrowserWindow): void {
+ const deviceId = getDeviceId()
+ const broadcast = (s: LicenseStatus) => {
+ if (!win.isDestroyed()) win.webContents.send(IPC.licenseChanged, s)
+ }
+
+ ipcMain.handle(IPC.licenseStatus, () => statusFrom(load().token))
+
+ ipcMain.handle(IPC.licenseActivate, async (_e, key: string) => {
+ const r = await call('/v1/license/activate', { key: String(key).trim(), deviceId })
+ if (r.token) await save({ key: String(key).trim(), token: r.token })
+ const status = statusFrom(r.token, r.error ?? null)
+ broadcast(status)
+ return status
+ })
+
+ ipcMain.handle(IPC.licenseDeactivate, async () => {
+ const stored = load()
+ if (stored.key) await call('/v1/license/deactivate', { key: stored.key, deviceId })
+ await save({})
+ const status = statusFrom(undefined)
+ broadcast(status)
+ return status
+ })
+
+ // On launch: refresh the token if we have a key, but keep the last valid token on failure.
+ void (async () => {
+ const stored = load()
+ if (!stored.key) return
+ const r = await call('/v1/license/refresh', { key: stored.key, deviceId })
+ if (r.token) {
+ await save({ key: stored.key, token: r.token })
+ broadcast(statusFrom(r.token))
+ } else {
+ broadcast(statusFrom(stored.token, r.error ?? null)) // offline grace
+ }
+ })()
+}