Skip to content

Commit

Permalink
feat: add contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
3esmit committed Jun 7, 2024
1 parent 61d27a7 commit 4782d00
Show file tree
Hide file tree
Showing 23 changed files with 2,056 additions and 9 deletions.
24 changes: 24 additions & 0 deletions contracts/common/Controlled.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: CC0-1.0

pragma solidity 0.8.19;

abstract contract Controlled {
/// @notice The address of the controller is the only address that can call
/// a function with this modifier
modifier onlyController {
require(msg.sender == controller, "Unauthorized");
_;
}

address payable public controller;

constructor() {
controller = payable(msg.sender);
}

/// @notice Changes the controller of the contract
/// @param _newController The new controller of the contract
function changeController(address payable _newController) public onlyController {
controller = _newController;
}
}
45 changes: 45 additions & 0 deletions contracts/common/MerkleProof.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.19;


/**
* @title MerkleProof
* @dev Merkle proof verification based on
* https://github.com/ameensol/merkle-tree-solidity/blob/master/src/MerkleProof.sol
*/
library MerkleProof {
/**
* @dev Verifies a Merkle proof proving the existence of a leaf in a Merkle tree. Assumes that each pair of leaves
* and each pair of pre-images are sorted.
* @param _proof Merkle proof containing sibling hashes on the branch from the leaf to the root of the Merkle tree
* @param _root Merkle root
* @param _leaf Leaf of Merkle tree
*/
function verifyProof(
bytes32[] memory _proof,
bytes32 _root,
bytes32 _leaf
)
internal
pure
returns (bool)
{
bytes32 computedHash = _leaf;

for (uint256 i = 0; i < _proof.length; i++) {
bytes32 proofElement = _proof[i];

if (computedHash < proofElement) {
// Hash(current computed hash + current element of the proof)
computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
} else {
// Hash(current element of the proof + current computed hash)
computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
}
}

// Check if the computed hash (root) is equal to the provided root
return computedHash == _root;
}
}
78 changes: 78 additions & 0 deletions contracts/common/MessageSigned.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: CC0-1.0

pragma solidity 0.8.19;

/**
* @notice Uses ethereum signed messages
*/
abstract contract MessageSigned {

/**
* @notice recovers address who signed the message
* @param _signHash operation ethereum signed message hash
* @param _messageSignature message `_signHash` signature
*/
function recoverAddress(
bytes32 _signHash,
bytes memory _messageSignature
)
internal
pure
returns(address)
{
uint8 v;
bytes32 r;
bytes32 s;
(v,r,s) = signatureSplit(_messageSignature);
return ecrecover(
_signHash,
v,
r,
s
);
}

/**
* @notice Hash a hash with `"\x19Ethereum Signed Message:\n32"`
* @param _hash Sign to hash.
* @return signHash Hash to be signed.
*/
function getSignHash(
bytes32 _hash
)
internal
pure
returns (bytes32 signHash)
{
signHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _hash));
}

/**
* @dev divides bytes signature into `uint8 v, bytes32 r, bytes32 s`
*/
function signatureSplit(bytes memory _signature)
internal
pure
returns (uint8 v, bytes32 r, bytes32 s)
{
require(_signature.length == 65, "Bad signature length");
// The signature format is a compact form of:
// {bytes32 r}{bytes32 s}{uint8 v}
// Compact means, uint8 is not padded to 32 bytes.
assembly {
r := mload(add(_signature, 32))
s := mload(add(_signature, 64))
// Here we are loading the last 32 bytes, including 31 bytes
// of 's'. There is no 'mload8' to do this.
//
// 'byte' is not working due to the Solidity parser, so lets
// use the second best option, 'and'
v := and(mload(add(_signature, 65)), 0xff)
}
if (v < 27) {
v += 27;
}
require(v == 27 || v == 28, "Bad signature version");
}

}
38 changes: 38 additions & 0 deletions contracts/common/Owned.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: CC0-1.0

pragma solidity 0.8.19;

/// @dev `Owned` is a base level contract that assigns an `owner` that can be
/// later changed
abstract contract Owned {

/// @dev `owner` is the only address that can call a function with this
/// modifier
modifier onlyOwner() {
require(msg.sender == owner, "Unauthorized");
_;
}

address payable public owner;

/// @notice The Constructor assigns the message sender to be `owner`
constructor() {
owner = payable(msg.sender);
}

address payable public newOwner;

/// @notice `owner` can step down and assign some other address to this role
/// @param _newOwner The address of the new owner. 0x0 can be used to create
/// an unowned neutral vault, however that cannot be undone
function changeOwner(address payable _newOwner) public onlyOwner {
newOwner = _newOwner;
}


function acceptOwnership() public {
if (msg.sender == newOwner) {
owner = newOwner;
}
}
}
48 changes: 48 additions & 0 deletions contracts/common/SafeMath.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.19;

/**
* Math operations with safety checks
*/
library SafeMath {
function mul(uint a, uint b) internal pure returns (uint) {
uint c = a * b;
assert(a == 0 || c / a == b);
return c;
}

function div(uint a, uint b) internal pure returns (uint) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}

function sub(uint a, uint b) internal pure returns (uint) {
assert(b <= a);
return a - b;
}

function add(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
assert(c >= a);
return c;
}

function max64(uint64 a, uint64 b) internal pure returns (uint64) {
return a >= b ? a : b;
}

function min64(uint64 a, uint64 b) internal pure returns (uint64) {
return a < b ? a : b;
}

function max256(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}

function min256(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
}
35 changes: 35 additions & 0 deletions contracts/ens/ENS.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: BSD-2-Clause

pragma solidity 0.8.19;


interface ENS {

// Logged when the owner of a node assigns a new owner to a subnode.
event NewOwner(bytes32 indexed node, bytes32 indexed label, address owner);

// Logged when the owner of a node transfers ownership to a new account.
event Transfer(bytes32 indexed node, address owner);

// Logged when the resolver for a node changes.
event NewResolver(bytes32 indexed node, address resolver);

// Logged when the TTL of a node changes
event NewTTL(bytes32 indexed node, uint64 ttl);

// Logged when an operator is added or removed.
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

function setRecord(bytes32 _node, address _owner, address _resolver, uint64 _ttl) external;
function setSubnodeRecord(bytes32 _node, bytes32 _label, address _owner, address _resolver, uint64 _ttl) external;
function setSubnodeOwner(bytes32 _node, bytes32 _label, address _owner) external returns(bytes32);
function setResolver(bytes32 _node, address _resolver) external;
function setOwner(bytes32 _node, address _owner) external;
function setTTL(bytes32 _node, uint64 _ttl) external;
function setApprovalForAll(address _operator, bool _approved) external;
function owner(bytes32 _node) external view returns (address);
function resolver(bytes32 _node) external view returns (address);
function ttl(bytes32 _node) external view returns (uint64);
function recordExists(bytes32 _node) external view returns (bool);
function isApprovedForAll(address _owner, address _operator) external view returns (bool);
}
Loading

0 comments on commit 4782d00

Please sign in to comment.