-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #654 from ionicprotocol/feat/hypernative-security-…
…oracle Hypernative security oracle
- Loading branch information
Showing
169 changed files
with
103,229 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/contracts/contracts/compound/CTokenOracleProtected.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity ^0.8.22; | ||
|
||
import { CErc20Storage } from "./CTokenInterfaces.sol"; | ||
import { IHypernativeOracle } from "../external/hypernative/interfaces/IHypernativeOracle.sol"; | ||
|
||
contract CTokenOracleProtected is CErc20Storage { | ||
error InteractionNotAllowed(); | ||
|
||
modifier onlyOracleApproved() { | ||
address oracleAddress = ap.getAddress("HYPERNATIVE_ORACLE"); | ||
if (oracleAddress == address(0)) { | ||
_; | ||
return; | ||
} | ||
IHypernativeOracle oracle = IHypernativeOracle(oracleAddress); | ||
if (oracle.isBlacklistedContext(msg.sender, tx.origin) || !oracle.isTimeExceeded(msg.sender)) { | ||
revert InteractionNotAllowed(); | ||
} | ||
_; | ||
} | ||
|
||
modifier onlyOracleApprovedAllowEOA() { | ||
address oracleAddress = ap.getAddress("HYPERNATIVE_ORACLE"); | ||
if (oracleAddress == address(0)) { | ||
_; | ||
return; | ||
} | ||
|
||
IHypernativeOracle oracle = IHypernativeOracle(oracleAddress); | ||
if (oracle.isBlacklistedAccount(msg.sender) || msg.sender != tx.origin) { | ||
revert InteractionNotAllowed(); | ||
} | ||
_; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/contracts/contracts/external/hypernative/interfaces/IHypernativeOracle.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.22; | ||
|
||
interface IHypernativeOracle { | ||
function register(address account) external; | ||
function registerStrict(address account) external; | ||
function isBlacklistedAccount(address account) external view returns (bool); | ||
function isBlacklistedContext(address sender, address origin) external view returns (bool); | ||
function isTimeExceeded(address account) external view returns (bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.22; | ||
|
||
import { IHypernativeOracle } from "../external/hypernative/interfaces/IHypernativeOracle.sol"; | ||
import { Ownable2Step } from "@openzeppelin/contracts/access/Ownable2Step.sol"; | ||
|
||
contract OracleRegistry is Ownable2Step { | ||
bytes32 private constant HYPERNATIVE_ORACLE_STORAGE_SLOT = | ||
bytes32(uint256(keccak256("eip1967.hypernative.oracle")) - 1); | ||
bytes32 private constant HYPERNATIVE_MODE_STORAGE_SLOT = | ||
bytes32(uint256(keccak256("eip1967.hypernative.is_strict_mode")) - 1); | ||
|
||
event OracleAdminChanged(address indexed previousAdmin, address indexed newAdmin); | ||
event OracleAddressChanged(address indexed previousOracle, address indexed newOracle); | ||
|
||
constructor() Ownable2Step() {} | ||
|
||
function oracleRegister(address _account) public { | ||
address oracleAddress = _getAddressBySlot(HYPERNATIVE_ORACLE_STORAGE_SLOT); | ||
IHypernativeOracle oracle = IHypernativeOracle(oracleAddress); | ||
if (hypernativeOracleIsStrictMode()) { | ||
oracle.registerStrict(_account); | ||
} else { | ||
oracle.register(_account); | ||
} | ||
} | ||
|
||
function setOracle(address _oracle) public onlyOwner { | ||
_setOracle(_oracle); | ||
} | ||
|
||
function setIsStrictMode(bool _mode) public onlyOwner { | ||
_setIsStrictMode(_mode); | ||
} | ||
|
||
function hypernativeOracleIsStrictMode() public view returns (bool) { | ||
return _getValueBySlot(HYPERNATIVE_MODE_STORAGE_SLOT) == 1; | ||
} | ||
|
||
function hypernativeOracle() public view returns (address) { | ||
return _getAddressBySlot(HYPERNATIVE_ORACLE_STORAGE_SLOT); | ||
} | ||
|
||
/** | ||
* @dev Admin only function, sets new oracle admin. set to address(0) to revoke oracle | ||
*/ | ||
function _setOracle(address _oracle) internal { | ||
address oldOracle = hypernativeOracle(); | ||
_setAddressBySlot(HYPERNATIVE_ORACLE_STORAGE_SLOT, _oracle); | ||
emit OracleAddressChanged(oldOracle, _oracle); | ||
} | ||
|
||
function _setIsStrictMode(bool _mode) internal { | ||
_setValueBySlot(HYPERNATIVE_MODE_STORAGE_SLOT, _mode ? 1 : 0); | ||
} | ||
|
||
function _setAddressBySlot(bytes32 slot, address newAddress) internal { | ||
assembly { | ||
sstore(slot, newAddress) | ||
} | ||
} | ||
|
||
function _setValueBySlot(bytes32 _slot, uint256 _value) internal { | ||
assembly { | ||
sstore(_slot, _value) | ||
} | ||
} | ||
|
||
function _getAddressBySlot(bytes32 slot) internal view returns (address addr) { | ||
assembly { | ||
addr := sload(slot) | ||
} | ||
} | ||
|
||
function _getValueBySlot(bytes32 _slot) internal view returns (uint256 _value) { | ||
assembly { | ||
_value := sload(_slot) | ||
} | ||
} | ||
} |
Oops, something went wrong.