generated from foundry-rs/forge-template
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Coral getter, readme update #97
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,25 @@ | ||
# <h1 align="center"> Forge Template </h1> | ||
|
||
**Template repository for getting started quickly with Foundry projects** | ||
|
||
![Github Actions](https://github.com/foundry-rs/forge-template/workflows/CI/badge.svg) | ||
|
||
## Getting Started | ||
|
||
Click "Use this template" on [GitHub](https://github.com/foundry-rs/forge-template) to create a new repository with this repo as the initial state. | ||
|
||
Or, if your repo already exists, run: | ||
```sh | ||
forge init | ||
forge build | ||
forge test | ||
# <h1 align="center"> Puffer Protocol </h1> | ||
[![Github Actions][gha-badge]][gha] [![Website][Website-badge]][Website] [![Docs][docs-badge]][docs] | ||
[![Discord][discord-badge]][discord] [![X][X-badge]][X] [![Foundry][foundry-badge]][foundry] | ||
|
||
[Website-badge]: https://img.shields.io/badge/WEBSITE-8A2BE2 | ||
[Website]: https://www.puffer.fi | ||
[X-badge]: https://img.shields.io/twitter/follow/puffer_finance | ||
[X]: https://twitter.com/puffer_finance | ||
[discord]: https://discord.gg/pufferfi | ||
[docs-badge]: https://img.shields.io/badge/DOCS-8A2BE2 | ||
[docs]: https://docs.puffer.fi/ | ||
[discord-badge]: https://dcbadge.vercel.app/api/server/pufferfi?style=flat | ||
[gha]: https://github.com/PufferFinance/PufferPool/actions | ||
[gha-badge]: https://github.com/PufferFinance/PufferPool/actions/workflows/ci.yml/badge.svg | ||
[foundry]: https://getfoundry.sh | ||
[foundry-badge]: https://img.shields.io/badge/Built%20with-Foundry-FFDB1C.svg | ||
|
||
![PUFFERS](image.png) | ||
|
||
# Tests | ||
|
||
Installing dependencies and running tests can be executed running: | ||
``` | ||
|
||
## Writing your first test | ||
|
||
All you need is to `import forge-std/Test.sol` and then inherit it from your test contract. Forge-std's Test contract comes with a pre-instatiated [cheatcodes environment](https://book.getfoundry.sh/cheatcodes/), the `vm`. It also has support for [ds-test](https://book.getfoundry.sh/reference/ds-test.html)-style logs and assertions. Finally, it supports Hardhat's [console.log](https://github.com/brockelmore/forge-std/blob/master/src/console.sol). The logging functionalities require `-vvvv`. | ||
|
||
```solidity | ||
pragma solidity 0.8.10; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
contract ContractTest is Test { | ||
function testExample() public { | ||
vm.roll(100); | ||
console.log(1); | ||
emit log("hi"); | ||
assertTrue(true); | ||
} | ||
} | ||
``` | ||
|
||
## Development | ||
|
||
This project uses [Foundry](https://getfoundry.sh). See the [book](https://book.getfoundry.sh/getting-started/installation.html) for instructions on how to install and use Foundry. | ||
forge test -vvv --match-path './test/unit/*' | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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 |
---|---|---|
|
@@ -2,21 +2,21 @@ | |
pragma solidity >=0.8.0 <0.9.0; | ||
|
||
import { Safe } from "safe-contracts/Safe.sol"; | ||
import { AccessManaged } from "openzeppelin/access/manager/AccessManaged.sol"; | ||
import { IGuardianModule } from "puffer/interface/IGuardianModule.sol"; | ||
import { PufferProtocol } from "puffer/PufferProtocol.sol"; | ||
import { IEnclaveVerifier } from "puffer/EnclaveVerifier.sol"; | ||
import { RaveEvidence } from "puffer/struct/RaveEvidence.sol"; | ||
import { ECDSA } from "openzeppelin/utils/cryptography/ECDSA.sol"; | ||
import { MessageHashUtils } from "openzeppelin/utils/cryptography/MessageHashUtils.sol"; | ||
import { Ownable } from "openzeppelin/access/Ownable.sol"; | ||
|
||
/** | ||
* @title Guardian module | ||
* @author Puffer finance | ||
* @dev This contract is both {Safe} module, and a logic contract to be called via `delegatecall` from {Safe} (GuardianAccount) | ||
* @custom:security-contact [email protected] | ||
*/ | ||
contract GuardianModule is Ownable, IGuardianModule { | ||
contract GuardianModule is AccessManaged, IGuardianModule { | ||
using ECDSA for bytes32; | ||
using MessageHashUtils for bytes32; | ||
|
||
|
@@ -25,24 +25,44 @@ contract GuardianModule is Ownable, IGuardianModule { | |
*/ | ||
uint256 internal constant _ECDSA_KEY_LENGTH = 65; | ||
|
||
/** | ||
* @notice Enclave Verifier smart contract | ||
*/ | ||
IEnclaveVerifier public immutable enclaveVerifier; | ||
|
||
/** | ||
* @notice Guardians {Safe} | ||
*/ | ||
Safe public immutable GUARDIANS; | ||
|
||
/** | ||
* @dev MRSIGNER value for SGX | ||
*/ | ||
bytes32 mrsigner; | ||
bytes32 mrenclave; | ||
|
||
/** | ||
* @dev Mapping from guardian address => enclave address | ||
* @dev MRENCLAVE value for SGX | ||
*/ | ||
mapping(address => address) internal _guardianEnclaves; | ||
bytes32 mrenclave; | ||
|
||
constructor(IEnclaveVerifier verifier, Safe guardians) Ownable(msg.sender) { | ||
struct GuardianData { | ||
bytes enclavePubKey; | ||
address enclaveAddress; | ||
} | ||
|
||
mapping(address guardian => GuardianData data) internal _guardianEnclaves; | ||
|
||
constructor(IEnclaveVerifier verifier, Safe guardians, address pufferAuthority) AccessManaged(pufferAuthority) { | ||
require(address(verifier) != address(0)); | ||
require(address(guardians) != address(0)); | ||
require(address(pufferAuthority) != address(0)); | ||
enclaveVerifier = verifier; | ||
GUARDIANS = guardians; | ||
} | ||
|
||
function setGuardianEnclaveMeasurements(bytes32 newMrenclave, bytes32 newMrsigner) public { | ||
//@audit don't forget owner modifier | ||
/** | ||
* @inheritdoc IGuardianModule | ||
*/ | ||
function setGuardianEnclaveMeasurements(bytes32 newMrenclave, bytes32 newMrsigner) external restricted { | ||
bytes32 previousMrEnclave = mrenclave; | ||
bytes32 previousMrsigner = mrsigner; | ||
mrenclave = newMrenclave; | ||
|
@@ -51,8 +71,11 @@ contract GuardianModule is Ownable, IGuardianModule { | |
emit MrSignerChanged(previousMrsigner, newMrsigner); | ||
} | ||
|
||
/** | ||
* @inheritdoc IGuardianModule | ||
*/ | ||
function validateGuardianSignatures( | ||
bytes memory pubKey, | ||
bytes calldata pubKey, | ||
bytes calldata signature, | ||
bytes32 depositDataRoot, | ||
bytes calldata withdrawalCredentials, | ||
|
@@ -63,13 +86,13 @@ contract GuardianModule is Ownable, IGuardianModule { | |
bytes32 msgToBeSigned = getMessageToBeSigned(pubKey, signature, withdrawalCredentials, depositDataRoot); | ||
|
||
address[] memory enclaveAddresses = getGuardiansEnclaveAddresses(); | ||
uint256 validSignatures = 0; | ||
uint256 validSignatures; | ||
|
||
// Iterate through guardian enclave addresses and make sure that the signers match | ||
for (uint256 i = 0; i < enclaveAddresses.length;) { | ||
for (uint256 i; i < enclaveAddresses.length;) { | ||
address currentSigner = ECDSA.recover(msgToBeSigned, guardianEnclaveSignatures[i]); | ||
if (currentSigner == enclaveAddresses[i]) { | ||
validSignatures++; | ||
++validSignatures; | ||
} | ||
unchecked { | ||
++i; | ||
|
@@ -81,6 +104,9 @@ contract GuardianModule is Ownable, IGuardianModule { | |
} | ||
} | ||
|
||
/** | ||
* @inheritdoc IGuardianModule | ||
*/ | ||
function getMessageToBeSigned( | ||
bytes memory pubKey, | ||
bytes calldata signature, | ||
|
@@ -90,13 +116,16 @@ contract GuardianModule is Ownable, IGuardianModule { | |
return keccak256(abi.encode(pubKey, withdrawalCredentials, signature, depositDataRoot)).toEthSignedMessageHash(); | ||
} | ||
|
||
/** | ||
* @inheritdoc IGuardianModule | ||
*/ | ||
function rotateGuardianKey(uint256 blockNumber, bytes calldata pubKey, RaveEvidence calldata evidence) external { | ||
address guardian = msg.sender; | ||
|
||
// Because this is called from the safe via .delegateCall | ||
// address(this) equals to {Safe} | ||
// This will revert if the caller is not one of the {Safe} owners | ||
if (!GUARDIANS.isOwner(msg.sender)) { | ||
if (!GUARDIANS.isOwner(guardian)) { | ||
revert Unauthorized(); | ||
} | ||
|
||
|
@@ -120,27 +149,51 @@ contract GuardianModule is Ownable, IGuardianModule { | |
// pubKey[1:] means we need to strip the first byte '0x' if we want to get the correct address | ||
address computedAddress = address(uint160(uint256(keccak256(pubKey[1:])))); | ||
|
||
_guardianEnclaves[guardian] = computedAddress; | ||
_guardianEnclaves[guardian].enclaveAddress = computedAddress; | ||
_guardianEnclaves[guardian].enclavePubKey = pubKey; | ||
|
||
emit RotatedGuardianKey(guardian, computedAddress, pubKey); | ||
} | ||
|
||
/** | ||
* @inheritdoc IGuardianModule | ||
*/ | ||
function isGuardiansEnclaveAddress(address guardian, address enclave) external view returns (bool) { | ||
// Assert if the stored enclaveAddress equals enclave | ||
return _guardianEnclaves[guardian] == enclave; | ||
return _guardianEnclaves[guardian].enclaveAddress == enclave; | ||
} | ||
|
||
/** | ||
* @inheritdoc IGuardianModule | ||
*/ | ||
function getGuardiansEnclaveAddresses() public view returns (address[] memory) { | ||
address[] memory guardians = GUARDIANS.getOwners(); | ||
address[] memory enclaveAddresses = new address[](guardians.length); | ||
|
||
for (uint256 i = 0; i < guardians.length;) { | ||
enclaveAddresses[i] = _guardianEnclaves[guardians[i]]; | ||
for (uint256 i; i < guardians.length;) { | ||
enclaveAddresses[i] = _guardianEnclaves[guardians[i]].enclaveAddress; | ||
unchecked { | ||
++i; | ||
} | ||
} | ||
|
||
return enclaveAddresses; | ||
} | ||
|
||
/** | ||
* @inheritdoc IGuardianModule | ||
*/ | ||
function getGuardiansEnclavePubkeys() public view returns (bytes[] memory) { | ||
address[] memory guardians = GUARDIANS.getOwners(); | ||
bytes[] memory enclavePubkeys = new bytes[](guardians.length); | ||
|
||
for (uint256 i; i < guardians.length;) { | ||
enclavePubkeys[i] = _guardianEnclaves[guardians[i]].enclavePubKey; | ||
unchecked { | ||
++i; | ||
} | ||
} | ||
|
||
return enclavePubkeys; | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JasonVranek How should we name this?