@agenticprimitives/contracts 1.0.0-alpha.45 → 1.0.0-alpha.47

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 (82) hide show
  1. package/AUDIT.md +60 -26
  2. package/deployments-base-sepolia.json +2 -0
  3. package/deployments-faithchain.json +2 -0
  4. package/deployments-hopechain.json +2 -0
  5. package/dist/abi/AgentAccount.json +175 -0
  6. package/dist/abi/AgentAccountFactory.json +47 -0
  7. package/dist/abi/AgentNameAttributeResolver.json +24 -0
  8. package/dist/abi/AgentNameRegistry.json +369 -0
  9. package/dist/abi/AgentNameUniversalResolver.json +34 -0
  10. package/dist/abi/AgentProfileResolver.json +48 -0
  11. package/dist/abi/AgentRegistryBase.json +158 -0
  12. package/dist/abi/AgentRelationship.json +212 -0
  13. package/dist/abi/AgreementRegistry.json +10 -0
  14. package/dist/abi/AttestationRegistry.json +49 -0
  15. package/dist/abi/AttributeStorage.json +24 -0
  16. package/dist/abi/CustodyPolicy.json +184 -0
  17. package/dist/abi/DelegationManager.json +96 -0
  18. package/dist/abi/DigestBindingEnforcer.json +103 -1
  19. package/dist/abi/EstateProjectionRegistry.json +137 -24
  20. package/dist/abi/GeoFeatureRegistry.json +42 -0
  21. package/dist/abi/IAgentAccountFactoryView.json +13 -0
  22. package/dist/abi/IAgentAccountGovernanceTimelockView.json +15 -0
  23. package/dist/abi/ICustodyPolicyFactoryView.json +13 -0
  24. package/dist/abi/ICustodyPolicyPasskeyView.json +50 -0
  25. package/dist/abi/IRedemptionContext.json +15 -0
  26. package/dist/abi/ITreasurySpendPolicyAccountView.json +44 -0
  27. package/dist/abi/IUpgradeVoucherView.json +26 -0
  28. package/dist/abi/OntologyTermRegistry.json +57 -0
  29. package/dist/abi/PaymentEnforcer.json +10 -0
  30. package/dist/abi/PermissionlessSubregistry.json +165 -0
  31. package/dist/abi/PricedSubregistry.json +57 -0
  32. package/dist/abi/RelationshipTypeRegistry.json +57 -0
  33. package/dist/abi/ShapeRegistry.json +57 -0
  34. package/dist/abi/SkillDefinitionRegistry.json +42 -0
  35. package/dist/abi/TreasurySpendPolicy.json +106 -1
  36. package/dist/abi/UpgradeVoucher.json +176 -0
  37. package/dist/abi/index.d.ts +6 -0
  38. package/dist/abi/index.js +6 -0
  39. package/dist/deployments/base-sepolia.d.ts +2 -0
  40. package/dist/deployments/base-sepolia.js +4 -0
  41. package/dist/deployments/faithchain.d.ts +2 -0
  42. package/dist/deployments/faithchain.js +4 -0
  43. package/dist/deployments/hopechain.d.ts +2 -0
  44. package/dist/deployments/hopechain.js +4 -0
  45. package/package.json +1 -1
  46. package/src/AgentAccount.sol +531 -92
  47. package/src/AgentAccountFactory.sol +28 -2
  48. package/src/IAgentAccount.sol +14 -0
  49. package/src/SmartAgentPaymaster.sol +30 -3
  50. package/src/UpgradeVoucher.sol +74 -0
  51. package/src/agency/DelegationManager.sol +177 -52
  52. package/src/agency/ICaveatEnforcer.sol +10 -0
  53. package/src/agency/IDelegationManager.sol +6 -5
  54. package/src/agreement/AgreementRegistry.sol +61 -17
  55. package/src/attestation/AttestationRegistry.sol +115 -22
  56. package/src/custody/CustodyPolicy.sol +393 -47
  57. package/src/custody/TreasurySpendPolicy.sol +192 -29
  58. package/src/enforcers/AllowedMethodsEnforcer.sol +6 -1
  59. package/src/enforcers/AllowedTargetsEnforcer.sol +6 -1
  60. package/src/enforcers/DigestBindingEnforcer.sol +91 -19
  61. package/src/enforcers/PaymentEnforcer.sol +32 -1
  62. package/src/enforcers/QuorumEnforcer.sol +1 -1
  63. package/src/enforcers/ValueEnforcer.sol +6 -1
  64. package/src/geo/GeoFeatureRegistry.sol +25 -1
  65. package/src/governance/AgenticGovernance.sol +16 -0
  66. package/src/identity/AgentProfileResolver.sol +11 -0
  67. package/src/naming/AgentNameAttributeResolver.sol +17 -2
  68. package/src/naming/AgentNamePredicates.sol +1 -1
  69. package/src/naming/AgentNameRegistry.sol +205 -10
  70. package/src/naming/AgentNameUniversalResolver.sol +26 -0
  71. package/src/naming/PermissionlessSubregistry.sol +69 -23
  72. package/src/naming/PricedSubregistry.sol +28 -0
  73. package/src/ontology/AttributeStorage.sol +93 -80
  74. package/src/ontology/OntologyTermRegistry.sol +28 -0
  75. package/src/ontology/ShapeRegistry.sol +28 -0
  76. package/src/payments/PaymentEscrow.sol +23 -15
  77. package/src/payments/PaymentReceiptRegistry.sol +11 -3
  78. package/src/registry/AgentRegistryBase.sol +39 -0
  79. package/src/registry/EstateProjectionRegistry.sol +94 -40
  80. package/src/relationships/AgentRelationship.sol +112 -7
  81. package/src/relationships/RelationshipTypeRegistry.sol +28 -0
  82. package/src/skills/SkillDefinitionRegistry.sol +25 -1
@@ -21,12 +21,21 @@ import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol
21
21
  * is owned by `newOwner` (caller decides — typically themselves
22
22
  * OR a PSA they control).
23
23
  *
24
- * Spam prevention:
25
- * - Minimum label length (`MIN_LABEL_LENGTH = 3`).
26
- * - One name per caller `msg.sender` (`claimedBy[caller] != 0`
27
- * reverts). Designed for demos / sybil-resistant rollups; a
28
- * production subregistry would gate on a registration fee or a
29
- * governance allowlist instead.
24
+ * Front-running (spec 445 / REG-5, ledger SUB-1): registration is COMMIT-REVEAL. A claimant first
25
+ * publishes `commit(keccak256(this, chainId, label, newOwner, secret))`, waits `MIN_COMMIT_AGE`, then
26
+ * reveals with `register(label, newOwner, secret)` before `MAX_COMMIT_AGE`. The label is not visible
27
+ * until the reveal, and a mempool observer who copies the reveal has no matured commitment of its own;
28
+ * copying the SAME reveal only registers the name to the owner the claimant chose (the commitment
29
+ * binds the owner, not the caller, so a relayer may reveal).
30
+ *
31
+ * The cap (ledger SUB-2) is one reveal per CALLER (`claimedBy[msg.sender]`). It is a courtesy rate
32
+ * limit and NOT a security control: a fresh address is free, so this contract does not ration names
33
+ * and nothing may rely on the cap. (A per-owner cap was considered and rejected: anyone could spend
34
+ * a victim's only slot by registering a junk name to them.) A root whose names are scarce or
35
+ * valuable MUST use `PricedSubregistry` (a coin and a gate's ticket); this contract is the testnet /
36
+ * demo posture and a production deploy does not put it in front of a typed root (spec 445 §2.1).
37
+ *
38
+ * Reserved labels and DNS-host injectivity are the registry's rule (`AgentNameRegistry.register`).
30
39
  *
31
40
  * Authority model:
32
41
  * - The contract calls `REGISTRY.register(...)`; the registry sees
@@ -52,7 +61,16 @@ contract PermissionlessSubregistry is ReentrancyGuard {
52
61
 
53
62
  uint256 public constant MIN_LABEL_LENGTH = 3;
54
63
 
55
- /// @notice msg.sender → the child node they've already claimed.
64
+ /// @notice A commitment must be at least this old to be revealed — the reveal lands in a later block
65
+ /// than the commit, so an observer of the reveal cannot commit-and-reveal ahead of it.
66
+ uint256 public constant MIN_COMMIT_AGE = 10 seconds;
67
+ /// @notice …and at most this old. A stale commitment is void and may be re-committed.
68
+ uint256 public constant MAX_COMMIT_AGE = 1 days;
69
+
70
+ /// @notice commitment → when it was committed (0 = none / consumed).
71
+ mapping(bytes32 => uint256) public committedAt;
72
+
73
+ /// @notice msg.sender → the child node they've already claimed. Courtesy limit, not sybil resistance.
56
74
  mapping(address => bytes32) public claimedBy;
57
75
  /// @notice Total claims served by this subregistry instance.
58
76
  uint256 public claimCount;
@@ -68,6 +86,15 @@ contract PermissionlessSubregistry is ReentrancyGuard {
68
86
  error LabelTooShort();
69
87
  error EmptyLabel();
70
88
  error ZeroNewOwner();
89
+ error ZeroCommitment();
90
+ /// @notice A live (not yet expired) commitment with this hash already exists — re-committing would
91
+ /// reset its age, which a griefer could use to keep a claimant's reveal immature.
92
+ error CommitmentPending(bytes32 commitment);
93
+ error CommitmentNotFound(bytes32 commitment);
94
+ error CommitmentTooNew(bytes32 commitment, uint256 matureAt);
95
+ error CommitmentExpired(bytes32 commitment);
96
+
97
+ event Committed(bytes32 indexed commitment, address indexed by);
71
98
 
72
99
  constructor(AgentNameRegistry registry, bytes32 parentNode, address defaultResolver) {
73
100
  REGISTRY = registry;
@@ -75,30 +102,49 @@ contract PermissionlessSubregistry is ReentrancyGuard {
75
102
  DEFAULT_RESOLVER = defaultResolver;
76
103
  }
77
104
 
105
+ /// @notice The commitment for a claim: binds this subregistry, the chain, the label, the owner the
106
+ /// name will be registered to, and a secret only the claimant knows.
107
+ function makeCommitment(string calldata label, address newOwner, bytes32 secret) public view returns (bytes32) {
108
+ return keccak256(abi.encode(address(this), block.chainid, keccak256(bytes(label)), newOwner, secret));
109
+ }
110
+
111
+ /// @notice Step 1 — publish a commitment. Anyone may; it reveals nothing about the label.
112
+ function commit(bytes32 commitment) external {
113
+ if (commitment == bytes32(0)) revert ZeroCommitment();
114
+ uint256 at = committedAt[commitment];
115
+ if (at != 0 && block.timestamp <= at + MAX_COMMIT_AGE) revert CommitmentPending(commitment);
116
+ committedAt[commitment] = block.timestamp;
117
+ emit Committed(commitment, msg.sender);
118
+ }
119
+
78
120
  /**
79
- * @notice Claim `<label>.<parent>` for `newOwner`. Reverts if the
80
- * caller has already claimed a name through this
81
- * subregistry, OR if the label fails the minimum-length
82
- * guard, OR if the registry rejects the registration
83
- * (e.g. label already taken).
121
+ * @notice Step 2 — reveal: claim `<label>.<parent>` for `newOwner`. Reverts when there is no matured,
122
+ * unexpired commitment for (label, newOwner, secret), when the caller already revealed a
123
+ * name here (courtesy limit), when the label fails the minimum-length guard, or when the registry
124
+ * refuses (label taken, reserved, or reading as a typed name).
84
125
  *
85
- * The caller pays gas. The contract does NOT collect a
86
- * fee — fee gating belongs in a different subregistry
87
- * shape if needed.
126
+ * The caller pays gas. The contract does NOT collect a fee — fee gating is
127
+ * `PricedSubregistry`.
88
128
  */
89
- function register(string calldata label, address newOwner) external nonReentrant returns (bytes32 childNode) {
129
+ function register(string calldata label, address newOwner, bytes32 secret) external nonReentrant returns (bytes32 childNode) {
90
130
  if (bytes(label).length == 0) revert EmptyLabel();
91
131
  if (bytes(label).length < MIN_LABEL_LENGTH) revert LabelTooShort();
92
132
  if (newOwner == address(0)) revert ZeroNewOwner();
93
133
  bytes32 prior = claimedBy[msg.sender];
94
134
  if (prior != bytes32(0)) revert AlreadyClaimed(prior);
95
- // R9 / docs/audits/r9-static-analysis-triage.md § Slither M-1:
96
- // Slither flags this as cross-function reentrancy because state is
97
- // written AFTER the external call. The function is `nonReentrant`
98
- // (line 89) + REGISTRY is an immutable contract we control
99
- // (`AgentNameRegistry`) that does not call back into this contract.
100
- // The cross-function reader (`hasClaimed`) is view-only. No invariant
101
- // is violated by the pre-write-window. Triaged false positive.
135
+
136
+ bytes32 commitment = makeCommitment(label, newOwner, secret);
137
+ uint256 at = committedAt[commitment];
138
+ if (at == 0) revert CommitmentNotFound(commitment);
139
+ if (block.timestamp < at + MIN_COMMIT_AGE) revert CommitmentTooNew(commitment, at + MIN_COMMIT_AGE);
140
+ if (block.timestamp > at + MAX_COMMIT_AGE) revert CommitmentExpired(commitment);
141
+ delete committedAt[commitment];
142
+ claimedBy[msg.sender] = bytes32(uint256(1)); // provisional: written before the external call
143
+
144
+ // REGISTRY is an immutable contract we control that does not call back into this contract, and
145
+ // the function is `nonReentrant`; the claim slot and the consumed commitment are written BEFORE
146
+ // the call, and only the slot's final value (the node the registry returned) after it.
147
+ // R9 / docs/audits/r9-static-analysis-triage.md § Slither M-1: triaged false positive.
102
148
  // slither-disable-next-line reentrancy-no-eth
103
149
  childNode = REGISTRY.register(PARENT_NODE, label, newOwner, DEFAULT_RESOLVER, 0);
104
150
  claimedBy[msg.sender] = childNode;
@@ -205,5 +205,33 @@ contract PricedSubregistry is ReentrancyGuard, EIP712 {
205
205
  if (newGovernor == address(0)) revert ZeroAddress();
206
206
  emit GovernorTransferred(governor, newGovernor);
207
207
  governor = newGovernor;
208
+ pendingGovernor = address(0); // a proposal made by the previous governor does not survive it
209
+ }
210
+
211
+ // ─── Two-step governor hand-over (spec 445 / REG-11) ────────────
212
+ //
213
+ // `transferGovernor` is one step: a wrong address is permanent. It stays for the scripted bootstrap
214
+ // hand-over, where the deploy script asserts the end state in the same run (spec 445 §2.1). Every
215
+ // LATER rotation should use this pair — the successor proves it can act before the role moves.
216
+
217
+ /// @notice The successor named by `proposeGovernor`, until it accepts. Zero otherwise.
218
+ address public pendingGovernor;
219
+
220
+ event GovernorProposed(address indexed governor, address indexed pendingGovernor);
221
+
222
+ error NotPendingGovernor();
223
+
224
+ /// @notice Name a successor (zero cancels). The governor keeps the role until the successor accepts.
225
+ function proposeGovernor(address newGovernor) external onlyGovernor {
226
+ pendingGovernor = newGovernor;
227
+ emit GovernorProposed(governor, newGovernor);
228
+ }
229
+
230
+ /// @notice Take the governor role. Only the proposed successor.
231
+ function acceptGovernor() external {
232
+ if (msg.sender != pendingGovernor || msg.sender == address(0)) revert NotPendingGovernor();
233
+ emit GovernorTransferred(governor, msg.sender);
234
+ governor = msg.sender;
235
+ pendingGovernor = address(0);
208
236
  }
209
237
  }
@@ -86,97 +86,91 @@ abstract contract AttributeStorage {
86
86
  function DT_ADDRESS_ARR_PUB() external pure returns (uint8) { return DT_ADDRESS_ARR; }
87
87
  function DT_BYTES32_ARR_PUB() external pure returns (uint8) { return DT_BYTES32_ARR; }
88
88
 
89
+ // ─── Storage key (spec 445 / REG-4) ─────────────────────────────
90
+
91
+ /// @notice The key a subject's VALUES are stored under. Identity by default. A subclass whose subject can
92
+ /// change hands (a name node) overrides this to fold in an owner epoch, so a record written by a
93
+ /// previous owner is unreachable for the next one WITHOUT a delete loop and without a log scan:
94
+ /// every setter and every getter goes through this one function, so a read can never see a value
95
+ /// the current key did not write. Enumeration (`allSubjects`) and the version watermark stay keyed
96
+ /// by the OUTWARD subject — they are indexes, not records.
97
+ function _storageKey(bytes32 subject) internal view virtual returns (bytes32) {
98
+ return subject;
99
+ }
100
+
89
101
  // ─── Internal setters (subclass-only) ───────────────────────────
90
102
 
91
103
  function _setString(bytes32 subject, bytes32 predicate, string memory value) internal {
92
- _requirePredicate(predicate);
93
- _string[subject][predicate] = value;
94
- _record(subject, predicate, DT_STRING);
104
+ bytes32 k = _write(subject, predicate, DT_STRING, false);
105
+ _string[k][predicate] = value;
95
106
  }
96
107
 
97
108
  function _setAddress(bytes32 subject, bytes32 predicate, address value) internal {
98
- _requirePredicate(predicate);
99
- _address[subject][predicate] = value;
100
- _record(subject, predicate, DT_ADDRESS);
109
+ bytes32 k = _write(subject, predicate, DT_ADDRESS, false);
110
+ _address[k][predicate] = value;
101
111
  }
102
112
 
103
113
  function _setBool(bytes32 subject, bytes32 predicate, bool value) internal {
104
- _requirePredicate(predicate);
105
- _bool[subject][predicate] = value;
106
- _record(subject, predicate, DT_BOOL);
114
+ bytes32 k = _write(subject, predicate, DT_BOOL, false);
115
+ _bool[k][predicate] = value;
107
116
  }
108
117
 
109
118
  function _setUint(bytes32 subject, bytes32 predicate, uint256 value) internal {
110
- _requirePredicate(predicate);
111
- _uint[subject][predicate] = value;
112
- _record(subject, predicate, DT_UINT256);
119
+ bytes32 k = _write(subject, predicate, DT_UINT256, false);
120
+ _uint[k][predicate] = value;
113
121
  }
114
122
 
115
123
  function _setBytes32(bytes32 subject, bytes32 predicate, bytes32 value) internal {
116
- _requirePredicate(predicate);
117
- _bytes32[subject][predicate] = value;
118
- _record(subject, predicate, DT_BYTES32);
124
+ bytes32 k = _write(subject, predicate, DT_BYTES32, false);
125
+ _bytes32[k][predicate] = value;
119
126
  }
120
127
 
121
128
  function _setStringArr(bytes32 subject, bytes32 predicate, string[] memory values) internal {
122
- _requirePredicate(predicate);
123
- delete _stringArr[subject][predicate];
129
+ bytes32 k = _write(subject, predicate, DT_STRING_ARR, false);
130
+ delete _stringArr[k][predicate];
124
131
  for (uint256 i = 0; i < values.length; i++) {
125
- _stringArr[subject][predicate].push(values[i]);
132
+ _stringArr[k][predicate].push(values[i]);
126
133
  }
127
- _record(subject, predicate, DT_STRING_ARR);
128
134
  }
129
135
 
130
136
  function _setAddressArr(bytes32 subject, bytes32 predicate, address[] memory values) internal {
131
- _requirePredicate(predicate);
132
- delete _addressArr[subject][predicate];
137
+ bytes32 k = _write(subject, predicate, DT_ADDRESS_ARR, false);
138
+ delete _addressArr[k][predicate];
133
139
  for (uint256 i = 0; i < values.length; i++) {
134
- _addressArr[subject][predicate].push(values[i]);
140
+ _addressArr[k][predicate].push(values[i]);
135
141
  }
136
- _record(subject, predicate, DT_ADDRESS_ARR);
137
142
  }
138
143
 
139
144
  function _setBytes32Arr(bytes32 subject, bytes32 predicate, bytes32[] memory values) internal {
140
- _requirePredicate(predicate);
141
- delete _bytes32Arr[subject][predicate];
145
+ bytes32 k = _write(subject, predicate, DT_BYTES32_ARR, false);
146
+ delete _bytes32Arr[k][predicate];
142
147
  for (uint256 i = 0; i < values.length; i++) {
143
- _bytes32Arr[subject][predicate].push(values[i]);
148
+ _bytes32Arr[k][predicate].push(values[i]);
144
149
  }
145
- _record(subject, predicate, DT_BYTES32_ARR);
146
150
  }
147
151
 
148
152
  function _appendString(bytes32 subject, bytes32 predicate, string memory value) internal {
149
- _requirePredicate(predicate);
150
- _stringArr[subject][predicate].push(value);
151
- _recordAppend(subject, predicate, DT_STRING_ARR);
153
+ bytes32 k = _write(subject, predicate, DT_STRING_ARR, true);
154
+ _stringArr[k][predicate].push(value);
152
155
  }
153
156
 
154
157
  function _appendAddress(bytes32 subject, bytes32 predicate, address value) internal {
155
- _requirePredicate(predicate);
156
- _addressArr[subject][predicate].push(value);
157
- _recordAppend(subject, predicate, DT_ADDRESS_ARR);
158
+ bytes32 k = _write(subject, predicate, DT_ADDRESS_ARR, true);
159
+ _addressArr[k][predicate].push(value);
158
160
  }
159
161
 
160
162
  function _appendBytes32(bytes32 subject, bytes32 predicate, bytes32 value) internal {
161
- _requirePredicate(predicate);
162
- _bytes32Arr[subject][predicate].push(value);
163
- _recordAppend(subject, predicate, DT_BYTES32_ARR);
163
+ bytes32 k = _write(subject, predicate, DT_BYTES32_ARR, true);
164
+ _bytes32Arr[k][predicate].push(value);
164
165
  }
165
166
 
166
167
  function _unset(bytes32 subject, bytes32 predicate) internal {
167
- if (!_isSet[subject][predicate]) revert AttributeNotSet();
168
- uint8 dt = _datatype[subject][predicate];
169
- if (dt == DT_STRING) delete _string[subject][predicate];
170
- else if (dt == DT_ADDRESS) delete _address[subject][predicate];
171
- else if (dt == DT_BOOL) delete _bool[subject][predicate];
172
- else if (dt == DT_UINT256) delete _uint[subject][predicate];
173
- else if (dt == DT_BYTES32) delete _bytes32[subject][predicate];
174
- else if (dt == DT_STRING_ARR) delete _stringArr[subject][predicate];
175
- else if (dt == DT_ADDRESS_ARR) delete _addressArr[subject][predicate];
176
- else if (dt == DT_BYTES32_ARR) delete _bytes32Arr[subject][predicate];
177
- _isSet[subject][predicate] = false;
178
- delete _datatype[subject][predicate];
179
- delete _updatedAt[subject][predicate];
168
+ bytes32 k = _storageKey(subject);
169
+ if (!_isSet[k][predicate]) revert AttributeNotSet();
170
+ _clearValue(k, predicate, _datatype[k][predicate]);
171
+ _isSet[k][predicate] = false;
172
+ delete _datatype[k][predicate];
173
+ delete _updatedAt[k][predicate];
180
174
  uint64 v = _bumpVersion(subject);
181
175
  emit AttributeUnset(subject, predicate, v);
182
176
  }
@@ -184,51 +178,62 @@ abstract contract AttributeStorage {
184
178
  // ─── Public getters ─────────────────────────────────────────────
185
179
 
186
180
  function getString(bytes32 subject, bytes32 predicate) external view returns (string memory) {
187
- return _string[subject][predicate];
181
+ return _string[_storageKey(subject)][predicate];
188
182
  }
189
183
  function getAddress(bytes32 subject, bytes32 predicate) external view returns (address) {
190
- return _address[subject][predicate];
184
+ return _address[_storageKey(subject)][predicate];
191
185
  }
192
186
  function getBool(bytes32 subject, bytes32 predicate) external view returns (bool) {
193
- return _bool[subject][predicate];
187
+ return _bool[_storageKey(subject)][predicate];
194
188
  }
195
189
  function getUint(bytes32 subject, bytes32 predicate) external view returns (uint256) {
196
- return _uint[subject][predicate];
190
+ return _uint[_storageKey(subject)][predicate];
197
191
  }
198
192
  function getBytes32(bytes32 subject, bytes32 predicate) external view returns (bytes32) {
199
- return _bytes32[subject][predicate];
193
+ return _bytes32[_storageKey(subject)][predicate];
200
194
  }
201
195
  function getStringArr(bytes32 subject, bytes32 predicate) external view returns (string[] memory) {
202
- return _stringArr[subject][predicate];
196
+ return _stringArr[_storageKey(subject)][predicate];
203
197
  }
204
198
  function getAddressArr(bytes32 subject, bytes32 predicate) external view returns (address[] memory) {
205
- return _addressArr[subject][predicate];
199
+ return _addressArr[_storageKey(subject)][predicate];
206
200
  }
207
201
  function getBytes32Arr(bytes32 subject, bytes32 predicate) external view returns (bytes32[] memory) {
208
- return _bytes32Arr[subject][predicate];
202
+ return _bytes32Arr[_storageKey(subject)][predicate];
209
203
  }
210
204
 
211
205
  function predicatesOf(bytes32 subject) external view returns (bytes32[] memory) {
212
- return _predicates[subject];
206
+ return _predicates[_storageKey(subject)];
213
207
  }
214
208
  function datatypeOf(bytes32 subject, bytes32 predicate) external view returns (uint8) {
215
- return _datatype[subject][predicate];
209
+ return _datatype[_storageKey(subject)][predicate];
216
210
  }
217
211
  function updatedAt(bytes32 subject, bytes32 predicate) external view returns (uint64) {
218
- return _updatedAt[subject][predicate];
212
+ return _updatedAt[_storageKey(subject)][predicate];
219
213
  }
220
214
  function isSet(bytes32 subject, bytes32 predicate) external view returns (bool) {
221
- return _isSet[subject][predicate];
215
+ return _isSet[_storageKey(subject)][predicate];
222
216
  }
223
217
  function subjectVersion(bytes32 subject) external view returns (uint64) {
224
218
  return _subjectVersion[subject];
225
219
  }
220
+ /// @dev REG-12: unbounded — anyone who can write a first record extends it. Kept for small deployments;
221
+ /// a reader of a large one uses `subjectsPage`.
226
222
  function allSubjects() external view returns (bytes32[] memory) {
227
223
  return _allSubjects;
228
224
  }
229
225
  function subjectCount() external view returns (uint256) {
230
226
  return _allSubjects.length;
231
227
  }
228
+ /// @notice REG-12 — a bounded window of the subject index: `limit` entries from `offset` (fewer at the end).
229
+ function subjectsPage(uint256 offset, uint256 limit) external view returns (bytes32[] memory page) {
230
+ uint256 n = _allSubjects.length;
231
+ if (offset >= n) return new bytes32[](0);
232
+ uint256 end = offset + limit;
233
+ if (end > n) end = n;
234
+ page = new bytes32[](end - offset);
235
+ for (uint256 i = offset; i < end; i++) page[i - offset] = _allSubjects[i];
236
+ }
232
237
 
233
238
  // ─── Internal helpers ───────────────────────────────────────────
234
239
 
@@ -236,28 +241,36 @@ abstract contract AttributeStorage {
236
241
  if (!ONTOLOGY.isActive(predicate)) revert PredicateNotActive();
237
242
  }
238
243
 
239
- function _record(bytes32 subject, bytes32 predicate, uint8 dt) internal {
240
- _trackSubject(subject);
241
- if (!_isSet[subject][predicate]) {
242
- _predicates[subject].push(predicate);
243
- _isSet[subject][predicate] = true;
244
- }
245
- _datatype[subject][predicate] = dt;
246
- uint64 v = _bumpVersion(subject);
247
- _updatedAt[subject][predicate] = v;
248
- emit AttributeSet(subject, predicate, dt, v);
249
- }
250
-
251
- function _recordAppend(bytes32 subject, bytes32 predicate, uint8 dt) internal {
244
+ /// @dev One write path: predicate active → storage key → (REG-14) if the predicate was set with ANOTHER
245
+ /// datatype, delete that family's value so `getString` cannot keep answering after the predicate was
246
+ /// rewritten as an address → index + version + event. Returns the key the caller writes under.
247
+ function _write(bytes32 subject, bytes32 predicate, uint8 dt, bool append) private returns (bytes32 k) {
248
+ _requirePredicate(predicate);
249
+ k = _storageKey(subject);
252
250
  _trackSubject(subject);
253
- if (!_isSet[subject][predicate]) {
254
- _predicates[subject].push(predicate);
255
- _isSet[subject][predicate] = true;
251
+ if (!_isSet[k][predicate]) {
252
+ _predicates[k].push(predicate);
253
+ _isSet[k][predicate] = true;
254
+ } else {
255
+ uint8 prior = _datatype[k][predicate];
256
+ if (prior != dt) _clearValue(k, predicate, prior);
256
257
  }
257
- _datatype[subject][predicate] = dt;
258
+ _datatype[k][predicate] = dt;
258
259
  uint64 v = _bumpVersion(subject);
259
- _updatedAt[subject][predicate] = v;
260
- emit AttributeAppended(subject, predicate, dt, v);
260
+ _updatedAt[k][predicate] = v;
261
+ if (append) emit AttributeAppended(subject, predicate, dt, v);
262
+ else emit AttributeSet(subject, predicate, dt, v);
263
+ }
264
+
265
+ function _clearValue(bytes32 k, bytes32 predicate, uint8 dt) private {
266
+ if (dt == DT_STRING) delete _string[k][predicate];
267
+ else if (dt == DT_ADDRESS) delete _address[k][predicate];
268
+ else if (dt == DT_BOOL) delete _bool[k][predicate];
269
+ else if (dt == DT_UINT256) delete _uint[k][predicate];
270
+ else if (dt == DT_BYTES32) delete _bytes32[k][predicate];
271
+ else if (dt == DT_STRING_ARR) delete _stringArr[k][predicate];
272
+ else if (dt == DT_ADDRESS_ARR) delete _addressArr[k][predicate];
273
+ else if (dt == DT_BYTES32_ARR) delete _bytes32Arr[k][predicate];
261
274
  }
262
275
 
263
276
  function _trackSubject(bytes32 subject) internal {
@@ -60,6 +60,34 @@ contract OntologyTermRegistry {
60
60
  if (newGovernor == address(0)) revert ZeroGovernor();
61
61
  emit GovernorTransferred(governor, newGovernor);
62
62
  governor = newGovernor;
63
+ pendingGovernor = address(0); // a proposal made by the previous governor does not survive it
64
+ }
65
+
66
+ // ─── Two-step governor hand-over (spec 445 / REG-11) ────────────
67
+ //
68
+ // `transferGovernor` is one step: a wrong address is permanent. It stays for the scripted bootstrap
69
+ // hand-over, where the deploy script asserts the end state in the same run (spec 445 §2.1). Every
70
+ // LATER rotation should use this pair — the successor proves it can act before the role moves.
71
+
72
+ /// @notice The successor named by `proposeGovernor`, until it accepts. Zero otherwise.
73
+ address public pendingGovernor;
74
+
75
+ event GovernorProposed(address indexed governor, address indexed pendingGovernor);
76
+
77
+ error NotPendingGovernor();
78
+
79
+ /// @notice Name a successor (zero cancels). The governor keeps the role until the successor accepts.
80
+ function proposeGovernor(address newGovernor) external onlyGovernor {
81
+ pendingGovernor = newGovernor;
82
+ emit GovernorProposed(governor, newGovernor);
83
+ }
84
+
85
+ /// @notice Take the governor role. Only the proposed successor.
86
+ function acceptGovernor() external {
87
+ if (msg.sender != pendingGovernor || msg.sender == address(0)) revert NotPendingGovernor();
88
+ emit GovernorTransferred(governor, msg.sender);
89
+ governor = msg.sender;
90
+ pendingGovernor = address(0);
63
91
  }
64
92
 
65
93
  function registerTerm(
@@ -86,6 +86,34 @@ contract ShapeRegistry {
86
86
  if (newGovernor == address(0)) revert ZeroGovernor();
87
87
  emit GovernorTransferred(governor, newGovernor);
88
88
  governor = newGovernor;
89
+ pendingGovernor = address(0); // a proposal made by the previous governor does not survive it
90
+ }
91
+
92
+ // ─── Two-step governor hand-over (spec 445 / REG-11) ────────────
93
+ //
94
+ // `transferGovernor` is one step: a wrong address is permanent. It stays for the scripted bootstrap
95
+ // hand-over, where the deploy script asserts the end state in the same run (spec 445 §2.1). Every
96
+ // LATER rotation should use this pair — the successor proves it can act before the role moves.
97
+
98
+ /// @notice The successor named by `proposeGovernor`, until it accepts. Zero otherwise.
99
+ address public pendingGovernor;
100
+
101
+ event GovernorProposed(address indexed governor, address indexed pendingGovernor);
102
+
103
+ error NotPendingGovernor();
104
+
105
+ /// @notice Name a successor (zero cancels). The governor keeps the role until the successor accepts.
106
+ function proposeGovernor(address newGovernor) external onlyGovernor {
107
+ pendingGovernor = newGovernor;
108
+ emit GovernorProposed(governor, newGovernor);
109
+ }
110
+
111
+ /// @notice Take the governor role. Only the proposed successor.
112
+ function acceptGovernor() external {
113
+ if (msg.sender != pendingGovernor || msg.sender == address(0)) revert NotPendingGovernor();
114
+ emit GovernorTransferred(governor, msg.sender);
115
+ governor = msg.sender;
116
+ pendingGovernor = address(0);
89
117
  }
90
118
 
91
119
  // ─── Shape definition ───────────────────────────────────────────
@@ -88,30 +88,38 @@ contract PaymentEscrow is ReentrancyGuard {
88
88
  if (asset == address(0) || payee == address(0)) revert ZeroAddress();
89
89
  if (expiry <= block.timestamp) revert BadExpiry();
90
90
 
91
- // EXT-PMT-3: credit the REALIZED amount received (balance delta), not the requested `amount`. A
92
- // fee-on-transfer / rebasing asset delivers less than `amount`, which would otherwise over-credit
93
- // the hold and let it draw from other holds' funds in the shared pool (cross-hold drain). The
94
- // external transfer happens before the storage write, but nonReentrant + the status check above
95
- // keep it safe (a reentrant deposit is blocked; a duplicate (payer,orderHash) reverts HoldExists).
96
- uint256 beforeBal = IERC20(asset).balanceOf(address(this));
97
- // Triaged false positive: `deposit` is `nonReentrant`, so the balanceOf delta cannot be
98
- // manipulated by re-entering between the two reads — the guard blocks any callback. The
99
- // before/after balance read is the EXT-PMT-3 realized-amount fix for fee-on-transfer assets.
100
- // slither-disable-next-line reentrancy-balance
101
- IERC20(asset).safeTransferFrom(msg.sender, address(this), amount);
102
- uint256 received = IERC20(asset).balanceOf(address(this)) - beforeBal;
103
- if (received == 0) revert ZeroAmount();
104
-
91
+ // Spec 445 (Slither `reentrancy-no-eth`): the hold is WRITTEN BEFORE the token is called. It
92
+ // is created `Held` with amount 0, so the (payer, orderHash) key is occupied for the whole of
93
+ // the pull — a token that calls back into `deposit` meets `HoldExists` on its own key even if
94
+ // the guard were not there, and nothing written during the callback can be overwritten by the
95
+ // tail of this function. `nonReentrant` (on every entry point) still blocks the callback
96
+ // outright; the ordering makes the property structural rather than guard-dependent.
105
97
  _holds[key] = Hold({
106
98
  payer: msg.sender,
107
99
  asset: asset,
108
- amount: received,
100
+ amount: 0,
109
101
  payee: payee,
110
102
  refundTo: refundTo == address(0) ? msg.sender : refundTo,
111
103
  releaser: releaser,
112
104
  expiry: expiry,
113
105
  status: Status.Held
114
106
  });
107
+
108
+ // EXT-PMT-3: credit the REALIZED amount received (balance delta), not the requested `amount`. A
109
+ // fee-on-transfer / rebasing asset delivers less than `amount`, which would otherwise over-credit
110
+ // the hold and let it draw from other holds' funds in the shared pool (cross-hold drain).
111
+ uint256 beforeBal = IERC20(asset).balanceOf(address(this));
112
+ // Triaged: every entry point is `nonReentrant`, so the balance delta cannot be manipulated by
113
+ // re-entering between the two reads, and the only post-call write is the realized amount of the
114
+ // hold this call already owns (created above; no other frame can reach it while the guard is held).
115
+ // slither-disable-next-line reentrancy-balance,reentrancy-no-eth
116
+ IERC20(asset).safeTransferFrom(msg.sender, address(this), amount);
117
+ uint256 received = IERC20(asset).balanceOf(address(this)) - beforeBal;
118
+ // A pull that delivered nothing is no deposit: revert (which also unwinds the hold above).
119
+ // slither-disable-next-line incorrect-equality
120
+ if (received == 0) revert ZeroAmount();
121
+ _holds[key].amount = received;
122
+
115
123
  emit EscrowDeposited(orderHash);
116
124
  }
117
125
 
@@ -13,6 +13,14 @@ pragma solidity ^0.8.28;
13
13
  * app cache, never inline `eth_getLogs` in product hot paths (ADR-0012). The
14
14
  * `PaymentEnforcer.PaymentCharged` event is the primary on-chain receipt; `PaymentSettled`
15
15
  * here adds payer/payee/mandateId-indexed querying for treasury views.
16
+ *
17
+ * WHAT A ROW PROVES (spec 445 / audit DLG-8). Enforced on chain before the row is written:
18
+ * `amount > 0` of `asset` reached `payee`, from `payer` — the ROOT delegator, the account the
19
+ * redemption executed through — under `delegationHash`, once for `nonce`. NOT proved:
20
+ * `mandateId` and `resourceHash`. They are copied from the redeemer's unsigned caveat `args`;
21
+ * they are the redeemer's ASSERTION of which mandate and resource the charge was for. A reader
22
+ * that needs them to be true compares them with a record it trusts (the mandate it issued, the
23
+ * resource it served); it never treats the row as that record.
16
24
  */
17
25
  contract PaymentReceiptRegistry {
18
26
  /// @notice The deployer who wires the sole authorized recorder once.
@@ -21,12 +29,12 @@ contract PaymentReceiptRegistry {
21
29
  address public enforcer;
22
30
 
23
31
  event PaymentSettled(
24
- bytes32 indexed mandateId,
25
- address indexed payer,
32
+ bytes32 indexed mandateId, // redeemer-asserted (unsigned)
33
+ address indexed payer, // the root delegator whose funds moved
26
34
  address indexed payee,
27
35
  address asset,
28
36
  uint256 amount,
29
- bytes32 resourceHash,
37
+ bytes32 resourceHash, // redeemer-asserted (unsigned)
30
38
  bytes32 delegationHash,
31
39
  bytes32 nonce
32
40
  );