Skip to content

Commit

Permalink
feat: abstract common contracts and clean up (#78)
Browse files Browse the repository at this point in the history
* chore: abstract common contracts

* chore: cleanup initialisers and constructors (#79)

* fix: vulnerability in abstraction

* fix: base relay recipient versioning
  • Loading branch information
justussoh authored Dec 7, 2020
1 parent f000ed5 commit 65092f8
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 436 deletions.
81 changes: 81 additions & 0 deletions contracts/BaseDocumentStore.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.6.10;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/Initializable.sol";

contract BaseDocumentStore is Initializable {
string public name;
string public version;

/// A mapping of the document hash to the block number that was issued
mapping(bytes32 => uint256) public documentIssued;
/// A mapping of the hash of the claim being revoked to the revocation block number
mapping(bytes32 => uint256) public documentRevoked;

event DocumentIssued(bytes32 indexed document);
event DocumentRevoked(bytes32 indexed document);

function initialize(string memory _name) public initializer {
version = "2.3.0";
name = _name;
}

function _issue(bytes32 document) internal onlyNotIssued(document) {
documentIssued[document] = block.number;
emit DocumentIssued(document);
}

function _bulkIssue(bytes32[] memory documents) internal {
for (uint256 i = 0; i < documents.length; i++) {
_issue(documents[i]);
}
}

function getIssuedBlock(bytes32 document) public view onlyIssued(document) returns (uint256) {
return documentIssued[document];
}

function isIssued(bytes32 document) public view returns (bool) {
return (documentIssued[document] != 0);
}

function isIssuedBefore(bytes32 document, uint256 blockNumber) public view returns (bool) {
return documentIssued[document] != 0 && documentIssued[document] <= blockNumber;
}

function _revoke(bytes32 document) internal onlyNotRevoked(document) returns (bool) {
documentRevoked[document] = block.number;
emit DocumentRevoked(document);
}

function _bulkRevoke(bytes32[] memory documents) internal {
for (uint256 i = 0; i < documents.length; i++) {
_revoke(documents[i]);
}
}

function isRevoked(bytes32 document) public view returns (bool) {
return documentRevoked[document] != 0;
}

function isRevokedBefore(bytes32 document, uint256 blockNumber) public view returns (bool) {
return documentRevoked[document] <= blockNumber && documentRevoked[document] != 0;
}

modifier onlyIssued(bytes32 document) {
require(isIssued(document), "Error: Only issued document hashes can be revoked");
_;
}

modifier onlyNotIssued(bytes32 document) {
require(!isIssued(document), "Error: Only hashes that have not been issued can be issued");
_;
}

modifier onlyNotRevoked(bytes32 claim) {
require(!isRevoked(claim), "Error: Hash has been revoked previously");
_;
}
}
24 changes: 0 additions & 24 deletions contracts/Context.sol

This file was deleted.

37 changes: 0 additions & 37 deletions contracts/ContextUpgradeSafe.sol

This file was deleted.

5 changes: 4 additions & 1 deletion contracts/DocumentStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

pragma solidity ^0.6.10;

import "./Ownable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

/*
* Legacy version for reference and backward compatibility, similar to OwnableDocumentStore
*/
contract DocumentStore is Ownable {
string public name;
string public version = "2.3.0";
Expand Down
79 changes: 3 additions & 76 deletions contracts/GsnCapable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,8 @@

pragma solidity ^0.6.10;

import "./Ownable.sol";

// File: contracts/introspection/IERC165.sol

/*
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

// File: contracts/introspection/ERC165.sol

/**
* @dev Implementation of the {IERC165} interface.
*
* Contracts may inherit from this and call {_registerInterface} to declare
* their support of an interface.
*/
contract ERC165 is IERC165 {
/*
* bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
*/
bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;

/**
* @dev Mapping of interface ids to whether or not it's supported.
*/
mapping(bytes4 => bool) private _supportedInterfaces;

constructor() internal {
// Derived contracts need only register support for their own interfaces,
// we register support for ERC165 itself here
_registerInterface(_INTERFACE_ID_ERC165);
}

/**
* @dev See {IERC165-supportsInterface}.
*
* Time complexity O(1), guaranteed to always use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external override view returns (bool) {
return _supportedInterfaces[interfaceId];
}

/**
* @dev Registers the contract as an implementer of the interface defined by
* `interfaceId`. Support of the actual ERC165 interface is automatic and
* registering its interface id is not required.
*
* See {IERC165-supportsInterface}.
*
* Requirements:
*
* - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`).
*/
function _registerInterface(bytes4 interfaceId) internal {
require(interfaceId != 0xffffffff, "ERC165: invalid interface id");
_supportedInterfaces[interfaceId] = true;
}
}
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/introspection/ERC165.sol";

contract GsnCapable is ERC165, Ownable {
address public paymaster;
Expand All @@ -99,7 +26,7 @@ contract GsnCapable is ERC165, Ownable {
}
}

contract calculateGsnCapableSelector {
contract CalculateGsnCapableSelector {
function calculateSelector() public pure returns (bytes4) {
GsnCapable i;
return i.setPaymaster.selector ^ i.getPaymaster.selector;
Expand Down
23 changes: 11 additions & 12 deletions contracts/GsnCapableDocumentStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@

pragma solidity ^0.6.10;

import "./Ownable.sol";
import "./DocumentStore.sol";
import "./GsnCapable.sol";
import "@opengsn/gsn/contracts/BaseRelayRecipient.sol";
import "@opengsn/gsn/contracts/interfaces/IKnowForwarderAddress.sol";

contract GsnCapableDocumentStore is DocumentStore, BaseRelayRecipient, IKnowForwarderAddress, GsnCapable {
constructor(string memory _name, address _forwarder) public DocumentStore(_name) {
import "./OwnableDocumentStore.sol";
import "./GsnCapable.sol";

contract GsnCapableDocumentStore is OwnableDocumentStore, BaseRelayRecipient, IKnowForwarderAddress, GsnCapable {

string public override versionRecipient = "2.0.0";

constructor(string memory _name, address _forwarder) public OwnableDocumentStore(_name) {
trustedForwarder = _forwarder;
}

function _msgSender() internal override(Context, BaseRelayRecipient) view returns (address payable) {
function _msgSender() internal view override(Context, BaseRelayRecipient) returns (address payable) {
return BaseRelayRecipient._msgSender();
}

function _msgData() internal override(Context, BaseRelayRecipient) view returns (bytes memory) {
function _msgData() internal view override(Context, BaseRelayRecipient) returns (bytes memory) {
return BaseRelayRecipient._msgData();
}

function getTrustedForwarder() public override view returns (address) {
function getTrustedForwarder() public view override returns (address) {
return trustedForwarder;
}

function setTrustedForwarder(address _forwarder) public onlyOwner {
trustedForwarder = _forwarder;
}

function versionRecipient() external virtual override view returns (string memory) {
return version;
}
}
64 changes: 0 additions & 64 deletions contracts/Initializable.sol

This file was deleted.

Loading

0 comments on commit 65092f8

Please sign in to comment.