55 * policy compliance, limit checks, and authorized counterparty validation.
66 */
77
8- const { Agent } = require ( "../models/agent " ) ;
8+ const MandateService = require ( "./mandate " ) ;
99const logger = require ( "../utils/logger" ) ;
1010
1111class A2AService {
12- constructor ( walletService , db ) {
12+ constructor ( walletService , db , config = { } ) {
1313 this . walletService = walletService ;
1414 this . db = db ;
15+ this . mandateService = config . mandateService || new MandateService ( config . mandateConfig ) ;
16+ this . strictMandateMode =
17+ config . strictMandateMode !== undefined
18+ ? config . strictMandateMode
19+ : process . env . STRICT_MANDATE_MODE === "true" ;
1520 }
1621
1722 /**
@@ -20,19 +25,45 @@ class A2AService {
2025 * @param {string } params.fromAgentId - Sender Agent ID
2126 * @param {string } params.toAgentId - Recipient Agent ID
2227 * @param {number } params.amount - Amount to transfer
28+ * @param {string } params.mandate - Optional signed Mandate (AP2) for Zero Trust validation
2329 * @param {Object } params.ucpPayload - The original UCP intent/payload
2430 */
25- async executeTransfer ( { fromAgentId, toAgentId, amount, ucpPayload = { } } ) {
31+ async executeTransfer ( {
32+ fromAgentId,
33+ toAgentId,
34+ amount,
35+ mandate,
36+ ucpPayload = { } ,
37+ } ) {
2638 try {
27- // 1. Validate Agents
28- const fromAgent = await Agent . findById ( fromAgentId ) ;
39+ // 0. Zero Trust Mandate Validation
40+ if ( mandate ) {
41+ try {
42+ await this . mandateService . verifyMandate ( mandate ) ;
43+ } catch ( error ) {
44+ throw new Error (
45+ `Zero Trust Validation Failed: Mandate verification failed: ${ error . message } ` ,
46+ ) ;
47+ }
48+ } else if ( this . strictMandateMode ) {
49+ throw new Error (
50+ "Zero Trust Validation Failed: Mandate required for A2A transfer in strict mode" ,
51+ ) ;
52+ }
53+
54+ // 1. Validate Agents using Repository Pattern
55+ const fromAgent = await this . db . findAgentById ( fromAgentId ) ;
2956 if ( ! fromAgent || fromAgent . status !== "active" ) {
30- throw new Error ( `Sender agent ${ fromAgentId } not found or inactive` ) ;
57+ throw new Error (
58+ `Zero Trust Validation Failed: Sender agent ${ fromAgentId } not found or inactive` ,
59+ ) ;
3160 }
3261
33- const toAgent = await Agent . findById ( toAgentId ) ;
62+ const toAgent = await this . db . findAgentById ( toAgentId ) ;
3463 if ( ! toAgent || toAgent . status !== "active" ) {
35- throw new Error ( `Recipient agent ${ toAgentId } not found or inactive` ) ;
64+ throw new Error (
65+ `Zero Trust Validation Failed: Recipient agent ${ toAgentId } not found or inactive` ,
66+ ) ;
3667 }
3768
3869 // 2. Policy Checks (Sender)
@@ -50,13 +81,10 @@ class A2AService {
5081 counterpartyAgentId : toAgentId ,
5182 ucpPayload,
5283 type : "a2a_transfer" ,
84+ mandate,
5385 } ,
5486 } ) ;
5587
56- // 4. Update Agent Usage (if we were tracking daily usage in db, we'd do it here)
57- // For now, limits are stateless checks against config.
58- // In a real implementation, we would query daily volume or update a usage record.
59-
6088 return {
6189 success : true ,
6290 transferId : transferResult . transferId ,
0 commit comments