@agenticprimitives/contracts 1.0.0-alpha.4 → 1.0.0-alpha.41
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 +330 -47
- package/README.md +56 -10
- package/deployments-base-sepolia.json +50 -0
- package/deployments-faithchain.json +75 -0
- package/deployments-hopechain.json +57 -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/AgentAccount.json +1827 -0
- package/dist/abi/AgentAccountFactory.json +545 -0
- package/dist/abi/AgentNameRegistry.json +51 -0
- package/dist/abi/AgentRegistryBase.json +652 -0
- package/dist/abi/AgenticGovernance.json +32 -0
- package/dist/abi/AgreementRegistry.json +427 -0
- package/dist/abi/ApprovedHashRegistry.json +47 -2
- package/dist/abi/AttestationRegistry.json +436 -0
- package/dist/abi/CallDataHashEnforcer.json +119 -0
- package/dist/abi/CustodyPolicy.json +1179 -0
- package/dist/abi/DelegationManager.json +656 -0
- package/dist/abi/DigestBindingEnforcer.json +251 -0
- package/dist/abi/EstateProjectionRegistry.json +293 -0
- package/dist/abi/GeoFeatureRegistry.json +625 -0
- package/dist/abi/IAgentAccount.json +262 -0
- package/dist/abi/IAgentAccountFactoryAccessor.json +15 -0
- package/dist/abi/IAgentAccountFactoryView.json +54 -0
- package/dist/abi/IAgentAccountPauseView.json +15 -0
- package/dist/abi/IAgenticPrimitivesAgentAccount.json +15 -0
- package/dist/abi/IApprovedHashRegistryView.json +26 -0
- package/dist/abi/IApprovedHashRegistryWrite.json +35 -0
- package/dist/abi/ICustodyPolicyAccountModuleView.json +31 -0
- package/dist/abi/ICustodyPolicyFactoryView.json +15 -0
- package/dist/abi/ICustodyPolicyPauseView.json +15 -0
- package/dist/abi/IDelegationManager.json +171 -0
- package/dist/abi/IERC7579HookLike.json +44 -0
- package/dist/abi/IERC7579ModuleLike.json +28 -0
- package/dist/abi/IPaymentReceiptRegistry.json +50 -0
- package/dist/abi/IRegistryMembershipPolicy.json +70 -0
- package/dist/abi/MockUSDC.json +621 -0
- package/dist/abi/PayloadClassesEnforcer.json +117 -0
- package/dist/abi/PaymentEnforcer.json +441 -0
- package/dist/abi/PaymentEscrow.json +324 -0
- package/dist/abi/PaymentReceiptRegistry.json +182 -0
- package/dist/abi/PermissionlessSubregistry.json +5 -0
- package/dist/abi/PricedSubregistry.json +869 -0
- package/dist/abi/QuorumEnforcer.json +311 -0
- package/dist/abi/ReceiptAnchorRegistry.json +153 -0
- package/dist/abi/SignatureSlotRecovery.json +88 -0
- package/dist/abi/SkillDefinitionRegistry.json +491 -0
- package/dist/abi/SmartAgentPaymaster.json +873 -0
- package/dist/abi/TreasurySpendPolicy.json +369 -0
- package/dist/abi/UniversalSignatureValidator.json +65 -0
- package/dist/abi/index.d.ts +38 -0
- package/dist/abi/index.js +38 -0
- package/dist/deployments/base-sepolia.d.ts +46 -0
- package/dist/deployments/base-sepolia.js +54 -0
- package/dist/deployments/faithchain.d.ts +55 -0
- package/dist/deployments/faithchain.js +79 -0
- package/dist/deployments/hopechain.d.ts +53 -0
- package/dist/deployments/hopechain.js +61 -0
- package/dist/deployments/index.d.ts +13 -0
- package/dist/deployments/index.js +28 -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 +55 -8
- package/src/AgentAccount.sol +569 -38
- package/src/AgentAccountFactory.sol +157 -16
- package/src/ApprovedHashRegistry.sol +41 -6
- package/src/IAgentAccount.sol +23 -0
- package/src/SmartAgentPaymaster.sol +156 -12
- package/src/UniversalSignatureValidator.sol +3 -0
- package/src/agency/DelegationManager.sol +179 -0
- package/src/agency/IDelegationManager.sol +38 -0
- package/src/agreement/AgreementRegistry.sol +331 -0
- package/src/attestation/AttestationRegistry.sol +384 -0
- package/src/custody/CustodyPolicy.sol +263 -15
- package/src/custody/TreasurySpendPolicy.sol +166 -0
- package/src/enforcers/AllowedMethodsEnforcer.sol +6 -0
- package/src/enforcers/AllowedTargetsEnforcer.sol +6 -0
- package/src/enforcers/CallDataHashEnforcer.sol +61 -0
- package/src/enforcers/DigestBindingEnforcer.sol +107 -0
- package/src/enforcers/PayloadClassesEnforcer.sol +30 -0
- package/src/enforcers/PaymentEnforcer.sol +257 -0
- package/src/enforcers/QuorumEnforcer.sol +24 -1
- package/src/enforcers/TimestampEnforcer.sol +7 -0
- package/src/enforcers/ValueEnforcer.sol +6 -0
- package/src/geo/GeoFeatureRegistry.sol +197 -0
- package/src/governance/AgenticGovernance.sol +26 -4
- package/src/identity/AgentProfilePredicates.sol +23 -0
- package/src/libraries/SignatureSlotRecovery.sol +24 -7
- package/src/libraries/WebAuthnLib.sol +13 -0
- package/src/mocks/MockUSDC.sol +152 -0
- package/src/naming/AgentNamePredicates.sol +89 -0
- package/src/naming/AgentNameRegistry.sol +65 -18
- package/src/naming/AgentNameUniversalResolver.sol +1 -1
- package/src/naming/PermissionlessSubregistry.sol +19 -2
- package/src/naming/PricedSubregistry.sol +209 -0
- package/src/payments/PaymentEscrow.sol +164 -0
- package/src/payments/PaymentReceiptRegistry.sol +67 -0
- package/src/provenance/ReceiptAnchorRegistry.sol +55 -0
- package/src/registry/AgentRegistryBase.sol +313 -0
- package/src/registry/EstateProjectionRegistry.sol +109 -0
- package/src/registry/IRegistryMembershipPolicy.sol +30 -0
- package/src/relationships/AgentRelationship.sol +8 -0
- package/src/skills/SkillDefinitionRegistry.sol +187 -0
|
@@ -86,6 +86,11 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
|
|
|
86
86
|
"Caveat(address enforcer,bytes terms)"
|
|
87
87
|
);
|
|
88
88
|
|
|
89
|
+
/// @dev Gas cap for the read-only caveat evaluation in `verifyAuthorizationForCall`.
|
|
90
|
+
/// Generous for any pure/view enforcer; bounds the gas a stateful enforcer would
|
|
91
|
+
/// burn before reverting under staticcall, so on-chain callers get a cheap `false`.
|
|
92
|
+
uint256 private constant CAVEAT_VIEW_GAS = 1_000_000;
|
|
93
|
+
|
|
89
94
|
/// @dev Revoked delegation hashes
|
|
90
95
|
mapping(bytes32 => bool) private _revoked;
|
|
91
96
|
|
|
@@ -104,6 +109,10 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
|
|
|
104
109
|
// Phase A.5 errors.
|
|
105
110
|
error NotDelegatorOrDelegate();
|
|
106
111
|
error HashMismatch();
|
|
112
|
+
/// @dev EXT-NOCODE-HOOK-1: a caveat enforcer with no code — a call to it would
|
|
113
|
+
/// succeed vacuously (fail-OPEN), so the live before/after hook loops reject it,
|
|
114
|
+
/// matching the view path (`verifyAuthorizationForCall` → "no-code-enforcer").
|
|
115
|
+
error NoCodeEnforcer(address enforcer);
|
|
107
116
|
/// @dev Thrown by the deprecated permissionless `revokeDelegation(bytes32)`
|
|
108
117
|
/// path. Migrate callers to `revokeDelegationByOwner(Delegation)`.
|
|
109
118
|
error LegacyRevocationDisabled();
|
|
@@ -242,6 +251,171 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
|
|
|
242
251
|
return keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, structHash));
|
|
243
252
|
}
|
|
244
253
|
|
|
254
|
+
/// @notice View-only verification of a delegation chain. Returns whether
|
|
255
|
+
/// the chain is well-formed (signatures, authority chain, not
|
|
256
|
+
/// revoked) WITHOUT executing it.
|
|
257
|
+
///
|
|
258
|
+
/// @dev ⚠️ DANGER — CHAIN-ONLY, NOT A CALL AUTHORIZATION (audit 2026-06-10). This validates ONLY
|
|
259
|
+
/// the chain (signatures / authority / revocation). It does NOT evaluate caveats, so a `true`
|
|
260
|
+
/// result is NOT permission to perform any specific (target, value, calldata). Anything
|
|
261
|
+
/// authorizing a concrete action MUST use {verifyAuthorizationForCall} (evaluates every caveat,
|
|
262
|
+
/// fail-closed) or live {redeemDelegation}. Treating this boolean as call authorization
|
|
263
|
+
/// re-opens the exact over-trust the caveats exist to prevent.
|
|
264
|
+
///
|
|
265
|
+
/// Spec 242 §6 (PD-9): the `attestations` package uses this to verify
|
|
266
|
+
/// bilateral-consent delegations as signed authorization predicates,
|
|
267
|
+
/// rather than redeeming them as cross-account execution. Returning
|
|
268
|
+
/// `(true, "")` means the delegation chain authorizes the named call from
|
|
269
|
+
/// `sender`. Caveat predicates (CalldataHashEnforcer, AllowedTargets,
|
|
270
|
+
/// AllowedMethods, Timestamp) are NOT executed here — callers are expected
|
|
271
|
+
/// to pin those into the delegation's caveats at issuance time, where
|
|
272
|
+
/// they're hash-bound to the delegation signature.
|
|
273
|
+
///
|
|
274
|
+
/// @param delegations Chain (leaf first; root authority last)
|
|
275
|
+
/// @param sender The address that would be the redeemer
|
|
276
|
+
/// @return ok True if every link verifies
|
|
277
|
+
/// @return reason Empty if ok; otherwise a short rejection reason
|
|
278
|
+
function verifyAuthorization(
|
|
279
|
+
Delegation[] calldata delegations,
|
|
280
|
+
address sender
|
|
281
|
+
) external view returns (bool ok, string memory reason) {
|
|
282
|
+
if (delegations.length == 0) return (false, "empty-chain");
|
|
283
|
+
|
|
284
|
+
for (uint256 i = 0; i < delegations.length; i++) {
|
|
285
|
+
(bool valid, string memory r) = _validateDelegationView(delegations, i, sender);
|
|
286
|
+
if (!valid) return (false, r);
|
|
287
|
+
}
|
|
288
|
+
return (true, "");
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/// @notice View verifier for a SPECIFIC call (ADR-0027 / spec 249, RW1-4). Validates the
|
|
292
|
+
/// delegation chain AND evaluates every caveat's `beforeHook` against the exact
|
|
293
|
+
/// `(target, value, data)` — read-only, FAIL-CLOSED.
|
|
294
|
+
/// @dev `verifyAuthorization` checks chain/signature/authority/revocation but NOT the
|
|
295
|
+
/// caveats — too weak to authorize a specific call. This evaluates each caveat via
|
|
296
|
+
/// `staticcall` (the hook is `external`/non-view because stateful enforcers like
|
|
297
|
+
/// Quorum exist). Returns `true` ONLY if the chain validates and EVERY caveat passes
|
|
298
|
+
/// read-only. A caveat that reverts (denied) OR cannot be evaluated read-only (a
|
|
299
|
+
/// stateful enforcer attempting a write under staticcall) yields `false` — so `true`
|
|
300
|
+
/// is a genuine authorization guarantee for this exact call, and `false` means "use
|
|
301
|
+
/// live redemption" (which evaluates stateful caveats properly). The caveat
|
|
302
|
+
/// evaluation mirrors `_runBeforeHooks` exactly (same args, same `sender`-as-redeemer).
|
|
303
|
+
/// SC-3 (audit 2026-06-09): a no-code enforcer is rejected here with `false`. A raw
|
|
304
|
+
/// `staticcall` to an empty address SUCCEEDS, which would otherwise make a delegation
|
|
305
|
+
/// whose caveat enforcer is a typo / undeployed pass as "constraint-checked" — fail-OPEN.
|
|
306
|
+
/// Live redemption fails CLOSED on the same case (solc's extcodesize check on the
|
|
307
|
+
/// high-level void-returning hook call reverts), so this restores symmetry. Pure
|
|
308
|
+
/// addition — `redeemDelegation` is untouched.
|
|
309
|
+
/// @param delegations Chain (leaf first; root authority last)
|
|
310
|
+
/// @param sender The address that would be the redeemer
|
|
311
|
+
/// @param target The exact target the delegated call would hit
|
|
312
|
+
/// @param value The exact ETH value
|
|
313
|
+
/// @param data The exact calldata
|
|
314
|
+
function verifyAuthorizationForCall(
|
|
315
|
+
Delegation[] calldata delegations,
|
|
316
|
+
address sender,
|
|
317
|
+
address target,
|
|
318
|
+
uint256 value,
|
|
319
|
+
bytes calldata data
|
|
320
|
+
) external view returns (bool ok, string memory reason) {
|
|
321
|
+
if (delegations.length == 0) return (false, "empty-chain");
|
|
322
|
+
|
|
323
|
+
for (uint256 i = 0; i < delegations.length; i++) {
|
|
324
|
+
(bool valid, string memory r) = _validateDelegationView(delegations, i, sender);
|
|
325
|
+
if (!valid) return (false, r);
|
|
326
|
+
|
|
327
|
+
Delegation calldata d = delegations[i];
|
|
328
|
+
bytes32 dHash = hashDelegation(d);
|
|
329
|
+
for (uint256 j = 0; j < d.caveats.length; j++) {
|
|
330
|
+
// SC-3: a no-code enforcer constrains NOTHING — a raw staticcall to an empty address
|
|
331
|
+
// returns success, which would fail-open. Reject explicitly so `true` is a genuine
|
|
332
|
+
// guarantee (live redemption fails closed here too via extcodesize on the high-level call).
|
|
333
|
+
if (d.caveats[j].enforcer.code.length == 0) return (false, "no-code-enforcer");
|
|
334
|
+
// staticcall the same hook redemption would call; success == satisfied read-only.
|
|
335
|
+
// Gas-bounded: a stateful enforcer (which would revert on its SSTORE under
|
|
336
|
+
// staticcall) is capped so an on-chain caller (e.g. RW1-1) sees a cheap, predictable
|
|
337
|
+
// `false`, not an out-of-gas of the whole call. CAVEAT_VIEW_GAS is generous for any
|
|
338
|
+
// pure/view enforcer (timestamp/value/targets/methods are all far below it).
|
|
339
|
+
(bool passed, ) = d.caveats[j].enforcer.staticcall{gas: CAVEAT_VIEW_GAS}(
|
|
340
|
+
abi.encodeWithSelector(
|
|
341
|
+
ICaveatEnforcer.beforeHook.selector,
|
|
342
|
+
d.caveats[j].terms,
|
|
343
|
+
d.caveats[j].args,
|
|
344
|
+
dHash,
|
|
345
|
+
d.delegator,
|
|
346
|
+
sender,
|
|
347
|
+
target,
|
|
348
|
+
value,
|
|
349
|
+
data
|
|
350
|
+
)
|
|
351
|
+
);
|
|
352
|
+
if (!passed) return (false, "caveat-failed");
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return (true, "");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/// @dev View-only sibling of `_validateDelegation`. Same checks, no state
|
|
359
|
+
/// writes, no events. Used by `verifyAuthorization`.
|
|
360
|
+
function _validateDelegationView(
|
|
361
|
+
Delegation[] calldata delegations,
|
|
362
|
+
uint256 i,
|
|
363
|
+
address sender
|
|
364
|
+
) internal view returns (bool ok, string memory reason) {
|
|
365
|
+
Delegation calldata d = delegations[i];
|
|
366
|
+
bytes32 dHash = hashDelegation(d);
|
|
367
|
+
|
|
368
|
+
if (_revoked[dHash]) return (false, "revoked");
|
|
369
|
+
|
|
370
|
+
// Delegate chain
|
|
371
|
+
if (i == 0) {
|
|
372
|
+
if (d.delegate != OPEN_DELEGATION && d.delegate != sender) {
|
|
373
|
+
return (false, "invalid-delegate");
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
if (d.delegate != OPEN_DELEGATION && d.delegate != delegations[i - 1].delegator) {
|
|
377
|
+
return (false, "invalid-delegate-chain");
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Authority chain
|
|
382
|
+
if (d.authority != ROOT_AUTHORITY) {
|
|
383
|
+
if (i + 1 >= delegations.length) return (false, "invalid-authority");
|
|
384
|
+
bytes32 parentHash = hashDelegation(delegations[i + 1]);
|
|
385
|
+
if (d.authority != parentHash) return (false, "invalid-authority-chain");
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Signature
|
|
389
|
+
if (!_isValidSignatureBool(d.delegator, dHash, d.signature)) {
|
|
390
|
+
return (false, "invalid-signature");
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
return (true, "");
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/// @dev Bool-returning sibling of `_validateSignature`. Returns false
|
|
397
|
+
/// instead of reverting, so view-only callers can produce a typed
|
|
398
|
+
/// `(ok, reason)` instead of bubbling a revert.
|
|
399
|
+
function _isValidSignatureBool(
|
|
400
|
+
address signer,
|
|
401
|
+
bytes32 digest,
|
|
402
|
+
bytes calldata signature
|
|
403
|
+
) internal view returns (bool) {
|
|
404
|
+
// ERC-1271 path for smart accounts
|
|
405
|
+
if (signer.code.length > 0) {
|
|
406
|
+
try IERC1271(signer).isValidSignature(digest, signature) returns (bytes4 result) {
|
|
407
|
+
return result == IERC1271.isValidSignature.selector;
|
|
408
|
+
} catch {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
// EOA — tryRecover the eth-signed message hash
|
|
413
|
+
bytes32 ethHash = digest.toEthSignedMessageHash();
|
|
414
|
+
(address recovered, ECDSA.RecoverError err, ) = ECDSA.tryRecover(ethHash, signature);
|
|
415
|
+
if (err != ECDSA.RecoverError.NoError) return false;
|
|
416
|
+
return recovered == signer;
|
|
417
|
+
}
|
|
418
|
+
|
|
245
419
|
// ─── Internal: Validation ──────────────────────────────────────────
|
|
246
420
|
|
|
247
421
|
function _validateDelegation(
|
|
@@ -284,6 +458,9 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
|
|
|
284
458
|
) internal {
|
|
285
459
|
bytes32 dHash = hashDelegation(d);
|
|
286
460
|
for (uint256 j = 0; j < d.caveats.length; j++) {
|
|
461
|
+
// EXT-NOCODE-HOOK-1: fail closed on a no-code enforcer (a call would succeed vacuously),
|
|
462
|
+
// symmetric with the view path's "no-code-enforcer" reject — never rely on call semantics.
|
|
463
|
+
if (d.caveats[j].enforcer.code.length == 0) revert NoCodeEnforcer(d.caveats[j].enforcer);
|
|
287
464
|
ICaveatEnforcer(d.caveats[j].enforcer).beforeHook(
|
|
288
465
|
d.caveats[j].terms,
|
|
289
466
|
d.caveats[j].args,
|
|
@@ -305,6 +482,8 @@ contract DelegationManager is IDelegationManager, ReentrancyGuard {
|
|
|
305
482
|
) internal {
|
|
306
483
|
bytes32 dHash = hashDelegation(d);
|
|
307
484
|
for (uint256 j = 0; j < d.caveats.length; j++) {
|
|
485
|
+
// EXT-NOCODE-HOOK-1: fail closed on a no-code enforcer, symmetric with the before-hook + view path.
|
|
486
|
+
if (d.caveats[j].enforcer.code.length == 0) revert NoCodeEnforcer(d.caveats[j].enforcer);
|
|
308
487
|
ICaveatEnforcer(d.caveats[j].enforcer).afterHook(
|
|
309
488
|
d.caveats[j].terms,
|
|
310
489
|
d.caveats[j].args,
|
|
@@ -66,4 +66,42 @@ interface IDelegationManager {
|
|
|
66
66
|
|
|
67
67
|
/// @notice Check if a delegation has been revoked.
|
|
68
68
|
function isRevoked(bytes32 delegationHash) external view returns (bool);
|
|
69
|
+
|
|
70
|
+
/// @notice View-only verification of a delegation chain. Returns whether
|
|
71
|
+
/// the chain authorizes `sender` to redeem WITHOUT executing it.
|
|
72
|
+
/// Used by trust-substrate contracts (e.g. AttestationRegistry's
|
|
73
|
+
/// bilateral-consent path per spec 242 PD-9) to validate a packed
|
|
74
|
+
/// delegation as a signed authorization predicate.
|
|
75
|
+
/// @dev ⚠️ DANGER — chain-only; does NOT evaluate caveats. `true` is NOT permission for any
|
|
76
|
+
/// specific call. Use {verifyAuthorizationForCall} (fail-closed, evaluates caveats) or live
|
|
77
|
+
/// redemption to authorize a concrete (target, value, calldata).
|
|
78
|
+
/// @param delegations Delegation chain, leaf first
|
|
79
|
+
/// @param sender The address that would be the redeemer
|
|
80
|
+
/// @return ok True if the chain verifies
|
|
81
|
+
/// @return reason Empty if ok; otherwise short rejection reason
|
|
82
|
+
function verifyAuthorization(
|
|
83
|
+
Delegation[] calldata delegations,
|
|
84
|
+
address sender
|
|
85
|
+
) external view returns (bool ok, string memory reason);
|
|
86
|
+
|
|
87
|
+
/// @notice View-only authorization of a SPECIFIC call (ADR-0027 / spec 249 RW1-4):
|
|
88
|
+
/// validates the chain AND evaluates every caveat's `beforeHook` against the exact
|
|
89
|
+
/// `(target, value, data)` read-only, fail-closed. `true` ONLY if the chain validates
|
|
90
|
+
/// and every caveat passes under `staticcall`; a denied OR non-view-evaluable caveat
|
|
91
|
+
/// yields `false` ("use live redemption"). Lets a registry bind consent to the exact
|
|
92
|
+
/// call inside its own transaction. Caveat evaluation mirrors `redeemDelegation`.
|
|
93
|
+
/// @param delegations Delegation chain, leaf first
|
|
94
|
+
/// @param sender The address that would be the redeemer
|
|
95
|
+
/// @param target The exact target of the delegated call
|
|
96
|
+
/// @param value The exact ETH value
|
|
97
|
+
/// @param data The exact calldata
|
|
98
|
+
/// @return ok True if the chain + every caveat authorize this exact call
|
|
99
|
+
/// @return reason Empty if ok; otherwise short rejection reason
|
|
100
|
+
function verifyAuthorizationForCall(
|
|
101
|
+
Delegation[] calldata delegations,
|
|
102
|
+
address sender,
|
|
103
|
+
address target,
|
|
104
|
+
uint256 value,
|
|
105
|
+
bytes calldata data
|
|
106
|
+
) external view returns (bool ok, string memory reason);
|
|
69
107
|
}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
import "@openzeppelin/contracts/interfaces/IERC1271.sol";
|
|
5
|
+
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @title AgreementRegistry
|
|
9
|
+
* @notice Commitment-only registry per spec 241 + ADR-0024 Decision 2.
|
|
10
|
+
*
|
|
11
|
+
* ─── Substrate role ───
|
|
12
|
+
*
|
|
13
|
+
* Layer 8 of the coordination spine — Agreement / Commitment. Stores the
|
|
14
|
+
* commitment HASH only (no agreement bodies, no party SAs). Bodies live in
|
|
15
|
+
* party Joint Vaults; party SAs surface only through joint assertions
|
|
16
|
+
* registered in the AttestationRegistry (which references our `commitmentHash`).
|
|
17
|
+
*
|
|
18
|
+
* ─── Key invariants (AR-*) ───
|
|
19
|
+
*
|
|
20
|
+
* AR-01 register() recomputes the commitment from supplied components
|
|
21
|
+
* AR-02 issuer EIP-712 attestation verified via ERC-1271
|
|
22
|
+
* AR-03 schema validated against (off-chain) ShapeRegistry
|
|
23
|
+
* AR-04 status state-machine: ACTIVE → COMPLETED | DISPUTED | REVOKED
|
|
24
|
+
* AR-05 bilateral signing required for ACTIVE→COMPLETED, ACTIVE→REVOKED
|
|
25
|
+
* AR-06 either-party for ACTIVE→DISPUTED
|
|
26
|
+
* AR-07 NO issuerRevoke / issuerStatusUpdate entrypoint — by static analysis
|
|
27
|
+
* AR-08 nullifier set prevents replay on status transitions
|
|
28
|
+
* AR-09 epoch-bucket timestamps; raw block.timestamp never stored
|
|
29
|
+
* AR-10 isAssertableCommitment view gateway for AttestationRegistry
|
|
30
|
+
* AR-11 party SA addresses NEVER appear in register() calldata
|
|
31
|
+
* AR-12 single on-chain row per agreement (D-37)
|
|
32
|
+
*/
|
|
33
|
+
contract AgreementRegistry {
|
|
34
|
+
// ─── Constants ──────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
uint64 public constant EPOCH_SECONDS = 3600;
|
|
37
|
+
|
|
38
|
+
uint8 public constant STATUS_NONE = 0;
|
|
39
|
+
uint8 public constant STATUS_ACTIVE = 1;
|
|
40
|
+
uint8 public constant STATUS_COMPLETED = 2;
|
|
41
|
+
uint8 public constant STATUS_DISPUTED = 3;
|
|
42
|
+
uint8 public constant STATUS_REVOKED = 4;
|
|
43
|
+
|
|
44
|
+
/// @dev RW1-3 (ADR-0027): the contract RECOMPUTES the transition digest the
|
|
45
|
+
/// parties sign from (agreementCommitment, toStatus, nullifier) — it no
|
|
46
|
+
/// longer trusts a caller-supplied `transitionStructHash`. One canonical
|
|
47
|
+
/// digest model; the TS side derives the same constant (cross-stack
|
|
48
|
+
/// typehash-equality gate). Plain struct hash (no domain separator),
|
|
49
|
+
/// matching the attestation registry's JOINT_CONSENT_TYPEHASH.
|
|
50
|
+
/// AGR-1 (audit 2026-06-10): also bind chainId + this contract so a transition signature
|
|
51
|
+
/// cannot be replayed on another chain or a redeployed registry.
|
|
52
|
+
bytes32 internal constant TRANSITION_TYPEHASH = keccak256(
|
|
53
|
+
"AgreementTransition(bytes32 agreementCommitment,uint8 toStatus,bytes32 nullifier,uint256 chainId,address verifyingContract)"
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
/// @dev SC-1 (audit 2026-06-09): the issuer attestation digest is RECOMPUTED on-chain from the
|
|
57
|
+
/// agreement's contents + chain + this contract — it is NOT a caller-supplied free-form hash.
|
|
58
|
+
/// Binds (agreementCommitment, schemaHash, issuer, chainId, verifyingContract) so a signature
|
|
59
|
+
/// lifted from any other context cannot back an attacker-chosen commitment. The TS side derives
|
|
60
|
+
/// the same constant (cross-stack typehash-equality gate).
|
|
61
|
+
bytes32 internal constant AGREEMENT_ISSUER_TYPEHASH = keccak256(
|
|
62
|
+
"AgreementIssuerAttestation(bytes32 agreementCommitment,bytes32 schemaHash,address issuer,uint256 chainId,address verifyingContract)"
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// ─── Errors ─────────────────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
error AlreadyRegistered();
|
|
68
|
+
error NotRegistered();
|
|
69
|
+
error InvalidStatus();
|
|
70
|
+
error InvalidIssuerSignature();
|
|
71
|
+
error InvalidTransitionSignature();
|
|
72
|
+
error NullifierUsed();
|
|
73
|
+
error CommitmentMismatch();
|
|
74
|
+
error SignerNotParty();
|
|
75
|
+
|
|
76
|
+
// ─── Storage ────────────────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
struct AgreementRow {
|
|
79
|
+
bytes32 agreementCommitment; // canonical commitment hash
|
|
80
|
+
bytes32 schemaHash; // SHACL shape hash from ShapeRegistry
|
|
81
|
+
address issuer; // SA whose EIP-712 attestation backs the commitment
|
|
82
|
+
uint8 status;
|
|
83
|
+
uint64 createdEpochBucket;
|
|
84
|
+
uint64 lastTransitionEpochBucket;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// @dev key = agreementCommitment hash
|
|
88
|
+
mapping(bytes32 => AgreementRow) private _rows;
|
|
89
|
+
|
|
90
|
+
/// @dev AR-08: nullifier set for replay protection on status transitions.
|
|
91
|
+
mapping(bytes32 => bool) private _nullifiers;
|
|
92
|
+
|
|
93
|
+
// ─── Requests ───────────────────────────────────────────────────────
|
|
94
|
+
|
|
95
|
+
/// @notice The components the contract recomputes the commitment from
|
|
96
|
+
/// (per spec 241 §3). Party SAs are intentionally NOT in this
|
|
97
|
+
/// payload — AR-11.
|
|
98
|
+
struct AgreementIssuancePayload {
|
|
99
|
+
bytes32 schemaHash;
|
|
100
|
+
address issuer;
|
|
101
|
+
/// @dev issuer's signature over the RECOMPUTED issuer-attestation digest (SC-1). The contract
|
|
102
|
+
/// derives the digest from the payload contents — no caller-supplied attestation hash.
|
|
103
|
+
bytes issuerSignature;
|
|
104
|
+
/// @dev The asserted canonical commitment hash; we verify recomputation.
|
|
105
|
+
bytes32 agreementCommitment;
|
|
106
|
+
/// @dev Component commitments for AR-01 recomputation:
|
|
107
|
+
/// keccak256(partySet, issuerCommitment, termsCommitment, scheduleCommitment, salt) == agreementCommitment
|
|
108
|
+
bytes32 partySetCommitment;
|
|
109
|
+
bytes32 issuerCommitment;
|
|
110
|
+
bytes32 termsCommitment;
|
|
111
|
+
bytes32 scheduleCommitment;
|
|
112
|
+
uint256 salt;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
struct StatusUpdatePayload {
|
|
116
|
+
bytes32 agreementCommitment;
|
|
117
|
+
uint8 toStatus;
|
|
118
|
+
bytes32 nullifier;
|
|
119
|
+
/// @dev Signatures over the RECOMPUTED transition digest (RW1-3): 1 for
|
|
120
|
+
/// either-party (DISPUTED), 2 for bilateral (COMPLETED/REVOKED).
|
|
121
|
+
bytes signature1;
|
|
122
|
+
bytes signature2;
|
|
123
|
+
/// @dev Party SAs that signed. For DISPUTED: signer1 only. For COMPLETED/REVOKED: both.
|
|
124
|
+
address signer1;
|
|
125
|
+
address signer2;
|
|
126
|
+
/// @dev RW1-2 (ADR-0027): the agreement's parties + commitment components, REVEALED at
|
|
127
|
+
/// transition time so the contract can recompute the commitment and PROVE the signers
|
|
128
|
+
/// are the parties. (Registration stays commitment-only — AR-12; the transition is
|
|
129
|
+
/// already a public state change that names its signers.)
|
|
130
|
+
address party1;
|
|
131
|
+
address party2;
|
|
132
|
+
bytes32 issuerCommitment;
|
|
133
|
+
bytes32 termsCommitment;
|
|
134
|
+
bytes32 scheduleCommitment;
|
|
135
|
+
uint256 commitmentSalt;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ─── Events ─────────────────────────────────────────────────────────
|
|
139
|
+
|
|
140
|
+
event AgreementRegistered(
|
|
141
|
+
bytes32 indexed agreementCommitment,
|
|
142
|
+
address indexed issuer,
|
|
143
|
+
bytes32 indexed schemaHash,
|
|
144
|
+
uint64 epochBucket
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
event StatusUpdated(
|
|
148
|
+
bytes32 indexed agreementCommitment,
|
|
149
|
+
uint8 indexed fromStatus,
|
|
150
|
+
uint8 indexed toStatus,
|
|
151
|
+
uint64 epochBucket
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
// ─── Public surface ─────────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
/// @notice Register an agreement by its commitment hash.
|
|
157
|
+
function register(AgreementIssuancePayload calldata p) external returns (bytes32) {
|
|
158
|
+
// AR-01 — recompute commitment from components
|
|
159
|
+
bytes32 recomputed = keccak256(
|
|
160
|
+
abi.encode(p.partySetCommitment, p.issuerCommitment, p.termsCommitment, p.scheduleCommitment, p.salt)
|
|
161
|
+
);
|
|
162
|
+
if (recomputed != p.agreementCommitment) revert CommitmentMismatch();
|
|
163
|
+
|
|
164
|
+
if (_rows[p.agreementCommitment].agreementCommitment != bytes32(0)) {
|
|
165
|
+
revert AlreadyRegistered();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// AR-02 / SC-1 — RECOMPUTE the issuer-attestation digest from the agreement contents (+ chain +
|
|
169
|
+
// this contract) and verify the issuer signed THAT. A signature over an arbitrary digest from
|
|
170
|
+
// any other context can no longer back an attacker-chosen commitment.
|
|
171
|
+
bytes32 issuerDigest = keccak256(
|
|
172
|
+
abi.encode(
|
|
173
|
+
AGREEMENT_ISSUER_TYPEHASH,
|
|
174
|
+
p.agreementCommitment,
|
|
175
|
+
p.schemaHash,
|
|
176
|
+
p.issuer,
|
|
177
|
+
block.chainid,
|
|
178
|
+
address(this)
|
|
179
|
+
)
|
|
180
|
+
);
|
|
181
|
+
if (!_isValidSignatureBool(p.issuer, issuerDigest, p.issuerSignature)) {
|
|
182
|
+
revert InvalidIssuerSignature();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
uint64 bucket = uint64(block.timestamp) / EPOCH_SECONDS;
|
|
186
|
+
|
|
187
|
+
_rows[p.agreementCommitment] = AgreementRow({
|
|
188
|
+
agreementCommitment: p.agreementCommitment,
|
|
189
|
+
schemaHash: p.schemaHash,
|
|
190
|
+
issuer: p.issuer,
|
|
191
|
+
status: STATUS_ACTIVE,
|
|
192
|
+
createdEpochBucket: bucket,
|
|
193
|
+
lastTransitionEpochBucket: bucket
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
emit AgreementRegistered(p.agreementCommitment, p.issuer, p.schemaHash, bucket);
|
|
197
|
+
return p.agreementCommitment;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/// @notice Update the agreement status. Signing requirements (AR-04..AR-06):
|
|
201
|
+
/// - DISPUTED: signer1 (either party) only
|
|
202
|
+
/// - COMPLETED / REVOKED: both signer1 + signer2 (bilateral)
|
|
203
|
+
/// RW1-2 (ADR-0027): the caller REVEALS the parties + commitment
|
|
204
|
+
/// components; the contract recomputes the commitment (register's
|
|
205
|
+
/// exact formula) and requires each signer to BE one of the two
|
|
206
|
+
/// parties. The on-chain row still stores NO party SAs (AR-11 covers
|
|
207
|
+
/// register() calldata only); the parties surface here because a
|
|
208
|
+
/// status transition is already a public state change that names its
|
|
209
|
+
/// signers. Replay protection comes from the nullifier. RW1-3: the
|
|
210
|
+
/// transition digest the parties sign is RECOMPUTED here from
|
|
211
|
+
/// (agreementCommitment, toStatus, nullifier) — not caller-supplied.
|
|
212
|
+
function updateStatus(StatusUpdatePayload calldata p) external {
|
|
213
|
+
AgreementRow storage row = _rows[p.agreementCommitment];
|
|
214
|
+
if (row.agreementCommitment == bytes32(0)) revert NotRegistered();
|
|
215
|
+
|
|
216
|
+
// Status transition validity (AR-04)
|
|
217
|
+
if (row.status != STATUS_ACTIVE && row.status != STATUS_DISPUTED) revert InvalidStatus();
|
|
218
|
+
if (
|
|
219
|
+
p.toStatus != STATUS_COMPLETED &&
|
|
220
|
+
p.toStatus != STATUS_DISPUTED &&
|
|
221
|
+
p.toStatus != STATUS_REVOKED
|
|
222
|
+
) {
|
|
223
|
+
revert InvalidStatus();
|
|
224
|
+
}
|
|
225
|
+
// DISPUTED → COMPLETED / REVOKED needs bilateral.
|
|
226
|
+
// ACTIVE → DISPUTED: either-party allowed.
|
|
227
|
+
// ACTIVE → COMPLETED / REVOKED: bilateral.
|
|
228
|
+
|
|
229
|
+
// AR-08 — replay protection
|
|
230
|
+
if (_nullifiers[p.nullifier]) revert NullifierUsed();
|
|
231
|
+
_nullifiers[p.nullifier] = true;
|
|
232
|
+
|
|
233
|
+
// RW1-2 (ADR-0027): PROVE the signers are the agreement's parties. Recompute the agreement
|
|
234
|
+
// commitment from the REVEALED parties + components (register's exact formula) and require it
|
|
235
|
+
// to equal the row's commitment; then require each signer to BE a party. The caller-supplied
|
|
236
|
+
// signer set is no longer trusted (a nonzero commitment ref / arbitrary signer is not authority).
|
|
237
|
+
{
|
|
238
|
+
bytes32 partySet = keccak256(abi.encodePacked(p.party1, p.party2));
|
|
239
|
+
bytes32 recomputed = keccak256(
|
|
240
|
+
abi.encode(partySet, p.issuerCommitment, p.termsCommitment, p.scheduleCommitment, p.commitmentSalt)
|
|
241
|
+
);
|
|
242
|
+
if (recomputed != p.agreementCommitment) revert CommitmentMismatch();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// RW1-3 (ADR-0027): RECOMPUTE the transition digest the parties sign — do not trust a
|
|
246
|
+
// caller-supplied hash. The signed payload is canonically bound to (commitment, toStatus,
|
|
247
|
+
// nullifier) here, on chain.
|
|
248
|
+
bytes32 transitionDigest = keccak256(
|
|
249
|
+
abi.encode(TRANSITION_TYPEHASH, p.agreementCommitment, p.toStatus, p.nullifier, block.chainid, address(this))
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
bool bilateralRequired = (p.toStatus != STATUS_DISPUTED);
|
|
253
|
+
|
|
254
|
+
// signer1 must BE a party, and must have signed.
|
|
255
|
+
if (p.signer1 != p.party1 && p.signer1 != p.party2) revert SignerNotParty();
|
|
256
|
+
if (!_isValidSignatureBool(p.signer1, transitionDigest, p.signature1)) {
|
|
257
|
+
revert InvalidTransitionSignature();
|
|
258
|
+
}
|
|
259
|
+
if (bilateralRequired) {
|
|
260
|
+
// both DISTINCT parties must sign.
|
|
261
|
+
if (p.signer2 == address(0) || p.signer2 == p.signer1) revert InvalidTransitionSignature();
|
|
262
|
+
if (p.signer2 != p.party1 && p.signer2 != p.party2) revert SignerNotParty();
|
|
263
|
+
if (!_isValidSignatureBool(p.signer2, transitionDigest, p.signature2)) {
|
|
264
|
+
revert InvalidTransitionSignature();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
uint8 fromStatus = row.status;
|
|
269
|
+
row.status = p.toStatus;
|
|
270
|
+
row.lastTransitionEpochBucket = uint64(block.timestamp) / EPOCH_SECONDS;
|
|
271
|
+
|
|
272
|
+
emit StatusUpdated(p.agreementCommitment, fromStatus, p.toStatus, row.lastTransitionEpochBucket);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/// @notice Gateway for spec 242 AttestationRegistry.assertJointAgreement.
|
|
276
|
+
/// Returns (true, "") if the commitment exists and is in a state
|
|
277
|
+
/// that permits public joint assertion (currently any non-NONE
|
|
278
|
+
/// status; revoked agreements can still be jointly disclosed as
|
|
279
|
+
/// historical record).
|
|
280
|
+
///
|
|
281
|
+
/// @dev NOT a consent or validity oracle (audit 2026-06-10). A `true`
|
|
282
|
+
/// result means only that `agreementCommitment` is REGISTERED — it is
|
|
283
|
+
/// an opaque back-reference, NOT proof that any particular party
|
|
284
|
+
/// consented, that the agreement is currently valid, or that a given
|
|
285
|
+
/// credential is bound to it. Bilateral consent for a joint assertion
|
|
286
|
+
/// is established separately by `AttestationRegistry.assertJointAgreement`,
|
|
287
|
+
/// which recomputes both parties' consent signatures. Consumers MUST
|
|
288
|
+
/// NOT treat the boolean here as authorization.
|
|
289
|
+
function isAssertableCommitment(
|
|
290
|
+
bytes32 agreementCommitment,
|
|
291
|
+
address /* actor */
|
|
292
|
+
) external view returns (bool ok, string memory reason) {
|
|
293
|
+
AgreementRow memory row = _rows[agreementCommitment];
|
|
294
|
+
if (row.agreementCommitment == bytes32(0)) return (false, "not-registered");
|
|
295
|
+
return (true, "");
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/// @notice Get the on-chain row.
|
|
299
|
+
function getRecord(bytes32 agreementCommitment) external view returns (AgreementRow memory) {
|
|
300
|
+
AgreementRow memory row = _rows[agreementCommitment];
|
|
301
|
+
if (row.agreementCommitment == bytes32(0)) revert NotRegistered();
|
|
302
|
+
return row;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/// @notice Has a nullifier been consumed?
|
|
306
|
+
function isNullifierUsed(bytes32 n) external view returns (bool) {
|
|
307
|
+
return _nullifiers[n];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// ─── Internal ───────────────────────────────────────────────────────
|
|
311
|
+
|
|
312
|
+
function _isValidSignatureBool(
|
|
313
|
+
address signer,
|
|
314
|
+
bytes32 digest,
|
|
315
|
+
bytes memory signature
|
|
316
|
+
) internal view returns (bool) {
|
|
317
|
+
if (signer.code.length > 0) {
|
|
318
|
+
try IERC1271(signer).isValidSignature(digest, signature) returns (bytes4 result) {
|
|
319
|
+
return result == IERC1271.isValidSignature.selector;
|
|
320
|
+
} catch {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// P2-2 (audit 2026-06-10): route EOA recovery through OpenZeppelin ECDSA.tryRecover instead of
|
|
325
|
+
// raw ecrecover — it rejects high-s (malleable) signatures and malformed lengths, matching the
|
|
326
|
+
// DelegationManager idiom so every signature-verifying contract shares one low-s-safe path.
|
|
327
|
+
bytes32 ethHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", digest));
|
|
328
|
+
(address recovered, ECDSA.RecoverError err, ) = ECDSA.tryRecover(ethHash, signature);
|
|
329
|
+
return err == ECDSA.RecoverError.NoError && recovered == signer;
|
|
330
|
+
}
|
|
331
|
+
}
|