|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause-Clear |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import "@fhevm/solidity/lib/Impl.sol"; |
| 5 | +import "@fhevm/solidity/config/ZamaConfig.sol"; |
| 6 | + |
| 7 | +interface ISafe { |
| 8 | + function getOwners() external view returns (address[] memory); |
| 9 | +} |
| 10 | + |
| 11 | +/// Helper contract to facilitate usage of fhevm with multisig accounts |
| 12 | +contract FHEVMMultiSigHelper { |
| 13 | + /// @notice Returned if the list of input handles is empty |
| 14 | + error EmptyInputHandles(); |
| 15 | + |
| 16 | + /// @notice Returned if the list of owners is empty |
| 17 | + error EmptyOwners(); |
| 18 | + |
| 19 | + /// @notice Returned if the multisig address is null |
| 20 | + error NullMultisig(); |
| 21 | + |
| 22 | + /// @notice Returned if one of the handles is null |
| 23 | + error UninitializedHandle(); |
| 24 | + |
| 25 | + constructor() { |
| 26 | + FHE.setCoprocessor(ZamaConfig.getEthereumCoprocessorConfig()); |
| 27 | + } |
| 28 | + |
| 29 | + /// @notice Helper method to allow a list of handles to the owners of a Safe multisig contract and to the Safe itself |
| 30 | + /// @param safeMultisig The Safe account address |
| 31 | + /// @param inputHandles The list of initialized handles that we want to allow to the Safe and its owners, must be non-empty |
| 32 | + /// @param inputProof The input proof corresponding to the list of inputHandles, must be non-empty |
| 33 | + /// @dev It is possible to use this method with any (initialized, non-null) handle type |
| 34 | + function allowForSafeMultiSig( |
| 35 | + address safeMultisig, |
| 36 | + bytes32[] memory inputHandles, |
| 37 | + bytes memory inputProof |
| 38 | + ) external { |
| 39 | + address[] memory owners = ISafe(safeMultisig).getOwners(); |
| 40 | + uint256 numOwners = owners.length; |
| 41 | + _allowHandlesForMultiSigAndOwners(safeMultisig, inputHandles, inputProof, owners, numOwners); |
| 42 | + } |
| 43 | + |
| 44 | + /// @notice More general helper method to allow a list of handles to a custom list of owners and a multisig, |
| 45 | + /// @notice but requires the user to manually input correct list of owners as argument |
| 46 | + /// @notice WARNING: the user is responsible to enter the correct list of owners corresponding to the multisig |
| 47 | + /// @notice Can also be used with non-Safe multisigs (eg Aragon, CoinbaseSmartWallet, etc) |
| 48 | + /// @param multisig The multisig account address (could be a non-Safe account) |
| 49 | + /// @param owners The list of owners, must be non-empty |
| 50 | + /// @param inputHandles The list of initialized handles that we want to allow to the owners, must be non-empty |
| 51 | + /// @param inputProof The input proof corresponding to the list of inputHandles, must be non-empty |
| 52 | + /// @dev It is possible to use this same function with any (initialized, non-null) handle type |
| 53 | + function allowForCustomMultiSigOwners( |
| 54 | + address multisig, |
| 55 | + address[] memory owners, |
| 56 | + bytes32[] memory inputHandles, |
| 57 | + bytes memory inputProof |
| 58 | + ) external { |
| 59 | + uint256 numOwners = owners.length; |
| 60 | + if (numOwners == 0) revert EmptyOwners(); |
| 61 | + _allowHandlesForMultiSigAndOwners(multisig, inputHandles, inputProof, owners, numOwners); |
| 62 | + } |
| 63 | + |
| 64 | + /// @notice Internal function to allow list of input handles to the multisig account and its owners |
| 65 | + /// @param multisig The multisig account address (could be a non-Safe account) |
| 66 | + /// @param inputHandles The list of initialized handles that we want to allow to the owners, must be non-empty |
| 67 | + /// @param inputProof The input proof corresponding to the list of inputHandles, must be non-empty |
| 68 | + /// @param owners The list of owners, must be non-empty |
| 69 | + /// @param numOwners The number of owners |
| 70 | + function _allowHandlesForMultiSigAndOwners( |
| 71 | + address multisig, |
| 72 | + bytes32[] memory inputHandles, |
| 73 | + bytes memory inputProof, |
| 74 | + address[] memory owners, |
| 75 | + uint256 numOwners |
| 76 | + ) internal { |
| 77 | + if (multisig == address(0)) revert NullMultisig(); |
| 78 | + uint256 inputHandlesLength = inputHandles.length; |
| 79 | + if (inputHandlesLength == 0) revert EmptyInputHandles(); |
| 80 | + for (uint256 idxHandle = 0; idxHandle < inputHandlesLength; idxHandle++) { |
| 81 | + bytes32 inputHandle = inputHandles[idxHandle]; |
| 82 | + if (inputHandle == bytes32(0)) revert UninitializedHandle(); |
| 83 | + Impl.verify(inputHandle, inputProof, FheType(uint8(inputHandle[30]))); |
| 84 | + Impl.allow(inputHandle, multisig); |
| 85 | + for (uint256 idxOwner; idxOwner < numOwners; idxOwner++) { |
| 86 | + Impl.allow(inputHandle, owners[idxOwner]); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments