Skip to content

Instantly share code, notes, and snippets.

@apetersson
Created August 29, 2025 09:34
Show Gist options
  • Select an option

  • Save apetersson/ab3be078c36cf5186355b605effbd408 to your computer and use it in GitHub Desktop.

Select an option

Save apetersson/ab3be078c36cf5186355b605effbd408 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Interface to access storage of Safe contracts
interface IStorageAccessible {
function getStorageAt(uint256 offset, uint256 length) external view returns (bytes memory);
}
library GuardReader {
// Storage slot for the guard address in a Gnosis Safe contract (from GuardManager.sol)
bytes32 constant GUARD_STORAGE_SLOT = 0x4a204f620c8c5ccdca3fd54d003badd85ba500436a431f0cbda4f558c93c34c8;
/**
* @notice Reads the guard address directly from a Safe's storage.
* @dev This is necessary for Safe v1.4.1 which does not have a public `getGuard()` function.
* @param safeAddress The address of the Gnosis Safe contract.
* @return guardAddress The address of the guard, or the zero address if none is set or if the call fails.
*/
function getGuard(address safeAddress) internal view returns (address guardAddress) {
try IStorageAccessible(safeAddress).getStorageAt(uint256(GUARD_STORAGE_SLOT), 1) returns (bytes memory guardBytes) {
if (guardBytes.length == 32) {
assembly {
guardAddress := mload(add(guardBytes, 32))
}
}
} catch {
// If the call fails for any reason, return the zero address.
return address(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment