-
Notifications
You must be signed in to change notification settings - Fork 1
feat(safe): add guard lifecycle scripts #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
makemake-kbo
merged 5 commits into
master
from
makemake/eng-4054-featguard-script-to-deploy-and-remove-guard
Jul 27, 2026
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6624c42
feat(safe): add guard lifecycle scripts
makemake-kbo e23f54d
fix(safe): validate registry before deploy + cover adversarial regist…
makemake-kbo 05213a9
docs(safe): document fail-open-on-registry-failure as an intentional …
makemake-kbo bb3d6c5
fix(safe-guard): reject non-canonical bool from registry in validateR…
makemake-kbo 22434a1
fix(safe): fail open when registry reports a future lastCredibleBlock
makemake-kbo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.13; | ||
|
|
||
| import {Script, console2} from "forge-std/Script.sol"; | ||
|
|
||
| import {CredibleSafeGuard} from "credible-std/protection/safe/CredibleSafeGuard.sol"; | ||
| import {ICredibleRegistry} from "credible-std/protection/safe/ICredibleRegistry.sol"; | ||
|
|
||
| /// @notice Deploys a Credible Safe guard using Foundry's configured broadcast wallet. | ||
| contract DeployCredibleSafeGuard is Script { | ||
| function run() external returns (CredibleSafeGuard guard) { | ||
| address registry = vm.envAddress("CREDIBLE_REGISTRY"); | ||
| uint256 threshold = vm.envUint("FAIL_OPEN_BLOCK_THRESHOLD"); | ||
|
|
||
| vm.startBroadcast(); | ||
| guard = deploy(registry, threshold); | ||
| vm.stopBroadcast(); | ||
|
|
||
| console2.log("Chain ID:", block.chainid); | ||
| console2.log("Credible Safe guard:", address(guard)); | ||
| console2.log("Credible Registry:", registry); | ||
| console2.log("Fail-open block threshold:", threshold); | ||
| } | ||
|
|
||
| function deploy(address registry, uint256 threshold) public returns (CredibleSafeGuard) { | ||
| return new CredibleSafeGuard(ICredibleRegistry(registry), threshold); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.13; | ||
|
|
||
| import {console2} from "forge-std/Script.sol"; | ||
|
|
||
| import {SafeGuardBatch} from "./SafeGuardBatch.sol"; | ||
|
|
||
| /// @notice Generates a Safe Transaction Builder JSON file for installing or removing a guard. | ||
| /// @dev This script never broadcasts or signs. Import the generated file into Safe{Wallet}'s | ||
| /// Transaction Builder and submit it through the Safe's normal owner-confirmation flow. | ||
| contract GenerateSafeGuardBatch is SafeGuardBatch { | ||
|
makemake-kbo marked this conversation as resolved.
|
||
| function run() external returns (string memory json, string memory path) { | ||
| address safe = vm.envAddress("SAFE_ADDRESS"); | ||
| Action action = parseAction(vm.envString("SAFE_GUARD_ACTION")); | ||
| address guard = vm.envOr("CREDIBLE_SAFE_GUARD", address(0)); | ||
| uint256 createdAt = vm.unixTime(); | ||
|
|
||
| if (action == Action.Install) { | ||
| json = buildInstallBatch(safe, guard, block.chainid, createdAt); | ||
| } else { | ||
| json = buildRemoveBatch(safe, block.chainid, createdAt); | ||
| } | ||
|
|
||
| path = outputPath(action); | ||
| vm.createDir("safe-guard-output", true); | ||
| vm.writeFile(path, json); | ||
|
|
||
| console2.log("Safe Transaction Builder file:", path); | ||
| console2.log("Chain ID:", block.chainid); | ||
| console2.log("Safe:", safe); | ||
| console2.log("Action:", action == Action.Install ? "install" : "remove"); | ||
| console2.log("Guard:", action == Action.Install ? guard : address(0)); | ||
| console2.log("Import this JSON in Safe{Wallet} Transaction Builder and verify it before signing."); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.13; | ||
|
|
||
| import {Script} from "forge-std/Script.sol"; | ||
|
|
||
| import {IERC165, ITransactionGuard} from "credible-std/protection/safe/CredibleSafeGuard.sol"; | ||
|
|
||
| /// @notice Builds Safe Transaction Builder batches for installing and removing a Safe guard. | ||
| /// @dev The checksum implementation matches Safe Transaction Builder's canonical serializer for | ||
| /// the fixed one-transaction schema emitted by this contract. | ||
| abstract contract SafeGuardBatch is Script { | ||
| enum Action { | ||
| Install, | ||
| Remove | ||
| } | ||
|
|
||
| bytes4 internal constant SAFE_GUARD_INTERFACE_ID = type(ITransactionGuard).interfaceId; | ||
| string internal constant TX_BUILDER_VERSION = "2.0.1"; | ||
|
|
||
| error ZeroSafeAddress(); | ||
| error SafeHasNoCode(address safe); | ||
| error ZeroGuardAddress(); | ||
| error GuardHasNoCode(address guard); | ||
| error UnsupportedGuardInterface(address guard); | ||
| error InvalidAction(string action); | ||
|
|
||
| function buildInstallBatch(address safe, address guard, uint256 chainId, uint256 createdAt) | ||
| public | ||
| view | ||
| returns (string memory) | ||
| { | ||
| _validateSafe(safe); | ||
| _validateGuard(guard); | ||
| return _buildBatch(safe, guard, Action.Install, chainId, createdAt); | ||
| } | ||
|
|
||
| function buildRemoveBatch(address safe, uint256 chainId, uint256 createdAt) public view returns (string memory) { | ||
| _validateSafe(safe); | ||
| return _buildBatch(safe, address(0), Action.Remove, chainId, createdAt); | ||
| } | ||
|
|
||
| function parseAction(string memory action) public pure returns (Action) { | ||
| bytes32 actionHash = keccak256(bytes(action)); | ||
| if (actionHash == keccak256("install")) return Action.Install; | ||
| if (actionHash == keccak256("remove")) return Action.Remove; | ||
| revert InvalidAction(action); | ||
| } | ||
|
|
||
| function outputPath(Action action) public pure returns (string memory) { | ||
| return action == Action.Install ? "safe-guard-output/install.json" : "safe-guard-output/remove.json"; | ||
| } | ||
|
|
||
| function calculateChecksum(address safe, address guard, uint256 chainId, uint256 createdAt) | ||
| public | ||
| view | ||
| returns (bytes32) | ||
| { | ||
| string memory safeString = vm.toString(safe); | ||
| string memory guardString = vm.toString(guard); | ||
| string memory chainIdString = vm.toString(chainId); | ||
| string memory createdAtString = vm.toString(createdAt); | ||
|
|
||
| string memory canonicalMeta = string.concat( | ||
| '{["createdFromOwnerAddress","createdFromSafeAddress","description","name","txBuilderVersion"]', | ||
| '"",', | ||
| _quote(safeString), | ||
| ',"",null,"', | ||
| TX_BUILDER_VERSION, | ||
| '",}' | ||
| ); | ||
|
|
||
| string memory canonicalInput = string.concat('{["internalType","name","type"]', '"address","guard","address",}'); | ||
| string memory canonicalMethod = | ||
| string.concat('{["inputs","name","payable"]', "[", canonicalInput, "]", ',"setGuard",false,}'); | ||
| string memory canonicalInputs = string.concat('{["guard"]', _quote(guardString), ",}"); | ||
| string memory canonicalTransaction = string.concat( | ||
| '{["contractInputsValues","contractMethod","data","to","value"]', | ||
| canonicalInputs, | ||
| ",", | ||
| canonicalMethod, | ||
| ",null,", | ||
| _quote(safeString), | ||
| ',"0",}' | ||
| ); | ||
| string memory canonicalRoot = string.concat( | ||
| '{["chainId","createdAt","meta","transactions","version"]', | ||
| _quote(chainIdString), | ||
| ",", | ||
| createdAtString, | ||
| ",", | ||
| canonicalMeta, | ||
| ",[", | ||
| canonicalTransaction, | ||
| '],"1.0",}' | ||
| ); | ||
|
|
||
| return keccak256(bytes(canonicalRoot)); | ||
| } | ||
|
|
||
| function _buildBatch(address safe, address guard, Action action, uint256 chainId, uint256 createdAt) | ||
| internal | ||
| view | ||
| returns (string memory) | ||
| { | ||
| string memory safeString = vm.toString(safe); | ||
| string memory guardString = vm.toString(guard); | ||
| string memory checksum = vm.toString(calculateChecksum(safe, guard, chainId, createdAt)); | ||
| string memory name = action == Action.Install ? "Install Credible Safe Guard" : "Remove Credible Safe Guard"; | ||
|
|
||
| return string.concat( | ||
| '{"version":"1.0","chainId":', | ||
| _quote(vm.toString(chainId)), | ||
| ',"createdAt":', | ||
| vm.toString(createdAt), | ||
| ',"meta":{"name":', | ||
| _quote(name), | ||
| ',"description":"","txBuilderVersion":"', | ||
| TX_BUILDER_VERSION, | ||
| '","createdFromSafeAddress":', | ||
| _quote(safeString), | ||
| ',"createdFromOwnerAddress":"","checksum":', | ||
| _quote(checksum), | ||
| '},"transactions":[{"to":', | ||
| _quote(safeString), | ||
| ',"value":"0","data":null,"contractMethod":{"inputs":[{"internalType":"address","name":"guard","type":"address"}],"name":"setGuard","payable":false},"contractInputsValues":{"guard":', | ||
| _quote(guardString), | ||
| "}}]}" | ||
| ); | ||
| } | ||
|
|
||
| function _validateSafe(address safe) internal view { | ||
| if (safe == address(0)) revert ZeroSafeAddress(); | ||
| if (safe.code.length == 0) revert SafeHasNoCode(safe); | ||
| } | ||
|
|
||
| function _validateGuard(address guard) internal view { | ||
| if (guard == address(0)) revert ZeroGuardAddress(); | ||
| if (guard.code.length == 0) revert GuardHasNoCode(guard); | ||
|
|
||
| (bool success, bytes memory result) = | ||
| guard.staticcall(abi.encodeCall(IERC165.supportsInterface, (SAFE_GUARD_INTERFACE_ID))); | ||
| uint256 supported; | ||
| if (success && result.length == 32) { | ||
| assembly ("memory-safe") { | ||
| supported := mload(add(result, 0x20)) | ||
| } | ||
| } | ||
| if (!success || result.length != 32 || supported != 1) revert UnsupportedGuardInterface(guard); | ||
| } | ||
|
|
||
| function _quote(string memory value) internal pure returns (string memory) { | ||
| return string.concat('"', value, '"'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.