|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.27; |
| 4 | + |
| 5 | +import {SafeCast} from "../../math/SafeCast.sol"; |
| 6 | +import {MultiSignerERC7913} from "./MultiSignerERC7913.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @dev Extension of {MultiSignerERC7913} that supports weighted signatures. |
| 10 | + * |
| 11 | + * This contract allows assigning different weights to each signer, enabling more |
| 12 | + * flexible governance schemes. For example, some signers could have higher weight |
| 13 | + * than others, allowing for weighted voting or prioritized authorization. |
| 14 | + * |
| 15 | + * Example of usage: |
| 16 | + * |
| 17 | + * ```solidity |
| 18 | + * contract MyWeightedMultiSignerAccount is Account, MultiSignerERC7913Weighted, Initializable { |
| 19 | + * function initialize(bytes[] memory signers, uint64[] memory weights, uint64 threshold) public initializer { |
| 20 | + * _addSigners(signers); |
| 21 | + * _setSignerWeights(signers, weights); |
| 22 | + * _setThreshold(threshold); |
| 23 | + * } |
| 24 | + * |
| 25 | + * function addSigners(bytes[] memory signers) public onlyEntryPointOrSelf { |
| 26 | + * _addSigners(signers); |
| 27 | + * } |
| 28 | + * |
| 29 | + * function removeSigners(bytes[] memory signers) public onlyEntryPointOrSelf { |
| 30 | + * _removeSigners(signers); |
| 31 | + * } |
| 32 | + * |
| 33 | + * function setThreshold(uint64 threshold) public onlyEntryPointOrSelf { |
| 34 | + * _setThreshold(threshold); |
| 35 | + * } |
| 36 | + * |
| 37 | + * function setSignerWeights(bytes[] memory signers, uint64[] memory weights) public onlyEntryPointOrSelf { |
| 38 | + * _setSignerWeights(signers, weights); |
| 39 | + * } |
| 40 | + * } |
| 41 | + * ``` |
| 42 | + * |
| 43 | + * IMPORTANT: When setting a threshold value, ensure it matches the scale used for signer weights. |
| 44 | + * For example, if signers have weights like 1, 2, or 3, then a threshold of 4 would require at |
| 45 | + * least two signers (e.g., one with weight 1 and one with weight 3). See {signerWeight}. |
| 46 | + */ |
| 47 | +abstract contract MultiSignerERC7913Weighted is MultiSignerERC7913 { |
| 48 | + using SafeCast for *; |
| 49 | + |
| 50 | + // Sum of all the extra weights of all signers. Storage packed with `MultiSignerERC7913._threshold` |
| 51 | + uint64 private _totalExtraWeight; |
| 52 | + |
| 53 | + // Mapping from signer to extraWeight (in addition to all authorized signers having weight 1) |
| 54 | + mapping(bytes signer => uint64) private _extraWeights; |
| 55 | + |
| 56 | + /** |
| 57 | + * @dev Emitted when a signer's weight is changed. |
| 58 | + * |
| 59 | + * NOTE: Not emitted in {_addSigners} or {_removeSigners}. Indexers must rely on {ERC7913SignerAdded} |
| 60 | + * and {ERC7913SignerRemoved} to index a default weight of 1. See {signerWeight}. |
| 61 | + */ |
| 62 | + event ERC7913SignerWeightChanged(bytes indexed signer, uint64 weight); |
| 63 | + |
| 64 | + /// @dev Thrown when a signer's weight is invalid. |
| 65 | + error MultiSignerERC7913WeightedInvalidWeight(bytes signer, uint64 weight); |
| 66 | + |
| 67 | + /// @dev Thrown when the arrays lengths don't match. See {_setSignerWeights}. |
| 68 | + error MultiSignerERC7913WeightedMismatchedLength(); |
| 69 | + |
| 70 | + /// @dev Gets the weight of a signer. Returns 0 if the signer is not authorized. |
| 71 | + function signerWeight(bytes memory signer) public view virtual returns (uint64) { |
| 72 | + unchecked { |
| 73 | + // Safe cast, _setSignerWeights guarantees 1+_extraWeights is a uint64 |
| 74 | + return uint64(isSigner(signer).toUint() * (1 + _extraWeights[signer])); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// @dev Gets the total weight of all signers. |
| 79 | + function totalWeight() public view virtual returns (uint64) { |
| 80 | + return (getSignerCount() + _totalExtraWeight).toUint64(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @dev Sets weights for multiple signers at once. Internal version without access control. |
| 85 | + * |
| 86 | + * Requirements: |
| 87 | + * |
| 88 | + * * `signers` and `weights` arrays must have the same length. Reverts with {MultiSignerERC7913WeightedMismatchedLength} on mismatch. |
| 89 | + * * Each signer must exist in the set of authorized signers. Otherwise reverts with {MultiSignerERC7913NonexistentSigner} |
| 90 | + * * Each weight must be greater than 0. Otherwise reverts with {MultiSignerERC7913WeightedInvalidWeight} |
| 91 | + * * See {_validateReachableThreshold} for the threshold validation. |
| 92 | + * |
| 93 | + * Emits {ERC7913SignerWeightChanged} for each signer. |
| 94 | + */ |
| 95 | + function _setSignerWeights(bytes[] memory signers, uint64[] memory weights) internal virtual { |
| 96 | + require(signers.length == weights.length, MultiSignerERC7913WeightedMismatchedLength()); |
| 97 | + |
| 98 | + uint256 extraWeightAdded = 0; |
| 99 | + uint256 extraWeightRemoved = 0; |
| 100 | + for (uint256 i = 0; i < signers.length; ++i) { |
| 101 | + bytes memory signer = signers[i]; |
| 102 | + uint64 weight = weights[i]; |
| 103 | + |
| 104 | + require(isSigner(signer), MultiSignerERC7913NonexistentSigner(signer)); |
| 105 | + require(weight > 0, MultiSignerERC7913WeightedInvalidWeight(signer, weight)); |
| 106 | + |
| 107 | + unchecked { |
| 108 | + // Overflow impossible: weight values are bounded by uint64 and economic constraints |
| 109 | + extraWeightRemoved += _extraWeights[signer]; |
| 110 | + extraWeightAdded += _extraWeights[signer] = weight - 1; |
| 111 | + } |
| 112 | + |
| 113 | + emit ERC7913SignerWeightChanged(signer, weight); |
| 114 | + } |
| 115 | + unchecked { |
| 116 | + // Safe from underflow: `extraWeightRemoved` is bounded by `_totalExtraWeight` by construction |
| 117 | + // and weight values are bounded by uint64 and economic constraints |
| 118 | + _totalExtraWeight = (uint256(_totalExtraWeight) + extraWeightAdded - extraWeightRemoved).toUint64(); |
| 119 | + } |
| 120 | + _validateReachableThreshold(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @dev See {MultiSignerERC7913-_removeSigners}. |
| 125 | + * |
| 126 | + * Just like {_addSigners}, this function does not emit {ERC7913SignerWeightChanged} events. The |
| 127 | + * {ERC7913SignerRemoved} event emitted by {MultiSignerERC7913-_removeSigners} is enough to track weights here. |
| 128 | + */ |
| 129 | + function _removeSigners(bytes[] memory signers) internal virtual override { |
| 130 | + // Clean up weights for removed signers |
| 131 | + // |
| 132 | + // The `extraWeightRemoved` is bounded by `_totalExtraWeight`. The `super._removeSigners` function will revert |
| 133 | + // if the signers array contains any duplicates, ensuring each signer's weight is only counted once. Since |
| 134 | + // `_totalExtraWeight` is stored as a `uint64`, the final subtraction operation is also safe. |
| 135 | + unchecked { |
| 136 | + uint64 extraWeightRemoved = 0; |
| 137 | + for (uint256 i = 0; i < signers.length; ++i) { |
| 138 | + bytes memory signer = signers[i]; |
| 139 | + |
| 140 | + extraWeightRemoved += _extraWeights[signer]; |
| 141 | + delete _extraWeights[signer]; |
| 142 | + } |
| 143 | + _totalExtraWeight -= extraWeightRemoved; |
| 144 | + } |
| 145 | + super._removeSigners(signers); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @dev Sets the threshold for the multisignature operation. Internal version without access control. |
| 150 | + * |
| 151 | + * Requirements: |
| 152 | + * |
| 153 | + * * The {totalWeight} must be `>=` the {threshold}. Otherwise reverts with {MultiSignerERC7913UnreachableThreshold} |
| 154 | + * |
| 155 | + * NOTE: This function intentionally does not call `super._validateReachableThreshold` because the base implementation |
| 156 | + * assumes each signer has a weight of 1, which is a subset of this weighted implementation. Consider that multiple |
| 157 | + * implementations of this function may exist in the contract, so important side effects may be missed |
| 158 | + * depending on the linearization order. |
| 159 | + */ |
| 160 | + function _validateReachableThreshold() internal view virtual override { |
| 161 | + uint64 weight = totalWeight(); |
| 162 | + uint64 currentThreshold = threshold(); |
| 163 | + require(weight >= currentThreshold, MultiSignerERC7913UnreachableThreshold(weight, currentThreshold)); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @dev Validates that the total weight of signers meets the threshold requirement. |
| 168 | + * |
| 169 | + * NOTE: This function intentionally does not call `super._validateThreshold` because the base implementation |
| 170 | + * assumes each signer has a weight of 1, which is a subset of this weighted implementation. Consider that multiple |
| 171 | + * implementations of this function may exist in the contract, so important side effects may be missed |
| 172 | + * depending on the linearization order. |
| 173 | + */ |
| 174 | + function _validateThreshold(bytes[] memory signers) internal view virtual override returns (bool) { |
| 175 | + unchecked { |
| 176 | + uint64 weight = 0; |
| 177 | + for (uint256 i = 0; i < signers.length; ++i) { |
| 178 | + // Overflow impossible: weight values are bounded by uint64 and economic constraints |
| 179 | + weight += signerWeight(signers[i]); |
| 180 | + } |
| 181 | + return weight >= threshold(); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments