2024-08-12 00:17:40 +03:00

172 lines
5.4 KiB
Solidity

// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.3;
import "./IAuthoriser.sol";
import "../../wallet/IWallet.sol";
import "../../infrastructure/IModuleRegistry.sol";
import "../../infrastructure/storage/IGuardianStorage.sol";
import "../../infrastructure/storage/ITransferStorage.sol";
import "./IModule.sol";
import "../../../lib_0.5/other/ERC20.sol";
abstract contract BaseModule is IModule {
// Empty calldata
bytes constant internal EMPTY_BYTES = "";
// Mock token address for ETH
address constant internal ETH_TOKEN = address(0);
// The module registry
IModuleRegistry internal immutable registry;
// The guardians storage
IGuardianStorage internal immutable guardianStorage;
// The trusted contacts storage
ITransferStorage internal immutable userWhitelist;
// The authoriser
IAuthoriser internal immutable authoriser;
event ModuleCreated(bytes32 name);
enum OwnerSignature {
Anyone, // Anyone
Required, // Owner required
Optional, // Owner and/or guardians
Disallowed, // Guardians only
Session // Session only
}
struct Session {
address key;
uint64 expires;
}
// Maps wallet to session
mapping (address => Session) internal sessions;
struct Lock {
// the lock's release timestamp
uint64 release;
// the signature of the method that set the last lock
bytes4 locker;
}
// Wallet specific lock storage
mapping (address => Lock) internal locks;
/**
* @notice Throws if the wallet is not locked.
*/
modifier onlyWhenLocked(address _wallet) {
require(_isLocked(_wallet), "BM: wallet must be locked");
_;
}
/**
* @notice Throws if the wallet is locked.
*/
modifier onlyWhenUnlocked(address _wallet) {
require(!_isLocked(_wallet), "BM: wallet locked");
_;
}
/**
* @notice Throws if the sender is not the module itself.
*/
modifier onlySelf() {
require(_isSelf(msg.sender), "BM: must be module");
_;
}
/**
* @notice Throws if the sender is not the module itself or the owner of the target wallet.
*/
modifier onlyWalletOwnerOrSelf(address _wallet) {
require(_isSelf(msg.sender) || _isOwner(_wallet, msg.sender), "BM: must be wallet owner/self");
_;
}
/**
* @dev Throws if the sender is not the target wallet of the call.
*/
modifier onlyWallet(address _wallet) {
require(msg.sender == _wallet, "BM: caller must be wallet");
_;
}
constructor(
IModuleRegistry _registry,
IGuardianStorage _guardianStorage,
ITransferStorage _userWhitelist,
IAuthoriser _authoriser,
bytes32 _name
) {
registry = _registry;
guardianStorage = _guardianStorage;
userWhitelist = _userWhitelist;
authoriser = _authoriser;
emit ModuleCreated(_name);
}
/**
* @notice Moves tokens that have been sent to the module by mistake.
* @param _token The target token.
*/
function recoverToken(address _token) external {
uint total = ERC20(_token).balanceOf(address(this));
ERC20(_token).transfer(address(registry), total);
}
function _clearSession(address _wallet) internal {
delete sessions[_wallet];
}
/**
* @notice Helper method to check if an address is the owner of a target wallet.
* @param _wallet The target wallet.
* @param _addr The address.
*/
function _isOwner(address _wallet, address _addr) internal view returns (bool) {
return IWallet(_wallet).owner() == _addr;
}
/**
* @notice Helper method to check if a wallet is locked.
* @param _wallet The target wallet.
*/
function _isLocked(address _wallet) internal view returns (bool) {
return locks[_wallet].release > uint64(block.timestamp);
}
/**
* @notice Helper method to check if an address is the module itself.
* @param _addr The target address.
*/
function _isSelf(address _addr) internal view returns (bool) {
return _addr == address(this);
}
/**
* @notice Helper method to invoke a wallet.
* @param _wallet The target wallet.
* @param _to The target address for the transaction.
* @param _value The value of the transaction.
* @param _data The data of the transaction.
*/
function invokeWallet(address _wallet, address _to, uint256 _value, bytes memory _data) internal returns (bytes memory _res) {
bool success;
(success, _res) = _wallet.call(abi.encodeWithSignature("invoke(address,uint256,bytes)", _to, _value, _data));
if (success && _res.length > 0) { //_res is empty if _wallet is an "old" BaseWallet that can't return output values
(_res) = abi.decode(_res, (bytes));
} else if (_res.length > 0) {
// solhint-disable-next-line no-inline-assembly
assembly {
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
}
} else if (!success) {
revert("BM: wallet invoke reverted");
}
}
}