// Copyright 2024-2026 Tymofii Pidlisnyi. Apache-2.0 license. See LICENSE.
// ══════════════════════════════════════════════════════════════════
// cross_issuer_attestation signal_type (v0.1): envelope helpers
// ══════════════════════════════════════════════════════════════════
// Signs the JCS-canonical form of the envelope with the signature
// field emptied, then attaches the resulting signature. Mirrors the
// pattern used by v2/cognitive_attestation and v2/payment-rails: the
// canonical bytes are the source of truth, the wire object is those
// bytes plus the composer signature.
// ══════════════════════════════════════════════════════════════════
import { canonicalizeJCS } from '../../core/canonical-jcs.js';
import { sign, publicKeyFromPrivate } from '../../crypto/keys.js';
/**
 * Canonicalize an envelope for signing. The signature field is set to
 * the empty string before serialization so that signing and
 * verification agree on the bytes-under-signature without either side
 * having to reconstruct the envelope from a partial shape.
 */
export function canonicalizeForSignature(envelope) {
    const draft = { ...envelope, signature: '' };
    return canonicalizeJCS(draft);
}
/**
 * Sign an unsigned envelope with the composer's Ed25519 private key
 * (hex). Derives the public key from the private key and writes it
 * into `composer_id` on the returned envelope. This guarantees that
 * the embedded composer_id matches the signing key, which the verifier
 * checks against; divergence between the two is always a bug, so a
 * caller-supplied composer_id is overwritten here. Use
 * `publicKeyFromPrivate` upstream if a pre-flight check is needed.
 */
export function signCrossIssuerAttestation(privateKeyHex, unsigned) {
    const composer_id = publicKeyFromPrivate(privateKeyHex);
    const withDerivedKey = { ...unsigned, composer_id };
    const bytes = canonicalizeForSignature(withDerivedKey);
    const signature = sign(bytes, privateKeyHex);
    return { ...withDerivedKey, signature };
}
//# sourceMappingURL=envelope.js.map