@agenticprimitives/contracts 1.0.0-alpha.5 → 1.0.0-alpha.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/AUDIT.md +119 -27
  2. package/README.md +47 -12
  3. package/deployments-base-sepolia.json +52 -1
  4. package/deployments-faithchain.json +103 -0
  5. package/deployments-hopechain.json +78 -0
  6. package/deployments-paymaster-anvil.json +1 -0
  7. package/deployments-registries-base-sepolia.json +1 -0
  8. package/deployments-registries-hopechain.json +1 -0
  9. package/dist/abi/AccountBootstrap.json +99 -0
  10. package/dist/abi/AgentAccount.json +461 -67
  11. package/dist/abi/AgentAccountFactory.json +309 -0
  12. package/dist/abi/AgentNameAttributeResolver.json +212 -0
  13. package/dist/abi/AgentNameRegistry.json +544 -0
  14. package/dist/abi/AgentNameUniversalResolver.json +222 -0
  15. package/dist/abi/AgentProfileResolver.json +236 -0
  16. package/dist/abi/AgentRegistryBase.json +1003 -0
  17. package/dist/abi/AgentRelationship.json +405 -0
  18. package/dist/abi/AgenticGovernance.json +155 -0
  19. package/dist/abi/AgreementRegistry.json +203 -5
  20. package/dist/abi/AllowedMethodsEnforcer.json +193 -0
  21. package/dist/abi/AllowedTargetsEnforcer.json +193 -0
  22. package/dist/abi/ApprovedHashRegistry.json +267 -2
  23. package/dist/abi/AttestationRegistry.json +242 -0
  24. package/dist/abi/AttributeStorage.json +24 -0
  25. package/dist/abi/CallDataHashEnforcer.json +193 -0
  26. package/dist/abi/CustodyPolicy.json +422 -55
  27. package/dist/abi/DelegationManager.json +292 -0
  28. package/dist/abi/DigestBindingEnforcer.json +541 -0
  29. package/dist/abi/Eip7702.json +13 -0
  30. package/dist/abi/EstateProjectionRegistry.json +599 -0
  31. package/dist/abi/Generation5Bridge.json +355 -0
  32. package/dist/abi/GeoFeatureRegistry.json +235 -0
  33. package/dist/abi/IAgentAccountFactoryView.json +26 -0
  34. package/dist/abi/IAgentAccountGovernanceTimelockView.json +15 -0
  35. package/dist/abi/IApprovedHashRegistryWrite.json +35 -0
  36. package/dist/abi/ICustodyEpochView.json +28 -0
  37. package/dist/abi/ICustodyPolicyAccountModuleView.json +31 -0
  38. package/dist/abi/ICustodyPolicyFactoryView.json +13 -0
  39. package/dist/abi/ICustodyPolicyPasskeyView.json +50 -0
  40. package/dist/abi/IDelegationManager.json +18 -0
  41. package/dist/abi/IGeneration4Reads.json +143 -0
  42. package/dist/abi/IGeneration5Account.json +33 -0
  43. package/dist/abi/IGeneration5Voucher.json +26 -0
  44. package/dist/abi/IMoveAccountView.json +57 -0
  45. package/dist/abi/IMoveCustodyView.json +169 -0
  46. package/dist/abi/IMoveFactoryView.json +41 -0
  47. package/dist/abi/IPaymentReceiptRegistry.json +50 -0
  48. package/dist/abi/IRedemptionContext.json +15 -0
  49. package/dist/abi/IRegistryMembershipPolicy.json +70 -0
  50. package/dist/abi/ITimelockVeto.json +93 -0
  51. package/dist/abi/ITreasurySpendPolicyAccountView.json +44 -0
  52. package/dist/abi/IUpgradeVoucherView.json +26 -0
  53. package/dist/abi/MockUSDC.json +621 -0
  54. package/dist/abi/OntologyTermRegistry.json +268 -0
  55. package/dist/abi/PayloadClassesEnforcer.json +310 -0
  56. package/dist/abi/PaymentEnforcer.json +639 -0
  57. package/dist/abi/PaymentEscrow.json +501 -0
  58. package/dist/abi/PaymentReceiptRegistry.json +388 -0
  59. package/dist/abi/PermissionlessSubregistry.json +353 -0
  60. package/dist/abi/PricedSubregistry.json +1141 -0
  61. package/dist/abi/QuorumEnforcer.json +209 -0
  62. package/dist/abi/ReceiptAnchorRegistry.json +346 -0
  63. package/dist/abi/RelationshipTypeRegistry.json +268 -0
  64. package/dist/abi/ShapeRegistry.json +268 -0
  65. package/dist/abi/SignatureVerifier.json +196 -0
  66. package/dist/abi/SkillDefinitionRegistry.json +235 -0
  67. package/dist/abi/SmartAgentPaymaster.json +445 -1
  68. package/dist/abi/StableProxyGoverned.json +190 -0
  69. package/dist/abi/StableProxyGovernedOneShot.json +172 -0
  70. package/dist/abi/TimestampEnforcer.json +193 -0
  71. package/dist/abi/TreasurySpendPolicy.json +667 -0
  72. package/dist/abi/UpgradeVoucher.json +328 -0
  73. package/dist/abi/ValueEnforcer.json +193 -0
  74. package/dist/abi/index.d.ts +35 -0
  75. package/dist/abi/index.js +35 -0
  76. package/dist/deployments/base-sepolia.d.ts +11 -0
  77. package/dist/deployments/base-sepolia.js +51 -28
  78. package/dist/deployments/faithchain.d.ts +77 -0
  79. package/dist/deployments/faithchain.js +107 -0
  80. package/dist/deployments/hopechain.d.ts +68 -0
  81. package/dist/deployments/hopechain.js +82 -0
  82. package/dist/deployments/index.d.ts +7 -2
  83. package/dist/deployments/index.js +15 -0
  84. package/dist/deployments/paymaster-anvil.d.ts +11 -0
  85. package/dist/deployments/paymaster-anvil.js +9 -0
  86. package/dist/deployments/registries-base-sepolia.d.ts +10 -0
  87. package/dist/deployments/registries-base-sepolia.js +8 -0
  88. package/dist/deployments/registries-hopechain.d.ts +10 -0
  89. package/dist/deployments/registries-hopechain.js +8 -0
  90. package/package.json +37 -7
  91. package/src/AccountBootstrap.sol +38 -0
  92. package/src/AgentAccount.sol +1091 -134
  93. package/src/AgentAccountFactory.sol +198 -21
  94. package/src/ApprovedHashRegistry.sol +134 -10
  95. package/src/IAgentAccount.sol +37 -0
  96. package/src/SmartAgentPaymaster.sol +275 -22
  97. package/src/UniversalSignatureValidator.sol +7 -0
  98. package/src/UpgradeVoucher.sol +87 -0
  99. package/src/agency/DelegationManager.sol +299 -76
  100. package/src/agency/ICaveatEnforcer.sol +10 -0
  101. package/src/agency/IDelegationManager.sol +37 -5
  102. package/src/agreement/AgreementRegistry.sol +105 -35
  103. package/src/attestation/AttestationRegistry.sol +220 -40
  104. package/src/custody/CustodyPolicy.sol +588 -70
  105. package/src/custody/TreasurySpendPolicy.sol +338 -0
  106. package/src/enforcers/AllowedMethodsEnforcer.sol +13 -2
  107. package/src/enforcers/AllowedTargetsEnforcer.sol +13 -2
  108. package/src/enforcers/CallDataHashEnforcer.sol +7 -1
  109. package/src/enforcers/DigestBindingEnforcer.sol +182 -0
  110. package/src/enforcers/PayloadClassesEnforcer.sol +36 -0
  111. package/src/enforcers/PaymentEnforcer.sol +291 -0
  112. package/src/enforcers/QuorumEnforcer.sol +24 -3
  113. package/src/enforcers/TimestampEnforcer.sol +7 -1
  114. package/src/enforcers/ValueEnforcer.sol +13 -2
  115. package/src/geo/GeoFeatureRegistry.sol +67 -22
  116. package/src/governance/AgenticGovernance.sol +187 -4
  117. package/src/governance/GovernanceManaged.sol +2 -0
  118. package/src/governance/StableProxyGoverned.sol +79 -0
  119. package/src/governance/StableProxyGovernedOneShot.sol +93 -0
  120. package/src/identity/AgentProfilePredicates.sol +23 -0
  121. package/src/identity/AgentProfileResolver.sol +22 -2
  122. package/src/libraries/Eip7702.sol +36 -0
  123. package/src/libraries/SignatureSlotRecovery.sol +28 -7
  124. package/src/libraries/SignatureVerifier.sol +52 -0
  125. package/src/libraries/WebAuthnLib.sol +16 -0
  126. package/src/migration/Generation5Bridge.sol +364 -0
  127. package/src/mocks/MockUSDC.sol +152 -0
  128. package/src/naming/AgentNameAttributeResolver.sol +29 -4
  129. package/src/naming/AgentNamePredicates.sol +89 -0
  130. package/src/naming/AgentNameRegistry.sol +324 -20
  131. package/src/naming/AgentNameUniversalResolver.sol +35 -2
  132. package/src/naming/PermissionlessSubregistry.sol +82 -25
  133. package/src/naming/PricedSubregistry.sol +293 -0
  134. package/src/ontology/AttributeStorage.sol +93 -80
  135. package/src/ontology/OntologyTermRegistry.sol +58 -2
  136. package/src/ontology/ShapeRegistry.sol +58 -2
  137. package/src/payments/PaymentEscrow.sol +187 -0
  138. package/src/payments/PaymentReceiptRegistry.sol +100 -0
  139. package/src/provenance/ReceiptAnchorRegistry.sol +67 -0
  140. package/src/registry/AgentRegistryBase.sol +372 -0
  141. package/src/registry/EstateProjectionRegistry.sol +177 -0
  142. package/src/registry/IRegistryMembershipPolicy.sol +30 -0
  143. package/src/relationships/AgentRelationship.sol +129 -8
  144. package/src/relationships/RelationshipTypeRegistry.sol +58 -2
  145. package/src/skills/SkillDefinitionRegistry.sol +61 -17
@@ -2,9 +2,12 @@
2
2
  pragma solidity ^0.8.28;
3
3
 
4
4
  import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
5
+ import {Eip7702} from "../libraries/Eip7702.sol";
5
6
  import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
6
7
  import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
7
8
  import "@openzeppelin/contracts/interfaces/IERC1271.sol";
9
+ import "@openzeppelin/contracts/proxy/utils/Initializable.sol";
10
+ import "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
8
11
  import "./IDelegationManager.sol";
9
12
  import "./ICaveatEnforcer.sol";
10
13
  import "../governance/IGovernance.sol";
@@ -34,7 +37,7 @@ import "../governance/IGovernance.sol";
34
37
  * - Execute through delegator account, not direct target.call
35
38
  * - Open delegations: delegate = address(0xa11) allows any redeemer
36
39
  */
37
- contract DelegationManager is IDelegationManager, ReentrancyGuard {
40
+ contract DelegationManager is IDelegationManager, ReentrancyGuard, Initializable, UUPSUpgradeable {
38
41
  using ECDSA for bytes32;
39
42
  using MessageHashUtils for bytes32;
40
43
 
@@ -47,12 +50,49 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
47
50
  // `AgenticGovernance.isPaused()` (a guardian can pause without delay;
48
51
  // unpause needs the timelock).
49
52
  //
50
- // `governance` is `address(0)` for legacy deploys; in that case the
51
- // pause check is skipped (no governance, no pause source). Production
52
- // deploys pass a real AgenticGovernance address.
53
- address public immutable governance;
53
+ // `governance` is `address(0)` ONLY as an explicit opt-out (legacy / test
54
+ // deploys): there is then no pause source and the gate is skipped.
55
+ //
56
+ // Spec 445 / audit DLG-7 — THE READ NO LONGER FAILS OPEN. Before generation 4 a
57
+ // governance read that reverted, returned fewer than 32 bytes, or was aimed at a
58
+ // codeless address was treated as "not paused", so a typo'd / griefed / broken
59
+ // governance silently disabled the kill-switch for the life of this immutable
60
+ // contract. The rule now:
61
+ // 1. construction: `governance_` is `address(0)` (the explicit opt-out) or an
62
+ // address that HAS CODE at deploy time — a codeless non-zero address reverts
63
+ // `GovernanceHasNoCode`. A kill-switch that can never fire is refused at
64
+ // birth rather than discovered during an incident;
65
+ // 2. redemption: a configured governance with code MUST answer `isPaused()`
66
+ // with exactly one ABI word that is 0 or 1. A revert, a short / long return,
67
+ // or a non-boolean word reverts `GovernanceReadFailed` (FAIL CLOSED): a
68
+ // pause source that cannot be read is not evidence of "not paused";
69
+ // 3. a configured governance that has NO code at redemption time is skipped.
70
+ // Rule 1 makes that state unreachable in practice (EIP-6780 removed code
71
+ // deletion outside the creating transaction), and treating an impossible
72
+ // state as "paused forever" would only add a way to brick the singleton.
73
+ // The trade is deliberate: fail-closed on a faulted read means a governance
74
+ // contract that breaks freezes redemptions until a new DelegationManager is
75
+ // deployed (this contract is a re-deploy-on-bug singleton, SC4 § 4.3.4) — the
76
+ // same posture `SmartAgentPaymaster._governanceIsPaused` already takes (L1).
77
+ //
78
+ // Spec 447 (generation 5): no longer immutable. The manager is deployed behind a UUPS proxy at an address
79
+ // that never changes, so the EIP-712 domain (`verifyingContract` = the proxy), every signed delegation and the
80
+ // revocation set survive a fix; `governance` is storage, set once by the constructor (a direct deployment) or
81
+ // by `initialize` (behind a proxy), and is also the only upgrade authority ({_authorizeUpgrade}).
82
+ function governance() public view returns (address) {
83
+ return _governance;
84
+ }
54
85
 
55
86
  error SystemPaused();
87
+ /// @dev DLG-7: constructor refused a non-zero governance address with no code.
88
+ error GovernanceHasNoCode(address governance);
89
+ /// @dev DLG-7: the configured governance did not answer `isPaused()` with one boolean word.
90
+ error GovernanceReadFailed();
91
+ /// @dev Spec 450 §6: {redemptionDepth} read while no caveat hook of a redemption is running.
92
+ error NotInCaveatHook();
93
+ /// @dev Spec 445 / audit DLG-2: a redemption whose `target` is the account that executes it
94
+ /// (the root delegator). See {redeemDelegation}.
95
+ error SelfTargetedRedemption();
56
96
 
57
97
  /// @dev Root authority constant — delegations with this authority are root-level
58
98
  bytes32 public constant ROOT_AUTHORITY = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
@@ -60,8 +100,24 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
60
100
  /// @dev Open delegation sentinel — any address can redeem
61
101
  address public constant OPEN_DELEGATION = address(0xa11);
62
102
 
63
- /// @dev EIP-712 domain separator
64
- bytes32 public immutable DOMAIN_SEPARATOR;
103
+ /// @dev Spec 447: an upgrade by anything but this manager's governance.
104
+ error NotGovernance();
105
+ /// @dev Spec 447: a manager with no governance cannot be upgraded (it is frozen at its implementation).
106
+ error UpgradeWithoutGovernance();
107
+
108
+ /// @dev The root delegator of the redemption in flight (zero outside one). Transient
109
+ /// (EIP-1153): set and cleared inside the non-reentrant {redeemDelegation}. Lets a
110
+ /// stateful enforcer on a RE-DELEGATED link learn whose account actually executes —
111
+ /// the hook's `delegator` argument is the delegator of the link carrying the caveat,
112
+ /// which for a derived mandate is an intermediary, not the payer (audit DLG-8c).
113
+ address private transient _redemptionRoot;
114
+
115
+ /// @dev Spec 450 §6 — the chain context behind {redemptionDepth}. Transient (EIP-1153), never authority.
116
+ /// `_redemptionLinks` is the length of the chain being redeemed (0 outside a redemption);
117
+ /// `_redemptionLinkPlusOne` is the root-first index of the link whose caveat hooks are running, plus
118
+ /// one (0 while no hook runs: during validation, execution, and outside a redemption).
119
+ uint256 private transient _redemptionLinks;
120
+ uint256 private transient _redemptionLinkPlusOne;
65
121
 
66
122
  /// @dev Delegation type hash for EIP-712.
67
123
  ///
@@ -109,23 +165,60 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
109
165
  // Phase A.5 errors.
110
166
  error NotDelegatorOrDelegate();
111
167
  error HashMismatch();
168
+ /// @dev EXT-NOCODE-HOOK-1: a caveat enforcer with no code — a call to it would
169
+ /// succeed vacuously (fail-OPEN), so the live before/after hook loops reject it,
170
+ /// matching the view path (`verifyAuthorizationForCall` → "no-code-enforcer").
171
+ error NoCodeEnforcer(address enforcer);
112
172
  /// @dev Thrown by the deprecated permissionless `revokeDelegation(bytes32)`
113
173
  /// path. Migrate callers to `revokeDelegationByOwner(Delegation)`.
114
174
  error LegacyRevocationDisabled();
115
175
 
116
- /// @dev Storage gap reserves 50 slots for future state. The contract
117
- /// is currently NOT upgradeable (SC4 § 4.3.4 — DelegationManager
118
- /// is singleton + re-deploy on bug), but the gap standardises
119
- /// our storage discipline and keeps the option open.
120
- uint256[50] private __gap;
121
-
122
- /// @notice Deploys the DelegationManager. Pass `address(0)` for
123
- /// `governance_` to opt out of the H7-C.10 pause gate
124
- /// (legacy / test deploys). Production deploys pass an
125
- /// `AgenticGovernance` address.
126
- constructor(address governance_) {
127
- governance = governance_;
128
- DOMAIN_SEPARATOR = keccak256(
176
+ /// @dev The pause source and upgrade authority (see {governance}). Spec 447: storage, not an immutable.
177
+ address private _governance;
178
+
179
+ /// @dev Storage gap. Spec 447: the manager is upgradeable behind a proxy from generation 5, so this layout is
180
+ /// gated by `check:storage-layouts`; a new field takes a slot from here and the gap shrinks by one.
181
+ uint256[49] private __gap;
182
+
183
+ /// @notice A DIRECT deployment (tests, a one-off instance): initialized here, so the instance works on its own
184
+ /// and nobody can initialize it later. Pass `address(0)` for `governance_` to opt out of the H7-C.10
185
+ /// pause gate (legacy / test deploys). Production deploys pass an `AgenticGovernance` address and put
186
+ /// the manager behind an ERC-1967 proxy, initialized with {initialize} (spec 447).
187
+ constructor(address governance_) initializer {
188
+ _initializeManager(governance_);
189
+ }
190
+
191
+ /// @notice The proxy's initializer (spec 447). Runs once per proxy.
192
+ function initialize(address governance_) external initializer {
193
+ _initializeManager(governance_);
194
+ }
195
+
196
+ function _initializeManager(address governance_) private {
197
+ // DLG-7 rule 1: zero is the explicit opt-out; anything else must already be a contract.
198
+ if (governance_ != address(0) && governance_.code.length == 0) revert GovernanceHasNoCode(governance_);
199
+ Eip7702.refuseDelegatedEOA(governance_); // "has code" is not "is a contract" (spec 448 §0)
200
+ _governance = governance_;
201
+ }
202
+
203
+ /// @dev Spec 447: only this manager's governance (the `AgenticGovernance` contract, acting through its
204
+ /// timelock) upgrades it. A manager with no governance is never upgraded. UUPS's `onlyProxy` already
205
+ /// refuses an upgrade of a direct deployment.
206
+ function _authorizeUpgrade(address) internal view override {
207
+ address gov = _governance;
208
+ if (gov == address(0)) revert UpgradeWithoutGovernance();
209
+ Eip7702.refuseDelegatedEOA(msg.sender); // spec 448 §0 (EIP-7702)
210
+ if (msg.sender != gov) revert NotGovernance();
211
+ }
212
+
213
+ /// @notice EIP-712 domain separator for the CURRENT chain id (audit DLG-10), with `verifyingContract` the
214
+ /// address callers use — the proxy's when behind one. Computed per call, never cached in an immutable:
215
+ /// an immutable would carry the implementation's address, not the proxy's (spec 447).
216
+ function DOMAIN_SEPARATOR() public view returns (bytes32) {
217
+ return _buildDomainSeparator();
218
+ }
219
+
220
+ function _buildDomainSeparator() private view returns (bytes32) {
221
+ return keccak256(
129
222
  abi.encode(
130
223
  keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
131
224
  keccak256("AgentDelegationManager"),
@@ -136,43 +229,96 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
136
229
  );
137
230
  }
138
231
 
232
+ /// @notice The account that executes the redemption in flight — the ROOT delegator, whose
233
+ /// funds / authority move. Zero outside a redemption (including under the view
234
+ /// verifiers, which execute nothing). Enforcers read it for attribution only; it is
235
+ /// never an authorization input.
236
+ function redemptionRootDelegator() external view returns (address) {
237
+ return _redemptionRoot;
238
+ }
239
+
240
+ /// @inheritdoc IDelegationManager
241
+ function redemptionDepth() external view returns (uint256 links, uint256 position) {
242
+ uint256 p = _redemptionLinkPlusOne;
243
+ if (p == 0) revert NotInCaveatHook();
244
+ return (_redemptionLinks, p - 1);
245
+ }
246
+
247
+ /// @dev DLG-7 — the pause gate. See the rule at `governance`.
248
+ function _requireNotPaused() internal view {
249
+ address gov = _governance;
250
+ if (gov == address(0) || gov.code.length == 0) return;
251
+ (bool ok, bytes memory ret) = gov.staticcall(abi.encodeWithSelector(IGovernanceView.isPaused.selector));
252
+ if (!ok || ret.length != 32) revert GovernanceReadFailed();
253
+ uint256 word = abi.decode(ret, (uint256));
254
+ if (word > 1) revert GovernanceReadFailed();
255
+ if (word == 1) revert SystemPaused();
256
+ }
257
+
139
258
  /// @inheritdoc IDelegationManager
140
259
  /// @dev Phase A.5 (SC5 § 6.2) — `nonReentrant` blocks nested
141
260
  /// redemption (a caveat enforcer or target call cannot
142
261
  /// reenter this function).
262
+ ///
263
+ /// Spec 445 / audit DLG-2 — A REDEMPTION NEVER TARGETS THE ACCOUNT THAT EXECUTES IT.
264
+ /// Every enforcer judges the one `(target, value, data)` handed to this function. If
265
+ /// `target` is the root delegator's own account, `data` is an instruction to that account
266
+ /// (`executeBatch(Call[])`, `execute(...)`, any future multi-call entry point) whose INNER
267
+ /// calls carry their own targets, values and calldata that no enforcer saw — a
268
+ /// `ValueEnforcer(0.01 ether)` delegation moved 10 ETH that way. The rule is therefore
269
+ /// structural rather than a selector list: `target == rootDelegator` reverts
270
+ /// `SelfTargetedRedemption`, so every call a delegation causes is exactly the call the
271
+ /// caveats were evaluated against. It holds for any account implementation (a selector
272
+ /// list would go stale the day an account gains another batching entry point) and costs
273
+ /// nothing legitimate: every self-only mutator on `AgentAccount` is already a governance
274
+ /// selector that a delegation stack may not reach (`_rejectDelegatedGovernance`), and no
275
+ /// flow in this repository or the product repos redeems against the delegator itself.
276
+ /// Atomic multi-call under delegation, if it is ever needed, is a batch-aware redemption
277
+ /// that presents every inner call to every hook — not a self-call. The view twin
278
+ /// {verifyAuthorizationForCall} returns `(false, "self-target")` for the same input.
143
279
  function redeemDelegation(
144
280
  Delegation[] calldata delegations,
145
281
  address target,
146
282
  uint256 value,
147
283
  bytes calldata data
148
284
  ) external nonReentrant {
149
- // H7-C.10 / EXT3-010: system-wide pause gate. Skipped when
150
- // `governance` is `address(0)`, an EOA, or a non-conforming
151
- // contract (legacy / test deploys). Production deploys pass an
152
- // `AgenticGovernance` address; a guardian can pause without delay;
153
- // unpause requires the 24h timelock.
154
- if (governance != address(0) && governance.code.length > 0) {
155
- (bool ok, bytes memory data) = governance.staticcall(
156
- abi.encodeWithSelector(IGovernanceView.isPaused.selector)
157
- );
158
- if (ok && data.length >= 32 && abi.decode(data, (bool))) revert SystemPaused();
159
- }
285
+ // H7-C.10 / EXT3-010: system-wide pause gate (DLG-7: a faulted read fails closed).
286
+ _requireNotPaused();
160
287
  if (delegations.length == 0) revert EmptyChain();
161
288
 
162
- // Phase 1: Validate chain + run beforeHooks (leaf to root)
163
- for (uint256 i = 0; i < delegations.length; i++) {
289
+ // DLG-2: the executing account is never the target (see NatSpec).
290
+ address rootDelegator = delegations[delegations.length - 1].delegator;
291
+ if (target == rootDelegator) revert SelfTargetedRedemption();
292
+ _redemptionRoot = rootDelegator;
293
+
294
+ // Spec 450 §6: the chain context {redemptionDepth} reports. `nonReentrant` makes a nested redemption
295
+ // unreachable today; the context is still saved and restored (not cleared) so an inner redemption could
296
+ // never leave the outer one reading the inner chain's numbers.
297
+ uint256 n = delegations.length;
298
+ uint256 savedLinks = _redemptionLinks;
299
+ uint256 savedLink = _redemptionLinkPlusOne;
300
+ _redemptionLinks = n;
301
+
302
+ // Phase 1: Validate chain + run beforeHooks (leaf to root). Leaf-first index i is root-first n-1-i.
303
+ for (uint256 i = 0; i < n; i++) {
164
304
  _validateDelegation(delegations, i);
305
+ _redemptionLinkPlusOne = n - i;
165
306
  _runBeforeHooks(delegations[i], target, value, data);
307
+ _redemptionLinkPlusOne = 0;
166
308
  }
167
309
 
168
310
  // Phase 2: Execute through the root delegator's smart account
169
- address rootDelegator = delegations[delegations.length - 1].delegator;
170
311
  _executeFromDelegator(rootDelegator, target, value, data);
171
312
 
172
313
  // Phase 3: After-hooks (root to leaf, per DeleGator convention)
173
- for (uint256 i = delegations.length; i > 0; i--) {
314
+ for (uint256 i = n; i > 0; i--) {
315
+ _redemptionLinkPlusOne = n - i + 1;
174
316
  _runAfterHooks(delegations[i - 1], target, value, data);
317
+ _redemptionLinkPlusOne = 0;
175
318
  }
319
+ _redemptionRoot = address(0);
320
+ _redemptionLinks = savedLinks;
321
+ _redemptionLinkPlusOne = savedLink;
176
322
  }
177
323
 
178
324
  /// @inheritdoc IDelegationManager
@@ -192,35 +338,67 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
192
338
  revert LegacyRevocationDisabled();
193
339
  }
194
340
 
195
- /// @notice Phase A.5 — authenticated revocation path that works for
196
- /// Variant A (off-chain caveated delegation, never
197
- /// registered) by deriving the hash from the delegation
198
- /// struct + signature provided by the caller.
341
+ /// @notice Authenticated revocation of one delegation, by its delegator or its delegate.
199
342
  ///
200
- /// @dev Authorization gate: `msg.sender` MUST be either
201
- /// `delegation.delegator` OR `delegation.delegate` (the latter
202
- /// blocks a random EOA from revoking a delegation it doesn't
203
- /// hold, which would otherwise be a permissionless DoS vector
204
- /// against the legacy `revokeDelegation(bytes32)` path).
343
+ /// @dev Works for Variant A (off-chain, never registered) delegations: the hash is derived
344
+ /// from the struct the caller presents.
205
345
  ///
206
- /// @param delegation The delegation to revoke. The signature inside
207
- /// it is verified to confirm the struct is
208
- /// legitimate before we mark the hash revoked —
209
- /// otherwise a malicious delegate could revoke a
210
- /// *forged* delegation hash by submitting a struct
211
- /// they invented.
346
+ /// Spec 445 / audit DLG-9 — THE DELEGATOR REVOKES WITHOUT A SIGNATURE CHECK. Before
347
+ /// generation 4 both parties had to present a signature that verifies NOW. A delegation
348
+ /// signed by a custodian who has since been removed, or approved under an earlier custody
349
+ /// epoch, fails that check today — so it could not be marked revoked, and it came back to
350
+ /// life with no revocation on record the moment the custodian was re-added or the digest
351
+ /// re-approved. `msg.sender == delegation.delegator` is the whole authorization for the
352
+ /// delegator path: the only hash it can mark is one whose struct names the caller itself
353
+ /// as delegator, i.e. a grant only the caller could ever have made. Marking a struct it
354
+ /// never signed harms nobody (it forecloses a grant the caller alone could issue).
355
+ ///
356
+ /// The DELEGATE path keeps the signature requirement: without it a delegate could revoke
357
+ /// the hash of a struct the delegator has not signed yet, pre-empting a future grant.
358
+ ///
359
+ /// Deliberately not `nonReentrant`: a delegator account revokes its own grants through a
360
+ /// redeemed mandate (`redeemDelegation(…, target = this, revokeDelegationByOwner(g))`),
361
+ /// which calls back in while the redemption guard is held.
362
+ ///
363
+ /// @param delegation The delegation to revoke.
212
364
  function revokeDelegationByOwner(Delegation calldata delegation) external {
213
- if (msg.sender != delegation.delegator && msg.sender != delegation.delegate) {
365
+ bytes32 dHash = hashDelegation(delegation);
366
+ if (msg.sender == delegation.delegator) {
367
+ // DLG-9: the delegator's own struct — no signature needed.
368
+ } else if (msg.sender == delegation.delegate) {
369
+ // A delegate may renounce only a delegation that really exists.
370
+ _validateSignature(delegation.delegator, dHash, delegation.signature);
371
+ } else {
214
372
  revert NotDelegatorOrDelegate();
215
373
  }
374
+ _revoke(dHash);
375
+ }
216
376
 
217
- bytes32 dHash = hashDelegation(delegation);
218
-
219
- // Verify the delegation struct is signed by its declared
220
- // delegator. We don't want a delegate to be able to revoke
221
- // a hash that was never a real delegation.
222
- _validateSignature(delegation.delegator, dHash, delegation.signature);
377
+ /// @notice Revocation of a DOWNSTREAM link by an upstream delegator (audit DLG-9, second half).
378
+ /// `chain` is leaf-first exactly as it would be redeemed, truncated wherever the caller
379
+ /// likes: `chain[0]` is the link to revoke and `chain[chain.length - 1].delegator` must
380
+ /// be `msg.sender`.
381
+ ///
382
+ /// @dev A delegator can always cut its own link (and with it every chain through it) via
383
+ /// {revokeDelegationByOwner}. This is the finer tool: prune ONE re-delegation made
384
+ /// under your grant while the grant itself, and its other descendants, stay live.
385
+ /// Authorization is structural — every link must name the next by hash
386
+ /// (`chain[k].authority == hashDelegation(chain[k + 1])`), which proves `chain[0]`
387
+ /// claims its authority from the caller's delegation. No signature is checked, for the
388
+ /// same reason as the delegator path above (a dormant link must be revocable); the
389
+ /// only hashes reachable are of structs that could never be redeemed except THROUGH
390
+ /// the caller's own grant, so the caller forecloses nothing that was not its to give.
391
+ function revokeDelegationAsUpstream(Delegation[] calldata chain) external {
392
+ uint256 n = chain.length;
393
+ if (n < 2) revert EmptyChain();
394
+ if (msg.sender != chain[n - 1].delegator) revert NotDelegatorOrDelegate();
395
+ for (uint256 k = 0; k + 1 < n; k++) {
396
+ if (chain[k].authority != hashDelegation(chain[k + 1])) revert InvalidAuthority();
397
+ }
398
+ _revoke(hashDelegation(chain[0]));
399
+ }
223
400
 
401
+ function _revoke(bytes32 dHash) private {
224
402
  _revoked[dHash] = true;
225
403
  emit DelegationRevoked(dHash);
226
404
  emit DelegationRevokedBy(dHash, msg.sender);
@@ -244,13 +422,20 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
244
422
  d.salt
245
423
  )
246
424
  );
247
- return keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, structHash));
425
+ return keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR(), structHash));
248
426
  }
249
427
 
250
428
  /// @notice View-only verification of a delegation chain. Returns whether
251
429
  /// the chain is well-formed (signatures, authority chain, not
252
430
  /// revoked) WITHOUT executing it.
253
431
  ///
432
+ /// @dev ⚠️ DANGER — CHAIN-ONLY, NOT A CALL AUTHORIZATION (audit 2026-06-10). This validates ONLY
433
+ /// the chain (signatures / authority / revocation). It does NOT evaluate caveats, so a `true`
434
+ /// result is NOT permission to perform any specific (target, value, calldata). Anything
435
+ /// authorizing a concrete action MUST use {verifyAuthorizationForCall} (evaluates every caveat,
436
+ /// fail-closed) or live {redeemDelegation}. Treating this boolean as call authorization
437
+ /// re-opens the exact over-trust the caveats exist to prevent.
438
+ ///
254
439
  /// Spec 242 §6 (PD-9): the `attestations` package uses this to verify
255
440
  /// bilateral-consent delegations as signed authorization predicates,
256
441
  /// rather than redeeming them as cross-account execution. Returning
@@ -268,6 +453,8 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
268
453
  Delegation[] calldata delegations,
269
454
  address sender
270
455
  ) external view returns (bool ok, string memory reason) {
456
+ // Spec 450 §6: never answered from inside a caveat hook (see {verifyAuthorizationForCall}).
457
+ if (_redemptionLinkPlusOne != 0) return (false, "in-redemption");
271
458
  if (delegations.length == 0) return (false, "empty-chain");
272
459
 
273
460
  for (uint256 i = 0; i < delegations.length; i++) {
@@ -288,8 +475,12 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
288
475
  /// stateful enforcer attempting a write under staticcall) yields `false` — so `true`
289
476
  /// is a genuine authorization guarantee for this exact call, and `false` means "use
290
477
  /// live redemption" (which evaluates stateful caveats properly). The caveat
291
- /// evaluation mirrors `_runBeforeHooks` exactly (same args, same `sender`-as-redeemer),
292
- /// so an unknown/no-code enforcer is a no-op here just as it is in redemption. Pure
478
+ /// evaluation mirrors `_runBeforeHooks` exactly (same args, same `sender`-as-redeemer).
479
+ /// SC-3 (audit 2026-06-09): a no-code enforcer is rejected here with `false`. A raw
480
+ /// `staticcall` to an empty address SUCCEEDS, which would otherwise make a delegation
481
+ /// whose caveat enforcer is a typo / undeployed pass as "constraint-checked" — fail-OPEN.
482
+ /// Live redemption fails CLOSED on the same case (solc's extcodesize check on the
483
+ /// high-level void-returning hook call reverts), so this restores symmetry. Pure
293
484
  /// addition — `redeemDelegation` is untouched.
294
485
  /// @param delegations Chain (leaf first; root authority last)
295
486
  /// @param sender The address that would be the redeemer
@@ -303,7 +494,12 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
303
494
  uint256 value,
304
495
  bytes calldata data
305
496
  ) external view returns (bool ok, string memory reason) {
497
+ // Spec 450 §6: while a caveat hook of a redemption is running, {redemptionDepth} reports THAT chain's
498
+ // context, so an enforcer evaluated here would judge this chain by the outer one's numbers. Refused.
499
+ if (_redemptionLinkPlusOne != 0) return (false, "in-redemption");
306
500
  if (delegations.length == 0) return (false, "empty-chain");
501
+ // DLG-2: identical to redemption — the executing account is never the target.
502
+ if (target == delegations[delegations.length - 1].delegator) return (false, "self-target");
307
503
 
308
504
  for (uint256 i = 0; i < delegations.length; i++) {
309
505
  (bool valid, string memory r) = _validateDelegationView(delegations, i, sender);
@@ -312,6 +508,10 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
312
508
  Delegation calldata d = delegations[i];
313
509
  bytes32 dHash = hashDelegation(d);
314
510
  for (uint256 j = 0; j < d.caveats.length; j++) {
511
+ // SC-3: a no-code enforcer constrains NOTHING — a raw staticcall to an empty address
512
+ // returns success, which would fail-open. Reject explicitly so `true` is a genuine
513
+ // guarantee (live redemption fails closed here too via extcodesize on the high-level call).
514
+ if (d.caveats[j].enforcer.code.length == 0) return (false, "no-code-enforcer");
315
515
  // staticcall the same hook redemption would call; success == satisfied read-only.
316
516
  // Gas-bounded: a stateful enforcer (which would revert on its SSTORE under
317
517
  // staticcall) is capped so an on-chain caller (e.g. RW1-1) sees a cheap, predictable
@@ -353,17 +553,23 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
353
553
  if (d.delegate != OPEN_DELEGATION && d.delegate != sender) {
354
554
  return (false, "invalid-delegate");
355
555
  }
556
+ if (d.delegate != OPEN_DELEGATION && Eip7702.isDelegatedEOA(sender)) {
557
+ return (false, "delegated-eoa-redeemer");
558
+ }
356
559
  } else {
357
560
  if (d.delegate != OPEN_DELEGATION && d.delegate != delegations[i - 1].delegator) {
358
561
  return (false, "invalid-delegate-chain");
359
562
  }
360
563
  }
361
564
 
362
- // Authority chain
363
- if (d.authority != ROOT_AUTHORITY) {
364
- if (i + 1 >= delegations.length) return (false, "invalid-authority");
365
- bytes32 parentHash = hashDelegation(delegations[i + 1]);
366
- if (d.authority != parentHash) return (false, "invalid-authority-chain");
565
+ // Authority chain — exactly one root, and it is the LAST link (spec 445 §1.1, audit H-1).
566
+ // Every non-terminal link must name its parent by hash; a ROOT_AUTHORITY link in a
567
+ // non-terminal position is refused (it never re-delegated the parent's authority).
568
+ if (i + 1 < delegations.length) {
569
+ if (d.authority == ROOT_AUTHORITY) return (false, "root-authority-not-terminal");
570
+ if (d.authority != hashDelegation(delegations[i + 1])) return (false, "invalid-authority-chain");
571
+ } else {
572
+ if (d.authority != ROOT_AUTHORITY) return (false, "invalid-authority");
367
573
  }
368
574
 
369
575
  // Signature
@@ -382,8 +588,8 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
382
588
  bytes32 digest,
383
589
  bytes calldata signature
384
590
  ) internal view returns (bool) {
385
- // ERC-1271 path for smart accounts
386
- if (signer.code.length > 0) {
591
+ // ERC-1271 path for smart accounts; a 7702-delegated EOA signs by ECDSA only (spec 448 §0)
592
+ if (signer.code.length > 0 && !Eip7702.isDelegatedEOA(signer)) {
387
593
  try IERC1271(signer).isValidSignature(digest, signature) returns (bytes4 result) {
388
594
  return result == IERC1271.isValidSignature.selector;
389
595
  } catch {
@@ -392,8 +598,11 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
392
598
  }
393
599
  // EOA — tryRecover the eth-signed message hash
394
600
  bytes32 ethHash = digest.toEthSignedMessageHash();
601
+ // The error value is checked explicitly; the third return is OpenZeppelin's error ARGUMENT
602
+ // (the offending length / s value), which carries nothing to act on once `err` is known.
603
+ // slither-disable-next-line unused-return
395
604
  (address recovered, ECDSA.RecoverError err, ) = ECDSA.tryRecover(ethHash, signature);
396
- if (err != ECDSA.RecoverError.NoError) return false;
605
+ if (err != ECDSA.RecoverError.NoError || recovered == address(0)) return false;
397
606
  return recovered == signer;
398
607
  }
399
608
 
@@ -411,16 +620,24 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
411
620
 
412
621
  // Validate delegate
413
622
  if (i == 0) {
414
- if (d.delegate != OPEN_DELEGATION && d.delegate != msg.sender) revert InvalidDelegate();
623
+ if (d.delegate != OPEN_DELEGATION) {
624
+ if (d.delegate != msg.sender) revert InvalidDelegate();
625
+ // Spec 448 §0 (EIP-7702): a named delegate that is a delegated EOA is refused as the redeemer —
626
+ // anyone can make its attached code call, so its `msg.sender` proves nothing about its key.
627
+ Eip7702.refuseDelegatedEOA(msg.sender);
628
+ }
415
629
  } else {
416
630
  if (d.delegate != OPEN_DELEGATION && d.delegate != delegations[i - 1].delegator) revert InvalidDelegate();
417
631
  }
418
632
 
419
- // Validate authority chain
420
- if (d.authority != ROOT_AUTHORITY) {
421
- if (i + 1 >= delegations.length) revert InvalidAuthority();
422
- bytes32 parentHash = hashDelegation(delegations[i + 1]);
423
- if (d.authority != parentHash) revert InvalidAuthority();
633
+ // Validate authority chain — exactly one root, and it is the LAST link (spec 445 §1.1,
634
+ // audit H-1). Every non-terminal link must name its parent by hash, so a ROOT_AUTHORITY
635
+ // link spliced in front of someone else's root grant is refused: the middle delegator
636
+ // never re-delegated. The terminal link must be the root.
637
+ if (i + 1 < delegations.length) {
638
+ if (d.authority != hashDelegation(delegations[i + 1])) revert InvalidAuthority();
639
+ } else {
640
+ if (d.authority != ROOT_AUTHORITY) revert InvalidAuthority();
424
641
  }
425
642
 
426
643
  // Validate signature
@@ -439,6 +656,9 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
439
656
  ) internal {
440
657
  bytes32 dHash = hashDelegation(d);
441
658
  for (uint256 j = 0; j < d.caveats.length; j++) {
659
+ // EXT-NOCODE-HOOK-1: fail closed on a no-code enforcer (a call would succeed vacuously),
660
+ // symmetric with the view path's "no-code-enforcer" reject — never rely on call semantics.
661
+ if (d.caveats[j].enforcer.code.length == 0) revert NoCodeEnforcer(d.caveats[j].enforcer);
442
662
  ICaveatEnforcer(d.caveats[j].enforcer).beforeHook(
443
663
  d.caveats[j].terms,
444
664
  d.caveats[j].args,
@@ -460,6 +680,8 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
460
680
  ) internal {
461
681
  bytes32 dHash = hashDelegation(d);
462
682
  for (uint256 j = 0; j < d.caveats.length; j++) {
683
+ // EXT-NOCODE-HOOK-1: fail closed on a no-code enforcer, symmetric with the before-hook + view path.
684
+ if (d.caveats[j].enforcer.code.length == 0) revert NoCodeEnforcer(d.caveats[j].enforcer);
463
685
  ICaveatEnforcer(d.caveats[j].enforcer).afterHook(
464
686
  d.caveats[j].terms,
465
687
  d.caveats[j].args,
@@ -503,8 +725,9 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
503
725
  bytes32 digest,
504
726
  bytes calldata signature
505
727
  ) internal view {
506
- // ERC-1271 for smart accounts
507
- if (signer.code.length > 0) {
728
+ // ERC-1271 for smart accounts. Spec 448 §0 (EIP-7702): a delegated EOA's attached code is never asked
729
+ // "is this signature yours?" — it signs by ECDSA below, like any EOA.
730
+ if (signer.code.length > 0 && !Eip7702.isDelegatedEOA(signer)) {
508
731
  bytes4 result = IERC1271(signer).isValidSignature(digest, signature);
509
732
  if (result != IERC1271.isValidSignature.selector) revert DelegationManager_InvalidSignature();
510
733
  return;
@@ -11,6 +11,16 @@ pragma solidity ^0.8.28;
11
11
  *
12
12
  * @dev terms: Immutable parameters set when the delegation is created (e.g., time window, allowed methods).
13
13
  * args: Mutable parameters provided by the redeemer at redemption time (e.g., proof data).
14
+ *
15
+ * Two rules for enforcer authors (spec 445, audit DLG-2 / DLG-3 / DLG-8):
16
+ * - `args` are NOT signed. Anything that bounds the redeemer — a use count, a nonce set, a payer —
17
+ * must come from `terms` or from chain state, never from `args`: the party a caveat constrains
18
+ * is the party that writes them.
19
+ * - `delegator` is the delegator of the LINK that carries the caveat. In a re-delegated chain the
20
+ * call executes from the ROOT delegator's account, which a stateful enforcer can read from
21
+ * `DelegationManager.redemptionRootDelegator()` during a redemption. The hooks see exactly the
22
+ * one `(target, value, callData)` being executed; the manager guarantees that is the whole
23
+ * call by refusing a target equal to the executing account.
14
24
  */
15
25
  interface ICaveatEnforcer {
16
26
  /**