Add end-to-end encryption for mobile WebSocket transport

Replace the disabled TLS cert pinning approach with application-layer
E2EE using tweetnacl (Curve25519 ECDH + XSalsa20-Poly1305). The
WebSocket stays plain ws:// but all RPC traffic is encrypted after
a one-round-trip handshake.

Desktop: generate persistent Curve25519 keypair, E2EE channel module
handles handshake state machine and transparent encrypt/decrypt.
Mobile: ephemeral keypair per connection for forward secrecy, expo-crypto
PRNG polyfill for Hermes compatibility.

Pairing schema updated from v1 (certFingerprint) to v2 (publicKeyB64).
Mock server updated with E2EE support. 24 tests covering crypto,
channel handshake, and integration.

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo-H
2026-04-30 18:03:24 -04:00
co-authored by Orca
parent cd340227ad
commit fc75b28f7d
25 changed files with 1191 additions and 201 deletions
+7 -7
View File
@@ -3,10 +3,10 @@ import { encodePairingOffer, decodePairingOffer, type PairingOffer } from './pai
describe('pairing offer', () => {
const offer: PairingOffer = {
v: 1,
endpoint: 'wss://192.168.1.10:6768',
v: 2,
endpoint: 'ws://192.168.1.10:6768',
deviceToken: 'abcdef1234567890abcdef1234567890abcdef1234567890',
certFingerprint: 'sha256:deadbeef'
publicKeyB64: 'dGVzdC1wdWJsaWMta2V5LWJhc2U2NC1lbmNvZGVk'
}
it('encode then decode round-trips correctly', () => {
@@ -32,19 +32,19 @@ describe('pairing offer', () => {
})
it('rejects payloads with missing fields', () => {
const partial = { v: 1, endpoint: 'wss://host:1234' }
const partial = { v: 2, endpoint: 'ws://host:1234' }
const base64 = Buffer.from(JSON.stringify(partial)).toString('base64')
expect(() => decodePairingOffer(`orca://pair#${base64}`)).toThrow()
})
it('rejects payloads with wrong version', () => {
const wrong = { ...offer, v: 2 }
const wrong = { ...offer, v: 1 }
const base64 = Buffer.from(JSON.stringify(wrong)).toString('base64')
expect(() => decodePairingOffer(`orca://pair#${base64}`)).toThrow()
})
it('rejects payloads with invalid fingerprint prefix', () => {
const wrong = { ...offer, certFingerprint: 'md5:abc' }
it('rejects payloads with missing publicKeyB64', () => {
const wrong = { v: 2, endpoint: 'ws://host:1234', deviceToken: 'tok' }
const base64 = Buffer.from(JSON.stringify(wrong)).toString('base64')
expect(() => decodePairingOffer(`orca://pair#${base64}`)).toThrow()
})
+7 -5
View File
@@ -1,15 +1,17 @@
import { z } from 'zod'
export const PAIRING_OFFER_VERSION = 1
export const PAIRING_OFFER_VERSION = 2
export const PairingOfferV1 = z.object({
export const PairingOfferSchema = z.object({
v: z.literal(PAIRING_OFFER_VERSION),
endpoint: z.string().min(1),
deviceToken: z.string().min(1),
certFingerprint: z.string().startsWith('sha256:')
// Why: the desktop's Curve25519 public key, base64-encoded. The mobile client
// uses this to derive a shared secret via ECDH for end-to-end encryption.
publicKeyB64: z.string().min(1)
})
export type PairingOffer = z.infer<typeof PairingOfferV1>
export type PairingOffer = z.infer<typeof PairingOfferSchema>
export function encodePairingOffer(offer: PairingOffer): string {
const json = JSON.stringify(offer)
@@ -29,5 +31,5 @@ export function decodePairingOffer(url: string): PairingOffer {
const base64url = url.slice(hashIndex + 1)
const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/')
const json = Buffer.from(base64, 'base64').toString('utf-8')
return PairingOfferV1.parse(JSON.parse(json))
return PairingOfferSchema.parse(JSON.parse(json))
}