@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.
- package/AUDIT.md +119 -27
- package/README.md +47 -12
- package/deployments-base-sepolia.json +52 -1
- package/deployments-faithchain.json +103 -0
- package/deployments-hopechain.json +78 -0
- package/deployments-paymaster-anvil.json +1 -0
- package/deployments-registries-base-sepolia.json +1 -0
- package/deployments-registries-hopechain.json +1 -0
- package/dist/abi/AccountBootstrap.json +99 -0
- package/dist/abi/AgentAccount.json +461 -67
- package/dist/abi/AgentAccountFactory.json +309 -0
- package/dist/abi/AgentNameAttributeResolver.json +212 -0
- package/dist/abi/AgentNameRegistry.json +544 -0
- package/dist/abi/AgentNameUniversalResolver.json +222 -0
- package/dist/abi/AgentProfileResolver.json +236 -0
- package/dist/abi/AgentRegistryBase.json +1003 -0
- package/dist/abi/AgentRelationship.json +405 -0
- package/dist/abi/AgenticGovernance.json +155 -0
- package/dist/abi/AgreementRegistry.json +203 -5
- package/dist/abi/AllowedMethodsEnforcer.json +193 -0
- package/dist/abi/AllowedTargetsEnforcer.json +193 -0
- package/dist/abi/ApprovedHashRegistry.json +267 -2
- package/dist/abi/AttestationRegistry.json +242 -0
- package/dist/abi/AttributeStorage.json +24 -0
- package/dist/abi/CallDataHashEnforcer.json +193 -0
- package/dist/abi/CustodyPolicy.json +422 -55
- package/dist/abi/DelegationManager.json +292 -0
- package/dist/abi/DigestBindingEnforcer.json +541 -0
- package/dist/abi/Eip7702.json +13 -0
- package/dist/abi/EstateProjectionRegistry.json +599 -0
- package/dist/abi/Generation5Bridge.json +355 -0
- package/dist/abi/GeoFeatureRegistry.json +235 -0
- package/dist/abi/IAgentAccountFactoryView.json +26 -0
- package/dist/abi/IAgentAccountGovernanceTimelockView.json +15 -0
- package/dist/abi/IApprovedHashRegistryWrite.json +35 -0
- package/dist/abi/ICustodyEpochView.json +28 -0
- package/dist/abi/ICustodyPolicyAccountModuleView.json +31 -0
- package/dist/abi/ICustodyPolicyFactoryView.json +13 -0
- package/dist/abi/ICustodyPolicyPasskeyView.json +50 -0
- package/dist/abi/IDelegationManager.json +18 -0
- package/dist/abi/IGeneration4Reads.json +143 -0
- package/dist/abi/IGeneration5Account.json +33 -0
- package/dist/abi/IGeneration5Voucher.json +26 -0
- package/dist/abi/IMoveAccountView.json +57 -0
- package/dist/abi/IMoveCustodyView.json +169 -0
- package/dist/abi/IMoveFactoryView.json +41 -0
- package/dist/abi/IPaymentReceiptRegistry.json +50 -0
- package/dist/abi/IRedemptionContext.json +15 -0
- package/dist/abi/IRegistryMembershipPolicy.json +70 -0
- package/dist/abi/ITimelockVeto.json +93 -0
- package/dist/abi/ITreasurySpendPolicyAccountView.json +44 -0
- package/dist/abi/IUpgradeVoucherView.json +26 -0
- package/dist/abi/MockUSDC.json +621 -0
- package/dist/abi/OntologyTermRegistry.json +268 -0
- package/dist/abi/PayloadClassesEnforcer.json +310 -0
- package/dist/abi/PaymentEnforcer.json +639 -0
- package/dist/abi/PaymentEscrow.json +501 -0
- package/dist/abi/PaymentReceiptRegistry.json +388 -0
- package/dist/abi/PermissionlessSubregistry.json +353 -0
- package/dist/abi/PricedSubregistry.json +1141 -0
- package/dist/abi/QuorumEnforcer.json +209 -0
- package/dist/abi/ReceiptAnchorRegistry.json +346 -0
- package/dist/abi/RelationshipTypeRegistry.json +268 -0
- package/dist/abi/ShapeRegistry.json +268 -0
- package/dist/abi/SignatureVerifier.json +196 -0
- package/dist/abi/SkillDefinitionRegistry.json +235 -0
- package/dist/abi/SmartAgentPaymaster.json +445 -1
- package/dist/abi/StableProxyGoverned.json +190 -0
- package/dist/abi/StableProxyGovernedOneShot.json +172 -0
- package/dist/abi/TimestampEnforcer.json +193 -0
- package/dist/abi/TreasurySpendPolicy.json +667 -0
- package/dist/abi/UpgradeVoucher.json +328 -0
- package/dist/abi/ValueEnforcer.json +193 -0
- package/dist/abi/index.d.ts +35 -0
- package/dist/abi/index.js +35 -0
- package/dist/deployments/base-sepolia.d.ts +11 -0
- package/dist/deployments/base-sepolia.js +51 -28
- package/dist/deployments/faithchain.d.ts +77 -0
- package/dist/deployments/faithchain.js +107 -0
- package/dist/deployments/hopechain.d.ts +68 -0
- package/dist/deployments/hopechain.js +82 -0
- package/dist/deployments/index.d.ts +7 -2
- package/dist/deployments/index.js +15 -0
- package/dist/deployments/paymaster-anvil.d.ts +11 -0
- package/dist/deployments/paymaster-anvil.js +9 -0
- package/dist/deployments/registries-base-sepolia.d.ts +10 -0
- package/dist/deployments/registries-base-sepolia.js +8 -0
- package/dist/deployments/registries-hopechain.d.ts +10 -0
- package/dist/deployments/registries-hopechain.js +8 -0
- package/package.json +37 -7
- package/src/AccountBootstrap.sol +38 -0
- package/src/AgentAccount.sol +1091 -134
- package/src/AgentAccountFactory.sol +198 -21
- package/src/ApprovedHashRegistry.sol +134 -10
- package/src/IAgentAccount.sol +37 -0
- package/src/SmartAgentPaymaster.sol +275 -22
- package/src/UniversalSignatureValidator.sol +7 -0
- package/src/UpgradeVoucher.sol +87 -0
- package/src/agency/DelegationManager.sol +299 -76
- package/src/agency/ICaveatEnforcer.sol +10 -0
- package/src/agency/IDelegationManager.sol +37 -5
- package/src/agreement/AgreementRegistry.sol +105 -35
- package/src/attestation/AttestationRegistry.sol +220 -40
- package/src/custody/CustodyPolicy.sol +588 -70
- package/src/custody/TreasurySpendPolicy.sol +338 -0
- package/src/enforcers/AllowedMethodsEnforcer.sol +13 -2
- package/src/enforcers/AllowedTargetsEnforcer.sol +13 -2
- package/src/enforcers/CallDataHashEnforcer.sol +7 -1
- package/src/enforcers/DigestBindingEnforcer.sol +182 -0
- package/src/enforcers/PayloadClassesEnforcer.sol +36 -0
- package/src/enforcers/PaymentEnforcer.sol +291 -0
- package/src/enforcers/QuorumEnforcer.sol +24 -3
- package/src/enforcers/TimestampEnforcer.sol +7 -1
- package/src/enforcers/ValueEnforcer.sol +13 -2
- package/src/geo/GeoFeatureRegistry.sol +67 -22
- package/src/governance/AgenticGovernance.sol +187 -4
- package/src/governance/GovernanceManaged.sol +2 -0
- package/src/governance/StableProxyGoverned.sol +79 -0
- package/src/governance/StableProxyGovernedOneShot.sol +93 -0
- package/src/identity/AgentProfilePredicates.sol +23 -0
- package/src/identity/AgentProfileResolver.sol +22 -2
- package/src/libraries/Eip7702.sol +36 -0
- package/src/libraries/SignatureSlotRecovery.sol +28 -7
- package/src/libraries/SignatureVerifier.sol +52 -0
- package/src/libraries/WebAuthnLib.sol +16 -0
- package/src/migration/Generation5Bridge.sol +364 -0
- package/src/mocks/MockUSDC.sol +152 -0
- package/src/naming/AgentNameAttributeResolver.sol +29 -4
- package/src/naming/AgentNamePredicates.sol +89 -0
- package/src/naming/AgentNameRegistry.sol +324 -20
- package/src/naming/AgentNameUniversalResolver.sol +35 -2
- package/src/naming/PermissionlessSubregistry.sol +82 -25
- package/src/naming/PricedSubregistry.sol +293 -0
- package/src/ontology/AttributeStorage.sol +93 -80
- package/src/ontology/OntologyTermRegistry.sol +58 -2
- package/src/ontology/ShapeRegistry.sol +58 -2
- package/src/payments/PaymentEscrow.sol +187 -0
- package/src/payments/PaymentReceiptRegistry.sol +100 -0
- package/src/provenance/ReceiptAnchorRegistry.sol +67 -0
- package/src/registry/AgentRegistryBase.sol +372 -0
- package/src/registry/EstateProjectionRegistry.sol +177 -0
- package/src/registry/IRegistryMembershipPolicy.sol +30 -0
- package/src/relationships/AgentRelationship.sol +129 -8
- package/src/relationships/RelationshipTypeRegistry.sol +58 -2
- package/src/skills/SkillDefinitionRegistry.sol +61 -17
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
import "../agency/ICaveatEnforcer.sol";
|
|
5
|
+
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
6
|
+
import "../governance/StableProxyGoverned.sol";
|
|
7
|
+
|
|
8
|
+
/// @dev The one thing this enforcer asks of its DelegationManager beyond calling the hooks: whose
|
|
9
|
+
/// account executes the redemption in flight (audit DLG-8c). Attribution only.
|
|
10
|
+
interface IRedemptionContext {
|
|
11
|
+
function redemptionRootDelegator() external view returns (address);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface IPaymentReceiptRegistry {
|
|
15
|
+
function record(
|
|
16
|
+
bytes32 mandateId,
|
|
17
|
+
address payer,
|
|
18
|
+
address payee,
|
|
19
|
+
address asset,
|
|
20
|
+
uint256 amount,
|
|
21
|
+
bytes32 resourceHash,
|
|
22
|
+
bytes32 delegationHash,
|
|
23
|
+
bytes32 nonce
|
|
24
|
+
) external;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @title PaymentEnforcer
|
|
29
|
+
* @notice x402 pay-per-use caveat enforcer (spec 272, PAY-CON-1). Constrains a redeemed PAYMENT
|
|
30
|
+
* delegation to EXACTLY a capped `IERC20.transfer(treasury, amount)`, with a per-delegation
|
|
31
|
+
* aggregate spend cap, a windowed frequency cap, and a single-use nonce (on-chain replay
|
|
32
|
+
* guard). Fuses smart-agent's AllocationLimit (spend) + RateLimit (window) into one
|
|
33
|
+
* `beforeHook` so spend, frequency, and binding are checked ATOMICALLY — a charge that passes
|
|
34
|
+
* the rate limit but busts the budget reverts the whole redemption as a unit (spec §2 divergence).
|
|
35
|
+
*
|
|
36
|
+
* @dev terms = abi.encode(
|
|
37
|
+
* address treasury, address asset, uint256 maxAmountPerCharge,
|
|
38
|
+
* uint256 maxAggregate, uint32 maxRedemptionsPerWindow, uint32 windowSeconds) // 192 bytes
|
|
39
|
+
* args = abi.encode(bytes32 mandateId, bytes32 nonce, bytes32 resourceHash)
|
|
40
|
+
*
|
|
41
|
+
* State is keyed [delegator][delegationHash] so sibling delegations never cross-interfere
|
|
42
|
+
* (a redeemer can't bypass the cap by re-anchoring under a different delegation).
|
|
43
|
+
*
|
|
44
|
+
* Fail-closed (spec §9): wrong/zero asset or treasury, non-`transfer` calldata, wrong
|
|
45
|
+
* recipient/amount, ANY cap breach, or a reused nonce reverts and blocks the whole
|
|
46
|
+
* `DelegationManager.redeemDelegation`. The state SSTOREs are atomic with the underlying
|
|
47
|
+
* transfer — if Phase-2 execution reverts, the budget/nonce writes revert with it.
|
|
48
|
+
*
|
|
49
|
+
* Spec 445 / audit DLG-8:
|
|
50
|
+
* (a) `mandateId` and `resourceHash` come from `args`, which the delegator did NOT sign. They
|
|
51
|
+
* are the REDEEMER'S ASSERTION of what the charge was for, carried into the receipt so a
|
|
52
|
+
* reader can match it against off-chain records — never proof of it. What a receipt
|
|
53
|
+
* proves is the signed part: `amount` of `asset` moved from `payer` to `treasury` under
|
|
54
|
+
* `delegationHash`, once, for `nonce`;
|
|
55
|
+
* (b) a zero-amount charge is refused (`ZeroAmount`) — it satisfied every check including the
|
|
56
|
+
* M1 balance observation and wrote a receipt for nothing;
|
|
57
|
+
* (c) the receipt's `payer` is the ROOT delegator — the account the DelegationManager
|
|
58
|
+
* executes through and whose balance moves — read from the manager's redemption
|
|
59
|
+
* context. The hook's `delegator` is the delegator of the LINK that carries this caveat;
|
|
60
|
+
* on a derived (re-delegated) mandate that is an intermediary which paid nothing. Budget,
|
|
61
|
+
* window and nonce stay keyed by that link (`[delegator][delegationHash]`): they bound
|
|
62
|
+
* the grant the link's delegator made, whoever's funds move.
|
|
63
|
+
*
|
|
64
|
+
* `transfer`-only by construction (spec §163): `target == asset` rejects multicall/router
|
|
65
|
+
* targets; the 68-byte length + selector check rejects `transferFrom`/`approve`/extra-arg
|
|
66
|
+
* smuggling; `value == 0` rejects ETH; the DelegationManager's single (target,value,data)
|
|
67
|
+
* redemption shape rejects batch/delegatecall.
|
|
68
|
+
*/
|
|
69
|
+
contract PaymentEnforcer is ICaveatEnforcer, StableProxyGoverned {
|
|
70
|
+
bytes4 private constant TRANSFER_SELECTOR = IERC20.transfer.selector; // 0xa9059cbb
|
|
71
|
+
|
|
72
|
+
struct Budget {
|
|
73
|
+
uint256 spent; // cumulative against maxAggregate
|
|
74
|
+
uint64 windowStart; // start of the current frequency window
|
|
75
|
+
uint32 callsInWindow; // redemptions so far in the current window
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// delegator => delegationHash => budget/window state
|
|
79
|
+
mapping(address => mapping(bytes32 => Budget)) private _budgets;
|
|
80
|
+
/// delegator => delegationHash => nonce => consumed (on-chain replay guard)
|
|
81
|
+
mapping(address => mapping(bytes32 => mapping(bytes32 => bool))) private _nonceUsed;
|
|
82
|
+
|
|
83
|
+
/// @notice M1 (seam audit): keccak256(delegator, delegationHash, nonce) => (treasury balanceBefore + 1).
|
|
84
|
+
/// Snapshotted in `beforeHook`; consumed in `afterHook` to verify the transfer ACTUALLY moved
|
|
85
|
+
/// `amount` to the treasury before the settlement receipt is written. `+1` sentinel so a set
|
|
86
|
+
/// entry is distinguishable from an unset slot even when the treasury balance is 0. Set and
|
|
87
|
+
/// deleted within a single non-reentrant `redeemDelegation`, so it never persists across txs.
|
|
88
|
+
mapping(bytes32 => uint256) private _settleSnap;
|
|
89
|
+
/// @dev Storage gap (spec 447: behind a stable proxy; layout gated by `check:storage-layouts`).
|
|
90
|
+
uint256[47] private __gap;
|
|
91
|
+
|
|
92
|
+
/// @notice The ONLY authorized caller of the hooks (EXT-PMT-1 / C-NEW-1). Unlike the stateless
|
|
93
|
+
/// enforcers (timestamp/value/targets/methods), this enforcer MUTATES state (budget, nonce)
|
|
94
|
+
/// and writes a settlement receipt — so it is NOT safe to call externally. A direct caller
|
|
95
|
+
/// that is not the DelegationManager could otherwise forge a `PaymentSettled` receipt and
|
|
96
|
+
/// grief the victim's budget/nonce with no actual transfer. Bound immutably at deploy; the
|
|
97
|
+
/// hooks revert `NotDelegationManager` for any other `msg.sender`.
|
|
98
|
+
address public immutable delegationManager;
|
|
99
|
+
|
|
100
|
+
/// @notice Trustless receipt sink (PAY-CON-4). Immutable; `address(0)` disables registry receipts
|
|
101
|
+
/// (the `PaymentCharged` event below is always the primary on-chain receipt).
|
|
102
|
+
IPaymentReceiptRegistry public immutable receiptRegistry;
|
|
103
|
+
|
|
104
|
+
event PaymentCharged(
|
|
105
|
+
address indexed delegator,
|
|
106
|
+
bytes32 indexed delegationHash,
|
|
107
|
+
bytes32 indexed mandateId,
|
|
108
|
+
address treasury,
|
|
109
|
+
address asset,
|
|
110
|
+
uint256 amount,
|
|
111
|
+
bytes32 nonce,
|
|
112
|
+
bytes32 resourceHash,
|
|
113
|
+
uint256 totalSpent,
|
|
114
|
+
uint32 callsInWindow
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
error InvalidTerms();
|
|
118
|
+
error ValueNotZero();
|
|
119
|
+
error AssetMismatch(address expectedAsset, address target);
|
|
120
|
+
error NotTransferCall();
|
|
121
|
+
error WrongRecipient(address treasury, address to);
|
|
122
|
+
error ChargeExceedsMax(uint256 amount, uint256 maxAmountPerCharge);
|
|
123
|
+
error AggregateExceeded(uint256 spent, uint256 maxAggregate, uint256 amount);
|
|
124
|
+
error FrequencyExceeded(uint32 callsInWindow, uint32 maxRedemptionsPerWindow);
|
|
125
|
+
error NonceReused(bytes32 nonce);
|
|
126
|
+
/// @notice DLG-8b: a charge of zero moves nothing and may not write a receipt.
|
|
127
|
+
error ZeroAmount();
|
|
128
|
+
/// @notice DLG-8c: the manager reported no redemption in flight, so there is no payer to name.
|
|
129
|
+
error NoRedemptionInFlight();
|
|
130
|
+
error NotDelegationManager(address caller);
|
|
131
|
+
/// @notice M1 (seam audit): the transfer did not move `amount` to the treasury (a token that returns
|
|
132
|
+
/// false without reverting, a fee-on-transfer skim, or a missing beforeHook snapshot) — so no
|
|
133
|
+
/// settlement receipt is written and the whole redemption reverts.
|
|
134
|
+
error SettlementNotObserved();
|
|
135
|
+
|
|
136
|
+
/// @dev The hooks are state-mutating + receipt-emitting, so ONLY the DelegationManager may invoke
|
|
137
|
+
/// them (EXT-PMT-1). `_runBeforeHooks`/`_runAfterHooks` call the enforcer directly, so during a
|
|
138
|
+
/// real `redeemDelegation` msg.sender IS the DelegationManager. The `verifyAuthorization` view
|
|
139
|
+
/// path staticcalls the hook from the same DelegationManager and still reverts on the SSTORE
|
|
140
|
+
/// under staticcall (yields `false`), exactly as before — the modifier does not change that.
|
|
141
|
+
modifier onlyDelegationManager() {
|
|
142
|
+
if (msg.sender != delegationManager) revert NotDelegationManager(msg.sender);
|
|
143
|
+
_;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
constructor(address delegationManager_, address receiptRegistry_) initializer {
|
|
147
|
+
require(delegationManager_ != address(0), "PaymentEnforcer: zero DelegationManager");
|
|
148
|
+
delegationManager = delegationManager_;
|
|
149
|
+
receiptRegistry = IPaymentReceiptRegistry(receiptRegistry_);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function beforeHook(
|
|
153
|
+
bytes calldata terms,
|
|
154
|
+
bytes calldata args,
|
|
155
|
+
bytes32 delegationHash,
|
|
156
|
+
address delegator,
|
|
157
|
+
address, /*redeemer*/
|
|
158
|
+
address target,
|
|
159
|
+
uint256 value,
|
|
160
|
+
bytes calldata callData
|
|
161
|
+
) external override onlyDelegationManager {
|
|
162
|
+
// ── terms (immutable, set at delegation creation) ──
|
|
163
|
+
if (terms.length != 192) revert InvalidTerms();
|
|
164
|
+
(
|
|
165
|
+
address treasury,
|
|
166
|
+
address asset,
|
|
167
|
+
uint256 maxPerCharge,
|
|
168
|
+
uint256 maxAggregate,
|
|
169
|
+
uint32 maxPerWindow,
|
|
170
|
+
uint32 windowSeconds
|
|
171
|
+
) = abi.decode(terms, (address, address, uint256, uint256, uint32, uint32));
|
|
172
|
+
if (
|
|
173
|
+
treasury == address(0) || asset == address(0) || maxPerCharge == 0 || maxAggregate < maxPerCharge
|
|
174
|
+
|| maxPerWindow == 0 || windowSeconds == 0
|
|
175
|
+
) {
|
|
176
|
+
revert InvalidTerms();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ── calldata MUST be exactly IERC20.transfer(treasury, amount): single call, no ETH ──
|
|
180
|
+
if (value != 0) revert ValueNotZero();
|
|
181
|
+
if (target != asset) revert AssetMismatch(asset, target);
|
|
182
|
+
if (callData.length != 68) revert NotTransferCall(); // 4 + 32 + 32; rejects transferFrom/approve/extra args
|
|
183
|
+
if (bytes4(callData[0:4]) != TRANSFER_SELECTOR) revert NotTransferCall();
|
|
184
|
+
(address to, uint256 amount) = abi.decode(callData[4:], (address, uint256));
|
|
185
|
+
if (to != treasury) revert WrongRecipient(treasury, to);
|
|
186
|
+
if (amount == 0) revert ZeroAmount();
|
|
187
|
+
if (amount > maxPerCharge) revert ChargeExceedsMax(amount, maxPerCharge);
|
|
188
|
+
|
|
189
|
+
// ── replay: single-use nonce per delegation (on-chain idempotency, PAY-RAIL-4) ──
|
|
190
|
+
(bytes32 mandateId, bytes32 nonce, bytes32 resourceHash) = abi.decode(args, (bytes32, bytes32, bytes32));
|
|
191
|
+
if (_nonceUsed[delegator][delegationHash][nonce]) revert NonceReused(nonce);
|
|
192
|
+
_nonceUsed[delegator][delegationHash][nonce] = true;
|
|
193
|
+
|
|
194
|
+
Budget storage b = _budgets[delegator][delegationHash];
|
|
195
|
+
|
|
196
|
+
// ── aggregate spend cap (port AllocationLimitEnforcer) ──
|
|
197
|
+
uint256 nextSpent = b.spent + amount;
|
|
198
|
+
if (nextSpent > maxAggregate) revert AggregateExceeded(b.spent, maxAggregate, amount);
|
|
199
|
+
|
|
200
|
+
// ── windowed frequency cap; roll on expiry (port RateLimitEnforcer) ──
|
|
201
|
+
uint64 nowTs = uint64(block.timestamp);
|
|
202
|
+
if (b.windowStart == 0 || nowTs >= b.windowStart + windowSeconds) {
|
|
203
|
+
b.windowStart = nowTs;
|
|
204
|
+
b.callsInWindow = 0;
|
|
205
|
+
}
|
|
206
|
+
uint32 nextCalls = b.callsInWindow + 1;
|
|
207
|
+
if (nextCalls > maxPerWindow) revert FrequencyExceeded(b.callsInWindow, maxPerWindow);
|
|
208
|
+
|
|
209
|
+
b.spent = nextSpent;
|
|
210
|
+
b.callsInWindow = nextCalls;
|
|
211
|
+
|
|
212
|
+
emit PaymentCharged(
|
|
213
|
+
delegator, delegationHash, mandateId, treasury, asset, amount, nonce, resourceHash, nextSpent, nextCalls
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
// M1 (seam audit): snapshot the treasury balance BEFORE the transfer. The trustless settlement
|
|
217
|
+
// receipt (PAY-CON-4) is written in `afterHook` ONLY once the transfer is observed to have moved
|
|
218
|
+
// `amount` to the treasury — a token that returns false without reverting (or a fee-on-transfer
|
|
219
|
+
// skim) then records NO receipt and reverts the whole redemption, so "a settled charge always has
|
|
220
|
+
// a receipt and an unsettled one never does" holds against the value that ACTUALLY moved.
|
|
221
|
+
_settleSnap[_settleKey(delegator, delegationHash, nonce)] = IERC20(asset).balanceOf(treasury) + 1;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function afterHook(
|
|
225
|
+
bytes calldata terms,
|
|
226
|
+
bytes calldata args,
|
|
227
|
+
bytes32 delegationHash,
|
|
228
|
+
address delegator,
|
|
229
|
+
address, /*redeemer*/
|
|
230
|
+
address, /*target*/
|
|
231
|
+
uint256, /*value*/
|
|
232
|
+
bytes calldata callData
|
|
233
|
+
) external override onlyDelegationManager {
|
|
234
|
+
// Only this enforcer's own 192-byte-terms caveat carries a snapshot (beforeHook validated the
|
|
235
|
+
// shape); ignore anything else defensively so a malformed sibling caveat can't revert settlement.
|
|
236
|
+
if (terms.length != 192) return;
|
|
237
|
+
(address treasury, address asset,,,,) = abi.decode(terms, (address, address, uint256, uint256, uint32, uint32));
|
|
238
|
+
(bytes32 mandateId, bytes32 nonce, bytes32 resourceHash) = abi.decode(args, (bytes32, bytes32, bytes32));
|
|
239
|
+
(, uint256 amount) = abi.decode(callData[4:], (address, uint256));
|
|
240
|
+
|
|
241
|
+
bytes32 key = _settleKey(delegator, delegationHash, nonce);
|
|
242
|
+
uint256 snap = _settleSnap[key];
|
|
243
|
+
if (snap == 0) revert SettlementNotObserved(); // no beforeHook snapshot (defensive)
|
|
244
|
+
delete _settleSnap[key];
|
|
245
|
+
|
|
246
|
+
// The treasury must have received AT LEAST `amount` since the snapshot. A silent transfer failure
|
|
247
|
+
// (balance unchanged) or a fee-on-transfer short-delivery fails this and reverts the redemption.
|
|
248
|
+
if (IERC20(asset).balanceOf(treasury) < (snap - 1) + amount) revert SettlementNotObserved();
|
|
249
|
+
|
|
250
|
+
if (address(receiptRegistry) != address(0)) {
|
|
251
|
+
// DLG-8c: the payer is the account that executed the transfer (the root delegator), not the
|
|
252
|
+
// delegator of whichever link carries this caveat. One source, no fallback: a manager that
|
|
253
|
+
// reports no redemption in flight cannot have executed one.
|
|
254
|
+
address payer = IRedemptionContext(delegationManager).redemptionRootDelegator();
|
|
255
|
+
if (payer == address(0)) revert NoRedemptionInFlight();
|
|
256
|
+
receiptRegistry.record(mandateId, payer, treasury, asset, amount, resourceHash, delegationHash, nonce);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/// @dev Unique per redemption — the nonce is single-use per [delegator][delegationHash].
|
|
261
|
+
function _settleKey(address delegator, bytes32 delegationHash, bytes32 nonce) private pure returns (bytes32) {
|
|
262
|
+
return keccak256(abi.encode(delegator, delegationHash, nonce));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// ── views (off-chain budget display + idempotency checks) ──
|
|
266
|
+
|
|
267
|
+
function getBudget(address delegator, bytes32 delegationHash)
|
|
268
|
+
external
|
|
269
|
+
view
|
|
270
|
+
returns (uint256 spent, uint64 windowStart, uint32 callsInWindow)
|
|
271
|
+
{
|
|
272
|
+
Budget storage b = _budgets[delegator][delegationHash];
|
|
273
|
+
return (b.spent, b.windowStart, b.callsInWindow);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function isNonceUsed(address delegator, bytes32 delegationHash, bytes32 nonce) external view returns (bool) {
|
|
277
|
+
return _nonceUsed[delegator][delegationHash][nonce];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/// @notice Encode the immutable caveat terms (mirrors `delegation.buildPaymentMandateCaveats`).
|
|
281
|
+
function encodeTerms(
|
|
282
|
+
address treasury,
|
|
283
|
+
address asset,
|
|
284
|
+
uint256 maxAmountPerCharge,
|
|
285
|
+
uint256 maxAggregate,
|
|
286
|
+
uint32 maxRedemptionsPerWindow,
|
|
287
|
+
uint32 windowSeconds
|
|
288
|
+
) external pure returns (bytes memory) {
|
|
289
|
+
return abi.encode(treasury, asset, maxAmountPerCharge, maxAggregate, maxRedemptionsPerWindow, windowSeconds);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -3,6 +3,7 @@ pragma solidity ^0.8.28;
|
|
|
3
3
|
|
|
4
4
|
import "../agency/ICaveatEnforcer.sol";
|
|
5
5
|
import "../libraries/SignatureSlotRecovery.sol";
|
|
6
|
+
import "../governance/StableProxyGoverned.sol";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @title QuorumEnforcer
|
|
@@ -77,11 +78,19 @@ import "../libraries/SignatureSlotRecovery.sol";
|
|
|
77
78
|
* dispatch. See `docs/audits/r6-contracts-recon-2026-05-31.md`
|
|
78
79
|
* § 4.4 + `test/EnforcerPauseInvariantR67.t.sol`.
|
|
79
80
|
*/
|
|
80
|
-
contract QuorumEnforcer is ICaveatEnforcer {
|
|
81
|
+
contract QuorumEnforcer is ICaveatEnforcer, StableProxyGoverned {
|
|
82
|
+
/// @notice A direct deployment, initialized here (no governance: never upgradeable). Behind a stable proxy
|
|
83
|
+
/// (spec 447), `initialize(governance_)` instead — an enforcer's address is in every delegation that
|
|
84
|
+
/// names it, so a fix must keep the address.
|
|
85
|
+
constructor() initializer {}
|
|
86
|
+
|
|
81
87
|
error InsufficientQuorum(uint256 supplied, uint8 threshold);
|
|
82
88
|
error UnauthorizedSigner(address signer);
|
|
83
89
|
error DuplicateOrUnsortedSigner(address signer);
|
|
84
90
|
error PayloadHashMismatch(bytes32 expected, bytes32 supplied);
|
|
91
|
+
/// @notice EN-11: a degenerate quorum caveat (threshold 0, or a threshold the
|
|
92
|
+
/// signer set can never satisfy) must fail closed, not pass with zero sigs.
|
|
93
|
+
error InvalidThreshold(uint8 threshold, uint256 signerSetSize);
|
|
85
94
|
|
|
86
95
|
/// @notice Typehash for the quorum-action binding (contract audit C-4).
|
|
87
96
|
/// @dev Bound to (chainId, enforcer, delegationHash, delegator, redeemer,
|
|
@@ -135,6 +144,15 @@ contract QuorumEnforcer is ICaveatEnforcer {
|
|
|
135
144
|
abi.decode(terms, (address[], uint8, address));
|
|
136
145
|
(bytes32 suppliedPayloadHash, bytes memory signatures) = abi.decode(args, (bytes32, bytes));
|
|
137
146
|
|
|
147
|
+
// EN-11: fail closed on a degenerate quorum. threshold == 0 would skip the
|
|
148
|
+
// verification loop entirely AND make `signatures.length < 0` unreachable, so
|
|
149
|
+
// the caveat would pass with ZERO signatures. A threshold larger than the
|
|
150
|
+
// signer set is unsatisfiable; reject it up front rather than running out of
|
|
151
|
+
// slots mid-loop.
|
|
152
|
+
if (threshold == 0 || threshold > signerSet.length) {
|
|
153
|
+
revert InvalidThreshold(threshold, signerSet.length);
|
|
154
|
+
}
|
|
155
|
+
|
|
138
156
|
// Contract audit C-4: bind the signed payload to the actual
|
|
139
157
|
// execution context. Previously the redeemer chose payloadHash
|
|
140
158
|
// freely, so a quorum signed off-chain to "approve a transfer
|
|
@@ -161,13 +179,16 @@ contract QuorumEnforcer is ICaveatEnforcer {
|
|
|
161
179
|
revert InsufficientQuorum(signatures.length / 65, threshold);
|
|
162
180
|
}
|
|
163
181
|
|
|
164
|
-
address prev;
|
|
182
|
+
address prev = address(0); // explicit: strictly-ascending signer order starts above zero
|
|
165
183
|
for (uint256 i; i < threshold; i++) {
|
|
166
184
|
address signer = SignatureSlotRecovery.recoverFromSlot(
|
|
167
185
|
expectedPayloadHash,
|
|
168
186
|
signatures,
|
|
169
187
|
i,
|
|
170
|
-
approvedHashRegistry
|
|
188
|
+
approvedHashRegistry,
|
|
189
|
+
// Spec 302 W2 — quorum passkey signers are custodians OF THE
|
|
190
|
+
// DELEGATOR account; their v=2 PIA is scoped to it.
|
|
191
|
+
delegator
|
|
171
192
|
);
|
|
172
193
|
|
|
173
194
|
// Sorted-ascending check (also rejects duplicates).
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
4
|
import "../agency/ICaveatEnforcer.sol";
|
|
5
|
+
import "../governance/StableProxyGoverned.sol";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* @title TimestampEnforcer
|
|
@@ -16,7 +17,12 @@ import "../agency/ICaveatEnforcer.sol";
|
|
|
16
17
|
* dispatch. See `docs/audits/r6-contracts-recon-2026-05-31.md`
|
|
17
18
|
* § 4.4 + `test/EnforcerPauseInvariantR67.t.sol`.
|
|
18
19
|
*/
|
|
19
|
-
contract TimestampEnforcer is ICaveatEnforcer {
|
|
20
|
+
contract TimestampEnforcer is ICaveatEnforcer, StableProxyGoverned {
|
|
21
|
+
/// @notice A direct deployment, initialized here (no governance: never upgradeable). Behind a stable proxy
|
|
22
|
+
/// (spec 447), `initialize(governance_)` instead — an enforcer's address is in every delegation that
|
|
23
|
+
/// names it, so a fix must keep the address.
|
|
24
|
+
constructor() initializer {}
|
|
25
|
+
|
|
20
26
|
error TimestampNotYetValid();
|
|
21
27
|
error TimestampExpired();
|
|
22
28
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
pragma solidity ^0.8.28;
|
|
3
3
|
|
|
4
4
|
import "../agency/ICaveatEnforcer.sol";
|
|
5
|
+
import "../governance/StableProxyGoverned.sol";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* @title ValueEnforcer
|
|
@@ -13,9 +14,19 @@ import "../agency/ICaveatEnforcer.sol";
|
|
|
13
14
|
* (hooks are `pure`, contract has zero storage). The DM-side pause
|
|
14
15
|
* check in `DelegationManager.redeemDelegation` gates all
|
|
15
16
|
* reachable enforcer dispatch. See `docs/audits/r6-contracts-recon-2026-05-31.md`
|
|
16
|
-
* § 4.4 + `test/EnforcerPauseInvariantR67.t.sol`.
|
|
17
|
+
* § 4.4 + `test/EnforcerPauseInvariantR67.t.sol`. *
|
|
18
|
+
* Spec 445 / audit DLG-2 — SCOPE. This enforcer judges the ONE `(target, value, callData)` the
|
|
19
|
+
* DelegationManager was asked to execute, nothing nested inside it. That is the whole call a
|
|
20
|
+
* delegation can cause only because the manager refuses a redemption whose target is the
|
|
21
|
+
* executing account itself (`SelfTargetedRedemption`) — the route by which a self-`executeBatch`
|
|
22
|
+
* once ran inner calls no enforcer saw. Do not reuse this enforcer under a manager without that rule.
|
|
17
23
|
*/
|
|
18
|
-
contract ValueEnforcer is ICaveatEnforcer {
|
|
24
|
+
contract ValueEnforcer is ICaveatEnforcer, StableProxyGoverned {
|
|
25
|
+
/// @notice A direct deployment, initialized here (no governance: never upgradeable). Behind a stable proxy
|
|
26
|
+
/// (spec 447), `initialize(governance_)` instead — an enforcer's address is in every delegation that
|
|
27
|
+
/// names it, so a fix must keep the address.
|
|
28
|
+
constructor() initializer {}
|
|
29
|
+
|
|
19
30
|
error ValueExceedsLimit();
|
|
20
31
|
|
|
21
32
|
function beforeHook(
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
pragma solidity ^0.8.28;
|
|
3
|
+
import "../governance/StableProxyGoverned.sol";
|
|
4
|
+
import {Eip7702} from "../libraries/Eip7702.sol";
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* @title GeoFeatureRegistry
|
|
@@ -34,7 +36,13 @@ pragma solidity ^0.8.28;
|
|
|
34
36
|
* Ported from smart-agent `GeoFeatureRegistry`; diverges per spec 251 (AP auth,
|
|
35
37
|
* C-box-anchored kinds, no name binding in v1).
|
|
36
38
|
*/
|
|
37
|
-
contract GeoFeatureRegistry {
|
|
39
|
+
contract GeoFeatureRegistry is StableProxyGoverned {
|
|
40
|
+
/// @notice Spec 447 — a DIRECT deployment, initialized here with no governance (its code is frozen, as
|
|
41
|
+
/// before). Behind a stable proxy, `initialize(governance_)` instead; the governance is then the only
|
|
42
|
+
/// upgrade authority. This contract has no admin of its own: governance can replace the code behind
|
|
43
|
+
/// the proxy and nothing else.
|
|
44
|
+
constructor() initializer {}
|
|
45
|
+
|
|
38
46
|
// ─── C-box feature-kind codelist (keccak of the cbox URIs) ──────────
|
|
39
47
|
// Concept URIs are declared in packages/ontology cbox/geo-vocabulary.ttl
|
|
40
48
|
// (scheme apg:geoKind); lockstep-gated against the ontology (ADR-0009).
|
|
@@ -50,6 +58,8 @@ contract GeoFeatureRegistry {
|
|
|
50
58
|
error FeatureNotFound();
|
|
51
59
|
error EmptyMetadata();
|
|
52
60
|
error EmptyId();
|
|
61
|
+
/// @notice REG-15 — `setValidity` may only end a version's window earlier.
|
|
62
|
+
error ValidityNotNarrowed();
|
|
53
63
|
|
|
54
64
|
// ─── Storage ────────────────────────────────────────────────────────
|
|
55
65
|
struct FeatureRecord {
|
|
@@ -113,6 +123,7 @@ contract GeoFeatureRegistry {
|
|
|
113
123
|
function publish(PublishInput calldata p) external returns (uint64 version) {
|
|
114
124
|
if (p.featureId == bytes32(0)) revert EmptyId();
|
|
115
125
|
if (bytes(p.metadataURI).length == 0) revert EmptyMetadata();
|
|
126
|
+
Eip7702.refuseDelegatedEOA(msg.sender); // spec 448 §0 (EIP-7702)
|
|
116
127
|
if (msg.sender != p.stewardAccount) revert NotSteward();
|
|
117
128
|
|
|
118
129
|
uint64 prevVer = latestVersion[p.featureId];
|
|
@@ -121,29 +132,36 @@ contract GeoFeatureRegistry {
|
|
|
121
132
|
}
|
|
122
133
|
|
|
123
134
|
version = prevVer + 1;
|
|
124
|
-
_records[p.featureId][version] = FeatureRecord({
|
|
125
|
-
featureId: p.featureId,
|
|
126
|
-
version: version,
|
|
127
|
-
stewardAccount: p.stewardAccount,
|
|
128
|
-
featureKind: p.featureKind,
|
|
129
|
-
geometryHash: p.geometryHash,
|
|
130
|
-
coverageRoot: p.coverageRoot,
|
|
131
|
-
sourceSetRoot: p.sourceSetRoot,
|
|
132
|
-
metadataURI: p.metadataURI,
|
|
133
|
-
centroidLat: p.centroidLat,
|
|
134
|
-
centroidLon: p.centroidLon,
|
|
135
|
-
bboxMinLat: p.bboxMinLat,
|
|
136
|
-
bboxMinLon: p.bboxMinLon,
|
|
137
|
-
bboxMaxLat: p.bboxMaxLat,
|
|
138
|
-
bboxMaxLon: p.bboxMaxLon,
|
|
139
|
-
validAfter: p.validAfter,
|
|
140
|
-
validUntil: p.validUntil,
|
|
141
|
-
active: true,
|
|
142
|
-
registeredAt: uint64(block.timestamp)
|
|
143
|
-
});
|
|
144
135
|
latestVersion[p.featureId] = version;
|
|
145
136
|
if (prevVer == 0) _allFeatures.push(p.featureId);
|
|
137
|
+
// The heavy record write + event live in a helper so `publish`'s own stack stays shallow.
|
|
138
|
+
_storeAndEmit(p, version);
|
|
139
|
+
}
|
|
146
140
|
|
|
141
|
+
/// @dev Writes the record FIELD-BY-FIELD to storage (no in-memory struct literal) and emits.
|
|
142
|
+
/// Behaviour is identical to the prior `_records[...] = FeatureRecord({...})` literal; the
|
|
143
|
+
/// split exists only to keep `publish` within solc's stack under `forge coverage --ir-minimum`
|
|
144
|
+
/// instrumentation (the 18-field memory literal + a dynamic `string` overflowed it by one slot).
|
|
145
|
+
function _storeAndEmit(PublishInput calldata p, uint64 version) internal {
|
|
146
|
+
FeatureRecord storage r = _records[p.featureId][version];
|
|
147
|
+
r.featureId = p.featureId;
|
|
148
|
+
r.version = version;
|
|
149
|
+
r.stewardAccount = p.stewardAccount;
|
|
150
|
+
r.featureKind = p.featureKind;
|
|
151
|
+
r.geometryHash = p.geometryHash;
|
|
152
|
+
r.coverageRoot = p.coverageRoot;
|
|
153
|
+
r.sourceSetRoot = p.sourceSetRoot;
|
|
154
|
+
r.metadataURI = p.metadataURI;
|
|
155
|
+
r.centroidLat = p.centroidLat;
|
|
156
|
+
r.centroidLon = p.centroidLon;
|
|
157
|
+
r.bboxMinLat = p.bboxMinLat;
|
|
158
|
+
r.bboxMinLon = p.bboxMinLon;
|
|
159
|
+
r.bboxMaxLat = p.bboxMaxLat;
|
|
160
|
+
r.bboxMaxLon = p.bboxMaxLon;
|
|
161
|
+
r.validAfter = p.validAfter;
|
|
162
|
+
r.validUntil = p.validUntil;
|
|
163
|
+
r.active = true;
|
|
164
|
+
r.registeredAt = uint64(block.timestamp);
|
|
147
165
|
emit FeaturePublished(p.featureId, version, p.stewardAccount, p.featureKind, p.geometryHash, p.coverageRoot, p.sourceSetRoot, p.metadataURI);
|
|
148
166
|
}
|
|
149
167
|
|
|
@@ -151,6 +169,7 @@ contract GeoFeatureRegistry {
|
|
|
151
169
|
uint64 v = latestVersion[featureId];
|
|
152
170
|
if (v == 0) revert FeatureNotFound();
|
|
153
171
|
FeatureRecord storage r = _records[featureId][v];
|
|
172
|
+
Eip7702.refuseDelegatedEOA(msg.sender); // spec 448 §0 (EIP-7702)
|
|
154
173
|
if (msg.sender != r.stewardAccount) revert NotSteward();
|
|
155
174
|
r.active = false;
|
|
156
175
|
emit FeatureDeactivated(featureId, v);
|
|
@@ -159,8 +178,14 @@ contract GeoFeatureRegistry {
|
|
|
159
178
|
function setValidity(bytes32 featureId, uint64 version, uint64 validAfter, uint64 validUntil) external {
|
|
160
179
|
FeatureRecord storage r = _records[featureId][version];
|
|
161
180
|
if (r.version == 0) revert FeatureNotFound();
|
|
181
|
+
Eip7702.refuseDelegatedEOA(msg.sender); // spec 448 §0 (EIP-7702)
|
|
162
182
|
if (msg.sender != r.stewardAccount) revert NotSteward();
|
|
163
|
-
|
|
183
|
+
// REG-15: a published version's window can be ENDED, never rewritten. `validAfter` is fixed at
|
|
184
|
+
// publish; `validUntil` may be set (0 → a time) or moved earlier, not extended or cleared — a
|
|
185
|
+
// credential that pinned this version must not find its window widened after the fact. To extend,
|
|
186
|
+
// publish a new version.
|
|
187
|
+
if (validAfter != r.validAfter) revert ValidityNotNarrowed();
|
|
188
|
+
if (validUntil == 0 || (r.validUntil != 0 && validUntil > r.validUntil)) revert ValidityNotNarrowed();
|
|
164
189
|
r.validUntil = validUntil;
|
|
165
190
|
emit FeatureValidityChanged(featureId, version, validAfter, validUntil);
|
|
166
191
|
}
|
|
@@ -168,6 +193,8 @@ contract GeoFeatureRegistry {
|
|
|
168
193
|
// ─── Views ──────────────────────────────────────────────────────────
|
|
169
194
|
function getFeature(bytes32 featureId, uint64 version) external view returns (FeatureRecord memory) {
|
|
170
195
|
FeatureRecord memory r = _records[featureId][version];
|
|
196
|
+
// Zero-default existence check (versions are 1-based) — benign.
|
|
197
|
+
// slither-disable-next-line incorrect-equality
|
|
171
198
|
if (r.version == 0) revert FeatureNotFound();
|
|
172
199
|
return r;
|
|
173
200
|
}
|
|
@@ -187,4 +214,22 @@ contract GeoFeatureRegistry {
|
|
|
187
214
|
function allFeatures() external view returns (bytes32[] memory) {
|
|
188
215
|
return _allFeatures;
|
|
189
216
|
}
|
|
217
|
+
|
|
218
|
+
function featureCount() external view returns (uint256) {
|
|
219
|
+
return _allFeatures.length;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/// @notice REG-12 — a bounded window of the id index: `limit` ids from `offset` (fewer at the end).
|
|
223
|
+
/// `allFeatures()` is unbounded (anyone may publish a new id) and is kept for small deployments.
|
|
224
|
+
function allFeaturesPage(uint256 offset, uint256 limit) external view returns (bytes32[] memory page) {
|
|
225
|
+
uint256 n = _allFeatures.length;
|
|
226
|
+
if (offset >= n) return new bytes32[](0);
|
|
227
|
+
uint256 end = offset + limit;
|
|
228
|
+
if (end > n) end = n;
|
|
229
|
+
page = new bytes32[](end - offset);
|
|
230
|
+
for (uint256 i = offset; i < end; i++) page[i - offset] = _allFeatures[i];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// @dev Spec 447 — storage gap: the contract may sit behind a stable proxy; layout gated by `check:storage-layouts`.
|
|
234
|
+
uint256[47] private __gap;
|
|
190
235
|
}
|