/**
 * Iframe Isolation Protocol
 *
 * Message types for postMessage communication between the parent page
 * (IframeKeyVault) and the cross-origin iframe (KeyVaultHost).
 */
// ============================================
// Protocol constants
// ============================================
export const PROTOCOL_ID = 'privacy-boost-vault';
export const PROTOCOL_VERSION = 1;
export const DEFAULT_IFRAME_URL = 'https://vault.privacyboost.io/v1/vault.html';
// ============================================
// Timeout categories
// ============================================
export const TIMEOUT_DEFAULT = 10_000;
export const TIMEOUT_LONG = 60_000;
export const TIMEOUT_WALLET = 300_000;
// ============================================
// Operation types
// ============================================
export const VaultOps = {
    // Lifecycle
    INIT: 'init',
    DISPOSE: 'dispose',
    // Authentication
    AUTHENTICATE: 'keys.authenticate',
    SUBMIT_CREDENTIAL: 'keys.submitCredential',
    PROCEED_AFTER_MNEMONIC: 'keys.proceedAfterMnemonic',
    LOGOUT: 'keys.logout',
    CLEAR_SESSION: 'keys.clearSession',
    GET_STORED_SESSION: 'keys.getStoredSession',
    // Key status
    GET_KEY_STATUS: 'keys.getStatus',
    GET_PUBLIC_KEYS: 'keys.getPublicKeys',
    DELETE_VAULT: 'keys.deleteVault',
    // Deposit crypto
    BUILD_SHIELD_COMMITMENT: 'crypto.buildShieldCommitment',
    ENCRYPT_DEPOSIT_CIPHERTEXT: 'crypto.encryptDepositCiphertext',
    // Transfer / withdraw crypto
    COMPUTE_NULLIFIERS: 'crypto.computeNullifiers',
    GENERATE_NPK: 'crypto.generateNpk',
    COMPUTE_OUTPUT_COMMITMENT: 'crypto.computeOutputCommitment',
    DERIVE_ECDH_KEYS: 'crypto.deriveEcdhKeys',
    ENCRYPT_TRANSFER_CIPHERTEXT: 'crypto.encryptTransferCiphertext',
    SIGN_SPENDING: 'crypto.signSpending',
    // Digest computation
    COMPUTE_TRANSFER_DIGEST: 'crypto.computeTransferDigest',
    COMPUTE_WITHDRAWAL_DIGEST: 'crypto.computeWithdrawalDigest',
    COMPUTE_WITHDRAWAL_COMMITMENT: 'crypto.computeWithdrawalCommitment',
    // Auth crypto
    SIGN_VIEW_AUTH: 'auth.signViewAuth',
    GET_REGISTRATION_DATA: 'auth.getRegistrationData',
    // Wallet bridge (iframe → parent)
    WALLET_SIGN_MESSAGE: 'wallet.signMessage',
    WALLET_SIGN_TYPED_DATA: 'wallet.signTypedData',
    WALLET_SEND_TRANSACTION: 'wallet.sendTransaction',
    WALLET_GET_ADDRESS: 'wallet.getAddress',
    WALLET_GET_CHAIN_ID: 'wallet.getChainId',
    WALLET_WAIT_RECEIPT: 'wallet.waitForTransactionReceipt',
    // API bridge (iframe → parent): fetch proxy for connect-src 'none'
    API_FETCH: 'api.fetch',
    // High-level operations (parent → iframe): monolithic flows
    SHIELD: 'ops.shield',
    SEND: 'ops.send',
    UNSHIELD: 'ops.unshield',
    UNWRAP_WETH: 'ops.unwrapWeth',
    CONSOLIDATE_NOTES: 'ops.consolidateNotes',
    // HTTP proxy (parent → iframe): routes requests through WASM encryption
    HTTP_PROXY: 'http.proxy',
};
// ============================================
// Timeout mapping
// ============================================
const longOps = new Set([
    VaultOps.INIT,
    VaultOps.AUTHENTICATE,
    VaultOps.SUBMIT_CREDENTIAL,
    VaultOps.PROCEED_AFTER_MNEMONIC,
    VaultOps.API_FETCH,
    VaultOps.HTTP_PROXY,
]);
// Operations that require user interaction (wallet confirmation or full
// transaction flows). These get the longest timeout (TIMEOUT_WALLET = 5min).
const interactiveOps = new Set([
    VaultOps.WALLET_SIGN_MESSAGE,
    VaultOps.WALLET_SIGN_TYPED_DATA,
    VaultOps.WALLET_SEND_TRANSACTION,
    VaultOps.WALLET_WAIT_RECEIPT,
    VaultOps.SHIELD,
    VaultOps.SEND,
    VaultOps.UNSHIELD,
    VaultOps.UNWRAP_WETH,
]);
export function getTimeout(op) {
    if (interactiveOps.has(op))
        return TIMEOUT_WALLET;
    if (longOps.has(op))
        return TIMEOUT_LONG;
    return TIMEOUT_DEFAULT;
}
// ============================================
// Validation helpers
// ============================================
export function createMessageId() {
    if (typeof crypto !== 'undefined' && crypto.randomUUID) {
        return crypto.randomUUID();
    }
    // Fallback for environments without crypto.randomUUID
    const bytes = new Uint8Array(16);
    crypto.getRandomValues(bytes);
    return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
}
export function isProtocolMessage(data) {
    if (typeof data !== 'object' || data === null)
        return false;
    const msg = data;
    return (msg.protocol === PROTOCOL_ID &&
        msg.version === PROTOCOL_VERSION &&
        typeof msg.id === 'string' &&
        typeof msg.type === 'string' &&
        (msg.direction === 'request' || msg.direction === 'response'));
}
/** Returns true for any iframe→parent bridge operation (wallet ops + API fetch). */
export function isIframeBridgeOp(type) {
    return type.startsWith('wallet.') || type === VaultOps.API_FETCH;
}
//# sourceMappingURL=protocol.js.map