Merge 8-fix-secrethash-zero into main
7d97bbb parent: e99639d modified
src/Love.sol +8 -0 | @@ -74,6 +74,12 @@ contract Love is ERC20 { | ||
| 74 | 74 | /// @param depositId The id that doesn't exist. |
| 75 | 75 | error UnknownDeposit(uint256 depositId); |
| 76 | 76 | |
| 77 | + /// @notice Thrown when a privacy deposit is attempted with a zero `secretHash`. | |
| 78 | + /// @dev `claim` uses `secretHash == bytes32(0)` as its "envelope doesn't | |
| 79 | + /// exist" sentinel, so a zero hash would otherwise create an envelope | |
| 80 | + /// that can never be claimed. | |
| 81 | + error ZeroSecretHash(); | |
| 82 | + | |
| 77 | 83 | /// @notice Thrown when a secret doesn't hash to the envelope's `secretHash`. |
| 78 | 84 | /// @param depositId The envelope the wrong secret was tried against. |
| 79 | 85 | error WrongSecret(uint256 depositId); |
| @@ -159,6 +165,8 @@ contract Love is ERC20 { | ||
| 159 | 165 | /// @param wethAmount The wETH to lock, in wei. |
| 160 | 166 | /// @return depositId The id this envelope was assigned. |
| 161 | 167 | function deposit(bytes32 secretHash, uint256 wethAmount) external returns (uint256 depositId) { |
| 168 | + if (secretHash == bytes32(0)) revert ZeroSecretHash(); | |
| 169 | + | |
| 162 | 170 | uint256 loveAmount = wethAmount * RATE; |
| 163 | 171 | |
| 164 | 172 | WETH.safeTransferFrom(msg.sender, address(this), wethAmount); |
| @@ -74,6 +74,12 @@ contract Love is ERC20 { | |||
| 74 | /// @param depositId The id that doesn't exist. | 74 | /// @param depositId The id that doesn't exist. |
| 75 | error UnknownDeposit(uint256 depositId); | 75 | error UnknownDeposit(uint256 depositId); |
| 76 | 76 | ||
| 77 | + /// @notice Thrown when a privacy deposit is attempted with a zero `secretHash`. | ||
| 78 | + /// @dev `claim` uses `secretHash == bytes32(0)` as its "envelope doesn't | ||
| 79 | + /// exist" sentinel, so a zero hash would otherwise create an envelope | ||
| 80 | + /// that can never be claimed. | ||
| 81 | + error ZeroSecretHash(); | ||
| 82 | + | ||
| 77 | /// @notice Thrown when a secret doesn't hash to the envelope's `secretHash`. | 83 | /// @notice Thrown when a secret doesn't hash to the envelope's `secretHash`. |
| 78 | /// @param depositId The envelope the wrong secret was tried against. | 84 | /// @param depositId The envelope the wrong secret was tried against. |
| 79 | error WrongSecret(uint256 depositId); | 85 | error WrongSecret(uint256 depositId); |
| @@ -159,6 +165,8 @@ contract Love is ERC20 { | |||
| 159 | /// @param wethAmount The wETH to lock, in wei. | 165 | /// @param wethAmount The wETH to lock, in wei. |
| 160 | /// @return depositId The id this envelope was assigned. | 166 | /// @return depositId The id this envelope was assigned. |
| 161 | function deposit(bytes32 secretHash, uint256 wethAmount) external returns (uint256 depositId) { | 167 | function deposit(bytes32 secretHash, uint256 wethAmount) external returns (uint256 depositId) { |
| 168 | + if (secretHash == bytes32(0)) revert ZeroSecretHash(); | ||
| 169 | + | ||
| 162 | uint256 loveAmount = wethAmount * RATE; | 170 | uint256 loveAmount = wethAmount * RATE; |
| 163 | 171 | ||
| 164 | WETH.safeTransferFrom(msg.sender, address(this), wethAmount); | 172 | WETH.safeTransferFrom(msg.sender, address(this), wethAmount); |
modified
test/LoveEnvelope.t.sol +9 -0 | @@ -102,6 +102,15 @@ contract LoveEnvelopeTest is Test, Fixtures { | ||
| 102 | 102 | love.deposit(SECRET_HASH, 100 ether); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | + function test_RevertWhen_SecretHashIsZero() public { | |
| 106 | + vm.prank(alice); | |
| 107 | + weth.approve(address(love), 1 ether); | |
| 108 | + | |
| 109 | + vm.prank(alice); | |
| 110 | + vm.expectRevert(Love.ZeroSecretHash.selector); | |
| 111 | + love.deposit(bytes32(0), 1 ether); | |
| 112 | + } | |
| 113 | + | |
| 105 | 114 | /*////////////////////////////////////////////////////////////// |
| 106 | 115 | CLAIM |
| 107 | 116 | //////////////////////////////////////////////////////////////*/ |
| @@ -102,6 +102,15 @@ contract LoveEnvelopeTest is Test, Fixtures { | |||
| 102 | love.deposit(SECRET_HASH, 100 ether); | 102 | love.deposit(SECRET_HASH, 100 ether); |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | + function test_RevertWhen_SecretHashIsZero() public { | ||
| 106 | + vm.prank(alice); | ||
| 107 | + weth.approve(address(love), 1 ether); | ||
| 108 | + | ||
| 109 | + vm.prank(alice); | ||
| 110 | + vm.expectRevert(Love.ZeroSecretHash.selector); | ||
| 111 | + love.deposit(bytes32(0), 1 ether); | ||
| 112 | + } | ||
| 113 | + | ||
| 105 | /*////////////////////////////////////////////////////////////// | 114 | /*////////////////////////////////////////////////////////////// |
| 106 | CLAIM | 115 | CLAIM |
| 107 | //////////////////////////////////////////////////////////////*/ | 116 | //////////////////////////////////////////////////////////////*/ |