Verified contractExactBUSL-1.1
SignTypedMessageLib
Address0xd8e9a20f9fdcae4e6390cc0724d88853c50d07e3
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.38
- Same code
Only this contract
Details
- Compiler
- solc 0.8.38+commit.1835d247
- Language
- solidity
- Optimization
- Yes, 10 runs
- EVM version
- cancun
- License
- BUSL-1.1
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,499,912
- Creation transaction
- 0xba4ce80d23…b3debe32a7
5 files
- project/contracts/periphery/signing/SignTypedMessageLib.solmain8.72 KB
- npm/@gnosis-guild/[email protected]/contracts/core/Operation.sol110 B
- project/contracts/evaluator/types/EvaluatorTypes.sol5.86 KB
- project/contracts/periphery/interfaces/SafeStorage.sol818 B
- project/contracts/periphery/signing/EIP712Encoder.sol9.05 KB
project/contracts/periphery/signing/SignTypedMessageLib.sol
// SPDX-License-Identifier: BUSL-1.1
// Copyright (c) 2026 Zodiac Labs GmbH
// Zodiac Roles Modifier v3
// Converts to LGPL-3.0-or-later on 2030-03-01
pragma solidity >=0.8.21 <0.9.0;
import "./EIP712Encoder.sol";
import "../interfaces/SafeStorage.sol";
/**
* @title SignTypedMessageLib - Marks (typed) messages as signed in SafeStorage
*
* @dev Execution context:
* Signing calls must be executed via `delegatecall`, reverting with
* `OnlyDelegateCall` otherwise. Callers are responsible for executing
* in a Safe context.
*/
contract SignTypedMessageLib is SafeStorage {
/// @dev Deployment address of the contract.
address public immutable deployedAt;
/// @dev Emitted with the SafeMessage hash stored as the approval key.
event SignMsg(bytes32 indexed msgHash);
/// @dev The EIP-712 type hash of Safe's message wrapper.
bytes32 private constant safeMessageTypeHash =
keccak256("SafeMessage(bytes message)");
/// @dev The library was called directly, not through delegatecall.
error OnlyDelegateCall();
/// @dev An argument of a fallback call does not lie within calldata.
error InvalidCalldata();
constructor() {
deployedAt = address(this);
}
/**
* @notice Marks raw bytes as signed, matching Safe's SignMessageLib
*
* @dev Wraps `message` directly in `SafeMessage`, without first applying a
* personal-sign prefix or computing a typed-data digest.
*
* @dev Storage write:
* `signedMessages[keccak256(safeMessagePreimage(message))] = 1`
*
* @dev Verification:
* - `isValidSignature(bytes32 digest, "") == 0x1626ba7e`
* when `message = abi.encode(digest)`.
* - `isValidSignature(bytes message, "") == 0x20c13b0b`
* for arbitrary-length `message` (legacy; Safe 1.3.0 and 1.4.1 only).
*
* @param message Raw bytes to approve, typically a 32-byte digest
*/
function signMessage(bytes calldata message) external {
if (address(this) == deployedAt) revert OnlyDelegateCall();
bytes32 safeMessageHash = keccak256(safeMessagePreimage(message));
signedMessages[safeMessageHash] = 1;
emit SignMsg(safeMessageHash);
}
/**
* @notice Marks an EIP-191 (`personal_sign`) message as signed
*
* @dev Computes `digest` using the EIP-191 personal-sign prefix and decimal
* byte length, then wraps `abi.encode(digest)` in `SafeMessage`.
*
* @dev Storage write:
* `signedMessages[keccak256(safeMessagePreimage(abi.encode(digest)))] = 1`
*
* @dev Verification:
* `isValidSignature(bytes32 digest, "") == 0x1626ba7e`
* where `digest` is the EIP-191 personal-sign hash.
*
* @param message Raw message bytes before EIP-191 prefixing
*/
function personalSign(bytes calldata message) external {
if (address(this) == deployedAt) revert OnlyDelegateCall();
bytes memory wrapped = abi.encodePacked(
"\x19Ethereum Signed Message:\n",
uintToString(message.length),
message
);
bytes32 safeMessageHash = keccak256(
safeMessagePreimage(abi.encode(keccak256(wrapped)))
);
signedMessages[safeMessageHash] = 1;
emit SignMsg(safeMessageHash);
}
/**
* @notice Marks an EIP-712 typed structured message as signed
*
* @dev Computes `digest` with `EIP712Encoder`, then wraps `abi.encode(digest)`
* in `SafeMessage`. Also reachable under unrecognized selectors via
* `fallback`.
*
* @dev Storage write:
* `signedMessages[keccak256(safeMessagePreimage(abi.encode(digest)))] = 1`
*
* @dev Verification:
* `isValidSignature(bytes32 digest, "") == 0x1626ba7e`
* where `digest` is the EIP-712 typed-data hash.
*
* @param domain ABI-encoded fields of the EIP-712 domain struct
* @param message ABI-encoded fields of the EIP-712 primary message struct
* @param types Flattened EIP-712 type layout for `domain` and `message`
*/
function signTypedMessage(
bytes calldata domain,
bytes calldata message,
TypeNodeFlat[] calldata types
) public {
if (address(this) == deployedAt) revert OnlyDelegateCall();
bytes32 safeMessageHash = keccak256(
safeTypedMessagePreimage(domain, message, types)
);
signedMessages[safeMessageHash] = 1;
emit SignMsg(safeMessageHash);
}
/**
* @notice Builds the EIP-712 `SafeMessage` approval preimage
* @dev Reads the domain separator from `ISafe(address(this)).domainSeparator()`.
* @param message Bytes to wrap in `SafeMessage(bytes message)`
* @return The preimage whose `keccak256` hash is the SafeMessage approval key
*/
function safeMessagePreimage(
bytes memory message
) public view returns (bytes memory) {
return
abi.encodePacked(
bytes2(0x1901),
ISafe(address(this)).domainSeparator(),
keccak256(abi.encode(safeMessageTypeHash, keccak256(message)))
);
}
/**
* @notice Builds a SafeMessage approval preimage for EIP-712 typed data
* @dev Computes the EIP-712 `digest` with `EIP712Encoder` and wraps
* `abi.encode(digest)` using `safeMessagePreimage`.
* @param domain ABI-encoded fields of the EIP-712 domain struct
* @param message ABI-encoded fields of the EIP-712 primary message struct
* @param types Flattened EIP-712 type layout for `domain` and `message`
* @return The preimage whose `keccak256` hash is the SafeMessage approval key
*/
function safeTypedMessagePreimage(
bytes calldata domain,
bytes calldata message,
TypeNodeFlat[] calldata types
) public view returns (bytes memory) {
return
safeMessagePreimage(
abi.encode(
EIP712Encoder.hashTypedMessage(domain, message, types)
)
);
}
/**
* @notice Dispatches unrecognized selectors to `signTypedMessage`
* @dev Decodes the same arguments as `signTypedMessage`, allowing Roles
* permissions to use selector aliases for different type layouts.
* Permissions must also constrain `types` to enforce the intended layout.
* Reverts with `InvalidCalldata` if arguments lie outside calldata.
*/
fallback() external {
// selector and three head words
if (msg.data.length < 0x64) revert InvalidCalldata();
bytes calldata domain;
bytes calldata message;
TypeNodeFlat[] calldata types;
(uint256 offset, uint256 length) = _locateArgument(0x04, 1);
assembly {
domain.offset := offset
domain.length := length
}
(offset, length) = _locateArgument(0x24, 1);
assembly {
message.offset := offset
message.length := length
}
// each TypeNodeFlat takes three words
(offset, length) = _locateArgument(0x44, 0x60);
assembly {
types.offset := offset
types.length := length
}
signTypedMessage(domain, message, types);
}
/**
* @dev Locates a dynamic argument from its head word, and requires its
* length word and content to lie within calldata.
*/
function _locateArgument(
uint256 head,
uint256 itemSize
) private pure returns (uint256 offset, uint256 length) {
uint256 pointer;
assembly {
pointer := calldataload(head)
}
// selector, pointer, length word
if (pointer > msg.data.length - 0x24) revert InvalidCalldata();
offset = pointer + 0x24;
assembly {
length := calldataload(add(pointer, 0x04))
}
if (length > (msg.data.length - offset) / itemSize) {
revert InvalidCalldata();
}
}
}
interface ISafe {
function domainSeparator() external view returns (bytes32);
}
function uintToString(uint256 value) pure returns (string memory) {
if (value == 0) {
return "0";
}
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
uint8 asciiZero = 48;
while (value != 0) {
digits--;
uint8 digit = uint8(value % 10);
buffer[digits] = bytes1(asciiZero + digit);
value /= 10;
}
return string(buffer);
}
npm/@gnosis-guild/[email protected]/contracts/core/Operation.sol
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.24;
enum Operation {
Call,
DelegateCall
}
project/contracts/evaluator/types/EvaluatorTypes.sol
// SPDX-License-Identifier: BUSL-1.1
// Copyright (c) 2026 Zodiac Labs GmbH
// Zodiac Roles Modifier v3
// Converts to LGPL-3.0-or-later on 2030-03-01
pragma solidity >=0.8.17 <0.9.0;
import {Operation} from "@gnosis-guild/zodiac-core/contracts/core/Operation.sol";
enum Encoding {
None,
Static,
Dynamic,
Tuple,
Array,
AbiEncoded,
/* EtherValue is a nonstructural, and an alias for None */
EtherValue
}
enum Operator {
// 00: EMPTY EXPRESSION (default, always passes)
// paramType: Static / Dynamic / Tuple / Array / EtherValue
// ❓ children (only for paramType: Tuple / Array to describe their structure)
// 🚫 compValue
/* 00: */ Pass,
// ------------------------------------------------------------
// 01-03: LOGICAL EXPRESSIONS
// paramType: None
// ✅ children
// 🚫 compValue
/* 01: */ And,
/* 02: */ Or,
/* 03: */ _Placeholder03,
// ------------------------------------------------------------
// 04: EMPTY CHECK (passes if data.length == 0)
// paramType: None
// 🚫 children
// 🚫 compValue
/* 04: */ Empty,
// ------------------------------------------------------------
// 05-12: COMPLEX EXPRESSIONS
// paramType: AbiEncoded / Tuple / Array,
// ✅ children
// 🚫 compValue (exception AbiEncoded.Matches uses compValue to define leading bytes)
/* 05: */ Matches,
/* 06: */ ArraySome,
/* 07: */ ArrayEvery,
/* 08: */ ArrayTailMatches,
/* 09: */ _Placeholder09,
/* 10: */ ZipSome, // paramType: None, compValue: at least 1 byte, one per plucked array
/* 11: */ ZipEvery, // paramType: None, compValue: at least 1 byte, one per plucked array
/* 12: */ _Placeholder12,
// ------------------------------------------------------------
// 13-14: EXTRACTION EXPRESSIONS
// ❓ children (at most one child, must resolve to Static)
// ✅ compValue
/* 13: */ Slice, // paramType: Static / Dynamic, compValue: 3 bytes (2 bytes shift + 1 byte size, 1-32)
/* 14: */ Pluck, // paramType: Static / EtherValue / Array, compValue: 1 byte (index into pluckedValues, 0-254)
// ------------------------------------------------------------
// 15: SPECIAL COMPARISON (without compValue)
// paramType: Static
// 🚫 children
// 🚫 compValue
/* 15: */ EqualToAvatar,
// ------------------------------------------------------------
// 16-31: COMPARISON EXPRESSIONS
// paramType: Static / Dynamic / Tuple / Array / EtherValue
// ❓ children (only for paramType: Tuple / Array to describe their structure)
// ✅ compValue
/* 16: */ EqualTo, // paramType: Static / Dynamic / Tuple / Array / EtherValue
/* 17: */ GreaterThan, // paramType: Static / EtherValue
/* 18: */ LessThan, // paramType: Static / EtherValue
/* 19: */ SignedIntGreaterThan, // paramType: Static / EtherValue
/* 20: */ SignedIntLessThan, // paramType: Static / EtherValue
/* 21: */ Bitmask, // paramType: Static / Dynamic
/* 22: */ Custom, // paramType: None / Static / Dynamic / Tuple / Array / EtherValue
/* 23: */ WithinRatio, // paramType: None
/* 24: */ _Placeholder24,
/* 25: */ _Placeholder25,
/* 26: */ _Placeholder26,
/* 27: */ _Placeholder27,
/* 28: */ WithinAllowance, // paramType: Static / EtherValue
/* 29: */ _Placeholder29,
/* 30: */ CallWithinAllowance, // paramType: None
/* 31: */ _Placeholder31
}
enum Status {
Ok,
/// Or condition not met
OrViolation,
/// Parameter value is not equal to allowed
ParameterNotAllowed,
/// Parameter value less than allowed
ParameterLessThanAllowed,
/// Parameter value greater than maximum allowed by role
ParameterGreaterThanAllowed,
/// Parameter value does not match
ParameterNotAMatch,
/// Array elements do not meet allowed criteria for every element
NotEveryArrayElementPasses,
/// Array elements do not meet allowed criteria for at least one element
NoArrayElementPasses,
/// Bitmask exceeded value length
BitmaskOverflow,
/// Bitmask not an allowed value
BitmaskNotAllowed,
CustomConditionViolation,
CustomConditionNotAContract,
CustomConditionReverted,
CustomConditionInvalidResult,
PricingAdapterNotAContract,
PricingAdapterReverted,
PricingAdapterInvalidResult,
PricingAdapterZeroPrice,
AllowanceExceeded,
/// No longer emitted: full-precision comparison reports AllowanceExceeded
/// instead. Kept to preserve status codes.
AllowanceValueOverflow,
CalldataOverflow,
RatioBelowMin,
RatioAboveMax,
/// Calldata is not empty when it should be
CalldataNotEmpty,
/// Leading bytes do not match expected value
LeadingBytesNotAMatch,
/// Zipped arrays have different lengths
ZippedArrayLengthMismatch,
/// No zipped element pair passes
NoZippedElementPasses,
/// Not every zipped element pair passes
NotEveryZippedElementPasses
}
struct Condition {
/// @dev BFS index from unpacking - identifies this node in the flattened tree
uint256 index;
Encoding encoding;
Operator operator;
bytes compValue;
Condition[] children;
/* meta fields */
uint256 inlineSize;
uint256 leadingBytes;
}
/// @dev Packed calldata slice (`offset` in low 128 bits, `length` in high 128).
/// Construct via `AbiLocation.toSpan`; `Span.wrap(0)` is an empty slice.
type Span is uint256;
struct Context {
address core;
address to;
uint256 value;
Span data;
Operation operation;
bytes32[] pluckedValues;
Span[] pluckedLocations;
}
project/contracts/periphery/interfaces/SafeStorage.sol
// SPDX-License-Identifier: BUSL-1.1
// Copyright (c) 2026 Zodiac Labs GmbH
// Zodiac Roles Modifier v3
// Converts to LGPL-3.0-or-later on 2030-03-01
pragma solidity >=0.8.21 <0.9.0;
/**
* @title SafeStorage - Storage layout of the Safe contracts
*/
contract SafeStorage {
// From /common/Singleton.sol
address internal singleton;
// From /common/ModuleManager.sol
mapping(address => address) internal modules;
// From /common/OwnerManager.sol
mapping(address => address) internal owners;
uint256 internal ownerCount;
uint256 internal threshold;
// From /Safe.sol
uint256 internal nonce;
bytes32 internal _deprecatedDomainSeparator;
mapping(bytes32 => uint256) internal signedMessages;
mapping(address => mapping(bytes32 => uint256)) internal approvedHashes;
}
project/contracts/periphery/signing/EIP712Encoder.sol
// SPDX-License-Identifier: BUSL-1.1
// Copyright (c) 2026 Zodiac Labs GmbH
// Zodiac Roles Modifier v3
// Converts to LGPL-3.0-or-later on 2030-03-01
pragma solidity >=0.8.21 <0.9.0;
import {Encoding} from "../../evaluator/types/EvaluatorTypes.sol";
/**
* @dev Flat node in a parent-ordered EIP-712 type tree:
* - `parent`: parent index in `types`
* - `encoding`: `Static`, `Dynamic`, `Tuple` or `Array`
* - `typeHash`: EIP-712 `typeHash` for structs, `bytes32(0)` otherwise
*/
struct TypeNodeFlat {
uint256 parent;
Encoding encoding;
bytes32 typeHash;
}
/// @dev Materialized node of a `TypeNodeFlat[]` tree.
struct TypeNode {
Encoding encoding;
TypeNode[] children;
bytes32 typeHash;
}
/**
* @title EIP712Encoder
* @notice Hashes EIP-712 structured data directly over ABI-encoded calldata.
*/
library EIP712Encoder {
error InvalidEIP712Encoding();
/**
* @dev Computes the hash of an EIP-712 message:
* `keccak256("\x19\x01" || domainSeparator || messageHash)`.
* @param domain ABI-encoded `EIP712Domain` tuple body.
* @param message ABI-encoded primary message tuple body.
* @param types Flattened type tree (`types[0]` = domain, `types[1]` = message).
*/
function hashTypedMessage(
bytes calldata domain,
bytes calldata message,
TypeNodeFlat[] calldata types
) internal pure returns (bytes32 result) {
bytes32 domainSeparator = _hashStruct(domain, types, 0);
bytes32 messageHash = _hashStruct(message, types, 1);
assembly {
let ptr := mload(0x40)
mstore(ptr, hex"1901")
mstore(add(ptr, 0x02), domainSeparator)
mstore(add(ptr, 0x22), messageHash)
result := keccak256(ptr, 0x42)
}
}
/**
* @dev Computes the EIP-712 domain separator `hashStruct(EIP712Domain)`.
* @param data ABI-encoded `EIP712Domain` tuple body.
* @param types Flattened type tree (`types[0]` = domain).
*/
function hashTypedDomain(
bytes calldata data,
TypeNodeFlat[] calldata types
) internal pure returns (bytes32) {
return _hashStruct(data, types, 0);
}
/**
* @dev Hashes the root struct at `types[index]` over `data` from offset 0.
* Requires `types[index]` to be a struct (`Tuple` with non-zero `typeHash`).
*/
function _hashStruct(
bytes calldata data,
TypeNodeFlat[] calldata types,
uint256 index
) private pure returns (bytes32) {
if (index >= types.length) {
revert InvalidEIP712Encoding();
}
TypeNode memory root = _toTree(types, index);
if (root.encoding != Encoding.Tuple || root.typeHash == bytes32(0)) {
revert InvalidEIP712Encoding();
}
return _hashField(data, root, 0);
}
/**
* @dev Encodes and hashes the value of `node` at byte offset `location` in `data`.
* `location` points to the 32-byte word (`Static`), length word (`Dynamic`),
* count word (`Array`), or head block (`Tuple`).
*/
function _hashField(
bytes calldata data,
TypeNode memory node,
uint256 location
) private pure returns (bytes32) {
Encoding encoding = node.encoding;
if (encoding == Encoding.Static) {
return _word(data, location);
}
if (encoding == Encoding.Dynamic) {
uint256 left = location + 32;
uint256 right = left + uint256(_word(data, location));
if (right > data.length) {
revert InvalidEIP712Encoding();
}
return keccak256(data[left:right]);
}
if (encoding == Encoding.Array) {
// arrays hash as the concatenation of element encodings,
// so a non-zero typeHash would yield a non-standard digest
if (node.typeHash != bytes32(0)) {
revert InvalidEIP712Encoding();
}
return
_hashBlock(
data,
node,
location + 32,
uint256(_word(data, location))
);
}
if (encoding == Encoding.Tuple) {
return _hashBlock(data, node, location, node.children.length);
}
revert InvalidEIP712Encoding();
}
/**
* @dev Hashes a head/tail ABI block starting at `blockLocation`.
* Dynamic child offsets are resolved relative to `blockLocation`.
* For `Array`, `node.children[0]` is repeated `childCount` times;
* for structs (`node.typeHash != 0`), `typeHash` is prepended.
*/
function _hashBlock(
bytes calldata data,
TypeNode memory node,
uint256 blockLocation,
uint256 childCount
) private pure returns (bytes32) {
bool isArray = node.encoding == Encoding.Array;
if (isArray && node.children.length != 1) {
revert InvalidEIP712Encoding();
}
bytes32[] memory result = new bytes32[](childCount);
uint256 headOffset;
for (uint256 i = 0; i < childCount; ++i) {
TypeNode memory child = node.children[isArray ? 0 : i];
(uint256 childLocation, uint256 childInlineSize) = _childLocation(
data,
blockLocation,
headOffset,
child
);
result[i] = _hashField(data, child, childLocation);
headOffset += childInlineSize > 0 ? childInlineSize : 32;
}
return
keccak256(
node.typeHash != bytes32(0)
? abi.encodePacked(node.typeHash, result)
: abi.encodePacked(result)
);
}
/**
* @dev Resolves a child's byte offset in `data` and its inline width (`0` if
* referenced via a 32-byte head offset relative to `blockLocation`).
*/
function _childLocation(
bytes calldata data,
uint256 blockLocation,
uint256 headOffset,
TypeNode memory child
) private pure returns (uint256 childLocation, uint256 childInlineSize) {
uint256 headLocation = blockLocation + headOffset;
childInlineSize = _inlineSize(child);
if (childInlineSize > 0) {
return (headLocation, childInlineSize);
}
// The offset must point past its own head slot
uint256 tailOffset = uint256(_word(data, headLocation));
if (tailOffset < headOffset + 32) {
revert InvalidEIP712Encoding();
}
return (blockLocation + tailOffset, 0);
}
/**
* @dev Returns a child node's inline byte width in its parent head, or `0`
* if it is dynamic. Empty nested tuples are rejected so `0` remains
* an unambiguous sentinel.
*/
function _inlineSize(
TypeNode memory node
) private pure returns (uint256 inlineSize) {
Encoding encoding = node.encoding;
if (encoding == Encoding.Dynamic || encoding == Encoding.Array) {
return 0;
}
if (encoding == Encoding.Static) {
return 32;
}
if (encoding != Encoding.Tuple || node.children.length == 0) {
revert InvalidEIP712Encoding();
}
for (uint256 i = 0; i < node.children.length; ++i) {
uint256 childInlineSize = _inlineSize(node.children[i]);
if (childInlineSize == 0) {
return 0;
}
inlineSize += childInlineSize;
}
}
/**
* @dev Materializes the subtree rooted at `types[index]`.
* Children must appear after their parent (`i > index`)
*/
function _toTree(
TypeNodeFlat[] calldata types,
uint256 index
) private pure returns (TypeNode memory node) {
node.encoding = types[index].encoding;
node.typeHash = types[index].typeHash;
// Count children (nodes where parent == index)
uint256 childCount = 0;
uint256 len = types.length;
for (uint256 i = index + 1; i < len; ++i) {
if (types[i].parent == index) {
++childCount;
}
}
if (childCount > 0) {
node.children = new TypeNode[](childCount);
uint256 childIndex = 0;
for (uint256 i = index + 1; childIndex < childCount; ++i) {
if (types[i].parent == index) {
node.children[childIndex++] = _toTree(types, i);
}
}
}
}
/// @dev Loads the 32-byte word at `location` in `data`
function _word(
bytes calldata data,
uint256 location
) private pure returns (bytes32 result) {
if (location + 32 > data.length) {
revert InvalidEIP712Encoding();
}
assembly {
result := calldataload(add(data.offset, location))
}
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 10,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"project/:@gnosis-guild/zodiac-core/=npm/@gnosis-guild/[email protected]/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.