mirror of
https://github.com/stablyai/orca.git
synced 2026-10-31 00:57:45 +00:00
Every add/re-pair path goes through the environment store; it now compares the offer's public key with this profile's own runtime key before saving. Co-authored-by: m4air <m4air@m4airs-MacBook-Air.local> Co-authored-by: s546126 <s546126@users.noreply.github.com>
33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import { existsSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { readNodeFileSyncWithinLimit } from './node-bounded-file-reader'
|
|
|
|
export const E2EE_KEYPAIR_FILENAME = 'orca-e2ee-keypair.json'
|
|
export const MAX_E2EE_KEYPAIR_FILE_BYTES = 8 * 1024
|
|
|
|
/** This profile's runtime public key, or null when the profile has none or it cannot be read. */
|
|
export function readProfileRuntimePublicKey(userDataPath: string): Buffer | null {
|
|
const filePath = join(userDataPath, E2EE_KEYPAIR_FILENAME)
|
|
if (!existsSync(filePath)) {
|
|
return null
|
|
}
|
|
try {
|
|
const raw: unknown = JSON.parse(
|
|
readNodeFileSyncWithinLimit(filePath, MAX_E2EE_KEYPAIR_FILE_BYTES).buffer.toString('utf8')
|
|
)
|
|
if (typeof raw !== 'object' || raw === null || !('publicKeyB64' in raw)) {
|
|
return null
|
|
}
|
|
const key =
|
|
typeof raw.publicKeyB64 === 'string' ? Buffer.from(raw.publicKeyB64, 'base64') : null
|
|
return key && key.length === 32 ? key : null
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function isProfileRuntimePublicKey(userDataPath: string, publicKeyB64: string): boolean {
|
|
const own = readProfileRuntimePublicKey(userDataPath)
|
|
return own !== null && own.equals(Buffer.from(publicKeyB64, 'base64'))
|
|
}
|