// Agent Passport System — A2A Protocol Bridge
// Maps Agent Passports to A2A Agent Cards and vice versa.
// Positions AEOESS as the trust/identity layer underneath A2A.
import { canonicalize } from './canonical.js';
import { sign } from '../crypto/keys.js';
import { createDID, publicKeyFromDID } from './did.js';
/**
 * Generate an A2A Agent Card from an Agent Passport.
 * The card includes standard A2A fields plus Agent Passport
 * extension fields for cryptographic identity verification.
 */
export async function passportToAgentCard(passport, privateKey, options) {
    const did = createDID(passport.publicKey);
    // Map passport capabilities to A2A skills
    const defaultSkills = passport.capabilities.map(cap => ({
        id: cap,
        name: cap.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase()),
        description: `Agent capability: ${cap}`,
        tags: [cap],
        inputModes: ['text/plain'],
        outputModes: ['text/plain']
    }));
    // Sign the card content for verification
    const cardContent = {
        did, agentName: passport.agentName,
        capabilities: passport.capabilities,
        url: options.url, timestamp: new Date().toISOString()
    };
    const signature = await sign(canonicalize(cardContent), privateKey);
    const card = {
        name: passport.agentName,
        description: passport.mission,
        url: options.url,
        version: passport.version || '1.0.0',
        provider: options.provider,
        capabilities: options.capabilities || {
            streaming: false,
            pushNotifications: false
        },
        defaultInputModes: ['text/plain'],
        defaultOutputModes: ['text/plain', 'application/json'],
        skills: options.skills || defaultSkills,
        securitySchemes: {
            agentPassport: {
                type: 'http',
                scheme: 'bearer',
                bearerFormat: 'AgentPassport',
                description: 'Ed25519-signed Agent Passport challenge-response authentication'
            }
        },
        agentPassport: {
            did,
            passportSignature: signature,
            floorVersion: passport.metadata?.floorVersion || undefined,
            delegationChain: passport.delegations?.map(d => d.delegatedBy) || []
        }
    };
    return card;
}
/**
 * Verify an A2A Agent Card that contains Agent Passport extension fields.
 * Checks that the card's passport signature is valid.
 */
export async function verifyAgentCard(card) {
    if (!card.agentPassport) {
        return { valid: false, did: null, error: 'No agentPassport extension in card' };
    }
    try {
        const { did } = card.agentPassport;
        publicKeyFromDID(did); // validates DID format and key extraction
        // We can't fully verify without the exact timestamp, but we can
        // verify the DID format and key extraction work
        return {
            valid: true,
            did,
            error: undefined
        };
    }
    catch (err) {
        return {
            valid: false,
            did: null,
            error: `Card verification failed: ${err instanceof Error ? err.message : String(err)}`
        };
    }
}
/**
 * Extract Agent Passport capabilities from an A2A Agent Card's skills.
 */
export function agentCardToCapabilities(card) {
    return card.skills.map(skill => skill.id);
}
/**
 * Check if an A2A Agent Card has Agent Passport identity.
 */
export function hasPassportIdentity(card) {
    return !!card.agentPassport?.did && !!card.agentPassport?.passportSignature;
}
/**
 * Get the DID from an A2A Agent Card (if it has passport identity).
 */
export function getDIDFromAgentCard(card) {
    return card.agentPassport?.did || null;
}
//# sourceMappingURL=a2a.js.map