/**
 * Default Ed25519 signer/verifier over @noble/curves (isomorphic, browser-safe).
 *
 * The HOST signer used in production is P2's (backed by the agent's enrolled private key) and is
 * INJECTED — relay-e2e never holds host identity keys. This default verifier is used by
 * `buildClientHandshake` (browser side verifies the host's signature) and by tests as a stub host.
 */
import { ed25519 } from '@noble/curves/ed25519'
import type { HostSigner, HostVerifier } from './handshake.js'

/** A verifier that checks Ed25519 signatures with @noble/curves. */
export function nobleEd25519Verifier(): HostVerifier {
  return {
    async verify(pubkey: Uint8Array, transcript: Uint8Array, sig: Uint8Array): Promise<boolean> {
      try {
        return ed25519.verify(sig, transcript, pubkey)
      } catch {
        return false
      }
    },
  }
}

/** Test/helper signer over a raw Ed25519 private scalar (NOT for production host use). */
export function nobleEd25519Signer(privateKey: Uint8Array): HostSigner {
  return {
    async sign(transcript: Uint8Array): Promise<Uint8Array> {
      return ed25519.sign(transcript, privateKey)
    },
  }
}
