Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AppBase #133

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/user/AppBase.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// This file is part of Darwinia.
// Copyright (C) 2018-2023 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Darwinia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

pragma solidity ^0.8.17;

// https://eips.ethereum.org/EIPS/eip-5164
abstract contract AppBase {
function ormpSender() public view virtual returns (address);
function ormpRecver() public view virtual returns (address);

modifier onlyORMPRecver() {
require(ormpRecver() == msg.sender, "!ormp-recver");
_;
}

function _messageId() internal pure returns (bytes32 _msgDataMessageId) {
require(msg.data.length >= 84, "!messageId");
assembly {
_msgDataMessageId := calldataload(sub(calldatasize(), 84))
}
}

function _fromChainId() internal pure returns (uint256 _msgDataFromChainId) {
require(msg.data.length >= 52, "!fromChainId");
assembly {
_msgDataFromChainId := calldataload(sub(calldatasize(), 52))
}
}

function _xmsgSender() internal pure returns (address payable _from) {
require(msg.data.length >= 20, "!xmsgSender");
assembly {
_from := shr(96, calldataload(sub(calldatasize(), 20)))
}
}
}
38 changes: 10 additions & 28 deletions src/user/Application.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,24 @@
pragma solidity ^0.8.17;

import "../interfaces/IORMP.sol";
import "./AppBase.sol";

// https://eips.ethereum.org/EIPS/eip-5164
abstract contract Application {
address public immutable TRUSTED_ORMP;
abstract contract Application is AppBase {
address public immutable ORMP;

constructor(address ormp) {
TRUSTED_ORMP = ormp;
ORMP = ormp;
}

function _setAppConfig(address oracle, address relayer) internal virtual {
IORMP(TRUSTED_ORMP).setAppConfig(oracle, relayer);
}

modifier onlyORMP() {
require(TRUSTED_ORMP == msg.sender, "!ormp");
_;
function ormpSender() public view override returns (address) {
return ORMP;
}

function _messageId() internal pure returns (bytes32 _msgDataMessageId) {
require(msg.data.length >= 84, "!messageId");
assembly {
_msgDataMessageId := calldataload(sub(calldatasize(), 84))
}
function ormpRecver() public view override returns (address) {
return ORMP;
}

function _fromChainId() internal pure returns (uint256 _msgDataFromChainId) {
require(msg.data.length >= 52, "!fromChainId");
assembly {
_msgDataFromChainId := calldataload(sub(calldatasize(), 52))
}
}

function _xmsgSender() internal pure returns (address payable _from) {
require(msg.data.length >= 20, "!xmsgSender");
assembly {
_from := shr(96, calldataload(sub(calldatasize(), 20)))
}
function _setAppConfig(address oracle, address relayer) internal virtual {
IORMP(ORMP).setAppConfig(oracle, relayer);
}
}
38 changes: 10 additions & 28 deletions src/user/UpgradeableApplication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
pragma solidity ^0.8.17;

import "../interfaces/IORMP.sol";
import "./AppBase.sol";

// https://eips.ethereum.org/EIPS/eip-5164
abstract contract UpgradeableApplication {
abstract contract UpgradeableApplication is AppBase {
address public sender;
address public recver;

Expand All @@ -32,6 +32,14 @@ abstract contract UpgradeableApplication {
recver = ormp;
}

function ormpSender() public view override returns (address) {
return sender;
}

function ormpRecver() public view override returns (address) {
return recver;
}

function _setSender(address ormp) internal virtual {
sender = ormp;
emit SetSender(ormp);
Expand All @@ -49,30 +57,4 @@ abstract contract UpgradeableApplication {
function _setRecverConfig(address oracle, address relayer) internal virtual {
IORMP(recver).setAppConfig(oracle, relayer);
}

modifier onlyORMP() {
require(recver == msg.sender, "!ormp");
_;
}

function _messageId() internal pure returns (bytes32 _msgDataMessageId) {
require(msg.data.length >= 84, "!messageId");
assembly {
_msgDataMessageId := calldataload(sub(calldatasize(), 84))
}
}

function _fromChainId() internal pure returns (uint256 _msgDataFromChainId) {
require(msg.data.length >= 52, "!fromChainId");
assembly {
_msgDataFromChainId := calldataload(sub(calldatasize(), 52))
}
}

function _xmsgSender() internal pure returns (address payable _from) {
require(msg.data.length >= 20, "!xmsgSender");
assembly {
_from := shr(96, calldataload(sub(calldatasize(), 20)))
}
}
}
2 changes: 1 addition & 1 deletion test/user/Application.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ contract UserApplication is Application {
address xmsgSender = _xmsgSender();
require(msgHash == bytes32(uint256(1)));
require(fromChainid == 1);
require(xmsgSender == TRUSTED_ORMP);
require(xmsgSender == ormpRecver());
}
}
Loading