{
    "Function": "executeRecovery",
    "File": "src/RecoverySpell.sol",
    "Parent Contracts": [
        "lib/openzeppelin-contracts/contracts/utils/cryptography/EIP712.sol",
        "lib/openzeppelin-contracts/contracts/interfaces/IERC5267.sol"
    ],
    "High-Level Calls": [
        "ECDSA",
        "Safe",
        "Safe"
    ],
    "Internal Calls": [
        "require(bool,string)",
        "getDigest",
        "abi.encodeWithSelector()",
        "abi.encodeWithSelector()",
        "require(bool,string)",
        "require(bool,string)",
        "tload(uint256)",
        "tstore(uint256,uint256)",
        "abi.encodeWithSelector()",
        "abi.encodeWithSelector()",
        "abi.encodeWithSelector()",
        "require(bool,string)",
        "require(bool,string)",
        "abi.encodeWithSelector()",
        "require(bool,string)",
        "tstore(uint256,uint256)"
    ],
    "Library Calls": [
        "recover"
    ],
    "Low-Level Calls": [],
    "Code": "function executeRecovery(\n        address previousModule,\n        uint8[] calldata v,\n        bytes32[] calldata r,\n        bytes32[] calldata s\n    ) external {\n        /// checks\n        require(\n            recoveryInitiated != type(uint256).max,\n            \"RecoverySpell: Already recovered\"\n        );\n\n        /// fails if recovery already executed due to math overflow\n        /// even if delay is 0, uint256.max + 1 will always revert\n        /// recovery initiated will always be lte block timestamp before the recovery is executed\n        require(\n            block.timestamp > recoveryInitiated + delay,\n            \"RecoverySpell: Recovery not ready\"\n        );\n        require(\n            v.length == r.length && r.length == s.length,\n            \"RecoverySpell: Invalid signature parameters\"\n        );\n        /// if there are not enough signers, even if all signatures are\n        /// valid and not duplicated, there is no possibility of this being\n        /// enough to execute the recovery.\n        require(\n            recoveryThreshold <= v.length,\n            \"RecoverySpell: Not enough signatures\"\n        );\n\n        for (uint256 i = 0; i < owners.length; i++) {\n            address owner = owners[i];\n            assembly (\"memory-safe\") {\n                tstore(owner, 1)\n            }\n        }\n\n        /// duplication and validity checks\n        /// ensure the signatures are\n        /// 1. valid signatures\n        /// 2. unique signers\n        /// 3. recovery owners about to be added to the safe\n\n        /// check if an address that provided a signature is an owner\n        /// in storage, then remove that address from used addresses\n        /// to prevent the same owner passing multiple signatures.\n\n        bytes32 digest = getDigest();\n        for (uint256 i = 0; i < v.length; i++) {\n            address recoveredAddress = ECDSA.recover(digest, v[i], r[i], s[i]);\n            bool valid;\n\n            assembly (\"memory-safe\") {\n                valid := tload(recoveredAddress)\n                if eq(valid, 1) { tstore(recoveredAddress, 0) }\n            }\n\n            /// if the address of the signer was not in storage, the value will\n            /// be 0 and the require will fail.\n            /// if the address of the signer duplicated signatures, the value\n            /// will be 0 on the second retrieval and the require will fail.\n            require(\n                valid && recoveredAddress != address(0),\n                \"RecoverySpell: Invalid signature\"\n            );\n        }\n\n        /// @notice execute the recovery process, can only be called\n        /// after the recovery delay has passed. Callable by any address\n\n        /// @param previousModule the address of the previous module\n        /// if the previous module is incorrect, this function will fail\n        ///\n        /// this function executes actions in the following order:\n        ///   1). remove all but final existing owner, set owner threshold to 1\n        ///   2). swap final existing owner for the first new owner\n        ///   3). add the remaining new owners to the safe, with the\n        ///   last owner being added updating the threshold to the new value\n        ///   4). remove the recovery module from the safe\n        address[] memory existingOwners = safe.getOwners();\n        uint256 existingOwnersLength = existingOwners.length;\n\n        /// + 1 is for the module removal\n        /// new owner length = 1\n        /// existing owner length = 1\n        IMulticall3.Call3[] memory calls3 =\n            new IMulticall3.Call3[](owners.length + existingOwnersLength + 1);\n\n        uint256 index = 0;\n\n        /// build interactions\n\n        /// remove all existing owners except the last one\n        for (uint256 i = 0; i < existingOwnersLength - 1; i++) {\n            calls3[index++].callData = abi.encodeWithSelector(\n                OwnerManager.removeOwner.selector,\n                SENTINEL,\n                existingOwners[i],\n                1\n            );\n        }\n\n        calls3[index++].callData = abi.encodeWithSelector(\n            OwnerManager.swapOwner.selector,\n            SENTINEL,\n            existingOwners[existingOwnersLength - 1],\n            owners[0]\n        );\n\n        /// only cover indexes 1 through new owners length\n        for (uint256 i = 1; i < owners.length; i++) {\n            calls3[index++].callData = abi.encodeWithSelector(\n                OwnerManager.addOwnerWithThreshold.selector, owners[i], 1\n            );\n        }\n\n        /// add new owner with the updated threshold\n        calls3[index++].callData = abi.encodeWithSelector(\n            OwnerManager.changeThreshold.selector, threshold\n        );\n\n        calls3[index].callData = abi.encodeWithSelector(\n            ModuleManager.disableModule.selector, previousModule, address(this)\n        );\n\n        for (uint256 i = 0; i < calls3.length; i++) {\n            calls3[i].allowFailure = false;\n            calls3[i].target = address(safe);\n        }\n\n        /// effects\n        /// now impossible to call initiate recovery as owners array is empty\n        delete owners;\n\n        /// array length is set to 0 impossible for executeRecovery to be\n        /// callable again as the require check will always revert\n        recoveryInitiated = type(uint256).max;\n\n        /// interactions\n\n        require(\n            safe.execTransactionFromModule(\n                MULTICALL3,\n                0,\n                abi.encodeWithSelector(IMulticall3.aggregate3.selector, calls3),\n                Enum.Operation.DelegateCall\n            ),\n            \"RecoverySpell: Recovery failed\"\n        );\n\n        emit SafeRecovered(block.timestamp);\n    }"
}