|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.27; |
| 4 | + |
| 5 | +import {AbstractSigner} from "./AbstractSigner.sol"; |
| 6 | +import {ERC7913Utils} from "./ERC7913Utils.sol"; |
| 7 | +import {EnumerableSetExtended} from "../structs/EnumerableSetExtended.sol"; |
| 8 | +import {Calldata} from "../../utils/Calldata.sol"; |
| 9 | +import {SafeCast} from "../../utils/math/SafeCast.sol"; |
| 10 | + |
| 11 | +/** |
| 12 | + * @dev Implementation of {AbstractSigner} using multiple ERC-7913 signers with a threshold-based |
| 13 | + * signature verification system. |
| 14 | + * |
| 15 | + * This contract allows managing a set of authorized signers and requires a minimum number of |
| 16 | + * signatures (threshold) to approve operations. It uses ERC-7913 formatted signers, which |
| 17 | + * concatenate a verifier address and a key: `verifier || key`. |
| 18 | + * |
| 19 | + * Example of usage: |
| 20 | + * |
| 21 | + * ```solidity |
| 22 | + * contract MyMultiSignerAccount is Account, MultiSignerERC7913, Initializable { |
| 23 | + * constructor() EIP712("MyMultiSignerAccount", "1") {} |
| 24 | + * |
| 25 | + * function initialize(bytes[] memory signers, uint256 threshold) public initializer { |
| 26 | + * _addSigners(signers); |
| 27 | + * _setThreshold(threshold); |
| 28 | + * } |
| 29 | + * |
| 30 | + * function addSigners(bytes[] memory signers) public onlyEntryPointOrSelf { |
| 31 | + * _addSigners(signers); |
| 32 | + * } |
| 33 | + * |
| 34 | + * function removeSigners(bytes[] memory signers) public onlyEntryPointOrSelf { |
| 35 | + * _removeSigners(signers); |
| 36 | + * } |
| 37 | + * |
| 38 | + * function setThreshold(uint256 threshold) public onlyEntryPointOrSelf { |
| 39 | + * _setThreshold(threshold); |
| 40 | + * } |
| 41 | + * } |
| 42 | + * ``` |
| 43 | + * |
| 44 | + * IMPORTANT: Failing to properly initialize the signers and threshold either during construction |
| 45 | + * (if used standalone) or during initialization (if used as a clone) may leave the contract |
| 46 | + * either front-runnable or unusable. |
| 47 | + */ |
| 48 | +abstract contract MultiSignerERC7913 is AbstractSigner { |
| 49 | + using EnumerableSetExtended for EnumerableSetExtended.BytesSet; |
| 50 | + using ERC7913Utils for *; |
| 51 | + using SafeCast for uint256; |
| 52 | + |
| 53 | + EnumerableSetExtended.BytesSet private _signersSet; |
| 54 | + uint128 private _threshold; |
| 55 | + |
| 56 | + /// @dev Emitted when signers are added. |
| 57 | + event ERC7913SignersAdded(bytes[] indexed signers); |
| 58 | + |
| 59 | + /// @dev Emitted when signers are removed. |
| 60 | + event ERC7913SignersRemoved(bytes[] indexed signers); |
| 61 | + |
| 62 | + /// @dev Emitted when the threshold is updated. |
| 63 | + event ERC7913ThresholdSet(uint256 threshold); |
| 64 | + |
| 65 | + /// @dev The `signer` already exists. |
| 66 | + error MultiSignerERC7913AlreadyExists(bytes signer); |
| 67 | + |
| 68 | + /// @dev The `signer` does not exist. |
| 69 | + error MultiSignerERC7913NonexistentSigner(bytes signer); |
| 70 | + |
| 71 | + /// @dev The `signer` is less than 20 bytes long. |
| 72 | + error MultiSignerERC7913InvalidSigner(bytes signer); |
| 73 | + |
| 74 | + /// @dev The `threshold` is unreachable given the number of `signers`. |
| 75 | + error MultiSignerERC7913UnreachableThreshold(uint256 signers, uint256 threshold); |
| 76 | + |
| 77 | + /** |
| 78 | + * @dev Returns the set of authorized signers. Prefer {_signers} for internal use. |
| 79 | + * |
| 80 | + * WARNING: This operation copies the entire signers set to memory, which can be expensive. This is designed |
| 81 | + * for view accessors queried without gas fees. Using it in state-changing functions may become uncallable |
| 82 | + * if the signers set grows too large. |
| 83 | + */ |
| 84 | + function signers() public view virtual returns (bytes[] memory) { |
| 85 | + return _signers().values(); |
| 86 | + } |
| 87 | + |
| 88 | + /// @dev Returns whether the `signer` is an authorized signer. |
| 89 | + function isSigner(bytes memory signer) public view virtual returns (bool) { |
| 90 | + return _signers().contains(signer); |
| 91 | + } |
| 92 | + |
| 93 | + /// @dev Returns the minimum number of signers required to approve a multisignature operation. |
| 94 | + function threshold() public view virtual returns (uint256) { |
| 95 | + return _threshold; |
| 96 | + } |
| 97 | + |
| 98 | + /// @dev Returns the set of authorized signers. |
| 99 | + function _signers() internal view virtual returns (EnumerableSetExtended.BytesSet storage) { |
| 100 | + return _signersSet; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @dev Adds the `newSigners` to those allowed to sign on behalf of this contract. |
| 105 | + * Internal version without access control. |
| 106 | + * |
| 107 | + * Requirements: |
| 108 | + * |
| 109 | + * * Each of `newSigners` must be at least 20 bytes long. Reverts with {MultiSignerERC7913InvalidSigner} if not. |
| 110 | + * * Each of `newSigners` must not be authorized. See {isSigner}. Reverts with {MultiSignerERC7913AlreadyExists} if so. |
| 111 | + */ |
| 112 | + function _addSigners(bytes[] memory newSigners) internal virtual { |
| 113 | + uint256 newSignersLength = newSigners.length; |
| 114 | + for (uint256 i = 0; i < newSignersLength; i++) { |
| 115 | + bytes memory signer = newSigners[i]; |
| 116 | + require(signer.length >= 20, MultiSignerERC7913InvalidSigner(signer)); |
| 117 | + require(_signers().add(signer), MultiSignerERC7913AlreadyExists(signer)); |
| 118 | + } |
| 119 | + emit ERC7913SignersAdded(newSigners); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @dev Removes the `oldSigners` from the authorized signers. Internal version without access control. |
| 124 | + * |
| 125 | + * Requirements: |
| 126 | + * |
| 127 | + * * Each of `oldSigners` must be authorized. See {isSigner}. Otherwise {MultiSignerERC7913NonexistentSigner} is thrown. |
| 128 | + * * See {_validateReachableThreshold} for the threshold validation. |
| 129 | + */ |
| 130 | + function _removeSigners(bytes[] memory oldSigners) internal virtual { |
| 131 | + uint256 oldSignersLength = oldSigners.length; |
| 132 | + for (uint256 i = 0; i < oldSignersLength; i++) { |
| 133 | + bytes memory signer = oldSigners[i]; |
| 134 | + require(_signers().remove(signer), MultiSignerERC7913NonexistentSigner(signer)); |
| 135 | + } |
| 136 | + _validateReachableThreshold(); |
| 137 | + emit ERC7913SignersRemoved(oldSigners); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @dev Sets the signatures `threshold` required to approve a multisignature operation. |
| 142 | + * Internal version without access control. |
| 143 | + * |
| 144 | + * Requirements: |
| 145 | + * |
| 146 | + * * See {_validateReachableThreshold} for the threshold validation. |
| 147 | + */ |
| 148 | + function _setThreshold(uint256 newThreshold) internal virtual { |
| 149 | + _threshold = newThreshold.toUint128(); |
| 150 | + _validateReachableThreshold(); |
| 151 | + emit ERC7913ThresholdSet(newThreshold); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @dev Validates the current threshold is reachable. |
| 156 | + * |
| 157 | + * Requirements: |
| 158 | + * |
| 159 | + * * The {signers}'s length must be `>=` to the {threshold}. Throws {MultiSignerERC7913UnreachableThreshold} if not. |
| 160 | + */ |
| 161 | + function _validateReachableThreshold() internal view virtual { |
| 162 | + uint256 totalSigners = _signers().length(); |
| 163 | + uint256 currentThreshold = threshold(); |
| 164 | + require( |
| 165 | + totalSigners >= currentThreshold, |
| 166 | + MultiSignerERC7913UnreachableThreshold(totalSigners, currentThreshold) |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @dev Decodes, validates the signature and checks the signers are authorized. |
| 172 | + * See {_validateSignatures} and {_validateThreshold} for more details. |
| 173 | + * |
| 174 | + * Example of signature encoding: |
| 175 | + * |
| 176 | + * ```solidity |
| 177 | + * // Encode signers (verifier || key) |
| 178 | + * bytes memory signer1 = abi.encodePacked(verifier1, key1); |
| 179 | + * bytes memory signer2 = abi.encodePacked(verifier2, key2); |
| 180 | + * |
| 181 | + * // Order signers by their id |
| 182 | + * if (keccak256(signer1) > keccak256(signer2)) { |
| 183 | + * (signer1, signer2) = (signer2, signer1); |
| 184 | + * (signature1, signature2) = (signature2, signature1); |
| 185 | + * } |
| 186 | + * |
| 187 | + * // Assign ordered signers and signatures |
| 188 | + * bytes[] memory signers = new bytes[](2); |
| 189 | + * bytes[] memory signatures = new bytes[](2); |
| 190 | + * signers[0] = signer1; |
| 191 | + * signatures[0] = signature1; |
| 192 | + * signers[1] = signer2; |
| 193 | + * signatures[1] = signature2; |
| 194 | + * |
| 195 | + * // Encode the multi signature |
| 196 | + * bytes memory signature = abi.encode(signers, signatures); |
| 197 | + * ``` |
| 198 | + * |
| 199 | + * Requirements: |
| 200 | + * |
| 201 | + * * The `signature` must be encoded as `abi.encode(signers, signatures)`. |
| 202 | + */ |
| 203 | + function _rawSignatureValidation( |
| 204 | + bytes32 hash, |
| 205 | + bytes calldata signature |
| 206 | + ) internal view virtual override returns (bool) { |
| 207 | + if (signature.length == 0) return false; // For ERC-7739 compatibility |
| 208 | + (bytes[] memory signingSigners, bytes[] memory signatures) = abi.decode(signature, (bytes[], bytes[])); |
| 209 | + if (signingSigners.length != signatures.length) return false; |
| 210 | + return _validateThreshold(signingSigners) && _validateSignatures(hash, signingSigners, signatures); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * @dev Validates the signatures using the signers and their corresponding signatures. |
| 215 | + * Returns whether whether the signers are authorized and the signatures are valid for the given hash. |
| 216 | + * |
| 217 | + * IMPORTANT: For simplicity, this contract assumes that the signers are ordered by their `keccak256` hash |
| 218 | + * to avoid duplication when iterating through the signers (i.e. `keccak256(signer1) < keccak256(signer2)`). |
| 219 | + * The function will return false if the signers are not ordered. |
| 220 | + * |
| 221 | + * Requirements: |
| 222 | + * |
| 223 | + * * The `signatures` arrays must be at least as large as the `signingSigners` arrays. Panics otherwise. |
| 224 | + */ |
| 225 | + function _validateSignatures( |
| 226 | + bytes32 hash, |
| 227 | + bytes[] memory signingSigners, |
| 228 | + bytes[] memory signatures |
| 229 | + ) internal view virtual returns (bool valid) { |
| 230 | + uint256 signersLength = signingSigners.length; |
| 231 | + for (uint256 i = 0; i < signersLength; i++) { |
| 232 | + if (!isSigner(signingSigners[i])) { |
| 233 | + return false; |
| 234 | + } |
| 235 | + } |
| 236 | + return hash.areValidSignaturesNow(signingSigners, signatures); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * @dev Validates that the number of signers meets the {threshold} requirement. |
| 241 | + * Assumes the signers were already validated. See {_validateSignatures} for more details. |
| 242 | + */ |
| 243 | + function _validateThreshold(bytes[] memory validatingSigners) internal view virtual returns (bool) { |
| 244 | + return validatingSigners.length >= threshold(); |
| 245 | + } |
| 246 | +} |
0 commit comments