Skip to content

Commit 1cc9ac5

Browse files
Merge pull request #25 from 0xJurassicPunk/minor-refactoring
refactor: Minor adjustments for revert statements
2 parents 2a9735f + a310507 commit 1cc9ac5

File tree

5 files changed

+46
-79
lines changed

5 files changed

+46
-79
lines changed

contracts/OwnableTwoSteps.sol

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,37 @@ import {IOwnableTwoSteps} from "./interfaces/IOwnableTwoSteps.sol";
1010
*/
1111
abstract contract OwnableTwoSteps is IOwnableTwoSteps {
1212
// Address of the current owner
13-
address private _owner;
13+
address public owner;
1414

1515
// Address of the potential owner
16-
address private _potentialOwner;
16+
address public potentialOwner;
17+
18+
// Delay for the timelock (in seconds)
19+
uint256 public delay;
1720

1821
// Earliest ownership renouncement timestamp
19-
uint256 private _earliestOwnershipRenouncementTime;
22+
uint256 public earliestOwnershipRenouncementTime;
2023

2124
// Ownership status
2225
Status public status;
2326

24-
// Current delay for the timelock (in seconds)
25-
uint256 public delay;
26-
2727
/**
2828
* @notice Modifier to wrap functions for contracts that inherit this contract
2929
*/
3030
modifier onlyOwner() {
31-
if (msg.sender != _owner) {
31+
if (msg.sender != owner) {
3232
revert NotOwner();
3333
}
3434
_;
3535
}
3636

3737
/**
3838
* @notice Constructor
39-
* @notice Initial owner is the deployment address while initial delay (for the timelock) must be set by contract that inherit from this.
39+
* Initial owner is the deployment address.
40+
* Delay (for the timelock) must be set by the contract that inherits from this.
4041
*/
4142
constructor() {
42-
_owner = msg.sender;
43+
owner = msg.sender;
4344
}
4445

4546
/**
@@ -51,9 +52,9 @@ abstract contract OwnableTwoSteps is IOwnableTwoSteps {
5152
if (status == Status.NoOngoingTransfer) revert NoOngoingTransferInProgress();
5253

5354
if (status == Status.TransferInProgress) {
54-
delete _potentialOwner;
55+
delete potentialOwner;
5556
} else if (status == Status.RenouncementInProgress) {
56-
delete _earliestOwnershipRenouncementTime;
57+
delete earliestOwnershipRenouncementTime;
5758
}
5859

5960
delete status;
@@ -66,10 +67,10 @@ abstract contract OwnableTwoSteps is IOwnableTwoSteps {
6667
*/
6768
function confirmOwnershipRenouncement() external onlyOwner {
6869
if (status != Status.RenouncementInProgress) revert RenouncementNotInProgress();
69-
if (block.timestamp < _earliestOwnershipRenouncementTime) revert RenouncementTooEarly();
70+
if (block.timestamp < earliestOwnershipRenouncementTime) revert RenouncementTooEarly();
7071

71-
delete _earliestOwnershipRenouncementTime;
72-
delete _owner;
72+
delete earliestOwnershipRenouncementTime;
73+
delete owner;
7374
delete status;
7475

7576
emit NewOwner(address(0));
@@ -81,26 +82,26 @@ abstract contract OwnableTwoSteps is IOwnableTwoSteps {
8182
*/
8283
function confirmOwnershipTransfer() external {
8384
if (status != Status.TransferInProgress) revert TransferNotInProgress();
84-
if (msg.sender != _potentialOwner) revert WrongPotentialOwner();
85+
if (msg.sender != potentialOwner) revert WrongPotentialOwner();
8586

86-
_owner = msg.sender;
87+
owner = msg.sender;
8788
delete status;
88-
delete _potentialOwner;
89+
delete potentialOwner;
8990

90-
emit NewOwner(_owner);
91+
emit NewOwner(owner);
9192
}
9293

9394
/**
9495
* @notice Initiate transfer of ownership to a new owner
95-
* @param newPotentialOwner address of the new potential owner
96+
* @param newPotentialOwner New potential owner address
9697
*/
9798
function initiateOwnershipTransfer(address newPotentialOwner) external onlyOwner {
9899
if (status != Status.NoOngoingTransfer) revert TransferAlreadyInProgress();
99100

100101
status = Status.TransferInProgress;
101-
_potentialOwner = newPotentialOwner;
102+
potentialOwner = newPotentialOwner;
102103

103-
emit InitiateOwnershipTransfer(_owner, newPotentialOwner);
104+
emit InitiateOwnershipTransfer(owner, newPotentialOwner);
104105
}
105106

106107
/**
@@ -110,40 +111,16 @@ abstract contract OwnableTwoSteps is IOwnableTwoSteps {
110111
if (status != Status.NoOngoingTransfer) revert TransferAlreadyInProgress();
111112

112113
status = Status.RenouncementInProgress;
113-
_earliestOwnershipRenouncementTime = block.timestamp + delay;
114-
115-
emit InitiateOwnershipRenouncement(_earliestOwnershipRenouncementTime);
116-
}
117-
118-
/**
119-
* @notice Returns owner address
120-
* @return owner address
121-
*/
122-
function owner() external view returns (address) {
123-
return _owner;
124-
}
125-
126-
/**
127-
* @notice Returns potential owner address
128-
* @return potential owner address
129-
*/
130-
function potentialOwner() external view returns (address) {
131-
return _potentialOwner;
132-
}
114+
earliestOwnershipRenouncementTime = block.timestamp + delay;
133115

134-
/**
135-
* @notice Returns the earliest timestamp after which the ownership renouncement
136-
* can be confirmed by the current owner.
137-
* @return earliest timestamp for renouncement confirmation
138-
*/
139-
function earliestOwnershipRenouncementTime() external view returns (uint256) {
140-
return _earliestOwnershipRenouncementTime;
116+
emit InitiateOwnershipRenouncement(earliestOwnershipRenouncementTime);
141117
}
142118

143119
/**
144120
* @notice Set up the timelock delay for renouncing ownership
145-
* @param _delay timelock delay for the owner to confirm renouncing the ownership
121+
* @param _delay Timelock delay for the owner to confirm renouncing the ownership
146122
* @dev This function is expected to be included in the constructor of the contract that inherits this contract.
123+
* If it is not set, there is no timelock to renounce the ownership.
147124
*/
148125
function _setupDelayForRenouncingOwnership(uint256 _delay) internal {
149126
delay = _delay;

contracts/ReentrancyGuard.sol

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {IReentrancyGuard} from "./interfaces/IReentrancyGuard.sol";
66
/**
77
* @title ReentrancyGuard
88
* @notice This contract protects against reentrancy attacks.
9-
* It is adjusted from OpenZeppelin.
9+
* It is adjusted from OpenZeppelin.
1010
*/
1111
abstract contract ReentrancyGuard is IReentrancyGuard {
1212
uint256 private _status;
@@ -19,9 +19,7 @@ abstract contract ReentrancyGuard is IReentrancyGuard {
1919
* @notice Modifier to wrap functions to prevent reentrancy calls
2020
*/
2121
modifier nonReentrant() {
22-
if (_status == 2) {
23-
revert ReentrancyFail();
24-
}
22+
if (_status == 2) revert ReentrancyFail();
2523

2624
_status = 2;
2725
_;

contracts/SignatureChecker.sol

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,30 @@ abstract contract SignatureChecker is ISignatureChecker {
4343
revert WrongSignatureLength(signature.length);
4444
}
4545

46-
// https://ethereum.stackexchange.com/questions/83174/is-it-best-practice-to-check-signature-malleability-in-ecrecover
47-
// https://crypto.iacr.org/2019/affevents/wac/medias/Heninger-BiasedNonceSense.pdf
48-
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
49-
revert BadSignatureS();
50-
}
46+
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) revert BadSignatureS();
5147

52-
if (v != 27 && v != 28) {
53-
revert BadSignatureV(v);
54-
}
48+
if (v != 27 && v != 28) revert BadSignatureV(v);
5549
}
5650

5751
/**
5852
* @notice Recover the signer of a signature (for EOA)
59-
* @param hash the hash of the signed message
60-
* @param signature bytes containing the signature (64 or 65 bytes)
53+
* @param hash Hash of the signed message
54+
* @param signature Bytes containing the signature (64 or 65 bytes)
6155
*/
62-
function _recoverEOASigner(bytes32 hash, bytes memory signature) internal pure returns (address) {
56+
function _recoverEOASigner(bytes32 hash, bytes memory signature) internal pure returns (address signer) {
6357
(bytes32 r, bytes32 s, uint8 v) = _splitSignature(signature);
6458

6559
// If the signature is valid (and not malleable), return the signer address
66-
address signer = ecrecover(hash, v, r, s);
67-
68-
if (signer == address(0)) {
69-
revert NullSignerAddress();
70-
}
60+
signer = ecrecover(hash, v, r, s);
7161

72-
return signer;
62+
if (signer == address(0)) revert NullSignerAddress();
7363
}
7464

7565
/**
7666
* @notice Checks whether the signer is valid
77-
* @param hash data hash
78-
* @param signer the signer address (to confirm message validity)
79-
* @param signature signature parameters encoded (v, r, s)
67+
* @param hash Data hash
68+
* @param signer Signer address (to confirm message validity)
69+
* @param signature Signature parameters encoded (v, r, s)
8070
* @dev For EIP-712 signatures, the hash must be the digest (computed with signature hash and domain separator)
8171
*/
8272
function _verify(
@@ -85,13 +75,9 @@ abstract contract SignatureChecker is ISignatureChecker {
8575
bytes memory signature
8676
) internal view {
8777
if (signer.code.length == 0) {
88-
if (_recoverEOASigner(hash, signature) != signer) {
89-
revert InvalidSignatureEOA();
90-
}
78+
if (_recoverEOASigner(hash, signature) != signer) revert InvalidSignatureEOA();
9179
} else {
92-
if (IERC1271(signer).isValidSignature(hash, signature) != 0x1626ba7e) {
93-
revert InvalidSignatureERC1271();
94-
}
80+
if (IERC1271(signer).isValidSignature(hash, signature) != 0x1626ba7e) revert InvalidSignatureERC1271();
9581
}
9682
}
9783
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.0;
33

4+
/**
5+
* @title IReentrancyGuard
6+
*/
47
interface IReentrancyGuard {
58
error ReentrancyFail();
69
}

contracts/interfaces/ISignatureChecker.sol

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.0;
33

4+
/**
5+
* @title ISignatureChecker
6+
*/
47
interface ISignatureChecker {
58
// Custom errors
69
error BadSignatureS();

0 commit comments

Comments
 (0)