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

[Prototype] AragonApp: signed calls and multi call execution (aka meta-txs support) #442

Closed
wants to merge 3 commits into from
Closed
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
23 changes: 23 additions & 0 deletions contracts/apps/AppStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ contract AppStorage {
// keccak256("aragonOS.appStorage.pinnedCode"), used by Proxy Pinned
bytes32 internal constant PINNED_CODE_POSITION = 0xdee64df20d65e53d7f51cb6ab6d921a0a6a638a91e942e1d8d02df28e31c038e;

bytes32 internal constant VOLATILE_SENDER_POSITION = keccak256("aragonOS.appStorage.volatile.sender");
bytes32 internal constant USED_NONCE_POSITION_BASE = keccak256("aragonOS.appStorage.usedNonce");

function kernel() public view returns (IKernel) {
return IKernel(KERNEL_POSITION.getStorageAddress());
}
Expand All @@ -26,11 +29,31 @@ contract AppStorage {
return APP_ID_POSITION.getStorageBytes32();
}

function volatileStorageSender() public view returns (address) {
return VOLATILE_SENDER_POSITION.getStorageAddress();
}

function usedNonce(address _account, uint256 _nonce) public view returns (bool) {
return usedNoncePosition(_account, _nonce).getStorageBool();
}

function setKernel(IKernel _kernel) internal {
KERNEL_POSITION.setStorageAddress(address(_kernel));
}

function setAppId(bytes32 _appId) internal {
APP_ID_POSITION.setStorageBytes32(_appId);
}

function setVolatileStorageSender(address _sender) internal {
VOLATILE_SENDER_POSITION.setStorageAddress(_sender);
}

function setUsedNonce(address _account, uint256 _nonce, bool _used) internal {
return usedNoncePosition(_account, _nonce).setStorageBool(_used);
}

function usedNoncePosition(address _account, uint256 _nonce) internal returns (bytes32) {
return keccak256(abi.encodePacked(USED_NONCE_POSITION_BASE, _account, _nonce));
}
}
68 changes: 66 additions & 2 deletions contracts/apps/AragonApp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

pragma solidity ^0.4.24;
// pragma experimental ABIEncoderV2;

import "./AppStorage.sol";
import "../common/Autopetrified.sol";
Expand All @@ -18,17 +19,52 @@ import "../acl/ACLSyntaxSugar.sol";
// that they are automatically usable by subclassing contracts
contract AragonApp is AppStorage, Autopetrified, VaultRecoverable, EVMScriptRunner, ACLSyntaxSugar {
string private constant ERROR_AUTH_FAILED = "APP_AUTH_FAILED";
string private constant ERROR_NONCE_REUSE = "APP_NONCE_REUSE";
string private constant ERROR_INVALID_SIGNATURE = "APP_INVALID_SIGNATURE";

address internal constant ZERO_ADDRESS = address(0);

modifier auth(bytes32 _role) {
require(canPerform(msg.sender, _role, new uint256[](0)), ERROR_AUTH_FAILED);
require(canPerform(sender(), _role, new uint256[](0)), ERROR_AUTH_FAILED);
_;
}

modifier authP(bytes32 _role, uint256[] _params) {
require(canPerform(msg.sender, _role, _params), ERROR_AUTH_FAILED);
require(canPerform(sender(), _role, _params), ERROR_AUTH_FAILED);
_;
}

// TODO: support standard? https://eips.ethereum.org/EIPS/eip-1077
function exec(address signer, bytes calldata, uint256 nonce, bytes signature) public {
require(!usedNonce(signer, nonce), ERROR_NONCE_REUSE);
bytes32 signedHash = executionHash(calldata, nonce);

require(isValidSignature(signer, signedHash, signature), ERROR_INVALID_SIGNATURE);

// This won't be too expensive on Constantinople: https://eips.ethereum.org/EIPS/eip-1283
setVolatileStorageSender(signer);
setUsedNonce(signer, nonce, true);
bool success = address(this).call(calldata);
if (!success) {
// no need to clean up storage as the entire execution frame is reverted
revertForwadingError();
}
setVolatileStorageSender(ZERO_ADDRESS);
}

/*
// TODO: Currently causing a solidity error: InternalCompilerError
function multiexec(address[] signers, bytes[] calldatas, uint256[] nonces, bytes[] signatures) external {
require(signers.length == calldatas.length);
require(signers.length == nonces.length);
require(signers.length == signatures.length);

for (uint256 i = 0; i < signers.length; i++) {
exec(signers[i], calldatas[i], nonces[i], signatures[i]);
}
}
*/

/**
* @dev Check whether an action can be performed by a sender for a particular role on this app
* @param _sender Sender of the call
Expand Down Expand Up @@ -66,4 +102,32 @@ contract AragonApp is AppStorage, Autopetrified, VaultRecoverable, EVMScriptRunn
// Funds recovery via a vault is only available when used with a kernel
return kernel().getRecoveryVault(); // if kernel is not set, it will revert
}

function isValidSignature(address signer, bytes32 hash, bytes signature) public pure returns (bool) {
// TODO: Actually check signature.
return true; // YOLO
}

function executionHash(bytes calldata, uint256 nonce) public pure returns (bytes32) {
return keccak256(abi.encodePacked(keccak256(calldata), nonce));
}

function revertForwadingError() internal {
assembly {
let size := returndatasize
let ptr := mload(0x40)
returndatacopy(ptr, 0, size)
revert(ptr, size)
}
}

function sender() internal view returns (address) {
// Prevents a sub-frame from re-entering into the app while the signer is authenticated
if (msg.sender != address(this)) {
return msg.sender;
}

address volatileSender = volatileStorageSender();
return volatileSender != ZERO_ADDRESS ? volatileSender : address(this);
}
}