-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2aService.js
More file actions
142 lines (129 loc) · 4.18 KB
/
Copy patha2aService.js
File metadata and controls
142 lines (129 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* A2A Service (Agent-to-Agent)
*
* Handles autonomous commerce transactions between AI agents, ensuring
* policy compliance, limit checks, and authorized counterparty validation.
*/
const MandateService = require("./mandate");
const logger = require("../utils/logger");
class A2AService {
constructor(walletService, db, config = {}) {
this.walletService = walletService;
this.db = db;
this.mandateService = config.mandateService || new MandateService(config.mandateConfig);
this.strictMandateMode =
config.strictMandateMode !== undefined
? config.strictMandateMode
: process.env.STRICT_MANDATE_MODE === "true";
}
/**
* Execute a transfer between two agents
* @param {Object} params
* @param {string} params.fromAgentId - Sender Agent ID
* @param {string} params.toAgentId - Recipient Agent ID
* @param {number} params.amount - Amount to transfer
* @param {string} params.mandate - Optional signed Mandate (AP2) for Zero Trust validation
* @param {Object} params.ucpPayload - The original UCP intent/payload
*/
async executeTransfer({
fromAgentId,
toAgentId,
amount,
mandate,
ucpPayload = {},
}) {
try {
// 0. Zero Trust Mandate Validation
if (mandate) {
try {
await this.mandateService.verifyMandate(mandate);
} catch (error) {
throw new Error(
`Zero Trust Validation Failed: Mandate verification failed: ${error.message}`,
);
}
} else if (this.strictMandateMode) {
throw new Error(
"Zero Trust Validation Failed: Mandate required for A2A transfer in strict mode",
);
}
// 1. Validate Agents using Repository Pattern
const fromAgent = await this.db.findAgentById(fromAgentId);
if (!fromAgent || fromAgent.status !== "active") {
throw new Error(
`Zero Trust Validation Failed: Sender agent ${fromAgentId} not found or inactive`,
);
}
const toAgent = await this.db.findAgentById(toAgentId);
if (!toAgent || toAgent.status !== "active") {
throw new Error(
`Zero Trust Validation Failed: Recipient agent ${toAgentId} not found or inactive`,
);
}
// 2. Policy Checks (Sender)
await this._validateAgentPolicy(fromAgent, toAgentId, amount);
// 3. Execute Wallet Transfer
const transferResult = await this.walletService.transfer({
fromWalletId: fromAgent.walletId,
toWalletId: toAgent.walletId,
amount,
description: `A2A Transfer: ${fromAgent.name} -> ${toAgent.name}`,
metadata: {
// Pass metadata for Transaction creation
agentId: fromAgentId,
counterpartyAgentId: toAgentId,
ucpPayload,
type: "a2a_transfer",
mandate,
},
});
return {
success: true,
transferId: transferResult.transferId,
timestamp: new Date(),
fromAgent: fromAgent.name,
toAgent: toAgent.name,
amount,
};
} catch (error) {
throw this._handleError("executeTransfer", error);
}
}
/**
* Validate agent policies
* @private
*/
async _validateAgentPolicy(agent, counterpartyId, amount) {
const { config } = agent;
if (!config) return;
// Check Per Transaction Limit
if (
config.limits?.perTransaction > 0 &&
amount > config.limits.perTransaction
) {
throw new Error(
`Zero Trust Validation Failed: Amount ${amount} exceeds agent per-transaction limit of ${config.limits.perTransaction}`,
);
}
// Check Authorized Counterparties
if (
config.authorizedCounterparties &&
config.authorizedCounterparties.length > 0
) {
if (!config.authorizedCounterparties.includes(counterpartyId)) {
throw new Error(
`Zero Trust Validation Failed: Agent ${agent.id} is not authorized to trade with ${counterpartyId}`,
);
}
}
}
/**
* Handle and format errors
* @private
*/
_handleError(method, error) {
logger.error(`A2AService.${method} error:`, error);
return error instanceof Error ? error : new Error(error);
}
}
module.exports = A2AService;