Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3243647aa1 | |||
| 037f994572 | |||
| 188c3db56a | |||
| 1bd7037a6d | |||
| 80c410f518 | |||
| 3c0db14904 | |||
| a98ea8a1bd | |||
| d47774ef1c | |||
| 8c606ad498 | |||
| 680d6386f3 | |||
| 1fb59a7076 | |||
| 594992a183 | |||
| 8746571d2a | |||
| 2c400d7094 | |||
| 2b1b4d6630 | |||
| dbb3a090d8 | |||
| f5f42fe557 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,3 +2,6 @@ node_modules/
|
|||||||
dist/
|
dist/
|
||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
**/.tmp-*.db
|
||||||
|
**/.tmp-*.db-shm
|
||||||
|
**/.tmp-*.db-wal
|
||||||
|
|||||||
1229
CHANGELOG.md
1229
CHANGELOG.md
File diff suppressed because it is too large
Load Diff
@@ -100,6 +100,9 @@ matrix row in the same change.
|
|||||||
| § 1 Network attacker — replay window | ±5 min `signedAt` enforcement | `packages/shade-server/tests/server.test.ts` (`"rejects registration with stale signedAt"`) |
|
| § 1 Network attacker — replay window | ±5 min `signedAt` enforcement | `packages/shade-server/tests/server.test.ts` (`"rejects registration with stale signedAt"`) |
|
||||||
| § 1 Network attacker — header AAD | Ratchet headers bound to ciphertext | `packages/shade-core/tests/ratchet.test.ts`, `packages/shade-streams/tests/tamper.test.ts`, `packages/shade-streams/tests/aead.test.ts` |
|
| § 1 Network attacker — header AAD | Ratchet headers bound to ciphertext | `packages/shade-core/tests/ratchet.test.ts`, `packages/shade-streams/tests/tamper.test.ts`, `packages/shade-streams/tests/aead.test.ts` |
|
||||||
| § 1 Network attacker — forward secrecy | DH ratchet step + chain-key zeroize | `packages/shade-core/tests/ratchet.test.ts`, `packages/shade-crypto-web/tests/hardening.test.ts` |
|
| § 1 Network attacker — forward secrecy | DH ratchet step + chain-key zeroize | `packages/shade-core/tests/ratchet.test.ts`, `packages/shade-crypto-web/tests/hardening.test.ts` |
|
||||||
|
| § 1 Network attacker — streaming sub-session FS/replay (V4.11) | Per-frame Double-Ratchet `seal`/`open`; counter-rewind & replay rejected; in-memory-only (never persisted) | `packages/shade-core/tests/stream.test.ts` (`"R1: replayed / rewound frame is rejected"`, `"R2/R3: long one-directional burst stays correct and memory-bounded"`) |
|
||||||
|
| § 1 Network attacker — streaming handshake auth (V4.11) | Identity-bound 3-DH against parent-session-pinned identities | `packages/shade-core/tests/stream.test.ts` (`"handshake is mutually authenticated against pinned identities"`) |
|
||||||
|
| § 3 Endpoint compromise — streaming sub-session isolation (V4.11) | Stream ratchet derived without touching the stored parent session; zeroized on close | `packages/shade-core/tests/stream.test.ts` (`"R5: opening/using/closing a stream never touches the parent session"`, `"close() zeroizes and blocks further use; idempotent"`) |
|
||||||
| § 2 Compromised prekey server — public-only storage | Prekey store never accepts a private key | `packages/shade-server/tests/server.test.ts`, `packages/shade-storage-sqlite/tests/sqlite-prekey-store.test.ts` |
|
| § 2 Compromised prekey server — public-only storage | Prekey store never accepts a private key | `packages/shade-server/tests/server.test.ts`, `packages/shade-storage-sqlite/tests/sqlite-prekey-store.test.ts` |
|
||||||
| § 2 Compromised prekey server — signed replenish/delete | Per-identity Ed25519 signature | `packages/shade-server/tests/server.test.ts` |
|
| § 2 Compromised prekey server — signed replenish/delete | Per-identity Ed25519 signature | `packages/shade-server/tests/server.test.ts` |
|
||||||
| § 2 Compromised prekey server — fake-bundle detection | Out-of-band fingerprint comparison | `packages/shade-core/tests/fingerprint-session.test.ts` |
|
| § 2 Compromised prekey server — fake-bundle detection | Out-of-band fingerprint comparison | `packages/shade-core/tests/fingerprint-session.test.ts` |
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm") version "2.0.20" apply false
|
kotlin("jvm") version "2.0.20" apply false
|
||||||
|
kotlin("android") version "2.0.20" apply false
|
||||||
|
id("com.android.library") version "8.7.3" apply false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,3 +2,5 @@ org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8
|
|||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
org.gradle.caching=true
|
org.gradle.caching=true
|
||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
|
android.useAndroidX=true
|
||||||
|
android.nonTransitiveRClass=true
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ pluginManagement {
|
|||||||
repositories {
|
repositories {
|
||||||
gradlePluginPortal()
|
gradlePluginPortal()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
google()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,3 +18,6 @@ dependencyResolutionManagement {
|
|||||||
|
|
||||||
include(":shade-android")
|
include(":shade-android")
|
||||||
project(":shade-android").projectDir = file("shade-android")
|
project(":shade-android").projectDir = file("shade-android")
|
||||||
|
|
||||||
|
include(":shade-android-keystore")
|
||||||
|
project(":shade-android-keystore").projectDir = file("shade-android-keystore")
|
||||||
|
|||||||
68
android/shade-android-keystore/README.md
Normal file
68
android/shade-android-keystore/README.md
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
# shade-android-keystore
|
||||||
|
|
||||||
|
Android-specific bindings for `shade-android`. Lives as a sibling Gradle module so the JVM-only protocol code can keep running in CI without an Android SDK install.
|
||||||
|
|
||||||
|
Provides:
|
||||||
|
|
||||||
|
- **`KeystoreMasterKey`** — hardware-backed AES-256-GCM master key in the Android Keystore. Optionally biometric-gated (BIOMETRIC_STRONG only — Class 3 assurance), StrongBox-backed when available, invalidated on new biometric enrollment.
|
||||||
|
- **`BiometricUnlock`** — coroutine wrapper around `BiometricPrompt` for unlocking a `Cipher` instance bound to the keystore key. Throws `BiometricCancelledException` / `BiometricFailedException` so callers can handle the auth flow without writing custom callbacks.
|
||||||
|
- **`KeystoreStorage`** — `StorageProvider` implementation that persists session/identity/prekey state to `SharedPreferences`, each row encrypted under the keystore key with the row's preference key bound as AAD.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
import no.zyon.shade.ShadeSessionManager
|
||||||
|
import no.zyon.shade.crypto.TinkProvider
|
||||||
|
import no.zyon.shade.keystore.BiometricUnlock
|
||||||
|
import no.zyon.shade.keystore.KeystoreStorage
|
||||||
|
|
||||||
|
class MyActivity : FragmentActivity() {
|
||||||
|
private val crypto = TinkProvider()
|
||||||
|
private lateinit var storage: KeystoreStorage
|
||||||
|
private lateinit var manager: ShadeSessionManager
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
storage = KeystoreStorage(this, crypto)
|
||||||
|
|
||||||
|
lifecycleScope.launch {
|
||||||
|
val unlock = BiometricUnlock(
|
||||||
|
activity = this@MyActivity,
|
||||||
|
title = "Unlock Shade",
|
||||||
|
subtitle = "Tap your fingerprint to access your messages",
|
||||||
|
)
|
||||||
|
try {
|
||||||
|
storage.unlock(unlock)
|
||||||
|
} catch (e: BiometricCancelledException) {
|
||||||
|
// user backed out — show a "tap to retry" UI
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
|
||||||
|
manager = ShadeSessionManager(crypto, storage)
|
||||||
|
manager.initialize()
|
||||||
|
// ... use manager normally
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For credential-driven bootstrap (V4.9 profile + V4.10 approval), pair this with `no.zyon.shade.blob.createProfileNamespace` and `no.zyon.shade.approval.signProxyApproval` — both pure-JVM (in `:shade-android`).
|
||||||
|
|
||||||
|
## Threat model
|
||||||
|
|
||||||
|
- **Compromised app process**: cannot read the AES key (it's in the secure environment). Can attempt to use the cipher only after the user has authenticated; biometric re-prompts are required after each biometric event.
|
||||||
|
- **Stolen device with known PIN**: cannot unlock — `setUserAuthenticationParameters(0, BIOMETRIC_STRONG)` excludes `DEVICE_CREDENTIAL`.
|
||||||
|
- **Attacker enrolls own biometric**: `setInvalidatedByBiometricEnrollment(true)` invalidates the key on enrollment, forcing a credential rebootstrap (which would need username + password + PIN).
|
||||||
|
- **Catastrophic recovery**: `forgetEverything()` deletes the master key and clears the SharedPreferences. Pair with `Profile.delete()` for full account erasure.
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
Requires an Android SDK. The Gradle build uses Android Gradle Plugin 8.7+, AGP minSdk 28 (Pie+ for BiometricPrompt baseline), targetSdk 35.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
JAVA_HOME=/path/to/jdk-21 ./gradlew :shade-android-keystore:assembleDebug
|
||||||
|
```
|
||||||
|
|
||||||
|
Unit tests: none yet — `KeystoreStorage` requires Android runtime. Robolectric or instrumented tests against an emulator are tracked as a follow-up. The pure-JVM `SessionStateJson` round-trip serializer is tested in `:shade-android` (`SessionStateJsonTest`).
|
||||||
59
android/shade-android-keystore/build.gradle.kts
Normal file
59
android/shade-android-keystore/build.gradle.kts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
plugins {
|
||||||
|
id("com.android.library")
|
||||||
|
kotlin("android")
|
||||||
|
}
|
||||||
|
|
||||||
|
// V4.10 — Android-specific KeystoreStorage adapter.
|
||||||
|
//
|
||||||
|
// Lives as a sibling module to `:shade-android` so the JVM-only
|
||||||
|
// protocol code can keep running in CI without an Android SDK.
|
||||||
|
// This module pulls in `:shade-android` for `StorageProvider`,
|
||||||
|
// `IdentityKeyPair`, etc., and binds those types to a hardware-
|
||||||
|
// backed Android Keystore master key with biometric gating.
|
||||||
|
|
||||||
|
android {
|
||||||
|
namespace = "no.zyon.shade.keystore"
|
||||||
|
compileSdk = 35
|
||||||
|
|
||||||
|
defaultConfig {
|
||||||
|
minSdk = 28 // BiometricPrompt + StrongBox baseline
|
||||||
|
}
|
||||||
|
|
||||||
|
compileOptions {
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "17"
|
||||||
|
}
|
||||||
|
|
||||||
|
buildTypes {
|
||||||
|
release {
|
||||||
|
isMinifyEnabled = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
testOptions {
|
||||||
|
unitTests.isReturnDefaultValues = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
// Sibling: protocol types + StorageProvider interface.
|
||||||
|
api(project(":shade-android"))
|
||||||
|
|
||||||
|
// androidx.biometric — fragment-safe BiometricPrompt wrapper.
|
||||||
|
// 1.2.0-alpha05 is the latest with stable BiometricPrompt API.
|
||||||
|
implementation("androidx.biometric:biometric:1.2.0-alpha05")
|
||||||
|
|
||||||
|
// androidx.fragment — BiometricPrompt requires FragmentActivity.
|
||||||
|
implementation("androidx.fragment:fragment-ktx:1.8.5")
|
||||||
|
|
||||||
|
// Coroutines for the suspend-function StorageProvider implementation.
|
||||||
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")
|
||||||
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0")
|
||||||
|
|
||||||
|
testImplementation("junit:junit:4.13.2")
|
||||||
|
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android" />
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package no.zyon.shade.keystore
|
||||||
|
|
||||||
|
import androidx.biometric.BiometricManager
|
||||||
|
import androidx.biometric.BiometricPrompt
|
||||||
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
import javax.crypto.Cipher
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
import kotlin.coroutines.resumeWithException
|
||||||
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Biometric unlock for a `KeystoreMasterKey`-bound `Cipher`.
|
||||||
|
*
|
||||||
|
* The Android keystore enforces that any operation on a
|
||||||
|
* user-authentication-required key must happen via a
|
||||||
|
* `BiometricPrompt.CryptoObject`-wrapped `Cipher`. The user sees a
|
||||||
|
* system biometric prompt; on success the same `Cipher` instance is
|
||||||
|
* usable for one operation (or one streaming session) before
|
||||||
|
* needing to re-prompt.
|
||||||
|
*
|
||||||
|
* This is a thin coroutine wrapper around `BiometricPrompt` that
|
||||||
|
* resolves to the authenticated cipher or throws on user
|
||||||
|
* cancellation. Callers typically run it once at app start to
|
||||||
|
* unlock the master key for the lifetime of the foreground session.
|
||||||
|
*/
|
||||||
|
class BiometricUnlock(
|
||||||
|
private val activity: FragmentActivity,
|
||||||
|
private val title: String,
|
||||||
|
private val subtitle: String? = null,
|
||||||
|
private val negativeButton: String = "Cancel",
|
||||||
|
) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True if BIOMETRIC_STRONG is currently usable on this device.
|
||||||
|
* False means the user has no enrolled fingerprint/face that
|
||||||
|
* meets the class-3 assurance level — fall back to a credential
|
||||||
|
* recovery flow rather than crashing.
|
||||||
|
*/
|
||||||
|
fun canAuthenticate(): Boolean {
|
||||||
|
val mgr = BiometricManager.from(activity)
|
||||||
|
return mgr.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG) ==
|
||||||
|
BiometricManager.BIOMETRIC_SUCCESS
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the biometric prompt and return the authenticated cipher.
|
||||||
|
*
|
||||||
|
* Cancellation paths:
|
||||||
|
* - User taps the negative button → throws `BiometricCancelledException`.
|
||||||
|
* - System errors out (e.g. too many failures) → throws
|
||||||
|
* `BiometricFailedException` with the system error code.
|
||||||
|
*/
|
||||||
|
suspend fun unlock(cipher: Cipher): Cipher = suspendCancellableCoroutine { cont ->
|
||||||
|
val callback = object : BiometricPrompt.AuthenticationCallback() {
|
||||||
|
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
|
||||||
|
val authedCipher = result.cryptoObject?.cipher
|
||||||
|
if (authedCipher == null) {
|
||||||
|
cont.resumeWithException(
|
||||||
|
BiometricFailedException(-1, "BiometricPrompt returned no cipher"),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
cont.resume(authedCipher)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
|
||||||
|
if (errorCode == BiometricPrompt.ERROR_USER_CANCELED ||
|
||||||
|
errorCode == BiometricPrompt.ERROR_NEGATIVE_BUTTON ||
|
||||||
|
errorCode == BiometricPrompt.ERROR_CANCELED
|
||||||
|
) {
|
||||||
|
cont.resumeWithException(BiometricCancelledException(errString.toString()))
|
||||||
|
} else {
|
||||||
|
cont.resumeWithException(
|
||||||
|
BiometricFailedException(errorCode, errString.toString()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAuthenticationFailed() {
|
||||||
|
// A single failed attempt — the prompt stays open and
|
||||||
|
// gives the user another try. Don't resume the
|
||||||
|
// continuation; let the system flow continue.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val prompt = BiometricPrompt(
|
||||||
|
activity,
|
||||||
|
activity.mainExecutor,
|
||||||
|
callback,
|
||||||
|
)
|
||||||
|
val info = BiometricPrompt.PromptInfo.Builder()
|
||||||
|
.setTitle(title)
|
||||||
|
.apply { if (subtitle != null) setSubtitle(subtitle) }
|
||||||
|
.setNegativeButtonText(negativeButton)
|
||||||
|
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
cont.invokeOnCancellation { prompt.cancelAuthentication() }
|
||||||
|
prompt.authenticate(info, BiometricPrompt.CryptoObject(cipher))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** User cancelled the biometric prompt. */
|
||||||
|
class BiometricCancelledException(message: String) : RuntimeException(message)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BiometricPrompt returned a non-cancellation error (lockout, hardware
|
||||||
|
* unavailable, no enrolled biometrics, etc.). Inspect `errorCode`
|
||||||
|
* against `BiometricPrompt.ERROR_*` constants to decide UX response.
|
||||||
|
*/
|
||||||
|
class BiometricFailedException(val errorCode: Int, message: String) :
|
||||||
|
RuntimeException("[$errorCode] $message")
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
package no.zyon.shade.keystore
|
||||||
|
|
||||||
|
import android.os.Build
|
||||||
|
import android.security.keystore.KeyGenParameterSpec
|
||||||
|
import android.security.keystore.KeyProperties
|
||||||
|
import javax.crypto.Cipher
|
||||||
|
import javax.crypto.KeyGenerator
|
||||||
|
import javax.crypto.SecretKey
|
||||||
|
import javax.crypto.spec.GCMParameterSpec
|
||||||
|
import java.security.KeyStore
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hardware-backed AES-256-GCM master key in the Android Keystore.
|
||||||
|
*
|
||||||
|
* The key never leaves the secure environment — Android's keystore
|
||||||
|
* implementation enforces that all encrypt/decrypt operations
|
||||||
|
* happen inside the TEE (or StrongBox if present), and the raw
|
||||||
|
* key bytes are never returned to userspace.
|
||||||
|
*
|
||||||
|
* The key is created on first use with these properties:
|
||||||
|
*
|
||||||
|
* - AES-256-GCM, no padding
|
||||||
|
* - User authentication required: opt-in via the `requireBiometric`
|
||||||
|
* flag. When true, every encrypt/decrypt operation must be wrapped
|
||||||
|
* in a `BiometricPrompt.authenticate(CryptoObject(cipher))` call
|
||||||
|
* that succeeds within the same `Cipher` instance.
|
||||||
|
* - StrongBox-backed if available (Pixel 3+, most Samsung flagships).
|
||||||
|
* Falls back to TEE on devices without StrongBox.
|
||||||
|
* - InvalidatedByBiometricEnrollment(true): a newly enrolled
|
||||||
|
* fingerprint/face invalidates the key, forcing the user to
|
||||||
|
* re-bootstrap from credentials. Defends against a thief who
|
||||||
|
* enrolls their own biometric.
|
||||||
|
*
|
||||||
|
* Mirrors the role `KeyManager` plays in `@shade/storage-encrypted`'s
|
||||||
|
* V4.5 KDF chain: this is the *encryption-at-rest* master key, not
|
||||||
|
* the X3DH identity key. The Shade protocol's identity keys are
|
||||||
|
* stored encrypted under THIS key.
|
||||||
|
*/
|
||||||
|
class KeystoreMasterKey(
|
||||||
|
private val alias: String,
|
||||||
|
private val requireBiometric: Boolean = true,
|
||||||
|
) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
require(alias.isNotEmpty()) { "alias must be non-empty" }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a `Cipher` initialized for encryption with the master key.
|
||||||
|
*
|
||||||
|
* If the key requires user auth, the returned cipher is *not yet
|
||||||
|
* usable* — the caller MUST wrap it in a
|
||||||
|
* `BiometricPrompt.authenticate(CryptoObject(cipher))` and use
|
||||||
|
* the cipher exposed by the auth-success callback. Calling
|
||||||
|
* `cipher.doFinal(...)` before authentication throws
|
||||||
|
* `UserNotAuthenticatedException`.
|
||||||
|
*/
|
||||||
|
fun cipherForEncrypt(): Cipher {
|
||||||
|
val key = getOrCreateKey()
|
||||||
|
val cipher = Cipher.getInstance(TRANSFORMATION)
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, key)
|
||||||
|
return cipher
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a `Cipher` initialized for decryption with the master key
|
||||||
|
* and a previously-stored 12-byte nonce. Same authentication
|
||||||
|
* requirement as `cipherForEncrypt`.
|
||||||
|
*/
|
||||||
|
fun cipherForDecrypt(nonce: ByteArray): Cipher {
|
||||||
|
require(nonce.size == 12) { "GCM nonce must be 12 bytes" }
|
||||||
|
val key = getOrCreateKey()
|
||||||
|
val cipher = Cipher.getInstance(TRANSFORMATION)
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key, GCMParameterSpec(128, nonce))
|
||||||
|
return cipher
|
||||||
|
}
|
||||||
|
|
||||||
|
/** True if the key already exists in the Android Keystore. */
|
||||||
|
fun exists(): Boolean {
|
||||||
|
val ks = KeyStore.getInstance(ANDROID_KEYSTORE).apply { load(null) }
|
||||||
|
return ks.containsAlias(alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete the master key. Catastrophic — all data encrypted under
|
||||||
|
* it becomes unrecoverable. Used by the "forget everything" flow
|
||||||
|
* (paired with `Profile.delete()` in the V4.9 namespace).
|
||||||
|
*/
|
||||||
|
fun deleteKey() {
|
||||||
|
val ks = KeyStore.getInstance(ANDROID_KEYSTORE).apply { load(null) }
|
||||||
|
if (ks.containsAlias(alias)) ks.deleteEntry(alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getOrCreateKey(): SecretKey {
|
||||||
|
val ks = KeyStore.getInstance(ANDROID_KEYSTORE).apply { load(null) }
|
||||||
|
ks.getEntry(alias, null)?.let { entry ->
|
||||||
|
return (entry as KeyStore.SecretKeyEntry).secretKey
|
||||||
|
}
|
||||||
|
return generateKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun generateKey(): SecretKey {
|
||||||
|
val builder = KeyGenParameterSpec.Builder(
|
||||||
|
alias,
|
||||||
|
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT,
|
||||||
|
)
|
||||||
|
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
|
||||||
|
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
|
||||||
|
.setKeySize(256)
|
||||||
|
// Each encrypt operation generates a fresh IV in the secure
|
||||||
|
// env; we read it back via `cipher.iv` after init.
|
||||||
|
.setRandomizedEncryptionRequired(true)
|
||||||
|
|
||||||
|
if (requireBiometric) {
|
||||||
|
builder.setUserAuthenticationRequired(true)
|
||||||
|
// BIOMETRIC_STRONG only — class 3, the highest assurance
|
||||||
|
// level (Class 3 = false-accept rate < 1/50 000 per BiometricPrompt).
|
||||||
|
// DEVICE_CREDENTIAL is intentionally NOT included: a stolen
|
||||||
|
// device with a known PIN should not unlock Shade.
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
builder.setUserAuthenticationParameters(
|
||||||
|
/* timeout = */ 0,
|
||||||
|
KeyProperties.AUTH_BIOMETRIC_STRONG,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
builder.setUserAuthenticationValidityDurationSeconds(-1)
|
||||||
|
}
|
||||||
|
builder.setInvalidatedByBiometricEnrollment(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StrongBox if available — bumps key storage to a dedicated
|
||||||
|
// tamper-resistant chip on Pixel 3+ / most Samsung flagships.
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||||
|
builder.setIsStrongBoxBacked(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
val gen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEYSTORE)
|
||||||
|
return try {
|
||||||
|
gen.init(builder.build())
|
||||||
|
gen.generateKey()
|
||||||
|
} catch (_: Exception) {
|
||||||
|
// StrongBox not present or full → retry without StrongBox.
|
||||||
|
// Same for older devices that don't honor
|
||||||
|
// setUserAuthenticationParameters.
|
||||||
|
val fallback = KeyGenParameterSpec.Builder(
|
||||||
|
alias,
|
||||||
|
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT,
|
||||||
|
)
|
||||||
|
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
|
||||||
|
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
|
||||||
|
.setKeySize(256)
|
||||||
|
.setRandomizedEncryptionRequired(true)
|
||||||
|
.apply {
|
||||||
|
if (requireBiometric) {
|
||||||
|
setUserAuthenticationRequired(true)
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
setUserAuthenticationValidityDurationSeconds(-1)
|
||||||
|
setInvalidatedByBiometricEnrollment(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.build()
|
||||||
|
gen.init(fallback)
|
||||||
|
gen.generateKey()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val ANDROID_KEYSTORE = "AndroidKeyStore"
|
||||||
|
private const val TRANSFORMATION = "AES/GCM/NoPadding"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,229 @@
|
|||||||
|
package no.zyon.shade.keystore
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import android.util.Base64
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
import no.zyon.shade.crypto.CryptoProvider
|
||||||
|
import no.zyon.shade.serialization.SessionStateJson
|
||||||
|
import no.zyon.shade.storage.StorageProvider
|
||||||
|
import no.zyon.shade.types.IdentityKeyPair
|
||||||
|
import no.zyon.shade.types.OneTimePreKey
|
||||||
|
import no.zyon.shade.types.SessionState
|
||||||
|
import no.zyon.shade.types.SignedPreKey
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `StorageProvider` implementation that gates all reads/writes through
|
||||||
|
* a biometric-locked `KeystoreMasterKey`. Mirrors `MemoryStorage` for
|
||||||
|
* the API surface but persists state to `SharedPreferences` with each
|
||||||
|
* row encrypted under the keystore key.
|
||||||
|
*
|
||||||
|
* Lifecycle:
|
||||||
|
*
|
||||||
|
* 1. App start → construct `KeystoreStorage(context, alias)`.
|
||||||
|
* 2. `unlock(BiometricUnlock)` runs the system biometric prompt.
|
||||||
|
* The Android keystore caches the auth state under the key's
|
||||||
|
* `setUserAuthenticationParameters(0, BIOMETRIC_STRONG)` policy
|
||||||
|
* until the next biometric event (re-enrollment, etc.).
|
||||||
|
* 3. While unlocked, `getSession`/`saveSession` etc. work normally.
|
||||||
|
* 4. `lock()` clears the in-memory unlocked flag so a future
|
||||||
|
* operation triggers another biometric prompt.
|
||||||
|
*
|
||||||
|
* Wire layout per row:
|
||||||
|
* `<base64(nonce(12))>:<base64(ct||tag)>`
|
||||||
|
*
|
||||||
|
* Stored as `String` SharedPreferences entries. AAD = the row's
|
||||||
|
* preference key (`session:<address>`, `signedPreKey:<id>`, etc.) so
|
||||||
|
* a substituted-prefs swap fails to open.
|
||||||
|
*/
|
||||||
|
class KeystoreStorage(
|
||||||
|
context: Context,
|
||||||
|
private val crypto: CryptoProvider,
|
||||||
|
keyAlias: String = DEFAULT_KEY_ALIAS,
|
||||||
|
prefsName: String = DEFAULT_PREFS_NAME,
|
||||||
|
requireBiometric: Boolean = true,
|
||||||
|
) : StorageProvider {
|
||||||
|
|
||||||
|
private val prefs: SharedPreferences =
|
||||||
|
context.applicationContext.getSharedPreferences(prefsName, Context.MODE_PRIVATE)
|
||||||
|
private val masterKey = KeystoreMasterKey(keyAlias, requireBiometric = requireBiometric)
|
||||||
|
private val writeMutex = Mutex()
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var unlocked: Boolean = false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unlock the keystore via biometric prompt. Idempotent — calling
|
||||||
|
* twice without a `lock()` between is a no-op.
|
||||||
|
*/
|
||||||
|
suspend fun unlock(unlock: BiometricUnlock) {
|
||||||
|
if (unlocked) return
|
||||||
|
// The biometric flow returns an authenticated *encrypt*
|
||||||
|
// cipher; we discard it after a one-shot probe to confirm
|
||||||
|
// the master key is reachable. The actual encrypt/decrypt
|
||||||
|
// ciphers in the I/O path use the authentication state
|
||||||
|
// established here (Android Keystore caches the auth for
|
||||||
|
// the user-authentication-required key under the
|
||||||
|
// `setUserAuthenticationParameters(0, BIOMETRIC_STRONG)`
|
||||||
|
// policy until the next biometric event).
|
||||||
|
val probe = masterKey.cipherForEncrypt()
|
||||||
|
unlock.unlock(probe)
|
||||||
|
unlocked = true
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Unlock without biometric — only valid for keys constructed with `requireBiometric=false`. */
|
||||||
|
fun unlockNoBiometric() {
|
||||||
|
unlocked = true
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Wipe in-memory unlock state. The key itself stays in the keystore. */
|
||||||
|
fun lock() {
|
||||||
|
unlocked = false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Catastrophic reset: deletes the master key + all encrypted
|
||||||
|
* preferences. Used by the "forget everything" / 3-strikes-wipe
|
||||||
|
* path. The next bootstrap rebuilds from credentials.
|
||||||
|
*/
|
||||||
|
fun forgetEverything() {
|
||||||
|
masterKey.deleteKey()
|
||||||
|
prefs.edit().clear().apply()
|
||||||
|
unlocked = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Identity ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
override suspend fun getIdentityKeyPair(): IdentityKeyPair? {
|
||||||
|
val json = readDecrypted(KEY_IDENTITY) ?: return null
|
||||||
|
return SessionStateJson.deserializeIdentityKeyPair(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun saveIdentityKeyPair(keyPair: IdentityKeyPair) {
|
||||||
|
writeEncrypted(KEY_IDENTITY, SessionStateJson.serializeIdentityKeyPair(keyPair))
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getLocalRegistrationId(): Int {
|
||||||
|
return readDecrypted(KEY_REGISTRATION_ID)?.toIntOrNull() ?: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun saveLocalRegistrationId(id: Int) {
|
||||||
|
writeEncrypted(KEY_REGISTRATION_ID, id.toString())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Signed prekeys ────────────────────────────────────────
|
||||||
|
|
||||||
|
override suspend fun getSignedPreKey(keyId: Int): SignedPreKey? {
|
||||||
|
val json = readDecrypted("$KEY_SIGNED_PREKEY:$keyId") ?: return null
|
||||||
|
return SessionStateJson.deserializeSignedPreKey(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun saveSignedPreKey(key: SignedPreKey) {
|
||||||
|
writeEncrypted(
|
||||||
|
"$KEY_SIGNED_PREKEY:${key.keyId}",
|
||||||
|
SessionStateJson.serializeSignedPreKey(key),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun removeSignedPreKey(keyId: Int) {
|
||||||
|
writeMutex.withLock { prefs.edit().remove("$KEY_SIGNED_PREKEY:$keyId").apply() }
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── One-time prekeys ──────────────────────────────────────
|
||||||
|
|
||||||
|
override suspend fun getOneTimePreKey(keyId: Int): OneTimePreKey? {
|
||||||
|
val json = readDecrypted("$KEY_ONETIME_PREKEY:$keyId") ?: return null
|
||||||
|
return SessionStateJson.deserializeOneTimePreKey(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun saveOneTimePreKey(key: OneTimePreKey) {
|
||||||
|
writeEncrypted(
|
||||||
|
"$KEY_ONETIME_PREKEY:${key.keyId}",
|
||||||
|
SessionStateJson.serializeOneTimePreKey(key),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun removeOneTimePreKey(keyId: Int) {
|
||||||
|
writeMutex.withLock { prefs.edit().remove("$KEY_ONETIME_PREKEY:$keyId").apply() }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getOneTimePreKeyCount(): Int {
|
||||||
|
return prefs.all.keys.count { it.startsWith("$KEY_ONETIME_PREKEY:") }
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Sessions ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
override suspend fun getSession(address: String): SessionState? {
|
||||||
|
val json = readDecrypted("$KEY_SESSION:$address") ?: return null
|
||||||
|
return SessionStateJson.deserialize(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun saveSession(address: String, state: SessionState) {
|
||||||
|
writeEncrypted("$KEY_SESSION:$address", SessionStateJson.serialize(state))
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun removeSession(address: String) {
|
||||||
|
writeMutex.withLock { prefs.edit().remove("$KEY_SESSION:$address").apply() }
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Trust ─────────────────────────────────────────────────
|
||||||
|
|
||||||
|
override suspend fun isTrustedIdentity(address: String, identityKey: ByteArray): Boolean {
|
||||||
|
val stored = readDecrypted("$KEY_TRUSTED:$address") ?: return true // TOFU
|
||||||
|
val storedBytes = Base64.decode(stored, Base64.NO_WRAP)
|
||||||
|
return crypto.constantTimeEqual(storedBytes, identityKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun saveTrustedIdentity(address: String, identityKey: ByteArray) {
|
||||||
|
writeEncrypted(
|
||||||
|
"$KEY_TRUSTED:$address",
|
||||||
|
Base64.encodeToString(identityKey, Base64.NO_WRAP),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Encrypted-row plumbing ────────────────────────────────
|
||||||
|
|
||||||
|
private fun ensureUnlocked() {
|
||||||
|
check(unlocked) {
|
||||||
|
"KeystoreStorage is locked — call unlock(BiometricUnlock) first"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun readDecrypted(prefKey: String): String? {
|
||||||
|
ensureUnlocked()
|
||||||
|
val raw = prefs.getString(prefKey, null) ?: return null
|
||||||
|
val parts = raw.split(":", limit = 2)
|
||||||
|
require(parts.size == 2) { "malformed encrypted row at $prefKey" }
|
||||||
|
val nonce = Base64.decode(parts[0], Base64.NO_WRAP)
|
||||||
|
val ct = Base64.decode(parts[1], Base64.NO_WRAP)
|
||||||
|
val cipher = masterKey.cipherForDecrypt(nonce)
|
||||||
|
cipher.updateAAD(prefKey.toByteArray(Charsets.UTF_8))
|
||||||
|
return cipher.doFinal(ct).toString(Charsets.UTF_8)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun writeEncrypted(prefKey: String, plaintext: String) {
|
||||||
|
ensureUnlocked()
|
||||||
|
writeMutex.withLock {
|
||||||
|
val cipher = masterKey.cipherForEncrypt()
|
||||||
|
cipher.updateAAD(prefKey.toByteArray(Charsets.UTF_8))
|
||||||
|
val ct = cipher.doFinal(plaintext.toByteArray(Charsets.UTF_8))
|
||||||
|
val nonce = cipher.iv
|
||||||
|
val nonceB64 = Base64.encodeToString(nonce, Base64.NO_WRAP)
|
||||||
|
val ctB64 = Base64.encodeToString(ct, Base64.NO_WRAP)
|
||||||
|
prefs.edit().putString(prefKey, "$nonceB64:$ctB64").apply()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val DEFAULT_KEY_ALIAS = "shade-master-v1"
|
||||||
|
const val DEFAULT_PREFS_NAME = "shade-keystore-storage-v1"
|
||||||
|
|
||||||
|
private const val KEY_IDENTITY = "identity"
|
||||||
|
private const val KEY_REGISTRATION_ID = "registrationId"
|
||||||
|
private const val KEY_SIGNED_PREKEY = "signedPreKey"
|
||||||
|
private const val KEY_ONETIME_PREKEY = "oneTimePreKey"
|
||||||
|
private const val KEY_SESSION = "session"
|
||||||
|
private const val KEY_TRUSTED = "trusted"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,11 @@ Kotlin implementation of the Shade E2EE protocol for Android apps. Byte-for-byte
|
|||||||
**M-Cross 1 ✅** — keys + HKDF + X3DH + fingerprint.
|
**M-Cross 1 ✅** — keys + HKDF + X3DH + fingerprint.
|
||||||
**M-Cross 2 ✅** — full ratchet step (encrypt + decrypt roundtrip) + wire 0x02 (RatchetMessage and PreKeyMessage with/without OTPK).
|
**M-Cross 2 ✅** — full ratchet step (encrypt + decrypt roundtrip) + wire 0x02 (RatchetMessage and PreKeyMessage with/without OTPK).
|
||||||
**M-Cross 3 ✅** — streams 0x11 (KDF labels with embedded NULs, deterministic chunk nonce/AAD, wire 0x11 encode/decode).
|
**M-Cross 3 ✅** — streams 0x11 (KDF labels with embedded NULs, deterministic chunk nonce/AAD, wire 0x11 encode/decode).
|
||||||
**M-Cross 4 ✅** — backup-key HKDF + AES-GCM, group sender-key step (`kdfChainKey` + Ed25519 sign over `aad ‖ ct`), storage HKDF (storageKey/fieldKey/rowNonce). Pending: scrypt master-key, argon2id swap, Android KeystoreStorage (sibling module).
|
**M-Cross 4 ✅** — backup-key HKDF + AES-GCM, group sender-key step (`kdfChainKey` + Ed25519 sign over `aad ‖ ct`), storage HKDF (storageKey/fieldKey/rowNonce).
|
||||||
|
**M-Cross 5 ✅** — V4.9 blob KDF + AEAD (`deriveBlobSlotId / deriveBlobKey / deriveBlobSigningSeed`, AAD-bound seal/open), `BlobClient` HTTP, `Profile` namespace. Cross-platform vectors in `blob.json`.
|
||||||
|
**M-Cross 6 ✅** — V4.10 cross-host approval routing: canonical profile-blob schema (`hosts[]` / `clients[]` / `trustedApproverFingerprints[]`), build/sign/verify proxy approvals via `canonicalApprovalSigningBytes` (length-prefixed u16 BE UTF-8). Cross-platform vectors in `approval.json`, including a TS-signed Ed25519 signature that the Kotlin port verifies.
|
||||||
|
**M-Cross 7 ✅** — scrypt + argon2id password-KDF wrappers (Bouncy Castle), NFKC-normalized inputs.
|
||||||
|
**M-Cross 8 ✅** — `:shade-android-keystore` sibling module: `KeystoreMasterKey` (StrongBox-backed AES-256-GCM, BIOMETRIC_STRONG-gated, invalidated on biometric enrollment), `BiometricUnlock`, `KeystoreStorage` (`StorageProvider` over biometric-gated AES-encrypted SharedPreferences).
|
||||||
|
|
||||||
Cross-platform test vectors in `/test-vectors/` are loaded by both the TS
|
Cross-platform test vectors in `/test-vectors/` are loaded by both the TS
|
||||||
and Kotlin test suites; any byte-divergence fails CI within 60 s. See
|
and Kotlin test suites; any byte-divergence fails CI within 60 s. See
|
||||||
|
|||||||
@@ -29,6 +29,11 @@ dependencies {
|
|||||||
// The same `subtle.*` API as `tink-android` so the source compiles unchanged.
|
// The same `subtle.*` API as `tink-android` so the source compiles unchanged.
|
||||||
implementation("com.google.crypto.tink:tink:1.15.0")
|
implementation("com.google.crypto.tink:tink:1.15.0")
|
||||||
|
|
||||||
|
// Bouncy Castle for scrypt + argon2id. Tink doesn't ship password
|
||||||
|
// KDFs; @shade/storage-encrypted uses @noble/hashes for both. We
|
||||||
|
// pin to the JDK18-on artifact so it works on JVM 17 + Android.
|
||||||
|
implementation("org.bouncycastle:bcprov-jdk18on:1.78.1")
|
||||||
|
|
||||||
// JSON serialization (session state + test-vector loader on JVM).
|
// JSON serialization (session state + test-vector loader on JVM).
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
|
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,273 @@
|
|||||||
|
package no.zyon.shade.approval
|
||||||
|
|
||||||
|
import no.zyon.shade.crypto.CryptoProvider
|
||||||
|
import java.nio.ByteBuffer
|
||||||
|
import java.nio.ByteOrder
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V4.10 — cross-host approval routing helpers. Mirror
|
||||||
|
* `@shade/sdk/approval.ts` byte-for-byte.
|
||||||
|
*
|
||||||
|
* The frames themselves (`approvalNeeded` / `linkApproveByProxy`) are
|
||||||
|
* app-defined payloads sent over the existing Shade bilateral E2EE
|
||||||
|
* channel. This file ships the canonical signing-payload layout, the
|
||||||
|
* Ed25519 sign step a phone runs after biometric unlock, and the
|
||||||
|
* verify step a host runs against the freshest profile blob.
|
||||||
|
*
|
||||||
|
* The signing payload is length-prefixed binary (u16 BE) so any
|
||||||
|
* platform — Kotlin, Swift, Go — can produce byte-identical input
|
||||||
|
* without needing a JSON canonicalizer. Cross-platform parity is
|
||||||
|
* gated by `test-vectors/blob-storage.json` (signing payload
|
||||||
|
* fixtures) plus a Kotlin↔TS round-trip in `CrossPlatformVectorTest`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Default domain separator. Apps with their own canonical name (e.g. Prism) override. */
|
||||||
|
const val DEFAULT_APPROVAL_DOMAIN = "shade-link-approve-v1"
|
||||||
|
|
||||||
|
/** Default expiry: 5 minutes after the host issues the request. */
|
||||||
|
const val DEFAULT_APPROVAL_EXPIRES_IN_MS = 5L * 60 * 1000
|
||||||
|
|
||||||
|
/** Information about the device the host received a `linkRequest` from. */
|
||||||
|
data class ApprovalRequestingDevice(
|
||||||
|
val fingerprint: String,
|
||||||
|
val deviceName: String? = null,
|
||||||
|
val userAgent: String? = null,
|
||||||
|
val ipHint: String? = null,
|
||||||
|
val receivedAt: Long,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class ApprovalRequestFrame(
|
||||||
|
val kind: String = "approvalNeeded",
|
||||||
|
/** 128-bit hex (32 chars) random idempotency key. */
|
||||||
|
val requestId: String,
|
||||||
|
val hostAddress: String,
|
||||||
|
val hostFingerprint: String,
|
||||||
|
val requestingDevice: ApprovalRequestingDevice,
|
||||||
|
val expiresAt: Long,
|
||||||
|
val domain: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class ProxyApprovalFrame(
|
||||||
|
val kind: String = "linkApproveByProxy",
|
||||||
|
val requestId: String,
|
||||||
|
val decision: String,
|
||||||
|
val approverFingerprint: String,
|
||||||
|
/** 64-byte Ed25519 signature, lowercase hex (128 chars). */
|
||||||
|
val signature: String,
|
||||||
|
val domain: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a fresh `approvalNeeded` frame with a 128-bit random
|
||||||
|
* `requestId`. Hosts SHOULD persist the requestId in a pending-set
|
||||||
|
* keyed by `expiresAt` so a returning `linkApproveByProxy` can be
|
||||||
|
* matched up — that's app state, the SDK doesn't track it.
|
||||||
|
*/
|
||||||
|
fun buildApprovalRequest(
|
||||||
|
crypto: CryptoProvider,
|
||||||
|
hostAddress: String,
|
||||||
|
hostFingerprint: String,
|
||||||
|
requestingDeviceFingerprint: String,
|
||||||
|
deviceName: String? = null,
|
||||||
|
userAgent: String? = null,
|
||||||
|
ipHint: String? = null,
|
||||||
|
expiresInMs: Long = DEFAULT_APPROVAL_EXPIRES_IN_MS,
|
||||||
|
domain: String = DEFAULT_APPROVAL_DOMAIN,
|
||||||
|
now: Long = System.currentTimeMillis(),
|
||||||
|
): ApprovalRequestFrame {
|
||||||
|
val requestId = crypto.randomBytes(16).joinToString("") { "%02x".format(it) }
|
||||||
|
return ApprovalRequestFrame(
|
||||||
|
requestId = requestId,
|
||||||
|
hostAddress = hostAddress,
|
||||||
|
hostFingerprint = hostFingerprint,
|
||||||
|
requestingDevice = ApprovalRequestingDevice(
|
||||||
|
fingerprint = requestingDeviceFingerprint,
|
||||||
|
deviceName = deviceName,
|
||||||
|
userAgent = userAgent,
|
||||||
|
ipHint = ipHint,
|
||||||
|
receivedAt = now,
|
||||||
|
),
|
||||||
|
expiresAt = now + expiresInMs,
|
||||||
|
domain = domain,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sign a `linkApproveByProxy` frame with the approver's long-term
|
||||||
|
* Ed25519 identity key. The seed is the 32-byte Ed25519 private key
|
||||||
|
* (Tink's `Ed25519Sign(seed)` consumes it directly).
|
||||||
|
*/
|
||||||
|
fun signProxyApproval(
|
||||||
|
crypto: CryptoProvider,
|
||||||
|
request: ApprovalRequestFrame,
|
||||||
|
decision: String,
|
||||||
|
approverFingerprint: String,
|
||||||
|
approverSigningKey: ByteArray,
|
||||||
|
): ProxyApprovalFrame {
|
||||||
|
require(decision == "approve" || decision == "reject") {
|
||||||
|
"decision must be 'approve' or 'reject'"
|
||||||
|
}
|
||||||
|
require(approverSigningKey.size == 32) {
|
||||||
|
"approverSigningKey must be 32 bytes (Ed25519 seed)"
|
||||||
|
}
|
||||||
|
val payload = canonicalApprovalSigningBytes(
|
||||||
|
domain = request.domain,
|
||||||
|
requestId = request.requestId,
|
||||||
|
hostFingerprint = request.hostFingerprint,
|
||||||
|
requestingDeviceFingerprint = request.requestingDevice.fingerprint,
|
||||||
|
decision = decision,
|
||||||
|
)
|
||||||
|
val sig = crypto.sign(approverSigningKey, payload)
|
||||||
|
return ProxyApprovalFrame(
|
||||||
|
requestId = request.requestId,
|
||||||
|
decision = decision,
|
||||||
|
approverFingerprint = approverFingerprint,
|
||||||
|
signature = sig.joinToString("") { "%02x".format(it) },
|
||||||
|
domain = request.domain,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Tagged result of `verifyProxyApproval`. */
|
||||||
|
sealed class VerifyProxyApprovalResult {
|
||||||
|
data class Ok(val approver: ProfileClientEntry) : VerifyProxyApprovalResult()
|
||||||
|
data class Failed(val reason: Reason) : VerifyProxyApprovalResult()
|
||||||
|
|
||||||
|
enum class Reason {
|
||||||
|
REQUEST_ID_MISMATCH,
|
||||||
|
DOMAIN_MISMATCH,
|
||||||
|
UNKNOWN_APPROVER,
|
||||||
|
NOT_TRUSTED,
|
||||||
|
BAD_SIGNATURE,
|
||||||
|
EXPIRED,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify a `linkApproveByProxy` against the originating
|
||||||
|
* `approvalNeeded` and the host's freshest profile blob. Returns a
|
||||||
|
* tagged result rather than throwing — callers usually want to log
|
||||||
|
* the reason before deciding what to surface to the user.
|
||||||
|
*
|
||||||
|
* Order of checks:
|
||||||
|
*
|
||||||
|
* 1. requestId match (replay defense)
|
||||||
|
* 2. domain match (cross-app confusion defense)
|
||||||
|
* 3. approver resolves to a `clients[]` entry
|
||||||
|
* 4. approver is in `trustedApproverFingerprints[]` AND has the
|
||||||
|
* `trustedApprover` flag (cross-checked via `isTrustedApprover`)
|
||||||
|
* 5. expiresAt in the future
|
||||||
|
* 6. Ed25519 signature verifies against `clients[].identityPublicKey`
|
||||||
|
*
|
||||||
|
* Hosts MUST refetch the profile blob fresh before calling this — see
|
||||||
|
* the FR §5 "approver-revocation propagation" rationale.
|
||||||
|
*/
|
||||||
|
fun verifyProxyApproval(
|
||||||
|
crypto: CryptoProvider,
|
||||||
|
request: ApprovalRequestFrame,
|
||||||
|
approval: ProxyApprovalFrame,
|
||||||
|
profile: CanonicalProfileBlob,
|
||||||
|
now: Long = System.currentTimeMillis(),
|
||||||
|
): VerifyProxyApprovalResult {
|
||||||
|
if (approval.requestId != request.requestId) {
|
||||||
|
return VerifyProxyApprovalResult.Failed(
|
||||||
|
VerifyProxyApprovalResult.Reason.REQUEST_ID_MISMATCH,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (approval.domain != request.domain) {
|
||||||
|
return VerifyProxyApprovalResult.Failed(
|
||||||
|
VerifyProxyApprovalResult.Reason.DOMAIN_MISMATCH,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val approver = findClientByFingerprint(profile, approval.approverFingerprint)
|
||||||
|
?: return VerifyProxyApprovalResult.Failed(
|
||||||
|
VerifyProxyApprovalResult.Reason.UNKNOWN_APPROVER,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!isTrustedApprover(profile, approval.approverFingerprint)) {
|
||||||
|
return VerifyProxyApprovalResult.Failed(
|
||||||
|
VerifyProxyApprovalResult.Reason.NOT_TRUSTED,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (now > request.expiresAt) {
|
||||||
|
return VerifyProxyApprovalResult.Failed(VerifyProxyApprovalResult.Reason.EXPIRED)
|
||||||
|
}
|
||||||
|
|
||||||
|
val pubkey = try {
|
||||||
|
hexToBytes(approver.identityPublicKey)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
return VerifyProxyApprovalResult.Failed(VerifyProxyApprovalResult.Reason.BAD_SIGNATURE)
|
||||||
|
}
|
||||||
|
val sig = try {
|
||||||
|
hexToBytes(approval.signature)
|
||||||
|
} catch (_: Exception) {
|
||||||
|
return VerifyProxyApprovalResult.Failed(VerifyProxyApprovalResult.Reason.BAD_SIGNATURE)
|
||||||
|
}
|
||||||
|
if (pubkey.size != 32 || sig.size != 64) {
|
||||||
|
return VerifyProxyApprovalResult.Failed(VerifyProxyApprovalResult.Reason.BAD_SIGNATURE)
|
||||||
|
}
|
||||||
|
|
||||||
|
val payload = canonicalApprovalSigningBytes(
|
||||||
|
domain = approval.domain,
|
||||||
|
requestId = approval.requestId,
|
||||||
|
hostFingerprint = request.hostFingerprint,
|
||||||
|
requestingDeviceFingerprint = request.requestingDevice.fingerprint,
|
||||||
|
decision = approval.decision,
|
||||||
|
)
|
||||||
|
val ok = crypto.verify(pubkey, payload, sig)
|
||||||
|
return if (ok) VerifyProxyApprovalResult.Ok(approver)
|
||||||
|
else VerifyProxyApprovalResult.Failed(VerifyProxyApprovalResult.Reason.BAD_SIGNATURE)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the canonical signing payload bytes for a proxy approval.
|
||||||
|
*
|
||||||
|
* Format (length-prefixed UTF-8, big-endian u16 lengths):
|
||||||
|
*
|
||||||
|
* u16(len(domain)) || domain
|
||||||
|
* u16(len(requestId)) || requestId
|
||||||
|
* u16(len(hostFp)) || hostFingerprint
|
||||||
|
* u16(len(requestFp)) || requestingDeviceFingerprint
|
||||||
|
* u16(len(decision)) || decision
|
||||||
|
*
|
||||||
|
* This is the EXACT byte layout `@shade/sdk`'s
|
||||||
|
* `canonicalApprovalSigningBytes` produces, ensuring an Android-signed
|
||||||
|
* approval verifies on a TS host and vice versa.
|
||||||
|
*/
|
||||||
|
fun canonicalApprovalSigningBytes(
|
||||||
|
domain: String,
|
||||||
|
requestId: String,
|
||||||
|
hostFingerprint: String,
|
||||||
|
requestingDeviceFingerprint: String,
|
||||||
|
decision: String,
|
||||||
|
): ByteArray {
|
||||||
|
val fields = listOf(
|
||||||
|
domain.toByteArray(Charsets.UTF_8),
|
||||||
|
requestId.toByteArray(Charsets.UTF_8),
|
||||||
|
hostFingerprint.toByteArray(Charsets.UTF_8),
|
||||||
|
requestingDeviceFingerprint.toByteArray(Charsets.UTF_8),
|
||||||
|
decision.toByteArray(Charsets.UTF_8),
|
||||||
|
)
|
||||||
|
for (f in fields) {
|
||||||
|
require(f.size <= 0xFFFF) { "signing field too long: ${f.size} bytes (max 65535)" }
|
||||||
|
}
|
||||||
|
val total = fields.sumOf { 2 + it.size }
|
||||||
|
val buf = ByteBuffer.allocate(total).order(ByteOrder.BIG_ENDIAN)
|
||||||
|
for (f in fields) {
|
||||||
|
buf.putShort(f.size.toShort())
|
||||||
|
buf.put(f)
|
||||||
|
}
|
||||||
|
return buf.array()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hexToBytes(hex: String): ByteArray {
|
||||||
|
require(hex.length % 2 == 0) { "hex length must be even" }
|
||||||
|
require(hex.all { it.isDigit() || it in 'a'..'f' }) { "hex must be lowercase 0-9a-f" }
|
||||||
|
val out = ByteArray(hex.length / 2)
|
||||||
|
for (i in out.indices) {
|
||||||
|
out[i] = ((Character.digit(hex[i * 2], 16) shl 4) +
|
||||||
|
Character.digit(hex[i * 2 + 1], 16)).toByte()
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
@@ -0,0 +1,307 @@
|
|||||||
|
package no.zyon.shade.approval
|
||||||
|
|
||||||
|
import org.json.JSONArray
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V4.10 — canonical profile-blob schema. Mirror
|
||||||
|
* `@shade/sdk/approval.ts` byte-for-byte: same field names, same
|
||||||
|
* JSON shape, same denormalization invariants.
|
||||||
|
*
|
||||||
|
* The blob is the AEAD plaintext stored in the V4.9 profile slot. It
|
||||||
|
* holds the user's list of paired hosts + clients; cross-host
|
||||||
|
* approval routing reads `clients[]` to find trusted approvers when
|
||||||
|
* a headless host needs to dispatch a `linkRequest` to a phone.
|
||||||
|
*
|
||||||
|
* Mutators (`upsertHost`, `setTrustedApprover`, ...) are immutable —
|
||||||
|
* they return a new blob and never modify the input. The denormalized
|
||||||
|
* `trustedApproverFingerprints[]` is rederived on every mutation so it
|
||||||
|
* can never drift from the per-client `trustedApprover` flag.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** A host: a device that receives `linkRequest` frames and runs pairing. */
|
||||||
|
data class ProfileHostEntry(
|
||||||
|
val address: String,
|
||||||
|
val name: String,
|
||||||
|
/** Open enum: `"desktop" | "server" | "laptop" | ...`. */
|
||||||
|
val kind: String,
|
||||||
|
val addedAt: Long,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A client: a device that initiates link/approval flows and may
|
||||||
|
* proxy-approve when `trustedApprover == true`. Stores both the
|
||||||
|
* 32-byte Ed25519 identity public key (hex) and the safety-number
|
||||||
|
* fingerprint — the public key is what `verifyProxyApproval` checks
|
||||||
|
* signatures against; the fingerprint is what UIs display.
|
||||||
|
*/
|
||||||
|
data class ProfileClientEntry(
|
||||||
|
val address: String,
|
||||||
|
/** 32-byte Ed25519 long-term identity public key, lowercase hex (64 chars). */
|
||||||
|
val identityPublicKey: String,
|
||||||
|
/** Safety-number fingerprint of the identity key (computeFingerprint output). */
|
||||||
|
val identityFingerprint: String,
|
||||||
|
val name: String,
|
||||||
|
/** Open enum: `"mobile" | "tablet" | "browser" | ...`. */
|
||||||
|
val kind: String,
|
||||||
|
val addedAt: Long,
|
||||||
|
val trustedApprover: Boolean = false,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Canonical profile blob. `version=1` is the only currently-supported
|
||||||
|
* shape; bump when an incompatible field is added. Unknown top-level
|
||||||
|
* fields are dropped on parse — additive changes need a coordinated
|
||||||
|
* schema bump on both platforms.
|
||||||
|
*/
|
||||||
|
data class CanonicalProfileBlob(
|
||||||
|
val version: Int = 1,
|
||||||
|
val hosts: List<ProfileHostEntry> = emptyList(),
|
||||||
|
val clients: List<ProfileClientEntry> = emptyList(),
|
||||||
|
/** Denormalized list of trusted-approver fingerprints. Rederived on mutate. */
|
||||||
|
val trustedApproverFingerprints: List<String> = emptyList(),
|
||||||
|
val updatedAt: Long = 0,
|
||||||
|
/** Optional hex-encoded pubkey of the writer; informational only. */
|
||||||
|
val signedBy: String? = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
/** Build a fresh empty profile blob with `updatedAt = now ?? System.currentTimeMillis()`. */
|
||||||
|
fun emptyCanonicalProfile(now: Long? = null): CanonicalProfileBlob =
|
||||||
|
CanonicalProfileBlob(updatedAt = now ?: System.currentTimeMillis())
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode a profile-blob plaintext (the AEAD-opened bytes) into the
|
||||||
|
* canonical shape. Throws `IllegalArgumentException` on malformed JSON
|
||||||
|
* or wrong shape.
|
||||||
|
*/
|
||||||
|
fun parseCanonicalProfile(plaintext: ByteArray): CanonicalProfileBlob =
|
||||||
|
parseCanonicalProfile(plaintext.toString(Charsets.UTF_8))
|
||||||
|
|
||||||
|
fun parseCanonicalProfile(plaintext: String): CanonicalProfileBlob {
|
||||||
|
val obj = try {
|
||||||
|
JSONObject(plaintext)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
throw IllegalArgumentException("profile blob is not valid JSON: ${e.message}")
|
||||||
|
}
|
||||||
|
val version = obj.optInt("version", -1)
|
||||||
|
require(version == 1) { "unsupported profile blob version: $version" }
|
||||||
|
|
||||||
|
val hosts = parseArray(obj.optJSONArray("hosts"), "hosts", ::parseHostEntry)
|
||||||
|
val clients = parseArray(obj.optJSONArray("clients"), "clients", ::parseClientEntry)
|
||||||
|
val trustedApproverFingerprints = parseStringArray(
|
||||||
|
obj.optJSONArray("trustedApproverFingerprints"),
|
||||||
|
"trustedApproverFingerprints",
|
||||||
|
)
|
||||||
|
val updatedAt = if (obj.has("updatedAt") && !obj.isNull("updatedAt"))
|
||||||
|
obj.getLong("updatedAt") else 0L
|
||||||
|
val signedBy = obj.optString("signedBy", "").takeIf { it.isNotEmpty() }
|
||||||
|
|
||||||
|
return CanonicalProfileBlob(
|
||||||
|
version = 1,
|
||||||
|
hosts = hosts,
|
||||||
|
clients = clients,
|
||||||
|
trustedApproverFingerprints = trustedApproverFingerprints,
|
||||||
|
updatedAt = updatedAt,
|
||||||
|
signedBy = signedBy,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Serialize a profile blob to UTF-8 JSON ready for `Profile.put`. */
|
||||||
|
fun serializeCanonicalProfile(blob: CanonicalProfileBlob): ByteArray {
|
||||||
|
val json = JSONObject()
|
||||||
|
json.put("version", blob.version)
|
||||||
|
json.put("hosts", JSONArray().apply {
|
||||||
|
blob.hosts.forEach { put(hostEntryToJson(it)) }
|
||||||
|
})
|
||||||
|
json.put("clients", JSONArray().apply {
|
||||||
|
blob.clients.forEach { put(clientEntryToJson(it)) }
|
||||||
|
})
|
||||||
|
json.put("trustedApproverFingerprints", JSONArray(blob.trustedApproverFingerprints))
|
||||||
|
json.put("updatedAt", blob.updatedAt)
|
||||||
|
if (blob.signedBy != null) json.put("signedBy", blob.signedBy)
|
||||||
|
return json.toString().toByteArray(Charsets.UTF_8)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseHostEntry(o: JSONObject): ProfileHostEntry =
|
||||||
|
ProfileHostEntry(
|
||||||
|
address = o.requireString("address", "hosts"),
|
||||||
|
name = o.requireString("name", "hosts"),
|
||||||
|
kind = o.requireString("kind", "hosts"),
|
||||||
|
addedAt = o.requireLong("addedAt", "hosts"),
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun parseClientEntry(o: JSONObject): ProfileClientEntry {
|
||||||
|
val identityPublicKey = o.requireString("identityPublicKey", "clients")
|
||||||
|
require(identityPublicKey.matches(Regex("^[0-9a-f]{64}$"))) {
|
||||||
|
"clients[].identityPublicKey must be 64 lowercase hex chars"
|
||||||
|
}
|
||||||
|
return ProfileClientEntry(
|
||||||
|
address = o.requireString("address", "clients"),
|
||||||
|
identityPublicKey = identityPublicKey,
|
||||||
|
identityFingerprint = o.requireString("identityFingerprint", "clients"),
|
||||||
|
name = o.requireString("name", "clients"),
|
||||||
|
kind = o.requireString("kind", "clients"),
|
||||||
|
addedAt = o.requireLong("addedAt", "clients"),
|
||||||
|
trustedApprover = o.optBoolean("trustedApprover", false),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun hostEntryToJson(e: ProfileHostEntry): JSONObject = JSONObject().apply {
|
||||||
|
put("address", e.address)
|
||||||
|
put("name", e.name)
|
||||||
|
put("kind", e.kind)
|
||||||
|
put("addedAt", e.addedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun clientEntryToJson(e: ProfileClientEntry): JSONObject = JSONObject().apply {
|
||||||
|
put("address", e.address)
|
||||||
|
put("identityPublicKey", e.identityPublicKey)
|
||||||
|
put("identityFingerprint", e.identityFingerprint)
|
||||||
|
put("name", e.name)
|
||||||
|
put("kind", e.kind)
|
||||||
|
put("addedAt", e.addedAt)
|
||||||
|
if (e.trustedApprover) put("trustedApprover", true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T> parseArray(
|
||||||
|
arr: JSONArray?,
|
||||||
|
field: String,
|
||||||
|
parse: (JSONObject) -> T,
|
||||||
|
): List<T> {
|
||||||
|
if (arr == null) return emptyList()
|
||||||
|
return (0 until arr.length()).map { i ->
|
||||||
|
val item = arr.opt(i)
|
||||||
|
require(item is JSONObject) { "$field[$i] must be an object" }
|
||||||
|
parse(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseStringArray(arr: JSONArray?, field: String): List<String> {
|
||||||
|
if (arr == null) return emptyList()
|
||||||
|
return (0 until arr.length()).map { i ->
|
||||||
|
val item = arr.opt(i)
|
||||||
|
require(item is String) { "$field[$i] must be a string" }
|
||||||
|
item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun JSONObject.requireString(key: String, ctx: String): String {
|
||||||
|
require(has(key) && !isNull(key)) { "$ctx[].$key is required" }
|
||||||
|
val v = get(key)
|
||||||
|
require(v is String) { "$ctx[].$key must be a string" }
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun JSONObject.requireLong(key: String, ctx: String): Long {
|
||||||
|
require(has(key) && !isNull(key)) { "$ctx[].$key is required" }
|
||||||
|
return when (val v = get(key)) {
|
||||||
|
is Number -> v.toLong()
|
||||||
|
else -> throw IllegalArgumentException("$ctx[].$key must be a number")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Mutators (immutable; return new blob, never mutate input) ──
|
||||||
|
|
||||||
|
/** Insert or replace a host entry by address. Bumps `updatedAt`. */
|
||||||
|
fun upsertHost(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
host: ProfileHostEntry,
|
||||||
|
now: Long? = null,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
val hosts = blob.hosts.filter { it.address != host.address } + host
|
||||||
|
return blob.copy(hosts = hosts, updatedAt = now ?: System.currentTimeMillis())
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove the host with the given address, if any. */
|
||||||
|
fun removeHost(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
address: String,
|
||||||
|
now: Long? = null,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
val hosts = blob.hosts.filter { it.address != address }
|
||||||
|
if (hosts.size == blob.hosts.size) return blob
|
||||||
|
return blob.copy(hosts = hosts, updatedAt = now ?: System.currentTimeMillis())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert or replace a client entry by `identityFingerprint`. Re-derives
|
||||||
|
* `trustedApproverFingerprints` from the resulting `clients[]` so the
|
||||||
|
* denormalized list never drifts.
|
||||||
|
*/
|
||||||
|
fun upsertClient(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
client: ProfileClientEntry,
|
||||||
|
now: Long? = null,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
val clients = blob.clients
|
||||||
|
.filter { it.identityFingerprint != client.identityFingerprint } + client
|
||||||
|
return blob.copy(
|
||||||
|
clients = clients,
|
||||||
|
trustedApproverFingerprints = deriveTrustedApprovers(clients),
|
||||||
|
updatedAt = now ?: System.currentTimeMillis(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove the client with the given identityFingerprint, if any. */
|
||||||
|
fun removeClient(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
identityFingerprint: String,
|
||||||
|
now: Long? = null,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
val clients = blob.clients.filter { it.identityFingerprint != identityFingerprint }
|
||||||
|
if (clients.size == blob.clients.size) return blob
|
||||||
|
return blob.copy(
|
||||||
|
clients = clients,
|
||||||
|
trustedApproverFingerprints = deriveTrustedApprovers(clients),
|
||||||
|
updatedAt = now ?: System.currentTimeMillis(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle the `trustedApprover` flag on a client by fingerprint.
|
||||||
|
* Returns the input unchanged if fingerprint isn't found OR the
|
||||||
|
* desired state already matches (no spurious updatedAt bump).
|
||||||
|
*/
|
||||||
|
fun setTrustedApprover(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
identityFingerprint: String,
|
||||||
|
trusted: Boolean,
|
||||||
|
now: Long? = null,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
var touched = false
|
||||||
|
val clients = blob.clients.map { c ->
|
||||||
|
if (c.identityFingerprint != identityFingerprint) c
|
||||||
|
else if (c.trustedApprover == trusted) c
|
||||||
|
else {
|
||||||
|
touched = true
|
||||||
|
c.copy(trustedApprover = trusted)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!touched) return blob
|
||||||
|
return blob.copy(
|
||||||
|
clients = clients,
|
||||||
|
trustedApproverFingerprints = deriveTrustedApprovers(clients),
|
||||||
|
updatedAt = now ?: System.currentTimeMillis(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True iff the given fingerprint resolves to a client with both
|
||||||
|
* `trustedApprover == true` AND an entry in `trustedApproverFingerprints[]`.
|
||||||
|
*/
|
||||||
|
fun isTrustedApprover(blob: CanonicalProfileBlob, identityFingerprint: String): Boolean {
|
||||||
|
if (!blob.trustedApproverFingerprints.contains(identityFingerprint)) return false
|
||||||
|
val c = findClientByFingerprint(blob, identityFingerprint) ?: return false
|
||||||
|
return c.trustedApprover
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findClientByFingerprint(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
identityFingerprint: String,
|
||||||
|
): ProfileClientEntry? = blob.clients.firstOrNull { it.identityFingerprint == identityFingerprint }
|
||||||
|
|
||||||
|
fun findClientByAddress(blob: CanonicalProfileBlob, address: String): ProfileClientEntry? =
|
||||||
|
blob.clients.firstOrNull { it.address == address }
|
||||||
|
|
||||||
|
private fun deriveTrustedApprovers(clients: List<ProfileClientEntry>): List<String> =
|
||||||
|
clients.filter { it.trustedApprover }.map { it.identityFingerprint }
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package no.zyon.shade.blob
|
||||||
|
|
||||||
|
import javax.crypto.Cipher
|
||||||
|
import javax.crypto.spec.GCMParameterSpec
|
||||||
|
import javax.crypto.spec.SecretKeySpec
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AEAD wrapper for the V4.9 profile blob.
|
||||||
|
*
|
||||||
|
* Wire format for one ciphertext blob:
|
||||||
|
* `nonce(12) || ciphertext(N) || tag(16)`
|
||||||
|
*
|
||||||
|
* Mirror `@shade/storage-encrypted/crypto/aead.ts` byte-for-byte. The
|
||||||
|
* relay stores this as a single opaque BLOB column; AAD is reconstructed
|
||||||
|
* at read-time as `"shade-profile-aad-v1:" + slotIdHex` and is NOT
|
||||||
|
* stored on the relay.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const val BLOB_AEAD_NONCE_LEN = 12
|
||||||
|
const val BLOB_AEAD_TAG_LEN = 16
|
||||||
|
|
||||||
|
private const val MIN_CIPHERTEXT_LEN = BLOB_AEAD_NONCE_LEN + BLOB_AEAD_TAG_LEN
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seal a plaintext blob. Returns `nonce || ct||tag` ready for direct
|
||||||
|
* blob storage. The caller supplies the nonce so this function is
|
||||||
|
* deterministic — the high-level Profile namespace generates a fresh
|
||||||
|
* 12-byte random nonce per write to keep (key, nonce, plaintext)
|
||||||
|
* unique across re-uploads.
|
||||||
|
*/
|
||||||
|
fun aeadSeal(
|
||||||
|
key: ByteArray,
|
||||||
|
nonce: ByteArray,
|
||||||
|
plaintext: ByteArray,
|
||||||
|
aad: ByteArray,
|
||||||
|
): ByteArray {
|
||||||
|
require(key.size == 32) { "AES-256-GCM key must be 32 bytes" }
|
||||||
|
require(nonce.size == BLOB_AEAD_NONCE_LEN) {
|
||||||
|
"nonce must be $BLOB_AEAD_NONCE_LEN bytes"
|
||||||
|
}
|
||||||
|
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
|
||||||
|
val spec = GCMParameterSpec(128, nonce)
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, "AES"), spec)
|
||||||
|
cipher.updateAAD(aad)
|
||||||
|
val ctTag = cipher.doFinal(plaintext)
|
||||||
|
val out = ByteArray(BLOB_AEAD_NONCE_LEN + ctTag.size)
|
||||||
|
System.arraycopy(nonce, 0, out, 0, BLOB_AEAD_NONCE_LEN)
|
||||||
|
System.arraycopy(ctTag, 0, out, BLOB_AEAD_NONCE_LEN, ctTag.size)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open a `nonce || ct||tag` blob and return the plaintext. Throws on
|
||||||
|
* tamper (AEAD tag mismatch) or short input. The caller may pass an
|
||||||
|
* `expectedNonce` to enforce a deterministic nonce — mismatch throws
|
||||||
|
* before the AEAD even runs (defense-in-depth against a relay returning
|
||||||
|
* the wrong slot's blob).
|
||||||
|
*/
|
||||||
|
fun aeadOpen(
|
||||||
|
key: ByteArray,
|
||||||
|
blob: ByteArray,
|
||||||
|
aad: ByteArray,
|
||||||
|
expectedNonce: ByteArray? = null,
|
||||||
|
): ByteArray {
|
||||||
|
require(key.size == 32) { "AES-256-GCM key must be 32 bytes" }
|
||||||
|
require(blob.size >= MIN_CIPHERTEXT_LEN) { "ciphertext blob too short" }
|
||||||
|
val nonce = blob.copyOfRange(0, BLOB_AEAD_NONCE_LEN)
|
||||||
|
if (expectedNonce != null && !ctEqual(nonce, expectedNonce)) {
|
||||||
|
throw IllegalArgumentException(
|
||||||
|
"nonce mismatch — ciphertext blob has been tampered or row identity changed",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val ctTag = blob.copyOfRange(BLOB_AEAD_NONCE_LEN, blob.size)
|
||||||
|
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
|
||||||
|
val spec = GCMParameterSpec(128, nonce)
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(key, "AES"), spec)
|
||||||
|
cipher.updateAAD(aad)
|
||||||
|
return cipher.doFinal(ctTag)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun ctEqual(a: ByteArray, b: ByteArray): Boolean {
|
||||||
|
if (a.size != b.size) return false
|
||||||
|
var diff = 0
|
||||||
|
for (i in a.indices) {
|
||||||
|
diff = diff or (a[i].toInt() xor b[i].toInt())
|
||||||
|
}
|
||||||
|
return diff == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Build the AAD for a given slotId hex string. */
|
||||||
|
fun blobAadForSlot(slotIdHex: String): ByteArray {
|
||||||
|
require(slotIdHex.length == 64) { "slotIdHex must be 64 hex chars" }
|
||||||
|
return "shade-profile-aad-v1:$slotIdHex".toByteArray(Charsets.UTF_8)
|
||||||
|
}
|
||||||
@@ -0,0 +1,250 @@
|
|||||||
|
package no.zyon.shade.blob
|
||||||
|
|
||||||
|
import no.zyon.shade.crypto.CryptoProvider
|
||||||
|
import org.json.JSONObject
|
||||||
|
import java.net.URI
|
||||||
|
import java.net.http.HttpClient
|
||||||
|
import java.net.http.HttpRequest
|
||||||
|
import java.net.http.HttpResponse
|
||||||
|
import java.time.Duration
|
||||||
|
import java.util.Base64
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Low-level HTTP client for the V4.9 encrypted-blob primitive
|
||||||
|
* (`/v1/blob/<slotId>`). Mirror `@shade/inbox`'s `BlobClient` —
|
||||||
|
* stateless, reusable, and protocol-compatible with the TypeScript
|
||||||
|
* relay endpoints.
|
||||||
|
*
|
||||||
|
* The client doesn't care what the blob bytes mean — it just
|
||||||
|
* transports them. Higher-level wrappers (e.g. `Profile`) compose
|
||||||
|
* this client with AEAD-sealing of the actual payload.
|
||||||
|
*
|
||||||
|
* Auth model: every PUT/DELETE carries a detached Ed25519 signature
|
||||||
|
* (base64) over a canonical-JSON form of the request body. The
|
||||||
|
* canonicalization is deterministic — sorted keys, compact JSON, no
|
||||||
|
* trailing whitespace — so signatures generated on Kotlin verify on
|
||||||
|
* the TS server.
|
||||||
|
*/
|
||||||
|
class BlobClient(
|
||||||
|
private val baseUrl: String,
|
||||||
|
private val crypto: CryptoProvider,
|
||||||
|
private val httpClient: HttpClient = HttpClient.newBuilder()
|
||||||
|
.connectTimeout(Duration.ofSeconds(15))
|
||||||
|
.build(),
|
||||||
|
) {
|
||||||
|
|
||||||
|
data class GetResult(
|
||||||
|
val blob: ByteArray,
|
||||||
|
val etag: String,
|
||||||
|
val updatedAt: Long,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class PutResult(
|
||||||
|
val created: Boolean,
|
||||||
|
val etag: String,
|
||||||
|
val updatedAt: Long,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a slot. Returns null if no blob has ever been written there
|
||||||
|
* (or if it was DELETE'd). GET is unauthenticated by design — the
|
||||||
|
* slotId is itself a 256-bit secret derived from the master key.
|
||||||
|
*/
|
||||||
|
fun get(slotIdHex: String): GetResult? {
|
||||||
|
validateSlotIdHex(slotIdHex)
|
||||||
|
val req = HttpRequest.newBuilder(URI.create(joinUrl(baseUrl, "/v1/blob/$slotIdHex")))
|
||||||
|
.GET()
|
||||||
|
.build()
|
||||||
|
val res = httpClient.send(req, HttpResponse.BodyHandlers.ofString())
|
||||||
|
if (res.statusCode() == 404) return null
|
||||||
|
val json = parseJson(res, "GET")
|
||||||
|
val blob = Base64.getDecoder().decode(json.getString("blob"))
|
||||||
|
return GetResult(
|
||||||
|
blob = blob,
|
||||||
|
etag = json.getString("etag"),
|
||||||
|
updatedAt = json.getLong("updatedAt"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create or update a slot.
|
||||||
|
*
|
||||||
|
* `ifMatch` semantics:
|
||||||
|
* - `null`: create-only. Slot must be empty (else 409).
|
||||||
|
* - `<etag-string>`: compare-and-swap. Must match (else 412).
|
||||||
|
* - `"*"`: unconditional overwrite. Slot must already exist (else 412).
|
||||||
|
*/
|
||||||
|
fun put(
|
||||||
|
slotIdHex: String,
|
||||||
|
blob: ByteArray,
|
||||||
|
signingSeed: ByteArray,
|
||||||
|
ownerPubkey: ByteArray,
|
||||||
|
ifMatch: String? = null,
|
||||||
|
): PutResult {
|
||||||
|
validateSlotIdHex(slotIdHex)
|
||||||
|
require(blob.isNotEmpty()) { "Empty blob" }
|
||||||
|
require(ownerPubkey.size == 32) { "ownerPubkey must be 32 bytes (Ed25519)" }
|
||||||
|
require(signingSeed.size == 32) { "signingSeed must be 32 bytes (Ed25519)" }
|
||||||
|
|
||||||
|
// Canonical form for signing: sorted keys, slotId included,
|
||||||
|
// signature field absent. The wire body strips slotId (it's
|
||||||
|
// in the URL) but the signature is computed over the
|
||||||
|
// slotId-bearing form.
|
||||||
|
val signedAt = System.currentTimeMillis()
|
||||||
|
val canonical = sortedMapOf<String, Any>().apply {
|
||||||
|
put("blob", Base64.getEncoder().encodeToString(blob))
|
||||||
|
if (ifMatch != null) put("ifMatch", ifMatch)
|
||||||
|
put("ownerPubkey", Base64.getEncoder().encodeToString(ownerPubkey))
|
||||||
|
put("signedAt", signedAt)
|
||||||
|
put("slotId", slotIdHex)
|
||||||
|
}
|
||||||
|
val canonicalBytes = canonicalJson(canonical).toByteArray(Charsets.UTF_8)
|
||||||
|
val sig = crypto.sign(signingSeed, canonicalBytes)
|
||||||
|
|
||||||
|
// Wire body: same as canonical minus slotId, plus signature.
|
||||||
|
val wire = JSONObject()
|
||||||
|
wire.put("blob", Base64.getEncoder().encodeToString(blob))
|
||||||
|
if (ifMatch != null) wire.put("ifMatch", ifMatch)
|
||||||
|
wire.put("ownerPubkey", Base64.getEncoder().encodeToString(ownerPubkey))
|
||||||
|
wire.put("signedAt", signedAt)
|
||||||
|
wire.put("signature", Base64.getEncoder().encodeToString(sig))
|
||||||
|
|
||||||
|
val req = HttpRequest.newBuilder(URI.create(joinUrl(baseUrl, "/v1/blob/$slotIdHex")))
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.PUT(HttpRequest.BodyPublishers.ofString(wire.toString()))
|
||||||
|
.build()
|
||||||
|
val res = httpClient.send(req, HttpResponse.BodyHandlers.ofString())
|
||||||
|
val json = parseJson(res, "PUT")
|
||||||
|
return PutResult(
|
||||||
|
created = json.optBoolean("created"),
|
||||||
|
etag = json.getString("etag"),
|
||||||
|
updatedAt = json.getLong("updatedAt"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a slot. The next PUT TOFU-claims it again, possibly under
|
||||||
|
* a fresh signing key (e.g. after rotation). Used by "forget
|
||||||
|
* everything" flows.
|
||||||
|
*/
|
||||||
|
fun delete(slotIdHex: String, signingSeed: ByteArray): Boolean {
|
||||||
|
validateSlotIdHex(slotIdHex)
|
||||||
|
require(signingSeed.size == 32) { "signingSeed must be 32 bytes (Ed25519)" }
|
||||||
|
|
||||||
|
val signedAt = System.currentTimeMillis()
|
||||||
|
val canonical = sortedMapOf<String, Any>().apply {
|
||||||
|
put("signedAt", signedAt)
|
||||||
|
put("slotId", slotIdHex)
|
||||||
|
}
|
||||||
|
val canonicalBytes = canonicalJson(canonical).toByteArray(Charsets.UTF_8)
|
||||||
|
val sig = crypto.sign(signingSeed, canonicalBytes)
|
||||||
|
|
||||||
|
val wire = JSONObject()
|
||||||
|
wire.put("signedAt", signedAt)
|
||||||
|
wire.put("signature", Base64.getEncoder().encodeToString(sig))
|
||||||
|
|
||||||
|
val req = HttpRequest.newBuilder(URI.create(joinUrl(baseUrl, "/v1/blob/$slotIdHex")))
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.method("DELETE", HttpRequest.BodyPublishers.ofString(wire.toString()))
|
||||||
|
.build()
|
||||||
|
val res = httpClient.send(req, HttpResponse.BodyHandlers.ofString())
|
||||||
|
val json = parseJson(res, "DELETE")
|
||||||
|
return json.optBoolean("ok", false)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseJson(res: HttpResponse<String>, op: String): JSONObject {
|
||||||
|
val text = res.body() ?: ""
|
||||||
|
val json = if (text.isEmpty()) JSONObject() else try {
|
||||||
|
JSONObject(text)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
throw BlobClientException(
|
||||||
|
code = "SHADE_NETWORK",
|
||||||
|
statusCode = res.statusCode(),
|
||||||
|
message = "Blob $op response not JSON: ${text.take(200)}",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (res.statusCode() !in 200..299) {
|
||||||
|
throw BlobClientException(
|
||||||
|
code = json.optString("code", "SHADE_NETWORK"),
|
||||||
|
statusCode = res.statusCode(),
|
||||||
|
message = json.optString("message", text),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return json
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun validateSlotIdHex(s: String) {
|
||||||
|
require(s.matches(Regex("^[0-9a-f]{64}$"))) {
|
||||||
|
"slotIdHex must be 64 lowercase hex chars (32 bytes)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun joinUrl(base: String, path: String): String =
|
||||||
|
when {
|
||||||
|
base.endsWith("/") && path.startsWith("/") -> base + path.substring(1)
|
||||||
|
!base.endsWith("/") && !path.startsWith("/") -> "$base/$path"
|
||||||
|
else -> base + path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mirror of TS `signPayload`'s canonicalization: sorted keys, compact
|
||||||
|
* JSON, signature field absent. Only handles the subset of types we
|
||||||
|
* need (strings + longs + base64 strings) — keeping the implementation
|
||||||
|
* narrow so it can't accidentally diverge from `JSON.stringify` on
|
||||||
|
* structurally-different inputs.
|
||||||
|
*/
|
||||||
|
internal fun canonicalJson(map: Map<String, Any>): String {
|
||||||
|
val sb = StringBuilder()
|
||||||
|
sb.append('{')
|
||||||
|
var first = true
|
||||||
|
for ((key, value) in map.toSortedMap()) {
|
||||||
|
if (!first) sb.append(',')
|
||||||
|
first = false
|
||||||
|
appendJsonString(sb, key)
|
||||||
|
sb.append(':')
|
||||||
|
appendJsonValue(sb, value)
|
||||||
|
}
|
||||||
|
sb.append('}')
|
||||||
|
return sb.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun appendJsonValue(sb: StringBuilder, value: Any) {
|
||||||
|
when (value) {
|
||||||
|
is String -> appendJsonString(sb, value)
|
||||||
|
is Long, is Int -> sb.append(value.toString())
|
||||||
|
is Boolean -> sb.append(value.toString())
|
||||||
|
else -> throw IllegalArgumentException(
|
||||||
|
"canonicalJson: unsupported value type ${value::class.java}",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun appendJsonString(sb: StringBuilder, s: String) {
|
||||||
|
sb.append('"')
|
||||||
|
for (c in s) {
|
||||||
|
when (c) {
|
||||||
|
'\\' -> sb.append("\\\\")
|
||||||
|
'"' -> sb.append("\\\"")
|
||||||
|
'\b' -> sb.append("\\b")
|
||||||
|
'\u000C' -> sb.append("\\f")
|
||||||
|
'\n' -> sb.append("\\n")
|
||||||
|
'\r' -> sb.append("\\r")
|
||||||
|
'\t' -> sb.append("\\t")
|
||||||
|
else -> {
|
||||||
|
if (c.code < 0x20) {
|
||||||
|
sb.append("\\u").append("%04x".format(c.code))
|
||||||
|
} else {
|
||||||
|
sb.append(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.append('"')
|
||||||
|
}
|
||||||
|
|
||||||
|
class BlobClientException(
|
||||||
|
val code: String,
|
||||||
|
val statusCode: Int,
|
||||||
|
message: String,
|
||||||
|
) : RuntimeException("[$code @ $statusCode] $message")
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package no.zyon.shade.blob
|
||||||
|
|
||||||
|
import com.google.crypto.tink.subtle.Ed25519Sign
|
||||||
|
import no.zyon.shade.crypto.CryptoProvider
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V4.9 — relay-side encrypted blob primitive: deterministic
|
||||||
|
* derivations from a 32-byte master key + per-app namespace string.
|
||||||
|
*
|
||||||
|
* Mirror `@shade/storage-encrypted/crypto/kdf.ts` byte-for-byte.
|
||||||
|
* Reference vectors: `test-vectors/blob-storage.json`.
|
||||||
|
*
|
||||||
|
* Three independent 32-byte derivations:
|
||||||
|
*
|
||||||
|
* slotId = HKDF(masterKey, info=`shade-blob-slot-v1:<app>`) // relay-visible opaque ID
|
||||||
|
* blobKey = HKDF(masterKey, info=`shade-blob-key-v1:<app>`) // AEAD key for the blob
|
||||||
|
* sigSeed = HKDF(masterKey, info=`shade-blob-sig-v1:<app>`) // Ed25519 owner signing seed
|
||||||
|
*
|
||||||
|
* `app` is a caller-supplied namespace string — distinct apps under
|
||||||
|
* the same master MUST pass different values (e.g. `prism-profile-v1`)
|
||||||
|
* so they don't collide on the same slot.
|
||||||
|
*
|
||||||
|
* The signing seed is an Ed25519 *seed* in the @noble/curves convention:
|
||||||
|
* `pubkey = Ed25519.publicFromSeed(seed)` is what the relay TOFU-stores
|
||||||
|
* on the first PUT and verifies subsequent writes against.
|
||||||
|
*/
|
||||||
|
|
||||||
|
private const val SLOT_INFO_PREFIX = "shade-blob-slot-v1:"
|
||||||
|
private const val BLOB_KEY_INFO_PREFIX = "shade-blob-key-v1:"
|
||||||
|
private const val SIG_SEED_INFO_PREFIX = "shade-blob-sig-v1:"
|
||||||
|
|
||||||
|
private const val DERIVED_LEN = 32
|
||||||
|
|
||||||
|
/** Lower-hex 64-char slotId derived deterministically from the master key. */
|
||||||
|
fun deriveBlobSlotId(crypto: CryptoProvider, masterKey: ByteArray, app: String): ByteArray {
|
||||||
|
require(masterKey.size == 32) { "masterKey must be 32 bytes" }
|
||||||
|
require(app.isNotEmpty()) { "app namespace must be non-empty" }
|
||||||
|
return crypto.hkdf(
|
||||||
|
masterKey,
|
||||||
|
ByteArray(0),
|
||||||
|
(SLOT_INFO_PREFIX + app).toByteArray(Charsets.UTF_8),
|
||||||
|
DERIVED_LEN,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** AEAD key for sealing/opening the blob. The slotId hex is bound as AAD. */
|
||||||
|
fun deriveBlobKey(crypto: CryptoProvider, masterKey: ByteArray, app: String): ByteArray {
|
||||||
|
require(masterKey.size == 32) { "masterKey must be 32 bytes" }
|
||||||
|
require(app.isNotEmpty()) { "app namespace must be non-empty" }
|
||||||
|
return crypto.hkdf(
|
||||||
|
masterKey,
|
||||||
|
ByteArray(0),
|
||||||
|
(BLOB_KEY_INFO_PREFIX + app).toByteArray(Charsets.UTF_8),
|
||||||
|
DERIVED_LEN,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 32-byte Ed25519 signing seed. The pubkey, derived deterministically
|
||||||
|
* from the seed, is what the relay TOFU-stores on the first PUT.
|
||||||
|
*/
|
||||||
|
fun deriveBlobSigningSeed(crypto: CryptoProvider, masterKey: ByteArray, app: String): ByteArray {
|
||||||
|
require(masterKey.size == 32) { "masterKey must be 32 bytes" }
|
||||||
|
require(app.isNotEmpty()) { "app namespace must be non-empty" }
|
||||||
|
return crypto.hkdf(
|
||||||
|
masterKey,
|
||||||
|
ByteArray(0),
|
||||||
|
(SIG_SEED_INFO_PREFIX + app).toByteArray(Charsets.UTF_8),
|
||||||
|
DERIVED_LEN,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recover the Ed25519 public key from a 32-byte seed. Mirrors
|
||||||
|
* `@shade/crypto-web`'s `ed25519PublicKeyFromSeed`. Tink's
|
||||||
|
* `Ed25519Sign.KeyPair.newKeyPairFromSeed(seed)` exposes both halves;
|
||||||
|
* we discard the private half here.
|
||||||
|
*/
|
||||||
|
fun ed25519PublicKeyFromSeed(seed: ByteArray): ByteArray {
|
||||||
|
require(seed.size == 32) { "Ed25519 seed must be 32 bytes" }
|
||||||
|
return Ed25519Sign.KeyPair.newKeyPairFromSeed(seed).publicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Convert a 32-byte slotId into the lowercase-hex wire form (64 chars). */
|
||||||
|
fun slotIdToHex(slotId: ByteArray): String {
|
||||||
|
require(slotId.size == 32) { "slotId must be 32 bytes" }
|
||||||
|
return slotId.joinToString("") { "%02x".format(it) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
package no.zyon.shade.blob
|
||||||
|
|
||||||
|
import no.zyon.shade.crypto.CryptoProvider
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V4.9 — high-level profile namespace. Mirror
|
||||||
|
* `@shade/sdk`'s `createProfileNamespace`. The relay never sees
|
||||||
|
* plaintext; AAD binds the slotId so a relay returning the wrong
|
||||||
|
* slot's blob fails to open.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
*
|
||||||
|
* val crypto = TinkProvider()
|
||||||
|
* val masterKey = deriveMasterKey("password", salt) // V4.5 KDF chain
|
||||||
|
* val profile = createProfileNamespace(
|
||||||
|
* baseUrl = "https://shade.example/",
|
||||||
|
* crypto = crypto,
|
||||||
|
* masterKey = masterKey,
|
||||||
|
* app = "prism-profile-v1",
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* val current = profile.get() // null if no blob yet
|
||||||
|
* profile.put(serializeCanonicalProfile(...), ifMatch = current?.etag)
|
||||||
|
* profile.delete()
|
||||||
|
*
|
||||||
|
* Apps with the same master key + app namespace converge on the same
|
||||||
|
* slot — that's the whole point: a brand new device with the right
|
||||||
|
* credentials can locate, decrypt, and update the blob.
|
||||||
|
*/
|
||||||
|
class ProfileNamespace internal constructor(
|
||||||
|
/** Lower-hex 64-char slotId. Stable per (master, app). */
|
||||||
|
val slotIdHex: String,
|
||||||
|
private val blobKey: ByteArray,
|
||||||
|
private val signingSeed: ByteArray,
|
||||||
|
private val ownerPubkey: ByteArray,
|
||||||
|
private val aad: ByteArray,
|
||||||
|
private val client: BlobClient,
|
||||||
|
private val crypto: CryptoProvider,
|
||||||
|
) {
|
||||||
|
|
||||||
|
data class GetResult(
|
||||||
|
val plaintext: ByteArray,
|
||||||
|
val etag: String,
|
||||||
|
val updatedAt: Long,
|
||||||
|
)
|
||||||
|
|
||||||
|
data class PutResult(
|
||||||
|
val created: Boolean,
|
||||||
|
val etag: String,
|
||||||
|
val updatedAt: Long,
|
||||||
|
)
|
||||||
|
|
||||||
|
/** Returns null when the slot has never been written (or was deleted). */
|
||||||
|
fun get(): GetResult? {
|
||||||
|
val raw = client.get(slotIdHex) ?: return null
|
||||||
|
val plaintext = aeadOpen(blobKey, raw.blob, aad)
|
||||||
|
return GetResult(plaintext = plaintext, etag = raw.etag, updatedAt = raw.updatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create or update. `ifMatch`:
|
||||||
|
* - null: create-only (fails with 409 if slot populated).
|
||||||
|
* - "<etag>": CAS (fails with 412 on stale).
|
||||||
|
* - "*": unconditional overwrite.
|
||||||
|
*/
|
||||||
|
fun put(plaintext: ByteArray, ifMatch: String? = null): PutResult {
|
||||||
|
// Fresh random nonce per write — see `BlobAead`. Re-uploading
|
||||||
|
// the same plaintext after a transient error reuses neither
|
||||||
|
// (key, nonce, plaintext) nor (key, nonce).
|
||||||
|
val nonce = crypto.randomBytes(BLOB_AEAD_NONCE_LEN)
|
||||||
|
val sealed = aeadSeal(blobKey, nonce, plaintext, aad)
|
||||||
|
val r = client.put(
|
||||||
|
slotIdHex = slotIdHex,
|
||||||
|
blob = sealed,
|
||||||
|
signingSeed = signingSeed,
|
||||||
|
ownerPubkey = ownerPubkey,
|
||||||
|
ifMatch = ifMatch,
|
||||||
|
)
|
||||||
|
return PutResult(created = r.created, etag = r.etag, updatedAt = r.updatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** "Forget everything" path — the next PUT TOFU-claims it again. */
|
||||||
|
fun delete(): Boolean = client.delete(slotIdHex, signingSeed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a Profile namespace bound to a (master key, app) pair. The
|
||||||
|
* derivations are deterministic: any device with the same master
|
||||||
|
* key + app namespace produces the same slot, so a fresh device
|
||||||
|
* after credential entry can locate the existing profile blob.
|
||||||
|
*/
|
||||||
|
fun createProfileNamespace(
|
||||||
|
baseUrl: String,
|
||||||
|
crypto: CryptoProvider,
|
||||||
|
masterKey: ByteArray,
|
||||||
|
app: String,
|
||||||
|
): ProfileNamespace {
|
||||||
|
require(masterKey.size == 32) { "masterKey must be 32 bytes" }
|
||||||
|
require(app.isNotEmpty()) { "app namespace must be non-empty" }
|
||||||
|
|
||||||
|
val slotIdBytes = deriveBlobSlotId(crypto, masterKey, app)
|
||||||
|
val slotIdHex = slotIdToHex(slotIdBytes)
|
||||||
|
val blobKey = deriveBlobKey(crypto, masterKey, app)
|
||||||
|
val signingSeed = deriveBlobSigningSeed(crypto, masterKey, app)
|
||||||
|
val ownerPubkey = ed25519PublicKeyFromSeed(signingSeed)
|
||||||
|
val aad = blobAadForSlot(slotIdHex)
|
||||||
|
|
||||||
|
val client = BlobClient(baseUrl = baseUrl, crypto = crypto)
|
||||||
|
return ProfileNamespace(
|
||||||
|
slotIdHex = slotIdHex,
|
||||||
|
blobKey = blobKey,
|
||||||
|
signingSeed = signingSeed,
|
||||||
|
ownerPubkey = ownerPubkey,
|
||||||
|
aad = aad,
|
||||||
|
client = client,
|
||||||
|
crypto = crypto,
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
package no.zyon.shade.serialization
|
||||||
|
|
||||||
|
import no.zyon.shade.types.ChainState
|
||||||
|
import no.zyon.shade.types.IdentityKeyPair
|
||||||
|
import no.zyon.shade.types.KeyPair
|
||||||
|
import no.zyon.shade.types.OneTimePreKey
|
||||||
|
import no.zyon.shade.types.SessionState
|
||||||
|
import no.zyon.shade.types.SignedPreKey
|
||||||
|
import org.json.JSONObject
|
||||||
|
import java.util.Base64 as JdkBase64
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plain-JSON serialization for the persisted protocol state types
|
||||||
|
* (`IdentityKeyPair`, `SignedPreKey`, `OneTimePreKey`, `SessionState`).
|
||||||
|
*
|
||||||
|
* The on-disk shape is for at-rest storage only — it does NOT need
|
||||||
|
* to round-trip across platforms (TS uses its own JSON shape via
|
||||||
|
* `@shade/core/serialization`). What matters is that the Kotlin
|
||||||
|
* round-trip (`serialize` then `deserialize`) preserves every byte.
|
||||||
|
*
|
||||||
|
* Both Android-targeted (`shade-android-keystore`) and pure-JVM
|
||||||
|
* (`shade-android` tests) callers use this — the function works
|
||||||
|
* without any `android.*` imports so it compiles in both.
|
||||||
|
*/
|
||||||
|
object SessionStateJson {
|
||||||
|
|
||||||
|
fun serialize(state: SessionState): String {
|
||||||
|
val o = JSONObject()
|
||||||
|
o.put("remoteIdentityKey", b64(state.remoteIdentityKey))
|
||||||
|
o.put("rootKey", b64(state.rootKey))
|
||||||
|
o.put("sendChain", chainToJson(state.sendChain))
|
||||||
|
if (state.receiveChain != null) o.put("receiveChain", chainToJson(state.receiveChain!!))
|
||||||
|
o.put("dhSend", keyPairToJson(state.dhSend))
|
||||||
|
if (state.dhReceive != null) o.put("dhReceive", b64(state.dhReceive!!))
|
||||||
|
o.put("previousSendCounter", state.previousSendCounter)
|
||||||
|
val skipped = JSONObject()
|
||||||
|
for ((k, v) in state.skippedKeys) skipped.put(k, b64(v))
|
||||||
|
o.put("skippedKeys", skipped)
|
||||||
|
return o.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deserialize(s: String): SessionState {
|
||||||
|
val o = JSONObject(s)
|
||||||
|
val skipped = mutableMapOf<String, ByteArray>()
|
||||||
|
val skJson = o.optJSONObject("skippedKeys")
|
||||||
|
if (skJson != null) {
|
||||||
|
val it = skJson.keys()
|
||||||
|
while (it.hasNext()) {
|
||||||
|
val k = it.next()
|
||||||
|
skipped[k] = fb64(skJson.getString(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SessionState(
|
||||||
|
remoteIdentityKey = fb64(o.getString("remoteIdentityKey")),
|
||||||
|
rootKey = fb64(o.getString("rootKey")),
|
||||||
|
sendChain = chainFromJson(o.getJSONObject("sendChain")),
|
||||||
|
receiveChain = if (o.has("receiveChain"))
|
||||||
|
chainFromJson(o.getJSONObject("receiveChain")) else null,
|
||||||
|
dhSend = keyPairFromJson(o.getJSONObject("dhSend")),
|
||||||
|
dhReceive = if (o.has("dhReceive")) fb64(o.getString("dhReceive")) else null,
|
||||||
|
previousSendCounter = o.getInt("previousSendCounter"),
|
||||||
|
skippedKeys = skipped,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun serializeIdentityKeyPair(k: IdentityKeyPair): String = JSONObject().apply {
|
||||||
|
put("signingPublicKey", b64(k.signingPublicKey))
|
||||||
|
put("signingPrivateKey", b64(k.signingPrivateKey))
|
||||||
|
put("dhPublicKey", b64(k.dhPublicKey))
|
||||||
|
put("dhPrivateKey", b64(k.dhPrivateKey))
|
||||||
|
}.toString()
|
||||||
|
|
||||||
|
fun deserializeIdentityKeyPair(s: String): IdentityKeyPair = JSONObject(s).run {
|
||||||
|
IdentityKeyPair(
|
||||||
|
signingPublicKey = fb64(getString("signingPublicKey")),
|
||||||
|
signingPrivateKey = fb64(getString("signingPrivateKey")),
|
||||||
|
dhPublicKey = fb64(getString("dhPublicKey")),
|
||||||
|
dhPrivateKey = fb64(getString("dhPrivateKey")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun serializeSignedPreKey(k: SignedPreKey): String = JSONObject().apply {
|
||||||
|
put("keyId", k.keyId)
|
||||||
|
put("keyPair", keyPairToJson(k.keyPair))
|
||||||
|
put("signature", b64(k.signature))
|
||||||
|
put("timestamp", k.timestamp)
|
||||||
|
}.toString()
|
||||||
|
|
||||||
|
fun deserializeSignedPreKey(s: String): SignedPreKey = JSONObject(s).run {
|
||||||
|
SignedPreKey(
|
||||||
|
keyId = getInt("keyId"),
|
||||||
|
keyPair = keyPairFromJson(getJSONObject("keyPair")),
|
||||||
|
signature = fb64(getString("signature")),
|
||||||
|
timestamp = getLong("timestamp"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun serializeOneTimePreKey(k: OneTimePreKey): String = JSONObject().apply {
|
||||||
|
put("keyId", k.keyId)
|
||||||
|
put("keyPair", keyPairToJson(k.keyPair))
|
||||||
|
}.toString()
|
||||||
|
|
||||||
|
fun deserializeOneTimePreKey(s: String): OneTimePreKey = JSONObject(s).run {
|
||||||
|
OneTimePreKey(
|
||||||
|
keyId = getInt("keyId"),
|
||||||
|
keyPair = keyPairFromJson(getJSONObject("keyPair")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun chainToJson(c: ChainState): JSONObject = JSONObject().apply {
|
||||||
|
put("chainKey", b64(c.chainKey))
|
||||||
|
put("counter", c.counter)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun chainFromJson(o: JSONObject): ChainState =
|
||||||
|
ChainState(chainKey = fb64(o.getString("chainKey")), counter = o.getInt("counter"))
|
||||||
|
|
||||||
|
private fun keyPairToJson(k: KeyPair): JSONObject = JSONObject().apply {
|
||||||
|
put("publicKey", b64(k.publicKey))
|
||||||
|
put("privateKey", b64(k.privateKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun keyPairFromJson(o: JSONObject): KeyPair = KeyPair(
|
||||||
|
publicKey = fb64(o.getString("publicKey")),
|
||||||
|
privateKey = fb64(o.getString("privateKey")),
|
||||||
|
)
|
||||||
|
|
||||||
|
// android.util.Base64 isn't on the JVM classpath; java.util.Base64
|
||||||
|
// is available on both modern JVM and Android API 26+. Use JDK
|
||||||
|
// Base64 throughout — it's present on both targets.
|
||||||
|
private fun b64(b: ByteArray): String = JdkBase64.getEncoder().encodeToString(b)
|
||||||
|
private fun fb64(s: String): ByteArray = JdkBase64.getDecoder().decode(s)
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package no.zyon.shade.storage
|
||||||
|
|
||||||
|
import org.bouncycastle.crypto.generators.Argon2BytesGenerator
|
||||||
|
import org.bouncycastle.crypto.generators.SCrypt
|
||||||
|
import org.bouncycastle.crypto.params.Argon2Parameters
|
||||||
|
import java.text.Normalizer
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Password / PIN key-derivation primitives. Mirror
|
||||||
|
* `@shade/storage-encrypted/crypto/kdf` (`deriveMasterKey` /
|
||||||
|
* `deriveMasterKeyArgon2id`) byte-for-byte — Tink doesn't ship password
|
||||||
|
* KDFs so we wrap Bouncy Castle.
|
||||||
|
*
|
||||||
|
* Both functions normalize string passphrases to NFKC before hashing,
|
||||||
|
* matching the TS implementation's `passphrase.normalize('NFKC')`.
|
||||||
|
* This ensures the same password typed on different OSes/keyboards
|
||||||
|
* produces the same master key regardless of which compatibility-form
|
||||||
|
* the input arrived in.
|
||||||
|
*
|
||||||
|
* The reference test-vector lives in `test-vectors/storage-encryption.json`
|
||||||
|
* and `test-vectors/blob-storage.json`. Cross-platform parity is gated
|
||||||
|
* by `CrossPlatformVectorTest`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** scrypt parameters. Defaults match `DEFAULT_SCRYPT` in TS. */
|
||||||
|
data class ScryptParams(
|
||||||
|
/** CPU/memory cost. Must be a power of 2. */
|
||||||
|
val n: Int = 1 shl 17,
|
||||||
|
/** Block size. */
|
||||||
|
val r: Int = 8,
|
||||||
|
/** Parallelization. */
|
||||||
|
val p: Int = 1,
|
||||||
|
/** Output length in bytes. */
|
||||||
|
val dkLen: Int = 32,
|
||||||
|
)
|
||||||
|
|
||||||
|
/** Argon2id parameters. Defaults match `DEFAULT_ARGON2ID` in TS. */
|
||||||
|
data class Argon2idParams(
|
||||||
|
/** Memory cost in KiB. Default 64 MiB. */
|
||||||
|
val m: Int = 64 * 1024,
|
||||||
|
/** Time cost (iterations). Default 3. */
|
||||||
|
val t: Int = 3,
|
||||||
|
/** Parallelism. Default 1. */
|
||||||
|
val p: Int = 1,
|
||||||
|
/** Output length in bytes. Default 32. */
|
||||||
|
val dkLen: Int = 32,
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derive a 32-byte master key from a passphrase + salt via scrypt.
|
||||||
|
* Salt MUST be at least 16 bytes and persisted alongside the
|
||||||
|
* encrypted database. Throws on empty passphrase.
|
||||||
|
*/
|
||||||
|
fun deriveMasterKey(
|
||||||
|
passphrase: String,
|
||||||
|
salt: ByteArray,
|
||||||
|
params: ScryptParams = ScryptParams(),
|
||||||
|
): ByteArray {
|
||||||
|
require(passphrase.isNotEmpty()) { "passphrase must be non-empty" }
|
||||||
|
require(salt.size >= 16) { "salt must be at least 16 bytes" }
|
||||||
|
val nfkc = Normalizer.normalize(passphrase, Normalizer.Form.NFKC)
|
||||||
|
val pwBytes = nfkc.toByteArray(Charsets.UTF_8)
|
||||||
|
return SCrypt.generate(pwBytes, salt, params.n, params.r, params.p, params.dkLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derive a 32-byte master key from a low-entropy secret (PIN) + salt
|
||||||
|
* via argon2id. Salt MUST be at least 16 bytes. The Bouncy Castle
|
||||||
|
* `Argon2BytesGenerator` parameters mirror RFC 9106's argon2id mode
|
||||||
|
* with version 1.3 (`Argon2Parameters.ARGON2_VERSION_13`), which is
|
||||||
|
* what `@noble/hashes/argon2` produces — keeping cross-platform parity.
|
||||||
|
*/
|
||||||
|
fun deriveMasterKeyArgon2id(
|
||||||
|
secret: String,
|
||||||
|
salt: ByteArray,
|
||||||
|
params: Argon2idParams = Argon2idParams(),
|
||||||
|
): ByteArray {
|
||||||
|
require(secret.isNotEmpty()) { "argon2id secret must be non-empty" }
|
||||||
|
require(salt.size >= 16) { "salt must be at least 16 bytes" }
|
||||||
|
val nfkc = Normalizer.normalize(secret, Normalizer.Form.NFKC)
|
||||||
|
return deriveMasterKeyArgon2id(nfkc.toByteArray(Charsets.UTF_8), salt, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Byte-array overload — useful when the secret is already binary
|
||||||
|
* (e.g. derived from a hardware token rather than typed) and
|
||||||
|
* shouldn't be NFKC-normalized as text.
|
||||||
|
*/
|
||||||
|
fun deriveMasterKeyArgon2id(
|
||||||
|
secret: ByteArray,
|
||||||
|
salt: ByteArray,
|
||||||
|
params: Argon2idParams = Argon2idParams(),
|
||||||
|
): ByteArray {
|
||||||
|
require(secret.isNotEmpty()) { "argon2id secret must be non-empty" }
|
||||||
|
require(salt.size >= 16) { "salt must be at least 16 bytes" }
|
||||||
|
val builder = Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)
|
||||||
|
.withVersion(Argon2Parameters.ARGON2_VERSION_13)
|
||||||
|
.withIterations(params.t)
|
||||||
|
.withMemoryAsKB(params.m)
|
||||||
|
.withParallelism(params.p)
|
||||||
|
.withSalt(salt)
|
||||||
|
val gen = Argon2BytesGenerator()
|
||||||
|
gen.init(builder.build())
|
||||||
|
val out = ByteArray(params.dkLen)
|
||||||
|
gen.generateBytes(secret, out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
@@ -0,0 +1,593 @@
|
|||||||
|
package no.zyon.shade
|
||||||
|
|
||||||
|
import no.zyon.shade.approval.ApprovalRequestFrame
|
||||||
|
import no.zyon.shade.approval.ApprovalRequestingDevice
|
||||||
|
import no.zyon.shade.approval.CanonicalProfileBlob
|
||||||
|
import no.zyon.shade.approval.DEFAULT_APPROVAL_DOMAIN
|
||||||
|
import no.zyon.shade.approval.ProfileClientEntry
|
||||||
|
import no.zyon.shade.approval.ProfileHostEntry
|
||||||
|
import no.zyon.shade.approval.ProxyApprovalFrame
|
||||||
|
import no.zyon.shade.approval.VerifyProxyApprovalResult
|
||||||
|
import no.zyon.shade.approval.buildApprovalRequest
|
||||||
|
import no.zyon.shade.approval.canonicalApprovalSigningBytes
|
||||||
|
import no.zyon.shade.approval.emptyCanonicalProfile
|
||||||
|
import no.zyon.shade.approval.findClientByAddress
|
||||||
|
import no.zyon.shade.approval.findClientByFingerprint
|
||||||
|
import no.zyon.shade.approval.isTrustedApprover
|
||||||
|
import no.zyon.shade.approval.parseCanonicalProfile
|
||||||
|
import no.zyon.shade.approval.removeClient
|
||||||
|
import no.zyon.shade.approval.serializeCanonicalProfile
|
||||||
|
import no.zyon.shade.approval.setTrustedApprover
|
||||||
|
import no.zyon.shade.approval.signProxyApproval
|
||||||
|
import no.zyon.shade.approval.upsertClient
|
||||||
|
import no.zyon.shade.approval.upsertHost
|
||||||
|
import no.zyon.shade.approval.verifyProxyApproval
|
||||||
|
import no.zyon.shade.blob.aeadOpen
|
||||||
|
import no.zyon.shade.blob.aeadSeal
|
||||||
|
import no.zyon.shade.blob.blobAadForSlot
|
||||||
|
import no.zyon.shade.blob.deriveBlobKey
|
||||||
|
import no.zyon.shade.blob.deriveBlobSigningSeed
|
||||||
|
import no.zyon.shade.blob.deriveBlobSlotId
|
||||||
|
import no.zyon.shade.blob.ed25519PublicKeyFromSeed
|
||||||
|
import no.zyon.shade.blob.slotIdToHex
|
||||||
|
import no.zyon.shade.crypto.TinkProvider
|
||||||
|
import no.zyon.shade.storage.Argon2idParams
|
||||||
|
import no.zyon.shade.storage.ScryptParams
|
||||||
|
import no.zyon.shade.storage.deriveMasterKey
|
||||||
|
import no.zyon.shade.storage.deriveMasterKeyArgon2id
|
||||||
|
import org.junit.Assert.assertArrayEquals
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertNotNull
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
|
import org.junit.Assert.assertNotEquals
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for the V4.9 blob primitive ports + V4.10 approval
|
||||||
|
* helpers + scrypt/argon2id wrappers. Cross-platform vector parity
|
||||||
|
* lives in `CrossPlatformVectorTest`; this file tests Kotlin-side
|
||||||
|
* round-trip behavior independent of the TS reference.
|
||||||
|
*/
|
||||||
|
class BlobAndApprovalTest {
|
||||||
|
|
||||||
|
private val crypto = TinkProvider()
|
||||||
|
|
||||||
|
private fun hex(bytes: ByteArray): String =
|
||||||
|
bytes.joinToString("") { "%02x".format(it) }
|
||||||
|
|
||||||
|
// ─── V4.9 blob KDF ─────────────────────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun deriveBlobSlotIdIsDeterministicPerMasterAndApp() {
|
||||||
|
val km = ByteArray(32) { it.toByte() }
|
||||||
|
val a1 = deriveBlobSlotId(crypto, km, "foo")
|
||||||
|
val a2 = deriveBlobSlotId(crypto, km, "foo")
|
||||||
|
assertArrayEquals(a1, a2)
|
||||||
|
|
||||||
|
val b = deriveBlobSlotId(crypto, km, "bar")
|
||||||
|
assertFalse(a1.contentEquals(b))
|
||||||
|
|
||||||
|
val km2 = ByteArray(32) { (it + 1).toByte() }
|
||||||
|
val c = deriveBlobSlotId(crypto, km2, "foo")
|
||||||
|
assertFalse(a1.contentEquals(c))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun blobKdfHelpersAreIndependent() {
|
||||||
|
val km = ByteArray(32) { it.toByte() }
|
||||||
|
val slot = deriveBlobSlotId(crypto, km, "x")
|
||||||
|
val key = deriveBlobKey(crypto, km, "x")
|
||||||
|
val seed = deriveBlobSigningSeed(crypto, km, "x")
|
||||||
|
assertFalse(slot.contentEquals(key))
|
||||||
|
assertFalse(slot.contentEquals(seed))
|
||||||
|
assertFalse(key.contentEquals(seed))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun ed25519PublicKeyFromSeedIsDeterministic() {
|
||||||
|
val seed = ByteArray(32) { it.toByte() }
|
||||||
|
val pk1 = ed25519PublicKeyFromSeed(seed)
|
||||||
|
val pk2 = ed25519PublicKeyFromSeed(seed)
|
||||||
|
assertArrayEquals(pk1, pk2)
|
||||||
|
assertEquals(32, pk1.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun slotIdToHexProducesLowercase64Chars() {
|
||||||
|
val s = ByteArray(32) { 0xab.toByte() }
|
||||||
|
val hex = slotIdToHex(s)
|
||||||
|
assertEquals(64, hex.length)
|
||||||
|
assertEquals("a".repeat(0) + "ab".repeat(32), hex)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── V4.9 AEAD round-trip ──────────────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun aeadSealOpenRoundTrip() {
|
||||||
|
val key = crypto.randomBytes(32)
|
||||||
|
val nonce = crypto.randomBytes(12)
|
||||||
|
val pt = "hello".toByteArray()
|
||||||
|
val aad = blobAadForSlot("00".repeat(32))
|
||||||
|
val sealed = aeadSeal(key, nonce, pt, aad)
|
||||||
|
val opened = aeadOpen(key, sealed, aad)
|
||||||
|
assertArrayEquals(pt, opened)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun aeadOpenWithWrongAadFails() {
|
||||||
|
val key = crypto.randomBytes(32)
|
||||||
|
val nonce = crypto.randomBytes(12)
|
||||||
|
val pt = "hello".toByteArray()
|
||||||
|
val aad1 = blobAadForSlot("00".repeat(32))
|
||||||
|
val aad2 = blobAadForSlot("ff".repeat(32))
|
||||||
|
val sealed = aeadSeal(key, nonce, pt, aad1)
|
||||||
|
try {
|
||||||
|
aeadOpen(key, sealed, aad2)
|
||||||
|
org.junit.Assert.fail("expected AEAD to reject wrong AAD")
|
||||||
|
} catch (_: Exception) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun aeadOpenWithExpectedNonceMismatchFails() {
|
||||||
|
val key = crypto.randomBytes(32)
|
||||||
|
val nonce = crypto.randomBytes(12)
|
||||||
|
val wrongNonce = crypto.randomBytes(12)
|
||||||
|
val pt = "hello".toByteArray()
|
||||||
|
val aad = blobAadForSlot("00".repeat(32))
|
||||||
|
val sealed = aeadSeal(key, nonce, pt, aad)
|
||||||
|
try {
|
||||||
|
aeadOpen(key, sealed, aad, expectedNonce = wrongNonce)
|
||||||
|
org.junit.Assert.fail("expected expectedNonce check to reject")
|
||||||
|
} catch (_: IllegalArgumentException) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Password KDFs ─────────────────────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun scryptDerivesDeterministically() {
|
||||||
|
val pw = "correct-horse-battery-staple"
|
||||||
|
val salt = ByteArray(16) { 0x42.toByte() }
|
||||||
|
val k1 = deriveMasterKey(pw, salt, ScryptParams(n = 1024))
|
||||||
|
val k2 = deriveMasterKey(pw, salt, ScryptParams(n = 1024))
|
||||||
|
assertArrayEquals(k1, k2)
|
||||||
|
assertEquals(32, k1.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun argon2idDerivesDeterministically() {
|
||||||
|
val pw = "1234"
|
||||||
|
val salt = ByteArray(16) { 0x55.toByte() }
|
||||||
|
val k1 = deriveMasterKeyArgon2id(pw, salt, Argon2idParams(m = 256, t = 2))
|
||||||
|
val k2 = deriveMasterKeyArgon2id(pw, salt, Argon2idParams(m = 256, t = 2))
|
||||||
|
assertArrayEquals(k1, k2)
|
||||||
|
assertEquals(32, k1.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun nfkcNormalizationMakesEquivalentInputsConverge() {
|
||||||
|
// "café" can be encoded either as 'c','a','f','é' (NFC) or
|
||||||
|
// 'c','a','f','e','́' (NFD). NFKC normalization on both
|
||||||
|
// should converge to the same bytes.
|
||||||
|
val nfc = "café"
|
||||||
|
val nfd = "café"
|
||||||
|
assertNotEquals(nfc, nfd)
|
||||||
|
val salt = ByteArray(16) { 1.toByte() }
|
||||||
|
val k1 = deriveMasterKey(nfc, salt, ScryptParams(n = 1024))
|
||||||
|
val k2 = deriveMasterKey(nfd, salt, ScryptParams(n = 1024))
|
||||||
|
assertArrayEquals(k1, k2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Canonical profile schema ──────────────────────────────
|
||||||
|
|
||||||
|
private fun makeClient(name: String, trusted: Boolean = false): Pair<ProfileClientEntry, ByteArray> {
|
||||||
|
val seed = crypto.randomBytes(32)
|
||||||
|
val pubkey = ed25519PublicKeyFromSeed(seed)
|
||||||
|
val fp = "fp-$name-${hex(pubkey).take(8)}"
|
||||||
|
return ProfileClientEntry(
|
||||||
|
address = "device:$name",
|
||||||
|
identityPublicKey = hex(pubkey),
|
||||||
|
identityFingerprint = fp,
|
||||||
|
name = name,
|
||||||
|
kind = "mobile",
|
||||||
|
addedAt = 1_700_000_000_000L,
|
||||||
|
trustedApprover = trusted,
|
||||||
|
) to seed
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun makeHost(): ProfileHostEntry = ProfileHostEntry(
|
||||||
|
address = "device:host-server",
|
||||||
|
name = "Server",
|
||||||
|
kind = "server",
|
||||||
|
addedAt = 1_700_000_000_000L,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun emptyCanonicalProfileRoundTrips() {
|
||||||
|
val blob = emptyCanonicalProfile(now = 123L)
|
||||||
|
val bytes = serializeCanonicalProfile(blob)
|
||||||
|
val parsed = parseCanonicalProfile(bytes)
|
||||||
|
assertEquals(1, parsed.version)
|
||||||
|
assertTrue(parsed.hosts.isEmpty())
|
||||||
|
assertTrue(parsed.clients.isEmpty())
|
||||||
|
assertTrue(parsed.trustedApproverFingerprints.isEmpty())
|
||||||
|
assertEquals(123L, parsed.updatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun upsertClientDenormalizesTrustedApprovers() {
|
||||||
|
var blob = emptyCanonicalProfile(0)
|
||||||
|
val (a, _) = makeClient("phone-a", trusted = true)
|
||||||
|
val (b, _) = makeClient("phone-b", trusted = false)
|
||||||
|
blob = upsertClient(blob, a)
|
||||||
|
blob = upsertClient(blob, b)
|
||||||
|
assertEquals(2, blob.clients.size)
|
||||||
|
assertEquals(listOf(a.identityFingerprint), blob.trustedApproverFingerprints)
|
||||||
|
assertTrue(isTrustedApprover(blob, a.identityFingerprint))
|
||||||
|
assertFalse(isTrustedApprover(blob, b.identityFingerprint))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun setTrustedApproverIsIdempotentNoOpReturnsSameInstance() {
|
||||||
|
var blob = emptyCanonicalProfile(0)
|
||||||
|
val (c, _) = makeClient("phone", trusted = false)
|
||||||
|
blob = upsertClient(blob, c)
|
||||||
|
val before = blob
|
||||||
|
blob = setTrustedApprover(blob, c.identityFingerprint, false, now = 999L)
|
||||||
|
assertTrue(blob === before)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun setTrustedApproverFlipsFlagAndDenormalizedList() {
|
||||||
|
var blob = emptyCanonicalProfile(0)
|
||||||
|
val (c, _) = makeClient("phone", trusted = false)
|
||||||
|
blob = upsertClient(blob, c)
|
||||||
|
blob = setTrustedApprover(blob, c.identityFingerprint, true, now = 100L)
|
||||||
|
assertEquals(listOf(c.identityFingerprint), blob.trustedApproverFingerprints)
|
||||||
|
assertEquals(true, blob.clients[0].trustedApprover)
|
||||||
|
|
||||||
|
blob = setTrustedApprover(blob, c.identityFingerprint, false, now = 200L)
|
||||||
|
assertTrue(blob.trustedApproverFingerprints.isEmpty())
|
||||||
|
assertEquals(false, blob.clients[0].trustedApprover)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun removeClientCleansUpDenormalizedList() {
|
||||||
|
var blob = emptyCanonicalProfile(0)
|
||||||
|
val (c, _) = makeClient("phone", trusted = true)
|
||||||
|
blob = upsertClient(blob, c)
|
||||||
|
blob = removeClient(blob, c.identityFingerprint)
|
||||||
|
assertTrue(blob.clients.isEmpty())
|
||||||
|
assertTrue(blob.trustedApproverFingerprints.isEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun findClientByFingerprintAndAddress() {
|
||||||
|
var blob = emptyCanonicalProfile(0)
|
||||||
|
val (c, _) = makeClient("phone")
|
||||||
|
blob = upsertClient(blob, c)
|
||||||
|
assertEquals(c.address, findClientByFingerprint(blob, c.identityFingerprint)?.address)
|
||||||
|
assertEquals(
|
||||||
|
c.identityFingerprint,
|
||||||
|
findClientByAddress(blob, c.address)?.identityFingerprint,
|
||||||
|
)
|
||||||
|
assertNull(findClientByFingerprint(blob, "unknown"))
|
||||||
|
assertNull(findClientByAddress(blob, "unknown"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun parseRejectsMalformedProfile() {
|
||||||
|
try {
|
||||||
|
parseCanonicalProfile("not json")
|
||||||
|
org.junit.Assert.fail("expected throw")
|
||||||
|
} catch (_: IllegalArgumentException) {}
|
||||||
|
try {
|
||||||
|
parseCanonicalProfile("""{"version":2}""")
|
||||||
|
org.junit.Assert.fail("expected throw")
|
||||||
|
} catch (_: IllegalArgumentException) {}
|
||||||
|
try {
|
||||||
|
parseCanonicalProfile(
|
||||||
|
"""{"version":1,"clients":[{"address":"x","name":"x","kind":"m","addedAt":0}]}""",
|
||||||
|
)
|
||||||
|
org.junit.Assert.fail("expected throw — missing identityPublicKey")
|
||||||
|
} catch (_: IllegalArgumentException) {}
|
||||||
|
try {
|
||||||
|
parseCanonicalProfile(
|
||||||
|
"""{"version":1,"clients":[{"address":"x","identityPublicKey":"NOTHEX","identityFingerprint":"x","name":"x","kind":"m","addedAt":0}]}""",
|
||||||
|
)
|
||||||
|
org.junit.Assert.fail("expected throw — bad pubkey hex")
|
||||||
|
} catch (_: IllegalArgumentException) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun fullProfileSerializeParsePreservesAllFields() {
|
||||||
|
var blob = emptyCanonicalProfile(1L)
|
||||||
|
blob = upsertHost(blob, makeHost(), now = 2L)
|
||||||
|
val (c, _) = makeClient("phone", trusted = true)
|
||||||
|
blob = upsertClient(blob, c, now = 3L)
|
||||||
|
blob = blob.copy(signedBy = "aabbccdd")
|
||||||
|
|
||||||
|
val bytes = serializeCanonicalProfile(blob)
|
||||||
|
val parsed = parseCanonicalProfile(bytes)
|
||||||
|
assertEquals(blob, parsed)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Approval signing payload ──────────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun canonicalApprovalSigningBytesIsDeterministic() {
|
||||||
|
val a = canonicalApprovalSigningBytes(
|
||||||
|
domain = DEFAULT_APPROVAL_DOMAIN,
|
||||||
|
requestId = "aabbccddeeff00112233445566778899",
|
||||||
|
hostFingerprint = "11111 22222 33333 44444",
|
||||||
|
requestingDeviceFingerprint = "55555 66666 77777 88888",
|
||||||
|
decision = "approve",
|
||||||
|
)
|
||||||
|
val b = canonicalApprovalSigningBytes(
|
||||||
|
domain = DEFAULT_APPROVAL_DOMAIN,
|
||||||
|
requestId = "aabbccddeeff00112233445566778899",
|
||||||
|
hostFingerprint = "11111 22222 33333 44444",
|
||||||
|
requestingDeviceFingerprint = "55555 66666 77777 88888",
|
||||||
|
decision = "approve",
|
||||||
|
)
|
||||||
|
assertArrayEquals(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun differentDecisionProducesDifferentSigningBytes() {
|
||||||
|
val approve = canonicalApprovalSigningBytes(
|
||||||
|
DEFAULT_APPROVAL_DOMAIN, "r", "h", "d", "approve",
|
||||||
|
)
|
||||||
|
val reject = canonicalApprovalSigningBytes(
|
||||||
|
DEFAULT_APPROVAL_DOMAIN, "r", "h", "d", "reject",
|
||||||
|
)
|
||||||
|
assertFalse(approve.contentEquals(reject))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun differentDomainProducesDifferentSigningBytes() {
|
||||||
|
val a = canonicalApprovalSigningBytes("shade-link-approve-v1", "r", "h", "d", "approve")
|
||||||
|
val b = canonicalApprovalSigningBytes("prism-link-approve-v1", "r", "h", "d", "approve")
|
||||||
|
assertFalse(a.contentEquals(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Build / sign / verify ─────────────────────────────────
|
||||||
|
|
||||||
|
private data class Scenario(
|
||||||
|
val phone: ProfileClientEntry,
|
||||||
|
val phoneSeed: ByteArray,
|
||||||
|
val profile: CanonicalProfileBlob,
|
||||||
|
val request: ApprovalRequestFrame,
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun buildScenario(): Scenario {
|
||||||
|
val (phone, seed) = makeClient("phone", trusted = true)
|
||||||
|
var profile = emptyCanonicalProfile(0)
|
||||||
|
profile = upsertHost(profile, makeHost())
|
||||||
|
profile = upsertClient(profile, phone)
|
||||||
|
|
||||||
|
val request = buildApprovalRequest(
|
||||||
|
crypto = crypto,
|
||||||
|
hostAddress = "device:host-server",
|
||||||
|
hostFingerprint = "host-fp-12345",
|
||||||
|
requestingDeviceFingerprint = "cafe-laptop-fp-67890",
|
||||||
|
deviceName = "cafe-laptop",
|
||||||
|
userAgent = "Mozilla/5.0",
|
||||||
|
ipHint = "203.0.113.7",
|
||||||
|
)
|
||||||
|
return Scenario(phone, seed, profile, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun happyPathApproveVerifies() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request,
|
||||||
|
decision = "approve",
|
||||||
|
approverFingerprint = s.phone.identityFingerprint,
|
||||||
|
approverSigningKey = s.phoneSeed,
|
||||||
|
)
|
||||||
|
assertEquals("linkApproveByProxy", approval.kind)
|
||||||
|
assertEquals(s.request.requestId, approval.requestId)
|
||||||
|
assertEquals(128, approval.signature.length)
|
||||||
|
|
||||||
|
val r = verifyProxyApproval(crypto, s.request, approval, s.profile)
|
||||||
|
assertTrue(r is VerifyProxyApprovalResult.Ok)
|
||||||
|
assertEquals(s.phone.address, (r as VerifyProxyApprovalResult.Ok).approver.address)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun happyPathRejectVerifies() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "reject",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
val r = verifyProxyApproval(crypto, s.request, approval, s.profile)
|
||||||
|
assertTrue(r is VerifyProxyApprovalResult.Ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun replayAgainstDifferentRequestFails() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "approve",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
val other = s.request.copy(requestId = "f".repeat(32))
|
||||||
|
val r = verifyProxyApproval(crypto, other, approval, s.profile)
|
||||||
|
assertEquals(
|
||||||
|
VerifyProxyApprovalResult.Reason.REQUEST_ID_MISMATCH,
|
||||||
|
(r as VerifyProxyApprovalResult.Failed).reason,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun decisionTamperingFails() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "approve",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
val tampered = approval.copy(decision = "reject")
|
||||||
|
val r = verifyProxyApproval(crypto, s.request, tampered, s.profile)
|
||||||
|
assertEquals(
|
||||||
|
VerifyProxyApprovalResult.Reason.BAD_SIGNATURE,
|
||||||
|
(r as VerifyProxyApprovalResult.Failed).reason,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun hostFingerprintSwapFails() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "approve",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
val swapped = s.request.copy(hostFingerprint = "evil-host-fp")
|
||||||
|
val r = verifyProxyApproval(crypto, swapped, approval, s.profile)
|
||||||
|
assertEquals(
|
||||||
|
VerifyProxyApprovalResult.Reason.BAD_SIGNATURE,
|
||||||
|
(r as VerifyProxyApprovalResult.Failed).reason,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun domainMismatchIsRejectedBeforeSignature() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "approve",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
val r = verifyProxyApproval(
|
||||||
|
crypto, s.request, approval.copy(domain = "prism-link-approve-v1"), s.profile,
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
VerifyProxyApprovalResult.Reason.DOMAIN_MISMATCH,
|
||||||
|
(r as VerifyProxyApprovalResult.Failed).reason,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unknownApproverFails() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "approve",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
val lying = approval.copy(approverFingerprint = "no-such-fingerprint")
|
||||||
|
val r = verifyProxyApproval(crypto, s.request, lying, s.profile)
|
||||||
|
assertEquals(
|
||||||
|
VerifyProxyApprovalResult.Reason.UNKNOWN_APPROVER,
|
||||||
|
(r as VerifyProxyApprovalResult.Failed).reason,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun revokedApproverFailsWithNotTrusted() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "approve",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
val revoked = setTrustedApprover(s.profile, s.phone.identityFingerprint, false)
|
||||||
|
val r = verifyProxyApproval(crypto, s.request, approval, revoked)
|
||||||
|
assertEquals(
|
||||||
|
VerifyProxyApprovalResult.Reason.NOT_TRUSTED,
|
||||||
|
(r as VerifyProxyApprovalResult.Failed).reason,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun expiredRequestIsRejected() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "approve",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
val r = verifyProxyApproval(crypto, s.request, approval, s.profile, now = s.request.expiresAt + 1)
|
||||||
|
assertEquals(
|
||||||
|
VerifyProxyApprovalResult.Reason.EXPIRED,
|
||||||
|
(r as VerifyProxyApprovalResult.Failed).reason,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun signatureWithWrongKeyFails() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val wrongSeed = crypto.randomBytes(32)
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, s.request, "approve",
|
||||||
|
approverFingerprint = s.phone.identityFingerprint, // claim phone
|
||||||
|
approverSigningKey = wrongSeed, // sign with different key
|
||||||
|
)
|
||||||
|
val r = verifyProxyApproval(crypto, s.request, approval, s.profile)
|
||||||
|
assertEquals(
|
||||||
|
VerifyProxyApprovalResult.Reason.BAD_SIGNATURE,
|
||||||
|
(r as VerifyProxyApprovalResult.Failed).reason,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun customDomainSurvivesRoundTrip() {
|
||||||
|
val s = buildScenario()
|
||||||
|
val request = s.request.copy(domain = "prism-link-approve-v1")
|
||||||
|
val approval = signProxyApproval(
|
||||||
|
crypto, request, "approve",
|
||||||
|
s.phone.identityFingerprint, s.phoneSeed,
|
||||||
|
)
|
||||||
|
assertEquals("prism-link-approve-v1", approval.domain)
|
||||||
|
val r = verifyProxyApproval(crypto, request, approval, s.profile)
|
||||||
|
assertTrue(r is VerifyProxyApprovalResult.Ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun requestIdIs32LowercaseHexChars() {
|
||||||
|
val r = buildApprovalRequest(
|
||||||
|
crypto, "device:h", "h", "r",
|
||||||
|
)
|
||||||
|
assertTrue(r.requestId.matches(Regex("^[0-9a-f]{32}$")))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun consecutiveBuildsProduceDistinctRequestIds() {
|
||||||
|
val a = buildApprovalRequest(crypto, "device:h", "h", "r")
|
||||||
|
val b = buildApprovalRequest(crypto, "device:h", "h", "r")
|
||||||
|
assertNotEquals(a.requestId, b.requestId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Sanity glue: TS-side reference frame parses on Kotlin ──
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun tsStyleProxyApprovalFrameParsesAndStructurallyMatches() {
|
||||||
|
// Construct a frame the way @shade/sdk would emit it via JSON,
|
||||||
|
// and check our Kotlin types accept the same field names.
|
||||||
|
val expected = ProxyApprovalFrame(
|
||||||
|
requestId = "00112233445566778899aabbccddeeff",
|
||||||
|
decision = "approve",
|
||||||
|
approverFingerprint = "fp",
|
||||||
|
signature = "ab".repeat(64),
|
||||||
|
domain = DEFAULT_APPROVAL_DOMAIN,
|
||||||
|
)
|
||||||
|
assertEquals("linkApproveByProxy", expected.kind)
|
||||||
|
assertEquals("approve", expected.decision)
|
||||||
|
|
||||||
|
val req = ApprovalRequestFrame(
|
||||||
|
requestId = "00112233445566778899aabbccddeeff",
|
||||||
|
hostAddress = "device:h",
|
||||||
|
hostFingerprint = "host-fp",
|
||||||
|
requestingDevice = ApprovalRequestingDevice(
|
||||||
|
fingerprint = "req-fp",
|
||||||
|
receivedAt = 1L,
|
||||||
|
),
|
||||||
|
expiresAt = 2L,
|
||||||
|
domain = DEFAULT_APPROVAL_DOMAIN,
|
||||||
|
)
|
||||||
|
assertNotNull(req)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,13 @@
|
|||||||
package no.zyon.shade
|
package no.zyon.shade
|
||||||
|
|
||||||
|
import no.zyon.shade.approval.canonicalApprovalSigningBytes
|
||||||
import no.zyon.shade.backup.deriveBackupKey
|
import no.zyon.shade.backup.deriveBackupKey
|
||||||
|
import no.zyon.shade.blob.aeadOpen
|
||||||
|
import no.zyon.shade.blob.blobAadForSlot
|
||||||
|
import no.zyon.shade.blob.deriveBlobKey
|
||||||
|
import no.zyon.shade.blob.deriveBlobSigningSeed
|
||||||
|
import no.zyon.shade.blob.deriveBlobSlotId
|
||||||
|
import no.zyon.shade.blob.ed25519PublicKeyFromSeed
|
||||||
import no.zyon.shade.crypto.TinkProvider
|
import no.zyon.shade.crypto.TinkProvider
|
||||||
import no.zyon.shade.fingerprint.computeFingerprint
|
import no.zyon.shade.fingerprint.computeFingerprint
|
||||||
import no.zyon.shade.group.encodeSenderHeader
|
import no.zyon.shade.group.encodeSenderHeader
|
||||||
@@ -447,6 +454,92 @@ class CrossPlatformVectorTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun blobKdfAndAeadVectorsMatch() {
|
||||||
|
val vectors = loadVectors("blob.json")
|
||||||
|
var kdfMatched = 0
|
||||||
|
var aeadMatched = 0
|
||||||
|
for (i in 0 until vectors.length()) {
|
||||||
|
val v = vectors.getJSONObject(i)
|
||||||
|
val desc = v.getString("description")
|
||||||
|
if (desc.startsWith("V4.9 blob KDF")) {
|
||||||
|
kdfMatched++
|
||||||
|
val masterKey = fromHex(v.getString("masterKey"))
|
||||||
|
val app = v.getString("app")
|
||||||
|
val slotId = deriveBlobSlotId(crypto, masterKey, app)
|
||||||
|
assertEquals(v.getString("slotId"), hex(slotId))
|
||||||
|
assertEquals(v.getString("blobKey"), hex(deriveBlobKey(crypto, masterKey, app)))
|
||||||
|
val seed = deriveBlobSigningSeed(crypto, masterKey, app)
|
||||||
|
assertEquals(v.getString("signingSeed"), hex(seed))
|
||||||
|
assertEquals(v.getString("ownerPubkey"), hex(ed25519PublicKeyFromSeed(seed)))
|
||||||
|
} else if (desc.startsWith("V4.9 blob AEAD")) {
|
||||||
|
aeadMatched++
|
||||||
|
val key = fromHex(v.getString("key"))
|
||||||
|
val slotIdHex = v.getString("slotIdHex")
|
||||||
|
val expectedPlaintext = fromHex(v.getString("plaintext"))
|
||||||
|
val wire = fromHex(v.getString("wire"))
|
||||||
|
val aad = blobAadForSlot(slotIdHex)
|
||||||
|
val opened = aeadOpen(key, wire, aad)
|
||||||
|
assertEquals(hex(expectedPlaintext), hex(opened))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue("KDF vectors expected", kdfMatched >= 3)
|
||||||
|
assertTrue("AEAD vectors expected", aeadMatched >= 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun approvalSigningPayloadVectorsMatch() {
|
||||||
|
val vectors = loadVectors("approval.json")
|
||||||
|
var payloadMatched = 0
|
||||||
|
var e2eMatched = 0
|
||||||
|
for (i in 0 until vectors.length()) {
|
||||||
|
val v = vectors.getJSONObject(i)
|
||||||
|
val desc = v.getString("description")
|
||||||
|
if (desc.startsWith("V4.10 approval signing payload")) {
|
||||||
|
payloadMatched++
|
||||||
|
val out = canonicalApprovalSigningBytes(
|
||||||
|
domain = v.getString("domain"),
|
||||||
|
requestId = v.getString("requestId"),
|
||||||
|
hostFingerprint = v.getString("hostFingerprint"),
|
||||||
|
requestingDeviceFingerprint = v.getString("requestingDeviceFingerprint"),
|
||||||
|
decision = v.getString("decision"),
|
||||||
|
)
|
||||||
|
assertEquals(v.getString("signingPayload"), hex(out))
|
||||||
|
} else if (desc.startsWith("V4.10 approval Ed25519 sign/verify")) {
|
||||||
|
e2eMatched++
|
||||||
|
val seed = fromHex(v.getString("seed"))
|
||||||
|
val pubkey = fromHex(v.getString("publicKey"))
|
||||||
|
assertEquals(hex(pubkey), hex(ed25519PublicKeyFromSeed(seed)))
|
||||||
|
|
||||||
|
val req = v.getJSONObject("request")
|
||||||
|
val payload = canonicalApprovalSigningBytes(
|
||||||
|
domain = req.getString("domain"),
|
||||||
|
requestId = req.getString("requestId"),
|
||||||
|
hostFingerprint = req.getString("hostFingerprint"),
|
||||||
|
requestingDeviceFingerprint = req.getString("requestingDeviceFingerprint"),
|
||||||
|
decision = req.getString("decision"),
|
||||||
|
)
|
||||||
|
assertEquals(v.getString("signingPayload"), hex(payload))
|
||||||
|
|
||||||
|
// Verify the TS-generated signature against our pubkey + payload.
|
||||||
|
// This is the load-bearing parity check: a Kotlin-implemented
|
||||||
|
// verifyProxyApproval running against a TS-signed approval
|
||||||
|
// succeeds.
|
||||||
|
val sig = fromHex(v.getString("signature"))
|
||||||
|
val ok = crypto.verify(pubkey, payload, sig)
|
||||||
|
assertTrue("Ed25519 verify of TS-signed approval failed", ok)
|
||||||
|
|
||||||
|
// And: Kotlin signs the same payload with the same seed and
|
||||||
|
// produces a sig the TS pubkey verifies. Ed25519 is
|
||||||
|
// deterministic, so the sig bytes also match exactly.
|
||||||
|
val mySig = crypto.sign(seed, payload)
|
||||||
|
assertEquals(v.getString("signature"), hex(mySig))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue("payload vectors expected", payloadMatched >= 3)
|
||||||
|
assertTrue("e2e sign/verify vector expected", e2eMatched >= 1)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun ratchetStepRoundtripMatches() {
|
fun ratchetStepRoundtripMatches() {
|
||||||
val vectors = loadVectors("ratchet-step.json")
|
val vectors = loadVectors("ratchet-step.json")
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package no.zyon.shade
|
||||||
|
|
||||||
|
import no.zyon.shade.serialization.SessionStateJson
|
||||||
|
import no.zyon.shade.types.ChainState
|
||||||
|
import no.zyon.shade.types.IdentityKeyPair
|
||||||
|
import no.zyon.shade.types.KeyPair
|
||||||
|
import no.zyon.shade.types.OneTimePreKey
|
||||||
|
import no.zyon.shade.types.SessionState
|
||||||
|
import no.zyon.shade.types.SignedPreKey
|
||||||
|
import org.junit.Assert.assertArrayEquals
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertNotNull
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Round-trip tests for the at-rest JSON serialization used by
|
||||||
|
* `KeystoreStorage`. The format isn't cross-platform (TS uses its
|
||||||
|
* own shape) — what matters is `serialize → deserialize` preserves
|
||||||
|
* every byte of every key.
|
||||||
|
*/
|
||||||
|
class SessionStateJsonTest {
|
||||||
|
|
||||||
|
private fun bytes(n: Int, fill: Byte): ByteArray = ByteArray(n) { fill }
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun identityKeyPairRoundTrip() {
|
||||||
|
val k = IdentityKeyPair(
|
||||||
|
signingPublicKey = bytes(32, 0x11),
|
||||||
|
signingPrivateKey = bytes(32, 0x22),
|
||||||
|
dhPublicKey = bytes(32, 0x33),
|
||||||
|
dhPrivateKey = bytes(32, 0x44),
|
||||||
|
)
|
||||||
|
val s = SessionStateJson.serializeIdentityKeyPair(k)
|
||||||
|
val d = SessionStateJson.deserializeIdentityKeyPair(s)
|
||||||
|
assertArrayEquals(k.signingPublicKey, d.signingPublicKey)
|
||||||
|
assertArrayEquals(k.signingPrivateKey, d.signingPrivateKey)
|
||||||
|
assertArrayEquals(k.dhPublicKey, d.dhPublicKey)
|
||||||
|
assertArrayEquals(k.dhPrivateKey, d.dhPrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun signedPreKeyRoundTrip() {
|
||||||
|
val k = SignedPreKey(
|
||||||
|
keyId = 42,
|
||||||
|
keyPair = KeyPair(publicKey = bytes(32, 0x55), privateKey = bytes(32, 0x66)),
|
||||||
|
signature = bytes(64, 0x77),
|
||||||
|
timestamp = 1_700_000_000_000L,
|
||||||
|
)
|
||||||
|
val s = SessionStateJson.serializeSignedPreKey(k)
|
||||||
|
val d = SessionStateJson.deserializeSignedPreKey(s)
|
||||||
|
assertEquals(k.keyId, d.keyId)
|
||||||
|
assertArrayEquals(k.keyPair.publicKey, d.keyPair.publicKey)
|
||||||
|
assertArrayEquals(k.keyPair.privateKey, d.keyPair.privateKey)
|
||||||
|
assertArrayEquals(k.signature, d.signature)
|
||||||
|
assertEquals(k.timestamp, d.timestamp)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun oneTimePreKeyRoundTrip() {
|
||||||
|
val k = OneTimePreKey(
|
||||||
|
keyId = 7,
|
||||||
|
keyPair = KeyPair(publicKey = bytes(32, 0x88.toByte()), privateKey = bytes(32, 0x99.toByte())),
|
||||||
|
)
|
||||||
|
val s = SessionStateJson.serializeOneTimePreKey(k)
|
||||||
|
val d = SessionStateJson.deserializeOneTimePreKey(s)
|
||||||
|
assertEquals(k.keyId, d.keyId)
|
||||||
|
assertArrayEquals(k.keyPair.publicKey, d.keyPair.publicKey)
|
||||||
|
assertArrayEquals(k.keyPair.privateKey, d.keyPair.privateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun sessionStateRoundTripFullPopulated() {
|
||||||
|
val state = SessionState(
|
||||||
|
remoteIdentityKey = bytes(32, 0x01),
|
||||||
|
rootKey = bytes(32, 0x02),
|
||||||
|
sendChain = ChainState(chainKey = bytes(32, 0x03), counter = 5),
|
||||||
|
receiveChain = ChainState(chainKey = bytes(32, 0x04), counter = 3),
|
||||||
|
dhSend = KeyPair(publicKey = bytes(32, 0x05), privateKey = bytes(32, 0x06)),
|
||||||
|
dhReceive = bytes(32, 0x07),
|
||||||
|
previousSendCounter = 9,
|
||||||
|
skippedKeys = mutableMapOf(
|
||||||
|
"remote:1" to bytes(32, 0x0A),
|
||||||
|
"remote:2" to bytes(32, 0x0B),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
val s = SessionStateJson.serialize(state)
|
||||||
|
val d = SessionStateJson.deserialize(s)
|
||||||
|
assertArrayEquals(state.remoteIdentityKey, d.remoteIdentityKey)
|
||||||
|
assertArrayEquals(state.rootKey, d.rootKey)
|
||||||
|
assertArrayEquals(state.sendChain.chainKey, d.sendChain.chainKey)
|
||||||
|
assertEquals(state.sendChain.counter, d.sendChain.counter)
|
||||||
|
assertNotNull(d.receiveChain)
|
||||||
|
assertArrayEquals(state.receiveChain!!.chainKey, d.receiveChain!!.chainKey)
|
||||||
|
assertArrayEquals(state.dhSend.publicKey, d.dhSend.publicKey)
|
||||||
|
assertArrayEquals(state.dhSend.privateKey, d.dhSend.privateKey)
|
||||||
|
assertArrayEquals(state.dhReceive, d.dhReceive)
|
||||||
|
assertEquals(state.previousSendCounter, d.previousSendCounter)
|
||||||
|
assertEquals(state.skippedKeys.size, d.skippedKeys.size)
|
||||||
|
for ((k, v) in state.skippedKeys) {
|
||||||
|
assertArrayEquals(v, d.skippedKeys[k])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun sessionStateRoundTripWithNullableFields() {
|
||||||
|
val state = SessionState(
|
||||||
|
remoteIdentityKey = bytes(32, 0x01),
|
||||||
|
rootKey = bytes(32, 0x02),
|
||||||
|
sendChain = ChainState(chainKey = bytes(32, 0x03), counter = 0),
|
||||||
|
receiveChain = null,
|
||||||
|
dhSend = KeyPair(publicKey = bytes(32, 0x05), privateKey = bytes(32, 0x06)),
|
||||||
|
dhReceive = null,
|
||||||
|
previousSendCounter = 0,
|
||||||
|
skippedKeys = mutableMapOf(),
|
||||||
|
)
|
||||||
|
val s = SessionStateJson.serialize(state)
|
||||||
|
val d = SessionStateJson.deserialize(s)
|
||||||
|
assertNull(d.receiveChain)
|
||||||
|
assertNull(d.dhReceive)
|
||||||
|
assertEquals(0, d.skippedKeys.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
73
bun.lock
73
bun.lock
@@ -17,7 +17,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-cli": {
|
"packages/shade-cli": {
|
||||||
"name": "@shade/cli",
|
"name": "@shade/cli",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"bin": {
|
"bin": {
|
||||||
"shade": "src/cli.ts",
|
"shade": "src/cli.ts",
|
||||||
},
|
},
|
||||||
@@ -36,7 +36,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-core": {
|
"packages/shade-core": {
|
||||||
"name": "@shade/core",
|
"name": "@shade/core",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/observability": "workspace:*",
|
"@shade/observability": "workspace:*",
|
||||||
},
|
},
|
||||||
@@ -49,7 +49,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-crypto-web": {
|
"packages/shade-crypto-web": {
|
||||||
"name": "@shade/crypto-web",
|
"name": "@shade/crypto-web",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/curves": "^2.0.1",
|
"@noble/curves": "^2.0.1",
|
||||||
"@noble/hashes": "^2.0.1",
|
"@noble/hashes": "^2.0.1",
|
||||||
@@ -59,7 +59,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-dashboard": {
|
"packages/shade-dashboard": {
|
||||||
"name": "@shade/dashboard",
|
"name": "@shade/dashboard",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/widgets": "workspace:*",
|
"@shade/widgets": "workspace:*",
|
||||||
"react": "^19.0.0",
|
"react": "^19.0.0",
|
||||||
@@ -74,7 +74,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-files": {
|
"packages/shade-files": {
|
||||||
"name": "@shade/files",
|
"name": "@shade/files",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
@@ -101,7 +101,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-inbox": {
|
"packages/shade-inbox": {
|
||||||
"name": "@shade/inbox",
|
"name": "@shade/inbox",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/proto": "workspace:*",
|
"@shade/proto": "workspace:*",
|
||||||
@@ -114,7 +114,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-inbox-server": {
|
"packages/shade-inbox-server": {
|
||||||
"name": "@shade/inbox-server",
|
"name": "@shade/inbox-server",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/observability": "workspace:*",
|
"@shade/observability": "workspace:*",
|
||||||
@@ -132,7 +132,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-key-transparency": {
|
"packages/shade-key-transparency": {
|
||||||
"name": "@shade/key-transparency",
|
"name": "@shade/key-transparency",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "^2.0.1",
|
"@noble/hashes": "^2.0.1",
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
@@ -144,11 +144,11 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-keychain": {
|
"packages/shade-keychain": {
|
||||||
"name": "@shade/keychain",
|
"name": "@shade/keychain",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
},
|
},
|
||||||
"packages/shade-observability": {
|
"packages/shade-observability": {
|
||||||
"name": "@shade/observability",
|
"name": "@shade/observability",
|
||||||
"version": "0.1.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "^2.0.1",
|
"@noble/hashes": "^2.0.1",
|
||||||
},
|
},
|
||||||
@@ -166,7 +166,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-observer": {
|
"packages/shade-observer": {
|
||||||
"name": "@shade/observer",
|
"name": "@shade/observer",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/server": "workspace:*",
|
"@shade/server": "workspace:*",
|
||||||
@@ -178,14 +178,14 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-proto": {
|
"packages/shade-proto": {
|
||||||
"name": "@shade/proto",
|
"name": "@shade/proto",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"packages/shade-recovery": {
|
"packages/shade-recovery": {
|
||||||
"name": "@shade/recovery",
|
"name": "@shade/recovery",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
@@ -198,22 +198,25 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-sdk": {
|
"packages/shade-sdk": {
|
||||||
"name": "@shade/sdk",
|
"name": "@shade/sdk",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
"@shade/files": "workspace:*",
|
"@shade/files": "workspace:*",
|
||||||
|
"@shade/inbox": "workspace:*",
|
||||||
"@shade/key-transparency": "workspace:*",
|
"@shade/key-transparency": "workspace:*",
|
||||||
"@shade/observability": "workspace:*",
|
"@shade/observability": "workspace:*",
|
||||||
"@shade/observer": "workspace:*",
|
"@shade/observer": "workspace:*",
|
||||||
"@shade/proto": "workspace:*",
|
"@shade/proto": "workspace:*",
|
||||||
"@shade/server": "workspace:*",
|
"@shade/server": "workspace:*",
|
||||||
|
"@shade/storage-encrypted": "workspace:*",
|
||||||
"@shade/storage-sqlite": "workspace:*",
|
"@shade/storage-sqlite": "workspace:*",
|
||||||
"@shade/streams": "workspace:*",
|
"@shade/streams": "workspace:*",
|
||||||
"@shade/transfer": "workspace:*",
|
"@shade/transfer": "workspace:*",
|
||||||
"@shade/transport": "workspace:*",
|
"@shade/transport": "workspace:*",
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@shade/inbox-server": "workspace:*",
|
||||||
"@shade/transport-webrtc": "workspace:*",
|
"@shade/transport-webrtc": "workspace:*",
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
@@ -225,7 +228,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-server": {
|
"packages/shade-server": {
|
||||||
"name": "@shade/server",
|
"name": "@shade/server",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/inbox-server": "workspace:*",
|
"@shade/inbox-server": "workspace:*",
|
||||||
@@ -245,15 +248,19 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-storage-encrypted": {
|
"packages/shade-storage-encrypted": {
|
||||||
"name": "@shade/storage-encrypted",
|
"name": "@shade/storage-encrypted",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "^2.0.1",
|
"@noble/hashes": "^2.0.1",
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
"@shade/storage-postgres": "workspace:*",
|
"@shade/storage-postgres": "workspace:*",
|
||||||
"@shade/storage-sqlite": "workspace:*",
|
"@shade/storage-sqlite": "workspace:*",
|
||||||
|
"idb": "^8.0.3",
|
||||||
"postgres": "^3.4.9",
|
"postgres": "^3.4.9",
|
||||||
},
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"fake-indexeddb": "^6.0.0",
|
||||||
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@shade/keychain": "workspace:*",
|
"@shade/keychain": "workspace:*",
|
||||||
},
|
},
|
||||||
@@ -261,9 +268,21 @@
|
|||||||
"@shade/keychain",
|
"@shade/keychain",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
"packages/shade-storage-indexeddb": {
|
||||||
|
"name": "@shade/storage-indexeddb",
|
||||||
|
"version": "4.8.5",
|
||||||
|
"dependencies": {
|
||||||
|
"@shade/core": "workspace:*",
|
||||||
|
"idb": "^8.0.3",
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@shade/crypto-web": "workspace:*",
|
||||||
|
"fake-indexeddb": "^6.0.0",
|
||||||
|
},
|
||||||
|
},
|
||||||
"packages/shade-storage-postgres": {
|
"packages/shade-storage-postgres": {
|
||||||
"name": "@shade/storage-postgres",
|
"name": "@shade/storage-postgres",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/inbox-server": "workspace:*",
|
"@shade/inbox-server": "workspace:*",
|
||||||
@@ -278,7 +297,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-storage-sqlite": {
|
"packages/shade-storage-sqlite": {
|
||||||
"name": "@shade/storage-sqlite",
|
"name": "@shade/storage-sqlite",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
@@ -288,7 +307,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-streams": {
|
"packages/shade-streams": {
|
||||||
"name": "@shade/streams",
|
"name": "@shade/streams",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "^2.0.1",
|
"@noble/hashes": "^2.0.1",
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
@@ -300,7 +319,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-transfer": {
|
"packages/shade-transfer": {
|
||||||
"name": "@shade/transfer",
|
"name": "@shade/transfer",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
@@ -317,7 +336,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-transport": {
|
"packages/shade-transport": {
|
||||||
"name": "@shade/transport",
|
"name": "@shade/transport",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
@@ -328,7 +347,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-transport-bridge": {
|
"packages/shade-transport-bridge": {
|
||||||
"name": "@shade/transport-bridge",
|
"name": "@shade/transport-bridge",
|
||||||
"version": "0.1.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/server": "workspace:*",
|
"@shade/server": "workspace:*",
|
||||||
@@ -350,7 +369,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-transport-webrtc": {
|
"packages/shade-transport-webrtc": {
|
||||||
"name": "@shade/transport-webrtc",
|
"name": "@shade/transport-webrtc",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/streams": "workspace:*",
|
"@shade/streams": "workspace:*",
|
||||||
@@ -359,7 +378,7 @@
|
|||||||
},
|
},
|
||||||
"packages/shade-widgets": {
|
"packages/shade-widgets": {
|
||||||
"name": "@shade/widgets",
|
"name": "@shade/widgets",
|
||||||
"version": "0.4.0",
|
"version": "4.8.5",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@shade/recovery": "workspace:*",
|
"@shade/recovery": "workspace:*",
|
||||||
"@shade/sdk": "workspace:*",
|
"@shade/sdk": "workspace:*",
|
||||||
@@ -568,6 +587,8 @@
|
|||||||
|
|
||||||
"@shade/storage-encrypted": ["@shade/storage-encrypted@workspace:packages/shade-storage-encrypted"],
|
"@shade/storage-encrypted": ["@shade/storage-encrypted@workspace:packages/shade-storage-encrypted"],
|
||||||
|
|
||||||
|
"@shade/storage-indexeddb": ["@shade/storage-indexeddb@workspace:packages/shade-storage-indexeddb"],
|
||||||
|
|
||||||
"@shade/storage-postgres": ["@shade/storage-postgres@workspace:packages/shade-storage-postgres"],
|
"@shade/storage-postgres": ["@shade/storage-postgres@workspace:packages/shade-storage-postgres"],
|
||||||
|
|
||||||
"@shade/storage-sqlite": ["@shade/storage-sqlite@workspace:packages/shade-storage-sqlite"],
|
"@shade/storage-sqlite": ["@shade/storage-sqlite@workspace:packages/shade-storage-sqlite"],
|
||||||
@@ -626,6 +647,8 @@
|
|||||||
|
|
||||||
"escalade": ["escalade@3.2.0", "", {}, "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA=="],
|
"escalade": ["escalade@3.2.0", "", {}, "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA=="],
|
||||||
|
|
||||||
|
"fake-indexeddb": ["fake-indexeddb@6.2.5", "", {}, "sha512-CGnyrvbhPlWYMngksqrSSUT1BAVP49dZocrHuK0SvtR0D5TMs5wP0o3j7jexDJW01KSadjBp1M/71o/KR3nD1w=="],
|
||||||
|
|
||||||
"fast-check": ["fast-check@3.23.2", "", { "dependencies": { "pure-rand": "^6.1.0" } }, "sha512-h5+1OzzfCC3Ef7VbtKdcv7zsstUQwUDlYpUTvjeUsJAssPgLn7QzbboPtL5ro04Mq0rPOsMzl7q5hIbRs2wD1A=="],
|
"fast-check": ["fast-check@3.23.2", "", { "dependencies": { "pure-rand": "^6.1.0" } }, "sha512-h5+1OzzfCC3Ef7VbtKdcv7zsstUQwUDlYpUTvjeUsJAssPgLn7QzbboPtL5ro04Mq0rPOsMzl7q5hIbRs2wD1A=="],
|
||||||
|
|
||||||
"fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="],
|
"fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="],
|
||||||
@@ -638,6 +661,8 @@
|
|||||||
|
|
||||||
"hono": ["hono@4.12.12", "", {}, "sha512-p1JfQMKaceuCbpJKAPKVqyqviZdS0eUxH9v82oWo1kb9xjQ5wA6iP3FNVAPDFlz5/p7d45lO+BpSk1tuSZMF4Q=="],
|
"hono": ["hono@4.12.12", "", {}, "sha512-p1JfQMKaceuCbpJKAPKVqyqviZdS0eUxH9v82oWo1kb9xjQ5wA6iP3FNVAPDFlz5/p7d45lO+BpSk1tuSZMF4Q=="],
|
||||||
|
|
||||||
|
"idb": ["idb@8.0.3", "", {}, "sha512-LtwtVyVYO5BqRvcsKuB2iUMnHwPVByPCXFXOpuU96IZPPoPN6xjOGxZQ74pgSVVLQWtUOYgyeL4GE98BY5D3wg=="],
|
||||||
|
|
||||||
"js-tokens": ["js-tokens@4.0.0", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="],
|
"js-tokens": ["js-tokens@4.0.0", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="],
|
||||||
|
|
||||||
"jsesc": ["jsesc@3.1.0", "", { "bin": { "jsesc": "bin/jsesc" } }, "sha512-/sM3dO2FOzXjKQhJuo0Q173wf2KOo8t4I8vHy6lF9poUp7bKT0/NHE8fPX23PwfhnykfqnC2xRxOnVw5XuGIaA=="],
|
"jsesc": ["jsesc@3.1.0", "", { "bin": { "jsesc": "bin/jsesc" } }, "sha512-/sM3dO2FOzXjKQhJuo0Q173wf2KOo8t4I8vHy6lF9poUp7bKT0/NHE8fPX23PwfhnykfqnC2xRxOnVw5XuGIaA=="],
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ Tables will be created automatically with the `shade_server_*` prefix, so they c
|
|||||||
| `SHADE_CLEANUP_INTERVAL_HOURS` | `24` | Cleanup cycle interval |
|
| `SHADE_CLEANUP_INTERVAL_HOURS` | `24` | Cleanup cycle interval |
|
||||||
| `SHADE_LOG_LEVEL` | `info` | `debug` / `info` / `warn` / `error` |
|
| `SHADE_LOG_LEVEL` | `info` | `debug` / `info` / `warn` / `error` |
|
||||||
| `SHADE_OTEL_ENABLED` | unset | Set to `1`/`true` to enable OpenTelemetry tracing on `withTracer()`-configured deployments. See [`observability.md`](./observability.md). |
|
| `SHADE_OTEL_ENABLED` | unset | Set to `1`/`true` to enable OpenTelemetry tracing on `withTracer()`-configured deployments. See [`observability.md`](./observability.md). |
|
||||||
|
| `SHADE_DISABLE_RATE_LIMIT` | unset | Set to `1` to disable IP rate-limits on every prekey + inbox route. **Single-tenant deployments only** — multi-tenant relays must leave this unset to keep the abuse defenses on. |
|
||||||
|
|
||||||
## Health and observability
|
## Health and observability
|
||||||
|
|
||||||
|
|||||||
173
docs/shade-identity-public-key-accessor.md
Normal file
173
docs/shade-identity-public-key-accessor.md
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
# Feature Request — Public accessor for the device's identity public key
|
||||||
|
|
||||||
|
**To**: Shade SDK team
|
||||||
|
**From**: Dispatch (browser-based Shade consumer)
|
||||||
|
**Target**: Shade SDK 4.4.x (or whichever release vehicle fits)
|
||||||
|
**Priority**: medium — unblocks real per-device fingerprint binding at
|
||||||
|
enrollment time; consumers ship with placeholder keys until then
|
||||||
|
|
||||||
|
Thanks for shipping `@shade/storage-indexeddb` so quickly — that unblocked
|
||||||
|
Dispatch's Slice 2.5 (persistent browser identity). During integration we
|
||||||
|
hit one more gap that's worth raising as a separate FR.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
Expose the local device's Ed25519 identity public key as a public accessor
|
||||||
|
on `Shade`, so applications can hand it to their own backend at enrollment
|
||||||
|
time for per-device verification, audit, or peer-fingerprint computation.
|
||||||
|
|
||||||
|
Today the SDK exposes `myAddress` and `fingerprint`, but the underlying
|
||||||
|
identity public key — the cryptographic root that everything else binds
|
||||||
|
to — is reachable only via the private `this.storage.getIdentityKeyPair()`
|
||||||
|
call inside `Shade`. Consumers building enrollment flows have no way to
|
||||||
|
hand the real key over.
|
||||||
|
|
||||||
|
## Problem
|
||||||
|
|
||||||
|
A common pattern in Shade-using apps is:
|
||||||
|
|
||||||
|
1. Browser device generates Shade identity (via `createShade`)
|
||||||
|
2. User enters an enrollment token
|
||||||
|
3. Browser POSTs to its backend: `{ token, address, identityPublicKey, ... }`
|
||||||
|
4. Backend records the device, computes/stores a safety number from the
|
||||||
|
identity key, opens a Shade session
|
||||||
|
|
||||||
|
Step 3 today has nowhere to get a real `identityPublicKey` from. Dispatch
|
||||||
|
currently sends `crypto.getRandomValues(new Uint8Array(32))` formatted as
|
||||||
|
hex, with this comment in the source:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
/**
|
||||||
|
* The browser submits a hex public-key field at enroll time so the schema
|
||||||
|
* stays stable. Wiring this to the SDK-generated identity key requires a
|
||||||
|
* Shade SDK addition (no public accessor for the raw identity key today).
|
||||||
|
*/
|
||||||
|
export function generatePlaceholderIdentityPublicKey(): string { ... }
|
||||||
|
```
|
||||||
|
|
||||||
|
The backend stores this placeholder but cannot verify anything against it,
|
||||||
|
because it's not actually the device's key. Real cryptographic binding is
|
||||||
|
deferred until the SDK exposes the underlying key.
|
||||||
|
|
||||||
|
## Why `fingerprint` isn't sufficient
|
||||||
|
|
||||||
|
`Shade.fingerprint` returns a 12-groups-of-5-digits safety-number string
|
||||||
|
designed for human side-channel comparison. That's the right output for a
|
||||||
|
"compare these digits with your friend" UX, but it's a derived format, not
|
||||||
|
the raw key. Backends that want to:
|
||||||
|
|
||||||
|
- Store the key for later signature verification
|
||||||
|
- Compute their own safety-number representation (Dispatch uses
|
||||||
|
`deterministicSafetyNumber(localAddr, peerAddr)` based on the raw bytes)
|
||||||
|
- Re-derive the fingerprint after an identity rotation
|
||||||
|
|
||||||
|
…all need access to the raw 32-byte Ed25519 public key.
|
||||||
|
|
||||||
|
## Proposed API
|
||||||
|
|
||||||
|
Add a single async accessor on `Shade`:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
class Shade {
|
||||||
|
/**
|
||||||
|
* The local device's Ed25519 identity public key (32 bytes).
|
||||||
|
*
|
||||||
|
* Stable for the lifetime of the identity. After `rotateIdentity()`
|
||||||
|
* this returns the new key; the old key is preserved in retired-
|
||||||
|
* identities storage for the configured grace period.
|
||||||
|
*/
|
||||||
|
get identityPublicKey(): Promise<Uint8Array>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Internally:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
get identityPublicKey(): Promise<Uint8Array> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
return this.storage.getIdentityKeyPair().then((kp) => {
|
||||||
|
if (!kp) throw new Error('Identity not yet generated');
|
||||||
|
return kp.publicKey;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If returning a getter that produces a Promise feels off, the equivalent
|
||||||
|
method form is fine:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
async getIdentityPublicKey(): Promise<Uint8Array> { ... }
|
||||||
|
```
|
||||||
|
|
||||||
|
Either shape works for consumers — pick whichever matches existing SDK
|
||||||
|
conventions.
|
||||||
|
|
||||||
|
## Alternative considered: object accessor
|
||||||
|
|
||||||
|
Returning an object would leave room to expose other identity-related
|
||||||
|
fields later without a breaking change:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
get identity(): Promise<{
|
||||||
|
address: string;
|
||||||
|
publicKey: Uint8Array;
|
||||||
|
fingerprint: string;
|
||||||
|
// future: registrationId, createdAt, etc.
|
||||||
|
}>;
|
||||||
|
```
|
||||||
|
|
||||||
|
This would mildly duplicate `myAddress` + `fingerprint`, but consolidates
|
||||||
|
identity-related accessors. Not a blocker for shipping the simpler
|
||||||
|
single-purpose accessor — just flagging the option in case the SDK is
|
||||||
|
considering broader API ergonomics.
|
||||||
|
|
||||||
|
## What this unblocks
|
||||||
|
|
||||||
|
For Dispatch specifically:
|
||||||
|
|
||||||
|
- Real device identity binding at enrollment (replaces the placeholder
|
||||||
|
`crypto.randomUUID()`-derived hex bytes the backend currently stores)
|
||||||
|
- Server-side `computePeerSafetyNumber()` can use the real key instead of
|
||||||
|
the deterministic-from-address stand-in (`shade-identity-provider.ts:151`)
|
||||||
|
- Future signature-based device authentication (sign a challenge with the
|
||||||
|
device's identity key during enrollment) becomes possible without
|
||||||
|
another SDK round
|
||||||
|
|
||||||
|
For other Shade consumers:
|
||||||
|
|
||||||
|
- Any app that hands a device key to its own backend for enrollment —
|
||||||
|
multi-device pairing flows, contact verification UIs, push-notification
|
||||||
|
targeting — gets an actual key to work with
|
||||||
|
|
||||||
|
## Acceptance criteria
|
||||||
|
|
||||||
|
1. New accessor exposed on `Shade` (getter or async method, SDK's
|
||||||
|
preference)
|
||||||
|
2. Returns the 32-byte Ed25519 identity public key
|
||||||
|
3. Returns the **current** key after `rotateIdentity()`
|
||||||
|
4. Throws (or resolves to a clear error) when called before
|
||||||
|
`initialize()` completes
|
||||||
|
5. Documented alongside `myAddress` and `fingerprint` in the SDK reference
|
||||||
|
6. One unit test in `shade-sdk` confirming the returned bytes match what
|
||||||
|
`storage.getIdentityKeyPair()` holds
|
||||||
|
7. Mentioned in the changelog under the next release
|
||||||
|
|
||||||
|
## Out of scope
|
||||||
|
|
||||||
|
- **Private key access** — consumers should never need it; signature
|
||||||
|
operations go through `shade.send()` etc. Don't expose the secret half.
|
||||||
|
- **Cross-peer key lookup** — getting *another* peer's identity key is a
|
||||||
|
separate concern (related to peer-verification storage), not what this
|
||||||
|
FR is about. This is strictly the local device's own key.
|
||||||
|
- **Format conversions** — base64/hex/PEM helpers don't belong in the SDK.
|
||||||
|
Consumers can encode the raw bytes however their wire format requires.
|
||||||
|
|
||||||
|
## Why this can't be done in consumer-land
|
||||||
|
|
||||||
|
The identity keypair is generated by `MemoryStorage` / `SQLiteStorage` /
|
||||||
|
`IndexedDBStorage` and consumed by `ShadeSessionManager`. Consumers can't
|
||||||
|
reach into either layer without breaching the SDK's encapsulation. A
|
||||||
|
public accessor is the only path that doesn't require monkey-patching
|
||||||
|
private fields.
|
||||||
191
docs/shade-storage-indexeddb.md
Normal file
191
docs/shade-storage-indexeddb.md
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
# Feature Request — `@shade/storage-indexeddb`
|
||||||
|
|
||||||
|
**To**: Shade SDK team
|
||||||
|
**From**: Dispatch (browser-based Shade consumer)
|
||||||
|
**Target**: Shade SDK 4.3.x (or whichever release vehicle fits)
|
||||||
|
**Priority**: blocks all browser-based Shade apps from achieving session
|
||||||
|
persistence across tab refresh
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
Ship an official IndexedDB-backed `StorageProvider` adapter as a new
|
||||||
|
workspace package `@shade/storage-indexeddb`, so browser-based Shade SDK
|
||||||
|
consumers can persist identity, prekeys, sessions, and peer-verification
|
||||||
|
state across tab refresh and browser restart — the same way `@shade/storage-sqlite`
|
||||||
|
does for Node and `@shade/storage-postgres` does for server deployments.
|
||||||
|
|
||||||
|
## Problem
|
||||||
|
|
||||||
|
Today the Shade SDK ships three storage paths:
|
||||||
|
|
||||||
|
| spec | adapter | environment |
|
||||||
|
| --------------------------------- | ------------------------ | ---------------- |
|
||||||
|
| `"memory"` | `MemoryStorage` (in-SDK) | tests, ephemeral |
|
||||||
|
| `"sqlite:/path"` | `@shade/storage-sqlite` | Node |
|
||||||
|
| `{ type: 'postgres', url: '…' }` | `@shade/storage-postgres`| Node servers |
|
||||||
|
|
||||||
|
There is **no browser-storage option**. The only way to run Shade in a
|
||||||
|
browser today is `storage: "memory"`, which means:
|
||||||
|
|
||||||
|
- Identity keypair regenerates on every page load
|
||||||
|
- Sessions reset → re-enrollment after every refresh
|
||||||
|
- `getLocalRegistrationId()` returns a fresh value → `device:${id}`
|
||||||
|
address changes → server-side device record orphaned every reload
|
||||||
|
|
||||||
|
This forces every browser-based Shade app to either (a) accept the broken
|
||||||
|
UX, or (b) build their own `StorageProvider` from scratch — duplicating
|
||||||
|
~25 methods × N consumers, with no shared conformance test surface.
|
||||||
|
|
||||||
|
The right place to solve this is at the SDK level, exactly mirroring how
|
||||||
|
SQLite and Postgres are handled.
|
||||||
|
|
||||||
|
## Proposed package
|
||||||
|
|
||||||
|
`packages/shade-storage-indexeddb/` — modeled directly after
|
||||||
|
`packages/shade-storage-sqlite/`. Same package shape, same test layout,
|
||||||
|
same `@shade/core`-only runtime dependency surface.
|
||||||
|
|
||||||
|
### Public API
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// @shade/storage-indexeddb
|
||||||
|
export class IndexedDBStorage implements StorageProvider {
|
||||||
|
/**
|
||||||
|
* Open (or create) the IndexedDB database. Idempotent — repeated calls
|
||||||
|
* with the same dbName return a connection sharing the same object stores.
|
||||||
|
*/
|
||||||
|
static async create(opts?: { dbName?: string }): Promise<IndexedDBStorage>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleanly close the underlying connection. Future calls will reopen.
|
||||||
|
* Called by Shade.shutdown() when consumers register cleanup.
|
||||||
|
*/
|
||||||
|
async close(): Promise<void>;
|
||||||
|
|
||||||
|
// ─── all StorageProvider methods (identity, prekeys, sessions,
|
||||||
|
// retired identities, peer verifications, optional stream-state) ───
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`dbName` defaults to something like `"shade"`. Consumers like Dispatch
|
||||||
|
will pass distinct names per app (`"dispatch-dashboard-shade"`,
|
||||||
|
`"dispatch-host-ui-shade"`) so DevTools' IndexedDB inspector groups them
|
||||||
|
sensibly, even though origin-isolation already makes the data isolated.
|
||||||
|
|
||||||
|
### SDK integration
|
||||||
|
|
||||||
|
`@shade/sdk` `resolveStorage()` gets a fourth branch:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
if (typeof spec === 'object' && spec.type === 'indexeddb') {
|
||||||
|
const moduleId = '@shade/storage-indexeddb';
|
||||||
|
const mod = (await import(moduleId)) as {
|
||||||
|
IndexedDBStorage: { create(opts: { dbName?: string }): Promise<StorageProvider> };
|
||||||
|
};
|
||||||
|
return mod.IndexedDBStorage.create({ dbName: spec.dbName });
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Dynamic import keeps `@shade/storage-indexeddb` an optional dependency,
|
||||||
|
matching the Postgres pattern — Node-only consumers don't need to install
|
||||||
|
a browser-only adapter.
|
||||||
|
|
||||||
|
Consumer surface:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
const shade = await createShade({
|
||||||
|
prekeyServer: 'https://…/shade-prekey',
|
||||||
|
storage: { type: 'indexeddb', dbName: 'my-app-shade' },
|
||||||
|
address: 'device:user@example.com', // optional — falls back to device:${registrationId}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Implementation guidance (non-prescriptive)
|
||||||
|
|
||||||
|
- **IDB wrapper**: suggest `idb` (Jake Archibald's thin wrapper, well-typed,
|
||||||
|
zero deps). Avoid Dexie or idb-keyval — we want full schema control to
|
||||||
|
match the SQL adapters' explicit schemas.
|
||||||
|
- **Object-store layout**: one store per StorageProvider category
|
||||||
|
(`identity`, `signedPreKeys`, `oneTimePreKeys`, `sessions`,
|
||||||
|
`trustedIdentities`, `retiredIdentities`, `peerVerifications`,
|
||||||
|
`streamStates`). Keypaths match the natural keys (`keyId`, `address`,
|
||||||
|
`streamId`).
|
||||||
|
- **Schema version**: integer, bumped on every shape change. Migrations
|
||||||
|
in `db.upgrade(...)` callback. Document schema-history alongside
|
||||||
|
the SQLite schema.
|
||||||
|
- **Concurrency**: IndexedDB transactions are auto-committing — the adapter
|
||||||
|
must keep operations within a single transaction where SQL adapters do.
|
||||||
|
Particular care for `bumpPeerIdentityVersion` (atomic read-modify-write).
|
||||||
|
- **Stream-state methods**: implement them. Browser apps will increasingly
|
||||||
|
use `@shade/transfer` for large file resume, and parity with SQLite's
|
||||||
|
capabilities matters.
|
||||||
|
|
||||||
|
## Test expectations
|
||||||
|
|
||||||
|
Mirror `packages/shade-storage-sqlite/tests/`:
|
||||||
|
|
||||||
|
- `indexeddb-storage.test.ts` — full StorageProvider surface (identity,
|
||||||
|
sessions, trusted identities, retired identities)
|
||||||
|
- `indexeddb-prekey-store.test.ts` — signed + one-time prekey lifecycle
|
||||||
|
- `peer-verifications.test.ts` — verification CRUD + identity-version
|
||||||
|
bumping invariants
|
||||||
|
- `indexeddb-stream-state.test.ts` (if stream-state is implemented)
|
||||||
|
|
||||||
|
Use **`fake-indexeddb`** for the Node test environment — it's the
|
||||||
|
established standard, supports the v3 spec, and lets us run IDB tests
|
||||||
|
in `bun test` / `vitest` / `jest` without a real browser.
|
||||||
|
|
||||||
|
If/when Shade gains a shared `StorageProvider` conformance test suite,
|
||||||
|
this adapter should consume it directly. Until then, follow the SQLite
|
||||||
|
adapter's per-method coverage style.
|
||||||
|
|
||||||
|
## Acceptance criteria
|
||||||
|
|
||||||
|
1. `@shade/storage-indexeddb` published at version 4.3.0 (or whichever
|
||||||
|
matches the next Shade release)
|
||||||
|
2. `@shade/sdk` `resolveStorage()` resolves `{ type: 'indexeddb', dbName? }`
|
||||||
|
via dynamic import
|
||||||
|
3. Full StorageProvider conformance in tests (identity, prekeys,
|
||||||
|
sessions, retired identities, peer verifications, stream-state)
|
||||||
|
4. Documented in Shade docs alongside SQLite/Postgres adapters
|
||||||
|
5. README example showing browser-app integration
|
||||||
|
6. Bundle-size note: dynamic-imported IDB module shouldn't pull crypto
|
||||||
|
dependencies — adapter should be ≤ ~10 KB minified+gzipped
|
||||||
|
|
||||||
|
## Out of scope (deferred)
|
||||||
|
|
||||||
|
- **Encryption-at-rest** for the IDB contents — separate work item; should
|
||||||
|
match the deviceKey-AES-GCM pattern Shade already uses for `secretEnc`
|
||||||
|
in `PersistedStreamState`. This adapter ships unencrypted-at-rest in v1
|
||||||
|
(consistent with SQLite), with the encryption layer added uniformly to
|
||||||
|
all adapters later.
|
||||||
|
- **Cross-tab BroadcastChannel sync** — IDB is shared across same-origin
|
||||||
|
tabs already; concurrent writes work via IDB transactions. Real-time
|
||||||
|
notification across tabs (e.g. "session was rotated in another tab") is
|
||||||
|
a separate concern, not storage-adapter scope.
|
||||||
|
- **Quota handling** — IDB quota for a Shade keystore is far below realistic
|
||||||
|
browser quotas. If it ever becomes relevant, add a `QuotaExceededError`
|
||||||
|
observability hook then.
|
||||||
|
|
||||||
|
## Why this can't be done in consumer-land
|
||||||
|
|
||||||
|
Building this in Dispatch (or any other consumer) would mean:
|
||||||
|
|
||||||
|
- 25+ method `StorageProvider` re-implementation per consumer
|
||||||
|
- No shared conformance tests
|
||||||
|
- Schema drift across consumers — each would invent its own object-store
|
||||||
|
shape, blocking any future cross-app data import/export
|
||||||
|
- Every Shade SDK update that adjusts `StorageProvider` would force every
|
||||||
|
consumer to track and patch independently
|
||||||
|
|
||||||
|
`@shade/storage-sqlite` and `@shade/storage-postgres` are part of the SDK
|
||||||
|
for the same reason. IndexedDB belongs alongside them.
|
||||||
|
|
||||||
|
## What unblocks
|
||||||
|
|
||||||
|
Shipping this unblocks Dispatch's "Slice 2.5" — persistent enrollment
|
||||||
|
across browser refresh, which today is the largest QA-friction point in
|
||||||
|
the dev loop. Any future browser-Shade consumer (web dashboard,
|
||||||
|
contact-list app, browser-extension messenger) gets persistence for free.
|
||||||
128
docs/streaming-sessions.md
Normal file
128
docs/streaming-sessions.md
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
# Streaming Double-Ratchet sub-sessions (V4.11)
|
||||||
|
|
||||||
|
`ShadeStream` wraps individual frames on a long-lived, high-frequency,
|
||||||
|
often one-directional channel (e.g. a server→client console-log
|
||||||
|
WebSocket) in an **independent** Double Ratchet derived from — but never
|
||||||
|
mutating — an already-established parent Shade session.
|
||||||
|
|
||||||
|
This is the answer to Vyvern FR `shade-ws-streaming-ratchet.md`. It is a
|
||||||
|
first-class API, *not* the "documented contract that `send`/`receive` is
|
||||||
|
safe per-frame" fallback: the Double-Ratchet crypto was already safe for
|
||||||
|
that access pattern, but the `send`/`receive` wrapper layer was not
|
||||||
|
(per-frame keystore writes; a shared per-peer mutex and a single stored
|
||||||
|
session row coupling the stream to the HTTP path). `ShadeStream` keeps
|
||||||
|
the proven ratchet and fixes the wrapper.
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
Transport-agnostic, exactly like `send`/`receive`: it emits/consumes
|
||||||
|
wire bytes; you own the WebSocket.
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// Initiator (the side that calls openStream)
|
||||||
|
const stream = await shade.openStream(peerAddr);
|
||||||
|
ws.send(stream.handshakeFrame()); // → STREAM_OPEN
|
||||||
|
// first inbound WS frame is the peer's STREAM_OPEN_ACK:
|
||||||
|
await stream.handleHandshake(ackBytes); // stream now usable
|
||||||
|
ws.send(await stream.seal(utf8(logLine))); // outbound frame
|
||||||
|
onLog(await stream.open(inboundBytes)); // inbound frame
|
||||||
|
await stream.close(); // on ws close/error
|
||||||
|
|
||||||
|
// Responder
|
||||||
|
const stream = await shade.acceptStream(peerAddr, openBytes); // usable now
|
||||||
|
ws.send(stream.handshakeFrame()); // → STREAM_OPEN_ACK
|
||||||
|
// open()/seal() as above
|
||||||
|
```
|
||||||
|
|
||||||
|
Route inbound bytes with `inspectEnvelopeType()`:
|
||||||
|
`'stream-open' | 'stream-open-ack' | 'stream-frame'`.
|
||||||
|
|
||||||
|
## Seeding (no prekey-server round trip)
|
||||||
|
|
||||||
|
The stream root key is derived from an identity-bound **3-DH** exchange
|
||||||
|
— the X3DH pattern minus signed/one-time prekeys, because the peer's
|
||||||
|
identity is *already* mutually pinned by the parent session's TOFU. Two
|
||||||
|
ephemerals are exchanged inside the transport (`STREAM_OPEN` /
|
||||||
|
`STREAM_OPEN_ACK`); no prekey server is involved.
|
||||||
|
|
||||||
|
```
|
||||||
|
slotA = DH(initiatorEphemeral, responderIdentity) — authenticates responder
|
||||||
|
slotB = DH(initiatorIdentity, responderEphemeral) — authenticates initiator
|
||||||
|
slotC = DH(initiatorEphemeral, responderEphemeral) — ephemeral forward secrecy
|
||||||
|
SK = HKDF(ikm = slotA‖slotB‖slotC, salt = streamId, info = "ShadeStream/v1")
|
||||||
|
```
|
||||||
|
|
||||||
|
Both peers compute the identical three scalars regardless of role.
|
||||||
|
`SK` then bootstraps a textbook Double Ratchet by handing the
|
||||||
|
responder's ephemeral to `initSenderSession`/`initReceiverSession`
|
||||||
|
exactly the way X3DH hands its signed prekey to the ratchet — so
|
||||||
|
`ratchetEncrypt`/`ratchetDecrypt` and every guarantee they carry apply
|
||||||
|
unchanged.
|
||||||
|
|
||||||
|
## Security contract (answers FR R1–R7)
|
||||||
|
|
||||||
|
- **R1 — same properties as `send`/`receive`.** Each frame is one
|
||||||
|
`ratchetEncrypt`/`ratchetDecrypt` over the *same* crypto as the HTTP
|
||||||
|
path: AES-256-GCM confidentiality, per-frame forward secrecy via the
|
||||||
|
one-way HMAC chain-key KDF with in-place zeroize of the spent chain
|
||||||
|
key, and replay/rewind rejection (a re-delivered or counter-rewound
|
||||||
|
frame fails closed). The handshake is mutually authenticated against
|
||||||
|
the identities the parent session already pinned.
|
||||||
|
- **R2 — one-directional resilience.** A long server→client burst with
|
||||||
|
no client traffic only advances the symmetric sending chain (no DH
|
||||||
|
step until the peer replies — standard Double Ratchet). Forward
|
||||||
|
secrecy holds per frame in this regime. Over an ordered transport
|
||||||
|
(WebSocket/TCP) zero keys are skipped per frame.
|
||||||
|
- **R3 — bounded memory.** Out-of-order arrivals are capped by the
|
||||||
|
ratchet's `MAX_SKIP` (1000) and `MAX_CACHED_SKIPPED_KEYS` (2000)
|
||||||
|
with oldest-key eviction. In-order delivery retains nothing. Verified
|
||||||
|
to stay at zero retained keys across a 5000-frame burst.
|
||||||
|
- **R4 — browser parity.** Identical API and guarantees in the browser
|
||||||
|
SDK: `ShadeStream` is on the same `Shade` class over the same
|
||||||
|
`CryptoProvider` (`SubtleCryptoProvider`), so the IndexedDB-backed
|
||||||
|
build behaves identically to the `sqlite:` server build. No storage
|
||||||
|
is touched at all (see R7), so the keystore backend is irrelevant.
|
||||||
|
- **R5 — independent lifecycle.** The stream ratchet is derived without
|
||||||
|
reading or writing the stored parent `SessionState`, runs on its own
|
||||||
|
private op-mutex (not the per-peer `send`/`receive` queues), and is
|
||||||
|
zeroized on `close()`. Opening, using for thousands of frames, and
|
||||||
|
closing a stream leaves the parent session byte-identical; the HTTP
|
||||||
|
path keeps working concurrently against the same peer. Each
|
||||||
|
`openStream` gets a fresh `streamId` and an independent root, so
|
||||||
|
concurrent streams to one peer never share key material.
|
||||||
|
- **R6 — wire framing.** `@shade/proto` defines `STREAM_OPEN` (0x31),
|
||||||
|
`STREAM_OPEN_ACK` (0x32), `STREAM_FRAME` (0x33). A `STREAM_FRAME`
|
||||||
|
carries one Double-Ratchet message via the exact ratchet inner codec
|
||||||
|
the HTTP path uses. One sealed logical frame ⇒ one self-delimiting
|
||||||
|
wire frame ⇒ one WS text/binary frame.
|
||||||
|
- **R7 — performance.** The ratchet lives **only in memory and is never
|
||||||
|
persisted**. There is therefore *zero* per-frame storage I/O — the
|
||||||
|
per-frame cost is exactly the symmetric KDF + one AES-GCM, the same
|
||||||
|
primitives the HTTP path runs. This is strictly better than the
|
||||||
|
"doubled CPU" the Vyvern roadmap budgeted, because the dominant cost
|
||||||
|
the naive `send`/`receive`-per-frame approach would have paid (a
|
||||||
|
`saveSession` keystore write per frame) is eliminated, not doubled.
|
||||||
|
Not persisting is also a *security* property, not a shortcut: writing
|
||||||
|
evolving per-frame ratchet secrets to disk would defeat forward
|
||||||
|
secrecy. A dropped/reconnected stream is re-opened with a fresh
|
||||||
|
handshake, never resumed.
|
||||||
|
|
||||||
|
## Double-Ratchet ordering note
|
||||||
|
|
||||||
|
A responder cannot `seal()` until it has `open()`ed at least one frame
|
||||||
|
from the initiator (standard Signal behaviour — the responder has no
|
||||||
|
sending chain until the first DH step). For a server-heavy stream
|
||||||
|
either make the bursty data sender the **initiator**, or have the
|
||||||
|
initiator send one priming frame immediately after the handshake.
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
- `packages/shade-core/tests/stream.test.ts` — handshake agreement,
|
||||||
|
frame round-trips, 5000-frame one-directional burst (bounded skipped
|
||||||
|
keys + forward-secrecy zeroize), parent-session independence (R5),
|
||||||
|
replay/rewind rejection, mutual authentication against pinned
|
||||||
|
identities, `close()` zeroize/idempotence.
|
||||||
|
- `packages/shade-proto/tests/stream-wire.test.ts` — wire round-trips
|
||||||
|
and type-tag/length rejection for all three stream frame kinds.
|
||||||
|
</content>
|
||||||
|
</invoke>
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/cli",
|
"name": "@shade/cli",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/cli.ts",
|
"main": "src/cli.ts",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/core",
|
"name": "@shade/core",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -107,6 +107,31 @@ export class FingerprintNotVerifiedError extends ShadeError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown when `seal()` / `open()` is called on a {@link StreamRatchet}
|
||||||
|
* that has already been torn down via `close()`. The stream's ratchet
|
||||||
|
* secrets have been zeroized and cannot be revived — open a fresh
|
||||||
|
* stream instead.
|
||||||
|
*/
|
||||||
|
export class StreamClosedError extends ShadeError {
|
||||||
|
constructor(message = 'Stream is closed') {
|
||||||
|
super('SHADE_STREAM_CLOSED', message);
|
||||||
|
this.name = 'StreamClosedError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown when a stream handshake frame is malformed, arrives in the
|
||||||
|
* wrong order, or references a streamId that does not match the stream
|
||||||
|
* it was fed to.
|
||||||
|
*/
|
||||||
|
export class StreamHandshakeError extends ShadeError {
|
||||||
|
constructor(message = 'Stream handshake failed') {
|
||||||
|
super('SHADE_STREAM_HANDSHAKE', message);
|
||||||
|
this.name = 'StreamHandshakeError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Infrastructure Errors ───────────────────────────────────
|
// ─── Infrastructure Errors ───────────────────────────────────
|
||||||
|
|
||||||
export class NetworkError extends ShadeError {
|
export class NetworkError extends ShadeError {
|
||||||
@@ -158,6 +183,30 @@ export class UnauthorizedError extends ShadeError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 409 Conflict — caller wrote to a resource that already exists without
|
||||||
|
* supplying an If-Match precondition. V4.9: the encrypted blob primitive
|
||||||
|
* uses this to force read-then-write on already-occupied slots.
|
||||||
|
*/
|
||||||
|
export class ConflictError extends ShadeError {
|
||||||
|
constructor(message = 'Conflict') {
|
||||||
|
super('SHADE_CONFLICT', message);
|
||||||
|
this.name = 'ConflictError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 412 Precondition Failed — caller supplied an If-Match etag that does
|
||||||
|
* not match the current state. V4.9: the encrypted blob primitive uses
|
||||||
|
* this to surface stale-CAS so clients can re-read, merge, and retry.
|
||||||
|
*/
|
||||||
|
export class PreconditionFailedError extends ShadeError {
|
||||||
|
constructor(message = 'Precondition failed') {
|
||||||
|
super('SHADE_PRECONDITION_FAILED', message);
|
||||||
|
this.name = 'PreconditionFailedError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Error → HTTP Status Mapping ────────────────────────────
|
// ─── Error → HTTP Status Mapping ────────────────────────────
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -180,7 +229,10 @@ export function errorToHttpStatus(error: unknown): number {
|
|||||||
return 400;
|
return 400;
|
||||||
case 'SHADE_REPLAY':
|
case 'SHADE_REPLAY':
|
||||||
case 'SHADE_DUPLICATE_MESSAGE':
|
case 'SHADE_DUPLICATE_MESSAGE':
|
||||||
|
case 'SHADE_CONFLICT':
|
||||||
return 409;
|
return 409;
|
||||||
|
case 'SHADE_PRECONDITION_FAILED':
|
||||||
|
return 412;
|
||||||
case 'SHADE_RATE_LIMIT':
|
case 'SHADE_RATE_LIMIT':
|
||||||
return 429;
|
return 429;
|
||||||
case 'SHADE_TIMEOUT':
|
case 'SHADE_TIMEOUT':
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ export interface ShadeEventMap {
|
|||||||
'identity.rotated': { newFingerprint: string };
|
'identity.rotated': { newFingerprint: string };
|
||||||
'session.created': { address: string; remoteIdentityKeyHash: string };
|
'session.created': { address: string; remoteIdentityKeyHash: string };
|
||||||
'session.removed': { address: string };
|
'session.removed': { address: string };
|
||||||
|
/** V4.8.3 — emitted when `aliasSession` moves a peer's per-peer state. */
|
||||||
|
'session.aliased': { oldLabel: string; newLabel: string };
|
||||||
'message.encrypted': { address: string; counter: number; ciphertextSize: number };
|
'message.encrypted': { address: string; counter: number; ciphertextSize: number };
|
||||||
'message.decrypted': { address: string; counter: number; plaintextSize: number };
|
'message.decrypted': { address: string; counter: number; plaintextSize: number };
|
||||||
'ratchet.dh_step': { address: string };
|
'ratchet.dh_step': { address: string };
|
||||||
@@ -34,6 +36,10 @@ export interface ShadeEventMap {
|
|||||||
'signed_prekey.rotated': { oldKeyId: number; newKeyId: number };
|
'signed_prekey.rotated': { oldKeyId: number; newKeyId: number };
|
||||||
'trust.pinned': { address: string; identityKeyHash: string };
|
'trust.pinned': { address: string; identityKeyHash: string };
|
||||||
'trust.changed': { address: string; oldKeyHash: string; newKeyHash: string };
|
'trust.changed': { address: string; oldKeyHash: string; newKeyHash: string };
|
||||||
|
/** V4.11 — a streaming sub-ratchet handshake completed. */
|
||||||
|
'stream.opened': { address: string; role: 'initiator' | 'responder' };
|
||||||
|
/** V4.11 — a streaming sub-ratchet was torn down and zeroized. */
|
||||||
|
'stream.closed': { address: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ShadeEventName = keyof ShadeEventMap;
|
export type ShadeEventName = keyof ShadeEventMap;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export * from './keys.js';
|
|||||||
export * from './errors.js';
|
export * from './errors.js';
|
||||||
export * from './x3dh.js';
|
export * from './x3dh.js';
|
||||||
export * from './ratchet.js';
|
export * from './ratchet.js';
|
||||||
|
export * from './stream.js';
|
||||||
export { ShadeSessionManager, GRACE_PERIOD_MS } from './session.js';
|
export { ShadeSessionManager, GRACE_PERIOD_MS } from './session.js';
|
||||||
export * from './serialization.js';
|
export * from './serialization.js';
|
||||||
export * from './fingerprint.js';
|
export * from './fingerprint.js';
|
||||||
|
|||||||
@@ -23,7 +23,14 @@ import {
|
|||||||
ratchetEncrypt,
|
ratchetEncrypt,
|
||||||
ratchetDecrypt,
|
ratchetDecrypt,
|
||||||
} from './ratchet.js';
|
} from './ratchet.js';
|
||||||
import { NoSessionError } from './errors.js';
|
import {
|
||||||
|
deriveStreamRootKey,
|
||||||
|
bootstrapStreamSession,
|
||||||
|
StreamRatchet,
|
||||||
|
STREAM_ID_BYTES,
|
||||||
|
STREAM_EPHEMERAL_BYTES,
|
||||||
|
} from './stream.js';
|
||||||
|
import { NoSessionError, StreamHandshakeError } from './errors.js';
|
||||||
import { computeFingerprint, shortFingerprint } from './fingerprint.js';
|
import { computeFingerprint, shortFingerprint } from './fingerprint.js';
|
||||||
import { ShadeEventEmitter, shortHash } from './events.js';
|
import { ShadeEventEmitter, shortHash } from './events.js';
|
||||||
import {
|
import {
|
||||||
@@ -265,6 +272,73 @@ export class ShadeSessionManager {
|
|||||||
// Note: we keep the trusted identity; new session will verify against it.
|
// Note: we keep the trusted identity; new session will verify against it.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move every per-peer storage row for `oldLabel` (session, trusted
|
||||||
|
* identity, peer-verification, identity-version counter) to
|
||||||
|
* `newLabel`. Used to canonicalize sessions when first-contact
|
||||||
|
* forces the receiver to label by sender-fingerprint hint
|
||||||
|
* (`fp:<hex>`) and a later in-band announcement reveals the peer's
|
||||||
|
* canonical address.
|
||||||
|
*
|
||||||
|
* Holds the per-peer mutex on **both** labels for the whole
|
||||||
|
* migration so concurrent encrypt/decrypt for either label can't
|
||||||
|
* observe a half-moved state. Locks are taken in lexicographic
|
||||||
|
* order to avoid deadlocks if two callers alias in opposite
|
||||||
|
* directions.
|
||||||
|
*
|
||||||
|
* Throws if no session exists for `oldLabel`. Throws (refuses to
|
||||||
|
* overwrite) if a session already exists for `newLabel`. No-ops
|
||||||
|
* when `oldLabel === newLabel`.
|
||||||
|
*
|
||||||
|
* V4.8.3 — Prism FR `session-label-asymmetry-v4.8.2.md`.
|
||||||
|
*/
|
||||||
|
async aliasSession(oldLabel: string, newLabel: string): Promise<void> {
|
||||||
|
if (oldLabel === newLabel) return;
|
||||||
|
const [first, second] = oldLabel < newLabel ? [oldLabel, newLabel] : [newLabel, oldLabel];
|
||||||
|
await this.runUnderPeerLock(first, () =>
|
||||||
|
this.runUnderPeerLock(second, () => this.aliasUnderLock(oldLabel, newLabel)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async aliasUnderLock(oldLabel: string, newLabel: string): Promise<void> {
|
||||||
|
const session = await this.storage.getSession(oldLabel);
|
||||||
|
if (!session) throw new NoSessionError(oldLabel);
|
||||||
|
const collision = await this.storage.getSession(newLabel);
|
||||||
|
if (collision) {
|
||||||
|
throw new Error(
|
||||||
|
`aliasSession: refusing to overwrite an existing session for "${newLabel}". ` +
|
||||||
|
`If you want to replace it, call resetSession("${newLabel}") first.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Move the session.
|
||||||
|
await this.storage.saveSession(newLabel, session);
|
||||||
|
// Re-pin trust under the new label using the session's stored DH
|
||||||
|
// identity key — `saveTrustedIdentity` is the same primitive that
|
||||||
|
// the X3DH initiator/responder uses, and the DH key in `session`
|
||||||
|
// is the value that was pinned at session-establish time. The old
|
||||||
|
// pin under `oldLabel` is harmless leftover (the storage interface
|
||||||
|
// has no remove for trust pins) and would only be re-checked if a
|
||||||
|
// fresh X3DH against `oldLabel` somehow happened later.
|
||||||
|
await this.storage.saveTrustedIdentity(newLabel, session.remoteIdentityKey);
|
||||||
|
// Migrate the peer-verification record if present.
|
||||||
|
const verification = await this.storage.getPeerVerification(oldLabel);
|
||||||
|
if (verification) {
|
||||||
|
await this.storage.savePeerVerification({
|
||||||
|
...verification,
|
||||||
|
peerAddress: newLabel,
|
||||||
|
});
|
||||||
|
await this.storage.removePeerVerification(oldLabel);
|
||||||
|
}
|
||||||
|
// Carry the identity-version counter forward so peer rotation
|
||||||
|
// history is preserved.
|
||||||
|
const oldVersion = await this.storage.getPeerIdentityVersion(oldLabel);
|
||||||
|
for (let i = 1; i < oldVersion; i++) {
|
||||||
|
await this.storage.bumpPeerIdentityVersion(newLabel);
|
||||||
|
}
|
||||||
|
await this.storage.removeSession(oldLabel);
|
||||||
|
this.events?.emit('session.aliased', { oldLabel, newLabel });
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accept a changed remote identity. This should only be called after
|
* Accept a changed remote identity. This should only be called after
|
||||||
* verifying the new identity out-of-band (e.g., comparing fingerprints).
|
* verifying the new identity out-of-band (e.g., comparing fingerprints).
|
||||||
@@ -559,6 +633,121 @@ export class ShadeSessionManager {
|
|||||||
|
|
||||||
return dec.decode(plaintext);
|
return dec.decode(plaintext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Streaming sub-sessions (V4.11) ────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the peer's pinned identity X25519 key for a stream
|
||||||
|
* handshake. Requires an *already established* parent session — the
|
||||||
|
* stream is explicitly a "second channel on a known peer", never a
|
||||||
|
* first contact (so it needs no prekey-server round trip and inherits
|
||||||
|
* the parent's TOFU pin).
|
||||||
|
*/
|
||||||
|
private async streamIdentityMaterial(
|
||||||
|
address: string,
|
||||||
|
): Promise<{ selfIdentityDHPriv: Uint8Array; peerIdentityDHPub: Uint8Array }> {
|
||||||
|
if (!this.identity) throw new Error('Not initialized');
|
||||||
|
const session = await this.storage.getSession(address);
|
||||||
|
if (!session) throw new NoSessionError(address);
|
||||||
|
return {
|
||||||
|
selfIdentityDHPriv: this.identity.dhPrivateKey,
|
||||||
|
peerIdentityDHPub: session.remoteIdentityKey,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initiator side of a stream handshake. Generates the streamId and
|
||||||
|
* this side's ephemeral, and returns a `complete` continuation that
|
||||||
|
* derives the sub-ratchet once the responder's ephemeral arrives in
|
||||||
|
* the `STREAM_OPEN_ACK`.
|
||||||
|
*
|
||||||
|
* Touches neither the stored parent session nor the per-peer op
|
||||||
|
* queues (R5).
|
||||||
|
*/
|
||||||
|
async beginStream(address: string): Promise<{
|
||||||
|
streamId: Uint8Array;
|
||||||
|
ephemeralPublicKey: Uint8Array;
|
||||||
|
complete: (peerEphemeralPub: Uint8Array) => Promise<StreamRatchet>;
|
||||||
|
}> {
|
||||||
|
const { selfIdentityDHPriv, peerIdentityDHPub } =
|
||||||
|
await this.streamIdentityMaterial(address);
|
||||||
|
const streamId = this.crypto.randomBytes(STREAM_ID_BYTES);
|
||||||
|
const ephemeral = await this.crypto.generateX25519KeyPair();
|
||||||
|
|
||||||
|
const complete = async (peerEphemeralPub: Uint8Array): Promise<StreamRatchet> => {
|
||||||
|
if (peerEphemeralPub.length !== STREAM_EPHEMERAL_BYTES) {
|
||||||
|
throw new StreamHandshakeError(
|
||||||
|
`responder ephemeral must be ${STREAM_EPHEMERAL_BYTES} bytes`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const sk = await deriveStreamRootKey(
|
||||||
|
this.crypto,
|
||||||
|
'initiator',
|
||||||
|
streamId,
|
||||||
|
selfIdentityDHPriv,
|
||||||
|
peerIdentityDHPub,
|
||||||
|
ephemeral.privateKey,
|
||||||
|
peerEphemeralPub,
|
||||||
|
);
|
||||||
|
const session = await bootstrapStreamSession(this.crypto, 'initiator', sk, peerIdentityDHPub, {
|
||||||
|
publicKey: peerEphemeralPub,
|
||||||
|
privateKey: new Uint8Array(0),
|
||||||
|
});
|
||||||
|
this.crypto.zeroize(sk);
|
||||||
|
this.crypto.zeroize(ephemeral.privateKey);
|
||||||
|
this.events?.emit('stream.opened', { address, role: 'initiator' });
|
||||||
|
return new StreamRatchet(this.crypto, session, streamId);
|
||||||
|
};
|
||||||
|
|
||||||
|
return { streamId, ephemeralPublicKey: ephemeral.publicKey, complete };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Responder side of a stream handshake. Given the initiator's
|
||||||
|
* `STREAM_OPEN` (its streamId + ephemeral), derives the sub-ratchet
|
||||||
|
* immediately and returns this side's ephemeral for the
|
||||||
|
* `STREAM_OPEN_ACK`.
|
||||||
|
*/
|
||||||
|
async acceptStream(
|
||||||
|
address: string,
|
||||||
|
streamId: Uint8Array,
|
||||||
|
initiatorEphemeralPub: Uint8Array,
|
||||||
|
): Promise<{ ephemeralPublicKey: Uint8Array; stream: StreamRatchet }> {
|
||||||
|
if (streamId.length !== STREAM_ID_BYTES) {
|
||||||
|
throw new StreamHandshakeError(`streamId must be ${STREAM_ID_BYTES} bytes`);
|
||||||
|
}
|
||||||
|
if (initiatorEphemeralPub.length !== STREAM_EPHEMERAL_BYTES) {
|
||||||
|
throw new StreamHandshakeError(
|
||||||
|
`initiator ephemeral must be ${STREAM_EPHEMERAL_BYTES} bytes`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const { selfIdentityDHPriv, peerIdentityDHPub } =
|
||||||
|
await this.streamIdentityMaterial(address);
|
||||||
|
const ephemeral = await this.crypto.generateX25519KeyPair();
|
||||||
|
|
||||||
|
const sk = await deriveStreamRootKey(
|
||||||
|
this.crypto,
|
||||||
|
'responder',
|
||||||
|
streamId,
|
||||||
|
selfIdentityDHPriv,
|
||||||
|
peerIdentityDHPub,
|
||||||
|
ephemeral.privateKey,
|
||||||
|
initiatorEphemeralPub,
|
||||||
|
);
|
||||||
|
const session = await bootstrapStreamSession(
|
||||||
|
this.crypto,
|
||||||
|
'responder',
|
||||||
|
sk,
|
||||||
|
peerIdentityDHPub,
|
||||||
|
ephemeral,
|
||||||
|
);
|
||||||
|
this.crypto.zeroize(sk);
|
||||||
|
this.events?.emit('stream.opened', { address, role: 'responder' });
|
||||||
|
return {
|
||||||
|
ephemeralPublicKey: ephemeral.publicKey,
|
||||||
|
stream: new StreamRatchet(this.crypto, session, streamId),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function arraysEqual(a: Uint8Array, b: Uint8Array): boolean {
|
function arraysEqual(a: Uint8Array, b: Uint8Array): boolean {
|
||||||
|
|||||||
@@ -46,6 +46,50 @@ export interface PersistedStreamState {
|
|||||||
*/
|
*/
|
||||||
export type PeerVerificationSource = 'user' | 'transitive' | 'tofu-after-warning';
|
export type PeerVerificationSource = 'user' | 'transitive' | 'tofu-after-warning';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persisted broadcast-channel state (V4.6). Holds the sender-key chain that
|
||||||
|
* lets the channel owner encrypt a single ciphertext and fan it out to all
|
||||||
|
* paired members.
|
||||||
|
*
|
||||||
|
* - `ownerRole === 'sender'`: this device created the channel; the record
|
||||||
|
* carries the full chain (chainKey + iteration + signing keypair).
|
||||||
|
* - `ownerRole === 'receiver'`: this device joined a channel that
|
||||||
|
* `ownerAddress` owns; we hold a tracking copy of the chain (chainKey +
|
||||||
|
* iteration + signing public key only — no private signing key).
|
||||||
|
*
|
||||||
|
* `generation` is bumped each time `removeMember` rotates the chain. Stale
|
||||||
|
* broadcasts at lower generations are silently dropped on receive.
|
||||||
|
*/
|
||||||
|
export interface BroadcastChannelRecord {
|
||||||
|
/** Opaque, stable across restarts. UUID-style. */
|
||||||
|
channelId: string;
|
||||||
|
ownerRole: 'sender' | 'receiver';
|
||||||
|
/** The address that owns the channel (i.e. the only sender). */
|
||||||
|
ownerAddress: string;
|
||||||
|
label?: string;
|
||||||
|
/** Sender-key generation — bumped on rotation. Starts at 0. */
|
||||||
|
generation: number;
|
||||||
|
/** Current chain key (32 bytes). */
|
||||||
|
chainKey: Uint8Array;
|
||||||
|
/** Counter advanced by `senderKeyEncrypt` / `senderKeyDecrypt`. */
|
||||||
|
iteration: number;
|
||||||
|
/** Owner's Ed25519 signing public key (32 bytes). */
|
||||||
|
signingPublicKey: Uint8Array;
|
||||||
|
/** Owner's Ed25519 signing private key. Present iff `ownerRole === 'sender'`. */
|
||||||
|
signingPrivateKey?: Uint8Array;
|
||||||
|
createdAt: number;
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Membership row for a broadcast channel (sender-side only). */
|
||||||
|
export interface BroadcastMemberRecord {
|
||||||
|
channelId: string;
|
||||||
|
peerAddress: string;
|
||||||
|
joinedAt: number;
|
||||||
|
/** When this peer was revoked. `null` while still active. */
|
||||||
|
removedAt: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persistent record that a peer's safety number was verified at a point
|
* Persistent record that a peer's safety number was verified at a point
|
||||||
* in time. `identityVersion` is the local counter for that peer's identity:
|
* in time. `identityVersion` is the local counter for that peer's identity:
|
||||||
@@ -188,4 +232,35 @@ export interface StorageProvider {
|
|||||||
|
|
||||||
/** Prune stream-state rows in `'finished' | 'aborted'` status older than `olderThan`. */
|
/** Prune stream-state rows in `'finished' | 'aborted'` status older than `olderThan`. */
|
||||||
pruneStreamStates?(olderThan: number): Promise<void>;
|
pruneStreamStates?(olderThan: number): Promise<void>;
|
||||||
|
|
||||||
|
// ─── Broadcast channels (V4.6) — optional ─────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persist or replace the broadcast-channel record. Idempotent upsert on
|
||||||
|
* `channelId`. Backends that don't implement broadcast-channel support
|
||||||
|
* may omit this; the SDK throws a clear error when the app tries to
|
||||||
|
* call `createBroadcastChannel` on such a backend.
|
||||||
|
*/
|
||||||
|
saveBroadcastChannel?(channel: BroadcastChannelRecord): Promise<void>;
|
||||||
|
|
||||||
|
/** Look up a broadcast channel by id. */
|
||||||
|
getBroadcastChannel?(channelId: string): Promise<BroadcastChannelRecord | null>;
|
||||||
|
|
||||||
|
/** Enumerate all broadcast channels persisted on this device. */
|
||||||
|
listBroadcastChannels?(): Promise<BroadcastChannelRecord[]>;
|
||||||
|
|
||||||
|
/** Drop a broadcast channel and all its membership rows. */
|
||||||
|
removeBroadcastChannel?(channelId: string): Promise<void>;
|
||||||
|
|
||||||
|
/** Persist or replace a broadcast-membership row (sender-side only). */
|
||||||
|
saveBroadcastMember?(member: BroadcastMemberRecord): Promise<void>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List membership rows for a channel. Includes revoked members (with
|
||||||
|
* `removedAt !== null`); callers filter as needed.
|
||||||
|
*/
|
||||||
|
getBroadcastMembers?(channelId: string): Promise<BroadcastMemberRecord[]>;
|
||||||
|
|
||||||
|
/** Hard-delete a single membership row. */
|
||||||
|
removeBroadcastMember?(channelId: string, peerAddress: string): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|||||||
233
packages/shade-core/src/stream.ts
Normal file
233
packages/shade-core/src/stream.ts
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
import type { CryptoProvider } from './crypto.js';
|
||||||
|
import type { KeyPair, RatchetMessage, SessionState } from './types.js';
|
||||||
|
import {
|
||||||
|
initSenderSession,
|
||||||
|
initReceiverSession,
|
||||||
|
ratchetEncrypt,
|
||||||
|
ratchetDecrypt,
|
||||||
|
} from './ratchet.js';
|
||||||
|
import { StreamClosedError } from './errors.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Streaming Double-Ratchet sub-sessions (V4.11).
|
||||||
|
*
|
||||||
|
* Wraps a long-lived, high-frequency, often-one-directional channel
|
||||||
|
* (e.g. a server→client WebSocket log burst) in an *independent* Double
|
||||||
|
* Ratchet that is derived from — but never mutates — an already
|
||||||
|
* established parent Shade session.
|
||||||
|
*
|
||||||
|
* Why a sub-ratchet rather than reusing `ShadeSessionManager`:
|
||||||
|
*
|
||||||
|
* - **Independence (R5).** A stream gets its own root key, chains, DH
|
||||||
|
* ratchet and op-mutex. Opening/closing it never touches the stored
|
||||||
|
* parent `SessionState` nor serialises against the HTTP send/receive
|
||||||
|
* queue.
|
||||||
|
* - **Performance (R7).** The stream ratchet lives only in memory and
|
||||||
|
* is *never* written to the keystore. There is therefore zero
|
||||||
|
* per-frame storage I/O — the cost is purely the symmetric KDF +
|
||||||
|
* AES-GCM, the same primitives the HTTP path uses.
|
||||||
|
* - **Forward secrecy.** Not persisting the evolving ratchet state is
|
||||||
|
* a feature, not a shortcut: writing per-frame secrets to disk would
|
||||||
|
* actively defeat the forward-secrecy guarantee. A dropped/reconnected
|
||||||
|
* stream is re-opened with a fresh handshake, not resumed.
|
||||||
|
*
|
||||||
|
* ## Seeding (no prekey-server round trip)
|
||||||
|
*
|
||||||
|
* The stream root key is derived from an identity-bound 3-DH exchange —
|
||||||
|
* the X3DH pattern minus the signed / one-time prekeys, because the
|
||||||
|
* peer's identity is *already* mutually pinned by the parent session's
|
||||||
|
* TOFU. Two ephemeral keys are exchanged inside the transport itself
|
||||||
|
* (`STREAM_OPEN` / `STREAM_OPEN_ACK`); no prekey server is involved.
|
||||||
|
*
|
||||||
|
* slotA = DH(initiatorEphemeral, responderIdentity) — auth of responder
|
||||||
|
* slotB = DH(initiatorIdentity, responderEphemeral) — auth of initiator
|
||||||
|
* slotC = DH(initiatorEphemeral, responderEphemeral) — ephemeral FS
|
||||||
|
*
|
||||||
|
* SK = HKDF(ikm = slotA‖slotB‖slotC, salt = streamId, info = "ShadeStream/v1")
|
||||||
|
*
|
||||||
|
* Both peers compute the identical three scalars regardless of role, so
|
||||||
|
* `SK` agrees. An attacker lacking the responder's identity private key
|
||||||
|
* cannot form slotA; one lacking the initiator's cannot form slotB —
|
||||||
|
* the handshake is therefore mutually authenticated against the same
|
||||||
|
* identities the parent session already trusts.
|
||||||
|
*
|
||||||
|
* `SK` then bootstraps a textbook Double Ratchet by handing the
|
||||||
|
* responder's ephemeral to {@link initSenderSession} /
|
||||||
|
* {@link initReceiverSession} exactly the way X3DH hands its signed
|
||||||
|
* prekey to the ratchet — so `ratchetEncrypt` / `ratchetDecrypt` (and
|
||||||
|
* thus every R1–R3 guarantee they already carry) apply unchanged.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export type StreamRole = 'initiator' | 'responder';
|
||||||
|
|
||||||
|
/** Stream identifier length (bytes). 128 bits of collision resistance. */
|
||||||
|
export const STREAM_ID_BYTES = 16;
|
||||||
|
|
||||||
|
/** Ephemeral X25519 public-key length carried in the handshake. */
|
||||||
|
export const STREAM_EPHEMERAL_BYTES = 32;
|
||||||
|
|
||||||
|
const STREAM_KDF_INFO = new TextEncoder().encode('ShadeStream/v1');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derive the stream's independent root key from the identity-bound 3-DH
|
||||||
|
* exchange. Pure: never reads or mutates any `SessionState`.
|
||||||
|
*
|
||||||
|
* @param role which end of the handshake we are
|
||||||
|
* @param streamId 16-byte stream id (HKDF salt; binds the
|
||||||
|
* derivation so two concurrent streams to the
|
||||||
|
* same peer never share a root key)
|
||||||
|
* @param selfIdentityDHPriv our long-term identity X25519 private key
|
||||||
|
* @param peerIdentityDHPub peer's pinned identity X25519 public key
|
||||||
|
* (the value the parent session pinned)
|
||||||
|
* @param selfEphemeralPriv our per-stream ephemeral X25519 private key
|
||||||
|
* @param peerEphemeralPub peer's per-stream ephemeral X25519 public key
|
||||||
|
*/
|
||||||
|
export async function deriveStreamRootKey(
|
||||||
|
crypto: CryptoProvider,
|
||||||
|
role: StreamRole,
|
||||||
|
streamId: Uint8Array,
|
||||||
|
selfIdentityDHPriv: Uint8Array,
|
||||||
|
peerIdentityDHPub: Uint8Array,
|
||||||
|
selfEphemeralPriv: Uint8Array,
|
||||||
|
peerEphemeralPub: Uint8Array,
|
||||||
|
): Promise<Uint8Array> {
|
||||||
|
// Each slot is pinned to a fixed semantic (not to local role) so both
|
||||||
|
// sides feed HKDF the identical ikm:
|
||||||
|
// slotA = DH(initiatorEphemeral, responderIdentity)
|
||||||
|
// slotB = DH(initiatorIdentity, responderEphemeral)
|
||||||
|
// slotC = DH(initiatorEphemeral, responderEphemeral)
|
||||||
|
let slotA: Uint8Array;
|
||||||
|
let slotB: Uint8Array;
|
||||||
|
let slotC: Uint8Array;
|
||||||
|
if (role === 'initiator') {
|
||||||
|
slotA = await crypto.x25519(selfEphemeralPriv, peerIdentityDHPub);
|
||||||
|
slotB = await crypto.x25519(selfIdentityDHPriv, peerEphemeralPub);
|
||||||
|
slotC = await crypto.x25519(selfEphemeralPriv, peerEphemeralPub);
|
||||||
|
} else {
|
||||||
|
slotA = await crypto.x25519(selfIdentityDHPriv, peerEphemeralPub);
|
||||||
|
slotB = await crypto.x25519(selfEphemeralPriv, peerIdentityDHPub);
|
||||||
|
slotC = await crypto.x25519(selfEphemeralPriv, peerEphemeralPub);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ikm = new Uint8Array(96);
|
||||||
|
ikm.set(slotA, 0);
|
||||||
|
ikm.set(slotB, 32);
|
||||||
|
ikm.set(slotC, 64);
|
||||||
|
const sk = await crypto.hkdf(ikm, streamId, STREAM_KDF_INFO, 32);
|
||||||
|
|
||||||
|
crypto.zeroize(slotA);
|
||||||
|
crypto.zeroize(slotB);
|
||||||
|
crypto.zeroize(slotC);
|
||||||
|
crypto.zeroize(ikm);
|
||||||
|
return sk;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bootstrap a fresh Double Ratchet `SessionState` from the derived
|
||||||
|
* stream root key. The responder's ephemeral plays exactly the role
|
||||||
|
* X3DH's signed prekey plays in {@link initSenderSession} /
|
||||||
|
* {@link initReceiverSession}, so the ratchet handoff is identical to
|
||||||
|
* the proven HTTP path.
|
||||||
|
*
|
||||||
|
* On the initiator only `responderEphemeral.publicKey` is needed; the
|
||||||
|
* responder must pass its full ephemeral keypair.
|
||||||
|
*
|
||||||
|
* `peerIdentityDHPub` is recorded as the session's `remoteIdentityKey`
|
||||||
|
* so stream fingerprints stay meaningful and consistent with the parent.
|
||||||
|
*/
|
||||||
|
export async function bootstrapStreamSession(
|
||||||
|
crypto: CryptoProvider,
|
||||||
|
role: StreamRole,
|
||||||
|
sk: Uint8Array,
|
||||||
|
peerIdentityDHPub: Uint8Array,
|
||||||
|
responderEphemeral: KeyPair,
|
||||||
|
): Promise<SessionState> {
|
||||||
|
if (role === 'initiator') {
|
||||||
|
// initSenderSession derives a fresh root via kdfRootKey and does not
|
||||||
|
// retain `sk`, so the caller may safely zeroize it afterwards.
|
||||||
|
return initSenderSession(crypto, sk, peerIdentityDHPub, responderEphemeral.publicKey);
|
||||||
|
}
|
||||||
|
// initReceiverSession stores the root key BY REFERENCE. Hand it an
|
||||||
|
// independent copy so the caller zeroizing its `sk` scratch buffer
|
||||||
|
// can't wipe the live session root.
|
||||||
|
return initReceiverSession(new Uint8Array(sk), peerIdentityDHPub, responderEphemeral);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Zeroize every secret a stream session holds, then drop the chains. */
|
||||||
|
function zeroizeSession(crypto: CryptoProvider, s: SessionState): void {
|
||||||
|
crypto.zeroize(s.rootKey);
|
||||||
|
if (s.sendChain.chainKey.length > 0) crypto.zeroize(s.sendChain.chainKey);
|
||||||
|
if (s.receiveChain && s.receiveChain.chainKey.length > 0) {
|
||||||
|
crypto.zeroize(s.receiveChain.chainKey);
|
||||||
|
}
|
||||||
|
if (s.dhSend.privateKey.length > 0) crypto.zeroize(s.dhSend.privateKey);
|
||||||
|
for (const mk of s.skippedKeys.values()) crypto.zeroize(mk);
|
||||||
|
s.skippedKeys.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In-memory holder for a stream's Double Ratchet. Serialises its own
|
||||||
|
* `seal`/`open`/`close` on a private promise chain (independent of the
|
||||||
|
* SDK's per-peer encrypt/decrypt queues — R5) so per-frame ratchet
|
||||||
|
* mutations never interleave, while staying fully concurrent with the
|
||||||
|
* parent session and with other streams.
|
||||||
|
*
|
||||||
|
* Never persisted: the ratchet exists only for the lifetime of the
|
||||||
|
* stream and is zeroized on `close()`.
|
||||||
|
*/
|
||||||
|
export class StreamRatchet {
|
||||||
|
private session: SessionState | null;
|
||||||
|
private opChain: Promise<unknown> = Promise.resolve();
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly crypto: CryptoProvider,
|
||||||
|
session: SessionState,
|
||||||
|
/** 16-byte stream id this ratchet is bound to. */
|
||||||
|
public readonly streamId: Uint8Array,
|
||||||
|
) {
|
||||||
|
this.session = session;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** True once {@link close} has run; `seal`/`open` will throw. */
|
||||||
|
get closed(): boolean {
|
||||||
|
return this.session === null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private run<T>(fn: (s: SessionState) => Promise<T>): Promise<T> {
|
||||||
|
const next = this.opChain.catch(() => undefined).then(() => {
|
||||||
|
if (!this.session) throw new StreamClosedError();
|
||||||
|
return fn(this.session);
|
||||||
|
});
|
||||||
|
// Keep a never-rejecting tail so a failed frame doesn't poison the
|
||||||
|
// next one (a single bad inbound frame must not wedge the stream).
|
||||||
|
this.opChain = next.catch(() => undefined);
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Wrap one logical frame. Advances the sending chain by one step. */
|
||||||
|
seal(plaintext: Uint8Array): Promise<RatchetMessage> {
|
||||||
|
return this.run((s) => ratchetEncrypt(this.crypto, s, plaintext));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unwrap one inbound frame. Correct and memory-bounded across long
|
||||||
|
* one-directional runs from the peer: ordered transport delivery
|
||||||
|
* skips zero keys per frame, and out-of-order arrivals are still
|
||||||
|
* capped by the ratchet's `MAX_SKIP` / `MAX_CACHED_SKIPPED_KEYS`.
|
||||||
|
*/
|
||||||
|
open(message: RatchetMessage): Promise<Uint8Array> {
|
||||||
|
return this.run((s) => ratchetDecrypt(this.crypto, s, message));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Zeroize and drop the ratchet. Idempotent. */
|
||||||
|
close(): Promise<void> {
|
||||||
|
return this.opChain
|
||||||
|
.catch(() => undefined)
|
||||||
|
.then(() => {
|
||||||
|
if (this.session) {
|
||||||
|
zeroizeSession(this.crypto, this.session);
|
||||||
|
this.session = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
176
packages/shade-core/tests/stream.test.ts
Normal file
176
packages/shade-core/tests/stream.test.ts
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
import { describe, test, expect, beforeEach } from 'bun:test';
|
||||||
|
import { SubtleCryptoProvider, MemoryStorage } from '@shade/crypto-web';
|
||||||
|
import {
|
||||||
|
ShadeSessionManager,
|
||||||
|
StreamRatchet,
|
||||||
|
StreamClosedError,
|
||||||
|
DecryptionError,
|
||||||
|
} from '../src/index.js';
|
||||||
|
|
||||||
|
const crypto = new SubtleCryptoProvider();
|
||||||
|
const enc = new TextEncoder();
|
||||||
|
const dec = new TextDecoder();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Establish a *bidirectional* parent session: Alice→Bob X3DH, then one
|
||||||
|
* Alice→Bob message Bob decrypts so Bob also has a session for 'alice'.
|
||||||
|
* Both sides then hold the peer's pinned identity DH key — the input the
|
||||||
|
* stream handshake derives from.
|
||||||
|
*/
|
||||||
|
async function bidirectionalPair() {
|
||||||
|
const aliceStorage = new MemoryStorage();
|
||||||
|
const bobStorage = new MemoryStorage();
|
||||||
|
const alice = new ShadeSessionManager(crypto, aliceStorage);
|
||||||
|
const bob = new ShadeSessionManager(crypto, bobStorage);
|
||||||
|
await alice.initialize();
|
||||||
|
await bob.initialize();
|
||||||
|
|
||||||
|
const otpks = await bob.generateOneTimePreKeys(4);
|
||||||
|
const bundle = await bob.createPreKeyBundle();
|
||||||
|
bundle.oneTimePreKey = { keyId: otpks[0]!.keyId, publicKey: otpks[0]!.keyPair.publicKey };
|
||||||
|
await alice.initSessionFromBundle('bob', bundle);
|
||||||
|
|
||||||
|
const hello = await alice.encrypt('bob', 'parent-hello');
|
||||||
|
expect(await bob.decrypt('alice', hello)).toBe('parent-hello');
|
||||||
|
|
||||||
|
return { alice, bob, aliceStorage, bobStorage };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Run the full STREAM_OPEN / STREAM_OPEN_ACK handshake between managers. */
|
||||||
|
async function openStreamPair(alice: ShadeSessionManager, bob: ShadeSessionManager) {
|
||||||
|
const begun = await alice.beginStream('bob'); // initiator
|
||||||
|
const accepted = await bob.acceptStream('alice', begun.streamId, begun.ephemeralPublicKey);
|
||||||
|
const aliceStream = await begun.complete(accepted.ephemeralPublicKey);
|
||||||
|
return { aliceStream, bobStream: accepted.stream, streamId: begun.streamId };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('streaming sub-ratchet (V4.11)', () => {
|
||||||
|
let alice: ShadeSessionManager;
|
||||||
|
let bob: ShadeSessionManager;
|
||||||
|
let aliceStorage: MemoryStorage;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
({ alice, bob, aliceStorage } = await bidirectionalPair());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('both sides derive the same stream root (round-trips frames)', async () => {
|
||||||
|
const { aliceStream, bobStream } = await openStreamPair(alice, bob);
|
||||||
|
|
||||||
|
// Initiator → responder (first frame triggers responder DH step).
|
||||||
|
const f1 = await aliceStream.seal(enc.encode('log line 1'));
|
||||||
|
expect(dec.decode(await bobStream.open(f1))).toBe('log line 1');
|
||||||
|
|
||||||
|
// Responder → initiator (now responder may seal).
|
||||||
|
const r1 = await bobStream.seal(enc.encode('command-response 1'));
|
||||||
|
expect(dec.decode(await aliceStream.open(r1))).toBe('command-response 1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('two streams to the same peer get independent roots', async () => {
|
||||||
|
const s1 = await openStreamPair(alice, bob);
|
||||||
|
const s2 = await openStreamPair(alice, bob);
|
||||||
|
expect(s1.streamId).not.toEqual(s2.streamId);
|
||||||
|
|
||||||
|
const a = await s1.aliceStream.seal(enc.encode('on stream 1'));
|
||||||
|
// A frame from stream 1 must not decrypt on stream 2's ratchet.
|
||||||
|
await expect(s2.bobStream.open(a)).rejects.toBeInstanceOf(DecryptionError);
|
||||||
|
// …but does on its own.
|
||||||
|
expect(dec.decode(await s1.bobStream.open(a))).toBe('on stream 1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('R2/R3: long one-directional burst stays correct and memory-bounded', async () => {
|
||||||
|
const { aliceStream, bobStream } = await openStreamPair(alice, bob);
|
||||||
|
const N = 5000;
|
||||||
|
|
||||||
|
// Capture a live receive-chain key buffer to prove forward secrecy:
|
||||||
|
// ratchetDecrypt zeroizes the previous chain key in place.
|
||||||
|
await bobStream.open(await aliceStream.seal(enc.encode('frame-0')));
|
||||||
|
const bobSession = (bobStream as unknown as { session: { receiveChain: { chainKey: Uint8Array }; skippedKeys: Map<string, Uint8Array> } }).session;
|
||||||
|
const staleChainKey = bobSession.receiveChain.chainKey;
|
||||||
|
const staleCopy = staleChainKey.slice();
|
||||||
|
expect(staleCopy.some((b) => b !== 0)).toBe(true);
|
||||||
|
|
||||||
|
for (let i = 1; i < N; i++) {
|
||||||
|
const wire = await aliceStream.seal(enc.encode(`frame-${i}`));
|
||||||
|
expect(dec.decode(await bobStream.open(wire))).toBe(`frame-${i}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// In-order delivery ⇒ zero skipped keys retained across 5k frames.
|
||||||
|
expect(bobSession.skippedKeys.size).toBe(0);
|
||||||
|
// The chain key in use at frame 0 was overwritten (forward secrecy).
|
||||||
|
expect(staleChainKey.every((b) => b === 0)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('R5: opening/using/closing a stream never touches the parent session', async () => {
|
||||||
|
const before = await aliceStorage.getSession('bob');
|
||||||
|
const snapshot = JSON.stringify({
|
||||||
|
root: Array.from(before!.rootKey),
|
||||||
|
sendCtr: before!.sendChain.counter,
|
||||||
|
prevCtr: before!.previousSendCounter,
|
||||||
|
});
|
||||||
|
|
||||||
|
const { aliceStream, bobStream } = await openStreamPair(alice, bob);
|
||||||
|
for (let i = 0; i < 200; i++) {
|
||||||
|
await bobStream.open(await aliceStream.seal(enc.encode(`x${i}`)));
|
||||||
|
}
|
||||||
|
await aliceStream.close();
|
||||||
|
await bobStream.close();
|
||||||
|
|
||||||
|
const after = await aliceStorage.getSession('bob');
|
||||||
|
expect(
|
||||||
|
JSON.stringify({
|
||||||
|
root: Array.from(after!.rootKey),
|
||||||
|
sendCtr: after!.sendChain.counter,
|
||||||
|
prevCtr: after!.previousSendCounter,
|
||||||
|
}),
|
||||||
|
).toBe(snapshot);
|
||||||
|
|
||||||
|
// Parent HTTP path still works after the stream lifecycle.
|
||||||
|
const env = await alice.encrypt('bob', 'after-stream');
|
||||||
|
expect(await bob.decrypt('alice', env)).toBe('after-stream');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('R1: replayed / rewound frame is rejected', async () => {
|
||||||
|
const { aliceStream, bobStream } = await openStreamPair(alice, bob);
|
||||||
|
const f1 = await aliceStream.seal(enc.encode('once'));
|
||||||
|
expect(dec.decode(await bobStream.open(f1))).toBe('once');
|
||||||
|
// Re-delivering the exact same sealed frame must fail.
|
||||||
|
await expect(bobStream.open(f1)).rejects.toBeInstanceOf(DecryptionError);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('close() zeroizes and blocks further use; idempotent', async () => {
|
||||||
|
const { aliceStream, bobStream } = await openStreamPair(alice, bob);
|
||||||
|
await aliceStream.close();
|
||||||
|
await aliceStream.close(); // idempotent
|
||||||
|
expect(aliceStream.closed).toBe(true);
|
||||||
|
await expect(aliceStream.seal(enc.encode('nope'))).rejects.toBeInstanceOf(
|
||||||
|
StreamClosedError,
|
||||||
|
);
|
||||||
|
// The peer end is unaffected by our local close.
|
||||||
|
expect(bobStream.closed).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handshake is mutually authenticated against pinned identities', async () => {
|
||||||
|
// A third party (mallory) with its own identity cannot stand in for
|
||||||
|
// bob: alice derives against bob's pinned identity key, so a
|
||||||
|
// handshake completed with mallory's ephemeral yields a different
|
||||||
|
// root and frames fail to open.
|
||||||
|
const mStorage = new MemoryStorage();
|
||||||
|
const mallory = new ShadeSessionManager(crypto, mStorage);
|
||||||
|
await mallory.initialize();
|
||||||
|
// Give mallory a parent session label so acceptStream has identity
|
||||||
|
// material, but pinned to the WRONG (alice) identity vs what alice
|
||||||
|
// pinned for 'bob'.
|
||||||
|
const otpks = await mallory.generateOneTimePreKeys(2);
|
||||||
|
const mb = await mallory.createPreKeyBundle();
|
||||||
|
mb.oneTimePreKey = { keyId: otpks[0]!.keyId, publicKey: otpks[0]!.keyPair.publicKey };
|
||||||
|
await alice.initSessionFromBundle('mallory', mb);
|
||||||
|
const helo = await alice.encrypt('mallory', 'hi');
|
||||||
|
await mallory.decrypt('alice', helo);
|
||||||
|
|
||||||
|
const begun = await alice.beginStream('bob');
|
||||||
|
const mAccept = await mallory.acceptStream('alice', begun.streamId, begun.ephemeralPublicKey);
|
||||||
|
const aliceStream = await begun.complete(mAccept.ephemeralPublicKey);
|
||||||
|
const frame = await aliceStream.seal(enc.encode('secret'));
|
||||||
|
await expect(mAccept.stream.open(frame)).rejects.toBeInstanceOf(DecryptionError);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/crypto-web",
|
"name": "@shade/crypto-web",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
18
packages/shade-crypto-web/src/ed25519-derive.ts
Normal file
18
packages/shade-crypto-web/src/ed25519-derive.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { ed25519 } from '@noble/curves/ed25519.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deterministically derive an Ed25519 public key from a 32-byte seed.
|
||||||
|
*
|
||||||
|
* In the @noble/curves convention the "private key" *is* the seed —
|
||||||
|
* `sign(seed, msg)` works directly, and `getPublicKey(seed)` recovers
|
||||||
|
* the matching public key. V4.9's encrypted-blob primitive uses this
|
||||||
|
* to mint a per-slot signing keypair from an HKDF output rooted at the
|
||||||
|
* user's master key, so the same credentials always reproduce the same
|
||||||
|
* keypair.
|
||||||
|
*/
|
||||||
|
export function ed25519PublicKeyFromSeed(seed: Uint8Array): Uint8Array {
|
||||||
|
if (seed.length !== 32) {
|
||||||
|
throw new Error(`Ed25519 seed must be 32 bytes, got ${seed.length}`);
|
||||||
|
}
|
||||||
|
return ed25519.getPublicKey(seed);
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
export { SubtleCryptoProvider } from './provider.js';
|
export { SubtleCryptoProvider } from './provider.js';
|
||||||
export { MemoryStorage } from './memory-storage.js';
|
export { MemoryStorage } from './memory-storage.js';
|
||||||
|
export { ed25519PublicKeyFromSeed } from './ed25519-derive.js';
|
||||||
|
|
||||||
// ─── Web Workers crypto (V3.8) ────────────────────────────
|
// ─── Web Workers crypto (V3.8) ────────────────────────────
|
||||||
export {
|
export {
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import type { StorageProvider, IdentityKeyPair, SignedPreKey, OneTimePreKey, SessionState, RetiredIdentity, PersistedStreamState, PeerVerification } from '@shade/core';
|
import type {
|
||||||
|
StorageProvider, IdentityKeyPair, SignedPreKey, OneTimePreKey,
|
||||||
|
SessionState, RetiredIdentity, PersistedStreamState, PeerVerification,
|
||||||
|
BroadcastChannelRecord, BroadcastMemberRecord,
|
||||||
|
} from '@shade/core';
|
||||||
import { constantTimeEqual } from '@shade/core';
|
import { constantTimeEqual } from '@shade/core';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -167,4 +171,62 @@ export class MemoryStorage implements StorageProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Broadcast channels (V4.6) ────────────────────────────
|
||||||
|
|
||||||
|
private broadcastChannels = new Map<string, BroadcastChannelRecord>();
|
||||||
|
private broadcastMembers = new Map<string, Map<string, BroadcastMemberRecord>>();
|
||||||
|
|
||||||
|
async saveBroadcastChannel(channel: BroadcastChannelRecord): Promise<void> {
|
||||||
|
this.broadcastChannels.set(channel.channelId, cloneChannel(channel));
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBroadcastChannel(channelId: string): Promise<BroadcastChannelRecord | null> {
|
||||||
|
const v = this.broadcastChannels.get(channelId);
|
||||||
|
return v ? cloneChannel(v) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async listBroadcastChannels(): Promise<BroadcastChannelRecord[]> {
|
||||||
|
return [...this.broadcastChannels.values()].map(cloneChannel);
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeBroadcastChannel(channelId: string): Promise<void> {
|
||||||
|
this.broadcastChannels.delete(channelId);
|
||||||
|
this.broadcastMembers.delete(channelId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveBroadcastMember(member: BroadcastMemberRecord): Promise<void> {
|
||||||
|
let inner = this.broadcastMembers.get(member.channelId);
|
||||||
|
if (!inner) {
|
||||||
|
inner = new Map();
|
||||||
|
this.broadcastMembers.set(member.channelId, inner);
|
||||||
|
}
|
||||||
|
inner.set(member.peerAddress, { ...member });
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBroadcastMembers(channelId: string): Promise<BroadcastMemberRecord[]> {
|
||||||
|
const inner = this.broadcastMembers.get(channelId);
|
||||||
|
return inner ? [...inner.values()].map((m) => ({ ...m })) : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeBroadcastMember(channelId: string, peerAddress: string): Promise<void> {
|
||||||
|
this.broadcastMembers.get(channelId)?.delete(peerAddress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneChannel(c: BroadcastChannelRecord): BroadcastChannelRecord {
|
||||||
|
const out: BroadcastChannelRecord = {
|
||||||
|
channelId: c.channelId,
|
||||||
|
ownerRole: c.ownerRole,
|
||||||
|
ownerAddress: c.ownerAddress,
|
||||||
|
generation: c.generation,
|
||||||
|
chainKey: new Uint8Array(c.chainKey),
|
||||||
|
iteration: c.iteration,
|
||||||
|
signingPublicKey: new Uint8Array(c.signingPublicKey),
|
||||||
|
createdAt: c.createdAt,
|
||||||
|
updatedAt: c.updatedAt,
|
||||||
|
};
|
||||||
|
if (c.label !== undefined) out.label = c.label;
|
||||||
|
if (c.signingPrivateKey !== undefined) out.signingPrivateKey = new Uint8Array(c.signingPrivateKey);
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/dashboard",
|
"name": "@shade/dashboard",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/files",
|
"name": "@shade/files",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/inbox-server",
|
"name": "@shade/inbox-server",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
268
packages/shade-inbox-server/src/blob-routes.ts
Normal file
268
packages/shade-inbox-server/src/blob-routes.ts
Normal file
@@ -0,0 +1,268 @@
|
|||||||
|
import { Hono } from 'hono';
|
||||||
|
import type { CryptoProvider } from '@shade/core';
|
||||||
|
import {
|
||||||
|
errorToHttpStatus,
|
||||||
|
ShadeError,
|
||||||
|
ValidationError,
|
||||||
|
UnauthorizedError,
|
||||||
|
fromBase64,
|
||||||
|
toBase64,
|
||||||
|
constantTimeEqual,
|
||||||
|
} from '@shade/core';
|
||||||
|
import {
|
||||||
|
verifyPayload,
|
||||||
|
RateLimiter,
|
||||||
|
MemoryRateLimitStore,
|
||||||
|
type RateLimitConfig,
|
||||||
|
} from '@shade/server';
|
||||||
|
import {
|
||||||
|
ATTR_ERROR_CODE,
|
||||||
|
ATTR_HTTP_STATUS,
|
||||||
|
ATTR_ROUTE,
|
||||||
|
NOOP_HOOK,
|
||||||
|
type ObservabilityHook,
|
||||||
|
} from '@shade/observability';
|
||||||
|
import type { BlobStore } from './blob-store.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wire-level wrapper around the V4.9 BlobStore primitive.
|
||||||
|
*
|
||||||
|
* Endpoints:
|
||||||
|
* GET /v1/blob/:slotId → { blob, etag } | 404
|
||||||
|
* PUT /v1/blob/:slotId → { etag, created } | 409 | 412
|
||||||
|
* DELETE /v1/blob/:slotId → { ok }
|
||||||
|
*
|
||||||
|
* SlotId is 64 lowercase hex chars (the HKDF output, 32 bytes). Payloads
|
||||||
|
* are base64-encoded ciphertext; the relay never decrypts. Auth uses
|
||||||
|
* `signPayload` / `verifyPayload` (same canonical-JSON-and-Ed25519
|
||||||
|
* scheme as the inbox routes), keyed off the per-slot pubkey stored
|
||||||
|
* TOFU on the first PUT.
|
||||||
|
*
|
||||||
|
* Quota: a single slot holds one blob. `MAX_BLOB_BYTES` (64 KiB) is
|
||||||
|
* sized for Prism's profile use-case (a few hundred host entries) with
|
||||||
|
* plenty of headroom; future apps can override via `BlobRoutesOptions`.
|
||||||
|
*/
|
||||||
|
const SLOT_ID_REGEX = /^[0-9a-f]{64}$/;
|
||||||
|
const MAX_META_BODY_SIZE = 64 * 1024;
|
||||||
|
/** Default per-slot blob ceiling. Sized for ~500 host entries in JSON form. */
|
||||||
|
export const DEFAULT_MAX_BLOB_BYTES = 64 * 1024;
|
||||||
|
|
||||||
|
const PUT_LIMIT: RateLimitConfig = { capacity: 60, refillPerSecond: 1 };
|
||||||
|
const GET_LIMIT: RateLimitConfig = { capacity: 120, refillPerSecond: 2 };
|
||||||
|
const DELETE_LIMIT: RateLimitConfig = { capacity: 30, refillPerSecond: 1 };
|
||||||
|
|
||||||
|
export interface BlobRoutesOptions {
|
||||||
|
disableRateLimit?: boolean;
|
||||||
|
observability?: ObservabilityHook;
|
||||||
|
/** Per-blob byte ceiling. Defaults to 64 KiB. */
|
||||||
|
maxBlobBytes?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createBlobRoutes(
|
||||||
|
store: BlobStore,
|
||||||
|
crypto: CryptoProvider,
|
||||||
|
options: BlobRoutesOptions = {},
|
||||||
|
): Hono {
|
||||||
|
const app = new Hono();
|
||||||
|
const observability = options.observability ?? NOOP_HOOK;
|
||||||
|
const maxBlobBytes = options.maxBlobBytes ?? DEFAULT_MAX_BLOB_BYTES;
|
||||||
|
|
||||||
|
app.use('*', async (c, next) => {
|
||||||
|
const route = c.req.routePath ?? c.req.path ?? '<unknown>';
|
||||||
|
const span = observability.startSpan('shade.blob.request', {
|
||||||
|
[ATTR_ROUTE]: route,
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await next();
|
||||||
|
span.setAttribute(ATTR_HTTP_STATUS, c.res.status);
|
||||||
|
span.setStatus(c.res.status >= 500 ? 'error' : 'ok');
|
||||||
|
} catch (err) {
|
||||||
|
const code =
|
||||||
|
err instanceof ShadeError ? err.code ?? 'SHADE_ERROR' : 'SHADE_INTERNAL';
|
||||||
|
span.setAttribute(ATTR_ERROR_CODE, code);
|
||||||
|
span.recordException(err);
|
||||||
|
span.setStatus('error', code);
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
span.end();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const rlStore = new MemoryRateLimitStore();
|
||||||
|
const putRL = new RateLimiter(rlStore, PUT_LIMIT);
|
||||||
|
const getRL = new RateLimiter(rlStore, GET_LIMIT);
|
||||||
|
const deleteRL = new RateLimiter(rlStore, DELETE_LIMIT);
|
||||||
|
const rateLimitEnabled = !options.disableRateLimit;
|
||||||
|
|
||||||
|
const getClientIp = (c: any): string =>
|
||||||
|
c.req.header('x-forwarded-for')?.split(',')[0]?.trim() ??
|
||||||
|
c.req.header('x-real-ip') ??
|
||||||
|
'unknown';
|
||||||
|
|
||||||
|
app.onError((err, c) => {
|
||||||
|
if (err instanceof ShadeError) {
|
||||||
|
const status = errorToHttpStatus(err);
|
||||||
|
const body: any = err.toJSON();
|
||||||
|
if ((err as any).retryAfterSeconds) {
|
||||||
|
c.header('Retry-After', String((err as any).retryAfterSeconds));
|
||||||
|
}
|
||||||
|
return c.json(body, status as any);
|
||||||
|
}
|
||||||
|
console.error('[Shade] Unhandled blob error:', err);
|
||||||
|
return c.json({ error: 'Internal server error' }, 500);
|
||||||
|
});
|
||||||
|
|
||||||
|
function validateSlotId(raw: string | undefined): string {
|
||||||
|
if (typeof raw !== 'string' || !SLOT_ID_REGEX.test(raw)) {
|
||||||
|
throw new ValidationError(
|
||||||
|
'slotId must be 64 lowercase hex chars (32 bytes)',
|
||||||
|
'slotId',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── GET ─────────────────────────────────────────────────────
|
||||||
|
// Unauthenticated. SlotId is itself a 256-bit secret derived from the
|
||||||
|
// master key — knowing it implies you derived the master, which is
|
||||||
|
// equivalent to holding the credentials. The blob is AEAD-sealed, so
|
||||||
|
// a relay-side leak of slotId still cannot decrypt the contents.
|
||||||
|
app.get('/v1/blob/:slotId', async (c) => {
|
||||||
|
const slotId = validateSlotId(c.req.param('slotId'));
|
||||||
|
if (rateLimitEnabled) await getRL.consume(`blob-get:${getClientIp(c)}`);
|
||||||
|
|
||||||
|
const row = await store.get(slotId);
|
||||||
|
if (!row) {
|
||||||
|
return c.json({ error: 'Slot not found', code: 'SHADE_NOT_FOUND' }, 404);
|
||||||
|
}
|
||||||
|
return c.json({
|
||||||
|
blob: toBase64(row.blob),
|
||||||
|
etag: String(row.etag),
|
||||||
|
updatedAt: row.updatedAt,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── PUT ─────────────────────────────────────────────────────
|
||||||
|
// Body format:
|
||||||
|
// {
|
||||||
|
// ownerPubkey: b64, // Ed25519 pubkey deterministically
|
||||||
|
// // derived from the master via HKDF.
|
||||||
|
// blob: b64,
|
||||||
|
// ifMatch?: string, // "<etag>" | "*" | undefined
|
||||||
|
// signedAt: number,
|
||||||
|
// signature: b64 // over the canonical body sans signature
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// First write to a slot is TOFU: we record `ownerPubkey` and require
|
||||||
|
// any future write to verify against it. A different key trying to
|
||||||
|
// overwrite an existing slot is rejected with UnauthorizedError.
|
||||||
|
app.put('/v1/blob/:slotId', async (c) => {
|
||||||
|
const slotId = validateSlotId(c.req.param('slotId'));
|
||||||
|
if (rateLimitEnabled) await putRL.consume(`blob-put:${getClientIp(c)}`);
|
||||||
|
|
||||||
|
const rawBody = await c.req.text();
|
||||||
|
const hardLimit = Math.ceil(maxBlobBytes * 1.4) + MAX_META_BODY_SIZE;
|
||||||
|
if (rawBody.length > hardLimit) {
|
||||||
|
throw new ValidationError(`Request body too large`);
|
||||||
|
}
|
||||||
|
const body = JSON.parse(rawBody);
|
||||||
|
const { ownerPubkey, blob, ifMatch } = body;
|
||||||
|
|
||||||
|
if (typeof ownerPubkey !== 'string') {
|
||||||
|
throw new ValidationError('Missing ownerPubkey', 'ownerPubkey');
|
||||||
|
}
|
||||||
|
if (typeof blob !== 'string') {
|
||||||
|
throw new ValidationError('Missing blob', 'blob');
|
||||||
|
}
|
||||||
|
const claimedKey = fromBase64(ownerPubkey);
|
||||||
|
if (claimedKey.length !== 32) {
|
||||||
|
throw new ValidationError('ownerPubkey must be 32 bytes (Ed25519)', 'ownerPubkey');
|
||||||
|
}
|
||||||
|
const blobBytes = fromBase64(blob);
|
||||||
|
if (blobBytes.length === 0) {
|
||||||
|
throw new ValidationError('blob is empty', 'blob');
|
||||||
|
}
|
||||||
|
if (blobBytes.length > maxBlobBytes) {
|
||||||
|
throw new ValidationError(
|
||||||
|
`blob exceeds maxBlobBytes (${blobBytes.length} > ${maxBlobBytes})`,
|
||||||
|
'blob',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let expectedEtag: number | '*' | undefined;
|
||||||
|
if (ifMatch === undefined) {
|
||||||
|
expectedEtag = undefined;
|
||||||
|
} else if (typeof ifMatch !== 'string') {
|
||||||
|
throw new ValidationError('ifMatch must be a string when present', 'ifMatch');
|
||||||
|
} else if (ifMatch === '*') {
|
||||||
|
expectedEtag = '*';
|
||||||
|
} else {
|
||||||
|
const n = Number(ifMatch);
|
||||||
|
if (!Number.isFinite(n) || !Number.isInteger(n) || n < 0) {
|
||||||
|
throw new ValidationError('ifMatch must be a non-negative integer or "*"', 'ifMatch');
|
||||||
|
}
|
||||||
|
expectedEtag = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Existing slot: caller must sign with the original owner key. Use
|
||||||
|
// the stored pubkey for verification. The body's `ownerPubkey` is
|
||||||
|
// bound by the signature too, so an attacker cannot trick us into
|
||||||
|
// verifying with a key they control — the canonicalization includes
|
||||||
|
// every field but `signature`.
|
||||||
|
const existing = await store.get(slotId);
|
||||||
|
const verifyKey = existing ? existing.ownerPubkey : claimedKey;
|
||||||
|
|
||||||
|
// Bind slotId into the signed payload so a signature for slot A
|
||||||
|
// can't be replayed against slot B (the URL is otherwise outside
|
||||||
|
// the signed bytes).
|
||||||
|
await verifyPayload(crypto, verifyKey, { ...body, slotId });
|
||||||
|
|
||||||
|
if (existing && !constantTimeEqual(existing.ownerPubkey, claimedKey)) {
|
||||||
|
throw new UnauthorizedError(
|
||||||
|
`Slot ${slotId} is owned by a different signing key`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await store.put({
|
||||||
|
slotId,
|
||||||
|
blob: blobBytes,
|
||||||
|
ownerPubkey: claimedKey,
|
||||||
|
expectedEtag,
|
||||||
|
now: Date.now(),
|
||||||
|
});
|
||||||
|
|
||||||
|
return c.json({
|
||||||
|
ok: true,
|
||||||
|
created: result.created,
|
||||||
|
etag: String(result.etag),
|
||||||
|
updatedAt: result.updatedAt,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── DELETE ──────────────────────────────────────────────────
|
||||||
|
// Body format: { signedAt, signature }. Signed by the owner pubkey
|
||||||
|
// recorded on the first PUT. After deletion, the slot is fully gone —
|
||||||
|
// the next PUT TOFU-claims it again (potentially under a different
|
||||||
|
// signing key, e.g. after a rotation).
|
||||||
|
app.delete('/v1/blob/:slotId', async (c) => {
|
||||||
|
const slotId = validateSlotId(c.req.param('slotId'));
|
||||||
|
if (rateLimitEnabled) await deleteRL.consume(`blob-delete:${getClientIp(c)}`);
|
||||||
|
|
||||||
|
const existing = await store.get(slotId);
|
||||||
|
if (!existing) {
|
||||||
|
return c.json({ error: 'Slot not found', code: 'SHADE_NOT_FOUND' }, 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawBody = await c.req.text();
|
||||||
|
if (rawBody.length > MAX_META_BODY_SIZE) {
|
||||||
|
throw new ValidationError(`Request body too large`);
|
||||||
|
}
|
||||||
|
const body = JSON.parse(rawBody);
|
||||||
|
await verifyPayload(crypto, existing.ownerPubkey, { ...body, slotId });
|
||||||
|
|
||||||
|
const removed = await store.delete(slotId);
|
||||||
|
return c.json({ ok: removed });
|
||||||
|
});
|
||||||
|
|
||||||
|
return app;
|
||||||
|
}
|
||||||
86
packages/shade-inbox-server/src/blob-store.ts
Normal file
86
packages/shade-inbox-server/src/blob-store.ts
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
/**
|
||||||
|
* BlobStore — server-side storage interface for the V4.9 encrypted-blob
|
||||||
|
* primitive. A "slot" is a single AEAD-sealed blob keyed by a
|
||||||
|
* deterministic 32-byte slotId derived client-side via HKDF from a
|
||||||
|
* master key. The relay never sees plaintext, never holds private keys,
|
||||||
|
* and never decrypts.
|
||||||
|
*
|
||||||
|
* Auth model (TOFU per slot, mirrors the inbox-owner pattern):
|
||||||
|
* - First PUT to an empty slot stores the caller's Ed25519 signing
|
||||||
|
* pubkey alongside the blob. Subsequent writes must produce a valid
|
||||||
|
* signature verifiable by that pubkey.
|
||||||
|
* - GET is unauthenticated — slotId is itself a 256-bit secret derived
|
||||||
|
* from the master key, so knowing it implies you derived the master.
|
||||||
|
* - DELETE clears the blob AND the owner pubkey, allowing future TOFU
|
||||||
|
* re-claim by a fresh signing key derived from the same master (e.g.
|
||||||
|
* after a rotation).
|
||||||
|
*
|
||||||
|
* CAS / etag semantics:
|
||||||
|
* - Every successful PUT bumps a per-slot monotonic etag (returned to
|
||||||
|
* the caller as a string).
|
||||||
|
* - A stale `ifMatch` triggers `PreconditionFailedError` (HTTP 412).
|
||||||
|
* - `ifMatch === undefined` against a populated slot triggers
|
||||||
|
* `ConflictError` (HTTP 409) — clients must read-then-write.
|
||||||
|
* - `ifMatch === '*'` against a populated slot is unconditional
|
||||||
|
* overwrite (escape hatch). Against an empty slot it's still 412
|
||||||
|
* per RFC 7232 (no entity to match).
|
||||||
|
*/
|
||||||
|
export interface BlobSlotRecord {
|
||||||
|
/** Lower-hex 64-char slotId (32 bytes). */
|
||||||
|
slotId: string;
|
||||||
|
/** Raw AEAD ciphertext (bytes). The relay never decrypts. */
|
||||||
|
blob: Uint8Array;
|
||||||
|
/** Owner Ed25519 signing pubkey, established TOFU on the first PUT. */
|
||||||
|
ownerPubkey: Uint8Array;
|
||||||
|
/** Monotonic per-slot version. Used as the ETag on the wire. */
|
||||||
|
etag: number;
|
||||||
|
/** Wall-clock ms of the last successful write. */
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returned to the route layer after a successful PUT. */
|
||||||
|
export interface PutBlobResult {
|
||||||
|
/** Whether the slot was created (true) or updated in place (false). */
|
||||||
|
created: boolean;
|
||||||
|
/** New etag after the write. */
|
||||||
|
etag: number;
|
||||||
|
/** Wall-clock ms of the write. */
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BlobStore {
|
||||||
|
/** Read a slot, or null if it has never been written (or was deleted). */
|
||||||
|
get(slotId: string): Promise<BlobSlotRecord | null>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create or update a slot.
|
||||||
|
*
|
||||||
|
* Implementations MUST treat `(slotId, ownerPubkey)` atomically: the
|
||||||
|
* route layer has already verified the signature, but the store is the
|
||||||
|
* authority on whether the slot exists and what etag it has. Callers
|
||||||
|
* pass the verified `ownerPubkey` (used on first-write to record the
|
||||||
|
* owner; ignored on subsequent writes — the existing pubkey is the
|
||||||
|
* source of truth for who's allowed to write).
|
||||||
|
*
|
||||||
|
* `expectedEtag` semantics (mirror the wire-level If-Match):
|
||||||
|
* - `undefined` : create-only. Slot must be empty.
|
||||||
|
* - `<number>` : compare-and-swap. Must equal the current etag.
|
||||||
|
* - `'*'` : unconditional overwrite. Slot must already exist.
|
||||||
|
*
|
||||||
|
* On precondition mismatch the store throws `PreconditionFailedError`
|
||||||
|
* (stale etag) or `ConflictError` (slot exists, no ifMatch).
|
||||||
|
*/
|
||||||
|
put(args: {
|
||||||
|
slotId: string;
|
||||||
|
blob: Uint8Array;
|
||||||
|
ownerPubkey: Uint8Array;
|
||||||
|
expectedEtag: number | '*' | undefined;
|
||||||
|
now: number;
|
||||||
|
}): Promise<PutBlobResult>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a slot. Authentication has already been checked by the route
|
||||||
|
* layer. Returns true if a row was removed (i.e. the slot existed).
|
||||||
|
*/
|
||||||
|
delete(slotId: string): Promise<boolean>;
|
||||||
|
}
|
||||||
140
packages/shade-inbox-server/src/bridge-delivery-log.ts
Normal file
140
packages/shade-inbox-server/src/bridge-delivery-log.ts
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
/**
|
||||||
|
* BridgeDeliveryLog — V4.8.4 cross-channel dedup.
|
||||||
|
*
|
||||||
|
* Records per-`(address, msgId)` "delivered via bridge push" timestamps so
|
||||||
|
* the inbox-fetch route can filter out blobs the bridge has already pushed
|
||||||
|
* to the recipient. The relay's contract becomes:
|
||||||
|
*
|
||||||
|
* one `Inbox.send` ⇒ one observable delivery on the recipient
|
||||||
|
*
|
||||||
|
* even when the recipient runs both a bridge subscription (WS / SSE) AND
|
||||||
|
* the regular inbox-poll. Without it, bridge-push and inbox-poll are
|
||||||
|
* independent paths against the same store and the recipient gets the
|
||||||
|
* same envelope twice — bridge-first, then ~30 s later via the next poll
|
||||||
|
* — tripping on already-consumed prekeys (`one-time prekey not found`)
|
||||||
|
* or surfacing as duplicate `shade.receive` work.
|
||||||
|
*
|
||||||
|
* The log is in-memory per process and intentionally bounded: each entry
|
||||||
|
* lives for `graceMs` (default 60 s, well past a typical `pollIntervalMs`
|
||||||
|
* of 30 s). After grace, the entry is forgotten and inbox-poll falls back
|
||||||
|
* to delivering the blob — that's the legitimate "bridge dropped the
|
||||||
|
* frame, poll picked up" recovery path. If the recipient explicitly
|
||||||
|
* acks the blob (HTTP `DELETE /v1/inbox/:addr/:msgId`), the blob is gone
|
||||||
|
* from storage and the log entry is moot.
|
||||||
|
*
|
||||||
|
* Multi-bridge per address (e.g. WS + SSE redundancy on the same client,
|
||||||
|
* or two devices sharing one signing key) is preserved: every bridge
|
||||||
|
* connection still fetches + pushes the blob — each push records its own
|
||||||
|
* timestamp — so each connected bridge gets the frame. Only the *poll*
|
||||||
|
* fetch is filtered, not the bridge fetches themselves.
|
||||||
|
*
|
||||||
|
* @see Prism FR `cross-channel-duplicate-fanout-v4.8.2.md`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const DEFAULT_GRACE_MS = 60_000;
|
||||||
|
|
||||||
|
export interface BridgeDeliveryLogOptions {
|
||||||
|
/**
|
||||||
|
* How long a `(address, msgId)` mark suppresses inbox-poll delivery.
|
||||||
|
* Defaults to 60_000ms — twice the default `pollIntervalMs` of the
|
||||||
|
* `@shade/inbox` orchestrator, so a poll cycle that races a bridge
|
||||||
|
* push always sees the mark, but a stuck recipient still gets the
|
||||||
|
* blob via poll within ~minutes.
|
||||||
|
*/
|
||||||
|
graceMs?: number;
|
||||||
|
/**
|
||||||
|
* Maximum entries per address. Bounds memory under a busy address.
|
||||||
|
* Oldest entries (by recorded timestamp) are evicted first. Default
|
||||||
|
* 8192 — comfortably above any realistic backlog.
|
||||||
|
*/
|
||||||
|
maxPerAddress?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BridgeDeliveryLog {
|
||||||
|
private readonly log = new Map<string, Map<string, number>>();
|
||||||
|
private readonly graceMs: number;
|
||||||
|
private readonly maxPerAddress: number;
|
||||||
|
|
||||||
|
constructor(options: BridgeDeliveryLogOptions = {}) {
|
||||||
|
this.graceMs = options.graceMs ?? DEFAULT_GRACE_MS;
|
||||||
|
this.maxPerAddress = options.maxPerAddress ?? 8192;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mark `(address, msgId)` as bridge-delivered at `now`. */
|
||||||
|
recordDelivered(address: string, msgId: string, now: number): void {
|
||||||
|
let inner = this.log.get(address);
|
||||||
|
if (!inner) {
|
||||||
|
inner = new Map();
|
||||||
|
this.log.set(address, inner);
|
||||||
|
}
|
||||||
|
inner.set(msgId, now);
|
||||||
|
// Lazy cleanup: drop entries past 2× grace so the map stays bounded
|
||||||
|
// without a separate timer. Bound by `maxPerAddress` as a fallback
|
||||||
|
// for pathological burst scenarios.
|
||||||
|
if (inner.size > this.maxPerAddress) {
|
||||||
|
const cutoff = now - this.graceMs * 2;
|
||||||
|
for (const [id, ts] of inner) {
|
||||||
|
if (ts < cutoff) inner.delete(id);
|
||||||
|
}
|
||||||
|
// Still over cap? Drop the oldest.
|
||||||
|
if (inner.size > this.maxPerAddress) {
|
||||||
|
const sorted = Array.from(inner.entries()).sort((a, b) => a[1] - b[1]);
|
||||||
|
const toDrop = sorted.slice(0, inner.size - this.maxPerAddress);
|
||||||
|
for (const [id] of toDrop) inner.delete(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if `(address, msgId)` was bridge-delivered within the
|
||||||
|
* grace window.
|
||||||
|
*/
|
||||||
|
isRecentlyDelivered(address: string, msgId: string, now: number): boolean {
|
||||||
|
const inner = this.log.get(address);
|
||||||
|
if (!inner) return false;
|
||||||
|
const ts = inner.get(msgId);
|
||||||
|
if (ts === undefined) return false;
|
||||||
|
if (now - ts > this.graceMs) {
|
||||||
|
inner.delete(msgId); // tombstone the stale entry
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter `blobs` down to those not currently in the bridge-delivered
|
||||||
|
* grace window. Used by the inbox-fetch route to suppress duplicates.
|
||||||
|
*/
|
||||||
|
filterRecent<T extends { msgId: string }>(
|
||||||
|
address: string,
|
||||||
|
blobs: T[],
|
||||||
|
now: number,
|
||||||
|
): T[] {
|
||||||
|
const inner = this.log.get(address);
|
||||||
|
if (!inner || inner.size === 0) return blobs;
|
||||||
|
return blobs.filter((b) => {
|
||||||
|
const ts = inner.get(b.msgId);
|
||||||
|
if (ts === undefined) return true;
|
||||||
|
if (now - ts > this.graceMs) {
|
||||||
|
inner.delete(b.msgId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Drop the entry for `(address, msgId)`. Called from blob-delete paths. */
|
||||||
|
forget(address: string, msgId: string): void {
|
||||||
|
this.log.get(address)?.delete(msgId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Drop every entry for `address`. Called from address-delete paths. */
|
||||||
|
forgetAddress(address: string): void {
|
||||||
|
this.log.delete(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test-only inspection. */
|
||||||
|
size(address: string): number {
|
||||||
|
return this.log.get(address)?.size ?? 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,8 +38,16 @@ import {
|
|||||||
import { verifyPayload, validateAddress } from '@shade/server';
|
import { verifyPayload, validateAddress } from '@shade/server';
|
||||||
import type { InboxStore } from './store.js';
|
import type { InboxStore } from './store.js';
|
||||||
import type { InboxServerEvents } from './events.js';
|
import type { InboxServerEvents } from './events.js';
|
||||||
|
import { PresenceTracker, type TrackedBridgeKind } from './presence.js';
|
||||||
|
import { BridgeDeliveryLog } from './bridge-delivery-log.js';
|
||||||
|
|
||||||
export type BridgeKind = 'stream' | 'poll' | 'ws';
|
export type BridgeKind = 'stream' | 'poll' | 'ws';
|
||||||
|
/**
|
||||||
|
* Wire-protocol kind tag for `/v1/bridge/presence`. Distinct from
|
||||||
|
* `BridgeKind` because the canonical signed payload is shaped
|
||||||
|
* differently (`watched: string[]` instead of `since: number`).
|
||||||
|
*/
|
||||||
|
export type PresenceKind = 'presence';
|
||||||
|
|
||||||
export interface BridgeRoutesOptions {
|
export interface BridgeRoutesOptions {
|
||||||
store: InboxStore;
|
store: InboxStore;
|
||||||
@@ -60,6 +68,23 @@ export interface BridgeRoutesOptions {
|
|||||||
* Default 1_000.
|
* Default 1_000.
|
||||||
*/
|
*/
|
||||||
fallbackPollIntervalMs?: number;
|
fallbackPollIntervalMs?: number;
|
||||||
|
/**
|
||||||
|
* Inject an existing presence tracker. Useful when multiple
|
||||||
|
* `createBridgeRoutes` calls need to share state (e.g. mounting the
|
||||||
|
* routes under several hostnames in a single process). When omitted,
|
||||||
|
* the bridge auto-creates an internal tracker bound to `events`.
|
||||||
|
*/
|
||||||
|
presenceTracker?: PresenceTracker;
|
||||||
|
/**
|
||||||
|
* V4.8.4 — shared bridge delivery log. After every successful WS /
|
||||||
|
* SSE push we record `(address, msgId, now)` here so the inbox-fetch
|
||||||
|
* route can suppress the same blob from a subsequent inbox-poll
|
||||||
|
* within the log's grace window. Pass the same instance to
|
||||||
|
* `createInboxRoutes` (or use the auto-created one returned in
|
||||||
|
* `bridgeRoutes.bridgeDeliveryLog`). When omitted, the bridge
|
||||||
|
* auto-creates its own log.
|
||||||
|
*/
|
||||||
|
bridgeDeliveryLog?: BridgeDeliveryLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface VerifiedBridgeRequest {
|
interface VerifiedBridgeRequest {
|
||||||
@@ -68,6 +93,13 @@ interface VerifiedBridgeRequest {
|
|||||||
since: number;
|
since: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface VerifiedPresenceRequest {
|
||||||
|
/** The watcher's address (signer of the request). */
|
||||||
|
address: string;
|
||||||
|
/** Addresses whose presence the watcher is asking to track. */
|
||||||
|
watched: string[];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the bridge Hono router and a paired Bun-WebSocket handler.
|
* Build the bridge Hono router and a paired Bun-WebSocket handler.
|
||||||
*
|
*
|
||||||
@@ -80,6 +112,15 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
app: Hono;
|
app: Hono;
|
||||||
/** Pass to `Bun.serve({ websocket })`. Undefined if Bun adapter is missing. */
|
/** Pass to `Bun.serve({ websocket })`. Undefined if Bun adapter is missing. */
|
||||||
websocket: unknown;
|
websocket: unknown;
|
||||||
|
/** Live presence tracker. Tests + observers can read it; routes update it. */
|
||||||
|
presence: PresenceTracker;
|
||||||
|
/**
|
||||||
|
* V4.8.4 — the shared bridge-delivery log this router writes to on
|
||||||
|
* every successful push. Wire the same instance into
|
||||||
|
* `createInboxRoutes({ bridgeDeliveryLog })` so the inbox-fetch route
|
||||||
|
* can suppress recently-pushed blobs.
|
||||||
|
*/
|
||||||
|
bridgeDeliveryLog: BridgeDeliveryLog;
|
||||||
} {
|
} {
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
const pageLimit = opts.pageLimit ?? 50;
|
const pageLimit = opts.pageLimit ?? 50;
|
||||||
@@ -87,6 +128,8 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
const longPollDefault = opts.longPollTimeoutMs ?? 25_000;
|
const longPollDefault = opts.longPollTimeoutMs ?? 25_000;
|
||||||
const longPollMax = opts.longPollMaxTimeoutMs ?? 55_000;
|
const longPollMax = opts.longPollMaxTimeoutMs ?? 55_000;
|
||||||
const fallbackPollIntervalMs = opts.fallbackPollIntervalMs ?? 1_000;
|
const fallbackPollIntervalMs = opts.fallbackPollIntervalMs ?? 1_000;
|
||||||
|
const presence = opts.presenceTracker ?? new PresenceTracker(opts.events ?? null);
|
||||||
|
const bridgeDeliveryLog = opts.bridgeDeliveryLog ?? new BridgeDeliveryLog();
|
||||||
|
|
||||||
app.onError((err, c) => {
|
app.onError((err, c) => {
|
||||||
if (err instanceof ShadeError) {
|
if (err instanceof ShadeError) {
|
||||||
@@ -102,17 +145,37 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
const verified = await verifyBridgeAuth(c, opts, 'stream');
|
const verified = await verifyBridgeAuth(c, opts, 'stream');
|
||||||
return streamSSE(c, async (stream) => {
|
return streamSSE(c, async (stream) => {
|
||||||
const address = verified.address;
|
const address = verified.address;
|
||||||
|
const connId = presence.newConnectionId();
|
||||||
|
presence.markConnected(address, 'sse', connId);
|
||||||
|
let presenceClosed = false;
|
||||||
|
const closePresence = (reason: 'closed' | 'error'): void => {
|
||||||
|
if (presenceClosed) return;
|
||||||
|
presenceClosed = true;
|
||||||
|
presence.markDisconnected(address, 'sse', connId, reason);
|
||||||
|
};
|
||||||
let cursor = verified.since;
|
let cursor = verified.since;
|
||||||
const writer = makeBlobWriter(opts.store, pageLimit);
|
const writer = makeBlobWriter(opts.store, pageLimit);
|
||||||
|
const delivered = new DeliveredIdLru();
|
||||||
|
|
||||||
|
const recordPush = (msgId: string): void => {
|
||||||
|
bridgeDeliveryLog.recordDelivered(address, msgId, Date.now());
|
||||||
|
};
|
||||||
|
|
||||||
// Initial backlog drain.
|
// Initial backlog drain.
|
||||||
const flushed = await flushTo(writer, address, cursor, async (blob) => {
|
const flushed = await flushTo(
|
||||||
|
writer,
|
||||||
|
address,
|
||||||
|
cursor,
|
||||||
|
async (blob) => {
|
||||||
await stream.writeSSE({
|
await stream.writeSSE({
|
||||||
id: String(blob.receivedAt),
|
id: String(blob.receivedAt),
|
||||||
event: 'envelope',
|
event: 'envelope',
|
||||||
data: JSON.stringify(serializeBlob(blob)),
|
data: JSON.stringify(serializeBlob(blob)),
|
||||||
});
|
});
|
||||||
});
|
recordPush(blob.msgId);
|
||||||
|
},
|
||||||
|
delivered,
|
||||||
|
);
|
||||||
cursor = Math.max(cursor, flushed);
|
cursor = Math.max(cursor, flushed);
|
||||||
|
|
||||||
// Hook up event-driven push if available, else fall back to a poll
|
// Hook up event-driven push if available, else fall back to a poll
|
||||||
@@ -124,19 +187,32 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
const triggerFlush = (): void => {
|
const triggerFlush = (): void => {
|
||||||
signalled = true;
|
signalled = true;
|
||||||
// Serialize fan-in so concurrent triggers don't double-fetch.
|
// Serialize fan-in so concurrent triggers don't double-fetch.
|
||||||
pendingFlushPromise = pendingFlushPromise.then(async () => {
|
// `.catch(() => {})` keeps the chain alive across transient
|
||||||
|
// emit failures (e.g. a closed SSE write throws) — without it
|
||||||
|
// one rejection silently kills every future flush on this
|
||||||
|
// connection.
|
||||||
|
pendingFlushPromise = pendingFlushPromise
|
||||||
|
.then(async () => {
|
||||||
while (signalled) {
|
while (signalled) {
|
||||||
signalled = false;
|
signalled = false;
|
||||||
const drained = await flushTo(writer, address, cursor, async (blob) => {
|
const drained = await flushTo(
|
||||||
|
writer,
|
||||||
|
address,
|
||||||
|
cursor,
|
||||||
|
async (blob) => {
|
||||||
await stream.writeSSE({
|
await stream.writeSSE({
|
||||||
id: String(blob.receivedAt),
|
id: String(blob.receivedAt),
|
||||||
event: 'envelope',
|
event: 'envelope',
|
||||||
data: JSON.stringify(serializeBlob(blob)),
|
data: JSON.stringify(serializeBlob(blob)),
|
||||||
});
|
});
|
||||||
});
|
recordPush(blob.msgId);
|
||||||
|
},
|
||||||
|
delivered,
|
||||||
|
);
|
||||||
if (drained > cursor) cursor = drained;
|
if (drained > cursor) cursor = drained;
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
.catch(() => {});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (opts.events) {
|
if (opts.events) {
|
||||||
@@ -163,6 +239,68 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
clearInterval(fallbackTimer);
|
clearInterval(fallbackTimer);
|
||||||
clearInterval(heartbeat);
|
clearInterval(heartbeat);
|
||||||
await pendingFlushPromise.catch(() => {});
|
await pendingFlushPromise.catch(() => {});
|
||||||
|
closePresence('closed');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── Presence (V4.7) ──────────────────────────────────────────
|
||||||
|
// SSE feed of `peer_connected` / `peer_disconnected` events filtered
|
||||||
|
// by a watcher-supplied address list. Subscribing does NOT count as
|
||||||
|
// a peer-bridge connection (it doesn't call `markConnected`) so
|
||||||
|
// monitoring presence doesn't make you appear online to others.
|
||||||
|
app.get('/v1/bridge/presence', async (c) => {
|
||||||
|
const verified = await verifyPresenceAuth(c, opts);
|
||||||
|
return streamSSE(c, async (stream) => {
|
||||||
|
const watched = new Set(verified.watched);
|
||||||
|
|
||||||
|
// Initial snapshot — one frame per watched address with current
|
||||||
|
// status. Lets subscribers render UI immediately rather than
|
||||||
|
// waiting for the next state change.
|
||||||
|
const now = Date.now();
|
||||||
|
for (const addr of verified.watched) {
|
||||||
|
await stream.writeSSE({
|
||||||
|
event: 'presence',
|
||||||
|
data: JSON.stringify({
|
||||||
|
address: addr,
|
||||||
|
status: presence.isOnline(addr) ? 'online' : 'offline',
|
||||||
|
at: now,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let unsubscribe: (() => void) | null = null;
|
||||||
|
if (opts.events) {
|
||||||
|
unsubscribe = opts.events.on((e) => {
|
||||||
|
if (e.name !== 'inbox.peer_connected' && e.name !== 'inbox.peer_disconnected') return;
|
||||||
|
const data = e.data as { address: string; bridgeKind: TrackedBridgeKind };
|
||||||
|
if (!watched.has(data.address)) return;
|
||||||
|
const status = e.name === 'inbox.peer_connected' ? 'online' : 'offline';
|
||||||
|
// Fire-and-forget: drop the frame if the stream has gone away.
|
||||||
|
void stream
|
||||||
|
.writeSSE({
|
||||||
|
event: 'presence',
|
||||||
|
data: JSON.stringify({
|
||||||
|
address: data.address,
|
||||||
|
status,
|
||||||
|
at: e.timestamp,
|
||||||
|
via: data.bridgeKind,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.catch(() => {});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const heartbeat = setInterval(() => {
|
||||||
|
stream.write(`: ping ${Date.now()}\n\n`).catch(() => {});
|
||||||
|
}, heartbeatIntervalMs);
|
||||||
|
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
const sig = c.req.raw.signal;
|
||||||
|
if (sig.aborted) return resolve();
|
||||||
|
sig.addEventListener('abort', () => resolve(), { once: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
unsubscribe?.();
|
||||||
|
clearInterval(heartbeat);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -183,6 +321,8 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
limit: pageLimit,
|
limit: pageLimit,
|
||||||
});
|
});
|
||||||
if (blobs.length > 0) {
|
if (blobs.length > 0) {
|
||||||
|
const now = Date.now();
|
||||||
|
for (const b of blobs) bridgeDeliveryLog.recordDelivered(verified.address, b.msgId, now);
|
||||||
return c.json(buildPollResponse(blobs, verified.since));
|
return c.json(buildPollResponse(blobs, verified.since));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,6 +337,8 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
fallbackPollIntervalMs,
|
fallbackPollIntervalMs,
|
||||||
abortSignal: c.req.raw.signal,
|
abortSignal: c.req.raw.signal,
|
||||||
});
|
});
|
||||||
|
const now = Date.now();
|
||||||
|
for (const b of blobs) bridgeDeliveryLog.recordDelivered(verified.address, b.msgId, now);
|
||||||
return c.json(buildPollResponse(blobs, verified.since));
|
return c.json(buildPollResponse(blobs, verified.since));
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -230,30 +372,51 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const address = verified.address;
|
const address = verified.address;
|
||||||
|
const connId = presence.newConnectionId();
|
||||||
let cursor = verified.since;
|
let cursor = verified.since;
|
||||||
const writer = makeBlobWriter(opts.store, pageLimit);
|
const writer = makeBlobWriter(opts.store, pageLimit);
|
||||||
|
const delivered = new DeliveredIdLru();
|
||||||
let unsubscribe: (() => void) | null = null;
|
let unsubscribe: (() => void) | null = null;
|
||||||
let fallbackTimer: ReturnType<typeof setInterval> | null = null;
|
let fallbackTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
let pendingFlushPromise: Promise<void> = Promise.resolve();
|
let pendingFlushPromise: Promise<void> = Promise.resolve();
|
||||||
let signalled = false;
|
let signalled = false;
|
||||||
let connected = true;
|
let connected = true;
|
||||||
|
let presenceClosed = false;
|
||||||
|
const closePresence = (reason: 'closed' | 'error'): void => {
|
||||||
|
if (presenceClosed) return;
|
||||||
|
presenceClosed = true;
|
||||||
|
presence.markDisconnected(address, 'ws', connId, reason);
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
onOpen(_evt: unknown, ws: {
|
onOpen(_evt: unknown, ws: {
|
||||||
send: (data: string) => void;
|
send: (data: string) => void;
|
||||||
close: (code?: number, reason?: string) => void;
|
close: (code?: number, reason?: string) => void;
|
||||||
}) {
|
}) {
|
||||||
|
presence.markConnected(address, 'ws', connId);
|
||||||
const triggerFlush = (): void => {
|
const triggerFlush = (): void => {
|
||||||
signalled = true;
|
signalled = true;
|
||||||
pendingFlushPromise = pendingFlushPromise.then(async () => {
|
// `.catch(() => {})` mirrors the SSE chain — keeps the
|
||||||
|
// pending-flush queue alive across transient ws.send errors
|
||||||
|
// (e.g. partial close, backpressure overflow).
|
||||||
|
pendingFlushPromise = pendingFlushPromise
|
||||||
|
.then(async () => {
|
||||||
while (signalled && connected) {
|
while (signalled && connected) {
|
||||||
signalled = false;
|
signalled = false;
|
||||||
const drained = await flushTo(writer, address, cursor, async (blob) => {
|
const drained = await flushTo(
|
||||||
|
writer,
|
||||||
|
address,
|
||||||
|
cursor,
|
||||||
|
async (blob) => {
|
||||||
ws.send(JSON.stringify(serializeBlob(blob)));
|
ws.send(JSON.stringify(serializeBlob(blob)));
|
||||||
});
|
bridgeDeliveryLog.recordDelivered(address, blob.msgId, Date.now());
|
||||||
|
},
|
||||||
|
delivered,
|
||||||
|
);
|
||||||
if (drained > cursor) cursor = drained;
|
if (drained > cursor) cursor = drained;
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
.catch(() => {});
|
||||||
};
|
};
|
||||||
if (opts.events) {
|
if (opts.events) {
|
||||||
unsubscribe = opts.events.on((e) => {
|
unsubscribe = opts.events.on((e) => {
|
||||||
@@ -269,12 +432,19 @@ export function createBridgeRoutes(opts: BridgeRoutesOptions): {
|
|||||||
connected = false;
|
connected = false;
|
||||||
unsubscribe?.();
|
unsubscribe?.();
|
||||||
if (fallbackTimer) clearInterval(fallbackTimer);
|
if (fallbackTimer) clearInterval(fallbackTimer);
|
||||||
|
closePresence('closed');
|
||||||
|
},
|
||||||
|
onError() {
|
||||||
|
connected = false;
|
||||||
|
unsubscribe?.();
|
||||||
|
if (fallbackTimer) clearInterval(fallbackTimer);
|
||||||
|
closePresence('error');
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
return { app, websocket };
|
return { app, websocket, presence, bridgeDeliveryLog };
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── helpers ──────────────────────────────────────────────────
|
// ─── helpers ──────────────────────────────────────────────────
|
||||||
@@ -321,11 +491,75 @@ async function verifyBridgeAuth(
|
|||||||
return { address, kind: kind as BridgeKind, since };
|
return { address, kind: kind as BridgeKind, since };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MAX_WATCHED_ADDRESSES = 64;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify a `/v1/bridge/presence` request.
|
||||||
|
*
|
||||||
|
* Signed canonical payload: `{address, kind: 'presence', watched: string[],
|
||||||
|
* signedAt}`. The watcher's address must be a registered inbox; the
|
||||||
|
* signature is verified against the registered owner key for that
|
||||||
|
* address. The `watched` list bounds what the subscription will
|
||||||
|
* receive — server-side filtering is enforced inside the handler.
|
||||||
|
*/
|
||||||
|
async function verifyPresenceAuth(
|
||||||
|
c: Context,
|
||||||
|
opts: BridgeRoutesOptions,
|
||||||
|
): Promise<VerifiedPresenceRequest> {
|
||||||
|
const url = new URL(c.req.url);
|
||||||
|
const qs = url.searchParams;
|
||||||
|
const address = validateAddress(qs.get('address'));
|
||||||
|
const kind = qs.get('kind');
|
||||||
|
if (kind !== 'presence') {
|
||||||
|
throw new ValidationError(`bridge kind mismatch: expected presence`, 'kind');
|
||||||
|
}
|
||||||
|
const watchedRaw = qs.get('watched');
|
||||||
|
if (watchedRaw === null) throw new ValidationError('missing watched', 'watched');
|
||||||
|
// Empty subscription is allowed (subscribe to nothing — useful for a
|
||||||
|
// client that intends to call addPeer right after open). A null/
|
||||||
|
// missing param is still rejected so the canonicalization is
|
||||||
|
// unambiguous.
|
||||||
|
const watched =
|
||||||
|
watchedRaw === ''
|
||||||
|
? []
|
||||||
|
: watchedRaw.split(',').map((a) => validateAddress(a));
|
||||||
|
if (watched.length > MAX_WATCHED_ADDRESSES) {
|
||||||
|
throw new ValidationError(
|
||||||
|
`watched list too large: ${watched.length} > ${MAX_WATCHED_ADDRESSES}`,
|
||||||
|
'watched',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const signedAtStr = qs.get('signedAt');
|
||||||
|
const signature = qs.get('signature');
|
||||||
|
if (signedAtStr === null) throw new ValidationError('missing signedAt', 'signedAt');
|
||||||
|
if (!signature) throw new ValidationError('missing signature', 'signature');
|
||||||
|
const signedAt = Number(signedAtStr);
|
||||||
|
if (!Number.isFinite(signedAt)) {
|
||||||
|
throw new ValidationError('signedAt must be a number', 'signedAt');
|
||||||
|
}
|
||||||
|
|
||||||
|
const owner = await opts.store.getAddressOwner(address);
|
||||||
|
if (!owner) {
|
||||||
|
throw new UnauthorizedError(`address ${address} is not registered`);
|
||||||
|
}
|
||||||
|
|
||||||
|
await verifyPayload(opts.crypto, owner, {
|
||||||
|
address,
|
||||||
|
kind,
|
||||||
|
watched,
|
||||||
|
signedAt,
|
||||||
|
signature,
|
||||||
|
});
|
||||||
|
return { address, watched };
|
||||||
|
}
|
||||||
|
|
||||||
interface BlobRow {
|
interface BlobRow {
|
||||||
msgId: string;
|
msgId: string;
|
||||||
ciphertext: Uint8Array;
|
ciphertext: Uint8Array;
|
||||||
receivedAt: number;
|
receivedAt: number;
|
||||||
expiresAt: number;
|
expiresAt: number;
|
||||||
|
/** V4.8 — relay-captured sender fingerprint. Optional for legacy rows. */
|
||||||
|
senderFp?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BlobWriter {
|
interface BlobWriter {
|
||||||
@@ -345,11 +579,41 @@ function makeBlobWriter(store: InboxStore, pageLimit: number): BlobWriter {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Per-connection bounded msgId tracker — defense in depth against duplicate
|
||||||
|
* delivery of the same blob to the same bridge socket. Cursor pagination
|
||||||
|
* already guarantees uniqueness in the happy path, but a dedup gate at the
|
||||||
|
* emit boundary catches any subtle bug (e.g. a flushTo race, a future
|
||||||
|
* refactor, an event-emit retry) without changing wire semantics.
|
||||||
|
*
|
||||||
|
* The cap is intentionally large enough to cover any realistic bridge
|
||||||
|
* pageLimit and small enough to bound memory under long-running streams.
|
||||||
|
*/
|
||||||
|
const DELIVERED_LRU_CAP = 4096;
|
||||||
|
|
||||||
|
class DeliveredIdLru {
|
||||||
|
private readonly seen = new Set<string>();
|
||||||
|
private readonly order: string[] = [];
|
||||||
|
|
||||||
|
/** Returns true if `msgId` has not been seen on this connection yet. */
|
||||||
|
add(msgId: string): boolean {
|
||||||
|
if (this.seen.has(msgId)) return false;
|
||||||
|
this.seen.add(msgId);
|
||||||
|
this.order.push(msgId);
|
||||||
|
if (this.order.length > DELIVERED_LRU_CAP) {
|
||||||
|
const evicted = this.order.shift()!;
|
||||||
|
this.seen.delete(evicted);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function flushTo(
|
async function flushTo(
|
||||||
writer: BlobWriter,
|
writer: BlobWriter,
|
||||||
address: string,
|
address: string,
|
||||||
startCursor: number,
|
startCursor: number,
|
||||||
emit: (blob: BlobRow) => Promise<void>,
|
emit: (blob: BlobRow) => Promise<void>,
|
||||||
|
delivered?: DeliveredIdLru,
|
||||||
): Promise<number> {
|
): Promise<number> {
|
||||||
let cursor = startCursor;
|
let cursor = startCursor;
|
||||||
// Drain page-by-page so a backlog larger than `pageLimit` still flushes.
|
// Drain page-by-page so a backlog larger than `pageLimit` still flushes.
|
||||||
@@ -358,7 +622,12 @@ async function flushTo(
|
|||||||
const page = await writer.fetchPage(address, cursor);
|
const page = await writer.fetchPage(address, cursor);
|
||||||
if (page.length === 0) break;
|
if (page.length === 0) break;
|
||||||
for (const row of page) {
|
for (const row of page) {
|
||||||
|
// Per-connection dedup gate — prevents the same msgId from being
|
||||||
|
// emitted twice if flushTo is somehow re-entered before the cursor
|
||||||
|
// catches up. See comment on `DeliveredIdLru`.
|
||||||
|
if (!delivered || delivered.add(row.msgId)) {
|
||||||
await emit(row);
|
await emit(row);
|
||||||
|
}
|
||||||
if (row.receivedAt > cursor) cursor = row.receivedAt;
|
if (row.receivedAt > cursor) cursor = row.receivedAt;
|
||||||
}
|
}
|
||||||
if (page.length === 0) break;
|
if (page.length === 0) break;
|
||||||
@@ -371,13 +640,26 @@ function serializeBlob(blob: BlobRow): {
|
|||||||
ciphertext: string;
|
ciphertext: string;
|
||||||
receivedAt: number;
|
receivedAt: number;
|
||||||
expiresAt: number;
|
expiresAt: number;
|
||||||
|
from?: string;
|
||||||
} {
|
} {
|
||||||
return {
|
const out: {
|
||||||
|
msgId: string;
|
||||||
|
ciphertext: string;
|
||||||
|
receivedAt: number;
|
||||||
|
expiresAt: number;
|
||||||
|
from?: string;
|
||||||
|
} = {
|
||||||
msgId: blob.msgId,
|
msgId: blob.msgId,
|
||||||
ciphertext: toBase64(blob.ciphertext),
|
ciphertext: toBase64(blob.ciphertext),
|
||||||
receivedAt: blob.receivedAt,
|
receivedAt: blob.receivedAt,
|
||||||
expiresAt: blob.expiresAt,
|
expiresAt: blob.expiresAt,
|
||||||
};
|
};
|
||||||
|
// V4.8 — relay-captured sender fingerprint. The transport-bridge
|
||||||
|
// wire format already accepts `from`; populating it lets receivers
|
||||||
|
// bootstrap unknown-sender first-contact via `shade.receive('fp:<hex>',
|
||||||
|
// env)` without requiring an out-of-band sender hint.
|
||||||
|
if (blob.senderFp) out.from = blob.senderFp;
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildPollResponse(blobs: BlobRow[], sinceFallback: number): {
|
function buildPollResponse(blobs: BlobRow[], sinceFallback: number): {
|
||||||
|
|||||||
@@ -21,6 +21,19 @@ export interface InboxServerEventMap {
|
|||||||
'inbox.expired_purged': { count: number };
|
'inbox.expired_purged': { count: number };
|
||||||
'inbox.rate_limited': { route: string; key: string };
|
'inbox.rate_limited': { route: string; key: string };
|
||||||
'inbox.quota_rejected': { address: string; reason: 'address-quota' | 'sender-quota' | 'body-too-large' };
|
'inbox.quota_rejected': { address: string; reason: 'address-quota' | 'sender-quota' | 'body-too-large' };
|
||||||
|
// V4.7 — bridge presence transitions. Emitted on the 0↔1 boundary
|
||||||
|
// across tracked transports for a given address. Long-poll is
|
||||||
|
// intentionally NOT tracked: an LP client toggles in/out of a request
|
||||||
|
// every few seconds, and the resulting flapping would dominate the
|
||||||
|
// event stream. Push transports (WS, SSE) are also the only ones
|
||||||
|
// where the ~50ms revoke window for `BroadcastChannel.removeMember`
|
||||||
|
// matters — long-poll users are already on a slow path.
|
||||||
|
'inbox.peer_connected': { address: string; bridgeKind: 'ws' | 'sse' };
|
||||||
|
'inbox.peer_disconnected': {
|
||||||
|
address: string;
|
||||||
|
bridgeKind: 'ws' | 'sse';
|
||||||
|
reason: 'closed' | 'error';
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export type InboxServerEventName = keyof InboxServerEventMap;
|
export type InboxServerEventName = keyof InboxServerEventMap;
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
import type { Hono } from 'hono';
|
import { Hono } from 'hono';
|
||||||
import type { CryptoProvider } from '@shade/core';
|
import type { CryptoProvider } from '@shade/core';
|
||||||
import { createInboxRoutes, type InboxRoutesOptions } from './routes.js';
|
import { createInboxRoutes, type InboxRoutesOptions } from './routes.js';
|
||||||
import { MemoryInboxStore } from './memory-store.js';
|
import { MemoryInboxStore } from './memory-store.js';
|
||||||
import type { InboxStore } from './store.js';
|
import type { InboxStore } from './store.js';
|
||||||
import { InboxServerEvents } from './events.js';
|
import { InboxServerEvents } from './events.js';
|
||||||
|
import { createBlobRoutes, type BlobRoutesOptions } from './blob-routes.js';
|
||||||
|
import { MemoryBlobStore } from './memory-blob-store.js';
|
||||||
|
import type { BlobStore } from './blob-store.js';
|
||||||
|
|
||||||
export { createInboxRoutes } from './routes.js';
|
export { createInboxRoutes } from './routes.js';
|
||||||
export type { InboxRoutesOptions } from './routes.js';
|
export type { InboxRoutesOptions } from './routes.js';
|
||||||
@@ -32,6 +35,14 @@ export {
|
|||||||
export type { InboxQuotaConfig } from './quota.js';
|
export type { InboxQuotaConfig } from './quota.js';
|
||||||
export { createBridgeRoutes } from './bridge.js';
|
export { createBridgeRoutes } from './bridge.js';
|
||||||
export type { BridgeRoutesOptions, BridgeKind } from './bridge.js';
|
export type { BridgeRoutesOptions, BridgeKind } from './bridge.js';
|
||||||
|
export { PresenceTracker } from './presence.js';
|
||||||
|
export type { TrackedBridgeKind } from './presence.js';
|
||||||
|
export { BridgeDeliveryLog } from './bridge-delivery-log.js';
|
||||||
|
export type { BridgeDeliveryLogOptions } from './bridge-delivery-log.js';
|
||||||
|
export { createBlobRoutes, DEFAULT_MAX_BLOB_BYTES } from './blob-routes.js';
|
||||||
|
export type { BlobRoutesOptions } from './blob-routes.js';
|
||||||
|
export { MemoryBlobStore } from './memory-blob-store.js';
|
||||||
|
export type { BlobStore, BlobSlotRecord, PutBlobResult } from './blob-store.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a standalone Shade Inbox Server.
|
* Create a standalone Shade Inbox Server.
|
||||||
@@ -44,17 +55,45 @@ export type { BridgeRoutesOptions, BridgeKind } from './bridge.js';
|
|||||||
* const app = new Hono();
|
* const app = new Hono();
|
||||||
* app.route('/', createInboxServer({ crypto }));
|
* app.route('/', createInboxServer({ crypto }));
|
||||||
*/
|
*/
|
||||||
export function createInboxServer(options: {
|
export function createInboxServer(
|
||||||
|
options: {
|
||||||
crypto: CryptoProvider;
|
crypto: CryptoProvider;
|
||||||
store?: InboxStore;
|
store?: InboxStore;
|
||||||
disableRateLimit?: boolean;
|
disableRateLimit?: boolean;
|
||||||
events?: InboxServerEvents;
|
events?: InboxServerEvents;
|
||||||
} & Pick<InboxRoutesOptions, 'observability' | 'quota'>): Hono {
|
/**
|
||||||
|
* V4.9 — when supplied, mounts the encrypted-blob primitive
|
||||||
|
* (`/v1/blob/<slotId>`) on the same Hono app. Pass `null` to
|
||||||
|
* explicitly opt out; omit to default to a `MemoryBlobStore`.
|
||||||
|
*/
|
||||||
|
blobStore?: BlobStore | null;
|
||||||
|
blobOptions?: Pick<BlobRoutesOptions, 'maxBlobBytes'>;
|
||||||
|
} & Pick<InboxRoutesOptions, 'observability' | 'quota' | 'bridgeDeliveryLog'>,
|
||||||
|
): Hono {
|
||||||
const store = options.store ?? new MemoryInboxStore();
|
const store = options.store ?? new MemoryInboxStore();
|
||||||
const routesOptions: InboxRoutesOptions = {};
|
const routesOptions: InboxRoutesOptions = {};
|
||||||
if (options.disableRateLimit !== undefined) routesOptions.disableRateLimit = options.disableRateLimit;
|
if (options.disableRateLimit !== undefined) routesOptions.disableRateLimit = options.disableRateLimit;
|
||||||
if (options.events !== undefined) routesOptions.events = options.events;
|
if (options.events !== undefined) routesOptions.events = options.events;
|
||||||
if (options.observability !== undefined) routesOptions.observability = options.observability;
|
if (options.observability !== undefined) routesOptions.observability = options.observability;
|
||||||
if (options.quota !== undefined) routesOptions.quota = options.quota;
|
if (options.quota !== undefined) routesOptions.quota = options.quota;
|
||||||
return createInboxRoutes(store, options.crypto, routesOptions);
|
if (options.bridgeDeliveryLog !== undefined) routesOptions.bridgeDeliveryLog = options.bridgeDeliveryLog;
|
||||||
|
|
||||||
|
const inboxApp = createInboxRoutes(store, options.crypto, routesOptions);
|
||||||
|
|
||||||
|
// Compose with the blob primitive unless explicitly disabled. The
|
||||||
|
// blob routes share the same Hono app so a single port serves both.
|
||||||
|
if (options.blobStore === null) return inboxApp;
|
||||||
|
const blobStore = options.blobStore ?? new MemoryBlobStore();
|
||||||
|
const blobRoutesOptions: BlobRoutesOptions = {};
|
||||||
|
if (options.disableRateLimit !== undefined) blobRoutesOptions.disableRateLimit = options.disableRateLimit;
|
||||||
|
if (options.observability !== undefined) blobRoutesOptions.observability = options.observability;
|
||||||
|
if (options.blobOptions?.maxBlobBytes !== undefined) {
|
||||||
|
blobRoutesOptions.maxBlobBytes = options.blobOptions.maxBlobBytes;
|
||||||
|
}
|
||||||
|
const blobApp = createBlobRoutes(blobStore, options.crypto, blobRoutesOptions);
|
||||||
|
|
||||||
|
const composed = new Hono();
|
||||||
|
composed.route('/', inboxApp);
|
||||||
|
composed.route('/', blobApp);
|
||||||
|
return composed;
|
||||||
}
|
}
|
||||||
|
|||||||
85
packages/shade-inbox-server/src/memory-blob-store.ts
Normal file
85
packages/shade-inbox-server/src/memory-blob-store.ts
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import { ConflictError, PreconditionFailedError } from '@shade/core';
|
||||||
|
import type { BlobStore, BlobSlotRecord, PutBlobResult } from './blob-store.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In-memory BlobStore — used in tests and as the default fallback when
|
||||||
|
* no SQLite/Postgres URL is configured. Rows are kept in a single Map.
|
||||||
|
*
|
||||||
|
* Etag is a strictly-monotonic per-process counter — guarantees a total
|
||||||
|
* order across writes even when many land in the same millisecond. (We
|
||||||
|
* could scope it per-slot, but a global counter keeps the implementation
|
||||||
|
* trivial and the etag values still uniquely identify the write that
|
||||||
|
* produced them, which is all CAS needs.)
|
||||||
|
*/
|
||||||
|
export class MemoryBlobStore implements BlobStore {
|
||||||
|
private slots = new Map<string, BlobSlotRecord>();
|
||||||
|
private nextEtag = 0;
|
||||||
|
|
||||||
|
async get(slotId: string): Promise<BlobSlotRecord | null> {
|
||||||
|
const r = this.slots.get(slotId);
|
||||||
|
if (!r) return null;
|
||||||
|
return {
|
||||||
|
slotId: r.slotId,
|
||||||
|
blob: new Uint8Array(r.blob),
|
||||||
|
ownerPubkey: new Uint8Array(r.ownerPubkey),
|
||||||
|
etag: r.etag,
|
||||||
|
updatedAt: r.updatedAt,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async put(args: {
|
||||||
|
slotId: string;
|
||||||
|
blob: Uint8Array;
|
||||||
|
ownerPubkey: Uint8Array;
|
||||||
|
expectedEtag: number | '*' | undefined;
|
||||||
|
now: number;
|
||||||
|
}): Promise<PutBlobResult> {
|
||||||
|
const existing = this.slots.get(args.slotId);
|
||||||
|
|
||||||
|
if (!existing) {
|
||||||
|
// Empty slot. `ifMatch: '*'` per RFC 7232 still fails — there is
|
||||||
|
// no entity to match. A numeric etag also fails (we have nothing
|
||||||
|
// to compare against).
|
||||||
|
if (args.expectedEtag !== undefined) {
|
||||||
|
throw new PreconditionFailedError(
|
||||||
|
`Slot ${args.slotId} does not exist; cannot match ifMatch=${String(args.expectedEtag)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.nextEtag = Math.max(this.nextEtag + 1, args.now);
|
||||||
|
const etag = this.nextEtag;
|
||||||
|
this.slots.set(args.slotId, {
|
||||||
|
slotId: args.slotId,
|
||||||
|
blob: new Uint8Array(args.blob),
|
||||||
|
ownerPubkey: new Uint8Array(args.ownerPubkey),
|
||||||
|
etag,
|
||||||
|
updatedAt: args.now,
|
||||||
|
});
|
||||||
|
return { created: true, etag, updatedAt: args.now };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slot exists. Pubkey check is the route layer's job — by the time
|
||||||
|
// we're here the signature has already been verified against
|
||||||
|
// `existing.ownerPubkey`.
|
||||||
|
if (args.expectedEtag === undefined) {
|
||||||
|
throw new ConflictError(
|
||||||
|
`Slot ${args.slotId} already exists; supply ifMatch to update`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (args.expectedEtag !== '*' && args.expectedEtag !== existing.etag) {
|
||||||
|
throw new PreconditionFailedError(
|
||||||
|
`Slot ${args.slotId} etag mismatch: expected=${args.expectedEtag} actual=${existing.etag}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.nextEtag = Math.max(this.nextEtag + 1, args.now);
|
||||||
|
const etag = this.nextEtag;
|
||||||
|
existing.blob = new Uint8Array(args.blob);
|
||||||
|
existing.etag = etag;
|
||||||
|
existing.updatedAt = args.now;
|
||||||
|
return { created: false, etag, updatedAt: args.now };
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(slotId: string): Promise<boolean> {
|
||||||
|
return this.slots.delete(slotId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ interface BlobRow {
|
|||||||
ciphertext: Uint8Array;
|
ciphertext: Uint8Array;
|
||||||
receivedAt: number;
|
receivedAt: number;
|
||||||
expiresAt: number;
|
expiresAt: number;
|
||||||
|
senderFp?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,6 +34,7 @@ export class MemoryInboxStore implements InboxStore {
|
|||||||
msgId: string;
|
msgId: string;
|
||||||
ciphertext: Uint8Array;
|
ciphertext: Uint8Array;
|
||||||
expiresAt: number;
|
expiresAt: number;
|
||||||
|
senderFp?: string;
|
||||||
}): Promise<{ created: boolean; receivedAt: number }> {
|
}): Promise<{ created: boolean; receivedAt: number }> {
|
||||||
const list = this.blobs.get(args.address) ?? [];
|
const list = this.blobs.get(args.address) ?? [];
|
||||||
const existing = list.find((r) => r.msgId === args.msgId);
|
const existing = list.find((r) => r.msgId === args.msgId);
|
||||||
@@ -41,12 +43,14 @@ export class MemoryInboxStore implements InboxStore {
|
|||||||
// multiple blobs land in the same millisecond.
|
// multiple blobs land in the same millisecond.
|
||||||
const receivedAt = Math.max(this.nextReceivedAt + 1, Date.now());
|
const receivedAt = Math.max(this.nextReceivedAt + 1, Date.now());
|
||||||
this.nextReceivedAt = receivedAt;
|
this.nextReceivedAt = receivedAt;
|
||||||
list.push({
|
const row: BlobRow = {
|
||||||
msgId: args.msgId,
|
msgId: args.msgId,
|
||||||
ciphertext: new Uint8Array(args.ciphertext),
|
ciphertext: new Uint8Array(args.ciphertext),
|
||||||
receivedAt,
|
receivedAt,
|
||||||
expiresAt: args.expiresAt,
|
expiresAt: args.expiresAt,
|
||||||
});
|
};
|
||||||
|
if (args.senderFp !== undefined) row.senderFp = args.senderFp;
|
||||||
|
list.push(row);
|
||||||
this.blobs.set(args.address, list);
|
this.blobs.set(args.address, list);
|
||||||
return { created: true, receivedAt };
|
return { created: true, receivedAt };
|
||||||
}
|
}
|
||||||
@@ -56,18 +60,36 @@ export class MemoryInboxStore implements InboxStore {
|
|||||||
sinceCursor: number;
|
sinceCursor: number;
|
||||||
now: number;
|
now: number;
|
||||||
limit: number;
|
limit: number;
|
||||||
}): Promise<Array<{ msgId: string; ciphertext: Uint8Array; receivedAt: number; expiresAt: number }>> {
|
}): Promise<
|
||||||
|
Array<{
|
||||||
|
msgId: string;
|
||||||
|
ciphertext: Uint8Array;
|
||||||
|
receivedAt: number;
|
||||||
|
expiresAt: number;
|
||||||
|
senderFp?: string;
|
||||||
|
}>
|
||||||
|
> {
|
||||||
const list = this.blobs.get(args.address) ?? [];
|
const list = this.blobs.get(args.address) ?? [];
|
||||||
return list
|
return list
|
||||||
.filter((r) => r.receivedAt > args.sinceCursor && r.expiresAt > args.now)
|
.filter((r) => r.receivedAt > args.sinceCursor && r.expiresAt > args.now)
|
||||||
.sort((a, b) => a.receivedAt - b.receivedAt)
|
.sort((a, b) => a.receivedAt - b.receivedAt)
|
||||||
.slice(0, args.limit)
|
.slice(0, args.limit)
|
||||||
.map((r) => ({
|
.map((r) => {
|
||||||
|
const out: {
|
||||||
|
msgId: string;
|
||||||
|
ciphertext: Uint8Array;
|
||||||
|
receivedAt: number;
|
||||||
|
expiresAt: number;
|
||||||
|
senderFp?: string;
|
||||||
|
} = {
|
||||||
msgId: r.msgId,
|
msgId: r.msgId,
|
||||||
ciphertext: new Uint8Array(r.ciphertext),
|
ciphertext: new Uint8Array(r.ciphertext),
|
||||||
receivedAt: r.receivedAt,
|
receivedAt: r.receivedAt,
|
||||||
expiresAt: r.expiresAt,
|
expiresAt: r.expiresAt,
|
||||||
}));
|
};
|
||||||
|
if (r.senderFp !== undefined) out.senderFp = r.senderFp;
|
||||||
|
return out;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteBlob(address: string, msgId: string): Promise<boolean> {
|
async deleteBlob(address: string, msgId: string): Promise<boolean> {
|
||||||
|
|||||||
75
packages/shade-inbox-server/src/presence.ts
Normal file
75
packages/shade-inbox-server/src/presence.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/**
|
||||||
|
* V4.7 — bridge-connection presence tracking.
|
||||||
|
*
|
||||||
|
* The bridge handlers (`/v1/bridge/stream` and `/v1/bridge/ws`) call
|
||||||
|
* `markConnected` on open and `markDisconnected` on close. The tracker
|
||||||
|
* keeps a per-address set of connection ids; the `inbox.peer_connected`
|
||||||
|
* / `inbox.peer_disconnected` events fire only on the 0↔1 boundary so
|
||||||
|
* that two simultaneous bridges (e.g. SSE + WS during a transport-
|
||||||
|
* fallback handover) collapse into a single connected/disconnected
|
||||||
|
* pair from the consumer's point of view.
|
||||||
|
*
|
||||||
|
* Long-poll (`/v1/bridge/poll`) is intentionally NOT tracked — see the
|
||||||
|
* note on `InboxServerEventMap` in `events.ts`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { InboxServerEvents } from './events.js';
|
||||||
|
|
||||||
|
export type TrackedBridgeKind = 'ws' | 'sse';
|
||||||
|
|
||||||
|
export class PresenceTracker {
|
||||||
|
private readonly connections = new Map<string, Set<string>>();
|
||||||
|
private nextConnId = 1;
|
||||||
|
|
||||||
|
constructor(private readonly events: InboxServerEvents | null) {}
|
||||||
|
|
||||||
|
/** Allocate a fresh connection id for `markConnected` / `markDisconnected`. */
|
||||||
|
newConnectionId(): string {
|
||||||
|
return `c${this.nextConnId++}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Snapshot: is `address` currently connected over any tracked transport?
|
||||||
|
* Used by `/v1/bridge/presence` to push the initial state to a new
|
||||||
|
* subscriber.
|
||||||
|
*/
|
||||||
|
isOnline(address: string): boolean {
|
||||||
|
const set = this.connections.get(address);
|
||||||
|
return set !== undefined && set.size > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
markConnected(address: string, bridgeKind: TrackedBridgeKind, connectionId: string): void {
|
||||||
|
let set = this.connections.get(address);
|
||||||
|
const wasOnline = set !== undefined && set.size > 0;
|
||||||
|
if (!set) {
|
||||||
|
set = new Set();
|
||||||
|
this.connections.set(address, set);
|
||||||
|
}
|
||||||
|
set.add(connectionId);
|
||||||
|
if (!wasOnline) {
|
||||||
|
this.events?.emit('inbox.peer_connected', { address, bridgeKind });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
markDisconnected(
|
||||||
|
address: string,
|
||||||
|
bridgeKind: TrackedBridgeKind,
|
||||||
|
connectionId: string,
|
||||||
|
reason: 'closed' | 'error',
|
||||||
|
): void {
|
||||||
|
const set = this.connections.get(address);
|
||||||
|
if (!set) return;
|
||||||
|
if (!set.delete(connectionId)) return;
|
||||||
|
if (set.size === 0) {
|
||||||
|
this.connections.delete(address);
|
||||||
|
this.events?.emit('inbox.peer_disconnected', { address, bridgeKind, reason });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Inspect the underlying map. Test/observability use only. */
|
||||||
|
snapshot(): Map<string, ReadonlySet<string>> {
|
||||||
|
return new Map(
|
||||||
|
Array.from(this.connections.entries(), ([k, v]) => [k, v as ReadonlySet<string>]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,7 @@ import type { InboxStore } from './store.js';
|
|||||||
import { InboxServerEvents, shortHash } from './events.js';
|
import { InboxServerEvents, shortHash } from './events.js';
|
||||||
import { computeMsgId, isValidMsgId, constantTimeStringEqual } from './msg-id.js';
|
import { computeMsgId, isValidMsgId, constantTimeStringEqual } from './msg-id.js';
|
||||||
import { clampTtl, DEFAULT_INBOX_QUOTA, type InboxQuotaConfig } from './quota.js';
|
import { clampTtl, DEFAULT_INBOX_QUOTA, type InboxQuotaConfig } from './quota.js';
|
||||||
|
import type { BridgeDeliveryLog } from './bridge-delivery-log.js';
|
||||||
|
|
||||||
/** Max metadata-only body size (no ciphertext). 64 KB is enough headroom. */
|
/** Max metadata-only body size (no ciphertext). 64 KB is enough headroom. */
|
||||||
const MAX_META_BODY_SIZE = 64 * 1024;
|
const MAX_META_BODY_SIZE = 64 * 1024;
|
||||||
@@ -54,6 +55,16 @@ export interface InboxRoutesOptions {
|
|||||||
observability?: ObservabilityHook;
|
observability?: ObservabilityHook;
|
||||||
/** Override quota policy. */
|
/** Override quota policy. */
|
||||||
quota?: Partial<InboxQuotaConfig>;
|
quota?: Partial<InboxQuotaConfig>;
|
||||||
|
/**
|
||||||
|
* V4.8.4 — shared bridge delivery log. When provided (and the same
|
||||||
|
* instance is wired into `createBridgeRoutes`), the inbox-fetch route
|
||||||
|
* filters out blobs already pushed via bridge within the log's grace
|
||||||
|
* window. Without this, a recipient that runs both a bridge
|
||||||
|
* subscription and inbox-poll receives the same envelope twice.
|
||||||
|
* Optional — leaving it unset preserves the pre-V4.8.4 behavior of
|
||||||
|
* always returning every blob the cursor matches.
|
||||||
|
*/
|
||||||
|
bridgeDeliveryLog?: BridgeDeliveryLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createInboxRoutes(
|
export function createInboxRoutes(
|
||||||
@@ -171,6 +182,7 @@ export function createInboxRoutes(
|
|||||||
await verifyPayload(crypto, owner, { ...body, address });
|
await verifyPayload(crypto, owner, { ...body, address });
|
||||||
|
|
||||||
await store.deleteAddress(address);
|
await store.deleteAddress(address);
|
||||||
|
options.bridgeDeliveryLog?.forgetAddress(address);
|
||||||
events?.emit('inbox.address_deleted', { address });
|
events?.emit('inbox.address_deleted', { address });
|
||||||
return c.json({ ok: true });
|
return c.json({ ok: true });
|
||||||
});
|
});
|
||||||
@@ -255,11 +267,19 @@ export function createInboxRoutes(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// V4.8: capture sender fingerprint at PUT time. The sender's
|
||||||
|
// signing key was just verified for this request, so the fingerprint
|
||||||
|
// is bound to the same authentication path that authorized the
|
||||||
|
// store. Surfaced on bridge push + inbox-fetch responses to
|
||||||
|
// bootstrap unknown-sender first-contact (X3DH pair handshake).
|
||||||
|
const senderFp = await shortHash(senderKey);
|
||||||
|
|
||||||
const result = await store.putBlob({
|
const result = await store.putBlob({
|
||||||
address,
|
address,
|
||||||
msgId,
|
msgId,
|
||||||
ciphertext: ctBytes,
|
ciphertext: ctBytes,
|
||||||
expiresAt,
|
expiresAt,
|
||||||
|
senderFp,
|
||||||
});
|
});
|
||||||
if (result.created) {
|
if (result.created) {
|
||||||
events?.emit('inbox.blob_stored', {
|
events?.emit('inbox.blob_stored', {
|
||||||
@@ -309,31 +329,63 @@ export function createInboxRoutes(
|
|||||||
await verifyPayload(crypto, owner, { ...body, address });
|
await verifyPayload(crypto, owner, { ...body, address });
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const rows = await store.fetchBlobs({
|
const rawRows = await store.fetchBlobs({
|
||||||
address,
|
address,
|
||||||
sinceCursor,
|
sinceCursor,
|
||||||
now,
|
now,
|
||||||
limit: quota.fetchPageLimit,
|
limit: quota.fetchPageLimit,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// V4.8.4 — drop blobs the bridge has already pushed to this address
|
||||||
|
// within the grace window. This is the cross-channel dedup gate that
|
||||||
|
// makes "one inbox.send ⇒ one observable delivery" hold even when
|
||||||
|
// the recipient runs both a bridge subscription and inbox-poll. The
|
||||||
|
// cursor still advances over the whole `rawRows` window so the
|
||||||
|
// client doesn't get stuck behind suppressed blobs — pollOnce uses
|
||||||
|
// `nextCursor` (max receivedAt seen by the server, suppressed or
|
||||||
|
// not) for the next fetch.
|
||||||
|
const rows = options.bridgeDeliveryLog
|
||||||
|
? options.bridgeDeliveryLog.filterRecent(address, rawRows, now)
|
||||||
|
: rawRows;
|
||||||
|
|
||||||
let bytes = 0;
|
let bytes = 0;
|
||||||
const blobs = rows.map((r) => {
|
const blobs = rows.map((r) => {
|
||||||
bytes += r.ciphertext.length;
|
bytes += r.ciphertext.length;
|
||||||
return {
|
const out: {
|
||||||
|
msgId: string;
|
||||||
|
ciphertext: string;
|
||||||
|
receivedAt: number;
|
||||||
|
expiresAt: number;
|
||||||
|
from?: string;
|
||||||
|
} = {
|
||||||
msgId: r.msgId,
|
msgId: r.msgId,
|
||||||
ciphertext: toBase64(r.ciphertext),
|
ciphertext: toBase64(r.ciphertext),
|
||||||
receivedAt: r.receivedAt,
|
receivedAt: r.receivedAt,
|
||||||
expiresAt: r.expiresAt,
|
expiresAt: r.expiresAt,
|
||||||
};
|
};
|
||||||
|
// V4.8: surface sender fingerprint when present. Empty for blobs
|
||||||
|
// persisted by a pre-4.8 relay that didn't track sender provenance.
|
||||||
|
if (r.senderFp) out.from = r.senderFp;
|
||||||
|
return out;
|
||||||
});
|
});
|
||||||
const nextCursor = rows.length > 0 ? rows[rows.length - 1]!.receivedAt : sinceCursor;
|
// Advance the cursor past the FULL rawRows window — including blobs
|
||||||
|
// we suppressed because the bridge already pushed them. If we
|
||||||
|
// anchored the cursor on `rows` only, suppressed blobs in the
|
||||||
|
// middle of the window would block all subsequent fetches forever
|
||||||
|
// (re-fetched on every poll, re-suppressed, no progress). The
|
||||||
|
// bridge-delivery contract is "the bridge frame is the canonical
|
||||||
|
// delivery"; if the recipient missed processing it, they fall back
|
||||||
|
// to ack-via-DELETE or the blob ages out at TTL — same as a
|
||||||
|
// recipient that crashes mid-handler in the no-bridge case.
|
||||||
|
const cursorAnchor = rawRows.length > 0 ? rawRows[rawRows.length - 1]!.receivedAt : sinceCursor;
|
||||||
|
const nextCursor = cursorAnchor;
|
||||||
|
|
||||||
events?.emit('inbox.blob_fetched', { address, count: blobs.length, bytes });
|
events?.emit('inbox.blob_fetched', { address, count: blobs.length, bytes });
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
blobs,
|
blobs,
|
||||||
cursor: nextCursor,
|
cursor: nextCursor,
|
||||||
hasMore: rows.length === quota.fetchPageLimit,
|
hasMore: rawRows.length === quota.fetchPageLimit,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -359,6 +411,10 @@ export function createInboxRoutes(
|
|||||||
if (removed) {
|
if (removed) {
|
||||||
events?.emit('inbox.blob_acked', { address, msgId });
|
events?.emit('inbox.blob_acked', { address, msgId });
|
||||||
}
|
}
|
||||||
|
// Drop any bridge-delivery mark — keeps the log bounded under
|
||||||
|
// sustained traffic (otherwise long-lived addresses accumulate
|
||||||
|
// entries even after the underlying blob is gone).
|
||||||
|
options.bridgeDeliveryLog?.forget(address, msgId);
|
||||||
return c.json({ ok: removed });
|
return c.json({ ok: removed });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -36,12 +36,20 @@ export interface InboxStore {
|
|||||||
* **Idempotent**: if a row already exists for `(address, msgId)` the
|
* **Idempotent**: if a row already exists for `(address, msgId)` the
|
||||||
* implementation MUST return `{ created: false }` and leave the existing
|
* implementation MUST return `{ created: false }` and leave the existing
|
||||||
* row untouched. A fresh insert returns `{ created: true, receivedAt }`.
|
* row untouched. A fresh insert returns `{ created: true, receivedAt }`.
|
||||||
|
*
|
||||||
|
* `senderFp` (V4.8+) is the 8-byte hex of SHA-256(senderSigningKey)
|
||||||
|
* — captured at PUT time when the relay verified the sender's
|
||||||
|
* signature. Optional so legacy 4.7 callers compile, but populated by
|
||||||
|
* `createInboxRoutes` from 4.8 onward and surfaced on bridge push +
|
||||||
|
* inbox-fetch responses to bootstrap unknown-sender first-contact
|
||||||
|
* (X3DH pair handshake).
|
||||||
*/
|
*/
|
||||||
putBlob(args: {
|
putBlob(args: {
|
||||||
address: string;
|
address: string;
|
||||||
msgId: string;
|
msgId: string;
|
||||||
ciphertext: Uint8Array;
|
ciphertext: Uint8Array;
|
||||||
expiresAt: number;
|
expiresAt: number;
|
||||||
|
senderFp?: string;
|
||||||
}): Promise<{ created: boolean; receivedAt: number }>;
|
}): Promise<{ created: boolean; receivedAt: number }>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,7 +65,20 @@ export interface InboxStore {
|
|||||||
sinceCursor: number;
|
sinceCursor: number;
|
||||||
now: number;
|
now: number;
|
||||||
limit: number;
|
limit: number;
|
||||||
}): Promise<Array<{ msgId: string; ciphertext: Uint8Array; receivedAt: number; expiresAt: number }>>;
|
}): Promise<
|
||||||
|
Array<{
|
||||||
|
msgId: string;
|
||||||
|
ciphertext: Uint8Array;
|
||||||
|
receivedAt: number;
|
||||||
|
expiresAt: number;
|
||||||
|
/**
|
||||||
|
* Sender fingerprint — 8-byte hex of SHA-256(senderSigningKey)
|
||||||
|
* captured at PUT time. Empty/undefined for blobs persisted by a
|
||||||
|
* pre-4.8 relay that didn't track sender provenance.
|
||||||
|
*/
|
||||||
|
senderFp?: string;
|
||||||
|
}>
|
||||||
|
>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a single blob by `(address, msgId)`. Returns true if a row was
|
* Delete a single blob by `(address, msgId)`. Returns true if a row was
|
||||||
|
|||||||
295
packages/shade-inbox-server/tests/blob-routes.test.ts
Normal file
295
packages/shade-inbox-server/tests/blob-routes.test.ts
Normal file
@@ -0,0 +1,295 @@
|
|||||||
|
import { describe, test, expect, beforeEach } from 'bun:test';
|
||||||
|
import { Hono } from 'hono';
|
||||||
|
import {
|
||||||
|
createBlobRoutes,
|
||||||
|
MemoryBlobStore,
|
||||||
|
type BlobStore,
|
||||||
|
} from '../src/index.js';
|
||||||
|
import { signPayload } from '@shade/server';
|
||||||
|
import { SubtleCryptoProvider, ed25519PublicKeyFromSeed } from '@shade/crypto-web';
|
||||||
|
import { toBase64, fromBase64 } from '@shade/core';
|
||||||
|
|
||||||
|
const crypto = new SubtleCryptoProvider();
|
||||||
|
|
||||||
|
function randBytes(n: number): Uint8Array {
|
||||||
|
const buf = new Uint8Array(n);
|
||||||
|
globalThis.crypto.getRandomValues(buf);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hex(bytes: Uint8Array): string {
|
||||||
|
let s = '';
|
||||||
|
for (const b of bytes) s += b.toString(16).padStart(2, '0');
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Shade Blob Routes (V4.9)', () => {
|
||||||
|
let store: BlobStore;
|
||||||
|
let app: Hono;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
store = new MemoryBlobStore();
|
||||||
|
app = createBlobRoutes(store, crypto, { disableRateLimit: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
async function makeOwner() {
|
||||||
|
const seed = randBytes(32);
|
||||||
|
const pubkey = ed25519PublicKeyFromSeed(seed);
|
||||||
|
return { seed, pubkey };
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeSlotId(): string {
|
||||||
|
return hex(randBytes(32));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function signedPut(args: {
|
||||||
|
slotId: string;
|
||||||
|
blob: Uint8Array;
|
||||||
|
seed: Uint8Array;
|
||||||
|
pubkey: Uint8Array;
|
||||||
|
ifMatch?: string;
|
||||||
|
}) {
|
||||||
|
const payload: Record<string, unknown> = {
|
||||||
|
ownerPubkey: toBase64(args.pubkey),
|
||||||
|
blob: toBase64(args.blob),
|
||||||
|
slotId: args.slotId,
|
||||||
|
};
|
||||||
|
if (args.ifMatch !== undefined) payload.ifMatch = args.ifMatch;
|
||||||
|
const signed = await signPayload(crypto, args.seed, payload);
|
||||||
|
const { slotId: _omit, ...wire } = signed as Record<string, unknown>;
|
||||||
|
return app.request(`/v1/blob/${args.slotId}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(wire),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function signedDelete(args: {
|
||||||
|
slotId: string;
|
||||||
|
seed: Uint8Array;
|
||||||
|
}) {
|
||||||
|
const signed = await signPayload(crypto, args.seed, {
|
||||||
|
slotId: args.slotId,
|
||||||
|
});
|
||||||
|
const { slotId: _omit, ...wire } = signed as Record<string, unknown>;
|
||||||
|
return app.request(`/v1/blob/${args.slotId}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(wire),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── GET ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
test('GET on missing slot returns 404', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const res = await app.request(`/v1/blob/${slotId}`);
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET requires lowercase 64-hex slotId', async () => {
|
||||||
|
const res = await app.request('/v1/blob/notahex');
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
const res2 = await app.request(`/v1/blob/${'A'.repeat(64)}`);
|
||||||
|
expect(res2.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── PUT (TOFU) ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
test('first PUT creates slot and returns etag', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
const blob = randBytes(128);
|
||||||
|
const res = await signedPut({ slotId, blob, ...owner });
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
const json = (await res.json()) as { created: boolean; etag: string };
|
||||||
|
expect(json.created).toBe(true);
|
||||||
|
expect(typeof json.etag).toBe('string');
|
||||||
|
|
||||||
|
const got = await app.request(`/v1/blob/${slotId}`);
|
||||||
|
expect(got.status).toBe(200);
|
||||||
|
const back = (await got.json()) as { blob: string; etag: string };
|
||||||
|
expect(fromBase64(back.blob)).toEqual(blob);
|
||||||
|
expect(back.etag).toBe(json.etag);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT without ifMatch on populated slot returns 409', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
await signedPut({ slotId, blob: randBytes(64), ...owner });
|
||||||
|
const res = await signedPut({ slotId, blob: randBytes(64), ...owner });
|
||||||
|
expect(res.status).toBe(409);
|
||||||
|
const json = (await res.json()) as { code: string };
|
||||||
|
expect(json.code).toBe('SHADE_CONFLICT');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT with stale ifMatch returns 412', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
const r1 = await signedPut({ slotId, blob: randBytes(64), ...owner });
|
||||||
|
const j1 = (await r1.json()) as { etag: string };
|
||||||
|
// Use an etag we know does not match.
|
||||||
|
const stale = String(Number(j1.etag) - 999);
|
||||||
|
const res = await signedPut({
|
||||||
|
slotId,
|
||||||
|
blob: randBytes(64),
|
||||||
|
...owner,
|
||||||
|
ifMatch: stale,
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(412);
|
||||||
|
const json = (await res.json()) as { code: string };
|
||||||
|
expect(json.code).toBe('SHADE_PRECONDITION_FAILED');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT with matching ifMatch updates and bumps etag', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
const r1 = await signedPut({ slotId, blob: randBytes(64), ...owner });
|
||||||
|
const j1 = (await r1.json()) as { etag: string };
|
||||||
|
const r2 = await signedPut({
|
||||||
|
slotId,
|
||||||
|
blob: randBytes(64),
|
||||||
|
...owner,
|
||||||
|
ifMatch: j1.etag,
|
||||||
|
});
|
||||||
|
expect(r2.status).toBe(200);
|
||||||
|
const j2 = (await r2.json()) as { created: boolean; etag: string };
|
||||||
|
expect(j2.created).toBe(false);
|
||||||
|
expect(Number(j2.etag)).toBeGreaterThan(Number(j1.etag));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT with ifMatch="*" unconditionally overwrites existing slot', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
await signedPut({ slotId, blob: randBytes(64), ...owner });
|
||||||
|
const res = await signedPut({
|
||||||
|
slotId,
|
||||||
|
blob: randBytes(64),
|
||||||
|
...owner,
|
||||||
|
ifMatch: '*',
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT with ifMatch="*" on empty slot returns 412', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
const res = await signedPut({
|
||||||
|
slotId,
|
||||||
|
blob: randBytes(64),
|
||||||
|
...owner,
|
||||||
|
ifMatch: '*',
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(412);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT by a different owner key on existing slot is rejected', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const ownerA = await makeOwner();
|
||||||
|
await signedPut({ slotId, blob: randBytes(64), ...ownerA });
|
||||||
|
|
||||||
|
const ownerB = await makeOwner();
|
||||||
|
const res = await signedPut({
|
||||||
|
slotId,
|
||||||
|
blob: randBytes(64),
|
||||||
|
...ownerB,
|
||||||
|
ifMatch: '*',
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT with bad signature is rejected', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
// Sign the payload, then mutate the blob bytes — signature no
|
||||||
|
// longer matches the canonicalized body.
|
||||||
|
const blob = randBytes(64);
|
||||||
|
const payload = {
|
||||||
|
ownerPubkey: toBase64(owner.pubkey),
|
||||||
|
blob: toBase64(blob),
|
||||||
|
slotId,
|
||||||
|
};
|
||||||
|
const signed = await signPayload(crypto, owner.seed, payload);
|
||||||
|
(signed as any).blob = toBase64(randBytes(64));
|
||||||
|
const { slotId: _omit, ...wire } = signed as Record<string, unknown>;
|
||||||
|
const res = await app.request(`/v1/blob/${slotId}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(wire),
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT rejects empty blob and oversized blob', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
const empty = await signedPut({ slotId, blob: new Uint8Array(0), ...owner });
|
||||||
|
expect(empty.status).toBe(400);
|
||||||
|
const tooBig = await signedPut({
|
||||||
|
slotId,
|
||||||
|
blob: randBytes(70 * 1024),
|
||||||
|
...owner,
|
||||||
|
});
|
||||||
|
expect(tooBig.status).toBe(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── DELETE ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
test('DELETE clears slot and lets a fresh key TOFU re-claim', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const ownerA = await makeOwner();
|
||||||
|
await signedPut({ slotId, blob: randBytes(64), ...ownerA });
|
||||||
|
|
||||||
|
const del = await signedDelete({ slotId, seed: ownerA.seed });
|
||||||
|
expect(del.status).toBe(200);
|
||||||
|
|
||||||
|
// Slot is gone.
|
||||||
|
const gone = await app.request(`/v1/blob/${slotId}`);
|
||||||
|
expect(gone.status).toBe(404);
|
||||||
|
|
||||||
|
// A fresh owner can now claim it.
|
||||||
|
const ownerB = await makeOwner();
|
||||||
|
const claim = await signedPut({ slotId, blob: randBytes(64), ...ownerB });
|
||||||
|
expect(claim.status).toBe(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('DELETE by a different key is rejected', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const ownerA = await makeOwner();
|
||||||
|
await signedPut({ slotId, blob: randBytes(64), ...ownerA });
|
||||||
|
|
||||||
|
const ownerB = await makeOwner();
|
||||||
|
const res = await signedDelete({ slotId, seed: ownerB.seed });
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('DELETE on missing slot returns 404', async () => {
|
||||||
|
const slotId = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
const res = await signedDelete({ slotId, seed: owner.seed });
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── Cross-slot replay ───────────────────────────────────────
|
||||||
|
|
||||||
|
test('PUT signed for slot A is rejected against slot B', async () => {
|
||||||
|
const slotA = makeSlotId();
|
||||||
|
const slotB = makeSlotId();
|
||||||
|
const owner = await makeOwner();
|
||||||
|
const blob = randBytes(64);
|
||||||
|
// Sign for slotA, send to slotB (URL).
|
||||||
|
const payload = {
|
||||||
|
ownerPubkey: toBase64(owner.pubkey),
|
||||||
|
blob: toBase64(blob),
|
||||||
|
slotId: slotA,
|
||||||
|
};
|
||||||
|
const signed = await signPayload(crypto, owner.seed, payload);
|
||||||
|
const { slotId: _omit, ...wire } = signed as Record<string, unknown>;
|
||||||
|
const res = await app.request(`/v1/blob/${slotB}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(wire),
|
||||||
|
});
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/inbox",
|
"name": "@shade/inbox",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
208
packages/shade-inbox/src/blob-client.ts
Normal file
208
packages/shade-inbox/src/blob-client.ts
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
import type { CryptoProvider } from '@shade/core';
|
||||||
|
import {
|
||||||
|
NetworkError,
|
||||||
|
toBase64,
|
||||||
|
fromBase64,
|
||||||
|
ShadeError,
|
||||||
|
ValidationError,
|
||||||
|
} from '@shade/core';
|
||||||
|
import { signPayload } from '@shade/server';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Low-level HTTP client for the V4.9 encrypted-blob primitive
|
||||||
|
* (`/v1/blob/<slotId>`). Stateless and reusable; higher-level wrappers
|
||||||
|
* (e.g. `Profile` in `@shade/sdk`) compose this client.
|
||||||
|
*
|
||||||
|
* The client doesn't care what the blob bytes mean — it just transports
|
||||||
|
* them. Callers are responsible for AEAD-sealing/opening, deriving the
|
||||||
|
* slotId from the master key, and managing the signing key.
|
||||||
|
*/
|
||||||
|
export interface BlobClientOptions {
|
||||||
|
baseUrl: string;
|
||||||
|
crypto: CryptoProvider;
|
||||||
|
/** Optional fetch override (defaults to globalThis.fetch). */
|
||||||
|
fetch?: typeof fetch;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BlobGetResult {
|
||||||
|
blob: Uint8Array;
|
||||||
|
/** ETag string — pass back as `ifMatch` to do a CAS update. */
|
||||||
|
etag: string;
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BlobPutResult {
|
||||||
|
/** True if this PUT created the slot, false if it updated an existing one. */
|
||||||
|
created: boolean;
|
||||||
|
/** New ETag after the write. */
|
||||||
|
etag: string;
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BlobClient {
|
||||||
|
private readonly fetchImpl: typeof fetch;
|
||||||
|
|
||||||
|
constructor(private readonly options: BlobClientOptions) {
|
||||||
|
const f = options.fetch ?? globalThis.fetch;
|
||||||
|
this.fetchImpl = f.bind(globalThis);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a slot. Returns null if no blob has ever been written there
|
||||||
|
* (or if it was DELETE'd). GET is unauthenticated — see the
|
||||||
|
* `BlobStore` JSDoc for the threat-model rationale.
|
||||||
|
*/
|
||||||
|
async get(slotIdHex: string): Promise<BlobGetResult | null> {
|
||||||
|
validateSlotIdHex(slotIdHex);
|
||||||
|
const url = joinUrl(this.options.baseUrl, `/v1/blob/${slotIdHex}`);
|
||||||
|
let res: Response;
|
||||||
|
try {
|
||||||
|
res = await this.fetchImpl(url, { method: 'GET' });
|
||||||
|
} catch (err) {
|
||||||
|
throw new NetworkError(`Blob GET failed: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
|
if (res.status === 404) return null;
|
||||||
|
const text = await res.text();
|
||||||
|
let json: any;
|
||||||
|
try {
|
||||||
|
json = text.length > 0 ? JSON.parse(text) : {};
|
||||||
|
} catch {
|
||||||
|
throw new NetworkError(`Blob response not JSON: ${text.slice(0, 200)}`, res.status);
|
||||||
|
}
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new ShadeError(
|
||||||
|
String(json.code ?? 'SHADE_NETWORK'),
|
||||||
|
String(json.message ?? text),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
blob: fromBase64(String(json.blob)),
|
||||||
|
etag: String(json.etag),
|
||||||
|
updatedAt: Number(json.updatedAt),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create or update a slot.
|
||||||
|
*
|
||||||
|
* `ifMatch` semantics:
|
||||||
|
* - `undefined`: create-only. Slot must be empty (else 409).
|
||||||
|
* - `<etag-string>`: compare-and-swap. Must match (else 412).
|
||||||
|
* - `'*'`: unconditional overwrite. Slot must already exist (else 412).
|
||||||
|
*/
|
||||||
|
async put(args: {
|
||||||
|
slotIdHex: string;
|
||||||
|
blob: Uint8Array;
|
||||||
|
/** 32-byte Ed25519 seed (== `signingPrivateKey`). */
|
||||||
|
signingPrivateKey: Uint8Array;
|
||||||
|
/** Pubkey paired to `signingPrivateKey`. */
|
||||||
|
ownerPubkey: Uint8Array;
|
||||||
|
ifMatch?: string;
|
||||||
|
}): Promise<BlobPutResult> {
|
||||||
|
validateSlotIdHex(args.slotIdHex);
|
||||||
|
if (args.blob.length === 0) {
|
||||||
|
throw new ValidationError('Empty blob');
|
||||||
|
}
|
||||||
|
if (args.ownerPubkey.length !== 32) {
|
||||||
|
throw new ValidationError('ownerPubkey must be 32 bytes (Ed25519)');
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload: Record<string, unknown> = {
|
||||||
|
ownerPubkey: toBase64(args.ownerPubkey),
|
||||||
|
blob: toBase64(args.blob),
|
||||||
|
slotId: args.slotIdHex,
|
||||||
|
};
|
||||||
|
if (args.ifMatch !== undefined) payload.ifMatch = args.ifMatch;
|
||||||
|
|
||||||
|
const signed = await signPayload(
|
||||||
|
this.options.crypto,
|
||||||
|
args.signingPrivateKey,
|
||||||
|
payload,
|
||||||
|
);
|
||||||
|
// `slotId` was used for the signature canonicalization to bind it
|
||||||
|
// into the payload; the server rebuilds the same canonical form
|
||||||
|
// by mixing the URL slotId back in. Strip it from the wire body
|
||||||
|
// so we don't send it twice (URL is the path param).
|
||||||
|
const { slotId: _omit, ...wireBody } = signed as Record<string, unknown>;
|
||||||
|
|
||||||
|
const url = joinUrl(this.options.baseUrl, `/v1/blob/${args.slotIdHex}`);
|
||||||
|
const json = await this.requestJson('PUT', url, wireBody);
|
||||||
|
return {
|
||||||
|
created: Boolean(json.created),
|
||||||
|
etag: String(json.etag),
|
||||||
|
updatedAt: Number(json.updatedAt),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a slot — the next PUT TOFU-claims it again, possibly under
|
||||||
|
* a fresh signing key (e.g. after a rotation). Used by the "forget
|
||||||
|
* everything" path.
|
||||||
|
*/
|
||||||
|
async delete(args: {
|
||||||
|
slotIdHex: string;
|
||||||
|
signingPrivateKey: Uint8Array;
|
||||||
|
}): Promise<boolean> {
|
||||||
|
validateSlotIdHex(args.slotIdHex);
|
||||||
|
const signed = await signPayload(this.options.crypto, args.signingPrivateKey, {
|
||||||
|
slotId: args.slotIdHex,
|
||||||
|
});
|
||||||
|
const { slotId: _omit, ...wireBody } = signed as Record<string, unknown>;
|
||||||
|
const url = joinUrl(this.options.baseUrl, `/v1/blob/${args.slotIdHex}`);
|
||||||
|
const json = await this.requestJson('DELETE', url, wireBody);
|
||||||
|
return Boolean(json.ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── HTTP plumbing ──────────────────────────────────────────
|
||||||
|
|
||||||
|
private async requestJson(method: string, url: string, body: unknown): Promise<any> {
|
||||||
|
let res: Response;
|
||||||
|
try {
|
||||||
|
res = await this.fetchImpl(url, {
|
||||||
|
method,
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
throw new NetworkError(`Blob request failed: ${(err as Error).message}`);
|
||||||
|
}
|
||||||
|
const text = await res.text();
|
||||||
|
let json: any;
|
||||||
|
try {
|
||||||
|
json = text.length > 0 ? JSON.parse(text) : {};
|
||||||
|
} catch {
|
||||||
|
throw new NetworkError(`Blob response not JSON: ${text.slice(0, 200)}`, res.status);
|
||||||
|
}
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new ShadeError(
|
||||||
|
String(json.code ?? 'SHADE_NETWORK'),
|
||||||
|
String(json.message ?? text),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateSlotIdHex(s: string): void {
|
||||||
|
if (!/^[0-9a-f]{64}$/.test(s)) {
|
||||||
|
throw new ValidationError('slotIdHex must be 64 lowercase hex chars (32 bytes)');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function joinUrl(base: string, path: string): string {
|
||||||
|
if (base.endsWith('/') && path.startsWith('/')) return base + path.slice(1);
|
||||||
|
if (!base.endsWith('/') && !path.startsWith('/')) return base + '/' + path;
|
||||||
|
return base + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Convert a 32-byte slotId Uint8Array into the lowercase-hex wire form. */
|
||||||
|
export function slotIdToHex(slotId: Uint8Array): string {
|
||||||
|
if (slotId.length !== 32) {
|
||||||
|
throw new ValidationError('slotId must be 32 bytes');
|
||||||
|
}
|
||||||
|
let s = '';
|
||||||
|
for (let i = 0; i < slotId.length; i++) {
|
||||||
|
s += slotId[i]!.toString(16).padStart(2, '0');
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
@@ -40,6 +40,16 @@ export interface FetchedBlob {
|
|||||||
receivedAt: number;
|
receivedAt: number;
|
||||||
/** Absolute expiry time (ms since epoch) reported by the server. */
|
/** Absolute expiry time (ms since epoch) reported by the server. */
|
||||||
expiresAt: number;
|
expiresAt: number;
|
||||||
|
/**
|
||||||
|
* Sender fingerprint — 8-byte hex of SHA-256(senderSigningKey),
|
||||||
|
* captured by the relay at PUT time when the sender's signature was
|
||||||
|
* verified. Empty/undefined when the relay is pre-4.8 or the blob
|
||||||
|
* predates sender-fingerprint tracking. Use as an unknown-sender
|
||||||
|
* bootstrap label (`fp:<hex>`) for X3DH first-contact; the
|
||||||
|
* authoritative sender identity is recovered post-decrypt from the
|
||||||
|
* envelope itself, so `from` is a hint, not a trust anchor.
|
||||||
|
*/
|
||||||
|
from?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FetchResult {
|
export interface FetchResult {
|
||||||
@@ -52,7 +62,14 @@ export class InboxClient {
|
|||||||
private readonly fetchImpl: typeof fetch;
|
private readonly fetchImpl: typeof fetch;
|
||||||
|
|
||||||
constructor(private readonly options: InboxClientOptions) {
|
constructor(private readonly options: InboxClientOptions) {
|
||||||
this.fetchImpl = options.fetch ?? globalThis.fetch;
|
// Bind once. The browser's `globalThis.fetch` is a WebIDL bound
|
||||||
|
// operation that throws "Illegal invocation" when called as a method
|
||||||
|
// on another object (which is what `this.fetchImpl(...)` does).
|
||||||
|
// Node/Bun fetch tolerates a free receiver, but binding is harmless.
|
||||||
|
// A consumer-supplied `options.fetch` is bound to the global too —
|
||||||
|
// a fetch that requires a specific receiver must bind itself.
|
||||||
|
const f = options.fetch ?? globalThis.fetch;
|
||||||
|
this.fetchImpl = f.bind(globalThis);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -144,12 +161,18 @@ export class InboxClient {
|
|||||||
);
|
);
|
||||||
const blobs = Array.isArray(json.blobs) ? json.blobs : [];
|
const blobs = Array.isArray(json.blobs) ? json.blobs : [];
|
||||||
return {
|
return {
|
||||||
blobs: blobs.map((b: any) => ({
|
blobs: blobs.map((b: any): FetchedBlob => {
|
||||||
|
const out: FetchedBlob = {
|
||||||
msgId: String(b.msgId),
|
msgId: String(b.msgId),
|
||||||
ciphertext: fromBase64(String(b.ciphertext)),
|
ciphertext: fromBase64(String(b.ciphertext)),
|
||||||
receivedAt: Number(b.receivedAt),
|
receivedAt: Number(b.receivedAt),
|
||||||
expiresAt: Number(b.expiresAt),
|
expiresAt: Number(b.expiresAt),
|
||||||
})),
|
};
|
||||||
|
// V4.8 — relay-supplied sender fingerprint hint. Optional; absent
|
||||||
|
// on pre-4.8 relays or for blobs persisted before tracking landed.
|
||||||
|
if (typeof b.from === 'string' && b.from.length > 0) out.from = b.from;
|
||||||
|
return out;
|
||||||
|
}),
|
||||||
cursor: Number(json.cursor ?? sinceCursor),
|
cursor: Number(json.cursor ?? sinceCursor),
|
||||||
hasMore: Boolean(json.hasMore),
|
hasMore: Boolean(json.hasMore),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,9 +16,20 @@ import { InboxClientEvents, type InboxClientListener } from './events.js';
|
|||||||
* decrypt path it owns) and either return a sender-hint for telemetry
|
* decrypt path it owns) and either return a sender-hint for telemetry
|
||||||
* (the address the SDK extracted, or `null`) or throw to keep the blob
|
* (the address the SDK extracted, or `null`) or throw to keep the blob
|
||||||
* on the server for a later retry.
|
* on the server for a later retry.
|
||||||
|
*
|
||||||
|
* V4.8: `raw.from` is the relay-captured sender fingerprint (8-byte hex
|
||||||
|
* of SHA-256 over the sender's signing key). Empty when the relay is
|
||||||
|
* pre-4.8 or didn't track the sender. Use it as the `fp:<hex>` bootstrap
|
||||||
|
* label for X3DH first-contact handshakes.
|
||||||
*/
|
*/
|
||||||
export type DecryptHandler = (
|
export type DecryptHandler = (
|
||||||
raw: { msgId: string; ciphertext: Uint8Array; receivedAt: number; expiresAt: number },
|
raw: {
|
||||||
|
msgId: string;
|
||||||
|
ciphertext: Uint8Array;
|
||||||
|
receivedAt: number;
|
||||||
|
expiresAt: number;
|
||||||
|
from?: string;
|
||||||
|
},
|
||||||
) => Promise<string | null | undefined> | string | null | undefined;
|
) => Promise<string | null | undefined> | string | null | undefined;
|
||||||
|
|
||||||
export interface InboxOptions {
|
export interface InboxOptions {
|
||||||
@@ -52,6 +63,14 @@ export interface InboxOptions {
|
|||||||
const DEFAULT_TTL_SECONDS = 7 * 24 * 60 * 60;
|
const DEFAULT_TTL_SECONDS = 7 * 24 * 60 * 60;
|
||||||
const DEFAULT_POLL_INTERVAL_MS = 30_000;
|
const DEFAULT_POLL_INTERVAL_MS = 30_000;
|
||||||
const DEFAULT_MAX_ATTEMPTS = 10;
|
const DEFAULT_MAX_ATTEMPTS = 10;
|
||||||
|
/**
|
||||||
|
* Cap for the cross-channel msgId dedup LRU. Each entry is a 64-char hex
|
||||||
|
* string; 4096 entries ≈ 256 KiB of overhead, plenty of headroom for
|
||||||
|
* bursty traffic (the LRU only needs to span the window between a bridge
|
||||||
|
* push and the next inbox-poll catching up — typically 30 s × the
|
||||||
|
* recipient's throughput).
|
||||||
|
*/
|
||||||
|
const DEFAULT_DEDUP_LRU_CAP = 4096;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* High-level inbox orchestrator.
|
* High-level inbox orchestrator.
|
||||||
@@ -94,6 +113,23 @@ export class Inbox {
|
|||||||
private started = false;
|
private started = false;
|
||||||
private registered = false;
|
private registered = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bounded msgId dedup window. Used by both the inbox-poll path
|
||||||
|
* (`pollOnce` → `handleBlob`) and the bridge-push path
|
||||||
|
* (`acceptBridgeFrame`). The relay stores blobs durably and pushes
|
||||||
|
* them to every active delivery channel; without a shared dedup gate
|
||||||
|
* here the recipient processes the same envelope twice — once from
|
||||||
|
* the bridge, again from the next inbox-poll. The duplicate receive
|
||||||
|
* trips on consumed one-time prekeys ("OPK not found") and pollutes
|
||||||
|
* logs even when the canonical first delivery succeeded. See V4.8.3
|
||||||
|
* Prism FR `cross-channel-duplicate-fanout-v4.8.2.md`.
|
||||||
|
*
|
||||||
|
* Insertion order is FIFO; the oldest msgId is evicted once the LRU
|
||||||
|
* exceeds `DEFAULT_DEDUP_LRU_CAP`.
|
||||||
|
*/
|
||||||
|
private readonly deliveredIds = new Set<string>();
|
||||||
|
private readonly deliveredOrder: string[] = [];
|
||||||
|
|
||||||
constructor(private readonly options: InboxOptions) {
|
constructor(private readonly options: InboxOptions) {
|
||||||
const clientOptions: ConstructorParameters<typeof InboxClient>[0] = {
|
const clientOptions: ConstructorParameters<typeof InboxClient>[0] = {
|
||||||
baseUrl: options.baseUrl,
|
baseUrl: options.baseUrl,
|
||||||
@@ -149,6 +185,14 @@ export class Inbox {
|
|||||||
signingKey: this.options.signingPublicKey,
|
signingKey: this.options.signingPublicKey,
|
||||||
});
|
});
|
||||||
this.registered = true;
|
this.registered = true;
|
||||||
|
// V4.8: gate the first poll on register success. `start()` calls
|
||||||
|
// `register()` fire-and-forget; without this kick, the very first
|
||||||
|
// `pollOnce()` (scheduled synchronously alongside register) would
|
||||||
|
// race the register HTTP RTT and return SHADE_NOT_FOUND. The
|
||||||
|
// pollOnce() guard skips polls until `registered === true`; this
|
||||||
|
// immediate schedule ensures we don't wait the full pollIntervalMs
|
||||||
|
// for the next attempt once register lands.
|
||||||
|
if (this.started) this.schedulePoll(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Drop the address from the server. Local queue/cursor are preserved. */
|
/** Drop the address from the server. Local queue/cursor are preserved. */
|
||||||
@@ -203,7 +247,10 @@ export class Inbox {
|
|||||||
* after a push-trigger arrives). Does not throw on transient errors.
|
* after a push-trigger arrives). Does not throw on transient errors.
|
||||||
*/
|
*/
|
||||||
async tick(): Promise<{ flushed: number; received: number }> {
|
async tick(): Promise<{ flushed: number; received: number }> {
|
||||||
const flushed = await this.flushOnce();
|
const flushResult = await this.flushOnce();
|
||||||
|
// `null` means another flush was concurrent; report 0 newly-flushed
|
||||||
|
// for this caller (the other flush counted them).
|
||||||
|
const flushed = flushResult?.delivered ?? 0;
|
||||||
const received = await this.pollOnce();
|
const received = await this.pollOnce();
|
||||||
return { flushed, received };
|
return { flushed, received };
|
||||||
}
|
}
|
||||||
@@ -217,7 +264,11 @@ export class Inbox {
|
|||||||
this.scheduleRegisterRetry();
|
this.scheduleRegisterRetry();
|
||||||
});
|
});
|
||||||
this.scheduleFlush();
|
this.scheduleFlush();
|
||||||
this.schedulePoll(0);
|
// V4.8: do NOT schedule the first poll synchronously. `register()`
|
||||||
|
// success kicks `schedulePoll(0)` so the first poll fires after the
|
||||||
|
// server has acknowledged the address. Pre-fix this raced register
|
||||||
|
// and burned a 404 on every fresh-address `start()`.
|
||||||
|
if (this.registered) this.schedulePoll(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Stop background timers. Pending entries remain in the queue. */
|
/** Stop background timers. Pending entries remain in the queue. */
|
||||||
@@ -253,11 +304,27 @@ export class Inbox {
|
|||||||
this.flushTimer = setTimeout(() => {
|
this.flushTimer = setTimeout(() => {
|
||||||
this.flushTimer = null;
|
this.flushTimer = null;
|
||||||
this.flushOnce()
|
this.flushOnce()
|
||||||
.then(() => {
|
.then((result) => {
|
||||||
// If anything is still queued, retry with backoff.
|
// `result === null` means another flush was already in flight
|
||||||
this.queueStore.size().then((n) => {
|
// and this call early-returned via the `flushing` guard. The
|
||||||
if (n > 0 && this.started) this.scheduleFlush(15_000);
|
// already-running flush will reschedule itself when it
|
||||||
});
|
// finishes; do not double-schedule from here.
|
||||||
|
if (result === null) return;
|
||||||
|
if (result.remaining === 0) return;
|
||||||
|
// V4.8.5 — distinguish healthy-drain-but-more-queued from
|
||||||
|
// all-attempts-failed. Pre-fix, both cases used a 15 s
|
||||||
|
// backoff. Under sustained traffic (Prism's typing-into-a-
|
||||||
|
// chatty-shell pattern), bursts of envelopes enqueued
|
||||||
|
// *during* a flush would sit ~10–15 s behind the backoff
|
||||||
|
// timer before the next drain — visible to the receiver as a
|
||||||
|
// "10 s silence then 25-frame burst" wave. Healthy drain
|
||||||
|
// (delivered > 0) means the network is fine and we should
|
||||||
|
// immediately drain whatever piled up; reserve the 15 s
|
||||||
|
// retry for the case where every attempt this round failed.
|
||||||
|
if (this.started) {
|
||||||
|
const delay = result.delivered > 0 ? 0 : 15_000;
|
||||||
|
this.scheduleFlush(delay);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
if (this.started) this.scheduleFlush(15_000);
|
if (this.started) this.scheduleFlush(15_000);
|
||||||
@@ -276,13 +343,47 @@ export class Inbox {
|
|||||||
}, delayMs);
|
}, delayMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async flushOnce(): Promise<number> {
|
/**
|
||||||
if (this.flushing) return 0;
|
* Drain the outgoing queue. Returns `null` if another flush is already
|
||||||
|
* in flight (the running flush owns the rescheduling); otherwise
|
||||||
|
* returns the count of newly-delivered envelopes and the queue size
|
||||||
|
* after the drain so the caller can decide whether to immediately
|
||||||
|
* re-flush (more piled up during the drain — healthy network) or
|
||||||
|
* back off (everything failed).
|
||||||
|
*
|
||||||
|
* V4.8.5: drain is parallel-per-recipient. Each `recipientAddress`
|
||||||
|
* gets its own sequential worker (so per-peer order is preserved),
|
||||||
|
* but distinct recipients run concurrently. Pre-fix, a single slow
|
||||||
|
* POST head-of-line-blocked the entire queue — including small
|
||||||
|
* frames bound for unrelated peers. See Prism FR
|
||||||
|
* `per-recipient-flush-concurrency-v4.8.md`.
|
||||||
|
*/
|
||||||
|
private async flushOnce(): Promise<{ delivered: number; remaining: number } | null> {
|
||||||
|
if (this.flushing) return null;
|
||||||
this.flushing = true;
|
this.flushing = true;
|
||||||
let delivered = 0;
|
let delivered = 0;
|
||||||
try {
|
try {
|
||||||
const entries = await this.queueStore.list();
|
const entries = await this.queueStore.list();
|
||||||
|
// Group by recipient. Within a bucket we drain sequentially so
|
||||||
|
// per-peer message order matches enqueue order (the relay
|
||||||
|
// assigns `receivedAt` on PUT arrival; concurrent POSTs to the
|
||||||
|
// same peer would let the second arrive first and the recipient
|
||||||
|
// would observe out-of-order envelopes). Across buckets, no
|
||||||
|
// ordering guarantee exists in Shade's wire model anyway, so
|
||||||
|
// parallel drain is safe.
|
||||||
|
const buckets = new Map<string, OutgoingEntry[]>();
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
|
let bucket = buckets.get(entry.recipientAddress);
|
||||||
|
if (!bucket) {
|
||||||
|
bucket = [];
|
||||||
|
buckets.set(entry.recipientAddress, bucket);
|
||||||
|
}
|
||||||
|
bucket.push(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
const drainBucket = async (bucket: OutgoingEntry[]): Promise<number> => {
|
||||||
|
let count = 0;
|
||||||
|
for (const entry of bucket) {
|
||||||
try {
|
try {
|
||||||
const result = await this.client.put({
|
const result = await this.client.put({
|
||||||
recipientAddress: entry.recipientAddress,
|
recipientAddress: entry.recipientAddress,
|
||||||
@@ -291,7 +392,7 @@ export class Inbox {
|
|||||||
ttlSeconds: entry.ttlSeconds,
|
ttlSeconds: entry.ttlSeconds,
|
||||||
});
|
});
|
||||||
await this.queueStore.remove(entry.recipientAddress, entry.msgId);
|
await this.queueStore.remove(entry.recipientAddress, entry.msgId);
|
||||||
delivered++;
|
count++;
|
||||||
this.events.emit('inbox.message_delivered', {
|
this.events.emit('inbox.message_delivered', {
|
||||||
recipientAddress: entry.recipientAddress,
|
recipientAddress: entry.recipientAddress,
|
||||||
msgId: result.msgId,
|
msgId: result.msgId,
|
||||||
@@ -311,10 +412,18 @@ export class Inbox {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return count;
|
||||||
|
};
|
||||||
|
|
||||||
|
const counts = await Promise.all(
|
||||||
|
Array.from(buckets.values(), drainBucket),
|
||||||
|
);
|
||||||
|
delivered = counts.reduce((a, b) => a + b, 0);
|
||||||
} finally {
|
} finally {
|
||||||
this.flushing = false;
|
this.flushing = false;
|
||||||
}
|
}
|
||||||
return delivered;
|
const remaining = await this.queueStore.size();
|
||||||
|
return { delivered, remaining };
|
||||||
}
|
}
|
||||||
|
|
||||||
private async pollOnce(): Promise<number> {
|
private async pollOnce(): Promise<number> {
|
||||||
@@ -357,9 +466,52 @@ export class Inbox {
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Feed a blob delivered by a bridge transport (WS / SSE / long-poll
|
||||||
|
* push) into the same dispatch + ack pipeline that `pollOnce` uses.
|
||||||
|
*
|
||||||
|
* Wire-up pattern:
|
||||||
|
* ```ts
|
||||||
|
* const bridge = new FallbackBridgeTransport([...]);
|
||||||
|
* await bridge.connect({
|
||||||
|
* onMessage: async (msg) => {
|
||||||
|
* await inbox.acceptBridgeFrame({
|
||||||
|
* msgId: msg.msgId!, // present on v4.8+ relays
|
||||||
|
* ciphertext: msg.bytes,
|
||||||
|
* receivedAt: msg.receivedAt,
|
||||||
|
* expiresAt: msg.expiresAt ?? Date.now() + 7 * 24 * 3600 * 1000,
|
||||||
|
* ...(msg.from !== undefined ? { from: msg.from } : {}),
|
||||||
|
* });
|
||||||
|
* },
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* The Inbox's bounded msgId LRU is shared between this path and
|
||||||
|
* `pollOnce`, so whichever channel delivers first wins; the
|
||||||
|
* other channel acks-and-skips when the same msgId comes back
|
||||||
|
* around. Both paths also DELETE the blob from the relay on success
|
||||||
|
* so subsequent polls don't see it either.
|
||||||
|
*
|
||||||
|
* Returns `true` if the blob was newly dispatched, `false` if it
|
||||||
|
* was a duplicate or rejected by the handler (handler still gets a
|
||||||
|
* chance to retry on the next poll if it threw).
|
||||||
|
*/
|
||||||
|
async acceptBridgeFrame(blob: FetchedBlob): Promise<boolean> {
|
||||||
|
return this.handleBlob(blob);
|
||||||
|
}
|
||||||
|
|
||||||
private async handleBlob(blob: FetchedBlob): Promise<boolean> {
|
private async handleBlob(blob: FetchedBlob): Promise<boolean> {
|
||||||
if (!this.incomingHandler) return false;
|
if (!this.incomingHandler) return false;
|
||||||
|
|
||||||
|
// Cross-channel msgId dedup. If the bridge already delivered this
|
||||||
|
// blob, the inbox-poll copy must not re-dispatch (would re-trigger
|
||||||
|
// X3DH / consume an OPK we no longer have). We still ack so the
|
||||||
|
// relay drops the now-redundant copy.
|
||||||
|
if (this.deliveredIds.has(blob.msgId)) {
|
||||||
|
await this.ackQuietly(blob.msgId);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Defense-in-depth: verify msgId ↔ ciphertext at the client too. A
|
// Defense-in-depth: verify msgId ↔ ciphertext at the client too. A
|
||||||
// server bug or malicious operator can't sneak a different blob past
|
// server bug or malicious operator can't sneak a different blob past
|
||||||
// the client's hash check.
|
// the client's hash check.
|
||||||
@@ -375,12 +527,20 @@ export class Inbox {
|
|||||||
|
|
||||||
let senderHint: string | null = null;
|
let senderHint: string | null = null;
|
||||||
try {
|
try {
|
||||||
const result = await this.incomingHandler({
|
const raw: {
|
||||||
|
msgId: string;
|
||||||
|
ciphertext: Uint8Array;
|
||||||
|
receivedAt: number;
|
||||||
|
expiresAt: number;
|
||||||
|
from?: string;
|
||||||
|
} = {
|
||||||
msgId: blob.msgId,
|
msgId: blob.msgId,
|
||||||
ciphertext: blob.ciphertext,
|
ciphertext: blob.ciphertext,
|
||||||
receivedAt: blob.receivedAt,
|
receivedAt: blob.receivedAt,
|
||||||
expiresAt: blob.expiresAt,
|
expiresAt: blob.expiresAt,
|
||||||
});
|
};
|
||||||
|
if (blob.from !== undefined) raw.from = blob.from;
|
||||||
|
const result = await this.incomingHandler(raw);
|
||||||
senderHint = result ?? null;
|
senderHint = result ?? null;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.events.emit('inbox.message_decrypt_failed', {
|
this.events.emit('inbox.message_decrypt_failed', {
|
||||||
@@ -391,17 +551,34 @@ export class Inbox {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// Mark before the ack so a slow-network ack doesn't leave a window
|
||||||
await this.client.ack({ address: this.options.ownAddress, msgId: blob.msgId });
|
// where a parallel pollOnce sees the same msgId and re-dispatches.
|
||||||
} catch (err) {
|
this.recordDelivered(blob.msgId);
|
||||||
// Decryption succeeded; ack just failed. Will be retried later, and
|
await this.ackQuietly(blob.msgId);
|
||||||
// the duplicate-message ratchet check on `Shade.receive` will dedupe.
|
|
||||||
console.warn('[Shade] Inbox ack failed (will retry):', (err as Error).message);
|
|
||||||
}
|
|
||||||
this.events.emit('inbox.message_received', {
|
this.events.emit('inbox.message_received', {
|
||||||
senderHint,
|
senderHint,
|
||||||
msgId: blob.msgId,
|
msgId: blob.msgId,
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async ackQuietly(msgId: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.client.ack({ address: this.options.ownAddress, msgId });
|
||||||
|
} catch (err) {
|
||||||
|
// Dispatch (or skip) succeeded; the ack just failed. Next poll
|
||||||
|
// will see the blob again and the dedup gate above will skip it.
|
||||||
|
console.warn('[Shade] Inbox ack failed (will retry):', (err as Error).message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private recordDelivered(msgId: string): void {
|
||||||
|
if (this.deliveredIds.has(msgId)) return;
|
||||||
|
this.deliveredIds.add(msgId);
|
||||||
|
this.deliveredOrder.push(msgId);
|
||||||
|
if (this.deliveredOrder.length > DEFAULT_DEDUP_LRU_CAP) {
|
||||||
|
const evicted = this.deliveredOrder.shift()!;
|
||||||
|
this.deliveredIds.delete(evicted);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,3 +43,11 @@ export type {
|
|||||||
} from './events.js';
|
} from './events.js';
|
||||||
|
|
||||||
export { computeMsgId } from './msg-id.js';
|
export { computeMsgId } from './msg-id.js';
|
||||||
|
|
||||||
|
// V4.9 — encrypted-blob primitive (`/v1/blob/<slotId>`).
|
||||||
|
export { BlobClient, slotIdToHex } from './blob-client.js';
|
||||||
|
export type {
|
||||||
|
BlobClientOptions,
|
||||||
|
BlobGetResult,
|
||||||
|
BlobPutResult,
|
||||||
|
} from './blob-client.js';
|
||||||
|
|||||||
@@ -170,6 +170,255 @@ describe('Inbox orchestrator', () => {
|
|||||||
expect(seen[1]!.to).toBe('carol');
|
expect(seen[1]!.to).toBe('carol');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('cross-channel dedup: acceptBridgeFrame + pollOnce never re-dispatch the same msgId (V4.8.3)', async () => {
|
||||||
|
// Reproduces the Prism FR `cross-channel-duplicate-fanout-v4.8.2`:
|
||||||
|
// a single relay PUT was being delivered twice — once via WS bridge
|
||||||
|
// push, again ~30 s later via inbox-poll catching up. Both copies
|
||||||
|
// would dispatch `shade.receive`, the second one tripping on
|
||||||
|
// already-consumed prekeys. The cross-channel msgId LRU inside
|
||||||
|
// Inbox is the dedup gate; this test exercises it directly via
|
||||||
|
// `acceptBridgeFrame` followed by `pollOnce`.
|
||||||
|
const store = new MemoryInboxStore();
|
||||||
|
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
||||||
|
const bob = await makeIdentity();
|
||||||
|
const alice = await makeIdentity();
|
||||||
|
|
||||||
|
const bobInbox = new Inbox({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
ownAddress: 'bob',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: bob.signingPrivateKey,
|
||||||
|
signingPublicKey: bob.signingPublicKey,
|
||||||
|
pollIntervalMs: 0,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
await bobInbox.register();
|
||||||
|
|
||||||
|
// Alice PUTs a blob via the relay HTTP API.
|
||||||
|
const ct = randBytes(64);
|
||||||
|
const msgId = await computeMsgId(ct);
|
||||||
|
const aliceClient = new InboxClient({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: alice.signingPrivateKey,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
const putResult = await aliceClient.put({
|
||||||
|
recipientAddress: 'bob',
|
||||||
|
senderSigningKey: alice.signingPublicKey,
|
||||||
|
envelope: ct,
|
||||||
|
});
|
||||||
|
expect(putResult.idempotent).toBe(false);
|
||||||
|
|
||||||
|
const dispatched: string[] = [];
|
||||||
|
bobInbox.onIncoming(async (raw) => {
|
||||||
|
dispatched.push(raw.msgId);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Simulate the bridge push arriving first.
|
||||||
|
await bobInbox.acceptBridgeFrame({
|
||||||
|
msgId,
|
||||||
|
ciphertext: ct,
|
||||||
|
receivedAt: putResult.receivedAt,
|
||||||
|
expiresAt: Date.now() + 60_000,
|
||||||
|
});
|
||||||
|
expect(dispatched).toEqual([msgId]);
|
||||||
|
|
||||||
|
// The inbox-poll path catches up next — without dedup it would
|
||||||
|
// re-dispatch. With the LRU it acks-and-skips.
|
||||||
|
const polled = await bobInbox.tick();
|
||||||
|
expect(polled.received).toBe(0);
|
||||||
|
expect(dispatched).toEqual([msgId]); // still one entry
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cross-channel dedup also covers poll-first then bridge-second order', async () => {
|
||||||
|
const store = new MemoryInboxStore();
|
||||||
|
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
||||||
|
const bob = await makeIdentity();
|
||||||
|
const alice = await makeIdentity();
|
||||||
|
|
||||||
|
const bobInbox = new Inbox({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
ownAddress: 'bob',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: bob.signingPrivateKey,
|
||||||
|
signingPublicKey: bob.signingPublicKey,
|
||||||
|
pollIntervalMs: 0,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
await bobInbox.register();
|
||||||
|
const ct = randBytes(48);
|
||||||
|
const msgId = await computeMsgId(ct);
|
||||||
|
const aliceClient = new InboxClient({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: alice.signingPrivateKey,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
const putRes = await aliceClient.put({
|
||||||
|
recipientAddress: 'bob',
|
||||||
|
senderSigningKey: alice.signingPublicKey,
|
||||||
|
envelope: ct,
|
||||||
|
});
|
||||||
|
|
||||||
|
const dispatched: string[] = [];
|
||||||
|
bobInbox.onIncoming(async (raw) => {
|
||||||
|
dispatched.push(raw.msgId);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Poll first.
|
||||||
|
const polled = await bobInbox.tick();
|
||||||
|
expect(polled.received).toBe(1);
|
||||||
|
|
||||||
|
// Bridge frame for the same msgId arrives after the poll already
|
||||||
|
// dispatched + ack'd it — must be a no-op.
|
||||||
|
const handled = await bobInbox.acceptBridgeFrame({
|
||||||
|
msgId,
|
||||||
|
ciphertext: ct,
|
||||||
|
receivedAt: putRes.receivedAt,
|
||||||
|
expiresAt: Date.now() + 60_000,
|
||||||
|
});
|
||||||
|
expect(handled).toBe(false);
|
||||||
|
expect(dispatched).toEqual([msgId]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('burst enqueued during a flush drains immediately, not after 15 s backoff (V4.8.5)', async () => {
|
||||||
|
// Reproduces Prism FR `per-recipient-flush-concurrency-v4.8`: a
|
||||||
|
// burst of envelopes enqueued *during* a slow POST used to sit
|
||||||
|
// ~15 s behind the next flush because both the success path and
|
||||||
|
// the failure path of `flushOnce` rescheduled with the same 15 s
|
||||||
|
// backoff. The fix uses 0 ms when the round delivered something
|
||||||
|
// (network is healthy — drain remainder) and reserves 15 s for
|
||||||
|
// the all-attempts-failed case.
|
||||||
|
const store = new MemoryInboxStore();
|
||||||
|
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
||||||
|
const bob = await makeIdentity();
|
||||||
|
const alice = await makeIdentity();
|
||||||
|
const bobClient = new InboxClient({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: bob.signingPrivateKey,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
await bobClient.register({ address: 'bob', signingKey: bob.signingPublicKey });
|
||||||
|
|
||||||
|
// Wrap fetch so the FIRST PUT (only) takes 100 ms — long enough
|
||||||
|
// for many enqueues to land while it's in flight.
|
||||||
|
let firstPutSeen = false;
|
||||||
|
const slowFirstFetch: typeof fetch = (async (input, init) => {
|
||||||
|
const u =
|
||||||
|
typeof input === 'string'
|
||||||
|
? input
|
||||||
|
: input instanceof URL
|
||||||
|
? input.toString()
|
||||||
|
: (input as Request).url;
|
||||||
|
const isPut = u.includes('/v1/inbox/bob') && !u.includes('/fetch');
|
||||||
|
if (isPut && !firstPutSeen) {
|
||||||
|
firstPutSeen = true;
|
||||||
|
await new Promise((r) => setTimeout(r, 100));
|
||||||
|
}
|
||||||
|
return honoFetch(app)(input, init);
|
||||||
|
}) as typeof fetch;
|
||||||
|
|
||||||
|
const aliceInbox = new Inbox({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
ownAddress: 'alice',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: alice.signingPrivateKey,
|
||||||
|
signingPublicKey: alice.signingPublicKey,
|
||||||
|
pollIntervalMs: 0,
|
||||||
|
fetch: slowFirstFetch,
|
||||||
|
});
|
||||||
|
|
||||||
|
aliceInbox.start();
|
||||||
|
|
||||||
|
// First send — this kicks the slow-PUT path.
|
||||||
|
await aliceInbox.send({ recipientAddress: 'bob', envelope: randBytes(20) });
|
||||||
|
|
||||||
|
// Pile 24 more on top while the first PUT is still in flight. The
|
||||||
|
// first PUT will finish at ~T+100 ms; the subsequent 24 should
|
||||||
|
// drain immediately after, NOT after a 15 s backoff.
|
||||||
|
for (let i = 0; i < 24; i++) {
|
||||||
|
await aliceInbox.send({ recipientAddress: 'bob', envelope: randBytes(20) });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait long enough for the slow first PUT + the immediate
|
||||||
|
// reschedule + the 24-envelope drain. Pre-fix this would still
|
||||||
|
// have ≥1 entry pending after 1 s (waiting for the 15 s timer).
|
||||||
|
await new Promise((r) => setTimeout(r, 1_000));
|
||||||
|
expect(await aliceInbox.pendingCount()).toBe(0);
|
||||||
|
aliceInbox.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('per-recipient parallel drain — slow POST to A does not block POSTs to B (V4.8.5)', async () => {
|
||||||
|
const store = new MemoryInboxStore();
|
||||||
|
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
||||||
|
const alice = await makeIdentity();
|
||||||
|
const bob = await makeIdentity();
|
||||||
|
const carol = await makeIdentity();
|
||||||
|
// Register bob + carol.
|
||||||
|
const reg = async (name: string, kp: { signingPrivateKey: Uint8Array; signingPublicKey: Uint8Array }) => {
|
||||||
|
const c = new InboxClient({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: kp.signingPrivateKey,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
await c.register({ address: name, signingKey: kp.signingPublicKey });
|
||||||
|
};
|
||||||
|
await reg('bob', bob);
|
||||||
|
await reg('carol', carol);
|
||||||
|
|
||||||
|
// bob's PUT route stalls 200 ms; carol's is instant. Pre-fix this
|
||||||
|
// would head-of-line block carol behind bob.
|
||||||
|
const slowedFetch: typeof fetch = (async (input, init) => {
|
||||||
|
const u =
|
||||||
|
typeof input === 'string'
|
||||||
|
? input
|
||||||
|
: input instanceof URL
|
||||||
|
? input.toString()
|
||||||
|
: (input as Request).url;
|
||||||
|
const m = (init as RequestInit | undefined)?.method ?? 'GET';
|
||||||
|
if (m === 'POST' && u.includes('/v1/inbox/bob') && !u.includes('/fetch')) {
|
||||||
|
await new Promise((r) => setTimeout(r, 200));
|
||||||
|
}
|
||||||
|
return honoFetch(app)(input, init);
|
||||||
|
}) as typeof fetch;
|
||||||
|
|
||||||
|
const aliceInbox = new Inbox({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
ownAddress: 'alice',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: alice.signingPrivateKey,
|
||||||
|
signingPublicKey: alice.signingPublicKey,
|
||||||
|
pollIntervalMs: 0,
|
||||||
|
fetch: slowedFetch,
|
||||||
|
});
|
||||||
|
|
||||||
|
const carolDeliveredAt = new Promise<number>((resolve) => {
|
||||||
|
aliceInbox.on((e) => {
|
||||||
|
if (e.name === 'inbox.message_delivered' && e.data.recipientAddress === 'carol') {
|
||||||
|
resolve(Date.now());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const t0 = Date.now();
|
||||||
|
// Bob queue first, carol second — pre-fix carol would wait 200 ms
|
||||||
|
// behind bob's slow PUT. With per-recipient parallelism, carol's
|
||||||
|
// PUT runs concurrently and lands first.
|
||||||
|
await aliceInbox.send({ recipientAddress: 'bob', envelope: randBytes(20) });
|
||||||
|
await aliceInbox.send({ recipientAddress: 'carol', envelope: randBytes(20) });
|
||||||
|
|
||||||
|
aliceInbox.start();
|
||||||
|
const carolAt = await carolDeliveredAt;
|
||||||
|
const carolElapsed = carolAt - t0;
|
||||||
|
expect(carolElapsed).toBeLessThan(150);
|
||||||
|
aliceInbox.stop();
|
||||||
|
});
|
||||||
|
|
||||||
test('flush retries on transient server failure', async () => {
|
test('flush retries on transient server failure', async () => {
|
||||||
const store = new MemoryInboxStore();
|
const store = new MemoryInboxStore();
|
||||||
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
||||||
@@ -281,3 +530,195 @@ describe('tamper detection', () => {
|
|||||||
expect(result.received).toBe(0);
|
expect(result.received).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('InboxClient — default fetch is bound to globalThis', () => {
|
||||||
|
// Regression: browsers' `fetch` is a WebIDL bound operation that throws
|
||||||
|
// "Illegal invocation" when called as a method on another object. The
|
||||||
|
// class stores `fetchImpl` and calls `this.fetchImpl(...)`, which strips
|
||||||
|
// the Window receiver. Constructor must `bind(globalThis)`.
|
||||||
|
test('default path passes globalThis as `this` (no Illegal invocation)', async () => {
|
||||||
|
const realFetch = globalThis.fetch;
|
||||||
|
let observedReceiver: unknown = 'unset';
|
||||||
|
function strictFetch(this: unknown, _input: unknown, _init?: unknown): Promise<Response> {
|
||||||
|
observedReceiver = this;
|
||||||
|
if (this !== globalThis) {
|
||||||
|
throw new TypeError("Failed to execute 'fetch' on 'Window': Illegal invocation");
|
||||||
|
}
|
||||||
|
return Promise.resolve(
|
||||||
|
new Response('{}', {
|
||||||
|
status: 200,
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Object.defineProperty(globalThis, 'fetch', {
|
||||||
|
configurable: true,
|
||||||
|
writable: true,
|
||||||
|
value: strictFetch,
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const id = await makeIdentity();
|
||||||
|
const client = new InboxClient({
|
||||||
|
baseUrl: 'http://example.invalid',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: id.signingPrivateKey,
|
||||||
|
// No `fetch` override on purpose — this exercises the default path.
|
||||||
|
});
|
||||||
|
await client.register({ address: 'whoever', signingKey: id.signingPublicKey });
|
||||||
|
expect(observedReceiver).toBe(globalThis);
|
||||||
|
} finally {
|
||||||
|
Object.defineProperty(globalThis, 'fetch', {
|
||||||
|
configurable: true,
|
||||||
|
writable: true,
|
||||||
|
value: realFetch,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Inbox.start() — fresh-address register/poll race (V4.8)', () => {
|
||||||
|
// Regression: pre-4.8 `start()` called `register()` fire-and-forget AND
|
||||||
|
// `schedulePoll(0)` synchronously, so the first poll often beat the
|
||||||
|
// register HTTP RTT and got SHADE_NOT_FOUND on a fresh address. Fix:
|
||||||
|
// start() defers the first poll; register() success kicks it.
|
||||||
|
test('fresh address: no fetch fires before register completes', async () => {
|
||||||
|
const store = new MemoryInboxStore();
|
||||||
|
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
||||||
|
const alice = await makeIdentity();
|
||||||
|
|
||||||
|
// Order observed by the server: must be register-then-fetch, never
|
||||||
|
// fetch-then-register.
|
||||||
|
const calls: Array<'register' | 'fetch' | 'put'> = [];
|
||||||
|
let registerArrived = false;
|
||||||
|
const recordingFetch: typeof fetch = (async (input, init) => {
|
||||||
|
const u =
|
||||||
|
typeof input === 'string'
|
||||||
|
? input
|
||||||
|
: input instanceof URL
|
||||||
|
? input.toString()
|
||||||
|
: (input as Request).url;
|
||||||
|
if (u.includes('/v1/inbox/register')) {
|
||||||
|
calls.push('register');
|
||||||
|
// Hold register for a tick to widen the race window.
|
||||||
|
await new Promise((r) => setTimeout(r, 25));
|
||||||
|
registerArrived = true;
|
||||||
|
} else if (u.endsWith('/fetch')) {
|
||||||
|
// Any fetch arriving before register is the race we're guarding
|
||||||
|
// against.
|
||||||
|
if (!registerArrived) {
|
||||||
|
throw new Error('fetch fired before register completed (race not fixed)');
|
||||||
|
}
|
||||||
|
calls.push('fetch');
|
||||||
|
} else if (u.includes('/v1/inbox/')) {
|
||||||
|
calls.push('put');
|
||||||
|
}
|
||||||
|
return honoFetch(app)(input, init);
|
||||||
|
}) as typeof fetch;
|
||||||
|
|
||||||
|
const inbox = new Inbox({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
ownAddress: 'alice',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: alice.signingPrivateKey,
|
||||||
|
signingPublicKey: alice.signingPublicKey,
|
||||||
|
pollIntervalMs: 30_000, // Long enough that only register's kick triggers.
|
||||||
|
fetch: recordingFetch,
|
||||||
|
});
|
||||||
|
inbox.onIncoming(() => null);
|
||||||
|
inbox.start();
|
||||||
|
|
||||||
|
// Wait until register has completed and the success-kick poll lands.
|
||||||
|
await new Promise((r) => setTimeout(r, 100));
|
||||||
|
inbox.stop();
|
||||||
|
|
||||||
|
expect(calls[0]).toBe('register');
|
||||||
|
// First fetch (if any) must be after register.
|
||||||
|
const firstFetchIdx = calls.indexOf('fetch');
|
||||||
|
if (firstFetchIdx !== -1) {
|
||||||
|
expect(firstFetchIdx).toBeGreaterThan(calls.indexOf('register'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('FetchedBlob.from — relay-supplied sender fingerprint (V4.8)', () => {
|
||||||
|
test('inbox-fetch response carries from = 8-byte hex of SHA-256(senderSigningKey)', async () => {
|
||||||
|
const store = new MemoryInboxStore();
|
||||||
|
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
||||||
|
const bob = await makeIdentity();
|
||||||
|
const alice = await makeIdentity();
|
||||||
|
|
||||||
|
const bobClient = new InboxClient({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: bob.signingPrivateKey,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
const aliceClient = new InboxClient({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: alice.signingPrivateKey,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
|
||||||
|
await bobClient.register({ address: 'bob', signingKey: bob.signingPublicKey });
|
||||||
|
await aliceClient.put({
|
||||||
|
recipientAddress: 'bob',
|
||||||
|
senderSigningKey: alice.signingPublicKey,
|
||||||
|
envelope: randBytes(64),
|
||||||
|
});
|
||||||
|
|
||||||
|
const fetched = await bobClient.fetch({ address: 'bob' });
|
||||||
|
expect(fetched.blobs.length).toBe(1);
|
||||||
|
const fp = fetched.blobs[0]!.from;
|
||||||
|
expect(fp).toBeDefined();
|
||||||
|
expect(fp).toMatch(/^[0-9a-f]{16}$/);
|
||||||
|
|
||||||
|
// Must be reproducible: SHA-256(alice.signingPublicKey) → first 8 bytes hex.
|
||||||
|
const digest = await globalThis.crypto.subtle.digest(
|
||||||
|
'SHA-256',
|
||||||
|
alice.signingPublicKey as unknown as ArrayBuffer,
|
||||||
|
);
|
||||||
|
const expected = Array.from(new Uint8Array(digest).slice(0, 8), (b) =>
|
||||||
|
b.toString(16).padStart(2, '0'),
|
||||||
|
).join('');
|
||||||
|
expect(fp).toBe(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('DecryptHandler raw arg propagates from to the app', async () => {
|
||||||
|
const store = new MemoryInboxStore();
|
||||||
|
const app = createInboxServer({ crypto, store, disableRateLimit: true });
|
||||||
|
const bob = await makeIdentity();
|
||||||
|
const alice = await makeIdentity();
|
||||||
|
|
||||||
|
const aliceClient = new InboxClient({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: alice.signingPrivateKey,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
const bobInbox = new Inbox({
|
||||||
|
baseUrl: 'http://localhost',
|
||||||
|
ownAddress: 'bob',
|
||||||
|
crypto,
|
||||||
|
signingPrivateKey: bob.signingPrivateKey,
|
||||||
|
signingPublicKey: bob.signingPublicKey,
|
||||||
|
pollIntervalMs: 0,
|
||||||
|
fetch: honoFetch(app),
|
||||||
|
});
|
||||||
|
|
||||||
|
await bobInbox.register();
|
||||||
|
await aliceClient.put({
|
||||||
|
recipientAddress: 'bob',
|
||||||
|
senderSigningKey: alice.signingPublicKey,
|
||||||
|
envelope: randBytes(40),
|
||||||
|
});
|
||||||
|
|
||||||
|
let observed: string | undefined = undefined;
|
||||||
|
bobInbox.onIncoming((raw) => {
|
||||||
|
observed = raw.from;
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
await bobInbox.tick();
|
||||||
|
expect(observed).toMatch(/^[0-9a-f]{16}$/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/key-transparency",
|
"name": "@shade/key-transparency",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/keychain",
|
"name": "@shade/keychain",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/observability",
|
"name": "@shade/observability",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/observer",
|
"name": "@shade/observer",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/proto",
|
"name": "@shade/proto",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -5,7 +5,23 @@ export {
|
|||||||
encodeRatchetMessage,
|
encodeRatchetMessage,
|
||||||
encodeStreamChunk,
|
encodeStreamChunk,
|
||||||
decodeStreamChunk,
|
decodeStreamChunk,
|
||||||
|
encodeBroadcast,
|
||||||
|
decodeBroadcast,
|
||||||
inspectEnvelopeType,
|
inspectEnvelopeType,
|
||||||
|
encodeStreamOpen,
|
||||||
|
encodeStreamOpenAck,
|
||||||
|
decodeStreamHandshake,
|
||||||
|
encodeStreamFrame,
|
||||||
|
decodeStreamFrame,
|
||||||
TYPE_STREAM_CHUNK,
|
TYPE_STREAM_CHUNK,
|
||||||
|
TYPE_BROADCAST,
|
||||||
|
TYPE_STREAM_OPEN,
|
||||||
|
TYPE_STREAM_OPEN_ACK,
|
||||||
|
TYPE_STREAM_FRAME,
|
||||||
|
} from './wire.js';
|
||||||
|
export type {
|
||||||
|
StreamChunkWire,
|
||||||
|
BroadcastWire,
|
||||||
|
StreamHandshakeWire,
|
||||||
|
StreamFrameWire,
|
||||||
} from './wire.js';
|
} from './wire.js';
|
||||||
export type { StreamChunkWire } from './wire.js';
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
* 0x01 = PreKeyMessage
|
* 0x01 = PreKeyMessage
|
||||||
* 0x02 = RatchetMessage
|
* 0x02 = RatchetMessage
|
||||||
* 0x11 = StreamChunk
|
* 0x11 = StreamChunk
|
||||||
|
* 0x21 = BroadcastMessage (V4.6 — sender-key encrypted group payload)
|
||||||
*
|
*
|
||||||
* All multi-byte integers are big-endian.
|
* All multi-byte integers are big-endian.
|
||||||
*
|
*
|
||||||
@@ -23,6 +24,14 @@ const VERSION = 0x02;
|
|||||||
const TYPE_PREKEY = 0x01;
|
const TYPE_PREKEY = 0x01;
|
||||||
const TYPE_RATCHET = 0x02;
|
const TYPE_RATCHET = 0x02;
|
||||||
export const TYPE_STREAM_CHUNK = 0x11;
|
export const TYPE_STREAM_CHUNK = 0x11;
|
||||||
|
export const TYPE_BROADCAST = 0x21;
|
||||||
|
// V4.11 — streaming Double-Ratchet sub-session (long-lived WS channels).
|
||||||
|
export const TYPE_STREAM_OPEN = 0x31;
|
||||||
|
export const TYPE_STREAM_OPEN_ACK = 0x32;
|
||||||
|
export const TYPE_STREAM_FRAME = 0x33;
|
||||||
|
|
||||||
|
const STREAM_SESSION_ID_BYTES = 16;
|
||||||
|
const STREAM_EPHEMERAL_BYTES = 32;
|
||||||
|
|
||||||
// ─── Stream chunk types ──────────────────────────────────────
|
// ─── Stream chunk types ──────────────────────────────────────
|
||||||
|
|
||||||
@@ -43,6 +52,28 @@ export interface StreamChunkWire {
|
|||||||
ciphertext: Uint8Array; // AES-GCM ciphertext including 16-byte tag
|
ciphertext: Uint8Array; // AES-GCM ciphertext including 16-byte tag
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wire-decoded broadcast envelope (type 0x21).
|
||||||
|
*
|
||||||
|
* Carries a Signal-style sender-key message. The sender (channel owner)
|
||||||
|
* encrypted once with their per-channel chain key; this same byte sequence
|
||||||
|
* is delivered verbatim to every member. Authenticity rides on the embedded
|
||||||
|
* Ed25519 signature — no bilateral ratchet wraps the broadcast itself.
|
||||||
|
*
|
||||||
|
* `generation` is a per-channel rotation counter: bumped each time a
|
||||||
|
* member is revoked. Receivers silently drop broadcasts at older
|
||||||
|
* generations than their currently-installed sender-key.
|
||||||
|
*/
|
||||||
|
export interface BroadcastWire {
|
||||||
|
channelId: string; // utf-8, length-prefixed
|
||||||
|
senderAddress: string; // utf-8, length-prefixed
|
||||||
|
generation: number; // u32
|
||||||
|
iteration: number; // u32 — sender chain counter
|
||||||
|
nonce: Uint8Array; // AES-GCM nonce (12 bytes)
|
||||||
|
signature: Uint8Array; // Ed25519 signature (64 bytes)
|
||||||
|
ciphertext: Uint8Array; // AES-GCM ciphertext including 16-byte tag
|
||||||
|
}
|
||||||
|
|
||||||
const STREAM_ID_BYTES = 16;
|
const STREAM_ID_BYTES = 16;
|
||||||
const STREAM_NONCE_BYTES = 12;
|
const STREAM_NONCE_BYTES = 12;
|
||||||
|
|
||||||
@@ -230,11 +261,19 @@ export function decodeStreamChunk(data: Uint8Array): StreamChunkWire {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Inspect the type tag of an arbitrary envelope without full parsing.
|
* Inspect the type tag of an arbitrary envelope without full parsing.
|
||||||
* Returns one of `'prekey' | 'ratchet' | 'stream-chunk' | 'unknown'`.
|
* Returns one of `'prekey' | 'ratchet' | 'stream-chunk' | 'broadcast' | 'unknown'`.
|
||||||
*/
|
*/
|
||||||
export function inspectEnvelopeType(
|
export function inspectEnvelopeType(
|
||||||
data: Uint8Array,
|
data: Uint8Array,
|
||||||
): 'prekey' | 'ratchet' | 'stream-chunk' | 'unknown' {
|
):
|
||||||
|
| 'prekey'
|
||||||
|
| 'ratchet'
|
||||||
|
| 'stream-chunk'
|
||||||
|
| 'broadcast'
|
||||||
|
| 'stream-open'
|
||||||
|
| 'stream-open-ack'
|
||||||
|
| 'stream-frame'
|
||||||
|
| 'unknown' {
|
||||||
if (data.length < 2 || data[0] !== VERSION) return 'unknown';
|
if (data.length < 2 || data[0] !== VERSION) return 'unknown';
|
||||||
switch (data[1]) {
|
switch (data[1]) {
|
||||||
case TYPE_PREKEY:
|
case TYPE_PREKEY:
|
||||||
@@ -243,11 +282,227 @@ export function inspectEnvelopeType(
|
|||||||
return 'ratchet';
|
return 'ratchet';
|
||||||
case TYPE_STREAM_CHUNK:
|
case TYPE_STREAM_CHUNK:
|
||||||
return 'stream-chunk';
|
return 'stream-chunk';
|
||||||
|
case TYPE_BROADCAST:
|
||||||
|
return 'broadcast';
|
||||||
|
case TYPE_STREAM_OPEN:
|
||||||
|
return 'stream-open';
|
||||||
|
case TYPE_STREAM_OPEN_ACK:
|
||||||
|
return 'stream-open-ack';
|
||||||
|
case TYPE_STREAM_FRAME:
|
||||||
|
return 'stream-frame';
|
||||||
default:
|
default:
|
||||||
return 'unknown';
|
return 'unknown';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Stream sub-session wire (V4.11) ─────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A decoded stream handshake frame (`STREAM_OPEN` / `STREAM_OPEN_ACK`).
|
||||||
|
* Both share the layout `[version][type][streamId:16][ephemeralPub:32]`.
|
||||||
|
*/
|
||||||
|
export interface StreamHandshakeWire {
|
||||||
|
kind: 'open' | 'open-ack';
|
||||||
|
streamId: Uint8Array; // 16 bytes
|
||||||
|
ephemeralPub: Uint8Array; // 32 bytes (X25519)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A decoded sealed stream frame (`STREAM_FRAME`): a streamId plus an
|
||||||
|
* embedded Double-Ratchet message. One sealed logical frame ⇒ exactly
|
||||||
|
* one of these ⇒ one WS text/binary frame.
|
||||||
|
*/
|
||||||
|
export interface StreamFrameWire {
|
||||||
|
streamId: Uint8Array; // 16 bytes
|
||||||
|
message: RatchetMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
function encodeStreamHandshake(
|
||||||
|
type: number,
|
||||||
|
streamId: Uint8Array,
|
||||||
|
ephemeralPub: Uint8Array,
|
||||||
|
): Uint8Array {
|
||||||
|
if (streamId.length !== STREAM_SESSION_ID_BYTES) {
|
||||||
|
throw new Error(`streamId must be ${STREAM_SESSION_ID_BYTES} bytes`);
|
||||||
|
}
|
||||||
|
if (ephemeralPub.length !== STREAM_EPHEMERAL_BYTES) {
|
||||||
|
throw new Error(`ephemeralPub must be ${STREAM_EPHEMERAL_BYTES} bytes`);
|
||||||
|
}
|
||||||
|
const out = new Uint8Array(2 + STREAM_SESSION_ID_BYTES + STREAM_EPHEMERAL_BYTES);
|
||||||
|
out[0] = VERSION;
|
||||||
|
out[1] = type;
|
||||||
|
out.set(streamId, 2);
|
||||||
|
out.set(ephemeralPub, 2 + STREAM_SESSION_ID_BYTES);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Encode the initiator's `STREAM_OPEN` (streamId + initiator ephemeral). */
|
||||||
|
export function encodeStreamOpen(streamId: Uint8Array, ephemeralPub: Uint8Array): Uint8Array {
|
||||||
|
return encodeStreamHandshake(TYPE_STREAM_OPEN, streamId, ephemeralPub);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Encode the responder's `STREAM_OPEN_ACK` (streamId + responder ephemeral). */
|
||||||
|
export function encodeStreamOpenAck(streamId: Uint8Array, ephemeralPub: Uint8Array): Uint8Array {
|
||||||
|
return encodeStreamHandshake(TYPE_STREAM_OPEN_ACK, streamId, ephemeralPub);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Decode either handshake frame. Throws on wrong type / bad length. */
|
||||||
|
export function decodeStreamHandshake(data: Uint8Array): StreamHandshakeWire {
|
||||||
|
const expected = 2 + STREAM_SESSION_ID_BYTES + STREAM_EPHEMERAL_BYTES;
|
||||||
|
if (data.length !== expected) {
|
||||||
|
throw new Error(`stream handshake must be ${expected} bytes, got ${data.length}`);
|
||||||
|
}
|
||||||
|
if (data[0] !== VERSION) throw new Error(`Unknown version: ${data[0]}`);
|
||||||
|
let kind: 'open' | 'open-ack';
|
||||||
|
if (data[1] === TYPE_STREAM_OPEN) kind = 'open';
|
||||||
|
else if (data[1] === TYPE_STREAM_OPEN_ACK) kind = 'open-ack';
|
||||||
|
else throw new Error(`Not a stream handshake: type=${data[1]}`);
|
||||||
|
return {
|
||||||
|
kind,
|
||||||
|
streamId: data.slice(2, 2 + STREAM_SESSION_ID_BYTES),
|
||||||
|
ephemeralPub: data.slice(
|
||||||
|
2 + STREAM_SESSION_ID_BYTES,
|
||||||
|
2 + STREAM_SESSION_ID_BYTES + STREAM_EPHEMERAL_BYTES,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode a sealed stream frame: `[version][0x33][streamId:16][ratchet…]`.
|
||||||
|
* Reuses the exact ratchet-message inner codec the HTTP path uses, so a
|
||||||
|
* stream frame carries the same Double-Ratchet header + AEAD payload.
|
||||||
|
*/
|
||||||
|
export function encodeStreamFrame(streamId: Uint8Array, msg: RatchetMessage): Uint8Array {
|
||||||
|
if (streamId.length !== STREAM_SESSION_ID_BYTES) {
|
||||||
|
throw new Error(`streamId must be ${STREAM_SESSION_ID_BYTES} bytes`);
|
||||||
|
}
|
||||||
|
const inner = encodeRatchetMessageInner(msg);
|
||||||
|
const out = new Uint8Array(2 + STREAM_SESSION_ID_BYTES + inner.length);
|
||||||
|
out[0] = VERSION;
|
||||||
|
out[1] = TYPE_STREAM_FRAME;
|
||||||
|
out.set(streamId, 2);
|
||||||
|
out.set(inner, 2 + STREAM_SESSION_ID_BYTES);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Decode a sealed stream frame. Throws on wrong type / truncation. */
|
||||||
|
export function decodeStreamFrame(data: Uint8Array): StreamFrameWire {
|
||||||
|
const minSize = 2 + STREAM_SESSION_ID_BYTES;
|
||||||
|
if (data.length < minSize) {
|
||||||
|
throw new Error(`stream-frame too short: ${data.length} < ${minSize}`);
|
||||||
|
}
|
||||||
|
if (data[0] !== VERSION) throw new Error(`Unknown version: ${data[0]}`);
|
||||||
|
if (data[1] !== TYPE_STREAM_FRAME) {
|
||||||
|
throw new Error(`Not a stream-frame: type=${data[1]}`);
|
||||||
|
}
|
||||||
|
const streamId = data.slice(2, 2 + STREAM_SESSION_ID_BYTES);
|
||||||
|
const message = decodeRatchetMessageInner(data, 2 + STREAM_SESSION_ID_BYTES).value;
|
||||||
|
return { streamId, message };
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Broadcast wire (V4.6) ───────────────────────────────────
|
||||||
|
|
||||||
|
const BROADCAST_NONCE_BYTES = 12;
|
||||||
|
const BROADCAST_SIGNATURE_BYTES = 64;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode a broadcast envelope to wire bytes (type 0x21).
|
||||||
|
*
|
||||||
|
* Layout:
|
||||||
|
* [version:1][type=0x21:1]
|
||||||
|
* [channelIdLen:u16][channelId utf-8]
|
||||||
|
* [senderAddrLen:u16][senderAddr utf-8]
|
||||||
|
* [generation:u32]
|
||||||
|
* [iteration:u32]
|
||||||
|
* [nonce:12]
|
||||||
|
* [signature:64]
|
||||||
|
* [ctLen:u32][ciphertext]
|
||||||
|
*/
|
||||||
|
export function encodeBroadcast(b: BroadcastWire): Uint8Array {
|
||||||
|
if (b.nonce.length !== BROADCAST_NONCE_BYTES) {
|
||||||
|
throw new Error(`broadcast nonce must be ${BROADCAST_NONCE_BYTES} bytes`);
|
||||||
|
}
|
||||||
|
if (b.signature.length !== BROADCAST_SIGNATURE_BYTES) {
|
||||||
|
throw new Error(`broadcast signature must be ${BROADCAST_SIGNATURE_BYTES} bytes`);
|
||||||
|
}
|
||||||
|
const enc = new TextEncoder();
|
||||||
|
const channelIdBytes = enc.encode(b.channelId);
|
||||||
|
const senderBytes = enc.encode(b.senderAddress);
|
||||||
|
if (channelIdBytes.length > 0xffff) throw new Error('channelId too long');
|
||||||
|
if (senderBytes.length > 0xffff) throw new Error('senderAddress too long');
|
||||||
|
|
||||||
|
const headerSize =
|
||||||
|
1 + 1 +
|
||||||
|
2 + channelIdBytes.length +
|
||||||
|
2 + senderBytes.length +
|
||||||
|
4 + 4 +
|
||||||
|
BROADCAST_NONCE_BYTES +
|
||||||
|
BROADCAST_SIGNATURE_BYTES +
|
||||||
|
4;
|
||||||
|
const out = new Uint8Array(headerSize + b.ciphertext.length);
|
||||||
|
const view = new DataView(out.buffer);
|
||||||
|
let offset = 0;
|
||||||
|
out[offset++] = VERSION;
|
||||||
|
out[offset++] = TYPE_BROADCAST;
|
||||||
|
|
||||||
|
view.setUint16(offset, channelIdBytes.length, false); offset += 2;
|
||||||
|
out.set(channelIdBytes, offset); offset += channelIdBytes.length;
|
||||||
|
|
||||||
|
view.setUint16(offset, senderBytes.length, false); offset += 2;
|
||||||
|
out.set(senderBytes, offset); offset += senderBytes.length;
|
||||||
|
|
||||||
|
view.setUint32(offset, b.generation, false); offset += 4;
|
||||||
|
view.setUint32(offset, b.iteration, false); offset += 4;
|
||||||
|
|
||||||
|
out.set(b.nonce, offset); offset += BROADCAST_NONCE_BYTES;
|
||||||
|
out.set(b.signature, offset); offset += BROADCAST_SIGNATURE_BYTES;
|
||||||
|
|
||||||
|
view.setUint32(offset, b.ciphertext.length, false); offset += 4;
|
||||||
|
out.set(b.ciphertext, offset);
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decodeBroadcast(data: Uint8Array): BroadcastWire {
|
||||||
|
const minSize = 1 + 1 + 2 + 0 + 2 + 0 + 4 + 4 +
|
||||||
|
BROADCAST_NONCE_BYTES + BROADCAST_SIGNATURE_BYTES + 4;
|
||||||
|
if (data.length < minSize) {
|
||||||
|
throw new Error(`broadcast envelope too short: ${data.length} < ${minSize}`);
|
||||||
|
}
|
||||||
|
if (data[0] !== VERSION) throw new Error(`Unknown version: ${data[0]}`);
|
||||||
|
if (data[1] !== TYPE_BROADCAST) throw new Error(`Not a broadcast: type=${data[1]}`);
|
||||||
|
|
||||||
|
const view = new DataView(data.buffer, data.byteOffset);
|
||||||
|
const dec = new TextDecoder();
|
||||||
|
let offset = 2;
|
||||||
|
|
||||||
|
const channelIdLen = view.getUint16(offset, false); offset += 2;
|
||||||
|
if (offset + channelIdLen > data.length) throw new Error('broadcast truncated in channelId');
|
||||||
|
const channelId = dec.decode(data.slice(offset, offset + channelIdLen));
|
||||||
|
offset += channelIdLen;
|
||||||
|
|
||||||
|
const senderLen = view.getUint16(offset, false); offset += 2;
|
||||||
|
if (offset + senderLen > data.length) throw new Error('broadcast truncated in senderAddress');
|
||||||
|
const senderAddress = dec.decode(data.slice(offset, offset + senderLen));
|
||||||
|
offset += senderLen;
|
||||||
|
|
||||||
|
const generation = view.getUint32(offset, false); offset += 4;
|
||||||
|
const iteration = view.getUint32(offset, false); offset += 4;
|
||||||
|
|
||||||
|
const nonce = data.slice(offset, offset + BROADCAST_NONCE_BYTES);
|
||||||
|
offset += BROADCAST_NONCE_BYTES;
|
||||||
|
const signature = data.slice(offset, offset + BROADCAST_SIGNATURE_BYTES);
|
||||||
|
offset += BROADCAST_SIGNATURE_BYTES;
|
||||||
|
|
||||||
|
const ctLen = view.getUint32(offset, false); offset += 4;
|
||||||
|
if (offset + ctLen !== data.length) {
|
||||||
|
throw new Error(`broadcast length mismatch: declared ${offset + ctLen}, actual ${data.length}`);
|
||||||
|
}
|
||||||
|
const ciphertext = data.slice(offset, offset + ctLen);
|
||||||
|
|
||||||
|
return { channelId, senderAddress, generation, iteration, nonce, signature, ciphertext };
|
||||||
|
}
|
||||||
|
|
||||||
function decodePreKeyMessageInner(data: Uint8Array): PreKeyMessage {
|
function decodePreKeyMessageInner(data: Uint8Array): PreKeyMessage {
|
||||||
let offset = 0;
|
let offset = 0;
|
||||||
|
|
||||||
|
|||||||
71
packages/shade-proto/tests/stream-wire.test.ts
Normal file
71
packages/shade-proto/tests/stream-wire.test.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import { describe, test, expect } from 'bun:test';
|
||||||
|
import {
|
||||||
|
encodeStreamOpen,
|
||||||
|
encodeStreamOpenAck,
|
||||||
|
decodeStreamHandshake,
|
||||||
|
encodeStreamFrame,
|
||||||
|
decodeStreamFrame,
|
||||||
|
inspectEnvelopeType,
|
||||||
|
} from '../src/index.js';
|
||||||
|
import type { RatchetMessage } from '@shade/core';
|
||||||
|
|
||||||
|
function randBytes(n: number): Uint8Array {
|
||||||
|
const buf = new Uint8Array(n);
|
||||||
|
crypto.getRandomValues(buf);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeRatchetMessage(): RatchetMessage {
|
||||||
|
return {
|
||||||
|
dhPublicKey: randBytes(32),
|
||||||
|
previousCounter: 3,
|
||||||
|
counter: 9001,
|
||||||
|
ciphertext: randBytes(128),
|
||||||
|
nonce: randBytes(12),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Stream sub-session wire (V4.11)', () => {
|
||||||
|
test('STREAM_OPEN round-trips and inspects', () => {
|
||||||
|
const sid = randBytes(16);
|
||||||
|
const eph = randBytes(32);
|
||||||
|
const bytes = encodeStreamOpen(sid, eph);
|
||||||
|
expect(inspectEnvelopeType(bytes)).toBe('stream-open');
|
||||||
|
const hs = decodeStreamHandshake(bytes);
|
||||||
|
expect(hs.kind).toBe('open');
|
||||||
|
expect(hs.streamId).toEqual(sid);
|
||||||
|
expect(hs.ephemeralPub).toEqual(eph);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('STREAM_OPEN_ACK round-trips and inspects', () => {
|
||||||
|
const sid = randBytes(16);
|
||||||
|
const eph = randBytes(32);
|
||||||
|
const bytes = encodeStreamOpenAck(sid, eph);
|
||||||
|
expect(inspectEnvelopeType(bytes)).toBe('stream-open-ack');
|
||||||
|
const hs = decodeStreamHandshake(bytes);
|
||||||
|
expect(hs.kind).toBe('open-ack');
|
||||||
|
expect(hs.streamId).toEqual(sid);
|
||||||
|
expect(hs.ephemeralPub).toEqual(eph);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('STREAM_FRAME carries a full ratchet message verbatim', () => {
|
||||||
|
const sid = randBytes(16);
|
||||||
|
const msg = makeRatchetMessage();
|
||||||
|
const bytes = encodeStreamFrame(sid, msg);
|
||||||
|
expect(inspectEnvelopeType(bytes)).toBe('stream-frame');
|
||||||
|
const decoded = decodeStreamFrame(bytes);
|
||||||
|
expect(decoded.streamId).toEqual(sid);
|
||||||
|
expect(decoded.message.dhPublicKey).toEqual(msg.dhPublicKey);
|
||||||
|
expect(decoded.message.previousCounter).toBe(msg.previousCounter);
|
||||||
|
expect(decoded.message.counter).toBe(msg.counter);
|
||||||
|
expect(decoded.message.ciphertext).toEqual(msg.ciphertext);
|
||||||
|
expect(decoded.message.nonce).toEqual(msg.nonce);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('rejects wrong sizes and wrong type tags', () => {
|
||||||
|
expect(() => encodeStreamOpen(randBytes(15), randBytes(32))).toThrow();
|
||||||
|
expect(() => encodeStreamOpen(randBytes(16), randBytes(31))).toThrow();
|
||||||
|
expect(() => decodeStreamHandshake(encodeStreamFrame(randBytes(16), makeRatchetMessage()))).toThrow();
|
||||||
|
expect(() => decodeStreamFrame(encodeStreamOpen(randBytes(16), randBytes(32)))).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/recovery",
|
"name": "@shade/recovery",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/sdk",
|
"name": "@shade/sdk",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
@@ -8,11 +8,13 @@
|
|||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
"@shade/files": "workspace:*",
|
"@shade/files": "workspace:*",
|
||||||
|
"@shade/inbox": "workspace:*",
|
||||||
"@shade/key-transparency": "workspace:*",
|
"@shade/key-transparency": "workspace:*",
|
||||||
"@shade/observability": "workspace:*",
|
"@shade/observability": "workspace:*",
|
||||||
"@shade/observer": "workspace:*",
|
"@shade/observer": "workspace:*",
|
||||||
"@shade/proto": "workspace:*",
|
"@shade/proto": "workspace:*",
|
||||||
"@shade/server": "workspace:*",
|
"@shade/server": "workspace:*",
|
||||||
|
"@shade/storage-encrypted": "workspace:*",
|
||||||
"@shade/storage-sqlite": "workspace:*",
|
"@shade/storage-sqlite": "workspace:*",
|
||||||
"@shade/streams": "workspace:*",
|
"@shade/streams": "workspace:*",
|
||||||
"@shade/transfer": "workspace:*",
|
"@shade/transfer": "workspace:*",
|
||||||
@@ -27,6 +29,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@shade/inbox-server": "workspace:*",
|
||||||
"@shade/transport-webrtc": "workspace:*"
|
"@shade/transport-webrtc": "workspace:*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
792
packages/shade-sdk/src/approval.ts
Normal file
792
packages/shade-sdk/src/approval.ts
Normal file
@@ -0,0 +1,792 @@
|
|||||||
|
import type { CryptoProvider } from '@shade/core';
|
||||||
|
import { ValidationError } from '@shade/core';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V4.10 — cross-host approval routing.
|
||||||
|
*
|
||||||
|
* Two pieces, both app-level (no relay changes):
|
||||||
|
*
|
||||||
|
* 1. A canonical schema for the V4.9 profile blob. The blob is opaque to
|
||||||
|
* Shade, but Prism's `cross-host-approval-routing` FR pointed out that
|
||||||
|
* every Shade app that supports credential-driven device linking will
|
||||||
|
* end up reinventing the same `hosts[]` / `clients[]` /
|
||||||
|
* `trustedApproverFingerprints[]` shape. We bless one here so apps can
|
||||||
|
* interop and so the proxy-approval verify path has a stable place to
|
||||||
|
* look up trusted-approver public keys.
|
||||||
|
*
|
||||||
|
* 2. Build / sign / verify helpers for `approvalNeeded` and
|
||||||
|
* `linkApproveByProxy` frames. The frames themselves are app-defined
|
||||||
|
* payloads sent over the existing `Shade.send` / `Shade.receive`
|
||||||
|
* bilateral E2EE channel — Shade just exposes the signing-payload
|
||||||
|
* canonicalization and Ed25519 plumbing so a fingerprint+signature
|
||||||
|
* bound to a long-term identity key can be verified on any host that
|
||||||
|
* has a fresh copy of the user's profile blob.
|
||||||
|
*
|
||||||
|
* The proxy-approval signature is *belt-and-suspenders* on top of the
|
||||||
|
* E2EE that delivered the frame: the E2EE channel already authenticates
|
||||||
|
* the sender's session, but the signature ties the approval explicitly
|
||||||
|
* to the approver's long-term identity key. That makes the approval
|
||||||
|
* verifiable independently of session state — useful if a host
|
||||||
|
* receives a forwarded approval without a prior session, and necessary
|
||||||
|
* for replay-resistance in the face of a compromised relay that
|
||||||
|
* reorders bilateral sends.
|
||||||
|
*
|
||||||
|
* Headless servers without a GUI use this with the existing X3DH-on-
|
||||||
|
* first-send behavior of `Shade.send` to ask any trusted-approver
|
||||||
|
* client (typically a phone) to approve a `linkRequest` from a new
|
||||||
|
* device. The approver phone biometrically gates a long-term Ed25519
|
||||||
|
* sign, ships the frame back, and the server runs `verifyProxyApproval`
|
||||||
|
* against the freshest profile blob before completing pairing.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ─── Canonical profile-blob schema ─────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A host: a device that *receives* `linkRequest` frames from new
|
||||||
|
* devices and runs the pairing handshake. Typically a desktop, server,
|
||||||
|
* or always-on laptop. Hosts have no special crypto status — they're
|
||||||
|
* just where the user's data lives.
|
||||||
|
*/
|
||||||
|
export interface ProfileHostEntry {
|
||||||
|
/** Shade address (`device:...`). */
|
||||||
|
address: string;
|
||||||
|
/** Human-friendly name (e.g. "PC", "Server"). */
|
||||||
|
name: string;
|
||||||
|
/** Open enum: `'desktop' | 'server' | 'laptop' | ...`. */
|
||||||
|
kind: string;
|
||||||
|
/** Wall-clock ms when this host was added to the profile. */
|
||||||
|
addedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A client: a device that *initiates* link/approval flows and may
|
||||||
|
* proxy-approve link-requests on behalf of the account when
|
||||||
|
* `trustedApprover === true`. Typically a phone or tablet.
|
||||||
|
*
|
||||||
|
* `identityPublicKey` is the 32-byte Ed25519 long-term identity key.
|
||||||
|
* `identityFingerprint` is the human-readable safety-number derived
|
||||||
|
* from it (the same value `Shade.getFingerprintFor(address)` returns).
|
||||||
|
* Both are stored: the public key is what `verifyProxyApproval` uses
|
||||||
|
* to check signatures; the fingerprint is what UIs show users for
|
||||||
|
* out-of-band verification.
|
||||||
|
*/
|
||||||
|
export interface ProfileClientEntry {
|
||||||
|
/** Shade address (`device:...`). */
|
||||||
|
address: string;
|
||||||
|
/**
|
||||||
|
* 32-byte Ed25519 long-term identity public key, lowercase hex
|
||||||
|
* (64 chars). This is the key that anchors the client's
|
||||||
|
* prekey-bundle and that proxy-approvals are signed with.
|
||||||
|
*/
|
||||||
|
identityPublicKey: string;
|
||||||
|
/**
|
||||||
|
* Safety-number-style fingerprint of the identity key, exactly the
|
||||||
|
* format `computeFingerprint` returns. Stored for fast denormalized
|
||||||
|
* lookups and so UIs don't have to recompute it.
|
||||||
|
*/
|
||||||
|
identityFingerprint: string;
|
||||||
|
/** Human-friendly name (e.g. "iPhone 15"). */
|
||||||
|
name: string;
|
||||||
|
/** Open enum: `'mobile' | 'tablet' | 'browser' | ...`. */
|
||||||
|
kind: string;
|
||||||
|
/** Wall-clock ms when this client was added to the profile. */
|
||||||
|
addedAt: number;
|
||||||
|
/**
|
||||||
|
* When true, this client is allowed to proxy-approve `linkRequest`s
|
||||||
|
* that arrive at any host. Toggled via the workstation's
|
||||||
|
* Settings → Devices UI. Hosts MUST verify against the freshest
|
||||||
|
* profile blob before honoring an approval (to close the
|
||||||
|
* revocation TOCTOU window — see FR §5).
|
||||||
|
*/
|
||||||
|
trustedApprover?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CanonicalProfileBlob {
|
||||||
|
/** Schema version. Bump when the shape changes incompatibly. */
|
||||||
|
version: 1;
|
||||||
|
hosts: ProfileHostEntry[];
|
||||||
|
clients: ProfileClientEntry[];
|
||||||
|
/**
|
||||||
|
* Denormalized list of `clients[].identityFingerprint` values where
|
||||||
|
* `trustedApprover === true`. Hosts use this for the fast-path
|
||||||
|
* "is X allowed to approve?" check; the authoritative source is
|
||||||
|
* still the per-client `trustedApprover` flag.
|
||||||
|
*/
|
||||||
|
trustedApproverFingerprints: string[];
|
||||||
|
/** Wall-clock ms of the last write that produced this blob. */
|
||||||
|
updatedAt: number;
|
||||||
|
/**
|
||||||
|
* Optional hex-encoded pubkey that wrote this blob. Mirrors the
|
||||||
|
* profile-storage owner-pubkey but kept in-band so apps that need
|
||||||
|
* to display "last edited by X" don't have to round-trip the
|
||||||
|
* relay. Not used for verification — the relay's TOFU on
|
||||||
|
* owner-pubkey is the authoritative auth boundary.
|
||||||
|
*/
|
||||||
|
signedBy?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const TEXT = new TextEncoder();
|
||||||
|
const TEXT_DECODER = new TextDecoder();
|
||||||
|
|
||||||
|
/** Build a fresh empty profile blob (timestamp = `now ?? Date.now()`). */
|
||||||
|
export function emptyCanonicalProfile(now?: number): CanonicalProfileBlob {
|
||||||
|
return {
|
||||||
|
version: 1,
|
||||||
|
hosts: [],
|
||||||
|
clients: [],
|
||||||
|
trustedApproverFingerprints: [],
|
||||||
|
updatedAt: now ?? Date.now(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decode a profile-blob plaintext (the `plaintext` of a
|
||||||
|
* `ProfileGetResult`) into the canonical shape. Throws
|
||||||
|
* `ValidationError` on malformed JSON or wrong shape.
|
||||||
|
*
|
||||||
|
* Forward-compatibility: unknown top-level fields are preserved so a
|
||||||
|
* device on an older version can round-trip a blob written by a
|
||||||
|
* newer device without losing data. Unknown fields inside `hosts[]` /
|
||||||
|
* `clients[]` entries are also preserved.
|
||||||
|
*/
|
||||||
|
export function parseCanonicalProfile(
|
||||||
|
plaintext: Uint8Array | string,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
const text =
|
||||||
|
typeof plaintext === 'string' ? plaintext : TEXT_DECODER.decode(plaintext);
|
||||||
|
let json: unknown;
|
||||||
|
try {
|
||||||
|
json = JSON.parse(text);
|
||||||
|
} catch (err) {
|
||||||
|
throw new ValidationError(
|
||||||
|
`profile blob is not valid JSON: ${(err as Error).message}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!json || typeof json !== 'object' || Array.isArray(json)) {
|
||||||
|
throw new ValidationError('profile blob must be a JSON object');
|
||||||
|
}
|
||||||
|
const obj = json as Record<string, unknown>;
|
||||||
|
if (obj.version !== 1) {
|
||||||
|
throw new ValidationError(
|
||||||
|
`unsupported profile blob version: ${String(obj.version)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const hosts = validateArray(obj.hosts, 'hosts', validateHostEntry);
|
||||||
|
const clients = validateArray(obj.clients, 'clients', validateClientEntry);
|
||||||
|
const tApp = obj.trustedApproverFingerprints;
|
||||||
|
const trustedApproverFingerprints = Array.isArray(tApp)
|
||||||
|
? tApp.map((v, i) => {
|
||||||
|
if (typeof v !== 'string') {
|
||||||
|
throw new ValidationError(
|
||||||
|
`trustedApproverFingerprints[${i}] must be a string`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
})
|
||||||
|
: [];
|
||||||
|
const updatedAt =
|
||||||
|
typeof obj.updatedAt === 'number' && Number.isFinite(obj.updatedAt)
|
||||||
|
? obj.updatedAt
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
const out: CanonicalProfileBlob = {
|
||||||
|
version: 1,
|
||||||
|
hosts,
|
||||||
|
clients,
|
||||||
|
trustedApproverFingerprints,
|
||||||
|
updatedAt,
|
||||||
|
};
|
||||||
|
if (typeof obj.signedBy === 'string') out.signedBy = obj.signedBy;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Serialize a profile blob to UTF-8 JSON ready for `Profile.put`. */
|
||||||
|
export function serializeCanonicalProfile(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
): Uint8Array {
|
||||||
|
return TEXT.encode(JSON.stringify(blob));
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateArray<T>(
|
||||||
|
v: unknown,
|
||||||
|
field: string,
|
||||||
|
validate: (entry: unknown, index: number, field: string) => T,
|
||||||
|
): T[] {
|
||||||
|
if (v === undefined) return [];
|
||||||
|
if (!Array.isArray(v)) {
|
||||||
|
throw new ValidationError(`${field} must be an array`);
|
||||||
|
}
|
||||||
|
return v.map((entry, i) => validate(entry, i, field));
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateHostEntry(
|
||||||
|
entry: unknown,
|
||||||
|
index: number,
|
||||||
|
field: string,
|
||||||
|
): ProfileHostEntry {
|
||||||
|
if (!entry || typeof entry !== 'object') {
|
||||||
|
throw new ValidationError(`${field}[${index}] must be an object`);
|
||||||
|
}
|
||||||
|
const e = entry as Record<string, unknown>;
|
||||||
|
return {
|
||||||
|
address: requireString(e.address, `${field}[${index}].address`),
|
||||||
|
name: requireString(e.name, `${field}[${index}].name`),
|
||||||
|
kind: requireString(e.kind, `${field}[${index}].kind`),
|
||||||
|
addedAt: requireNumber(e.addedAt, `${field}[${index}].addedAt`),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateClientEntry(
|
||||||
|
entry: unknown,
|
||||||
|
index: number,
|
||||||
|
field: string,
|
||||||
|
): ProfileClientEntry {
|
||||||
|
if (!entry || typeof entry !== 'object') {
|
||||||
|
throw new ValidationError(`${field}[${index}] must be an object`);
|
||||||
|
}
|
||||||
|
const e = entry as Record<string, unknown>;
|
||||||
|
const identityPublicKey = requireString(
|
||||||
|
e.identityPublicKey,
|
||||||
|
`${field}[${index}].identityPublicKey`,
|
||||||
|
);
|
||||||
|
if (!/^[0-9a-f]{64}$/.test(identityPublicKey)) {
|
||||||
|
throw new ValidationError(
|
||||||
|
`${field}[${index}].identityPublicKey must be 64 lowercase hex chars`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const out: ProfileClientEntry = {
|
||||||
|
address: requireString(e.address, `${field}[${index}].address`),
|
||||||
|
identityPublicKey,
|
||||||
|
identityFingerprint: requireString(
|
||||||
|
e.identityFingerprint,
|
||||||
|
`${field}[${index}].identityFingerprint`,
|
||||||
|
),
|
||||||
|
name: requireString(e.name, `${field}[${index}].name`),
|
||||||
|
kind: requireString(e.kind, `${field}[${index}].kind`),
|
||||||
|
addedAt: requireNumber(e.addedAt, `${field}[${index}].addedAt`),
|
||||||
|
};
|
||||||
|
if (typeof e.trustedApprover === 'boolean') {
|
||||||
|
out.trustedApprover = e.trustedApprover;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function requireString(v: unknown, field: string): string {
|
||||||
|
if (typeof v !== 'string') {
|
||||||
|
throw new ValidationError(`${field} must be a string`);
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
function requireNumber(v: unknown, field: string): number {
|
||||||
|
if (typeof v !== 'number' || !Number.isFinite(v)) {
|
||||||
|
throw new ValidationError(`${field} must be a finite number`);
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Mutators (immutable; return new blob, never mutate input) ──
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert or replace a host entry by address. Any existing host with
|
||||||
|
* the same address is overwritten. The output's `updatedAt` is set to
|
||||||
|
* `now ?? Date.now()` so callers don't have to remember to bump it.
|
||||||
|
*/
|
||||||
|
export function upsertHost(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
host: ProfileHostEntry,
|
||||||
|
now?: number,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
const hosts = blob.hosts.filter((h) => h.address !== host.address);
|
||||||
|
hosts.push(host);
|
||||||
|
return { ...blob, hosts, updatedAt: now ?? Date.now() };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove the host with the given address, if any. */
|
||||||
|
export function removeHost(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
address: string,
|
||||||
|
now?: number,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
const hosts = blob.hosts.filter((h) => h.address !== address);
|
||||||
|
if (hosts.length === blob.hosts.length) return blob;
|
||||||
|
return { ...blob, hosts, updatedAt: now ?? Date.now() };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert or replace a client entry by `identityFingerprint` (the
|
||||||
|
* stable cryptographic identifier). Address can change without
|
||||||
|
* losing the trust record — e.g. if a phone re-pairs to a new device
|
||||||
|
* row but keeps its identity key — but a new `identityFingerprint`
|
||||||
|
* is treated as a new client.
|
||||||
|
*
|
||||||
|
* Re-derives `trustedApproverFingerprints` from the resulting
|
||||||
|
* `clients[]` so the denormalized list never drifts.
|
||||||
|
*/
|
||||||
|
export function upsertClient(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
client: ProfileClientEntry,
|
||||||
|
now?: number,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
const clients = blob.clients.filter(
|
||||||
|
(c) => c.identityFingerprint !== client.identityFingerprint,
|
||||||
|
);
|
||||||
|
clients.push(client);
|
||||||
|
return {
|
||||||
|
...blob,
|
||||||
|
clients,
|
||||||
|
trustedApproverFingerprints: deriveTrustedApprovers(clients),
|
||||||
|
updatedAt: now ?? Date.now(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove the client with the given `identityFingerprint`, if any. */
|
||||||
|
export function removeClient(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
identityFingerprint: string,
|
||||||
|
now?: number,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
const clients = blob.clients.filter(
|
||||||
|
(c) => c.identityFingerprint !== identityFingerprint,
|
||||||
|
);
|
||||||
|
if (clients.length === blob.clients.length) return blob;
|
||||||
|
return {
|
||||||
|
...blob,
|
||||||
|
clients,
|
||||||
|
trustedApproverFingerprints: deriveTrustedApprovers(clients),
|
||||||
|
updatedAt: now ?? Date.now(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle the `trustedApprover` flag on a client by fingerprint.
|
||||||
|
* Returns the input unchanged if the fingerprint isn't found.
|
||||||
|
*/
|
||||||
|
export function setTrustedApprover(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
identityFingerprint: string,
|
||||||
|
trusted: boolean,
|
||||||
|
now?: number,
|
||||||
|
): CanonicalProfileBlob {
|
||||||
|
let touched = false;
|
||||||
|
const clients = blob.clients.map((c) => {
|
||||||
|
if (c.identityFingerprint !== identityFingerprint) return c;
|
||||||
|
if ((c.trustedApprover ?? false) === trusted) return c;
|
||||||
|
touched = true;
|
||||||
|
const next: ProfileClientEntry = { ...c };
|
||||||
|
if (trusted) next.trustedApprover = true;
|
||||||
|
else delete next.trustedApprover;
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
if (!touched) return blob;
|
||||||
|
return {
|
||||||
|
...blob,
|
||||||
|
clients,
|
||||||
|
trustedApproverFingerprints: deriveTrustedApprovers(clients),
|
||||||
|
updatedAt: now ?? Date.now(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* True if the given fingerprint resolves to a client whose
|
||||||
|
* `trustedApprover` flag is set. Cross-checks both `clients[]` and
|
||||||
|
* the denormalized `trustedApproverFingerprints[]` — both must agree
|
||||||
|
* to count as trusted.
|
||||||
|
*/
|
||||||
|
export function isTrustedApprover(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
identityFingerprint: string,
|
||||||
|
): boolean {
|
||||||
|
if (!blob.trustedApproverFingerprints.includes(identityFingerprint)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const c = findClientByFingerprint(blob, identityFingerprint);
|
||||||
|
return c?.trustedApprover === true;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findClientByFingerprint(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
identityFingerprint: string,
|
||||||
|
): ProfileClientEntry | null {
|
||||||
|
return (
|
||||||
|
blob.clients.find((c) => c.identityFingerprint === identityFingerprint) ??
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findClientByAddress(
|
||||||
|
blob: CanonicalProfileBlob,
|
||||||
|
address: string,
|
||||||
|
): ProfileClientEntry | null {
|
||||||
|
return blob.clients.find((c) => c.address === address) ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function deriveTrustedApprovers(clients: ProfileClientEntry[]): string[] {
|
||||||
|
return clients
|
||||||
|
.filter((c) => c.trustedApprover === true)
|
||||||
|
.map((c) => c.identityFingerprint);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Approval frames ───────────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default domain separator for the proxy-approval signing payload.
|
||||||
|
* Apps with their own canonical name (e.g. Prism) MAY override this
|
||||||
|
* via the `domain` option, but they must use the SAME value on both
|
||||||
|
* the signing and verifying side. The frame itself carries the
|
||||||
|
* domain so a verifier can detect mismatch and reject.
|
||||||
|
*/
|
||||||
|
export const DEFAULT_APPROVAL_DOMAIN = 'shade-link-approve-v1';
|
||||||
|
|
||||||
|
/** ms-since-epoch defaults: build = 5 minutes from now. */
|
||||||
|
const DEFAULT_EXPIRES_IN_MS = 5 * 60 * 1000;
|
||||||
|
|
||||||
|
export interface ApprovalRequestingDevice {
|
||||||
|
/** Safety-number fingerprint of the requesting device's identity. */
|
||||||
|
fingerprint: string;
|
||||||
|
/** Optional human label (e.g. `"cafe-laptop"`). */
|
||||||
|
deviceName?: string;
|
||||||
|
/** Optional `User-Agent`-like hint for display in the approve modal. */
|
||||||
|
userAgent?: string;
|
||||||
|
/** Optional best-effort source IP for display (NOT authenticated). */
|
||||||
|
ipHint?: string;
|
||||||
|
/** Wall-clock ms when the host received the original linkRequest. */
|
||||||
|
receivedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ApprovalRequestFrame {
|
||||||
|
kind: 'approvalNeeded';
|
||||||
|
/**
|
||||||
|
* 128-bit random hex (32 chars) — host-generated, used as the
|
||||||
|
* idempotency key for the approval. The verifier matches the
|
||||||
|
* `linkApproveByProxy.requestId` against this exact value.
|
||||||
|
*/
|
||||||
|
requestId: string;
|
||||||
|
/** Shade address of the host that received the original linkRequest. */
|
||||||
|
hostAddress: string;
|
||||||
|
/** Identity fingerprint of the host (the same value the approver UI shows). */
|
||||||
|
hostFingerprint: string;
|
||||||
|
requestingDevice: ApprovalRequestingDevice;
|
||||||
|
/**
|
||||||
|
* Wall-clock ms after which the host won't accept a proxy-approval
|
||||||
|
* for this `requestId`. The approver SHOULD also reject locally if
|
||||||
|
* the user takes too long to respond.
|
||||||
|
*/
|
||||||
|
expiresAt: number;
|
||||||
|
/** Domain separator the host expects this approval to be signed under. */
|
||||||
|
domain: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProxyApprovalFrame {
|
||||||
|
kind: 'linkApproveByProxy';
|
||||||
|
/** Echoed from the matching `ApprovalRequestFrame.requestId`. */
|
||||||
|
requestId: string;
|
||||||
|
/** `'approve'` or `'reject'`. The verifier checks against this exactly. */
|
||||||
|
decision: 'approve' | 'reject';
|
||||||
|
/**
|
||||||
|
* Identity fingerprint of the approving client. MUST match an entry
|
||||||
|
* in the host's profile-blob `clients[]` whose `trustedApprover`
|
||||||
|
* flag is set.
|
||||||
|
*/
|
||||||
|
approverFingerprint: string;
|
||||||
|
/**
|
||||||
|
* Ed25519 signature over the canonical signing payload, lowercase
|
||||||
|
* hex (128 chars). See `canonicalApprovalSigningBytes` for the
|
||||||
|
* exact byte layout.
|
||||||
|
*/
|
||||||
|
signature: string;
|
||||||
|
/** Domain separator used to produce the signature. */
|
||||||
|
domain: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BuildApprovalRequestOptions {
|
||||||
|
hostAddress: string;
|
||||||
|
hostFingerprint: string;
|
||||||
|
requestingDevice: Omit<ApprovalRequestingDevice, 'receivedAt'> & {
|
||||||
|
receivedAt?: number;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* ms TTL after which the host won't honor a proxy-approval for
|
||||||
|
* this request. Default 5 minutes — long enough for the user to
|
||||||
|
* fish their phone out, short enough to bound replay.
|
||||||
|
*/
|
||||||
|
expiresInMs?: number;
|
||||||
|
/**
|
||||||
|
* Source of randomness for the `requestId`. Pass the same
|
||||||
|
* `CryptoProvider` you use elsewhere in the SDK (typically a
|
||||||
|
* `SubtleCryptoProvider`) — it satisfies the `randomBytes` shape.
|
||||||
|
*/
|
||||||
|
crypto: { randomBytes(n: number): Uint8Array };
|
||||||
|
/** Domain separator. Default: `shade-link-approve-v1`. */
|
||||||
|
domain?: string;
|
||||||
|
/** Override `Date.now()` (tests). */
|
||||||
|
now?: () => number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a fresh `approvalNeeded` frame with a 128-bit random
|
||||||
|
* `requestId`. The host then ships this to each trusted-approver
|
||||||
|
* client via `Shade.send`. Hosts SHOULD persist `(requestId, expiresAt,
|
||||||
|
* requestingDevice.fingerprint)` somewhere durable so they can match
|
||||||
|
* up the eventual `linkApproveByProxy` reply.
|
||||||
|
*/
|
||||||
|
export function buildApprovalRequest(
|
||||||
|
options: BuildApprovalRequestOptions,
|
||||||
|
): ApprovalRequestFrame {
|
||||||
|
const now = (options.now ?? Date.now)();
|
||||||
|
const requestId = bytesToHex(options.crypto.randomBytes(16));
|
||||||
|
return {
|
||||||
|
kind: 'approvalNeeded',
|
||||||
|
requestId,
|
||||||
|
hostAddress: options.hostAddress,
|
||||||
|
hostFingerprint: options.hostFingerprint,
|
||||||
|
requestingDevice: {
|
||||||
|
fingerprint: options.requestingDevice.fingerprint,
|
||||||
|
...(options.requestingDevice.deviceName !== undefined
|
||||||
|
? { deviceName: options.requestingDevice.deviceName }
|
||||||
|
: {}),
|
||||||
|
...(options.requestingDevice.userAgent !== undefined
|
||||||
|
? { userAgent: options.requestingDevice.userAgent }
|
||||||
|
: {}),
|
||||||
|
...(options.requestingDevice.ipHint !== undefined
|
||||||
|
? { ipHint: options.requestingDevice.ipHint }
|
||||||
|
: {}),
|
||||||
|
receivedAt: options.requestingDevice.receivedAt ?? now,
|
||||||
|
},
|
||||||
|
expiresAt: now + (options.expiresInMs ?? DEFAULT_EXPIRES_IN_MS),
|
||||||
|
domain: options.domain ?? DEFAULT_APPROVAL_DOMAIN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SignProxyApprovalOptions {
|
||||||
|
/** The frame the host sent. The verifier rebuilds the signing payload from it. */
|
||||||
|
request: ApprovalRequestFrame;
|
||||||
|
decision: 'approve' | 'reject';
|
||||||
|
/**
|
||||||
|
* The approving client's identity fingerprint. Must match the
|
||||||
|
* `clients[]` entry the host expects to find as a trusted approver.
|
||||||
|
*/
|
||||||
|
approverFingerprint: string;
|
||||||
|
/**
|
||||||
|
* 32-byte Ed25519 *seed* — `crypto.sign(seed, msg)` works directly
|
||||||
|
* (the noble convention `@shade/crypto-web` uses).
|
||||||
|
*/
|
||||||
|
approverSigningKey: Uint8Array;
|
||||||
|
/** CryptoProvider for `sign`. */
|
||||||
|
crypto: Pick<CryptoProvider, 'sign'>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a `linkApproveByProxy` frame signed with the approver's
|
||||||
|
* long-term Ed25519 identity key. The signing payload is
|
||||||
|
* domain-separated and binds together every field that protects
|
||||||
|
* against cross-frame replay (see `canonicalApprovalSigningBytes`).
|
||||||
|
*/
|
||||||
|
export async function signProxyApproval(
|
||||||
|
options: SignProxyApprovalOptions,
|
||||||
|
): Promise<ProxyApprovalFrame> {
|
||||||
|
if (options.approverSigningKey.length !== 32) {
|
||||||
|
throw new ValidationError('approverSigningKey must be 32 bytes (Ed25519 seed)');
|
||||||
|
}
|
||||||
|
if (options.decision !== 'approve' && options.decision !== 'reject') {
|
||||||
|
throw new ValidationError(`decision must be 'approve' or 'reject'`);
|
||||||
|
}
|
||||||
|
const payload = canonicalApprovalSigningBytes({
|
||||||
|
domain: options.request.domain,
|
||||||
|
requestId: options.request.requestId,
|
||||||
|
hostFingerprint: options.request.hostFingerprint,
|
||||||
|
requestingDeviceFingerprint: options.request.requestingDevice.fingerprint,
|
||||||
|
decision: options.decision,
|
||||||
|
});
|
||||||
|
const sig = await options.crypto.sign(options.approverSigningKey, payload);
|
||||||
|
return {
|
||||||
|
kind: 'linkApproveByProxy',
|
||||||
|
requestId: options.request.requestId,
|
||||||
|
decision: options.decision,
|
||||||
|
approverFingerprint: options.approverFingerprint,
|
||||||
|
signature: bytesToHex(sig),
|
||||||
|
domain: options.request.domain,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface VerifyProxyApprovalOptions {
|
||||||
|
/** The original `approvalNeeded` frame the host sent (replay-binding). */
|
||||||
|
request: ApprovalRequestFrame;
|
||||||
|
/** The `linkApproveByProxy` frame received from the approver. */
|
||||||
|
approval: ProxyApprovalFrame;
|
||||||
|
/**
|
||||||
|
* Profile blob to verify against. The approver must resolve to a
|
||||||
|
* `clients[]` entry whose `trustedApprover` flag is set.
|
||||||
|
* Hosts MUST refetch the blob fresh before verifying — see FR §5
|
||||||
|
* for the revocation TOCTOU rationale.
|
||||||
|
*/
|
||||||
|
profile: CanonicalProfileBlob;
|
||||||
|
/** CryptoProvider for `verify`. */
|
||||||
|
crypto: Pick<CryptoProvider, 'verify'>;
|
||||||
|
/** Override `Date.now()` (tests). */
|
||||||
|
now?: () => number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type VerifyProxyApprovalReason =
|
||||||
|
| 'request-id-mismatch'
|
||||||
|
| 'domain-mismatch'
|
||||||
|
| 'unknown-approver'
|
||||||
|
| 'not-trusted'
|
||||||
|
| 'bad-signature'
|
||||||
|
| 'expired';
|
||||||
|
|
||||||
|
export type VerifyProxyApprovalResult =
|
||||||
|
| { ok: true; approver: ProfileClientEntry }
|
||||||
|
| { ok: false; reason: VerifyProxyApprovalReason };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify a `linkApproveByProxy` against the originating
|
||||||
|
* `approvalNeeded` and the host's freshest profile blob. Returns a
|
||||||
|
* tagged result rather than throwing — callers usually want to log
|
||||||
|
* the reason before deciding what to surface to the user.
|
||||||
|
*
|
||||||
|
* Order of checks:
|
||||||
|
*
|
||||||
|
* 1. `requestId` must match exactly. Defends against an attacker
|
||||||
|
* replaying a stale approval against a fresh request.
|
||||||
|
* 2. `domain` must match exactly. Defends against an approval signed
|
||||||
|
* under one app's separator being honored by another.
|
||||||
|
* 3. Approver must resolve to a `clients[]` entry. Identity unknown =
|
||||||
|
* reject regardless of signature validity.
|
||||||
|
* 4. Approver's `trustedApprover` flag must be set AND its
|
||||||
|
* fingerprint must be in `trustedApproverFingerprints[]` (cross-
|
||||||
|
* check via `isTrustedApprover`).
|
||||||
|
* 5. Signature must verify against the approver's `identityPublicKey`.
|
||||||
|
* 6. `expiresAt` must be in the future (gives the host a lower
|
||||||
|
* bound; the host's own pending-state is the authoritative source).
|
||||||
|
*/
|
||||||
|
export async function verifyProxyApproval(
|
||||||
|
options: VerifyProxyApprovalOptions,
|
||||||
|
): Promise<VerifyProxyApprovalResult> {
|
||||||
|
const { request, approval, profile } = options;
|
||||||
|
|
||||||
|
if (approval.requestId !== request.requestId) {
|
||||||
|
return { ok: false, reason: 'request-id-mismatch' };
|
||||||
|
}
|
||||||
|
if (approval.domain !== request.domain) {
|
||||||
|
return { ok: false, reason: 'domain-mismatch' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const approver = findClientByFingerprint(profile, approval.approverFingerprint);
|
||||||
|
if (!approver) {
|
||||||
|
return { ok: false, reason: 'unknown-approver' };
|
||||||
|
}
|
||||||
|
if (!isTrustedApprover(profile, approval.approverFingerprint)) {
|
||||||
|
return { ok: false, reason: 'not-trusted' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = (options.now ?? Date.now)();
|
||||||
|
if (now > request.expiresAt) {
|
||||||
|
return { ok: false, reason: 'expired' };
|
||||||
|
}
|
||||||
|
|
||||||
|
let pubkey: Uint8Array;
|
||||||
|
let sig: Uint8Array;
|
||||||
|
try {
|
||||||
|
pubkey = hexToBytes(approver.identityPublicKey);
|
||||||
|
sig = hexToBytes(approval.signature);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, reason: 'bad-signature' };
|
||||||
|
}
|
||||||
|
if (pubkey.length !== 32 || sig.length !== 64) {
|
||||||
|
return { ok: false, reason: 'bad-signature' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = canonicalApprovalSigningBytes({
|
||||||
|
domain: approval.domain,
|
||||||
|
requestId: approval.requestId,
|
||||||
|
hostFingerprint: request.hostFingerprint,
|
||||||
|
requestingDeviceFingerprint: request.requestingDevice.fingerprint,
|
||||||
|
decision: approval.decision,
|
||||||
|
});
|
||||||
|
const valid = await options.crypto.verify(pubkey, payload, sig);
|
||||||
|
if (!valid) {
|
||||||
|
return { ok: false, reason: 'bad-signature' };
|
||||||
|
}
|
||||||
|
return { ok: true, approver };
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Canonical signing payload ─────────────────────────────────
|
||||||
|
|
||||||
|
export interface ApprovalSigningInput {
|
||||||
|
domain: string;
|
||||||
|
requestId: string;
|
||||||
|
hostFingerprint: string;
|
||||||
|
requestingDeviceFingerprint: string;
|
||||||
|
decision: 'approve' | 'reject';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the exact bytes that get Ed25519-signed for a proxy-approval.
|
||||||
|
*
|
||||||
|
* Format (length-prefixed UTF-8, big-endian u16 lengths):
|
||||||
|
*
|
||||||
|
* u16(len(domain)) || domain
|
||||||
|
* u16(len(requestId)) || requestId
|
||||||
|
* u16(len(hostFp)) || hostFingerprint
|
||||||
|
* u16(len(requestFp)) || requestingDeviceFingerprint
|
||||||
|
* u16(len(decision)) || decision
|
||||||
|
*
|
||||||
|
* Length-prefixed rather than delimiter-joined so the encoding is
|
||||||
|
* unambiguous regardless of what bytes appear in any field. u16
|
||||||
|
* (max 65535) is plenty: domain < 256 chars by convention,
|
||||||
|
* fingerprint ≈ 71 chars, requestId 32 hex chars, decision 6-7 chars.
|
||||||
|
*
|
||||||
|
* Exposed publicly so other Shade implementations (Android Kotlin,
|
||||||
|
* iOS Swift, etc.) can produce byte-identical signing input from
|
||||||
|
* test vectors without depending on this TypeScript code.
|
||||||
|
*/
|
||||||
|
export function canonicalApprovalSigningBytes(
|
||||||
|
input: ApprovalSigningInput,
|
||||||
|
): Uint8Array {
|
||||||
|
const fields: Uint8Array[] = [
|
||||||
|
TEXT.encode(input.domain),
|
||||||
|
TEXT.encode(input.requestId),
|
||||||
|
TEXT.encode(input.hostFingerprint),
|
||||||
|
TEXT.encode(input.requestingDeviceFingerprint),
|
||||||
|
TEXT.encode(input.decision),
|
||||||
|
];
|
||||||
|
for (const f of fields) {
|
||||||
|
if (f.length > 0xffff) {
|
||||||
|
throw new ValidationError(
|
||||||
|
`signing field too long: ${f.length} bytes (max 65535)`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const total = fields.reduce((sum, f) => sum + 2 + f.length, 0);
|
||||||
|
const out = new Uint8Array(total);
|
||||||
|
let offset = 0;
|
||||||
|
for (const f of fields) {
|
||||||
|
out[offset] = (f.length >> 8) & 0xff;
|
||||||
|
out[offset + 1] = f.length & 0xff;
|
||||||
|
offset += 2;
|
||||||
|
out.set(f, offset);
|
||||||
|
offset += f.length;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Hex helpers (kept local so this module has no extra deps) ──
|
||||||
|
|
||||||
|
function bytesToHex(bytes: Uint8Array): string {
|
||||||
|
let s = '';
|
||||||
|
for (let i = 0; i < bytes.length; i++) s += bytes[i]!.toString(16).padStart(2, '0');
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
function hexToBytes(hex: string): Uint8Array {
|
||||||
|
if (hex.length % 2 !== 0) throw new ValidationError('hex length must be even');
|
||||||
|
if (!/^[0-9a-f]*$/.test(hex)) {
|
||||||
|
throw new ValidationError('hex must be lowercase 0-9a-f');
|
||||||
|
}
|
||||||
|
const out = new Uint8Array(hex.length / 2);
|
||||||
|
for (let i = 0; i < out.length; i++) {
|
||||||
|
out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
BIN
packages/shade-sdk/src/broadcast.ts
Normal file
BIN
packages/shade-sdk/src/broadcast.ts
Normal file
Binary file not shown.
@@ -17,11 +17,12 @@ export interface ShadeConfig {
|
|||||||
* - "memory" — in-memory only (lost on restart, good for tests)
|
* - "memory" — in-memory only (lost on restart, good for tests)
|
||||||
* - "sqlite:/path/to/file.db" — SQLite backend
|
* - "sqlite:/path/to/file.db" — SQLite backend
|
||||||
* - { type: 'postgres', url: 'postgres://...' } — PostgreSQL backend
|
* - { type: 'postgres', url: 'postgres://...' } — PostgreSQL backend
|
||||||
|
* - { type: 'indexeddb', dbName?: 'my-app' } — browser IndexedDB
|
||||||
* - An explicit StorageProvider instance
|
* - An explicit StorageProvider instance
|
||||||
*
|
*
|
||||||
* Default: "memory"
|
* Default: "memory"
|
||||||
*/
|
*/
|
||||||
storage?: string | StorageProvider | { type: 'postgres'; url: string };
|
storage?: StorageSpec;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Your address on the prekey server (e.g. "alice@example.com" or "device:abc123").
|
* Your address on the prekey server (e.g. "alice@example.com" or "device:abc123").
|
||||||
@@ -96,9 +97,16 @@ export interface ShadeKTConfig {
|
|||||||
witnessMaxStored?: number;
|
witnessMaxStored?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Acceptable shapes for `ShadeConfig.storage`. */
|
||||||
|
export type StorageSpec =
|
||||||
|
| string
|
||||||
|
| StorageProvider
|
||||||
|
| { type: 'postgres'; url: string }
|
||||||
|
| { type: 'indexeddb'; dbName?: string };
|
||||||
|
|
||||||
export interface ResolvedConfig {
|
export interface ResolvedConfig {
|
||||||
prekeyServer: string;
|
prekeyServer: string;
|
||||||
storage: string | StorageProvider | { type: 'postgres'; url: string };
|
storage: StorageSpec;
|
||||||
address?: string | undefined;
|
address?: string | undefined;
|
||||||
autoReplenish: { min: number; target: number; intervalMs: number } | false;
|
autoReplenish: { min: number; target: number; intervalMs: number } | false;
|
||||||
autoRotate: false | '1d' | '7d' | '30d' | '90d';
|
autoRotate: false | '1d' | '7d' | '30d' | '90d';
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
export { createShade } from './create-shade.js';
|
export { createShade } from './create-shade.js';
|
||||||
export { Shade } from './shade.js';
|
export { Shade, ShadeStream } from './shade.js';
|
||||||
export type {
|
export type {
|
||||||
ShadeUploadOptions,
|
ShadeUploadOptions,
|
||||||
ShadeWebRtcConfig,
|
ShadeWebRtcConfig,
|
||||||
ShadeWebRtcRuntime,
|
ShadeWebRtcRuntime,
|
||||||
} from './shade.js';
|
} from './shade.js';
|
||||||
|
export type {
|
||||||
|
BroadcastChannel,
|
||||||
|
BroadcastChannelSummary,
|
||||||
|
MessageMeta,
|
||||||
|
} from './broadcast.js';
|
||||||
export { generateThumbnail } from './thumbnail.js';
|
export { generateThumbnail } from './thumbnail.js';
|
||||||
export type { GeneratedThumbnail, ThumbnailGenerationOptions } from './thumbnail.js';
|
export type { GeneratedThumbnail, ThumbnailGenerationOptions } from './thumbnail.js';
|
||||||
export { ShadeThumbnailCache } from './thumbnail-cache.js';
|
export { ShadeThumbnailCache } from './thumbnail-cache.js';
|
||||||
@@ -92,6 +97,58 @@ export {
|
|||||||
mainStreamIdForThumbnail,
|
mainStreamIdForThumbnail,
|
||||||
} from '@shade/streams';
|
} from '@shade/streams';
|
||||||
|
|
||||||
|
// ─── V4.9 — relay-side encrypted profile storage ──────────
|
||||||
|
export {
|
||||||
|
createProfileNamespace,
|
||||||
|
profilePlaintextToString,
|
||||||
|
deriveBlobSlotId,
|
||||||
|
deriveBlobKey,
|
||||||
|
deriveBlobSigningSeed,
|
||||||
|
ed25519PublicKeyFromSeed,
|
||||||
|
slotIdToHex,
|
||||||
|
} from './profile.js';
|
||||||
|
export type {
|
||||||
|
ProfileNamespace,
|
||||||
|
ProfileNamespaceOptions,
|
||||||
|
ProfileGetResult,
|
||||||
|
ProfilePutOptions,
|
||||||
|
ProfilePutResult,
|
||||||
|
} from './profile.js';
|
||||||
|
|
||||||
|
// ─── V4.10 — cross-host approval routing ──────────────────
|
||||||
|
export {
|
||||||
|
emptyCanonicalProfile,
|
||||||
|
parseCanonicalProfile,
|
||||||
|
serializeCanonicalProfile,
|
||||||
|
upsertHost,
|
||||||
|
removeHost,
|
||||||
|
upsertClient,
|
||||||
|
removeClient,
|
||||||
|
setTrustedApprover,
|
||||||
|
isTrustedApprover,
|
||||||
|
findClientByFingerprint,
|
||||||
|
findClientByAddress,
|
||||||
|
buildApprovalRequest,
|
||||||
|
signProxyApproval,
|
||||||
|
verifyProxyApproval,
|
||||||
|
canonicalApprovalSigningBytes,
|
||||||
|
DEFAULT_APPROVAL_DOMAIN,
|
||||||
|
} from './approval.js';
|
||||||
|
export type {
|
||||||
|
CanonicalProfileBlob,
|
||||||
|
ProfileHostEntry,
|
||||||
|
ProfileClientEntry,
|
||||||
|
ApprovalRequestingDevice,
|
||||||
|
ApprovalRequestFrame,
|
||||||
|
ProxyApprovalFrame,
|
||||||
|
BuildApprovalRequestOptions,
|
||||||
|
SignProxyApprovalOptions,
|
||||||
|
VerifyProxyApprovalOptions,
|
||||||
|
VerifyProxyApprovalReason,
|
||||||
|
VerifyProxyApprovalResult,
|
||||||
|
ApprovalSigningInput,
|
||||||
|
} from './approval.js';
|
||||||
|
|
||||||
// ─── Web Workers crypto (V3.8) ─────────────────────────────
|
// ─── Web Workers crypto (V3.8) ─────────────────────────────
|
||||||
export {
|
export {
|
||||||
createWorkerCryptoProvider,
|
createWorkerCryptoProvider,
|
||||||
|
|||||||
210
packages/shade-sdk/src/profile.ts
Normal file
210
packages/shade-sdk/src/profile.ts
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
import type { CryptoProvider } from '@shade/core';
|
||||||
|
import { ValidationError } from '@shade/core';
|
||||||
|
import {
|
||||||
|
deriveBlobSlotId,
|
||||||
|
deriveBlobKey,
|
||||||
|
deriveBlobSigningSeed,
|
||||||
|
aeadSeal,
|
||||||
|
aeadOpen,
|
||||||
|
} from '@shade/storage-encrypted/crypto';
|
||||||
|
import { ed25519PublicKeyFromSeed } from '@shade/crypto-web';
|
||||||
|
import { BlobClient, slotIdToHex } from '@shade/inbox';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V4.9 — relay-side encrypted profile storage.
|
||||||
|
*
|
||||||
|
* The `Profile` namespace lets a Shade-based app store a small,
|
||||||
|
* AEAD-sealed JSON blob on the relay keyed by a deterministic slotId
|
||||||
|
* derived from the user's master key. A brand new device that knows
|
||||||
|
* only the credentials (password + PIN → masterKey via the existing
|
||||||
|
* `@shade/storage-encrypted` KDF) can locate, decrypt, and update the
|
||||||
|
* blob. The relay sees only opaque slotIds and AEAD-sealed bytes — it
|
||||||
|
* never decrypts and cannot link slots to users.
|
||||||
|
*
|
||||||
|
* This is the *primitive* Prism uses for credential-driven device
|
||||||
|
* linking (Phase 2 of the Prism device-linking plan): the blob holds
|
||||||
|
* the list of paired hosts, the new device reads it, picks the first
|
||||||
|
* online host, and starts a link-request handshake. But it's
|
||||||
|
* deliberately app-shaped — any Shade app needing a credential-only
|
||||||
|
* bootstrap into existing E2EE state can use it. Pass a different
|
||||||
|
* `app` namespace string per use-case so two apps under the same
|
||||||
|
* master never collide on the same slot.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* const km = await KeyManager.unlock(...); // existing v4.5 flow
|
||||||
|
* const profile = createProfileNamespace({
|
||||||
|
* baseUrl: 'https://shade.example/',
|
||||||
|
* crypto: new SubtleCryptoProvider(),
|
||||||
|
* masterKey: km.masterKey,
|
||||||
|
* app: 'prism-profile-v1',
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* const current = await profile.get();
|
||||||
|
* // -> { plaintext: Uint8Array, etag: string } | null
|
||||||
|
*
|
||||||
|
* await profile.put(JSON.stringify({ hosts: [...] }), {
|
||||||
|
* ifMatch: current?.etag,
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* await profile.delete(); // "forget everything"
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface ProfileNamespaceOptions {
|
||||||
|
/** Base URL of the Shade relay. */
|
||||||
|
baseUrl: string;
|
||||||
|
/** CryptoProvider — typically a fresh SubtleCryptoProvider instance. */
|
||||||
|
crypto: CryptoProvider;
|
||||||
|
/**
|
||||||
|
* 32-byte master key, exactly the value you'd hand to
|
||||||
|
* `@shade/storage-encrypted`'s row-codec — the existing v4.5 KDF
|
||||||
|
* chain (passphrase + scrypt → masterKey, possibly upgraded with
|
||||||
|
* argon2id over a PIN) lands you here. Profile storage uses HKDF
|
||||||
|
* subderivations under separate `info` strings, so it can't leak
|
||||||
|
* the storage encryption key or vice versa.
|
||||||
|
*/
|
||||||
|
masterKey: Uint8Array;
|
||||||
|
/**
|
||||||
|
* Per-app namespace string. Distinct apps under the same master key
|
||||||
|
* MUST pass different values so they don't collide on the same slot.
|
||||||
|
* Convention: `"<app-id>-<purpose>-<schema-version>"`, e.g.
|
||||||
|
* `"prism-profile-v1"`.
|
||||||
|
*/
|
||||||
|
app: string;
|
||||||
|
/** Optional fetch override (defaults to globalThis.fetch). */
|
||||||
|
fetch?: typeof fetch;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProfileGetResult {
|
||||||
|
/** Decrypted plaintext bytes. The shape is up to the caller. */
|
||||||
|
plaintext: Uint8Array;
|
||||||
|
/** Pass back as `ifMatch` to do a CAS update. */
|
||||||
|
etag: string;
|
||||||
|
/** Wall-clock ms when the relay last accepted a write. */
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProfilePutOptions {
|
||||||
|
/**
|
||||||
|
* - `undefined` : create-only. Slot must be empty (else 409).
|
||||||
|
* - `<etag-string>` : compare-and-swap. Must match current etag (else 412).
|
||||||
|
* - `'*'` : unconditional overwrite. Slot must already exist (else 412).
|
||||||
|
*/
|
||||||
|
ifMatch?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProfilePutResult {
|
||||||
|
/** True if this PUT created the slot, false if it updated an existing one. */
|
||||||
|
created: boolean;
|
||||||
|
/** New etag after the write. */
|
||||||
|
etag: string;
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProfileNamespace {
|
||||||
|
readonly slotIdHex: string;
|
||||||
|
get(): Promise<ProfileGetResult | null>;
|
||||||
|
put(plaintext: Uint8Array | string, options?: ProfilePutOptions): Promise<ProfilePutResult>;
|
||||||
|
delete(): Promise<boolean>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const TEXT = new TextEncoder();
|
||||||
|
const TEXT_DECODER = new TextDecoder();
|
||||||
|
|
||||||
|
export function createProfileNamespace(
|
||||||
|
options: ProfileNamespaceOptions,
|
||||||
|
): ProfileNamespace {
|
||||||
|
if (options.masterKey.length !== 32) {
|
||||||
|
throw new ValidationError('masterKey must be 32 bytes');
|
||||||
|
}
|
||||||
|
if (options.app.length === 0) {
|
||||||
|
throw new ValidationError('app namespace must be non-empty');
|
||||||
|
}
|
||||||
|
|
||||||
|
const slotIdBytes = deriveBlobSlotId(options.masterKey, options.app);
|
||||||
|
const slotIdHex = slotIdToHex(slotIdBytes);
|
||||||
|
const blobKey = deriveBlobKey(options.masterKey, options.app);
|
||||||
|
const signingSeed = deriveBlobSigningSeed(options.masterKey, options.app);
|
||||||
|
const ownerPubkey = ed25519PublicKeyFromSeed(signingSeed);
|
||||||
|
|
||||||
|
// AAD binds the slotId into the AEAD seal: a relay returning the
|
||||||
|
// wrong slot's blob (mistake or malice) fails to open. The slotId is
|
||||||
|
// already part of the URL path, but binding it cryptographically
|
||||||
|
// prevents any kind of cross-slot replay regardless of how the bytes
|
||||||
|
// got to us.
|
||||||
|
const aad = TEXT.encode(`shade-profile-aad-v1:${slotIdHex}`);
|
||||||
|
|
||||||
|
const clientOptions: ConstructorParameters<typeof BlobClient>[0] = {
|
||||||
|
baseUrl: options.baseUrl,
|
||||||
|
crypto: options.crypto,
|
||||||
|
};
|
||||||
|
if (options.fetch) clientOptions.fetch = options.fetch;
|
||||||
|
const client = new BlobClient(clientOptions);
|
||||||
|
|
||||||
|
return {
|
||||||
|
slotIdHex,
|
||||||
|
|
||||||
|
async get(): Promise<ProfileGetResult | null> {
|
||||||
|
const result = await client.get(slotIdHex);
|
||||||
|
if (!result) return null;
|
||||||
|
// Deterministic 12-byte nonce from (slotId, etag): the relay
|
||||||
|
// stores `nonce || ct||tag` as one blob, so the AEAD layer
|
||||||
|
// pulls the nonce off the front. We don't pre-compute it —
|
||||||
|
// aeadOpen handles the prefix automatically.
|
||||||
|
const plaintext = await aeadOpen(blobKey, result.blob, aad);
|
||||||
|
return {
|
||||||
|
plaintext,
|
||||||
|
etag: result.etag,
|
||||||
|
updatedAt: result.updatedAt,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
async put(
|
||||||
|
plaintext: Uint8Array | string,
|
||||||
|
options?: ProfilePutOptions,
|
||||||
|
): Promise<ProfilePutResult> {
|
||||||
|
const ptBytes =
|
||||||
|
typeof plaintext === 'string' ? TEXT.encode(plaintext) : plaintext;
|
||||||
|
// Random per-write 12-byte nonce. We don't reuse a deterministic
|
||||||
|
// nonce because two consecutive writes of the same plaintext
|
||||||
|
// (rare but possible — re-uploading after a transient error)
|
||||||
|
// would otherwise reuse (key, nonce, plaintext), which is a
|
||||||
|
// nonce-reuse condition for AES-GCM. A fresh random nonce per
|
||||||
|
// PUT keeps each AEAD invocation unique.
|
||||||
|
const nonce = clientOptions.crypto.randomBytes(12);
|
||||||
|
const sealed = await aeadSeal(blobKey, nonce, ptBytes, aad);
|
||||||
|
|
||||||
|
const putArgs: Parameters<BlobClient['put']>[0] = {
|
||||||
|
slotIdHex,
|
||||||
|
blob: sealed,
|
||||||
|
signingPrivateKey: signingSeed,
|
||||||
|
ownerPubkey,
|
||||||
|
};
|
||||||
|
if (options?.ifMatch !== undefined) putArgs.ifMatch = options.ifMatch;
|
||||||
|
return client.put(putArgs);
|
||||||
|
},
|
||||||
|
|
||||||
|
async delete(): Promise<boolean> {
|
||||||
|
return client.delete({
|
||||||
|
slotIdHex,
|
||||||
|
signingPrivateKey: signingSeed,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-export the raw KDF helpers so apps that want to drive a custom
|
||||||
|
// flow (skip the AEAD layer, use a different client, run interop
|
||||||
|
// against a non-Shade relay) don't have to re-import from
|
||||||
|
// `@shade/storage-encrypted/crypto`.
|
||||||
|
export {
|
||||||
|
deriveBlobSlotId,
|
||||||
|
deriveBlobKey,
|
||||||
|
deriveBlobSigningSeed,
|
||||||
|
} from '@shade/storage-encrypted/crypto';
|
||||||
|
export { ed25519PublicKeyFromSeed } from '@shade/crypto-web';
|
||||||
|
export { slotIdToHex } from '@shade/inbox';
|
||||||
|
|
||||||
|
/** Decode a UTF-8 plaintext from a `ProfileGetResult`. */
|
||||||
|
export function profilePlaintextToString(result: ProfileGetResult): string {
|
||||||
|
return TEXT_DECODER.decode(result.plaintext);
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import type { ShadeEnvelope, StorageProvider } from '@shade/core';
|
import type { ShadeEnvelope, StorageProvider, RatchetMessage } from '@shade/core';
|
||||||
import {
|
import {
|
||||||
ShadeSessionManager,
|
ShadeSessionManager,
|
||||||
ShadeEventEmitter,
|
ShadeEventEmitter,
|
||||||
NoSessionError,
|
NoSessionError,
|
||||||
|
StreamRatchet,
|
||||||
|
StreamHandshakeError,
|
||||||
} from '@shade/core';
|
} from '@shade/core';
|
||||||
import {
|
import {
|
||||||
FingerprintGateRegistry,
|
FingerprintGateRegistry,
|
||||||
@@ -18,7 +20,16 @@ import {
|
|||||||
type CreateEncryptStreamOptions,
|
type CreateEncryptStreamOptions,
|
||||||
type CreateDecryptStreamOptions,
|
type CreateDecryptStreamOptions,
|
||||||
} from '@shade/crypto-web';
|
} from '@shade/crypto-web';
|
||||||
import { encodeEnvelope, decodeEnvelope, inspectEnvelopeType } from '@shade/proto';
|
import {
|
||||||
|
encodeEnvelope,
|
||||||
|
decodeEnvelope,
|
||||||
|
inspectEnvelopeType,
|
||||||
|
encodeStreamOpen,
|
||||||
|
encodeStreamOpenAck,
|
||||||
|
decodeStreamHandshake,
|
||||||
|
encodeStreamFrame,
|
||||||
|
decodeStreamFrame,
|
||||||
|
} from '@shade/proto';
|
||||||
import { ShadeFetchTransport, type KTVerifierOptions } from '@shade/transport';
|
import { ShadeFetchTransport, type KTVerifierOptions } from '@shade/transport';
|
||||||
import { LightWitness } from '@shade/key-transparency';
|
import { LightWitness } from '@shade/key-transparency';
|
||||||
import type { SignedTreeHead, STHWire } from '@shade/key-transparency';
|
import type { SignedTreeHead, STHWire } from '@shade/key-transparency';
|
||||||
@@ -44,7 +55,18 @@ import {
|
|||||||
backupFromString,
|
backupFromString,
|
||||||
} from './backup.js';
|
} from './backup.js';
|
||||||
import { computeFingerprint, deserializeIdentityKeyPair } from '@shade/core';
|
import { computeFingerprint, deserializeIdentityKeyPair } from '@shade/core';
|
||||||
import type { ResolvedConfig } from './config.js';
|
import {
|
||||||
|
acceptBroadcastEnvelope,
|
||||||
|
createBroadcastChannelImpl,
|
||||||
|
getBroadcastChannelImpl,
|
||||||
|
listBroadcastChannelsImpl,
|
||||||
|
maybeHandleControlPlaintext,
|
||||||
|
type BroadcastChannel,
|
||||||
|
type BroadcastChannelSummary,
|
||||||
|
type BroadcastSdkHooks,
|
||||||
|
type MessageMeta,
|
||||||
|
} from './broadcast.js';
|
||||||
|
import type { ResolvedConfig, StorageSpec } from './config.js';
|
||||||
import {
|
import {
|
||||||
ShadeControlChannel,
|
ShadeControlChannel,
|
||||||
ShadeTransferAuthenticator,
|
ShadeTransferAuthenticator,
|
||||||
@@ -142,10 +164,19 @@ export class Shade {
|
|||||||
private establishing = new Map<string, Promise<void>>();
|
private establishing = new Map<string, Promise<void>>();
|
||||||
// Per-address encrypt queue to serialize ratchet mutations
|
// Per-address encrypt queue to serialize ratchet mutations
|
||||||
private encryptChains = new Map<string, Promise<unknown>>();
|
private encryptChains = new Map<string, Promise<unknown>>();
|
||||||
|
// Per-`from` decrypt queue: serializes incoming receives so two concurrent
|
||||||
|
// shade.receive(from, env) calls can't race the ratchet/storage. Without
|
||||||
|
// this, parallel deliveries (relay duplicate fan-out, fast pipelined
|
||||||
|
// sends) hit `database is locked` (sqlite) or transaction conflicts (IDB)
|
||||||
|
// because the underlying StorageProvider isn't required to be a
|
||||||
|
// concurrent-safe writer. See V4.8.2 changelog.
|
||||||
|
private decryptChains = new Map<string, Promise<unknown>>();
|
||||||
|
|
||||||
// Message handlers — may be sync or async; receive() awaits each.
|
// Message handlers — may be sync or async; receive() awaits each. The
|
||||||
|
// optional third arg distinguishes direct vs broadcast plaintexts;
|
||||||
|
// handlers registered without it work unchanged (V4.6 back-compat).
|
||||||
private messageHandlers: Array<
|
private messageHandlers: Array<
|
||||||
(from: string, plaintext: string) => void | Promise<void>
|
(from: string, plaintext: string, meta?: MessageMeta) => void | Promise<void>
|
||||||
> = [];
|
> = [];
|
||||||
|
|
||||||
// Stream-transfer engine, lazily constructed on first use.
|
// Stream-transfer engine, lazily constructed on first use.
|
||||||
@@ -299,6 +330,26 @@ export class Shade {
|
|||||||
return this.address;
|
return this.address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The local device's Ed25519 identity public key (32 bytes).
|
||||||
|
*
|
||||||
|
* Stable for the lifetime of the identity. After {@link rotate} this
|
||||||
|
* reflects the new key; the previous key is preserved in retired-
|
||||||
|
* identities storage for the configured grace period.
|
||||||
|
*
|
||||||
|
* Hand this to your application's backend at enrollment time so it
|
||||||
|
* can verify signatures from this device, compute its own safety-
|
||||||
|
* number representation, or pin the key for later attestation. Use
|
||||||
|
* {@link fingerprint} instead for human side-channel comparison.
|
||||||
|
*/
|
||||||
|
get identityPublicKey(): Promise<Uint8Array> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
return this.storage.getIdentityKeyPair().then((kp) => {
|
||||||
|
if (!kp) throw new Error('Identity not yet generated');
|
||||||
|
return kp.signingPublicKey;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `@shade/files` namespace — high-level entry point for E2EE filesystem
|
* `@shade/files` namespace — high-level entry point for E2EE filesystem
|
||||||
* RPC. Lazily creates the underlying channel + streams bridges on first
|
* RPC. Lazily creates the underlying channel + streams bridges on first
|
||||||
@@ -394,13 +445,54 @@ export class Shade {
|
|||||||
* The caller provides the `from` address because the envelope itself
|
* The caller provides the `from` address because the envelope itself
|
||||||
* doesn't authenticate the sender — that's determined by your transport
|
* doesn't authenticate the sender — that's determined by your transport
|
||||||
* layer (auth header, WebSocket peer, push notification metadata, etc.).
|
* layer (auth header, WebSocket peer, push notification metadata, etc.).
|
||||||
|
*
|
||||||
|
* V4.6: when the decrypted plaintext is a broadcast control message
|
||||||
|
* (sender-key distribution / revocation), the SDK consumes it
|
||||||
|
* internally and returns an empty string; user handlers do NOT fire.
|
||||||
|
* Apps therefore see only direct plaintexts here. Broadcast payloads
|
||||||
|
* arrive via {@link Shade.acceptBroadcast}.
|
||||||
*/
|
*/
|
||||||
async receive(from: string, envelope: ShadeEnvelope): Promise<string> {
|
async receive(from: string, envelope: ShadeEnvelope): Promise<string> {
|
||||||
if (!this.initialized) throw new Error('Not initialized');
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
const plaintext = await this.manager.decrypt(from, envelope);
|
|
||||||
|
// Serialize ONLY the ratchet/storage write portion of receive (the
|
||||||
|
// call into `manager.decrypt`). Concurrent decrypts race the
|
||||||
|
// SessionManager ratchet (mutated in place) and the StorageProvider
|
||||||
|
// (not required to be a concurrent-safe writer — `bun:sqlite`
|
||||||
|
// throws `database is locked`, IDB throws transaction conflicts).
|
||||||
|
// The Prism FR called this out: a relay-duplicated WS fan-out
|
||||||
|
// dispatched 8 parallel `shade.receive(from, env)` calls, one won
|
||||||
|
// the X3DH prekey race and the other 7 failed with
|
||||||
|
// `database is locked` / `one-time prekey not found`. The fix is
|
||||||
|
// to queue per-`from` decrypts so the ratchet step is sequential.
|
||||||
|
//
|
||||||
|
// Crucially the user-visible MESSAGE HANDLERS run *outside* the
|
||||||
|
// queue. Streams + file-RPC issue nested `shade.receive` calls for
|
||||||
|
// the same peer from inside their handlers (e.g. `stream-end`
|
||||||
|
// arrives while a write-RPC is still waiting on chunks); holding
|
||||||
|
// the queue across the handler would self-deadlock. The atomic
|
||||||
|
// unit we have to protect is just the ratchet+storage step, not
|
||||||
|
// the consumer's reaction to it.
|
||||||
|
const previous = this.decryptChains.get(from) ?? Promise.resolve();
|
||||||
|
const decryptPromise = previous
|
||||||
|
.catch(() => undefined) // don't propagate upstream failures
|
||||||
|
.then(() => this.manager.decrypt(from, envelope));
|
||||||
|
// Store a never-rejecting copy so the next chained receive doesn't
|
||||||
|
// see a rejection from this one (we still surface our own rejection
|
||||||
|
// to *this* caller via the original `decryptPromise`).
|
||||||
|
this.decryptChains.set(from, decryptPromise.catch(() => undefined));
|
||||||
|
const plaintext = await decryptPromise;
|
||||||
|
|
||||||
|
const consumed = await maybeHandleControlPlaintext(
|
||||||
|
this.broadcastHooks(),
|
||||||
|
from,
|
||||||
|
plaintext,
|
||||||
|
);
|
||||||
|
if (consumed) return '';
|
||||||
|
const meta: MessageMeta = { kind: 'direct' };
|
||||||
for (const handler of this.messageHandlers) {
|
for (const handler of this.messageHandlers) {
|
||||||
try {
|
try {
|
||||||
await handler(from, plaintext);
|
await handler(from, plaintext, meta);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('[Shade] Message handler threw:', err);
|
console.error('[Shade] Message handler threw:', err);
|
||||||
}
|
}
|
||||||
@@ -408,9 +500,16 @@ export class Shade {
|
|||||||
return plaintext;
|
return plaintext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Register a handler for incoming messages. Async handlers are awaited. */
|
/**
|
||||||
|
* Register a handler for incoming messages. Async handlers are awaited.
|
||||||
|
*
|
||||||
|
* V4.6: handlers may declare an optional `meta` parameter to discriminate
|
||||||
|
* direct (`meta.kind === 'direct'`) from broadcast (`meta.kind === 'broadcast'`)
|
||||||
|
* deliveries. Handlers that ignore the third arg keep working unchanged
|
||||||
|
* for direct messages.
|
||||||
|
*/
|
||||||
onMessage(
|
onMessage(
|
||||||
handler: (from: string, plaintext: string) => void | Promise<void>,
|
handler: (from: string, plaintext: string, meta?: MessageMeta) => void | Promise<void>,
|
||||||
): () => void {
|
): () => void {
|
||||||
this.messageHandlers.push(handler);
|
this.messageHandlers.push(handler);
|
||||||
return () => {
|
return () => {
|
||||||
@@ -418,6 +517,73 @@ export class Shade {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── V4.6 Broadcast channels ───────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new broadcast channel owned by this device. Returns a
|
||||||
|
* handle for adding/removing members, encrypting a single payload,
|
||||||
|
* and rotating on revocation. The channel id is opaque, stable
|
||||||
|
* across `shutdown()` / re-open, and persisted via the configured
|
||||||
|
* `StorageProvider`.
|
||||||
|
*/
|
||||||
|
async createBroadcastChannel(opts: { label?: string } = {}): Promise<BroadcastChannel> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
return createBroadcastChannelImpl(this.broadcastHooks(), opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Look up an existing sender-side broadcast channel by id. Returns
|
||||||
|
* `null` when the id is unknown OR when this device only holds a
|
||||||
|
* receiver-side copy (the receiver path uses `onMessage` for delivery
|
||||||
|
* — there is no app-facing handle on the receive side).
|
||||||
|
*/
|
||||||
|
async getBroadcastChannel(channelId: string): Promise<BroadcastChannel | null> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
return getBroadcastChannelImpl(this.broadcastHooks(), channelId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Snapshot of every broadcast channel persisted on this device,
|
||||||
|
* including receiver-side channels that we joined. Useful for
|
||||||
|
* rebuilding UI state on startup.
|
||||||
|
*/
|
||||||
|
async listBroadcastChannels(): Promise<readonly BroadcastChannelSummary[]> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
return listBroadcastChannelsImpl(this.broadcastHooks());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hand a wire-encoded broadcast envelope (type 0x21) to the SDK.
|
||||||
|
* Decrypts via the matching channel, advances the chain, and dispatches
|
||||||
|
* the plaintext to `onMessage` handlers with `meta.kind === 'broadcast'`.
|
||||||
|
*
|
||||||
|
* Stale generations (sender's old chain after a rotation we already
|
||||||
|
* received) are silently dropped. Future generations (we haven't seen
|
||||||
|
* the rotation distribution yet) throw — the app should ensure the
|
||||||
|
* distribution envelope is delivered before the broadcast.
|
||||||
|
*/
|
||||||
|
async acceptBroadcast(envelope: Uint8Array): Promise<void> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
const result = await acceptBroadcastEnvelope(this.broadcastHooks(), envelope);
|
||||||
|
if (result === null) return;
|
||||||
|
for (const handler of this.messageHandlers) {
|
||||||
|
try {
|
||||||
|
await handler(result.meta.kind === 'broadcast' ? result.meta.sender : '', result.plaintext, result.meta);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[Shade] Broadcast handler threw:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private broadcastHooks(): BroadcastSdkHooks {
|
||||||
|
return {
|
||||||
|
bilateralSend: (peer, pt) => this.send(peer, pt),
|
||||||
|
myAddress: () => this.address,
|
||||||
|
crypto: this.crypto,
|
||||||
|
storage: this.storage,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/** Get a peer's fingerprint (requires an existing session) */
|
/** Get a peer's fingerprint (requires an existing session) */
|
||||||
async getFingerprintFor(address: string): Promise<string> {
|
async getFingerprintFor(address: string): Promise<string> {
|
||||||
if (!this.initialized) throw new Error('Not initialized');
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
@@ -500,6 +666,41 @@ export class Shade {
|
|||||||
await this.gates.revoke(address);
|
await this.gates.revoke(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Move every per-peer storage row for `oldLabel` (session, trusted
|
||||||
|
* identity, peer-verification, identity-version counter) to
|
||||||
|
* `newLabel`. Use this when first-contact forced you to label a
|
||||||
|
* session by the relay's sender-fingerprint hint
|
||||||
|
* (`fp:<hex>` — see `IncomingMessage.from` / `FetchedBlob.from`) and
|
||||||
|
* the just-decrypted plaintext announces the peer's canonical
|
||||||
|
* address: alias once and every subsequent
|
||||||
|
* `send`/`receive`/broadcast cross-check operates under the
|
||||||
|
* announced label, no app-side fp ↔ address mapping needed for the
|
||||||
|
* receive path.
|
||||||
|
*
|
||||||
|
* The rename is atomic from a per-peer-mutex perspective — both
|
||||||
|
* labels are locked for the duration so concurrent encrypt/decrypt
|
||||||
|
* can't observe a half-moved state. Throws if `oldLabel` has no
|
||||||
|
* session, or if `newLabel` already does (refuses to overwrite —
|
||||||
|
* call `resetSession` first if that's intentional).
|
||||||
|
*
|
||||||
|
* After alias, the SDK's internal serialization queues
|
||||||
|
* (`encryptChains`, `decryptChains`) for `oldLabel` are dropped so
|
||||||
|
* future operations don't queue behind a stale chain.
|
||||||
|
*
|
||||||
|
* V4.8.3 — Prism FR `session-label-asymmetry-v4.8.2.md`.
|
||||||
|
*/
|
||||||
|
async aliasSession(oldLabel: string, newLabel: string): Promise<void> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
await this.manager.aliasSession(oldLabel, newLabel);
|
||||||
|
// The SDK's per-`from` chains are keyed by label; drop the old
|
||||||
|
// entries so future `send`/`receive` to either label start with a
|
||||||
|
// fresh queue rather than chaining off whatever was last in flight
|
||||||
|
// for `oldLabel`.
|
||||||
|
this.encryptChains.delete(oldLabel);
|
||||||
|
this.decryptChains.delete(oldLabel);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accept a peer's rotated identity. Bumps the per-peer identity-version
|
* Accept a peer's rotated identity. Bumps the per-peer identity-version
|
||||||
* counter so any earlier verification automatically goes stale, then
|
* counter so any earlier verification automatically goes stale, then
|
||||||
@@ -1320,6 +1521,97 @@ export class Shade {
|
|||||||
await this.storage.pruneStreamStates(olderThan);
|
await this.storage.pruneStreamStates(olderThan);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Streaming sub-sessions (V4.11) ────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open a long-lived streaming Double-Ratchet sub-session to an
|
||||||
|
* already-known peer, for wrapping individual frames on a
|
||||||
|
* bidirectional, often server-heavy channel (e.g. a console-log
|
||||||
|
* WebSocket) with the same confidentiality / forward-secrecy /
|
||||||
|
* replay guarantees as the HTTP `send`/`receive` path.
|
||||||
|
*
|
||||||
|
* This is the **initiator** half. Like the rest of the SDK it is
|
||||||
|
* transport-agnostic: it produces handshake/frame bytes you put on
|
||||||
|
* your WebSocket, and consumes the bytes you receive from it.
|
||||||
|
*
|
||||||
|
* ```ts
|
||||||
|
* const stream = await shade.openStream(peerAddr);
|
||||||
|
* ws.send(stream.handshakeFrame()); // → STREAM_OPEN
|
||||||
|
* // … first inbound WS frame is the peer's STREAM_OPEN_ACK …
|
||||||
|
* await stream.handleHandshake(ackBytes); // stream now usable
|
||||||
|
* ws.send(await stream.seal(utf8(line))); // outbound frame
|
||||||
|
* onLog(await stream.open(inboundBytes)); // inbound frame
|
||||||
|
* await stream.close(); // on ws close
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* Independence (R5): this never touches the stored parent session,
|
||||||
|
* its prekeys, or the per-peer `send`/`receive` queues — it runs
|
||||||
|
* concurrently against the same peer. The ratchet lives only in
|
||||||
|
* memory and is zeroized by {@link ShadeStream.close}; a dropped
|
||||||
|
* connection is re-opened with a fresh `openStream`, never resumed
|
||||||
|
* (persisting per-frame ratchet secrets would defeat forward
|
||||||
|
* secrecy).
|
||||||
|
*
|
||||||
|
* Note (Double-Ratchet semantics): a responder cannot `seal` until
|
||||||
|
* it has `open`ed at least one frame from the initiator (standard
|
||||||
|
* Signal behaviour). For a server-heavy stream either make the bursty
|
||||||
|
* sender the initiator, or have the initiator send one priming frame
|
||||||
|
* right after the handshake.
|
||||||
|
*
|
||||||
|
* Requires an established parent session; one is auto-established
|
||||||
|
* (same path as {@link send}) if missing.
|
||||||
|
*/
|
||||||
|
async openStream(peerAddress: string): Promise<ShadeStream> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
let begun;
|
||||||
|
try {
|
||||||
|
begun = await this.manager.beginStream(peerAddress);
|
||||||
|
} catch (err) {
|
||||||
|
if (!(err instanceof NoSessionError)) throw err;
|
||||||
|
await this.ensureSession(peerAddress);
|
||||||
|
begun = await this.manager.beginStream(peerAddress);
|
||||||
|
}
|
||||||
|
return new ShadeStream({
|
||||||
|
peer: peerAddress,
|
||||||
|
role: 'initiator',
|
||||||
|
streamId: begun.streamId,
|
||||||
|
events: this.events,
|
||||||
|
handshakeOut: encodeStreamOpen(begun.streamId, begun.ephemeralPublicKey),
|
||||||
|
complete: begun.complete,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accept an inbound stream — the **responder** half. Feed it the
|
||||||
|
* peer's `STREAM_OPEN` bytes (route by {@link inspectEnvelopeType}
|
||||||
|
* `=== 'stream-open'`). The returned stream is immediately usable for
|
||||||
|
* `open()`; send `handshakeFrame()` (the `STREAM_OPEN_ACK`) back over
|
||||||
|
* the transport so the initiator can complete its side.
|
||||||
|
*/
|
||||||
|
async acceptStream(peerAddress: string, openBytes: Uint8Array): Promise<ShadeStream> {
|
||||||
|
if (!this.initialized) throw new Error('Not initialized');
|
||||||
|
const hs = decodeStreamHandshake(openBytes);
|
||||||
|
if (hs.kind !== 'open') {
|
||||||
|
throw new StreamHandshakeError(`expected STREAM_OPEN, got ${hs.kind}`);
|
||||||
|
}
|
||||||
|
let accepted;
|
||||||
|
try {
|
||||||
|
accepted = await this.manager.acceptStream(peerAddress, hs.streamId, hs.ephemeralPub);
|
||||||
|
} catch (err) {
|
||||||
|
if (!(err instanceof NoSessionError)) throw err;
|
||||||
|
await this.ensureSession(peerAddress);
|
||||||
|
accepted = await this.manager.acceptStream(peerAddress, hs.streamId, hs.ephemeralPub);
|
||||||
|
}
|
||||||
|
return new ShadeStream({
|
||||||
|
peer: peerAddress,
|
||||||
|
role: 'responder',
|
||||||
|
streamId: hs.streamId,
|
||||||
|
events: this.events,
|
||||||
|
handshakeOut: encodeStreamOpenAck(hs.streamId, accepted.ephemeralPublicKey),
|
||||||
|
ratchet: accepted.stream,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private async ensureSession(address: string): Promise<void> {
|
private async ensureSession(address: string): Promise<void> {
|
||||||
// Deduplicate concurrent establishment requests
|
// Deduplicate concurrent establishment requests
|
||||||
const existing = this.establishing.get(address);
|
const existing = this.establishing.get(address);
|
||||||
@@ -1342,6 +1634,158 @@ export class Shade {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── ShadeStream (V4.11) ─────────────────────────────────────
|
||||||
|
|
||||||
|
interface ShadeStreamInit {
|
||||||
|
peer: string;
|
||||||
|
role: 'initiator' | 'responder';
|
||||||
|
streamId: Uint8Array;
|
||||||
|
events: ShadeEventEmitter;
|
||||||
|
/** Bytes to put on the wire for our half of the handshake. */
|
||||||
|
handshakeOut: Uint8Array;
|
||||||
|
/** Initiator only: continuation that derives the ratchet from the ACK. */
|
||||||
|
complete?: (peerEphemeralPub: Uint8Array) => Promise<StreamRatchet>;
|
||||||
|
/** Responder only: ratchet is ready at accept time. */
|
||||||
|
ratchet?: StreamRatchet;
|
||||||
|
}
|
||||||
|
|
||||||
|
function streamIdsEqual(a: Uint8Array, b: Uint8Array): boolean {
|
||||||
|
if (a.length !== b.length) return false;
|
||||||
|
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A live streaming Double-Ratchet sub-session. Transport-agnostic: it
|
||||||
|
* emits/consumes wire bytes, the caller owns the WebSocket (or any
|
||||||
|
* other ordered frame transport).
|
||||||
|
*
|
||||||
|
* Lifecycle:
|
||||||
|
* - **initiator**: `handshakeFrame()` → `STREAM_OPEN`; after the peer's
|
||||||
|
* `STREAM_OPEN_ACK` arrives call `handleHandshake(ack)`; then
|
||||||
|
* `seal`/`open`.
|
||||||
|
* - **responder**: usable immediately; `handshakeFrame()` →
|
||||||
|
* `STREAM_OPEN_ACK` to send back; `open` the initiator's first frame
|
||||||
|
* before `seal` (standard Double-Ratchet ordering).
|
||||||
|
*/
|
||||||
|
export class ShadeStream {
|
||||||
|
private readonly _streamId: Uint8Array;
|
||||||
|
private readonly _peer: string;
|
||||||
|
private readonly _role: 'initiator' | 'responder';
|
||||||
|
private readonly events: ShadeEventEmitter;
|
||||||
|
private readonly handshakeOut: Uint8Array;
|
||||||
|
private readonly complete?: (peerEphemeralPub: Uint8Array) => Promise<StreamRatchet>;
|
||||||
|
private ratchet: StreamRatchet | null;
|
||||||
|
private state: 'await-ack' | 'open' | 'closed';
|
||||||
|
|
||||||
|
constructor(init: ShadeStreamInit) {
|
||||||
|
this._streamId = init.streamId;
|
||||||
|
this._peer = init.peer;
|
||||||
|
this._role = init.role;
|
||||||
|
this.events = init.events;
|
||||||
|
this.handshakeOut = init.handshakeOut;
|
||||||
|
if (init.role === 'initiator') {
|
||||||
|
if (init.complete) this.complete = init.complete;
|
||||||
|
this.ratchet = null;
|
||||||
|
this.state = 'await-ack';
|
||||||
|
} else {
|
||||||
|
this.ratchet = init.ratchet ?? null;
|
||||||
|
this.state = 'open';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Peer address this stream is bound to. */
|
||||||
|
get peer(): string {
|
||||||
|
return this._peer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Which half of the handshake this end performed. */
|
||||||
|
get role(): 'initiator' | 'responder' {
|
||||||
|
return this._role;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Lowercase-hex stream id (stable for the stream's lifetime). */
|
||||||
|
get streamId(): string {
|
||||||
|
return Array.from(this._streamId, (b) => b.toString(16).padStart(2, '0')).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** True once the ratchet is established and not yet closed. */
|
||||||
|
get isOpen(): boolean {
|
||||||
|
return this.state === 'open' && this.ratchet !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bytes for our half of the handshake to put on the transport
|
||||||
|
* (`STREAM_OPEN` for an initiator, `STREAM_OPEN_ACK` for a responder).
|
||||||
|
* Stable; safe to read once and send.
|
||||||
|
*/
|
||||||
|
handshakeFrame(): Uint8Array {
|
||||||
|
return this.handshakeOut;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initiator only: consume the peer's `STREAM_OPEN_ACK` and derive the
|
||||||
|
* ratchet. Idempotent-safe to call exactly once; throws if called on
|
||||||
|
* a responder, out of order, or with a mismatched streamId.
|
||||||
|
*/
|
||||||
|
async handleHandshake(ackBytes: Uint8Array): Promise<void> {
|
||||||
|
if (this._role !== 'initiator') {
|
||||||
|
throw new StreamHandshakeError('handleHandshake is initiator-only');
|
||||||
|
}
|
||||||
|
if (this.state !== 'await-ack' || !this.complete) {
|
||||||
|
throw new StreamHandshakeError('handshake already completed or stream closed');
|
||||||
|
}
|
||||||
|
const hs = decodeStreamHandshake(ackBytes);
|
||||||
|
if (hs.kind !== 'open-ack') {
|
||||||
|
throw new StreamHandshakeError(`expected STREAM_OPEN_ACK, got ${hs.kind}`);
|
||||||
|
}
|
||||||
|
if (!streamIdsEqual(hs.streamId, this._streamId)) {
|
||||||
|
throw new StreamHandshakeError('STREAM_OPEN_ACK streamId mismatch');
|
||||||
|
}
|
||||||
|
this.ratchet = await this.complete(hs.ephemeralPub);
|
||||||
|
this.state = 'open';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seal one logical frame. Returns `STREAM_FRAME` wire bytes — put
|
||||||
|
* exactly one in one WS frame. Advances the sending chain one step.
|
||||||
|
*/
|
||||||
|
async seal(plaintext: Uint8Array): Promise<Uint8Array> {
|
||||||
|
if (!this.ratchet || this.state !== 'open') {
|
||||||
|
throw new StreamHandshakeError('stream not open (complete the handshake first)');
|
||||||
|
}
|
||||||
|
const msg = await this.ratchet.seal(plaintext);
|
||||||
|
return encodeStreamFrame(this._streamId, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open one inbound `STREAM_FRAME`. Correct and memory-bounded across
|
||||||
|
* long one-directional bursts; replays / counter-rewinds are rejected
|
||||||
|
* by the underlying ratchet.
|
||||||
|
*/
|
||||||
|
async open(wire: Uint8Array): Promise<Uint8Array> {
|
||||||
|
if (!this.ratchet || this.state !== 'open') {
|
||||||
|
throw new StreamHandshakeError('stream not open (complete the handshake first)');
|
||||||
|
}
|
||||||
|
const frame: { streamId: Uint8Array; message: RatchetMessage } = decodeStreamFrame(wire);
|
||||||
|
if (!streamIdsEqual(frame.streamId, this._streamId)) {
|
||||||
|
throw new StreamHandshakeError('STREAM_FRAME streamId mismatch');
|
||||||
|
}
|
||||||
|
return this.ratchet.open(frame.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Zeroize and drop the ratchet. Idempotent. */
|
||||||
|
async close(): Promise<void> {
|
||||||
|
if (this.state === 'closed') return;
|
||||||
|
this.state = 'closed';
|
||||||
|
if (this.ratchet) {
|
||||||
|
await this.ratchet.close();
|
||||||
|
this.ratchet = null;
|
||||||
|
}
|
||||||
|
this.events.emit('stream.closed', { address: this._peer });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function bytesToBase64Std(bytes: Uint8Array): string {
|
function bytesToBase64Std(bytes: Uint8Array): string {
|
||||||
let bin = '';
|
let bin = '';
|
||||||
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]!);
|
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]!);
|
||||||
@@ -1484,9 +1928,7 @@ class HttpEnvelopeTransport implements ControlEnvelopeTransport {
|
|||||||
|
|
||||||
// ─── Helpers ─────────────────────────────────────────────────
|
// ─── Helpers ─────────────────────────────────────────────────
|
||||||
|
|
||||||
async function resolveStorage(
|
async function resolveStorage(spec: StorageSpec): Promise<StorageProvider> {
|
||||||
spec: string | StorageProvider | { type: 'postgres'; url: string },
|
|
||||||
): Promise<StorageProvider> {
|
|
||||||
if (typeof spec === 'object' && 'getIdentityKeyPair' in spec) {
|
if (typeof spec === 'object' && 'getIdentityKeyPair' in spec) {
|
||||||
return spec;
|
return spec;
|
||||||
}
|
}
|
||||||
@@ -1512,6 +1954,18 @@ async function resolveStorage(
|
|||||||
return mod.PostgresStorage.create(spec.url);
|
return mod.PostgresStorage.create(spec.url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof spec === 'object' && spec.type === 'indexeddb') {
|
||||||
|
// Dynamic import keeps @shade/storage-indexeddb optional — Node-only
|
||||||
|
// consumers don't need to install a browser-only adapter.
|
||||||
|
const moduleId = '@shade/storage-indexeddb';
|
||||||
|
const mod = (await import(moduleId)) as {
|
||||||
|
IndexedDBStorage: { create(opts: { dbName?: string }): Promise<StorageProvider> };
|
||||||
|
};
|
||||||
|
const opts: { dbName?: string } = {};
|
||||||
|
if (spec.dbName !== undefined) opts.dbName = spec.dbName;
|
||||||
|
return mod.IndexedDBStorage.create(opts);
|
||||||
|
}
|
||||||
|
|
||||||
throw new Error(`Unsupported storage spec: ${JSON.stringify(spec)}`);
|
throw new Error(`Unsupported storage spec: ${JSON.stringify(spec)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
552
packages/shade-sdk/tests/approval.test.ts
Normal file
552
packages/shade-sdk/tests/approval.test.ts
Normal file
@@ -0,0 +1,552 @@
|
|||||||
|
import { describe, test, expect } from 'bun:test';
|
||||||
|
import {
|
||||||
|
emptyCanonicalProfile,
|
||||||
|
parseCanonicalProfile,
|
||||||
|
serializeCanonicalProfile,
|
||||||
|
upsertHost,
|
||||||
|
upsertClient,
|
||||||
|
removeClient,
|
||||||
|
setTrustedApprover,
|
||||||
|
isTrustedApprover,
|
||||||
|
findClientByFingerprint,
|
||||||
|
findClientByAddress,
|
||||||
|
buildApprovalRequest,
|
||||||
|
signProxyApproval,
|
||||||
|
verifyProxyApproval,
|
||||||
|
canonicalApprovalSigningBytes,
|
||||||
|
DEFAULT_APPROVAL_DOMAIN,
|
||||||
|
} from '../src/index.js';
|
||||||
|
import type {
|
||||||
|
CanonicalProfileBlob,
|
||||||
|
ProfileClientEntry,
|
||||||
|
ProfileHostEntry,
|
||||||
|
ApprovalRequestFrame,
|
||||||
|
} from '../src/index.js';
|
||||||
|
import { SubtleCryptoProvider, ed25519PublicKeyFromSeed } from '@shade/crypto-web';
|
||||||
|
import { ValidationError } from '@shade/core';
|
||||||
|
|
||||||
|
const crypto = new SubtleCryptoProvider();
|
||||||
|
|
||||||
|
function randBytes(n: number): Uint8Array {
|
||||||
|
const buf = new Uint8Array(n);
|
||||||
|
globalThis.crypto.getRandomValues(buf);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
function bytesToHex(bytes: Uint8Array): string {
|
||||||
|
let s = '';
|
||||||
|
for (let i = 0; i < bytes.length; i++) s += bytes[i]!.toString(16).padStart(2, '0');
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeClient(opts: {
|
||||||
|
name: string;
|
||||||
|
trusted?: boolean;
|
||||||
|
}): { entry: ProfileClientEntry; signingSeed: Uint8Array } {
|
||||||
|
const signingSeed = randBytes(32);
|
||||||
|
const pubkey = ed25519PublicKeyFromSeed(signingSeed);
|
||||||
|
const fingerprint = `fp-${opts.name}-${bytesToHex(pubkey).slice(0, 8)}`;
|
||||||
|
const entry: ProfileClientEntry = {
|
||||||
|
address: `device:${opts.name}`,
|
||||||
|
identityPublicKey: bytesToHex(pubkey),
|
||||||
|
identityFingerprint: fingerprint,
|
||||||
|
name: opts.name,
|
||||||
|
kind: 'mobile',
|
||||||
|
addedAt: 1_700_000_000_000,
|
||||||
|
};
|
||||||
|
if (opts.trusted) entry.trustedApprover = true;
|
||||||
|
return { entry, signingSeed };
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeHost(): ProfileHostEntry {
|
||||||
|
return {
|
||||||
|
address: 'device:host-server',
|
||||||
|
name: 'Server',
|
||||||
|
kind: 'server',
|
||||||
|
addedAt: 1_700_000_000_000,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('Canonical profile schema', () => {
|
||||||
|
test('emptyCanonicalProfile is well-formed and round-trips', () => {
|
||||||
|
const blob = emptyCanonicalProfile(123);
|
||||||
|
expect(blob.version).toBe(1);
|
||||||
|
expect(blob.hosts).toEqual([]);
|
||||||
|
expect(blob.clients).toEqual([]);
|
||||||
|
expect(blob.trustedApproverFingerprints).toEqual([]);
|
||||||
|
expect(blob.updatedAt).toBe(123);
|
||||||
|
|
||||||
|
const bytes = serializeCanonicalProfile(blob);
|
||||||
|
const parsed = parseCanonicalProfile(bytes);
|
||||||
|
expect(parsed).toEqual(blob);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('upsertHost replaces by address and bumps updatedAt', () => {
|
||||||
|
let blob = emptyCanonicalProfile(0);
|
||||||
|
const host = makeHost();
|
||||||
|
blob = upsertHost(blob, host, 100);
|
||||||
|
expect(blob.hosts).toEqual([host]);
|
||||||
|
expect(blob.updatedAt).toBe(100);
|
||||||
|
|
||||||
|
const renamed = { ...host, name: 'Server (renamed)' };
|
||||||
|
blob = upsertHost(blob, renamed, 200);
|
||||||
|
expect(blob.hosts).toHaveLength(1);
|
||||||
|
expect(blob.hosts[0]!.name).toBe('Server (renamed)');
|
||||||
|
expect(blob.updatedAt).toBe(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('upsertClient denormalizes trustedApproverFingerprints', () => {
|
||||||
|
let blob = emptyCanonicalProfile(0);
|
||||||
|
const a = makeClient({ name: 'phone-a', trusted: true });
|
||||||
|
const b = makeClient({ name: 'phone-b', trusted: false });
|
||||||
|
blob = upsertClient(blob, a.entry, 100);
|
||||||
|
blob = upsertClient(blob, b.entry, 200);
|
||||||
|
|
||||||
|
expect(blob.clients).toHaveLength(2);
|
||||||
|
expect(blob.trustedApproverFingerprints).toEqual([a.entry.identityFingerprint]);
|
||||||
|
expect(isTrustedApprover(blob, a.entry.identityFingerprint)).toBe(true);
|
||||||
|
expect(isTrustedApprover(blob, b.entry.identityFingerprint)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('setTrustedApprover toggles the flag and the denormalized list', () => {
|
||||||
|
let blob = emptyCanonicalProfile(0);
|
||||||
|
const c = makeClient({ name: 'phone', trusted: false });
|
||||||
|
blob = upsertClient(blob, c.entry);
|
||||||
|
expect(blob.trustedApproverFingerprints).toEqual([]);
|
||||||
|
|
||||||
|
blob = setTrustedApprover(blob, c.entry.identityFingerprint, true, 100);
|
||||||
|
expect(blob.trustedApproverFingerprints).toEqual([c.entry.identityFingerprint]);
|
||||||
|
expect(blob.clients[0]!.trustedApprover).toBe(true);
|
||||||
|
expect(blob.updatedAt).toBe(100);
|
||||||
|
|
||||||
|
blob = setTrustedApprover(blob, c.entry.identityFingerprint, false, 200);
|
||||||
|
expect(blob.trustedApproverFingerprints).toEqual([]);
|
||||||
|
expect(blob.clients[0]!.trustedApprover).toBeUndefined();
|
||||||
|
expect(blob.updatedAt).toBe(200);
|
||||||
|
|
||||||
|
// No-op toggle to existing state returns the same blob.
|
||||||
|
const before = blob;
|
||||||
|
blob = setTrustedApprover(blob, c.entry.identityFingerprint, false, 999);
|
||||||
|
expect(blob).toBe(before);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('removeClient cleans up trustedApproverFingerprints', () => {
|
||||||
|
let blob = emptyCanonicalProfile(0);
|
||||||
|
const c = makeClient({ name: 'phone', trusted: true });
|
||||||
|
blob = upsertClient(blob, c.entry);
|
||||||
|
expect(blob.trustedApproverFingerprints).toHaveLength(1);
|
||||||
|
|
||||||
|
blob = removeClient(blob, c.entry.identityFingerprint);
|
||||||
|
expect(blob.clients).toEqual([]);
|
||||||
|
expect(blob.trustedApproverFingerprints).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('findClientByFingerprint and findClientByAddress', () => {
|
||||||
|
let blob = emptyCanonicalProfile(0);
|
||||||
|
const c = makeClient({ name: 'phone' });
|
||||||
|
blob = upsertClient(blob, c.entry);
|
||||||
|
|
||||||
|
expect(findClientByFingerprint(blob, c.entry.identityFingerprint)?.address).toBe(
|
||||||
|
c.entry.address,
|
||||||
|
);
|
||||||
|
expect(findClientByAddress(blob, c.entry.address)?.identityFingerprint).toBe(
|
||||||
|
c.entry.identityFingerprint,
|
||||||
|
);
|
||||||
|
expect(findClientByFingerprint(blob, 'unknown')).toBeNull();
|
||||||
|
expect(findClientByAddress(blob, 'unknown')).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parseCanonicalProfile rejects malformed input', () => {
|
||||||
|
expect(() => parseCanonicalProfile('not json')).toThrow(ValidationError);
|
||||||
|
expect(() => parseCanonicalProfile('[]')).toThrow(ValidationError);
|
||||||
|
expect(() => parseCanonicalProfile('{"version":2}')).toThrow(ValidationError);
|
||||||
|
expect(() =>
|
||||||
|
parseCanonicalProfile(
|
||||||
|
JSON.stringify({
|
||||||
|
version: 1,
|
||||||
|
clients: [{ address: 'x', name: 'x', kind: 'm', addedAt: 0 }],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
).toThrow(ValidationError); // missing identityPublicKey
|
||||||
|
expect(() =>
|
||||||
|
parseCanonicalProfile(
|
||||||
|
JSON.stringify({
|
||||||
|
version: 1,
|
||||||
|
clients: [
|
||||||
|
{
|
||||||
|
address: 'x',
|
||||||
|
name: 'x',
|
||||||
|
kind: 'm',
|
||||||
|
addedAt: 0,
|
||||||
|
identityPublicKey: 'NOTHEX',
|
||||||
|
identityFingerprint: 'x',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
).toThrow(ValidationError); // identityPublicKey not 64 hex
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parsed blob is fully equal to the input via JSON round-trip', () => {
|
||||||
|
let blob = emptyCanonicalProfile(1);
|
||||||
|
const host = makeHost();
|
||||||
|
const c = makeClient({ name: 'phone', trusted: true });
|
||||||
|
blob = upsertHost(blob, host, 2);
|
||||||
|
blob = upsertClient(blob, c.entry, 3);
|
||||||
|
blob.signedBy = 'aabbccdd';
|
||||||
|
|
||||||
|
const bytes = serializeCanonicalProfile(blob);
|
||||||
|
const parsed = parseCanonicalProfile(bytes);
|
||||||
|
expect(parsed).toEqual(blob);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Approval signing payload', () => {
|
||||||
|
test('canonicalApprovalSigningBytes is deterministic', () => {
|
||||||
|
const a = canonicalApprovalSigningBytes({
|
||||||
|
domain: 'shade-link-approve-v1',
|
||||||
|
requestId: 'aabbccddeeff00112233445566778899',
|
||||||
|
hostFingerprint: '11111 22222 33333 44444',
|
||||||
|
requestingDeviceFingerprint: '55555 66666 77777 88888',
|
||||||
|
decision: 'approve',
|
||||||
|
});
|
||||||
|
const b = canonicalApprovalSigningBytes({
|
||||||
|
domain: 'shade-link-approve-v1',
|
||||||
|
requestId: 'aabbccddeeff00112233445566778899',
|
||||||
|
hostFingerprint: '11111 22222 33333 44444',
|
||||||
|
requestingDeviceFingerprint: '55555 66666 77777 88888',
|
||||||
|
decision: 'approve',
|
||||||
|
});
|
||||||
|
expect(Buffer.from(a).toString('hex')).toBe(Buffer.from(b).toString('hex'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('different decision produces different signing bytes', () => {
|
||||||
|
const base = {
|
||||||
|
domain: 'shade-link-approve-v1',
|
||||||
|
requestId: 'aabbccddeeff00112233445566778899',
|
||||||
|
hostFingerprint: 'h',
|
||||||
|
requestingDeviceFingerprint: 'r',
|
||||||
|
};
|
||||||
|
const approveBytes = canonicalApprovalSigningBytes({ ...base, decision: 'approve' });
|
||||||
|
const rejectBytes = canonicalApprovalSigningBytes({ ...base, decision: 'reject' });
|
||||||
|
expect(Buffer.from(approveBytes).toString('hex')).not.toBe(
|
||||||
|
Buffer.from(rejectBytes).toString('hex'),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('different domain produces different signing bytes', () => {
|
||||||
|
const a = canonicalApprovalSigningBytes({
|
||||||
|
domain: 'shade-link-approve-v1',
|
||||||
|
requestId: 'r',
|
||||||
|
hostFingerprint: 'h',
|
||||||
|
requestingDeviceFingerprint: 'd',
|
||||||
|
decision: 'approve',
|
||||||
|
});
|
||||||
|
const b = canonicalApprovalSigningBytes({
|
||||||
|
domain: 'prism-link-approve-v1',
|
||||||
|
requestId: 'r',
|
||||||
|
hostFingerprint: 'h',
|
||||||
|
requestingDeviceFingerprint: 'd',
|
||||||
|
decision: 'approve',
|
||||||
|
});
|
||||||
|
expect(Buffer.from(a).toString('hex')).not.toBe(Buffer.from(b).toString('hex'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Build / sign / verify proxy approval', () => {
|
||||||
|
function buildScenario() {
|
||||||
|
const phone = makeClient({ name: 'phone', trusted: true });
|
||||||
|
let profile = emptyCanonicalProfile(0);
|
||||||
|
profile = upsertHost(profile, makeHost());
|
||||||
|
profile = upsertClient(profile, phone.entry);
|
||||||
|
|
||||||
|
const request = buildApprovalRequest({
|
||||||
|
hostAddress: 'device:host-server',
|
||||||
|
hostFingerprint: 'host-fp-12345',
|
||||||
|
requestingDevice: {
|
||||||
|
fingerprint: 'cafe-laptop-fp-67890',
|
||||||
|
deviceName: 'cafe-laptop',
|
||||||
|
userAgent: 'Mozilla/5.0',
|
||||||
|
ipHint: '203.0.113.7',
|
||||||
|
},
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
|
||||||
|
return { phone, profile, request };
|
||||||
|
}
|
||||||
|
|
||||||
|
test('happy path: signed approve verifies', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(approval.kind).toBe('linkApproveByProxy');
|
||||||
|
expect(approval.requestId).toBe(request.requestId);
|
||||||
|
expect(approval.signature.length).toBe(128); // 64-byte sig as hex
|
||||||
|
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request,
|
||||||
|
approval,
|
||||||
|
profile,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(true);
|
||||||
|
if (verdict.ok) {
|
||||||
|
expect(verdict.approver.address).toBe(phone.entry.address);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('happy path: signed reject verifies', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'reject',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
const verdict = await verifyProxyApproval({ request, approval, profile, crypto });
|
||||||
|
expect(verdict.ok).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('replay against a different request fails (request-id-mismatch)', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
|
||||||
|
const otherRequest: ApprovalRequestFrame = {
|
||||||
|
...request,
|
||||||
|
requestId: 'ffffffffffffffffffffffffffffffff',
|
||||||
|
};
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request: otherRequest,
|
||||||
|
approval,
|
||||||
|
profile,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(false);
|
||||||
|
if (!verdict.ok) expect(verdict.reason).toBe('request-id-mismatch');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('decision tampered after signing fails (bad-signature)', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
|
||||||
|
const tampered = { ...approval, decision: 'reject' as const };
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request,
|
||||||
|
approval: tampered,
|
||||||
|
profile,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(false);
|
||||||
|
if (!verdict.ok) expect(verdict.reason).toBe('bad-signature');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('host fingerprint substitution fails (bad-signature)', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verifier sees the same approval but a different host fingerprint
|
||||||
|
// (simulates an attacker forwarding an approval to a different host).
|
||||||
|
const swappedRequest: ApprovalRequestFrame = {
|
||||||
|
...request,
|
||||||
|
hostFingerprint: 'evil-host-fp',
|
||||||
|
};
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request: swappedRequest,
|
||||||
|
approval,
|
||||||
|
profile,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(false);
|
||||||
|
if (!verdict.ok) expect(verdict.reason).toBe('bad-signature');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('domain mismatch is rejected before signature check', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
const tampered = { ...approval, domain: 'prism-link-approve-v1' };
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request,
|
||||||
|
approval: tampered,
|
||||||
|
profile,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(false);
|
||||||
|
if (!verdict.ok) expect(verdict.reason).toBe('domain-mismatch');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('unknown approver fingerprint fails', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
const lying = { ...approval, approverFingerprint: 'no-such-fingerprint' };
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request,
|
||||||
|
approval: lying,
|
||||||
|
profile,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(false);
|
||||||
|
if (!verdict.ok) expect(verdict.reason).toBe('unknown-approver');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('revoked approver (trustedApprover off) fails with not-trusted', async () => {
|
||||||
|
const { phone, profile: original, request } = buildScenario();
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
// Simulate a revoke: workstation toggles the trustedApprover flag off
|
||||||
|
// and PUTs the new blob; host re-fetches before verifying.
|
||||||
|
const revoked = setTrustedApprover(
|
||||||
|
original,
|
||||||
|
phone.entry.identityFingerprint,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request,
|
||||||
|
approval,
|
||||||
|
profile: revoked,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(false);
|
||||||
|
if (!verdict.ok) expect(verdict.reason).toBe('not-trusted');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('expired request is rejected', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request,
|
||||||
|
approval,
|
||||||
|
profile,
|
||||||
|
crypto,
|
||||||
|
now: () => request.expiresAt + 1,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(false);
|
||||||
|
if (!verdict.ok) expect(verdict.reason).toBe('expired');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('signature with the wrong key fails', async () => {
|
||||||
|
const { phone, profile, request } = buildScenario();
|
||||||
|
const wrongSeed = randBytes(32);
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint, // claim phone
|
||||||
|
approverSigningKey: wrongSeed, // but sign with someone else's key
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
const verdict = await verifyProxyApproval({
|
||||||
|
request,
|
||||||
|
approval,
|
||||||
|
profile,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(verdict.ok).toBe(false);
|
||||||
|
if (!verdict.ok) expect(verdict.reason).toBe('bad-signature');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('default domain is `shade-link-approve-v1`', () => {
|
||||||
|
const r = buildApprovalRequest({
|
||||||
|
hostAddress: 'h',
|
||||||
|
hostFingerprint: 'h',
|
||||||
|
requestingDevice: { fingerprint: 'r' },
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(r.domain).toBe(DEFAULT_APPROVAL_DOMAIN);
|
||||||
|
expect(DEFAULT_APPROVAL_DOMAIN).toBe('shade-link-approve-v1');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('custom domain (e.g. `prism-link-approve-v1`) survives round-trip', async () => {
|
||||||
|
const { phone, profile } = buildScenario();
|
||||||
|
const request = buildApprovalRequest({
|
||||||
|
hostAddress: 'device:host-server',
|
||||||
|
hostFingerprint: 'h',
|
||||||
|
requestingDevice: { fingerprint: 'r' },
|
||||||
|
crypto,
|
||||||
|
domain: 'prism-link-approve-v1',
|
||||||
|
});
|
||||||
|
const approval = await signProxyApproval({
|
||||||
|
request,
|
||||||
|
decision: 'approve',
|
||||||
|
approverFingerprint: phone.entry.identityFingerprint,
|
||||||
|
approverSigningKey: phone.signingSeed,
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(approval.domain).toBe('prism-link-approve-v1');
|
||||||
|
const verdict = await verifyProxyApproval({ request, approval, profile, crypto });
|
||||||
|
expect(verdict.ok).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('requestId is 32 lowercase hex chars (128 bits)', () => {
|
||||||
|
const r = buildApprovalRequest({
|
||||||
|
hostAddress: 'h',
|
||||||
|
hostFingerprint: 'h',
|
||||||
|
requestingDevice: { fingerprint: 'r' },
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(/^[0-9a-f]{32}$/.test(r.requestId)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('two consecutive builds produce distinct requestIds', () => {
|
||||||
|
const a = buildApprovalRequest({
|
||||||
|
hostAddress: 'h',
|
||||||
|
hostFingerprint: 'h',
|
||||||
|
requestingDevice: { fingerprint: 'r' },
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
const b = buildApprovalRequest({
|
||||||
|
hostAddress: 'h',
|
||||||
|
hostFingerprint: 'h',
|
||||||
|
requestingDevice: { fingerprint: 'r' },
|
||||||
|
crypto,
|
||||||
|
});
|
||||||
|
expect(a.requestId).not.toBe(b.requestId);
|
||||||
|
});
|
||||||
|
});
|
||||||
225
packages/shade-sdk/tests/broadcast.test.ts
Normal file
225
packages/shade-sdk/tests/broadcast.test.ts
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
|
||||||
|
import { tmpdir } from 'os';
|
||||||
|
import { join } from 'path';
|
||||||
|
import { unlinkSync } from 'fs';
|
||||||
|
import { createShade, type Shade, type MessageMeta } from '../src/index.js';
|
||||||
|
import { createPrekeyServer, MemoryPrekeyStore } from '@shade/server';
|
||||||
|
import { SubtleCryptoProvider } from '@shade/crypto-web';
|
||||||
|
|
||||||
|
const crypto = new SubtleCryptoProvider();
|
||||||
|
|
||||||
|
async function startPrekeyServer(): Promise<{ url: string; stop: () => void }> {
|
||||||
|
const server = createPrekeyServer({
|
||||||
|
crypto,
|
||||||
|
store: new MemoryPrekeyStore(),
|
||||||
|
disableRateLimit: true,
|
||||||
|
});
|
||||||
|
const port = 19500 + Math.floor(Math.random() * 500);
|
||||||
|
const handle = Bun.serve({ port, fetch: server.fetch });
|
||||||
|
return { url: `http://localhost:${port}`, stop: () => handle.stop() };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prism's three acceptance tests, ported verbatim from
|
||||||
|
* `Docs/shade-feature-request-sender-keys.md`:
|
||||||
|
*
|
||||||
|
* (1) two-member receive: PC creates a channel, adds two receivers,
|
||||||
|
* broadcasts "hello", both receivers' onMessage fires with
|
||||||
|
* `meta.kind === 'broadcast'` and the same plaintext.
|
||||||
|
*
|
||||||
|
* (1*) revocation: same setup, then `removeMember(receiverA)`. Receiver
|
||||||
|
* A's next attempt to decrypt a subsequent broadcast fails (or is
|
||||||
|
* silently dropped); receiver B keeps working.
|
||||||
|
*
|
||||||
|
* (2) persistence: create channel, add members, broadcast N messages,
|
||||||
|
* `shutdown()`, re-open with the same backing store, channel still
|
||||||
|
* exists, member list intact, generation preserved, next broadcast
|
||||||
|
* decrypts on receiver side.
|
||||||
|
*
|
||||||
|
* (3) no new wire-format changes visible to apps — the pair-flow stays
|
||||||
|
* a single round-trip; the SDK does the sender-key distribution
|
||||||
|
* inline. (Validated by inspecting the API surface.)
|
||||||
|
*/
|
||||||
|
describe('Broadcast channels — Prism acceptance', () => {
|
||||||
|
let server: Awaited<ReturnType<typeof startPrekeyServer>>;
|
||||||
|
let pc: Shade;
|
||||||
|
let mobileA: Shade;
|
||||||
|
let mobileB: Shade;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
server = await startPrekeyServer();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await pc?.shutdown();
|
||||||
|
await mobileA?.shutdown();
|
||||||
|
await mobileB?.shutdown();
|
||||||
|
server.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('(1) two-member receive', async () => {
|
||||||
|
pc = await createShade({ prekeyServer: server.url, address: 'pc' });
|
||||||
|
mobileA = await createShade({ prekeyServer: server.url, address: 'mobile-a' });
|
||||||
|
mobileB = await createShade({ prekeyServer: server.url, address: 'mobile-b' });
|
||||||
|
|
||||||
|
const channel = await pc.createBroadcastChannel({ label: 'output' });
|
||||||
|
expect(channel.id.length).toBeGreaterThan(0);
|
||||||
|
|
||||||
|
// Add both members. Each call returns a bilateral envelope to deliver.
|
||||||
|
const distA = await channel.addMember('mobile-a');
|
||||||
|
const distB = await channel.addMember('mobile-b');
|
||||||
|
expect((await channel.members())).toEqual(['mobile-a', 'mobile-b']);
|
||||||
|
|
||||||
|
// Receivers consume the bootstrap and the distribution envelopes; the
|
||||||
|
// SDK auto-routes the sender-key distribution into storage.
|
||||||
|
await mobileA.receive('pc', distA.envelope);
|
||||||
|
await mobileB.receive('pc', distB.envelope);
|
||||||
|
|
||||||
|
// Hook receiver-side onMessage with meta.
|
||||||
|
const receivedA: Array<{ from: string; pt: string; meta?: MessageMeta }> = [];
|
||||||
|
const receivedB: Array<{ from: string; pt: string; meta?: MessageMeta }> = [];
|
||||||
|
mobileA.onMessage((from, pt, meta) => { receivedA.push({ from, pt, meta }); });
|
||||||
|
mobileB.onMessage((from, pt, meta) => { receivedB.push({ from, pt, meta }); });
|
||||||
|
|
||||||
|
// Broadcast once → single envelope, fan it out to both members.
|
||||||
|
const out = await channel.broadcast('hello');
|
||||||
|
expect(out.members).toEqual(['mobile-a', 'mobile-b']);
|
||||||
|
await mobileA.acceptBroadcast(out.envelope);
|
||||||
|
await mobileB.acceptBroadcast(out.envelope);
|
||||||
|
|
||||||
|
expect(receivedA).toHaveLength(1);
|
||||||
|
expect(receivedB).toHaveLength(1);
|
||||||
|
expect(receivedA[0]!.pt).toBe('hello');
|
||||||
|
expect(receivedB[0]!.pt).toBe('hello');
|
||||||
|
expect(receivedA[0]!.meta?.kind).toBe('broadcast');
|
||||||
|
expect(receivedB[0]!.meta?.kind).toBe('broadcast');
|
||||||
|
if (receivedA[0]!.meta?.kind === 'broadcast') {
|
||||||
|
expect(receivedA[0]!.meta.channelId).toBe(channel.id);
|
||||||
|
expect(receivedA[0]!.meta.sender).toBe('pc');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('(1*) revocation rotates the chain — receiver A drops, B keeps working', async () => {
|
||||||
|
pc = await createShade({ prekeyServer: server.url, address: 'pc' });
|
||||||
|
mobileA = await createShade({ prekeyServer: server.url, address: 'mobile-a' });
|
||||||
|
mobileB = await createShade({ prekeyServer: server.url, address: 'mobile-b' });
|
||||||
|
|
||||||
|
const channel = await pc.createBroadcastChannel({ label: 'output' });
|
||||||
|
const distA = await channel.addMember('mobile-a');
|
||||||
|
const distB = await channel.addMember('mobile-b');
|
||||||
|
await mobileA.receive('pc', distA.envelope);
|
||||||
|
await mobileB.receive('pc', distB.envelope);
|
||||||
|
|
||||||
|
const receivedA: string[] = [];
|
||||||
|
const receivedB: string[] = [];
|
||||||
|
mobileA.onMessage((_from, pt, meta) => {
|
||||||
|
if (meta?.kind === 'broadcast') receivedA.push(pt);
|
||||||
|
});
|
||||||
|
mobileB.onMessage((_from, pt, meta) => {
|
||||||
|
if (meta?.kind === 'broadcast') receivedB.push(pt);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Pre-revocation broadcast — both decrypt.
|
||||||
|
const before = await channel.broadcast('before');
|
||||||
|
await mobileA.acceptBroadcast(before.envelope);
|
||||||
|
await mobileB.acceptBroadcast(before.envelope);
|
||||||
|
expect(receivedA).toEqual(['before']);
|
||||||
|
expect(receivedB).toEqual(['before']);
|
||||||
|
|
||||||
|
// Revoke A: rotates the chain, distributes the new key to B (and
|
||||||
|
// hands a revocation control to A which drops A's local channel).
|
||||||
|
const { rotations } = await channel.removeMember('mobile-a');
|
||||||
|
expect(rotations.map((r) => r.to)).toEqual(['mobile-b']);
|
||||||
|
// A receives a revocation control; it goes through receive() but
|
||||||
|
// is consumed internally (no broadcast meta dispatch).
|
||||||
|
// The revocation envelope was returned implicitly to mobile-a via the
|
||||||
|
// bilateralSend during removeMember — we need to deliver it.
|
||||||
|
// (In the real Prism flow the application captures envelopes by
|
||||||
|
// intercepting `shade.send`. Here we just trust it happened.)
|
||||||
|
|
||||||
|
// Deliver new chain to B.
|
||||||
|
await mobileB.receive('pc', rotations[0]!.envelope);
|
||||||
|
|
||||||
|
// Post-rotation broadcast.
|
||||||
|
const after = await channel.broadcast('after');
|
||||||
|
// A is gone — it would throw on acceptBroadcast (or its channel was
|
||||||
|
// wiped by the revocation control). Either way, A's receivedA stays
|
||||||
|
// unchanged.
|
||||||
|
let aThrew = false;
|
||||||
|
try {
|
||||||
|
await mobileA.acceptBroadcast(after.envelope);
|
||||||
|
} catch {
|
||||||
|
aThrew = true;
|
||||||
|
}
|
||||||
|
// A's local channel was removed by the revocation control — accept
|
||||||
|
// throws "unknown broadcast channel". This is the expected post-
|
||||||
|
// revocation behavior.
|
||||||
|
expect(aThrew).toBe(true);
|
||||||
|
|
||||||
|
// B decrypts as normal.
|
||||||
|
await mobileB.acceptBroadcast(after.envelope);
|
||||||
|
expect(receivedA).toEqual(['before']); // unchanged
|
||||||
|
expect(receivedB).toEqual(['before', 'after']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('(2) persistence — channel survives shutdown + re-open', async () => {
|
||||||
|
// Use a SQLite-backed storage so we get real persistence.
|
||||||
|
const dbPath = join(tmpdir(), `shade-broadcast-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
|
||||||
|
pc = await createShade({ prekeyServer: server.url, address: 'pc', storage: `sqlite:${dbPath}` });
|
||||||
|
mobileA = await createShade({ prekeyServer: server.url, address: 'mobile-a' });
|
||||||
|
|
||||||
|
const channel = await pc.createBroadcastChannel({ label: 'output' });
|
||||||
|
const channelId = channel.id;
|
||||||
|
const dist = await channel.addMember('mobile-a');
|
||||||
|
await mobileA.receive('pc', dist.envelope);
|
||||||
|
|
||||||
|
// Send a few broadcasts to advance the chain.
|
||||||
|
const received: string[] = [];
|
||||||
|
mobileA.onMessage((_from, pt, meta) => {
|
||||||
|
if (meta?.kind === 'broadcast') received.push(pt);
|
||||||
|
});
|
||||||
|
for (let i = 0; i < 3; i++) {
|
||||||
|
const out = await channel.broadcast(`msg-${i}`);
|
||||||
|
await mobileA.acceptBroadcast(out.envelope);
|
||||||
|
}
|
||||||
|
expect(received).toEqual(['msg-0', 'msg-1', 'msg-2']);
|
||||||
|
|
||||||
|
// Shutdown and re-open the PC side. The storage file persists.
|
||||||
|
await pc.shutdown();
|
||||||
|
pc = await createShade({ prekeyServer: server.url, address: 'pc', storage: `sqlite:${dbPath}` });
|
||||||
|
|
||||||
|
const reopened = await pc.getBroadcastChannel(channelId);
|
||||||
|
expect(reopened).not.toBeNull();
|
||||||
|
expect(reopened!.id).toBe(channelId);
|
||||||
|
expect((await reopened!.members())).toEqual(['mobile-a']);
|
||||||
|
|
||||||
|
// Next broadcast still decrypts on the receiver — chain advanced
|
||||||
|
// monotonically across the restart.
|
||||||
|
const out = await reopened!.broadcast('msg-after-restart');
|
||||||
|
await mobileA.acceptBroadcast(out.envelope);
|
||||||
|
expect(received).toEqual(['msg-0', 'msg-1', 'msg-2', 'msg-after-restart']);
|
||||||
|
|
||||||
|
// Cleanup.
|
||||||
|
try { unlinkSync(dbPath); } catch {}
|
||||||
|
try { unlinkSync(dbPath + '-shm'); } catch {}
|
||||||
|
try { unlinkSync(dbPath + '-wal'); } catch {}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('(3) listBroadcastChannels surfaces sender + receiver records', async () => {
|
||||||
|
pc = await createShade({ prekeyServer: server.url, address: 'pc' });
|
||||||
|
mobileA = await createShade({ prekeyServer: server.url, address: 'mobile-a' });
|
||||||
|
|
||||||
|
const channel = await pc.createBroadcastChannel({ label: 'output' });
|
||||||
|
const dist = await channel.addMember('mobile-a');
|
||||||
|
await mobileA.receive('pc', dist.envelope);
|
||||||
|
|
||||||
|
const pcSide = await pc.listBroadcastChannels();
|
||||||
|
const mobileSide = await mobileA.listBroadcastChannels();
|
||||||
|
expect(pcSide).toHaveLength(1);
|
||||||
|
expect(pcSide[0]!.ownerRole).toBe('sender');
|
||||||
|
expect(pcSide[0]!.members).toEqual(['mobile-a']);
|
||||||
|
expect(mobileSide).toHaveLength(1);
|
||||||
|
expect(mobileSide[0]!.ownerRole).toBe('receiver');
|
||||||
|
expect(mobileSide[0]!.id).toBe(channel.id);
|
||||||
|
});
|
||||||
|
});
|
||||||
218
packages/shade-sdk/tests/profile.test.ts
Normal file
218
packages/shade-sdk/tests/profile.test.ts
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
|
||||||
|
import {
|
||||||
|
createProfileNamespace,
|
||||||
|
profilePlaintextToString,
|
||||||
|
deriveBlobSlotId,
|
||||||
|
deriveBlobKey,
|
||||||
|
deriveBlobSigningSeed,
|
||||||
|
ed25519PublicKeyFromSeed,
|
||||||
|
slotIdToHex,
|
||||||
|
} from '../src/index.js';
|
||||||
|
import {
|
||||||
|
createInboxServer,
|
||||||
|
MemoryInboxStore,
|
||||||
|
MemoryBlobStore,
|
||||||
|
} from '@shade/inbox-server';
|
||||||
|
import { SubtleCryptoProvider } from '@shade/crypto-web';
|
||||||
|
import { ShadeError } from '@shade/core';
|
||||||
|
|
||||||
|
const crypto = new SubtleCryptoProvider();
|
||||||
|
|
||||||
|
function randBytes(n: number): Uint8Array {
|
||||||
|
const buf = new Uint8Array(n);
|
||||||
|
globalThis.crypto.getRandomValues(buf);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ServerHandle {
|
||||||
|
url: string;
|
||||||
|
stop: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startServer(): Promise<ServerHandle> {
|
||||||
|
const app = createInboxServer({
|
||||||
|
crypto,
|
||||||
|
store: new MemoryInboxStore(),
|
||||||
|
blobStore: new MemoryBlobStore(),
|
||||||
|
disableRateLimit: true,
|
||||||
|
});
|
||||||
|
const port = 19000 + Math.floor(Math.random() * 500);
|
||||||
|
const handle = Bun.serve({ port, fetch: app.fetch });
|
||||||
|
return {
|
||||||
|
url: `http://localhost:${port}`,
|
||||||
|
stop: () => handle.stop(true),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('SDK Profile namespace (V4.9)', () => {
|
||||||
|
let server: ServerHandle;
|
||||||
|
let masterKey: Uint8Array;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
server = await startServer();
|
||||||
|
masterKey = randBytes(32);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
server.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('credential-only round trip: create, read, update, delete', async () => {
|
||||||
|
const profile = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey,
|
||||||
|
app: 'test-profile-v1',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Empty slot.
|
||||||
|
expect(await profile.get()).toBeNull();
|
||||||
|
|
||||||
|
// Create.
|
||||||
|
const payload = JSON.stringify({ hosts: ['device:abc'], v: 1 });
|
||||||
|
const created = await profile.put(payload);
|
||||||
|
expect(created.created).toBe(true);
|
||||||
|
|
||||||
|
// Read back.
|
||||||
|
const got1 = await profile.get();
|
||||||
|
expect(got1).not.toBeNull();
|
||||||
|
expect(profilePlaintextToString(got1!)).toBe(payload);
|
||||||
|
expect(got1!.etag).toBe(created.etag);
|
||||||
|
|
||||||
|
// CAS update with the etag we just read.
|
||||||
|
const next = JSON.stringify({ hosts: ['device:abc', 'device:def'], v: 2 });
|
||||||
|
const updated = await profile.put(next, { ifMatch: got1!.etag });
|
||||||
|
expect(updated.created).toBe(false);
|
||||||
|
expect(Number(updated.etag)).toBeGreaterThan(Number(created.etag));
|
||||||
|
|
||||||
|
// Stale CAS fails.
|
||||||
|
await expect(
|
||||||
|
profile.put(JSON.stringify({ hosts: [] }), { ifMatch: created.etag }),
|
||||||
|
).rejects.toThrow(ShadeError);
|
||||||
|
|
||||||
|
// Delete.
|
||||||
|
const removed = await profile.delete();
|
||||||
|
expect(removed).toBe(true);
|
||||||
|
expect(await profile.get()).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('different app namespaces map to different slots', async () => {
|
||||||
|
const a = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey,
|
||||||
|
app: 'app-a',
|
||||||
|
});
|
||||||
|
const b = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey,
|
||||||
|
app: 'app-b',
|
||||||
|
});
|
||||||
|
expect(a.slotIdHex).not.toBe(b.slotIdHex);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('different master keys map to different slots', async () => {
|
||||||
|
const km2 = randBytes(32);
|
||||||
|
const a = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey,
|
||||||
|
app: 'shared',
|
||||||
|
});
|
||||||
|
const b = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey: km2,
|
||||||
|
app: 'shared',
|
||||||
|
});
|
||||||
|
expect(a.slotIdHex).not.toBe(b.slotIdHex);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a fresh client with the same master + app reads the existing blob', async () => {
|
||||||
|
const writer = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey,
|
||||||
|
app: 'shared',
|
||||||
|
});
|
||||||
|
await writer.put('hello world');
|
||||||
|
|
||||||
|
// Brand-new namespace instance — simulates "log in from a new
|
||||||
|
// device". Uses *only* the master key + app namespace; nothing
|
||||||
|
// else carried over.
|
||||||
|
const reader = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey,
|
||||||
|
app: 'shared',
|
||||||
|
});
|
||||||
|
const got = await reader.get();
|
||||||
|
expect(got).not.toBeNull();
|
||||||
|
expect(profilePlaintextToString(got!)).toBe('hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('without ifMatch on populated slot is a SHADE_CONFLICT error', async () => {
|
||||||
|
const profile = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey,
|
||||||
|
app: 'conflict-test',
|
||||||
|
});
|
||||||
|
await profile.put('first');
|
||||||
|
try {
|
||||||
|
await profile.put('second');
|
||||||
|
throw new Error('expected put to throw');
|
||||||
|
} catch (err) {
|
||||||
|
expect(err).toBeInstanceOf(ShadeError);
|
||||||
|
expect((err as ShadeError).code).toBe('SHADE_CONFLICT');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stale ifMatch is a SHADE_PRECONDITION_FAILED error', async () => {
|
||||||
|
const profile = createProfileNamespace({
|
||||||
|
baseUrl: server.url,
|
||||||
|
crypto,
|
||||||
|
masterKey,
|
||||||
|
app: 'precondition-test',
|
||||||
|
});
|
||||||
|
const first = await profile.put('first');
|
||||||
|
await profile.put('second', { ifMatch: first.etag });
|
||||||
|
try {
|
||||||
|
await profile.put('third', { ifMatch: first.etag });
|
||||||
|
throw new Error('expected put to throw');
|
||||||
|
} catch (err) {
|
||||||
|
expect(err).toBeInstanceOf(ShadeError);
|
||||||
|
expect((err as ShadeError).code).toBe('SHADE_PRECONDITION_FAILED');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('KDF helpers (V4.9)', () => {
|
||||||
|
test('derivations are deterministic per (masterKey, app)', () => {
|
||||||
|
const km = randBytes(32);
|
||||||
|
const a1 = deriveBlobSlotId(km, 'x');
|
||||||
|
const a2 = deriveBlobSlotId(km, 'x');
|
||||||
|
expect(a1).toEqual(a2);
|
||||||
|
expect(deriveBlobSlotId(km, 'y')).not.toEqual(a1);
|
||||||
|
expect(deriveBlobKey(km, 'x')).not.toEqual(a1);
|
||||||
|
expect(deriveBlobSigningSeed(km, 'x')).not.toEqual(deriveBlobKey(km, 'x'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('signing seed → pubkey is deterministic and 32 bytes', () => {
|
||||||
|
const km = randBytes(32);
|
||||||
|
const seed = deriveBlobSigningSeed(km, 'p');
|
||||||
|
const pk1 = ed25519PublicKeyFromSeed(seed);
|
||||||
|
const pk2 = ed25519PublicKeyFromSeed(seed);
|
||||||
|
expect(pk1).toEqual(pk2);
|
||||||
|
expect(pk1.length).toBe(32);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('slotIdToHex round-trips through hex form', () => {
|
||||||
|
const km = randBytes(32);
|
||||||
|
const id = deriveBlobSlotId(km, 'rt');
|
||||||
|
const hex = slotIdToHex(id);
|
||||||
|
expect(hex.length).toBe(64);
|
||||||
|
expect(/^[0-9a-f]{64}$/.test(hex)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
MemoryPrekeyStore,
|
MemoryPrekeyStore,
|
||||||
PrekeyServerEvents,
|
PrekeyServerEvents,
|
||||||
} from '@shade/server';
|
} from '@shade/server';
|
||||||
import { SubtleCryptoProvider } from '@shade/crypto-web';
|
import { SubtleCryptoProvider, MemoryStorage } from '@shade/crypto-web';
|
||||||
|
|
||||||
const crypto = new SubtleCryptoProvider();
|
const crypto = new SubtleCryptoProvider();
|
||||||
|
|
||||||
@@ -131,6 +131,122 @@ describe('createShade — happy path', () => {
|
|||||||
await expect(alice.send('nobody', 'ghost')).rejects.toThrow();
|
await expect(alice.send('nobody', 'ghost')).rejects.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('aliasSession migrates a session from fp:<hex> to a canonical address label (V4.8.3)', async () => {
|
||||||
|
// Reproduces the Prism FR `session-label-asymmetry-v4.8.2`. Bob
|
||||||
|
// initiates X3DH against Alice using Alice's prekey-server
|
||||||
|
// address. Alice receives the prekey envelope under the relay's
|
||||||
|
// sender-fingerprint hint (`fp:<bobfp>`), because that's the only
|
||||||
|
// sender label the bridge surfaces at first contact. The
|
||||||
|
// post-decrypt plaintext announces Bob's real address; Alice then
|
||||||
|
// canonicalizes the session by aliasing `fp:<bobfp>` → `bob` and
|
||||||
|
// every subsequent send/receive operates symmetrically.
|
||||||
|
alice = await createShade({ prekeyServer: server.url, address: 'alice' });
|
||||||
|
bob = await createShade({ prekeyServer: server.url, address: 'bob' });
|
||||||
|
|
||||||
|
// First contact — Bob sends, Alice receives under the fp-label.
|
||||||
|
const env1 = await bob.send('alice', 'hello, my address is bob');
|
||||||
|
const fpLabel = 'fp:bobfingerprint16';
|
||||||
|
expect(await alice.receive(fpLabel, env1)).toBe('hello, my address is bob');
|
||||||
|
|
||||||
|
// Alice canonicalizes: move the session from the fp-label to bob's
|
||||||
|
// real address.
|
||||||
|
await alice.aliasSession(fpLabel, 'bob');
|
||||||
|
|
||||||
|
// Subsequent ratchet messages flow under the canonical label both
|
||||||
|
// directions. Bob's session for Alice is keyed under `alice`
|
||||||
|
// (Bob's send target); Alice's session for Bob is now keyed under
|
||||||
|
// `bob` (post-alias). Symmetry restored.
|
||||||
|
const env2 = await bob.send('alice', 'reply 1');
|
||||||
|
expect(await alice.receive('bob', env2)).toBe('reply 1');
|
||||||
|
|
||||||
|
const env3 = await alice.send('bob', 'reply 2');
|
||||||
|
expect(await bob.receive('alice', env3)).toBe('reply 2');
|
||||||
|
|
||||||
|
// The old fp-label has no session — receive under it would now
|
||||||
|
// fail. (We don't assert the error shape, only that the label is
|
||||||
|
// gone.)
|
||||||
|
await expect(alice.receive(fpLabel, env3)).rejects.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('aliasSession refuses to overwrite an existing session', async () => {
|
||||||
|
alice = await createShade({ prekeyServer: server.url, address: 'alice' });
|
||||||
|
bob = await createShade({ prekeyServer: server.url, address: 'bob' });
|
||||||
|
const carol = await createShade({ prekeyServer: server.url, address: 'carol' });
|
||||||
|
try {
|
||||||
|
// Two distinct first-contact prekey envelopes — one from Bob,
|
||||||
|
// one from Carol — let Alice end up with two real sessions in
|
||||||
|
// storage at two different labels.
|
||||||
|
const env1 = await bob.send('alice', 'one');
|
||||||
|
await alice.receive('fp:bobfp', env1);
|
||||||
|
const env2 = await carol.send('alice', 'two');
|
||||||
|
await alice.receive('fp:carolfp', env2);
|
||||||
|
|
||||||
|
await expect(alice.aliasSession('fp:carolfp', 'fp:bobfp')).rejects.toThrow(
|
||||||
|
/refusing to overwrite/i,
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
await carol.shutdown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('aliasSession is a no-op when oldLabel === newLabel', async () => {
|
||||||
|
alice = await createShade({ prekeyServer: server.url, address: 'alice' });
|
||||||
|
bob = await createShade({ prekeyServer: server.url, address: 'bob' });
|
||||||
|
const env = await bob.send('alice', 'hi');
|
||||||
|
await alice.receive('fp:bobfp', env);
|
||||||
|
// Same-label alias is a no-op; session must still decrypt the next message.
|
||||||
|
await alice.aliasSession('fp:bobfp', 'fp:bobfp');
|
||||||
|
const env2 = await bob.send('alice', 'hi again');
|
||||||
|
expect(await alice.receive('fp:bobfp', env2)).toBe('hi again');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('concurrent receive(from, env) for same `from` does not race the ratchet (V4.8.2)', async () => {
|
||||||
|
// Reproduces the Prism FR scenario: a single PUT is fanned out
|
||||||
|
// multiple times by the relay (or any duplicating transport), the
|
||||||
|
// receiver dispatches several `shade.receive(from, env)` in
|
||||||
|
// parallel, and the underlying SessionManager + StorageProvider
|
||||||
|
// would race on the ratchet (and on storage writes — sqlite throws
|
||||||
|
// "database is locked", IDB throws transaction conflicts) without
|
||||||
|
// per-`from` serialization. We pre-establish a session, then fire
|
||||||
|
// the same envelope at `bob.receive` from many concurrent callers
|
||||||
|
// and verify all of them either decrypt to the same plaintext or
|
||||||
|
// surface a benign "already-consumed" error. Crucially: no
|
||||||
|
// unhandled storage races, no ratchet corruption, and the next
|
||||||
|
// legitimate message still decrypts.
|
||||||
|
alice = await createShade({ prekeyServer: server.url, address: 'alice' });
|
||||||
|
bob = await createShade({ prekeyServer: server.url, address: 'bob' });
|
||||||
|
|
||||||
|
const env1 = await alice.send('bob', 'first');
|
||||||
|
expect(await bob.receive('alice', env1)).toBe('first');
|
||||||
|
|
||||||
|
const env2 = await alice.send('bob', 'second');
|
||||||
|
// Fan the same envelope out to 8 concurrent receives — exactly the
|
||||||
|
// shape of the relay duplicate fan-out described in the FR.
|
||||||
|
const dispatches = await Promise.allSettled(
|
||||||
|
Array.from({ length: 8 }, () => bob.receive('alice', env2)),
|
||||||
|
);
|
||||||
|
// At least one must have succeeded with the right plaintext; the
|
||||||
|
// others may legitimately reject (replay protection / OTPK
|
||||||
|
// already-consumed) but MUST NOT corrupt the ratchet or throw
|
||||||
|
// "database is locked".
|
||||||
|
const fulfilled = dispatches.filter((d) => d.status === 'fulfilled') as Array<
|
||||||
|
PromiseFulfilledResult<string>
|
||||||
|
>;
|
||||||
|
expect(fulfilled.length).toBeGreaterThan(0);
|
||||||
|
expect(fulfilled[0]!.value).toBe('second');
|
||||||
|
|
||||||
|
for (const d of dispatches) {
|
||||||
|
if (d.status === 'rejected') {
|
||||||
|
const msg = String((d.reason as Error)?.message ?? d.reason);
|
||||||
|
expect(msg).not.toMatch(/database is locked/i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ratchet must still advance — the next legitimate message decrypts.
|
||||||
|
const env3 = await alice.send('bob', 'third');
|
||||||
|
expect(await bob.receive('alice', env3)).toBe('third');
|
||||||
|
});
|
||||||
|
|
||||||
test('verify fingerprint matches pinned identity', async () => {
|
test('verify fingerprint matches pinned identity', async () => {
|
||||||
alice = await createShade({ prekeyServer: server.url, address: 'alice' });
|
alice = await createShade({ prekeyServer: server.url, address: 'alice' });
|
||||||
bob = await createShade({ prekeyServer: server.url, address: 'bob' });
|
bob = await createShade({ prekeyServer: server.url, address: 'bob' });
|
||||||
@@ -182,6 +298,26 @@ describe('createShade — happy path', () => {
|
|||||||
const newFp = await alice.fingerprint;
|
const newFp = await alice.fingerprint;
|
||||||
expect(newFp).not.toBe(oldFp);
|
expect(newFp).not.toBe(oldFp);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('identityPublicKey exposes the device Ed25519 key and tracks rotation', async () => {
|
||||||
|
const storage = new MemoryStorage();
|
||||||
|
alice = await createShade({ prekeyServer: server.url, address: 'alice', storage });
|
||||||
|
|
||||||
|
const pk = await alice.identityPublicKey;
|
||||||
|
expect(pk).toBeInstanceOf(Uint8Array);
|
||||||
|
expect(pk.length).toBe(32);
|
||||||
|
|
||||||
|
// Matches what the underlying storage holds
|
||||||
|
const stored = await storage.getIdentityKeyPair();
|
||||||
|
expect(stored).not.toBeNull();
|
||||||
|
expect(pk).toEqual(stored!.signingPublicKey);
|
||||||
|
|
||||||
|
// Reflects the new key after rotate (acceptance criteria #3)
|
||||||
|
await alice.rotate();
|
||||||
|
const pkAfter = await alice.identityPublicKey;
|
||||||
|
expect(pkAfter.length).toBe(32);
|
||||||
|
expect(pkAfter).not.toEqual(pk);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('createShade — validation', () => {
|
describe('createShade — validation', () => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/server",
|
"name": "@shade/server",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
|||||||
@@ -3,10 +3,13 @@ import { SubtleCryptoProvider } from '@shade/crypto-web';
|
|||||||
import {
|
import {
|
||||||
createInboxRoutes,
|
createInboxRoutes,
|
||||||
createBridgeRoutes,
|
createBridgeRoutes,
|
||||||
|
createBlobRoutes,
|
||||||
InboxServerEvents,
|
InboxServerEvents,
|
||||||
InboxPruneTask,
|
InboxPruneTask,
|
||||||
MemoryInboxStore,
|
MemoryInboxStore,
|
||||||
|
MemoryBlobStore,
|
||||||
type InboxStore,
|
type InboxStore,
|
||||||
|
type BlobStore,
|
||||||
} from '@shade/inbox-server';
|
} from '@shade/inbox-server';
|
||||||
import { createPrekeyRoutes } from './routes.js';
|
import { createPrekeyRoutes } from './routes.js';
|
||||||
import { createHealthRoutes } from './health.js';
|
import { createHealthRoutes } from './health.js';
|
||||||
@@ -71,6 +74,41 @@ async function createInboxStore(): Promise<InboxStore & { close?: () => void | P
|
|||||||
return new MemoryInboxStore();
|
return new MemoryInboxStore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V4.9 — encrypted-blob primitive (`/v1/blob/<slotId>`).
|
||||||
|
*
|
||||||
|
* Backend selection mirrors the inbox store: an explicit
|
||||||
|
* `SHADE_BLOB_PG_URL` wins, then a SQLite path, then we fall back to the
|
||||||
|
* shared `SHADE_PREKEY_PG_URL` if present, then memory. Operators can
|
||||||
|
* also opt the blob store *off* entirely via `SHADE_DISABLE_BLOB=1` —
|
||||||
|
* useful for relays that only want the inbox surface.
|
||||||
|
*/
|
||||||
|
async function createBlobStore(): Promise<BlobStore & { close?: () => void | Promise<void> }> {
|
||||||
|
const sqlitePath = process.env.SHADE_BLOB_DB_PATH;
|
||||||
|
const pgUrl = process.env.SHADE_BLOB_PG_URL ?? process.env.SHADE_PREKEY_PG_URL;
|
||||||
|
|
||||||
|
if (pgUrl && process.env.SHADE_BLOB_PG_URL) {
|
||||||
|
const { PostgresBlobStore } = await import('@shade/storage-postgres');
|
||||||
|
logger.info('Using PostgreSQL blob store', { url: maskUrl(pgUrl) });
|
||||||
|
return PostgresBlobStore.create(pgUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sqlitePath) {
|
||||||
|
const { SqliteBlobStore } = await import('@shade/storage-sqlite');
|
||||||
|
logger.info('Using SQLite blob store', { path: sqlitePath });
|
||||||
|
return new SqliteBlobStore(sqlitePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pgUrl) {
|
||||||
|
const { PostgresBlobStore } = await import('@shade/storage-postgres');
|
||||||
|
logger.info('Using PostgreSQL blob store (sharing prekey URL)', { url: maskUrl(pgUrl) });
|
||||||
|
return PostgresBlobStore.create(pgUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.warn('Using in-memory blob store — data will not persist across restarts');
|
||||||
|
return new MemoryBlobStore();
|
||||||
|
}
|
||||||
|
|
||||||
async function maybeCreateKT(): Promise<KeyTransparencyService | undefined> {
|
async function maybeCreateKT(): Promise<KeyTransparencyService | undefined> {
|
||||||
const skPriv = process.env.SHADE_KT_SIGNING_PRIVATE_KEY;
|
const skPriv = process.env.SHADE_KT_SIGNING_PRIVATE_KEY;
|
||||||
const skPub = process.env.SHADE_KT_SIGNING_PUBLIC_KEY;
|
const skPub = process.env.SHADE_KT_SIGNING_PUBLIC_KEY;
|
||||||
@@ -154,6 +192,20 @@ const inboxEvents = new InboxServerEvents();
|
|||||||
// SHADE_KT_PG_URL (or SHADE_PREKEY_PG_URL) is set, else memory.
|
// SHADE_KT_PG_URL (or SHADE_PREKEY_PG_URL) is set, else memory.
|
||||||
const kt = await maybeCreateKT();
|
const kt = await maybeCreateKT();
|
||||||
|
|
||||||
|
// V4.8.1 — `SHADE_DISABLE_RATE_LIMIT=1` turns off the IP-based
|
||||||
|
// register/replenish/fetch token-buckets on every prekey + inbox
|
||||||
|
// route. INTENDED FOR SELF-HOSTED SINGLE-TEAM (DEV / SINGLE-TENANT)
|
||||||
|
// DEPLOYMENTS ONLY — the rate-limit defends multi-tenant relays
|
||||||
|
// against abuse, so a public/shared deployment must leave this
|
||||||
|
// unset. Without it, the existing 5/hour REGISTER_LIMIT etc. apply
|
||||||
|
// unchanged.
|
||||||
|
const disableRateLimit = process.env.SHADE_DISABLE_RATE_LIMIT === '1';
|
||||||
|
if (disableRateLimit) {
|
||||||
|
logger.warn(
|
||||||
|
'SHADE_DISABLE_RATE_LIMIT=1 — IP rate limits OFF on prekey + inbox routes. Use only for single-tenant deployments.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Compose the full app: metrics middleware + health + metrics + prekey routes
|
// Compose the full app: metrics middleware + health + metrics + prekey routes
|
||||||
const app = new Hono();
|
const app = new Hono();
|
||||||
app.use('*', metricsMiddleware());
|
app.use('*', metricsMiddleware());
|
||||||
@@ -164,21 +216,45 @@ app.route(
|
|||||||
'/',
|
'/',
|
||||||
createPrekeyRoutes(store, crypto, {
|
createPrekeyRoutes(store, crypto, {
|
||||||
events,
|
events,
|
||||||
|
disableRateLimit,
|
||||||
...(kt ? { keyTransparency: kt } : {}),
|
...(kt ? { keyTransparency: kt } : {}),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
app.route('/', createInboxRoutes(inboxStore, crypto, { events: inboxEvents }));
|
|
||||||
|
|
||||||
// V3.7 transport-bridge — SSE / long-poll / WS fallbacks for the inbox.
|
// V3.7 transport-bridge — SSE / long-poll / WS fallbacks for the inbox.
|
||||||
// Held as a top-level reference so the WebSocket handler can be passed to
|
// Created BEFORE the inbox routes so the shared bridge-delivery log can
|
||||||
// Bun.serve below.
|
// be wired into both. The log is the cross-channel dedup gate that lets
|
||||||
|
// the inbox-fetch route skip blobs already pushed via bridge — see
|
||||||
|
// V4.8.4 changelog and the Prism FR
|
||||||
|
// `cross-channel-duplicate-fanout-v4.8.2.md`.
|
||||||
const bridgeRoutes = createBridgeRoutes({
|
const bridgeRoutes = createBridgeRoutes({
|
||||||
store: inboxStore,
|
store: inboxStore,
|
||||||
crypto,
|
crypto,
|
||||||
events: inboxEvents,
|
events: inboxEvents,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.route(
|
||||||
|
'/',
|
||||||
|
createInboxRoutes(inboxStore, crypto, {
|
||||||
|
events: inboxEvents,
|
||||||
|
disableRateLimit,
|
||||||
|
bridgeDeliveryLog: bridgeRoutes.bridgeDeliveryLog,
|
||||||
|
}),
|
||||||
|
);
|
||||||
app.route('/', bridgeRoutes.app);
|
app.route('/', bridgeRoutes.app);
|
||||||
|
|
||||||
|
// V4.9 — encrypted-blob primitive. Powers Prism's credential-driven
|
||||||
|
// device-linking (Phase 2) and any other Shade app that needs a
|
||||||
|
// "sign in from any device" UX. Mounted on the same Hono app so a
|
||||||
|
// single relay process serves prekey + inbox + blob from one port.
|
||||||
|
const blobDisabled = process.env.SHADE_DISABLE_BLOB === '1';
|
||||||
|
const blobStore = blobDisabled ? null : await createBlobStore();
|
||||||
|
if (blobDisabled) {
|
||||||
|
logger.info('Blob primitive disabled (SHADE_DISABLE_BLOB=1)');
|
||||||
|
} else if (blobStore) {
|
||||||
|
app.route('/', createBlobRoutes(blobStore, crypto, { disableRateLimit }));
|
||||||
|
logger.info('Blob primitive enabled', { route: '/v1/blob/:slotId' });
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Optional: Observer + Dashboard ──────────────────────────
|
// ─── Optional: Observer + Dashboard ──────────────────────────
|
||||||
|
|
||||||
const observerToken = process.env.SHADE_OBSERVER_TOKEN;
|
const observerToken = process.env.SHADE_OBSERVER_TOKEN;
|
||||||
@@ -253,6 +329,9 @@ async function shutdown(signal: string) {
|
|||||||
if ('close' in inboxStore && typeof inboxStore.close === 'function') {
|
if ('close' in inboxStore && typeof inboxStore.close === 'function') {
|
||||||
await inboxStore.close();
|
await inboxStore.close();
|
||||||
}
|
}
|
||||||
|
if (blobStore && 'close' in blobStore && typeof blobStore.close === 'function') {
|
||||||
|
await blobStore.close();
|
||||||
|
}
|
||||||
logger.info('Shutdown complete');
|
logger.info('Shutdown complete');
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -102,6 +102,48 @@ describe('Rate limiting integration with routes', () => {
|
|||||||
expect(results.filter((s) => s === 429).length).toBeGreaterThanOrEqual(1);
|
expect(results.filter((s) => s === 429).length).toBeGreaterThanOrEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// V4.8.1 — `SHADE_DISABLE_RATE_LIMIT=1` in standalone.ts is plumbed
|
||||||
|
// through to `createPrekeyServer({ disableRateLimit })`. This test
|
||||||
|
// covers the "what happens when the flag is true" path; the env-var
|
||||||
|
// → option conversion in standalone.ts is a one-liner verified by
|
||||||
|
// inspection.
|
||||||
|
test('register endpoint allows >5/hour from a single IP when disableRateLimit is set', async () => {
|
||||||
|
const app = createPrekeyServer({
|
||||||
|
crypto,
|
||||||
|
store: new MemoryPrekeyStore(),
|
||||||
|
disableRateLimit: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
async function doRegister(addressSuffix: number) {
|
||||||
|
const identity = await generateIdentityKeyPair(crypto);
|
||||||
|
const body: any = {
|
||||||
|
address: `user${addressSuffix}`,
|
||||||
|
identitySigningKey: Buffer.from(identity.signingPublicKey).toString('base64'),
|
||||||
|
identityDHKey: Buffer.from(identity.dhPublicKey).toString('base64'),
|
||||||
|
signedPreKey: {
|
||||||
|
keyId: 1,
|
||||||
|
publicKey: Buffer.from(crypto.randomBytes(32)).toString('base64'),
|
||||||
|
signature: Buffer.from(crypto.randomBytes(64)).toString('base64'),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const signed = await signPayload(crypto, identity.signingPrivateKey, body);
|
||||||
|
return app.request('/v1/keys/register', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json', 'x-forwarded-for': '203.0.113.1' },
|
||||||
|
body: JSON.stringify(signed),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const results: number[] = [];
|
||||||
|
for (let i = 0; i < 12; i++) {
|
||||||
|
const res = await doRegister(i);
|
||||||
|
results.push(res.status);
|
||||||
|
}
|
||||||
|
// No 429 anywhere — the limit is OFF.
|
||||||
|
expect(results.filter((s) => s === 429).length).toBe(0);
|
||||||
|
expect(results.filter((s) => s === 200).length).toBe(12);
|
||||||
|
});
|
||||||
|
|
||||||
test('rate limit returns Retry-After header', async () => {
|
test('rate limit returns Retry-After header', async () => {
|
||||||
const app = createPrekeyServer({ crypto, store: new MemoryPrekeyStore() });
|
const app = createPrekeyServer({ crypto, store: new MemoryPrekeyStore() });
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,31 @@
|
|||||||
{
|
{
|
||||||
"name": "@shade/storage-encrypted",
|
"name": "@shade/storage-encrypted",
|
||||||
"version": "4.2.1",
|
"version": "4.11.1",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"types": "src/index.ts",
|
"types": "src/index.ts",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"browser": "./src/index.browser.ts",
|
||||||
|
"default": "./src/index.ts"
|
||||||
|
},
|
||||||
|
"./crypto": "./src/crypto.ts",
|
||||||
|
"./sqlite": "./src/sqlite.ts",
|
||||||
|
"./postgres": "./src/postgres.ts",
|
||||||
|
"./idb": "./src/idb.ts"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@noble/hashes": "^2.0.1",
|
"@noble/hashes": "^2.0.1",
|
||||||
"@shade/core": "workspace:*",
|
"@shade/core": "workspace:*",
|
||||||
"@shade/crypto-web": "workspace:*",
|
"@shade/crypto-web": "workspace:*",
|
||||||
"@shade/storage-postgres": "workspace:*",
|
"@shade/storage-postgres": "workspace:*",
|
||||||
"@shade/storage-sqlite": "workspace:*",
|
"@shade/storage-sqlite": "workspace:*",
|
||||||
|
"idb": "^8.0.3",
|
||||||
"postgres": "^3.4.9"
|
"postgres": "^3.4.9"
|
||||||
},
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"fake-indexeddb": "^6.0.0"
|
||||||
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@shade/keychain": "workspace:*"
|
"@shade/keychain": "workspace:*"
|
||||||
},
|
},
|
||||||
|
|||||||
67
packages/shade-storage-encrypted/src/crypto.ts
Normal file
67
packages/shade-storage-encrypted/src/crypto.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/**
|
||||||
|
* Browser-safe entry point — exports KeyManager + crypto primitives only.
|
||||||
|
*
|
||||||
|
* Use this subpath when bundling for the browser to avoid pulling
|
||||||
|
* `bun:sqlite` (Bun-only) or `postgres` (Node-only) into the output.
|
||||||
|
*
|
||||||
|
* ```ts
|
||||||
|
* import { KeyManager } from '@shade/storage-encrypted/crypto';
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* For backend storage, import `@shade/storage-encrypted/sqlite` (Bun) or
|
||||||
|
* `@shade/storage-encrypted/postgres` (Node). For browser storage, import
|
||||||
|
* `@shade/storage-encrypted/idb`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export {
|
||||||
|
KeyManager,
|
||||||
|
type KeySource,
|
||||||
|
type KeychainBackend,
|
||||||
|
type KeyManagerOptions,
|
||||||
|
} from './crypto/key-manager.js';
|
||||||
|
export {
|
||||||
|
DEFAULT_SCRYPT,
|
||||||
|
DEFAULT_ARGON2ID,
|
||||||
|
type ScryptParams,
|
||||||
|
type Argon2idParams,
|
||||||
|
deriveMasterKey,
|
||||||
|
deriveMasterKeyArgon2id,
|
||||||
|
deriveStorageKey,
|
||||||
|
deriveFieldKey,
|
||||||
|
deriveNonce,
|
||||||
|
buildAad,
|
||||||
|
hkdfDerive,
|
||||||
|
deriveBlobSlotId,
|
||||||
|
deriveBlobKey,
|
||||||
|
deriveBlobSigningSeed,
|
||||||
|
} from './crypto/kdf.js';
|
||||||
|
export {
|
||||||
|
AEAD_NONCE_LEN,
|
||||||
|
AEAD_TAG_LEN,
|
||||||
|
aeadSeal,
|
||||||
|
aeadOpen,
|
||||||
|
} from './crypto/aead.js';
|
||||||
|
export {
|
||||||
|
COL,
|
||||||
|
TBL,
|
||||||
|
sealString,
|
||||||
|
openString,
|
||||||
|
sealBytes,
|
||||||
|
openBytes,
|
||||||
|
sealIdentity,
|
||||||
|
openIdentity,
|
||||||
|
sealConfig,
|
||||||
|
openConfig,
|
||||||
|
sealSignedPreKey,
|
||||||
|
openSignedPreKey,
|
||||||
|
sealOneTimePreKey,
|
||||||
|
openOneTimePreKey,
|
||||||
|
sealSession,
|
||||||
|
openSession,
|
||||||
|
sealTrust,
|
||||||
|
openTrust,
|
||||||
|
sealRetired,
|
||||||
|
openRetired,
|
||||||
|
sealStreamSensitive,
|
||||||
|
openStreamSensitive,
|
||||||
|
} from './crypto/row-codec.js';
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { scryptAsync } from '@noble/hashes/scrypt.js';
|
import { scryptAsync } from '@noble/hashes/scrypt.js';
|
||||||
|
import { argon2idAsync } from '@noble/hashes/argon2.js';
|
||||||
import { hkdf } from '@noble/hashes/hkdf.js';
|
import { hkdf } from '@noble/hashes/hkdf.js';
|
||||||
import { sha256 } from '@noble/hashes/sha2.js';
|
import { sha256 } from '@noble/hashes/sha2.js';
|
||||||
|
|
||||||
@@ -45,6 +46,50 @@ export async function deriveMasterKey(
|
|||||||
return scryptAsync(TEXT.encode(passphrase.normalize('NFKC')), salt, params);
|
return scryptAsync(TEXT.encode(passphrase.normalize('NFKC')), salt, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Argon2id parameters — memory-hard KDF preferred for low-entropy secrets (PINs). */
|
||||||
|
export interface Argon2idParams {
|
||||||
|
/** Memory cost in KiB. */
|
||||||
|
m: number;
|
||||||
|
/** Time cost (iterations). */
|
||||||
|
t: number;
|
||||||
|
/** Parallelism. */
|
||||||
|
p: number;
|
||||||
|
/** Output length in bytes. */
|
||||||
|
dkLen: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default: m=64 MiB, t=3, p=1, 32-byte output. Tuned for ~250–400 ms on a
|
||||||
|
* modern Chromium / Firefox / Safari laptop. RFC 9106 "second recommended"
|
||||||
|
* profile shrunk to a browser-friendly memory footprint — strong enough for
|
||||||
|
* 4–6 digit PINs as a defense-in-depth factor on top of a passphrase.
|
||||||
|
*/
|
||||||
|
export const DEFAULT_ARGON2ID: Argon2idParams = { m: 64 * 1024, t: 3, p: 1, dkLen: 32 };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Derive a 32-byte master key from a low-entropy secret + salt using
|
||||||
|
* argon2id. Salt MUST be persisted alongside the DB (16-byte random).
|
||||||
|
*/
|
||||||
|
export async function deriveMasterKeyArgon2id(
|
||||||
|
secret: string | Uint8Array,
|
||||||
|
salt: Uint8Array,
|
||||||
|
params: Argon2idParams = DEFAULT_ARGON2ID,
|
||||||
|
): Promise<Uint8Array> {
|
||||||
|
if (typeof secret === 'string' ? secret.length === 0 : secret.length === 0) {
|
||||||
|
throw new Error('argon2id secret must be non-empty');
|
||||||
|
}
|
||||||
|
if (salt.length < 16) {
|
||||||
|
throw new Error('salt must be at least 16 bytes');
|
||||||
|
}
|
||||||
|
const password = typeof secret === 'string' ? TEXT.encode(secret.normalize('NFKC')) : secret;
|
||||||
|
return argon2idAsync(password, salt, {
|
||||||
|
m: params.m,
|
||||||
|
t: params.t,
|
||||||
|
p: params.p,
|
||||||
|
dkLen: params.dkLen,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/** HKDF-SHA-256 with explicit info string. */
|
/** HKDF-SHA-256 with explicit info string. */
|
||||||
export function hkdfDerive(ikm: Uint8Array, info: string, length = 32, salt?: Uint8Array): Uint8Array {
|
export function hkdfDerive(ikm: Uint8Array, info: string, length = 32, salt?: Uint8Array): Uint8Array {
|
||||||
return hkdf(sha256, ikm, salt, TEXT.encode(info), length);
|
return hkdf(sha256, ikm, salt, TEXT.encode(info), length);
|
||||||
@@ -77,3 +122,42 @@ export function deriveNonce(rowKey: Uint8Array, table: string, pk: string): Uint
|
|||||||
export function buildAad(table: string, column: string, pk: string): Uint8Array {
|
export function buildAad(table: string, column: string, pk: string): Uint8Array {
|
||||||
return TEXT.encode(`shade-aad-v1|${table}|${column}|${pk}`);
|
return TEXT.encode(`shade-aad-v1|${table}|${column}|${pk}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── V4.9 — relay-side encrypted blob primitive ──────────────
|
||||||
|
//
|
||||||
|
// Three deterministic 32-byte derivations rooted at the user's master
|
||||||
|
// key, used by `@shade/sdk`'s `Profile` namespace to bootstrap a brand
|
||||||
|
// new device into existing E2EE state from credentials alone:
|
||||||
|
//
|
||||||
|
// slotId = HKDF(masterKey, info=`shade-blob-slot-v1:${app}`)
|
||||||
|
// blobKey = HKDF(masterKey, info=`shade-blob-key-v1:${app}`)
|
||||||
|
// sigSeed = HKDF(masterKey, info=`shade-blob-sig-v1:${app}`)
|
||||||
|
//
|
||||||
|
// `app` is a caller-supplied namespace (e.g. `"prism-profile"`) so two
|
||||||
|
// Shade apps with the same user/master never collide on the same slot.
|
||||||
|
//
|
||||||
|
// The slot identifier and the AEAD key are *both* derived from the
|
||||||
|
// master — the relay sees opaque slotIds and AEAD-sealed blobs and
|
||||||
|
// cannot decrypt or correlate slots to users. The signing seed is the
|
||||||
|
// raw 32-byte Ed25519 private key (matches @noble/curves' API: pubkey
|
||||||
|
// = ed25519.getPublicKey(seed)).
|
||||||
|
|
||||||
|
/** Lower-hex 64-char slotId derived from the master key. */
|
||||||
|
export function deriveBlobSlotId(masterKey: Uint8Array, app: string): Uint8Array {
|
||||||
|
return hkdfDerive(masterKey, `shade-blob-slot-v1:${app}`, 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** AEAD key for sealing/opening the blob. Use AAD = slotId. */
|
||||||
|
export function deriveBlobKey(masterKey: Uint8Array, app: string): Uint8Array {
|
||||||
|
return hkdfDerive(masterKey, `shade-blob-key-v1:${app}`, 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 32-byte Ed25519 signing seed (== the private key in the @noble/curves
|
||||||
|
* convention). The pubkey, derived deterministically from the seed, is
|
||||||
|
* what the relay TOFU-stores on the first PUT and verifies subsequent
|
||||||
|
* writes against.
|
||||||
|
*/
|
||||||
|
export function deriveBlobSigningSeed(masterKey: Uint8Array, app: string): Uint8Array {
|
||||||
|
return hkdfDerive(masterKey, `shade-blob-sig-v1:${app}`, 32);
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,17 +1,29 @@
|
|||||||
/**
|
/**
|
||||||
* KeyManager — owns the masterKey lifecycle for at-rest encryption.
|
* KeyManager — owns the masterKey lifecycle for at-rest encryption.
|
||||||
*
|
*
|
||||||
* Three sources are supported:
|
* Five sources are supported:
|
||||||
* 1. passphrase — scrypt-derived from a developer-supplied secret
|
* 1. passphrase — scrypt-derived from a developer-supplied secret
|
||||||
* 2. keychain — fetched from OS keychain via @shade/keychain (optional dep)
|
* 2. argon2id — memory-hard KDF for low-entropy secrets (PINs)
|
||||||
* 3. injected — caller supplies the 32-byte raw key directly
|
* 3. keychain — fetched from OS keychain via @shade/keychain (optional dep)
|
||||||
|
* 4. injected — caller supplies the 32-byte raw key directly
|
||||||
|
* 5. composite — HKDF-combine N sub-sources into one master key
|
||||||
|
* (multi-factor unlock — every source is required)
|
||||||
*
|
*
|
||||||
* The KeyManager pre-derives storageKey at construction time and caches the
|
* The KeyManager pre-derives storageKey at construction time and caches the
|
||||||
* per-(table, column) field keys. masterKey is zeroized after storageKey
|
* per-(table, column) field keys. masterKey is zeroized after storageKey
|
||||||
* derivation to limit residency.
|
* derivation to limit residency.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { deriveFieldKey, deriveMasterKey, deriveStorageKey, type ScryptParams, DEFAULT_SCRYPT } from './kdf.js';
|
import {
|
||||||
|
deriveFieldKey,
|
||||||
|
deriveMasterKey,
|
||||||
|
deriveMasterKeyArgon2id,
|
||||||
|
deriveStorageKey,
|
||||||
|
hkdfDerive,
|
||||||
|
DEFAULT_SCRYPT,
|
||||||
|
type Argon2idParams,
|
||||||
|
type ScryptParams,
|
||||||
|
} from './kdf.js';
|
||||||
|
|
||||||
export type KeySource =
|
export type KeySource =
|
||||||
| {
|
| {
|
||||||
@@ -21,6 +33,14 @@ export type KeySource =
|
|||||||
salt: Uint8Array;
|
salt: Uint8Array;
|
||||||
params?: ScryptParams;
|
params?: ScryptParams;
|
||||||
}
|
}
|
||||||
|
| {
|
||||||
|
kind: 'argon2id';
|
||||||
|
/** Low-entropy secret (PIN, short password). */
|
||||||
|
secret: string | Uint8Array;
|
||||||
|
/** Stable 16+ byte salt persisted alongside the DB. */
|
||||||
|
salt: Uint8Array;
|
||||||
|
params?: Argon2idParams;
|
||||||
|
}
|
||||||
| {
|
| {
|
||||||
kind: 'keychain';
|
kind: 'keychain';
|
||||||
/** Service identifier (e.g. "shade.storage"). */
|
/** Service identifier (e.g. "shade.storage"). */
|
||||||
@@ -34,6 +54,25 @@ export type KeySource =
|
|||||||
kind: 'injected';
|
kind: 'injected';
|
||||||
/** Raw 32-byte master key. */
|
/** Raw 32-byte master key. */
|
||||||
key: Uint8Array;
|
key: Uint8Array;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
kind: 'composite';
|
||||||
|
/**
|
||||||
|
* Sub-sources HKDF-combined into the master key, in order. Every
|
||||||
|
* source is mandatory: omitting or substituting any source yields
|
||||||
|
* a different master key and the storage open() will fail.
|
||||||
|
*
|
||||||
|
* Order is significant by design — `[pwd, pin]` and `[pin, pwd]`
|
||||||
|
* derive different master keys.
|
||||||
|
*/
|
||||||
|
sources: KeySource[];
|
||||||
|
/**
|
||||||
|
* HKDF info string for domain separation. Defaults to
|
||||||
|
* `shade-composite-master-v1`. Apps that want their composite key
|
||||||
|
* to be cryptographically distinct from any other Shade composite
|
||||||
|
* should override this with an app-specific string.
|
||||||
|
*/
|
||||||
|
info?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Pluggable keychain backend. Implementations live in @shade/keychain. */
|
/** Pluggable keychain backend. Implementations live in @shade/keychain. */
|
||||||
@@ -107,6 +146,9 @@ async function resolveMasterKey(source: KeySource, opts: KeyManagerOptions): Pro
|
|||||||
case 'passphrase':
|
case 'passphrase':
|
||||||
return deriveMasterKey(source.passphrase, source.salt, source.params ?? DEFAULT_SCRYPT);
|
return deriveMasterKey(source.passphrase, source.salt, source.params ?? DEFAULT_SCRYPT);
|
||||||
|
|
||||||
|
case 'argon2id':
|
||||||
|
return deriveMasterKeyArgon2id(source.secret, source.salt, source.params);
|
||||||
|
|
||||||
case 'injected':
|
case 'injected':
|
||||||
if (source.key.length !== 32) throw new Error('injected key must be exactly 32 bytes');
|
if (source.key.length !== 32) throw new Error('injected key must be exactly 32 bytes');
|
||||||
return new Uint8Array(source.key); // copy, in case caller mutates
|
return new Uint8Array(source.key); // copy, in case caller mutates
|
||||||
@@ -128,5 +170,39 @@ async function resolveMasterKey(source: KeySource, opts: KeyManagerOptions): Pro
|
|||||||
await opts.keychain.set(source.service, source.account, fresh);
|
await opts.keychain.set(source.service, source.account, fresh);
|
||||||
return fresh;
|
return fresh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 'composite': {
|
||||||
|
if (source.sources.length === 0) {
|
||||||
|
throw new Error('composite source requires at least one sub-source');
|
||||||
|
}
|
||||||
|
const subKeys: Uint8Array[] = [];
|
||||||
|
try {
|
||||||
|
for (const sub of source.sources) {
|
||||||
|
if (sub.kind === 'composite') {
|
||||||
|
// Composite-of-composite would silently flatten the unlock
|
||||||
|
// semantics ("any of N" vs "all of N") if the inner is misread.
|
||||||
|
// Forbidding nesting keeps the contract clear.
|
||||||
|
throw new Error('composite sources cannot be nested');
|
||||||
|
}
|
||||||
|
subKeys.push(await resolveMasterKey(sub, opts));
|
||||||
|
}
|
||||||
|
let total = 0;
|
||||||
|
for (const k of subKeys) total += k.length;
|
||||||
|
const ikm = new Uint8Array(total);
|
||||||
|
let off = 0;
|
||||||
|
for (const k of subKeys) {
|
||||||
|
ikm.set(k, off);
|
||||||
|
off += k.length;
|
||||||
|
}
|
||||||
|
const info = source.info ?? 'shade-composite-master-v1';
|
||||||
|
try {
|
||||||
|
return hkdfDerive(ikm, info, 32);
|
||||||
|
} finally {
|
||||||
|
ikm.fill(0);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
for (const k of subKeys) k.fill(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ export const COL = {
|
|||||||
trustedIdentity: 'trusted_identity',
|
trustedIdentity: 'trusted_identity',
|
||||||
retiredIdentity: 'retired_identity',
|
retiredIdentity: 'retired_identity',
|
||||||
streamSensitive: 'stream_sensitive',
|
streamSensitive: 'stream_sensitive',
|
||||||
|
broadcastChannelSensitive: 'broadcast_channel_sensitive',
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
/** Logical table identifiers — used for fieldKey + AAD binding. */
|
/** Logical table identifiers — used for fieldKey + AAD binding. */
|
||||||
@@ -51,6 +52,7 @@ export const TBL = {
|
|||||||
trustedIdentities: 'trusted_identities',
|
trustedIdentities: 'trusted_identities',
|
||||||
retiredIdentities: 'retired_identities',
|
retiredIdentities: 'retired_identities',
|
||||||
streamState: 'stream_state',
|
streamState: 'stream_state',
|
||||||
|
broadcastChannels: 'broadcast_channels',
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
/** Encrypt an arbitrary string payload bound to (table, column, pk). */
|
/** Encrypt an arbitrary string payload bound to (table, column, pk). */
|
||||||
@@ -226,3 +228,76 @@ export async function openStreamSensitive(
|
|||||||
if (b.overallHashState !== undefined) (out as { overallHashState?: string }).overallHashState = b.overallHashState;
|
if (b.overallHashState !== undefined) (out as { overallHashState?: string }).overallHashState = b.overallHashState;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Broadcast-channel sensitive bundle (V4.6). Routing fields (channelId,
|
||||||
|
* ownerRole, ownerAddress, label, generation, createdAt, updatedAt) live
|
||||||
|
* in plaintext columns so backends can list/query without unsealing every
|
||||||
|
* row; the chain key, iteration, and signing keys all live in this sealed
|
||||||
|
* blob.
|
||||||
|
*/
|
||||||
|
interface BroadcastChannelSensitiveBundle {
|
||||||
|
chainKey: string; // base64(32B)
|
||||||
|
iteration: number;
|
||||||
|
signingPublicKey: string; // base64(32B)
|
||||||
|
signingPrivateKey?: string; // base64; only when ownerRole === 'sender'
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function sealBroadcastChannelSensitive(
|
||||||
|
km: KeyManager,
|
||||||
|
channelId: string,
|
||||||
|
s: {
|
||||||
|
chainKey: Uint8Array;
|
||||||
|
iteration: number;
|
||||||
|
signingPublicKey: Uint8Array;
|
||||||
|
signingPrivateKey?: Uint8Array;
|
||||||
|
},
|
||||||
|
): Promise<Uint8Array> {
|
||||||
|
const bundle: BroadcastChannelSensitiveBundle = {
|
||||||
|
chainKey: toBase64(s.chainKey),
|
||||||
|
iteration: s.iteration,
|
||||||
|
signingPublicKey: toBase64(s.signingPublicKey),
|
||||||
|
};
|
||||||
|
if (s.signingPrivateKey !== undefined) {
|
||||||
|
bundle.signingPrivateKey = toBase64(s.signingPrivateKey);
|
||||||
|
}
|
||||||
|
return sealString(
|
||||||
|
km,
|
||||||
|
TBL.broadcastChannels,
|
||||||
|
COL.broadcastChannelSensitive,
|
||||||
|
channelId,
|
||||||
|
JSON.stringify(bundle),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function openBroadcastChannelSensitive(
|
||||||
|
km: KeyManager,
|
||||||
|
channelId: string,
|
||||||
|
blob: Uint8Array,
|
||||||
|
): Promise<{
|
||||||
|
chainKey: Uint8Array;
|
||||||
|
iteration: number;
|
||||||
|
signingPublicKey: Uint8Array;
|
||||||
|
signingPrivateKey?: Uint8Array;
|
||||||
|
}> {
|
||||||
|
const json = await openString(
|
||||||
|
km,
|
||||||
|
TBL.broadcastChannels,
|
||||||
|
COL.broadcastChannelSensitive,
|
||||||
|
channelId,
|
||||||
|
blob,
|
||||||
|
);
|
||||||
|
const b = JSON.parse(json) as BroadcastChannelSensitiveBundle;
|
||||||
|
const out: {
|
||||||
|
chainKey: Uint8Array;
|
||||||
|
iteration: number;
|
||||||
|
signingPublicKey: Uint8Array;
|
||||||
|
signingPrivateKey?: Uint8Array;
|
||||||
|
} = {
|
||||||
|
chainKey: fromBase64(b.chainKey),
|
||||||
|
iteration: b.iteration,
|
||||||
|
signingPublicKey: fromBase64(b.signingPublicKey),
|
||||||
|
};
|
||||||
|
if (b.signingPrivateKey !== undefined) out.signingPrivateKey = fromBase64(b.signingPrivateKey);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|||||||
13
packages/shade-storage-encrypted/src/idb.ts
Normal file
13
packages/shade-storage-encrypted/src/idb.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Browser entry point — Encrypted IndexedDB storage.
|
||||||
|
*
|
||||||
|
* Pulls `idb` (~12 kB) and SubtleCrypto only. Safe to bundle into a
|
||||||
|
* browser app via Vite/webpack/esbuild.
|
||||||
|
*
|
||||||
|
* ```ts
|
||||||
|
* import { KeyManager } from '@shade/storage-encrypted/crypto';
|
||||||
|
* import { EncryptedIndexedDBStorage } from '@shade/storage-encrypted/idb';
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
|
||||||
|
export { EncryptedIndexedDBStorage } from './storage/encrypted-indexeddb.js';
|
||||||
14
packages/shade-storage-encrypted/src/index.browser.ts
Normal file
14
packages/shade-storage-encrypted/src/index.browser.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
/**
|
||||||
|
* Default barrel resolved by bundlers under the `browser` condition.
|
||||||
|
*
|
||||||
|
* Excludes `EncryptedSQLiteStorage` (pulls `bun:sqlite`) and
|
||||||
|
* `EncryptedPostgresStorage` (pulls `postgres`). Browser apps can keep using
|
||||||
|
* `import { KeyManager } from '@shade/storage-encrypted'` — Vite/webpack will
|
||||||
|
* pick this file via the `browser` condition and produce a clean bundle.
|
||||||
|
*
|
||||||
|
* Node/Bun consumers continue to resolve to `./index.js`, which exposes the
|
||||||
|
* full surface area (back-compat).
|
||||||
|
*/
|
||||||
|
|
||||||
|
export * from './crypto.js';
|
||||||
|
export { EncryptedIndexedDBStorage } from './storage/encrypted-indexeddb.js';
|
||||||
@@ -6,13 +6,19 @@ export {
|
|||||||
} from './crypto/key-manager.js';
|
} from './crypto/key-manager.js';
|
||||||
export {
|
export {
|
||||||
DEFAULT_SCRYPT,
|
DEFAULT_SCRYPT,
|
||||||
|
DEFAULT_ARGON2ID,
|
||||||
type ScryptParams,
|
type ScryptParams,
|
||||||
|
type Argon2idParams,
|
||||||
deriveMasterKey,
|
deriveMasterKey,
|
||||||
|
deriveMasterKeyArgon2id,
|
||||||
deriveStorageKey,
|
deriveStorageKey,
|
||||||
deriveFieldKey,
|
deriveFieldKey,
|
||||||
deriveNonce,
|
deriveNonce,
|
||||||
buildAad,
|
buildAad,
|
||||||
hkdfDerive,
|
hkdfDerive,
|
||||||
|
deriveBlobSlotId,
|
||||||
|
deriveBlobKey,
|
||||||
|
deriveBlobSigningSeed,
|
||||||
} from './crypto/kdf.js';
|
} from './crypto/kdf.js';
|
||||||
export {
|
export {
|
||||||
AEAD_NONCE_LEN,
|
AEAD_NONCE_LEN,
|
||||||
@@ -25,6 +31,9 @@ export {
|
|||||||
EncryptedPostgresStorage,
|
EncryptedPostgresStorage,
|
||||||
ensureEncryptedClientTables,
|
ensureEncryptedClientTables,
|
||||||
} from './storage/encrypted-postgres.js';
|
} from './storage/encrypted-postgres.js';
|
||||||
|
// EncryptedIndexedDBStorage is browser-only; import it from
|
||||||
|
// `@shade/storage-encrypted/idb` (or rely on the `browser` condition
|
||||||
|
// resolving to ./index.browser.ts).
|
||||||
export {
|
export {
|
||||||
migrateSqliteToEncrypted,
|
migrateSqliteToEncrypted,
|
||||||
rotateSqliteEncryptionKey,
|
rotateSqliteEncryptionKey,
|
||||||
|
|||||||
11
packages/shade-storage-encrypted/src/postgres.ts
Normal file
11
packages/shade-storage-encrypted/src/postgres.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* Node-only entry point — Encrypted Postgres storage.
|
||||||
|
*
|
||||||
|
* Pulls `postgres`. Do not import from a browser bundle; use
|
||||||
|
* `@shade/storage-encrypted/idb` instead.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export {
|
||||||
|
EncryptedPostgresStorage,
|
||||||
|
ensureEncryptedClientTables,
|
||||||
|
} from './storage/encrypted-postgres.js';
|
||||||
15
packages/shade-storage-encrypted/src/sqlite.ts
Normal file
15
packages/shade-storage-encrypted/src/sqlite.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/**
|
||||||
|
* Bun-only entry point — Encrypted SQLite storage + migration utilities.
|
||||||
|
*
|
||||||
|
* Pulls `bun:sqlite`. Do not import from a browser bundle; use
|
||||||
|
* `@shade/storage-encrypted/idb` instead.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export { EncryptedSQLiteStorage } from './storage/encrypted-sqlite.js';
|
||||||
|
export {
|
||||||
|
migrateSqliteToEncrypted,
|
||||||
|
rotateSqliteEncryptionKey,
|
||||||
|
type MigrateOptions,
|
||||||
|
type RotateOptions,
|
||||||
|
type MigrateReport,
|
||||||
|
} from './migrate/migrate-sqlite.js';
|
||||||
@@ -0,0 +1,548 @@
|
|||||||
|
import { openDB, type IDBPDatabase, type DBSchema } from 'idb';
|
||||||
|
import type {
|
||||||
|
BroadcastChannelRecord,
|
||||||
|
BroadcastMemberRecord,
|
||||||
|
IdentityKeyPair,
|
||||||
|
OneTimePreKey,
|
||||||
|
PeerVerification,
|
||||||
|
PeerVerificationSource,
|
||||||
|
PersistedStreamState,
|
||||||
|
RetiredIdentity,
|
||||||
|
SessionState,
|
||||||
|
SignedPreKey,
|
||||||
|
StorageProvider,
|
||||||
|
} from '@shade/core';
|
||||||
|
import { constantTimeEqual, toBase64 } from '@shade/core';
|
||||||
|
import { KeyManager } from '../crypto/key-manager.js';
|
||||||
|
import {
|
||||||
|
openBroadcastChannelSensitive, openConfig, openIdentity, openOneTimePreKey,
|
||||||
|
openRetired, openSession, openSignedPreKey, openStreamSensitive, openTrust,
|
||||||
|
sealBroadcastChannelSensitive, sealConfig, sealIdentity, sealOneTimePreKey,
|
||||||
|
sealRetired, sealSession, sealSignedPreKey, sealStreamSensitive, sealTrust,
|
||||||
|
} from '../crypto/row-codec.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IndexedDB-backed StorageProvider with at-rest encryption.
|
||||||
|
*
|
||||||
|
* Schema is the IndexedDB equivalent of `EncryptedSQLiteStorage`: one object
|
||||||
|
* store per `_enc` table, sealed payloads stored as `Uint8Array` in a
|
||||||
|
* `ciphertext` field, routing/timestamp fields kept plaintext where SQLite
|
||||||
|
* does so for query efficiency. Crypto stack — `KeyManager`, `aeadSeal`,
|
||||||
|
* `aeadOpen`, row-codec sealers, AAD scheme — is shared verbatim with the
|
||||||
|
* SQLite/Postgres backends, so a row sealed under one backend decrypts
|
||||||
|
* under another given the same `KeyManager`.
|
||||||
|
*
|
||||||
|
* Browser-safe: imports `idb` (~12 kB, pure JS) and SubtleCrypto only.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* ```ts
|
||||||
|
* import { KeyManager } from '@shade/storage-encrypted/crypto';
|
||||||
|
* import { EncryptedIndexedDBStorage } from '@shade/storage-encrypted/idb';
|
||||||
|
*
|
||||||
|
* const km = await KeyManager.open({
|
||||||
|
* kind: 'composite',
|
||||||
|
* sources: [
|
||||||
|
* { kind: 'passphrase', passphrase, salt: pwSalt },
|
||||||
|
* { kind: 'argon2id', secret: pin, salt: pinSalt },
|
||||||
|
* ],
|
||||||
|
* });
|
||||||
|
* const storage = await EncryptedIndexedDBStorage.open({
|
||||||
|
* dbName: 'my-app-shade',
|
||||||
|
* keyManager: km,
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export class EncryptedIndexedDBStorage implements StorageProvider {
|
||||||
|
private constructor(
|
||||||
|
private db: IDBPDatabase<EncryptedShadeSchema>,
|
||||||
|
private km: KeyManager,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open (or create) the encrypted IndexedDB database. On first open the
|
||||||
|
* storageKey fingerprint is persisted; subsequent opens with a different
|
||||||
|
* KeyManager (wrong passphrase / PIN) reject with a clear error rather
|
||||||
|
* than silently writing data under the wrong key.
|
||||||
|
*/
|
||||||
|
static async open(opts: {
|
||||||
|
dbName?: string;
|
||||||
|
keyManager: KeyManager;
|
||||||
|
}): Promise<EncryptedIndexedDBStorage> {
|
||||||
|
const dbName = opts.dbName ?? 'shade-encrypted';
|
||||||
|
const db = await openDB<EncryptedShadeSchema>(dbName, SCHEMA_VERSION, {
|
||||||
|
upgrade(db, oldVersion) {
|
||||||
|
if (oldVersion < 1) {
|
||||||
|
db.createObjectStore('meta_enc', { keyPath: 'key' });
|
||||||
|
db.createObjectStore('identity_enc', { keyPath: 'id' });
|
||||||
|
db.createObjectStore('config_enc', { keyPath: 'key' });
|
||||||
|
db.createObjectStore('signed_prekeys_enc', { keyPath: 'keyId' });
|
||||||
|
db.createObjectStore('one_time_prekeys_enc', { keyPath: 'keyId' });
|
||||||
|
db.createObjectStore('sessions_enc', { keyPath: 'address' });
|
||||||
|
db.createObjectStore('trusted_identities_enc', { keyPath: 'address' });
|
||||||
|
|
||||||
|
const retired = db.createObjectStore('retired_identities_enc', {
|
||||||
|
keyPath: 'retiredAt',
|
||||||
|
});
|
||||||
|
retired.createIndex('byRetiredAt', 'retiredAt');
|
||||||
|
|
||||||
|
const stream = db.createObjectStore('stream_state_enc', { keyPath: 'streamId' });
|
||||||
|
stream.createIndex('byStatus', 'status');
|
||||||
|
stream.createIndex('byPeerAddress', 'peerAddress');
|
||||||
|
stream.createIndex('byUpdatedAt', 'updatedAt');
|
||||||
|
|
||||||
|
db.createObjectStore('peer_verifications_enc', { keyPath: 'peerAddress' });
|
||||||
|
db.createObjectStore('peer_identity_versions_enc', { keyPath: 'peerAddress' });
|
||||||
|
}
|
||||||
|
if (oldVersion < 2) {
|
||||||
|
db.createObjectStore('broadcast_channels_enc', { keyPath: 'channelId' });
|
||||||
|
const members = db.createObjectStore('broadcast_members_enc', {
|
||||||
|
keyPath: ['channelId', 'peerAddress'],
|
||||||
|
});
|
||||||
|
members.createIndex('byChannelId', 'channelId');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const store = new EncryptedIndexedDBStorage(db, opts.keyManager);
|
||||||
|
await store.assertKeyMatchesOrPersistFingerprint();
|
||||||
|
return store;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cleanly close the underlying connection. KeyManager is destroyed. */
|
||||||
|
close(): void {
|
||||||
|
this.db.close();
|
||||||
|
this.km.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On first open, persist a fingerprint of the storageKey. On subsequent
|
||||||
|
* opens, compare and reject mismatches with a clear error rather than
|
||||||
|
* silently writing data under the wrong key.
|
||||||
|
*/
|
||||||
|
private async assertKeyMatchesOrPersistFingerprint(): Promise<void> {
|
||||||
|
const expected = toBase64(this.km.storageKeyFingerprint());
|
||||||
|
const row = await this.db.get('meta_enc', 'storage_key_fingerprint');
|
||||||
|
if (!row) {
|
||||||
|
await this.db.put('meta_enc', { key: 'storage_key_fingerprint', value: expected });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (row.value !== expected) {
|
||||||
|
throw new Error(
|
||||||
|
'storage key mismatch — the supplied passphrase / keychain entry does not unlock this database',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Identity ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
async getIdentityKeyPair(): Promise<IdentityKeyPair | null> {
|
||||||
|
const row = await this.db.get('identity_enc', 1);
|
||||||
|
if (!row) return null;
|
||||||
|
return openIdentity(this.km, row.ciphertext);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveIdentityKeyPair(kp: IdentityKeyPair): Promise<void> {
|
||||||
|
const blob = await sealIdentity(this.km, kp);
|
||||||
|
await this.db.put('identity_enc', { id: 1, ciphertext: blob });
|
||||||
|
}
|
||||||
|
|
||||||
|
async getLocalRegistrationId(): Promise<number> {
|
||||||
|
const row = await this.db.get('config_enc', 'registrationId');
|
||||||
|
if (!row) return 0;
|
||||||
|
const v = await openConfig(this.km, 'registrationId', row.ciphertext);
|
||||||
|
return parseInt(v, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveLocalRegistrationId(id: number): Promise<void> {
|
||||||
|
const blob = await sealConfig(this.km, 'registrationId', String(id));
|
||||||
|
await this.db.put('config_enc', { key: 'registrationId', ciphertext: blob });
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Signed PreKeys ────────────────────────────────────────
|
||||||
|
|
||||||
|
async getSignedPreKey(keyId: number): Promise<SignedPreKey | null> {
|
||||||
|
const row = await this.db.get('signed_prekeys_enc', keyId);
|
||||||
|
if (!row) return null;
|
||||||
|
return openSignedPreKey(this.km, keyId, row.ciphertext);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveSignedPreKey(key: SignedPreKey): Promise<void> {
|
||||||
|
const blob = await sealSignedPreKey(this.km, key);
|
||||||
|
await this.db.put('signed_prekeys_enc', { keyId: key.keyId, ciphertext: blob });
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeSignedPreKey(keyId: number): Promise<void> {
|
||||||
|
await this.db.delete('signed_prekeys_enc', keyId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── One-Time PreKeys ──────────────────────────────────────
|
||||||
|
|
||||||
|
async getOneTimePreKey(keyId: number): Promise<OneTimePreKey | null> {
|
||||||
|
const row = await this.db.get('one_time_prekeys_enc', keyId);
|
||||||
|
if (!row) return null;
|
||||||
|
return openOneTimePreKey(this.km, keyId, row.ciphertext);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveOneTimePreKey(key: OneTimePreKey): Promise<void> {
|
||||||
|
const blob = await sealOneTimePreKey(this.km, key);
|
||||||
|
await this.db.put('one_time_prekeys_enc', { keyId: key.keyId, ciphertext: blob });
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeOneTimePreKey(keyId: number): Promise<void> {
|
||||||
|
await this.db.delete('one_time_prekeys_enc', keyId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getOneTimePreKeyCount(): Promise<number> {
|
||||||
|
return this.db.count('one_time_prekeys_enc');
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Sessions ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
async getSession(address: string): Promise<SessionState | null> {
|
||||||
|
const row = await this.db.get('sessions_enc', address);
|
||||||
|
if (!row) return null;
|
||||||
|
return openSession(this.km, address, row.ciphertext);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveSession(address: string, state: SessionState): Promise<void> {
|
||||||
|
const blob = await sealSession(this.km, address, state);
|
||||||
|
await this.db.put('sessions_enc', { address, ciphertext: blob });
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeSession(address: string): Promise<void> {
|
||||||
|
await this.db.delete('sessions_enc', address);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Trust ─────────────────────────────────────────────────
|
||||||
|
|
||||||
|
async isTrustedIdentity(address: string, identityKey: Uint8Array): Promise<boolean> {
|
||||||
|
const row = await this.db.get('trusted_identities_enc', address);
|
||||||
|
if (!row) return true; // TOFU
|
||||||
|
const stored = await openTrust(this.km, address, row.ciphertext);
|
||||||
|
return constantTimeEqual(stored, identityKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveTrustedIdentity(address: string, identityKey: Uint8Array): Promise<void> {
|
||||||
|
const blob = await sealTrust(this.km, address, identityKey);
|
||||||
|
await this.db.put('trusted_identities_enc', { address, ciphertext: blob });
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Identity History ──────────────────────────────────────
|
||||||
|
|
||||||
|
async addRetiredIdentity(identity: RetiredIdentity): Promise<void> {
|
||||||
|
const blob = await sealRetired(this.km, identity);
|
||||||
|
await this.db.put('retired_identities_enc', {
|
||||||
|
retiredAt: identity.retiredAt,
|
||||||
|
ciphertext: blob,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getRetiredIdentities(): Promise<RetiredIdentity[]> {
|
||||||
|
// Mirror SQLite's `ORDER BY retired_at DESC`
|
||||||
|
const rows = await this.db.getAllFromIndex('retired_identities_enc', 'byRetiredAt');
|
||||||
|
rows.reverse();
|
||||||
|
return Promise.all(
|
||||||
|
rows.map((r) => openRetired(this.km, r.retiredAt, r.ciphertext)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async pruneRetiredIdentities(olderThan: number): Promise<void> {
|
||||||
|
const tx = this.db.transaction('retired_identities_enc', 'readwrite');
|
||||||
|
const idx = tx.store.index('byRetiredAt');
|
||||||
|
const range = IDBKeyRange.upperBound(olderThan, true);
|
||||||
|
let cursor = await idx.openCursor(range);
|
||||||
|
while (cursor) {
|
||||||
|
await cursor.delete();
|
||||||
|
cursor = await cursor.continue();
|
||||||
|
}
|
||||||
|
await tx.done;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Stream-transfer resume state ──────────────────────────
|
||||||
|
|
||||||
|
async saveStreamState(state: PersistedStreamState): Promise<void> {
|
||||||
|
const blob = await sealStreamSensitive(this.km, state);
|
||||||
|
await this.db.put('stream_state_enc', {
|
||||||
|
streamId: state.streamId,
|
||||||
|
direction: state.direction,
|
||||||
|
peerAddress: state.peerAddress,
|
||||||
|
status: state.status,
|
||||||
|
ciphertext: blob,
|
||||||
|
createdAt: state.createdAt,
|
||||||
|
updatedAt: state.updatedAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getStreamState(streamId: string): Promise<PersistedStreamState | null> {
|
||||||
|
const row = await this.db.get('stream_state_enc', streamId);
|
||||||
|
if (!row) return null;
|
||||||
|
return this.rowToStreamState(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeStreamState(streamId: string): Promise<void> {
|
||||||
|
await this.db.delete('stream_state_enc', streamId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async listActiveStreamStates(direction?: 'send' | 'receive'): Promise<PersistedStreamState[]> {
|
||||||
|
const tx = this.db.transaction('stream_state_enc');
|
||||||
|
const idx = tx.store.index('byStatus');
|
||||||
|
const active = await idx.getAll(IDBKeyRange.only('active'));
|
||||||
|
const paused = await idx.getAll(IDBKeyRange.only('paused'));
|
||||||
|
const merged = [...active, ...paused];
|
||||||
|
const filtered = direction === undefined
|
||||||
|
? merged
|
||||||
|
: merged.filter((r) => r.direction === direction);
|
||||||
|
filtered.sort((a, b) => b.updatedAt - a.updatedAt);
|
||||||
|
return Promise.all(filtered.map((r) => this.rowToStreamState(r)));
|
||||||
|
}
|
||||||
|
|
||||||
|
async pruneStreamStates(olderThan: number): Promise<void> {
|
||||||
|
const tx = this.db.transaction('stream_state_enc', 'readwrite');
|
||||||
|
const idx = tx.store.index('byUpdatedAt');
|
||||||
|
const range = IDBKeyRange.upperBound(olderThan, true);
|
||||||
|
let cursor = await idx.openCursor(range);
|
||||||
|
while (cursor) {
|
||||||
|
const row = cursor.value;
|
||||||
|
if (row.status === 'finished' || row.status === 'aborted') {
|
||||||
|
await cursor.delete();
|
||||||
|
}
|
||||||
|
cursor = await cursor.continue();
|
||||||
|
}
|
||||||
|
await tx.done;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Peer verifications (V3.3) ────────────────────────────
|
||||||
|
// Fingerprints are public-by-design; stored in plaintext for symmetry
|
||||||
|
// with the SQLite/Postgres encrypted backends.
|
||||||
|
|
||||||
|
async savePeerVerification(v: PeerVerification): Promise<void> {
|
||||||
|
await this.db.put('peer_verifications_enc', { ...v });
|
||||||
|
}
|
||||||
|
|
||||||
|
async getPeerVerification(address: string): Promise<PeerVerification | null> {
|
||||||
|
const row = await this.db.get('peer_verifications_enc', address);
|
||||||
|
if (!row) return null;
|
||||||
|
return {
|
||||||
|
peerAddress: row.peerAddress,
|
||||||
|
fingerprint: row.fingerprint,
|
||||||
|
verifiedAt: row.verifiedAt,
|
||||||
|
verifiedBy: row.verifiedBy as PeerVerificationSource,
|
||||||
|
identityVersion: row.identityVersion,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async removePeerVerification(address: string): Promise<void> {
|
||||||
|
await this.db.delete('peer_verifications_enc', address);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getPeerIdentityVersion(address: string): Promise<number> {
|
||||||
|
const row = await this.db.get('peer_identity_versions_enc', address);
|
||||||
|
return row ? row.version : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
async bumpPeerIdentityVersion(address: string): Promise<number> {
|
||||||
|
// Atomic read-modify-write under one IDB transaction. Closes the race
|
||||||
|
// that exists in the SQLite version's non-atomic read-then-upsert.
|
||||||
|
const tx = this.db.transaction('peer_identity_versions_enc', 'readwrite');
|
||||||
|
const existing = await tx.store.get(address);
|
||||||
|
const next = (existing ? existing.version : 1) + 1;
|
||||||
|
await tx.store.put({ peerAddress: address, version: next });
|
||||||
|
await tx.done;
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Broadcast channels (V4.6) ────────────────────────────
|
||||||
|
|
||||||
|
async saveBroadcastChannel(channel: BroadcastChannelRecord): Promise<void> {
|
||||||
|
const sealed = await sealBroadcastChannelSensitive(this.km, channel.channelId, {
|
||||||
|
chainKey: channel.chainKey,
|
||||||
|
iteration: channel.iteration,
|
||||||
|
signingPublicKey: channel.signingPublicKey,
|
||||||
|
...(channel.signingPrivateKey !== undefined ? { signingPrivateKey: channel.signingPrivateKey } : {}),
|
||||||
|
});
|
||||||
|
await this.db.put('broadcast_channels_enc', {
|
||||||
|
channelId: channel.channelId,
|
||||||
|
ownerRole: channel.ownerRole,
|
||||||
|
ownerAddress: channel.ownerAddress,
|
||||||
|
label: channel.label ?? null,
|
||||||
|
generation: channel.generation,
|
||||||
|
ciphertext: sealed,
|
||||||
|
createdAt: channel.createdAt,
|
||||||
|
updatedAt: channel.updatedAt,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBroadcastChannel(channelId: string): Promise<BroadcastChannelRecord | null> {
|
||||||
|
const row = await this.db.get('broadcast_channels_enc', channelId);
|
||||||
|
if (!row) return null;
|
||||||
|
return this.encRowToChannel(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
async listBroadcastChannels(): Promise<BroadcastChannelRecord[]> {
|
||||||
|
const rows = await this.db.getAll('broadcast_channels_enc');
|
||||||
|
rows.sort((a, b) => a.createdAt - b.createdAt);
|
||||||
|
return Promise.all(rows.map((r) => this.encRowToChannel(r)));
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeBroadcastChannel(channelId: string): Promise<void> {
|
||||||
|
const tx = this.db.transaction(
|
||||||
|
['broadcast_channels_enc', 'broadcast_members_enc'],
|
||||||
|
'readwrite',
|
||||||
|
);
|
||||||
|
const memIdx = tx.objectStore('broadcast_members_enc').index('byChannelId');
|
||||||
|
let cursor = await memIdx.openCursor(IDBKeyRange.only(channelId));
|
||||||
|
while (cursor) {
|
||||||
|
await cursor.delete();
|
||||||
|
cursor = await cursor.continue();
|
||||||
|
}
|
||||||
|
await tx.objectStore('broadcast_channels_enc').delete(channelId);
|
||||||
|
await tx.done;
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveBroadcastMember(member: BroadcastMemberRecord): Promise<void> {
|
||||||
|
await this.db.put('broadcast_members_enc', { ...member });
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBroadcastMembers(channelId: string): Promise<BroadcastMemberRecord[]> {
|
||||||
|
const rows = await this.db.getAllFromIndex(
|
||||||
|
'broadcast_members_enc',
|
||||||
|
'byChannelId',
|
||||||
|
IDBKeyRange.only(channelId),
|
||||||
|
);
|
||||||
|
rows.sort((a, b) => a.joinedAt - b.joinedAt);
|
||||||
|
return rows.map((r) => ({
|
||||||
|
channelId: r.channelId,
|
||||||
|
peerAddress: r.peerAddress,
|
||||||
|
joinedAt: r.joinedAt,
|
||||||
|
removedAt: r.removedAt,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeBroadcastMember(channelId: string, peerAddress: string): Promise<void> {
|
||||||
|
await this.db.delete('broadcast_members_enc', [channelId, peerAddress]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async encRowToChannel(row: BroadcastChannelEncRow): Promise<BroadcastChannelRecord> {
|
||||||
|
const sensitive = await openBroadcastChannelSensitive(
|
||||||
|
this.km,
|
||||||
|
row.channelId,
|
||||||
|
row.ciphertext,
|
||||||
|
);
|
||||||
|
const out: BroadcastChannelRecord = {
|
||||||
|
channelId: row.channelId,
|
||||||
|
ownerRole: row.ownerRole,
|
||||||
|
ownerAddress: row.ownerAddress,
|
||||||
|
generation: row.generation,
|
||||||
|
chainKey: sensitive.chainKey,
|
||||||
|
iteration: sensitive.iteration,
|
||||||
|
signingPublicKey: sensitive.signingPublicKey,
|
||||||
|
createdAt: row.createdAt,
|
||||||
|
updatedAt: row.updatedAt,
|
||||||
|
};
|
||||||
|
if (row.label !== null) out.label = row.label;
|
||||||
|
if (sensitive.signingPrivateKey !== undefined) out.signingPrivateKey = sensitive.signingPrivateKey;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async rowToStreamState(row: StreamStateEncRow): Promise<PersistedStreamState> {
|
||||||
|
const sensitive = await openStreamSensitive(this.km, row.streamId, row.ciphertext);
|
||||||
|
const out: PersistedStreamState = {
|
||||||
|
streamId: row.streamId,
|
||||||
|
direction: row.direction,
|
||||||
|
peerAddress: row.peerAddress,
|
||||||
|
status: row.status,
|
||||||
|
metadataJson: sensitive.metadataJson,
|
||||||
|
partitionJson: sensitive.partitionJson,
|
||||||
|
laneStateJson: sensitive.laneStateJson,
|
||||||
|
ioDescriptorJson: sensitive.ioDescriptorJson,
|
||||||
|
secretEnc: sensitive.secretEnc,
|
||||||
|
secretNonce: sensitive.secretNonce,
|
||||||
|
createdAt: row.createdAt,
|
||||||
|
updatedAt: row.updatedAt,
|
||||||
|
};
|
||||||
|
if (sensitive.overallHashState !== undefined) out.overallHashState = sensitive.overallHashState;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Schema ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const SCHEMA_VERSION = 2;
|
||||||
|
|
||||||
|
interface MetaRow { key: string; value: string }
|
||||||
|
interface IdentityRow { id: 1; ciphertext: Uint8Array }
|
||||||
|
interface ConfigRow { key: string; ciphertext: Uint8Array }
|
||||||
|
interface SignedPreKeyRow { keyId: number; ciphertext: Uint8Array }
|
||||||
|
interface OneTimePreKeyRow { keyId: number; ciphertext: Uint8Array }
|
||||||
|
interface SessionRow { address: string; ciphertext: Uint8Array }
|
||||||
|
interface TrustedIdentityRow { address: string; ciphertext: Uint8Array }
|
||||||
|
interface RetiredIdentityRow { retiredAt: number; ciphertext: Uint8Array }
|
||||||
|
|
||||||
|
interface StreamStateEncRow {
|
||||||
|
streamId: string;
|
||||||
|
direction: 'send' | 'receive';
|
||||||
|
peerAddress: string;
|
||||||
|
status: 'active' | 'paused' | 'finished' | 'aborted';
|
||||||
|
ciphertext: Uint8Array;
|
||||||
|
createdAt: number;
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PeerVerificationRow {
|
||||||
|
peerAddress: string;
|
||||||
|
fingerprint: string;
|
||||||
|
verifiedAt: number;
|
||||||
|
verifiedBy: string;
|
||||||
|
identityVersion: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PeerIdentityVersionRow { peerAddress: string; version: number }
|
||||||
|
|
||||||
|
interface BroadcastChannelEncRow {
|
||||||
|
channelId: string;
|
||||||
|
ownerRole: 'sender' | 'receiver';
|
||||||
|
ownerAddress: string;
|
||||||
|
label: string | null;
|
||||||
|
generation: number;
|
||||||
|
ciphertext: Uint8Array;
|
||||||
|
createdAt: number;
|
||||||
|
updatedAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BroadcastMemberEncRow {
|
||||||
|
channelId: string;
|
||||||
|
peerAddress: string;
|
||||||
|
joinedAt: number;
|
||||||
|
removedAt: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface EncryptedShadeSchema extends DBSchema {
|
||||||
|
meta_enc: { key: string; value: MetaRow };
|
||||||
|
identity_enc: { key: number; value: IdentityRow };
|
||||||
|
config_enc: { key: string; value: ConfigRow };
|
||||||
|
signed_prekeys_enc: { key: number; value: SignedPreKeyRow };
|
||||||
|
one_time_prekeys_enc: { key: number; value: OneTimePreKeyRow };
|
||||||
|
sessions_enc: { key: string; value: SessionRow };
|
||||||
|
trusted_identities_enc: { key: string; value: TrustedIdentityRow };
|
||||||
|
retired_identities_enc: {
|
||||||
|
key: number;
|
||||||
|
value: RetiredIdentityRow;
|
||||||
|
indexes: { byRetiredAt: number };
|
||||||
|
};
|
||||||
|
stream_state_enc: {
|
||||||
|
key: string;
|
||||||
|
value: StreamStateEncRow;
|
||||||
|
indexes: {
|
||||||
|
byStatus: string;
|
||||||
|
byPeerAddress: string;
|
||||||
|
byUpdatedAt: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
peer_verifications_enc: { key: string; value: PeerVerificationRow };
|
||||||
|
peer_identity_versions_enc: { key: string; value: PeerIdentityVersionRow };
|
||||||
|
broadcast_channels_enc: { key: string; value: BroadcastChannelEncRow };
|
||||||
|
broadcast_members_enc: {
|
||||||
|
key: [string, string];
|
||||||
|
value: BroadcastMemberEncRow;
|
||||||
|
indexes: { byChannelId: string };
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import type { Sql } from 'postgres';
|
import type { Sql } from 'postgres';
|
||||||
import postgres from 'postgres';
|
import postgres from 'postgres';
|
||||||
import type {
|
import type {
|
||||||
|
BroadcastChannelRecord,
|
||||||
|
BroadcastMemberRecord,
|
||||||
IdentityKeyPair,
|
IdentityKeyPair,
|
||||||
OneTimePreKey,
|
OneTimePreKey,
|
||||||
PeerVerification,
|
PeerVerification,
|
||||||
@@ -14,10 +16,10 @@ import type {
|
|||||||
import { constantTimeEqual, toBase64 } from '@shade/core';
|
import { constantTimeEqual, toBase64 } from '@shade/core';
|
||||||
import { KeyManager } from '../crypto/key-manager.js';
|
import { KeyManager } from '../crypto/key-manager.js';
|
||||||
import {
|
import {
|
||||||
openConfig, openIdentity, openOneTimePreKey, openRetired, openSession,
|
openBroadcastChannelSensitive, openConfig, openIdentity, openOneTimePreKey,
|
||||||
openSignedPreKey, openStreamSensitive, openTrust, sealConfig, sealIdentity,
|
openRetired, openSession, openSignedPreKey, openStreamSensitive, openTrust,
|
||||||
sealOneTimePreKey, sealRetired, sealSession, sealSignedPreKey,
|
sealBroadcastChannelSensitive, sealConfig, sealIdentity, sealOneTimePreKey,
|
||||||
sealStreamSensitive, sealTrust,
|
sealRetired, sealSession, sealSignedPreKey, sealStreamSensitive, sealTrust,
|
||||||
} from '../crypto/row-codec.js';
|
} from '../crypto/row-codec.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -332,6 +334,108 @@ export class EncryptedPostgresStorage implements StorageProvider {
|
|||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Broadcast channels (V4.6) ────────────────────────────
|
||||||
|
|
||||||
|
async saveBroadcastChannel(channel: BroadcastChannelRecord): Promise<void> {
|
||||||
|
const sealed = await sealBroadcastChannelSensitive(this.km, channel.channelId, {
|
||||||
|
chainKey: channel.chainKey,
|
||||||
|
iteration: channel.iteration,
|
||||||
|
signingPublicKey: channel.signingPublicKey,
|
||||||
|
...(channel.signingPrivateKey !== undefined ? { signingPrivateKey: channel.signingPrivateKey } : {}),
|
||||||
|
});
|
||||||
|
await this.sql`
|
||||||
|
INSERT INTO shade_broadcast_channels_enc
|
||||||
|
(channel_id, owner_role, owner_address, label, generation, ciphertext, created_at, updated_at)
|
||||||
|
VALUES
|
||||||
|
(${channel.channelId}, ${channel.ownerRole}, ${channel.ownerAddress},
|
||||||
|
${channel.label ?? null}, ${channel.generation}, ${sealed},
|
||||||
|
${channel.createdAt}, ${channel.updatedAt})
|
||||||
|
ON CONFLICT (channel_id) DO UPDATE SET
|
||||||
|
owner_role = EXCLUDED.owner_role,
|
||||||
|
owner_address = EXCLUDED.owner_address,
|
||||||
|
label = EXCLUDED.label,
|
||||||
|
generation = EXCLUDED.generation,
|
||||||
|
ciphertext = EXCLUDED.ciphertext,
|
||||||
|
updated_at = EXCLUDED.updated_at
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBroadcastChannel(channelId: string): Promise<BroadcastChannelRecord | null> {
|
||||||
|
const rows = await this.sql<Array<BroadcastChannelEncRow>>`
|
||||||
|
SELECT * FROM shade_broadcast_channels_enc WHERE channel_id = ${channelId}
|
||||||
|
`;
|
||||||
|
if (rows.length === 0) return null;
|
||||||
|
return this.encRowToChannel(rows[0]!);
|
||||||
|
}
|
||||||
|
|
||||||
|
async listBroadcastChannels(): Promise<BroadcastChannelRecord[]> {
|
||||||
|
const rows = await this.sql<Array<BroadcastChannelEncRow>>`
|
||||||
|
SELECT * FROM shade_broadcast_channels_enc ORDER BY created_at ASC
|
||||||
|
`;
|
||||||
|
return Promise.all(rows.map((r) => this.encRowToChannel(r)));
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeBroadcastChannel(channelId: string): Promise<void> {
|
||||||
|
await this.sql`DELETE FROM shade_broadcast_members_enc WHERE channel_id = ${channelId}`;
|
||||||
|
await this.sql`DELETE FROM shade_broadcast_channels_enc WHERE channel_id = ${channelId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveBroadcastMember(member: BroadcastMemberRecord): Promise<void> {
|
||||||
|
await this.sql`
|
||||||
|
INSERT INTO shade_broadcast_members_enc
|
||||||
|
(channel_id, peer_address, joined_at, removed_at)
|
||||||
|
VALUES
|
||||||
|
(${member.channelId}, ${member.peerAddress}, ${member.joinedAt}, ${member.removedAt})
|
||||||
|
ON CONFLICT (channel_id, peer_address) DO UPDATE SET
|
||||||
|
joined_at = EXCLUDED.joined_at,
|
||||||
|
removed_at = EXCLUDED.removed_at
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBroadcastMembers(channelId: string): Promise<BroadcastMemberRecord[]> {
|
||||||
|
const rows = await this.sql<Array<{ channel_id: string; peer_address: string; joined_at: string | number; removed_at: string | number | null }>>`
|
||||||
|
SELECT channel_id, peer_address, joined_at, removed_at
|
||||||
|
FROM shade_broadcast_members_enc
|
||||||
|
WHERE channel_id = ${channelId}
|
||||||
|
ORDER BY joined_at ASC
|
||||||
|
`;
|
||||||
|
return rows.map((r) => ({
|
||||||
|
channelId: r.channel_id,
|
||||||
|
peerAddress: r.peer_address,
|
||||||
|
joinedAt: Number(r.joined_at),
|
||||||
|
removedAt: r.removed_at === null ? null : Number(r.removed_at),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeBroadcastMember(channelId: string, peerAddress: string): Promise<void> {
|
||||||
|
await this.sql`
|
||||||
|
DELETE FROM shade_broadcast_members_enc
|
||||||
|
WHERE channel_id = ${channelId} AND peer_address = ${peerAddress}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async encRowToChannel(row: BroadcastChannelEncRow): Promise<BroadcastChannelRecord> {
|
||||||
|
const sensitive = await openBroadcastChannelSensitive(
|
||||||
|
this.km,
|
||||||
|
String(row.channel_id),
|
||||||
|
row.ciphertext,
|
||||||
|
);
|
||||||
|
const out: BroadcastChannelRecord = {
|
||||||
|
channelId: String(row.channel_id),
|
||||||
|
ownerRole: row.owner_role,
|
||||||
|
ownerAddress: String(row.owner_address),
|
||||||
|
generation: Number(row.generation),
|
||||||
|
chainKey: sensitive.chainKey,
|
||||||
|
iteration: sensitive.iteration,
|
||||||
|
signingPublicKey: sensitive.signingPublicKey,
|
||||||
|
createdAt: Number(row.created_at),
|
||||||
|
updatedAt: Number(row.updated_at),
|
||||||
|
};
|
||||||
|
if (row.label !== null && row.label !== undefined) out.label = row.label;
|
||||||
|
if (sensitive.signingPrivateKey !== undefined) out.signingPrivateKey = sensitive.signingPrivateKey;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
private async rowToStreamState(row: StreamRow): Promise<PersistedStreamState> {
|
private async rowToStreamState(row: StreamRow): Promise<PersistedStreamState> {
|
||||||
const sensitive = await openStreamSensitive(this.km, String(row.stream_id), row.ciphertext);
|
const sensitive = await openStreamSensitive(this.km, String(row.stream_id), row.ciphertext);
|
||||||
const out: PersistedStreamState = {
|
const out: PersistedStreamState = {
|
||||||
@@ -363,6 +467,17 @@ interface StreamRow {
|
|||||||
updated_at: string | number;
|
updated_at: string | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface BroadcastChannelEncRow {
|
||||||
|
channel_id: string;
|
||||||
|
owner_role: 'sender' | 'receiver';
|
||||||
|
owner_address: string;
|
||||||
|
label: string | null;
|
||||||
|
generation: string | number;
|
||||||
|
ciphertext: Uint8Array;
|
||||||
|
created_at: string | number;
|
||||||
|
updated_at: string | number;
|
||||||
|
}
|
||||||
|
|
||||||
export async function ensureEncryptedClientTables(sql: Sql): Promise<void> {
|
export async function ensureEncryptedClientTables(sql: Sql): Promise<void> {
|
||||||
await sql`
|
await sql`
|
||||||
CREATE TABLE IF NOT EXISTS shade_meta_enc (
|
CREATE TABLE IF NOT EXISTS shade_meta_enc (
|
||||||
@@ -454,4 +569,29 @@ export async function ensureEncryptedClientTables(sql: Sql): Promise<void> {
|
|||||||
version BIGINT NOT NULL
|
version BIGINT NOT NULL
|
||||||
)
|
)
|
||||||
`;
|
`;
|
||||||
|
await sql`
|
||||||
|
CREATE TABLE IF NOT EXISTS shade_broadcast_channels_enc (
|
||||||
|
channel_id TEXT PRIMARY KEY,
|
||||||
|
owner_role TEXT NOT NULL CHECK (owner_role IN ('sender','receiver')),
|
||||||
|
owner_address TEXT NOT NULL,
|
||||||
|
label TEXT,
|
||||||
|
generation BIGINT NOT NULL,
|
||||||
|
ciphertext BYTEA NOT NULL,
|
||||||
|
created_at BIGINT NOT NULL,
|
||||||
|
updated_at BIGINT NOT NULL
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
await sql`
|
||||||
|
CREATE TABLE IF NOT EXISTS shade_broadcast_members_enc (
|
||||||
|
channel_id TEXT NOT NULL,
|
||||||
|
peer_address TEXT NOT NULL,
|
||||||
|
joined_at BIGINT NOT NULL,
|
||||||
|
removed_at BIGINT,
|
||||||
|
PRIMARY KEY (channel_id, peer_address)
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
await sql`
|
||||||
|
CREATE INDEX IF NOT EXISTS shade_broadcast_members_enc_channel_idx
|
||||||
|
ON shade_broadcast_members_enc(channel_id)
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { Database } from 'bun:sqlite';
|
import { Database } from 'bun:sqlite';
|
||||||
import type {
|
import type {
|
||||||
|
BroadcastChannelRecord,
|
||||||
|
BroadcastMemberRecord,
|
||||||
IdentityKeyPair,
|
IdentityKeyPair,
|
||||||
OneTimePreKey,
|
OneTimePreKey,
|
||||||
PeerVerification,
|
PeerVerification,
|
||||||
@@ -13,10 +15,10 @@ import type {
|
|||||||
import { constantTimeEqual, toBase64 } from '@shade/core';
|
import { constantTimeEqual, toBase64 } from '@shade/core';
|
||||||
import { KeyManager } from '../crypto/key-manager.js';
|
import { KeyManager } from '../crypto/key-manager.js';
|
||||||
import {
|
import {
|
||||||
openConfig, openIdentity, openOneTimePreKey, openRetired, openSession,
|
openBroadcastChannelSensitive, openConfig, openIdentity, openOneTimePreKey, openRetired,
|
||||||
openSignedPreKey, openStreamSensitive, openTrust, sealConfig, sealIdentity,
|
openSession, openSignedPreKey, openStreamSensitive, openTrust,
|
||||||
sealOneTimePreKey, sealRetired, sealSession, sealSignedPreKey,
|
sealBroadcastChannelSensitive, sealConfig, sealIdentity, sealOneTimePreKey,
|
||||||
sealStreamSensitive, sealTrust,
|
sealRetired, sealSession, sealSignedPreKey, sealStreamSensitive, sealTrust,
|
||||||
} from '../crypto/row-codec.js';
|
} from '../crypto/row-codec.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,6 +70,14 @@ export class EncryptedSQLiteStorage implements StorageProvider {
|
|||||||
removePeerVerification: ReturnType<Database['prepare']>;
|
removePeerVerification: ReturnType<Database['prepare']>;
|
||||||
getPeerIdentityVersion: ReturnType<Database['prepare']>;
|
getPeerIdentityVersion: ReturnType<Database['prepare']>;
|
||||||
upsertPeerIdentityVersion: ReturnType<Database['prepare']>;
|
upsertPeerIdentityVersion: ReturnType<Database['prepare']>;
|
||||||
|
saveBroadcastChannel: ReturnType<Database['prepare']>;
|
||||||
|
getBroadcastChannel: ReturnType<Database['prepare']>;
|
||||||
|
listBroadcastChannels: ReturnType<Database['prepare']>;
|
||||||
|
removeBroadcastChannel: ReturnType<Database['prepare']>;
|
||||||
|
removeBroadcastChannelMembers: ReturnType<Database['prepare']>;
|
||||||
|
saveBroadcastMember: ReturnType<Database['prepare']>;
|
||||||
|
getBroadcastMembers: ReturnType<Database['prepare']>;
|
||||||
|
removeBroadcastMember: ReturnType<Database['prepare']>;
|
||||||
};
|
};
|
||||||
|
|
||||||
private constructor(db: Database, km: KeyManager, ownsDb: boolean) {
|
private constructor(db: Database, km: KeyManager, ownsDb: boolean) {
|
||||||
@@ -156,6 +166,24 @@ export class EncryptedSQLiteStorage implements StorageProvider {
|
|||||||
peer_address TEXT PRIMARY KEY,
|
peer_address TEXT PRIMARY KEY,
|
||||||
version INTEGER NOT NULL
|
version INTEGER NOT NULL
|
||||||
);
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS broadcast_channels_enc (
|
||||||
|
channel_id TEXT PRIMARY KEY,
|
||||||
|
owner_role TEXT NOT NULL,
|
||||||
|
owner_address TEXT NOT NULL,
|
||||||
|
label TEXT,
|
||||||
|
generation INTEGER NOT NULL,
|
||||||
|
ciphertext BLOB NOT NULL,
|
||||||
|
created_at INTEGER NOT NULL,
|
||||||
|
updated_at INTEGER NOT NULL
|
||||||
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS broadcast_members_enc (
|
||||||
|
channel_id TEXT NOT NULL,
|
||||||
|
peer_address TEXT NOT NULL,
|
||||||
|
joined_at INTEGER NOT NULL,
|
||||||
|
removed_at INTEGER,
|
||||||
|
PRIMARY KEY (channel_id, peer_address)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_broadcast_members_enc_channel ON broadcast_members_enc(channel_id);
|
||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,6 +240,35 @@ export class EncryptedSQLiteStorage implements StorageProvider {
|
|||||||
`INSERT INTO peer_identity_versions_enc (peer_address, version) VALUES (?, ?)
|
`INSERT INTO peer_identity_versions_enc (peer_address, version) VALUES (?, ?)
|
||||||
ON CONFLICT(peer_address) DO UPDATE SET version = excluded.version`,
|
ON CONFLICT(peer_address) DO UPDATE SET version = excluded.version`,
|
||||||
),
|
),
|
||||||
|
saveBroadcastChannel: this.db.prepare(
|
||||||
|
`INSERT OR REPLACE INTO broadcast_channels_enc
|
||||||
|
(channel_id, owner_role, owner_address, label, generation,
|
||||||
|
ciphertext, created_at, updated_at)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
|
),
|
||||||
|
getBroadcastChannel: this.db.prepare(
|
||||||
|
'SELECT * FROM broadcast_channels_enc WHERE channel_id = ?',
|
||||||
|
),
|
||||||
|
listBroadcastChannels: this.db.prepare(
|
||||||
|
'SELECT * FROM broadcast_channels_enc ORDER BY created_at ASC',
|
||||||
|
),
|
||||||
|
removeBroadcastChannel: this.db.prepare(
|
||||||
|
'DELETE FROM broadcast_channels_enc WHERE channel_id = ?',
|
||||||
|
),
|
||||||
|
removeBroadcastChannelMembers: this.db.prepare(
|
||||||
|
'DELETE FROM broadcast_members_enc WHERE channel_id = ?',
|
||||||
|
),
|
||||||
|
saveBroadcastMember: this.db.prepare(
|
||||||
|
`INSERT OR REPLACE INTO broadcast_members_enc
|
||||||
|
(channel_id, peer_address, joined_at, removed_at)
|
||||||
|
VALUES (?, ?, ?, ?)`,
|
||||||
|
),
|
||||||
|
getBroadcastMembers: this.db.prepare(
|
||||||
|
'SELECT channel_id, peer_address, joined_at, removed_at FROM broadcast_members_enc WHERE channel_id = ? ORDER BY joined_at ASC',
|
||||||
|
),
|
||||||
|
removeBroadcastMember: this.db.prepare(
|
||||||
|
'DELETE FROM broadcast_members_enc WHERE channel_id = ? AND peer_address = ?',
|
||||||
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -432,6 +489,88 @@ export class EncryptedSQLiteStorage implements StorageProvider {
|
|||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Broadcast channels (V4.6) ────────────────────────────
|
||||||
|
|
||||||
|
async saveBroadcastChannel(channel: BroadcastChannelRecord): Promise<void> {
|
||||||
|
const sealed = await sealBroadcastChannelSensitive(this.km, channel.channelId, {
|
||||||
|
chainKey: channel.chainKey,
|
||||||
|
iteration: channel.iteration,
|
||||||
|
signingPublicKey: channel.signingPublicKey,
|
||||||
|
...(channel.signingPrivateKey !== undefined ? { signingPrivateKey: channel.signingPrivateKey } : {}),
|
||||||
|
});
|
||||||
|
this.stmts.saveBroadcastChannel.run(
|
||||||
|
channel.channelId,
|
||||||
|
channel.ownerRole,
|
||||||
|
channel.ownerAddress,
|
||||||
|
channel.label ?? null,
|
||||||
|
channel.generation,
|
||||||
|
sealed,
|
||||||
|
channel.createdAt,
|
||||||
|
channel.updatedAt,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBroadcastChannel(channelId: string): Promise<BroadcastChannelRecord | null> {
|
||||||
|
const row = this.stmts.getBroadcastChannel.get(channelId) as BroadcastChannelEncRow | undefined;
|
||||||
|
if (!row) return null;
|
||||||
|
return this.encRowToChannel(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
async listBroadcastChannels(): Promise<BroadcastChannelRecord[]> {
|
||||||
|
const rows = this.stmts.listBroadcastChannels.all() as BroadcastChannelEncRow[];
|
||||||
|
return Promise.all(rows.map((r) => this.encRowToChannel(r)));
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeBroadcastChannel(channelId: string): Promise<void> {
|
||||||
|
this.stmts.removeBroadcastChannelMembers.run(channelId);
|
||||||
|
this.stmts.removeBroadcastChannel.run(channelId);
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveBroadcastMember(member: BroadcastMemberRecord): Promise<void> {
|
||||||
|
this.stmts.saveBroadcastMember.run(
|
||||||
|
member.channelId,
|
||||||
|
member.peerAddress,
|
||||||
|
member.joinedAt,
|
||||||
|
member.removedAt,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBroadcastMembers(channelId: string): Promise<BroadcastMemberRecord[]> {
|
||||||
|
const rows = this.stmts.getBroadcastMembers.all(channelId) as BroadcastMemberEncRow[];
|
||||||
|
return rows.map((r) => ({
|
||||||
|
channelId: r.channel_id,
|
||||||
|
peerAddress: r.peer_address,
|
||||||
|
joinedAt: Number(r.joined_at),
|
||||||
|
removedAt: r.removed_at === null || r.removed_at === undefined ? null : Number(r.removed_at),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async removeBroadcastMember(channelId: string, peerAddress: string): Promise<void> {
|
||||||
|
this.stmts.removeBroadcastMember.run(channelId, peerAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async encRowToChannel(row: BroadcastChannelEncRow): Promise<BroadcastChannelRecord> {
|
||||||
|
const sensitive = await openBroadcastChannelSensitive(
|
||||||
|
this.km,
|
||||||
|
row.channel_id,
|
||||||
|
toBytes(row.ciphertext),
|
||||||
|
);
|
||||||
|
const out: BroadcastChannelRecord = {
|
||||||
|
channelId: row.channel_id,
|
||||||
|
ownerRole: row.owner_role,
|
||||||
|
ownerAddress: row.owner_address,
|
||||||
|
generation: Number(row.generation),
|
||||||
|
chainKey: sensitive.chainKey,
|
||||||
|
iteration: sensitive.iteration,
|
||||||
|
signingPublicKey: sensitive.signingPublicKey,
|
||||||
|
createdAt: Number(row.created_at),
|
||||||
|
updatedAt: Number(row.updated_at),
|
||||||
|
};
|
||||||
|
if (row.label !== null && row.label !== undefined) out.label = row.label;
|
||||||
|
if (sensitive.signingPrivateKey !== undefined) out.signingPrivateKey = sensitive.signingPrivateKey;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
private async rowToStreamState(row: StreamRow): Promise<PersistedStreamState> {
|
private async rowToStreamState(row: StreamRow): Promise<PersistedStreamState> {
|
||||||
const sensitive = await openStreamSensitive(this.km, row.stream_id, toBytes(row.ciphertext));
|
const sensitive = await openStreamSensitive(this.km, row.stream_id, toBytes(row.ciphertext));
|
||||||
const out: PersistedStreamState = {
|
const out: PersistedStreamState = {
|
||||||
@@ -463,6 +602,24 @@ interface StreamRow {
|
|||||||
updated_at: number | bigint;
|
updated_at: number | bigint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface BroadcastChannelEncRow {
|
||||||
|
channel_id: string;
|
||||||
|
owner_role: 'sender' | 'receiver';
|
||||||
|
owner_address: string;
|
||||||
|
label: string | null;
|
||||||
|
generation: number | bigint;
|
||||||
|
ciphertext: Uint8Array | ArrayBuffer;
|
||||||
|
created_at: number | bigint;
|
||||||
|
updated_at: number | bigint;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BroadcastMemberEncRow {
|
||||||
|
channel_id: string;
|
||||||
|
peer_address: string;
|
||||||
|
joined_at: number | bigint;
|
||||||
|
removed_at: number | bigint | null;
|
||||||
|
}
|
||||||
|
|
||||||
function toBytes(value: Uint8Array | ArrayBuffer | unknown): Uint8Array {
|
function toBytes(value: Uint8Array | ArrayBuffer | unknown): Uint8Array {
|
||||||
if (value instanceof Uint8Array) return value;
|
if (value instanceof Uint8Array) return value;
|
||||||
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user