Agent Trust Registry Smart Contract

View as Markdown

The Agent Trust Registry is an on-chain smart contract (inspired by ERC-8004) that manages AI agent identity, reputation scoring, capability registration, and validator hooks.

Interface

1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.24;
3
4interface IAgentTrustRegistry {
5 enum AgentState { Registered, Active, Suspended, Decommissioned }
6 enum AutonomyLevel { Intern, Junior, Senior, Principal }
7
8 struct AgentRecord {
9 string agentDid;
10 string operatorDid;
11 uint8 trustScore;
12 AgentState state;
13 AutonomyLevel autonomy;
14 uint256 registeredAt;
15 uint256 lastUpdated;
16 uint256 totalActions;
17 uint256 successfulActions;
18 }
19
20 struct Attestation {
21 string validatorDid;
22 uint8 score;
23 string attestationType;
24 uint256 issuedAt;
25 }
26
27 // Events
28 event AgentRegistered(string indexed agentDid, string operatorDid);
29 event TrustScoreUpdated(string indexed agentDid, uint8 oldScore, uint8 newScore, string reason);
30 event AgentStateChanged(string indexed agentDid, AgentState oldState, AgentState newState);
31 event AgentPromoted(string indexed agentDid, AutonomyLevel oldLevel, AutonomyLevel newLevel);
32 event AgentDecommissioned(string indexed agentDid);
33 event AttestationAdded(string indexed agentDid, string validatorDid, uint8 score);
34 event ValidatorRegistered(address indexed validator, string validatorDid);
35
36 // Agent lifecycle
37 function registerAgent(
38 string calldata agentDid,
39 string calldata operatorDid,
40 string[] calldata capabilities
41 ) external;
42
43 function getAgent(string calldata agentDid) external view returns (AgentRecord memory);
44
45 function setAgentState(string calldata agentDid, AgentState state) external;
46
47 function decommissionAgent(string calldata agentDid) external;
48
49 // Trust scoring
50 function updateTrustScore(string calldata agentDid, int8 delta, string calldata reason) external;
51
52 function getTrustScore(string calldata agentDid) external view returns (uint8);
53
54 function recordAction(string calldata agentDid, bool success) external;
55
56 // Autonomy
57 function promoteAgent(string calldata agentDid, AutonomyLevel level) external;
58
59 function getAutonomyLevel(string calldata agentDid) external view returns (AutonomyLevel);
60
61 // Attestations
62 function addAttestation(
63 string calldata agentDid,
64 string calldata validatorDid,
65 uint8 score,
66 string calldata attestationType
67 ) external;
68
69 function getAttestations(string calldata agentDid) external view returns (Attestation[] memory);
70
71 // Validators
72 function registerValidator(address validator, string calldata validatorDid) external;
73
74 function isValidator(address account) external view returns (bool);
75
76 // Discovery
77 function searchAgents(
78 uint8 minTrustScore,
79 AgentState state,
80 uint256 offset,
81 uint256 limit
82 ) external view returns (AgentRecord[] memory);
83
84 function getAgentCount() external view returns (uint256);
85}

Key Functions

registerAgent

Registers a new AI agent on-chain.

1function registerAgent(
2 string calldata agentDid,
3 string calldata operatorDid,
4 string[] calldata capabilities
5) external;

Requirements:

  • msg.sender must be the operator’s address
  • Agent DID must not already exist
  • Sets initial state to Registered, autonomy to Intern, trust score to 50

updateTrustScore

Adjusts an agent’s trust score by a signed delta.

1function updateTrustScore(string calldata agentDid, int8 delta, string calldata reason) external;

Requirements:

  • Caller must be authorized (platform service or validator)
  • Score is clamped to [0, 100]
  • Emits TrustScoreUpdated event

decommissionAgent

Permanently decommissions an agent.

1function decommissionAgent(string calldata agentDid) external;

Effects:

  • State set to Decommissioned
  • Trust score set to 0
  • Emits AgentDecommissioned event
  • Should be followed by credential batch revocation (off-chain)

Interaction Examples

Register an Agent

1const agentRegistry = new ethers.Contract(AGENT_TRUST_REGISTRY_ADDRESS, ABI, operatorSigner);
2
3const tx = await agentRegistry.registerAgent(
4 'did:adi:agent:7f3a9b2e...',
5 'did:adi:operator001...',
6 ['shopping', 'price-comparison']
7);
8await tx.wait();

Query Trust Score

1const score = await agentRegistry.getTrustScore('did:adi:agent:7f3a9b2e...');
2console.log('Trust score:', score); // 85

Record an Action

1// Called by the platform after each agent action
2const tx = await agentRegistry.recordAction('did:adi:agent:7f3a9b2e...', true); // success
3await tx.wait();

Add a Validator Attestation

1// Called by a registered validator
2const tx = await agentRegistry.addAttestation(
3 'did:adi:agent:7f3a9b2e...',
4 'did:adi:auditor001...',
5 15, // score boost
6 'safety-audit' // attestation type
7);
8await tx.wait();

Search Agents

1const agents = await agentRegistry.searchAgents(
2 80, // minTrustScore
3 1, // AgentState.Active
4 0, // offset
5 20 // limit
6);
7agents.forEach(a => console.log(a.agentDid, a.trustScore));

Gas Costs

OperationEstimated Gas
registerAgent~250,000
getAgent0 (view)
updateTrustScore~55,000
recordAction~45,000
promoteAgent~50,000
addAttestation~120,000
decommissionAgent~60,000
searchAgents0 (view)

Access Control

FunctionAuthorized Callers
registerAgentOperator address
setAgentStateOperator address
decommissionAgentOperator address
updateTrustScorePlatform service, registered validators
recordActionPlatform service
promoteAgentOperator address (with score threshold check)
addAttestationRegistered validators only
registerValidatorContract admin

Events for Off-Chain Indexing

All state changes emit events that the IDA API indexes in PostgreSQL for fast querying. The indexer listens for:

  • AgentRegistered — new agent in the registry
  • TrustScoreUpdated — update cached trust score
  • AgentStateChanged — update agent state
  • AgentPromoted — update autonomy level
  • AgentDecommissioned — trigger credential revocation cascade
  • AttestationAdded — update attestation list