import type { Result } from '@private.me/shared';
/** Rotated key material (preserved for decryption of old messages). */
export interface RotatedKeys {
    /** Timestamp when keys were rotated (milliseconds since epoch). */
    readonly rotatedAt: number;
    /** Previous X25519 private key for decrypting messages encrypted with old key. */
    readonly x25519PrivateKey: CryptoKey;
    /** Previous ML-KEM-768 secret key for hybrid decryption. */
    readonly mlKemSecretKey?: Uint8Array;
}
/** Agent cryptographic identity backed by Ed25519 keypair. */
export interface AgentIdentity {
    /** Decentralized identifier: did:key:z6Mk... */
    readonly did: string;
    /** Web Crypto public key handle */
    readonly publicKey: CryptoKey;
    /** Web Crypto private key handle */
    readonly privateKey: CryptoKey;
    /** Raw 32-byte Ed25519 public key */
    readonly rawPublicKey: Uint8Array;
    /** Web Crypto X25519 private key for ECDH forward secrecy. */
    readonly x25519PrivateKey: CryptoKey;
    /** Web Crypto X25519 public key for ECDH forward secrecy. */
    readonly x25519PublicKey: CryptoKey;
    /** Raw 32-byte X25519 public key for wire transport. */
    readonly rawX25519PublicKey: Uint8Array;
    /** ML-KEM-768 public key (1184 bytes) for hybrid post-quantum KEM. */
    readonly mlKemPublicKey?: Uint8Array;
    /** ML-KEM-768 secret key (2400 bytes) for hybrid post-quantum KEM. */
    readonly mlKemSecretKey?: Uint8Array;
    /** ML-DSA-65 public key (1952 bytes) for hybrid post-quantum signatures. */
    readonly mlDsaPublicKey?: Uint8Array;
    /** ML-DSA-65 secret key (4032 bytes) for hybrid post-quantum signatures. */
    readonly mlDsaSecretKey?: Uint8Array;
    /** Previous rotated keys (for backward-compatible decryption). */
    readonly rotatedKeys?: RotatedKeys[];
}
/** Error codes for identity operations. */
export type IdentityError = 'KEYGEN_FAILED' | 'SIGN_FAILED' | 'VERIFY_FAILED' | 'INVALID_DID' | 'INVALID_KEY_LENGTH' | 'EXPORT_FAILED' | 'IMPORT_FAILED' | 'ROTATION_FAILED';
/**
 * Generate a new Ed25519 agent identity.
 * Creates keypair via Web Crypto API, derives did:key DID.
 *
 * @param opts - Optional configuration
 * @param opts.postQuantumSig - Enable ML-DSA-65 post-quantum signatures (default: false)
 * @returns Result containing the agent identity or error
 *
 * @example
 * ```typescript
 * import { generateIdentity } from '@private.me/xbind';
 *
 * // Generate identity with classical cryptography only
 * const identity = await generateIdentity();
 * if (!identity.ok) {
 *   throw new Error(`Failed to generate identity: ${identity.error}`);
 * }
 * console.log('DID:', identity.value.did);
 *
 * // Generate identity with post-quantum signatures
 * const pqIdentity = await generateIdentity({ postQuantumSig: true });
 * if (pqIdentity.ok) {
 *   console.log('Post-quantum DID:', pqIdentity.value.did);
 *   console.log('Has ML-DSA key:', !!pqIdentity.value.mlDsaPublicKey);
 * }
 * ```
 */
export declare function generateIdentity(opts?: {
    postQuantumSig?: boolean;
}): Promise<Result<AgentIdentity, IdentityError>>;
/**
 * Sign data with an Ed25519 private key.
 * @returns 64-byte Ed25519 signature.
 */
export declare function sign(privateKey: CryptoKey, data: Uint8Array): Promise<Result<Uint8Array, IdentityError>>;
/**
 * Verify an Ed25519 signature.
 * @returns true if valid, false if invalid (not an error).
 */
export declare function verify(publicKey: CryptoKey, signature: Uint8Array, data: Uint8Array): Promise<Result<boolean, IdentityError>>;
/**
 * Import a raw 32-byte Ed25519 public key into a CryptoKey.
 */
export declare function importPublicKey(rawPublicKey: Uint8Array): Promise<Result<CryptoKey, IdentityError>>;
/**
 * Convert 32-byte Ed25519 public key to did:key DID.
 * Format: did:key:z + base58btc(0xed01 || publicKey).
 */
export declare function publicKeyToDid(rawPublicKey: Uint8Array): string;
/**
 * Extract raw 32-byte public key bytes from a did:key DID.
 */
export declare function didToPublicKeyBytes(did: string): Result<Uint8Array, IdentityError>;
/**
 * Export an Ed25519 private key as PKCS8 DER bytes.
 *
 * Use this to persist an agent identity across restarts.
 * Node 20 does not support raw export for Ed25519 private keys —
 * PKCS8 is the portable format.
 *
 * @param privateKey - Ed25519 CryptoKey (must be extractable).
 * @returns PKCS8 DER bytes or error.
 */
export declare function exportPKCS8(privateKey: CryptoKey): Promise<Result<Uint8Array, IdentityError>>;
/**
 * Export an X25519 private key as PKCS8 DER bytes.
 *
 * @param privateKey - X25519 CryptoKey (must be extractable).
 * @returns PKCS8 DER bytes or error.
 */
export declare function exportX25519PKCS8(privateKey: CryptoKey): Promise<Result<Uint8Array, IdentityError>>;
/**
 * Import an Ed25519 identity from PKCS8 DER bytes.
 *
 * Generates a new X25519 keypair for forward secrecy.
 * Use this when you only persisted the Ed25519 key.
 *
 * @param pkcs8 - PKCS8-encoded Ed25519 private key bytes.
 * @returns Full AgentIdentity or error.
 */
export declare function importFromPKCS8(pkcs8: Uint8Array): Promise<Result<AgentIdentity, IdentityError>>;
/**
 * Import a full identity from both PKCS8 blobs (Ed25519 + X25519).
 *
 * Use this when you persisted both keys for full identity restoration
 * including the original X25519 key (preserves registered key agreement).
 *
 * @param ed25519Pkcs8 - PKCS8-encoded Ed25519 private key bytes.
 * @param x25519Pkcs8 - PKCS8-encoded X25519 private key bytes.
 * @returns Full AgentIdentity or error.
 */
export declare function importIdentity(ed25519Pkcs8: Uint8Array, x25519Pkcs8: Uint8Array, mlKemSecretKey?: Uint8Array, mlKemPublicKey?: Uint8Array, mlDsaSecretKey?: Uint8Array, mlDsaPublicKey?: Uint8Array): Promise<Result<AgentIdentity, IdentityError>>;
/**
 * Export ML-KEM-768 secret key bytes from an identity.
 *
 * @param identity - Agent identity with ML-KEM keys.
 * @returns Raw 2400-byte ML-KEM secret key or undefined if not available.
 */
export declare function exportMlKemSecretKey(identity: AgentIdentity): Uint8Array | undefined;
/**
 * Export ML-KEM-768 public key bytes from an identity.
 *
 * @param identity - Agent identity with ML-KEM keys.
 * @returns Raw 1184-byte ML-KEM public key or undefined if not available.
 */
export declare function exportMlKemPublicKey(identity: AgentIdentity): Uint8Array | undefined;
/** ML-DSA-65 signature length in bytes. */
export declare const ML_DSA65_SIG_BYTES = 3309;
/** ML-DSA-65 public key length in bytes. */
export declare const ML_DSA65_PK_BYTES = 1952;
/** ML-DSA-65 secret key seed length in bytes (using seed format for storage). */
export declare const ML_DSA65_SK_BYTES = 32;
/**
 * Sign data with an ML-DSA-65 secret key (FIPS 204).
 * @param secretKey - 32-byte ML-DSA-65 secret key seed.
 * @param data - Data to sign.
 * @returns 3309-byte ML-DSA-65 signature.
 */
export declare function signMlDsa65(secretKey: Uint8Array, data: Uint8Array): Promise<Result<Uint8Array, IdentityError>>;
/**
 * Verify an ML-DSA-65 signature (FIPS 204).
 * @param publicKey - 1952-byte ML-DSA-65 public key.
 * @param signature - 3309-byte ML-DSA-65 signature.
 * @param data - Data that was signed.
 * @returns true if valid, false if invalid (not an error).
 */
export declare function verifyMlDsa65(publicKey: Uint8Array, signature: Uint8Array, data: Uint8Array): Promise<Result<boolean, IdentityError>>;
/**
 * Export ML-DSA-65 secret key bytes from an identity.
 *
 * @param identity - Agent identity with ML-DSA keys.
 * @returns Raw 4032-byte ML-DSA-65 secret key or undefined if not available.
 */
export declare function exportMlDsaSecretKey(identity: AgentIdentity): Uint8Array | undefined;
/**
 * Export ML-DSA-65 public key bytes from an identity.
 *
 * @param identity - Agent identity with ML-DSA keys.
 * @returns Raw 1952-byte ML-DSA-65 public key or undefined if not available.
 */
export declare function exportMlDsaPublicKey(identity: AgentIdentity): Uint8Array | undefined;
/**
 * Derive a deterministic AgentIdentity from a 32-byte seed.
 *
 * Uses HKDF-SHA256 to derive separate Ed25519 and X25519 private keys
 * from the seed. The same seed always produces the same identity (same DID).
 *
 * @param seed - Exactly 32 bytes of high-entropy key material.
 * @returns Full AgentIdentity or error.
 */
export declare function identityFromSeed(seed: Uint8Array, opts?: {
    postQuantumSig?: boolean;
}): Promise<Result<AgentIdentity, IdentityError>>;
/**
 * Extract the raw 32-byte Ed25519 private key from a PKCS8 DER blob.
 *
 * Strips the 16-byte ASN.1 header. Validates prefix matches Ed25519 OID.
 *
 * @param pkcs8 - 48-byte PKCS8 DER for Ed25519.
 * @returns 32-byte raw private key or error.
 */
export declare function extractRawEd25519(pkcs8: Uint8Array): Result<Uint8Array, IdentityError>;
/**
 * Extract the raw 32-byte X25519 private key from a PKCS8 DER blob.
 *
 * Strips the 16-byte ASN.1 header. Validates prefix matches X25519 OID.
 *
 * @param pkcs8 - 48-byte PKCS8 DER for X25519.
 * @returns 32-byte raw private key or error.
 */
export declare function extractRawX25519(pkcs8: Uint8Array): Result<Uint8Array, IdentityError>;
/**
 * Rotate ML-KEM + X25519 keys while preserving old keys for decryption.
 *
 * Generates new X25519 and ML-KEM-768 keypairs. Saves the old keys
 * in rotatedKeys array (with timestamp) for backward-compatible decryption
 * of messages encrypted with the old keys. Ed25519 keys (for signing) are
 * NOT rotated — they remain constant for persistent identity.
 *
 * After rotation:
 * - New messages use the new X25519 + ML-KEM keys
 * - Old messages decrypt using preserved rotatedKeys
 * - DID remains unchanged (anchored to Ed25519 public key)
 * - Registry must be updated with new X25519 + ML-KEM public keys
 *
 * @param identity - Current identity with keys to rotate
 * @returns Updated identity with new keys + old keys in rotatedKeys array, or error
 *
 * @example
 * ```ts
 * const rotated = await identity.rotateKeys();
 * if (rotated.ok) {
 *   // New keys are active; old keys preserved for decryption
 *   console.log('Rotated at:', rotated.value.rotatedKeys?.[0]?.rotatedAt);
 *   // Update registry with new X25519 + ML-KEM public keys
 *   await registry.updateKeys(rotated.value.did, rotated.value.rawX25519PublicKey, rotated.value.mlKemPublicKey);
 * }
 * ```
 */
export declare function rotateKeys(identity: AgentIdentity): Promise<Result<AgentIdentity, IdentityError>>;
