Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
e83176ba
Commit
e83176ba
authored
Mar 29, 2023
by
James Kim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor optimistInviterHelper
parent
471b4af5
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
46 deletions
+121
-46
OptimistInviter.t.sol
...s-periphery/contracts/foundry-tests/OptimistInviter.t.sol
+13
-46
OptimistInviterHelper.sol
...phery/contracts/testing/helpers/OptimistInviterHelper.sol
+108
-0
No files found.
packages/contracts-periphery/contracts/foundry-tests/OptimistInviter.t.sol
View file @
e83176ba
...
@@ -8,9 +8,7 @@ import { OptimistInviter } from "../universal/op-nft/OptimistInviter.sol";
...
@@ -8,9 +8,7 @@ import { OptimistInviter } from "../universal/op-nft/OptimistInviter.sol";
import { Optimist } from "../universal/op-nft/Optimist.sol";
import { Optimist } from "../universal/op-nft/Optimist.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { TestERC1271Wallet } from "../testing/helpers/TestERC1271Wallet.sol";
import { TestERC1271Wallet } from "../testing/helpers/TestERC1271Wallet.sol";
import {
import { OptimistInviterHelper } from "../testing/helpers/OptimistInviterHelper.sol";
ClaimableInviteEIP712TypedData
} from "../testing/helpers/ClaimableInviteEIP712TypedData.sol";
contract OptimistInviter_Initializer is Test {
contract OptimistInviter_Initializer is Test {
event InviteClaimed(address indexed issuer, address indexed claimer);
event InviteClaimed(address indexed issuer, address indexed claimer);
...
@@ -35,16 +33,14 @@ contract OptimistInviter_Initializer is Test {
...
@@ -35,16 +33,14 @@ contract OptimistInviter_Initializer is Test {
address internal carol;
address internal carol;
uint256 internal carolPrivateKey;
uint256 internal carolPrivateKey;
uint256 currentNonce;
TestERC1271Wallet carolERC1271Wallet;
TestERC1271Wallet carolERC1271Wallet;
AttestationStation attestationStation;
AttestationStation attestationStation;
OptimistInviter optimistInviter;
OptimistInviter optimistInviter;
function setUp() public {
OptimistInviterHelper optimistInviterHelper;
currentNonce = 0;
function setUp() public {
alice_inviteGranter = makeAddr("alice_inviteGranter");
alice_inviteGranter = makeAddr("alice_inviteGranter");
sally = makeAddr("sally");
sally = makeAddr("sally");
ted = makeAddr("ted");
ted = makeAddr("ted");
...
@@ -83,14 +79,8 @@ contract OptimistInviter_Initializer is Test {
...
@@ -83,14 +79,8 @@ contract OptimistInviter_Initializer is Test {
vm.expectEmit(true, true, true, true, address(optimistInviter));
vm.expectEmit(true, true, true, true, address(optimistInviter));
emit Initialized(1);
emit Initialized(1);
optimistInviter.initialize("OptimistInviter");
optimistInviter.initialize("OptimistInviter");
}
/**
optimistInviterHelper = new OptimistInviterHelper(optimistInviter, "OptimistInviter");
* @notice Returns a bytes32 nonce that should change everytime. In practice, people should use
* pseudorandom nonces.
*/
function _consumeNonce() internal returns (bytes32) {
return bytes32(keccak256(abi.encode(currentNonce++)));
}
}
/**
/**
...
@@ -157,18 +147,14 @@ contract OptimistInviter_Initializer is Test {
...
@@ -157,18 +147,14 @@ contract OptimistInviter_Initializer is Test {
uint256 _eip712Chainid,
uint256 _eip712Chainid,
address _eip712VerifyingContract
address _eip712VerifyingContract
) internal returns (OptimistInviter.ClaimableInvite memory, bytes memory) {
) internal returns (OptimistInviter.ClaimableInvite memory, bytes memory) {
bytes32 nonce = _consumeNonce();
address issuer = vm.addr(_issuerPrivateKey);
address issuer = vm.addr(_issuerPrivateKey);
OptimistInviter.ClaimableInvite memory claimableInvite = OptimistInviter.ClaimableInvite(
OptimistInviter.ClaimableInvite memory claimableInvite = optimistInviterHelper
issuer,
.getClaimableInviteWithNewNonce(issuer);
nonce
);
return (
return (
claimableInvite,
claimableInvite,
_getSignature(
_getSignature(
_issuerPrivateKey,
_issuerPrivateKey,
ClaimableInviteEIP712TypedData.getDigest
(
optimistInviterHelper.getDigestWithEIP712Domain
(
claimableInvite,
claimableInvite,
_eip712Name,
_eip712Name,
_eip712Version,
_eip712Version,
...
@@ -261,25 +247,6 @@ contract OptimistInviter_Initializer is Test {
...
@@ -261,25 +247,6 @@ contract OptimistInviter_Initializer is Test {
assertEq(_getInviteCount(_to), 3);
assertEq(_getInviteCount(_to), 3);
}
}
/**
* @notice Issues 3 invites to the given address. Checks that all expected events are emitted
* and that state is updated correctly.
*/
function _getEIP712Digest(OptimistInviter.ClaimableInvite memory _claimableInvite)
internal
view
returns (bytes32)
{
return
ClaimableInviteEIP712TypedData.getDigest(
_claimableInvite,
bytes("OptimistInviter"),
bytes(optimistInviter.EIP712_VERSION()),
block.chainid,
address(optimistInviter)
);
}
}
}
contract OptimistInviterTest is OptimistInviter_Initializer {
contract OptimistInviterTest is OptimistInviter_Initializer {
...
@@ -521,13 +488,13 @@ contract OptimistInviterTest is OptimistInviter_Initializer {
...
@@ -521,13 +488,13 @@ contract OptimistInviterTest is OptimistInviter_Initializer {
function test_claimInvite_usingERC1271Wallet_succeeds() external {
function test_claimInvite_usingERC1271Wallet_succeeds() external {
_grantInvitesTo(address(carolERC1271Wallet));
_grantInvitesTo(address(carolERC1271Wallet));
bytes32 nonce = _consumeNonce();
OptimistInviter.ClaimableInvite memory claimableInvite = optimistInviterHelper
OptimistInviter.ClaimableInvite memory claimableInvite = OptimistInviter.ClaimableInvite(
.getClaimableInviteWithNewNonce(address(carolERC1271Wallet));
address(carolERC1271Wallet),
nonce
);
bytes memory signature = _getSignature(carolPrivateKey, _getEIP712Digest(claimableInvite));
bytes memory signature = _getSignature(
carolPrivateKey,
optimistInviterHelper.getDigest(claimableInvite)
);
// Sally tries to claim the invite
// Sally tries to claim the invite
_commitInviteAs(sally, signature);
_commitInviteAs(sally, signature);
...
...
packages/contracts-periphery/contracts/testing/helpers/
ClaimableInviteEIP712TypedData
.sol
→
packages/contracts-periphery/contracts/testing/helpers/
OptimistInviterHelper
.sol
View file @
e83176ba
...
@@ -2,12 +2,14 @@
...
@@ -2,12 +2,14 @@
pragma solidity 0.8.15;
pragma solidity 0.8.15;
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import { OptimistInviter } from "../../universal/op-nft/OptimistInviter.sol";
import { OptimistInviter } from "../../universal/op-nft/OptimistInviter.sol";
/**
/**
* Helper library that generates the EIP712 digest for a ClaimableInvite.
* Simple helper contract that helps with testing flow and signature for OptimistInviter contract.
* Made this a separate contract instead of including in OptimistInviter.t.sol for reusability.
*/
*/
library ClaimableInviteEIP712TypedData
{
contract OptimistInviterHelper
{
bytes32 public constant CLAIMABLE_INVITE_TYPEHASH =
bytes32 public constant CLAIMABLE_INVITE_TYPEHASH =
keccak256("ClaimableInvite(address issuer,bytes32 nonce)");
keccak256("ClaimableInvite(address issuer,bytes32 nonce)");
...
@@ -16,8 +18,21 @@ library ClaimableInviteEIP712TypedData {
...
@@ -16,8 +18,21 @@ library ClaimableInviteEIP712TypedData {
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
);
);
function _getStructHash(OptimistInviter.ClaimableInvite memory _claimableInvite)
OptimistInviter public optimistInviter;
internal
string public name;
uint256 public currentNonce;
constructor(OptimistInviter _optimistInviter, string memory _name) {
optimistInviter = _optimistInviter;
name = _name;
currentNonce = 0;
}
/**
* @notice Returns the hash of the struct ClaimableInvite
*/
function getClaimableInviteStructHash(OptimistInviter.ClaimableInvite memory _claimableInvite)
public
pure
pure
returns (bytes32)
returns (bytes32)
{
{
...
@@ -31,11 +46,47 @@ library ClaimableInviteEIP712TypedData {
...
@@ -31,11 +46,47 @@ library ClaimableInviteEIP712TypedData {
);
);
}
}
/**
* @notice Returns a bytes32 nonce that should change everytime. In practice, people should use
* pseudorandom nonces.
*/
function consumeNonce() public returns (bytes32) {
return bytes32(keccak256(abi.encode(currentNonce++)));
}
/**
* @notice Returns a ClaimableInvite with the issuer and current nonce
*/
function getClaimableInviteWithNewNonce(address _issuer)
public
returns (OptimistInviter.ClaimableInvite memory)
{
return OptimistInviter.ClaimableInvite(_issuer, consumeNonce());
}
/**
* @notice Computes the EIP712 digest with default correct parameters.
*/
function getDigest(OptimistInviter.ClaimableInvite calldata _claimableInvite)
public
view
returns (bytes32)
{
return
getDigestWithEIP712Domain(
_claimableInvite,
bytes(name),
bytes(optimistInviter.EIP712_VERSION()),
block.chainid,
address(optimistInviter)
);
}
/**
/**
* @notice Computes the EIP712 digest with the given domain parameters.
* @notice Computes the EIP712 digest with the given domain parameters.
* Used for testing that different domain parameters fail.
* Used for testing that different domain parameters fail.
*/
*/
function getDigest(
function getDigest
WithEIP712Domain
(
OptimistInviter.ClaimableInvite calldata _claimableInvite,
OptimistInviter.ClaimableInvite calldata _claimableInvite,
bytes memory _name,
bytes memory _name,
bytes memory _version,
bytes memory _version,
...
@@ -51,6 +102,7 @@ library ClaimableInviteEIP712TypedData {
...
@@ -51,6 +102,7 @@ library ClaimableInviteEIP712TypedData {
_verifyingContract
_verifyingContract
)
)
);
);
return ECDSA.toTypedDataHash(domainSeparator, _getStructHash(_claimableInvite));
return
ECDSA.toTypedDataHash(domainSeparator, getClaimableInviteStructHash(_claimableInvite));
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment