You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
7.6 KiB
222 lines
7.6 KiB
4 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
|
pragma solidity >=0.6.11;
|
||
|
|
||
3 years ago
|
// ============ Internal Imports ============
|
||
|
import {Home} from "./Home.sol";
|
||
|
import {Replica} from "./Replica.sol";
|
||
|
import {TypeCasts} from "../libs/TypeCasts.sol";
|
||
|
// ============ External Imports ============
|
||
|
import {ECDSA} from "@openzeppelin/contracts/cryptography/ECDSA.sol";
|
||
|
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
|
||
4 years ago
|
|
||
3 years ago
|
/**
|
||
|
* @title XAppConnectionManager
|
||
|
* @author Celo Labs Inc.
|
||
|
* @notice Manages a registry of local Replica contracts
|
||
|
* for remote Home domains. Accepts Watcher signatures
|
||
|
* to un-enroll Replicas attached to fraudulent remote Homes
|
||
|
*/
|
||
4 years ago
|
contract XAppConnectionManager is Ownable {
|
||
3 years ago
|
// ============ Public Storage ============
|
||
4 years ago
|
|
||
3 years ago
|
// Home contract
|
||
4 years ago
|
Home public home;
|
||
3 years ago
|
// local Replica address => remote Home domain
|
||
|
mapping(address => uint32) public replicaToDomain;
|
||
|
// remote Home domain => local Replica address
|
||
|
mapping(uint32 => address) public domainToReplica;
|
||
4 years ago
|
// watcher address => replica remote domain => has/doesn't have permission
|
||
4 years ago
|
mapping(address => mapping(uint32 => bool)) private watcherPermissions;
|
||
4 years ago
|
|
||
3 years ago
|
// ============ Events ============
|
||
|
|
||
|
/**
|
||
|
* @notice Emitted when a new Replica is enrolled / added
|
||
|
* @param domain the remote domain of the Home contract for the Replica
|
||
|
* @param replica the address of the Replica
|
||
|
*/
|
||
4 years ago
|
event ReplicaEnrolled(uint32 indexed domain, address replica);
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Emitted when a new Replica is un-enrolled / removed
|
||
|
* @param domain the remote domain of the Home contract for the Replica
|
||
|
* @param replica the address of the Replica
|
||
|
*/
|
||
4 years ago
|
event ReplicaUnenrolled(uint32 indexed domain, address replica);
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Emitted when Watcher permissions are changed
|
||
|
* @param domain the remote domain of the Home contract for the Replica
|
||
|
* @param watcher the address of the Watcher
|
||
|
* @param access TRUE if the Watcher was given permissions, FALSE if permissions were removed
|
||
|
*/
|
||
4 years ago
|
event WatcherPermissionSet(
|
||
|
uint32 indexed domain,
|
||
|
address watcher,
|
||
|
bool access
|
||
|
);
|
||
|
|
||
3 years ago
|
// ============ Modifiers ============
|
||
4 years ago
|
|
||
4 years ago
|
modifier onlyReplica() {
|
||
|
require(isReplica(msg.sender), "!replica");
|
||
|
_;
|
||
|
}
|
||
|
|
||
3 years ago
|
// ============ Constructor ============
|
||
|
|
||
|
// solhint-disable-next-line no-empty-blocks
|
||
|
constructor() Ownable() {}
|
||
|
|
||
|
// ============ External Functions ============
|
||
|
|
||
|
/**
|
||
|
* @notice Un-Enroll a replica contract
|
||
|
* in the case that fraud was detected on the Home
|
||
|
* @dev in the future, if fraud occurs on the Home contract,
|
||
|
* the Watcher will submit their signature directly to the Home
|
||
|
* and it can be relayed to all remote chains to un-enroll the Replicas
|
||
|
* @param _domain the remote domain of the Home contract for the Replica
|
||
|
* @param _updater the address of the Updater for the Home contract (also stored on Replica)
|
||
|
* @param _signature signature of watcher on (domain, replica address, updater address)
|
||
|
*/
|
||
4 years ago
|
function unenrollReplica(
|
||
|
uint32 _domain,
|
||
4 years ago
|
bytes32 _updater,
|
||
4 years ago
|
bytes memory _signature
|
||
|
) external {
|
||
3 years ago
|
// ensure that the replica is currently set
|
||
4 years ago
|
address _replica = domainToReplica[_domain];
|
||
|
require(_replica != address(0), "!replica exists");
|
||
3 years ago
|
// ensure that the signature is on the proper updater
|
||
4 years ago
|
require(
|
||
|
Replica(_replica).updater() == TypeCasts.bytes32ToAddress(_updater),
|
||
|
"!current updater"
|
||
|
);
|
||
3 years ago
|
// get the watcher address from the signature
|
||
|
// and ensure that the watcher has permission to un-enroll this replica
|
||
3 years ago
|
address _watcher = _recoverWatcherFromSig(
|
||
3 years ago
|
_domain,
|
||
|
TypeCasts.addressToBytes32(_replica),
|
||
|
_updater,
|
||
|
_signature
|
||
|
);
|
||
4 years ago
|
require(watcherPermissions[_watcher][_domain], "!valid watcher");
|
||
3 years ago
|
// remove the replica from mappings
|
||
3 years ago
|
_unenrollReplica(_replica);
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Set the address of the local Home contract
|
||
|
* @param _home the address of the local Home contract
|
||
|
*/
|
||
|
function setHome(address _home) external onlyOwner {
|
||
4 years ago
|
home = Home(_home);
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Allow Owner to enroll Replica contract
|
||
|
* @param _replica the address of the Replica
|
||
|
* @param _domain the remote domain of the Home contract for the Replica
|
||
|
*/
|
||
4 years ago
|
function ownerEnrollReplica(address _replica, uint32 _domain)
|
||
3 years ago
|
external
|
||
4 years ago
|
onlyOwner
|
||
|
{
|
||
3 years ago
|
// un-enroll any existing replica
|
||
3 years ago
|
_unenrollReplica(_replica);
|
||
3 years ago
|
// add replica and domain to two-way mapping
|
||
4 years ago
|
replicaToDomain[_replica] = _domain;
|
||
|
domainToReplica[_domain] = _replica;
|
||
|
emit ReplicaEnrolled(_domain, _replica);
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Allow Owner to un-enroll Replica contract
|
||
|
* @param _replica the address of the Replica
|
||
|
*/
|
||
|
function ownerUnenrollReplica(address _replica) external onlyOwner {
|
||
3 years ago
|
_unenrollReplica(_replica);
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Allow Owner to set Watcher permissions for a Replica
|
||
|
* @param _watcher the address of the Watcher
|
||
|
* @param _domain the remote domain of the Home contract for the Replica
|
||
|
* @param _access TRUE to give the Watcher permissions, FALSE to remove permissions
|
||
|
*/
|
||
4 years ago
|
function setWatcherPermission(
|
||
|
address _watcher,
|
||
|
uint32 _domain,
|
||
|
bool _access
|
||
3 years ago
|
) external onlyOwner {
|
||
4 years ago
|
watcherPermissions[_watcher][_domain] = _access;
|
||
|
emit WatcherPermissionSet(_domain, _watcher, _access);
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Query local domain from Home
|
||
|
* @return local domain
|
||
|
*/
|
||
|
function localDomain() external view returns (uint32) {
|
||
4 years ago
|
return home.localDomain();
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Get access permissions for the watcher on the domain
|
||
|
* @param _watcher the address of the watcher
|
||
|
* @param _domain the domain to check for watcher permissions
|
||
|
* @return TRUE iff _watcher has permission to un-enroll replicas on _domain
|
||
|
*/
|
||
4 years ago
|
function watcherPermission(address _watcher, uint32 _domain)
|
||
3 years ago
|
external
|
||
4 years ago
|
view
|
||
|
returns (bool)
|
||
|
{
|
||
|
return watcherPermissions[_watcher][_domain];
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
3 years ago
|
// ============ Public Functions ============
|
||
|
|
||
|
/**
|
||
|
* @notice Check whether _replica is enrolled
|
||
|
* @param _replica the replica to check for enrollment
|
||
|
* @return TRUE iff _replica is enrolled
|
||
|
*/
|
||
|
function isReplica(address _replica) public view returns (bool) {
|
||
|
return replicaToDomain[_replica] != 0;
|
||
|
}
|
||
|
|
||
|
// ============ Internal Functions ============
|
||
|
|
||
|
/**
|
||
|
* @notice Remove the replica from the two-way mappings
|
||
|
* @param _replica replica to un-enroll
|
||
|
*/
|
||
3 years ago
|
function _unenrollReplica(address _replica) internal {
|
||
4 years ago
|
uint32 _currentDomain = replicaToDomain[_replica];
|
||
|
domainToReplica[_currentDomain] = address(0);
|
||
|
replicaToDomain[_replica] = 0;
|
||
|
emit ReplicaUnenrolled(_currentDomain, _replica);
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @notice Get the Watcher address from the provided signature
|
||
|
* @return address of watcher that signed
|
||
|
*/
|
||
3 years ago
|
function _recoverWatcherFromSig(
|
||
4 years ago
|
uint32 _domain,
|
||
4 years ago
|
bytes32 _replica,
|
||
|
bytes32 _updater,
|
||
4 years ago
|
bytes memory _signature
|
||
|
) internal view returns (address) {
|
||
3 years ago
|
bytes32 _homeDomainHash = Replica(TypeCasts.bytes32ToAddress(_replica))
|
||
3 years ago
|
.homeDomainHash();
|
||
3 years ago
|
bytes32 _digest = keccak256(
|
||
|
abi.encodePacked(_homeDomainHash, _domain, _updater)
|
||
|
);
|
||
4 years ago
|
_digest = ECDSA.toEthSignedMessageHash(_digest);
|
||
|
return ECDSA.recover(_digest, _signature);
|
||
|
}
|
||
4 years ago
|
}
|