-
Notifications
You must be signed in to change notification settings - Fork 95
feat(TBAEF-1634): Add signature discount validator #146
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
302fb2d
Add new discount validator contract
stevieraykatz f74c1bd
Add unit tests
stevieraykatz 41db42d
Update ensip-19 fork tests to be compatible with a post-ensip-19 world
stevieraykatz 39f6da6
Temporarily add test script
stevieraykatz 95805f7
remove test script
stevieraykatz eeba32c
Fix ci/cd to only lint files changed in the pr
stevieraykatz 8a84ac9
Use more targeted checkout strategy
stevieraykatz 38e4da6
Fix checkout depth
stevieraykatz df6afdc
go back to less sophisticated checkout depth
stevieraykatz 0f40ddf
iterate through files for forge fmt checks
stevieraykatz 3e38d88
Fix iterator source
stevieraykatz 4d85835
Update src/L2/discounts/SignatureDiscountValidator.sol
stevieraykatz ed95c09
Update src/L2/discounts/SignatureDiscountValidator.sol
stevieraykatz ba3679e
Add fuzz tests
stevieraykatz 839e426
Update src/L2/discounts/SignatureDiscountValidator.sol
stevieraykatz a47d29f
Add zero address checks
stevieraykatz fb15e22
Update src/L2/discounts/SignatureDiscountValidator.sol
stevieraykatz 46bc962
Merge remote-tracking branch 'origin/signature-discount-validator' in…
stevieraykatz 791f5a0
Fix comment in lib
stevieraykatz 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| //SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.23; | ||
|
|
||
| import {Ownable} from "solady/auth/Ownable.sol"; | ||
|
|
||
| import {IDiscountValidator} from "src/L2/interface/IDiscountValidator.sol"; | ||
| import {SybilResistanceVerifier} from "src/lib/SybilResistanceVerifier.sol"; | ||
|
|
||
| /// @title Discount Validator for: Signature Discount Validator | ||
| /// | ||
| /// @notice Implements a simple signature validation schema which performs signature verification to validate | ||
| /// signatures were generated from the Base Signer Service. | ||
| /// | ||
| /// @author Coinbase (https://github.com/base-org/basenames) | ||
| contract SignatureDiscountValidator is Ownable, IDiscountValidator { | ||
| /// @dev The Base Signer Service signer address. | ||
| address signer; | ||
|
|
||
| /// @dev Thrown when setting the zero address as `owner` or `signer`. | ||
| error NoZeroAddress(); | ||
|
|
||
| /// @notice constructor | ||
| /// | ||
| /// @param owner_ The permissioned `owner` in the `Ownable` context. | ||
| /// @param signer_ The off-chain signer of the Base Signer Service. | ||
| constructor(address owner_, address signer_) { | ||
| if (owner_ == address(0)) revert NoZeroAddress(); | ||
| if (signer_ == address(0)) revert NoZeroAddress(); | ||
| _initializeOwner(owner_); | ||
| signer = signer_; | ||
| } | ||
|
|
||
| /// @notice Allows the owner to update the expected signer. | ||
| /// | ||
| /// @param signer_ The address of the new signer. | ||
| function setSigner(address signer_) external onlyOwner { | ||
|
||
| if (signer_ == address(0)) revert NoZeroAddress(); | ||
| signer = signer_; | ||
| } | ||
|
|
||
| /// @notice Required implementation for compatibility with IDiscountValidator. | ||
| /// | ||
| /// @dev The data must be encoded as `abi.encode(discountClaimerAddress, expiry, signature_bytes)`. | ||
| /// | ||
| /// @param claimer the discount claimer's address. | ||
| /// @param validationData opaque bytes for performing the validation. | ||
| /// | ||
| /// @return `true` if the validation data provided is determined to be valid for the specified claimer, else `false`. | ||
| function isValidDiscountRegistration(address claimer, bytes calldata validationData) external view returns (bool) { | ||
| return SybilResistanceVerifier.verifySignature(signer, claimer, validationData); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
41 changes: 41 additions & 0 deletions
41
test/discounts/SignatureDiscountValidator/IsValidDiscountRegistration.t.sol
stevieraykatz marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.23; | ||
|
|
||
| import {SignatureDiscountValidatorBase} from "./SignatureDiscountValidatorBase.t.sol"; | ||
| import {SybilResistanceVerifier} from "src/lib/SybilResistanceVerifier.sol"; | ||
|
|
||
| contract IsValidDiscountRegistration is SignatureDiscountValidatorBase { | ||
| function test_reverts_whenTheValidationData_claimerAddressMismatch(address notUser) public { | ||
| vm.assume(notUser != user && notUser != address(0)); | ||
| bytes memory validationData = _getDefaultValidationData(); | ||
| (, uint64 expires, bytes memory sig) = abi.decode(validationData, (address, uint64, bytes)); | ||
| bytes memory claimerMismatchValidationData = abi.encode(notUser, expires, sig); | ||
|
|
||
| vm.expectRevert(abi.encodeWithSelector(SybilResistanceVerifier.ClaimerAddressMismatch.selector, notUser, user)); | ||
| validator.isValidDiscountRegistration(user, claimerMismatchValidationData); | ||
| } | ||
|
|
||
| function test_reverts_whenTheValidationData_signatureIsExpired() public { | ||
| bytes memory validationData = _getDefaultValidationData(); | ||
| (address expectedClaimer,, bytes memory sig) = abi.decode(validationData, (address, uint64, bytes)); | ||
| bytes memory claimerMismatchValidationData = abi.encode(expectedClaimer, (block.timestamp - 1), sig); | ||
|
|
||
| vm.expectRevert(abi.encodeWithSelector(SybilResistanceVerifier.SignatureExpired.selector)); | ||
| validator.isValidDiscountRegistration(user, claimerMismatchValidationData); | ||
| } | ||
|
|
||
| function test_returnsFalse_whenTheExpectedSignerMismatches(uint256 pk) public view { | ||
| vm.assume(pk != signerPk && pk != 0 && pk < type(uint128).max); | ||
| address badSigner = vm.addr(pk); | ||
| bytes32 digest = SybilResistanceVerifier._makeSignatureHash(address(validator), badSigner, user, expires); | ||
| (uint8 v, bytes32 r, bytes32 s) = vm.sign(pk, digest); | ||
| bytes memory sig = abi.encodePacked(r, s, v); | ||
| bytes memory badSignerValidationData = abi.encode(user, expires, sig); | ||
|
|
||
| assertFalse(validator.isValidDiscountRegistration(user, badSignerValidationData)); | ||
| } | ||
|
|
||
| function test_returnsTrue_whenEverythingIsHappy() public { | ||
| assertTrue(validator.isValidDiscountRegistration(user, _getDefaultValidationData())); | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| //SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.23; | ||
|
|
||
| import {SignatureDiscountValidator} from "src/L2/discounts/SignatureDiscountValidator.sol"; | ||
| import {SignatureDiscountValidatorBase} from "./SignatureDiscountValidatorBase.t.sol"; | ||
| import {Ownable} from "solady/auth/Ownable.sol"; | ||
|
|
||
| contract SetSigner is SignatureDiscountValidatorBase { | ||
| function test_reverts_whenCalledByNonOwner(address caller) public { | ||
| vm.assume(caller != owner && caller != address(0)); | ||
| vm.expectRevert(Ownable.Unauthorized.selector); | ||
| vm.prank(caller); | ||
| validator.setSigner(caller); | ||
| } | ||
|
|
||
| function test_allowsTheOwner_toUpdateTheSigner(address newSigner) public { | ||
| vm.assume(newSigner != signer && newSigner != address(0)); | ||
| vm.prank(owner); | ||
| validator.setSigner(newSigner); | ||
| } | ||
|
|
||
| function test_revertWhen_settingSignerToZeroAddress() public { | ||
| vm.expectRevert(SignatureDiscountValidator.NoZeroAddress.selector); | ||
| vm.prank(owner); | ||
| validator.setSigner(address(0)); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
test/discounts/SignatureDiscountValidator/SignatureDiscountValidatorBase.t.sol
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.23; | ||
|
|
||
| import {Test, console} from "forge-std/Test.sol"; | ||
| import {SybilResistanceVerifier} from "src/lib/SybilResistanceVerifier.sol"; | ||
|
|
||
| import {SignatureDiscountValidator} from "src/L2/discounts/SignatureDiscountValidator.sol"; | ||
|
|
||
| contract SignatureDiscountValidatorBase is Test { | ||
| address public owner = makeAddr("owner"); | ||
| address public signer; | ||
| uint256 public signerPk; | ||
| address public user = makeAddr("user"); | ||
| uint64 time = 1717200000; | ||
| uint64 expires = 1893456000; | ||
|
|
||
| SignatureDiscountValidator validator; | ||
|
|
||
| function setUp() public { | ||
| vm.warp(time); | ||
| (signer, signerPk) = makeAddrAndKey("signer"); | ||
|
|
||
| validator = new SignatureDiscountValidator(owner, signer); | ||
| } | ||
|
|
||
| function _getDefaultValidationData() internal virtual returns (bytes memory) { | ||
| bytes32 digest = SybilResistanceVerifier._makeSignatureHash(address(validator), signer, user, expires); | ||
| (uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPk, digest); | ||
| bytes memory sig = abi.encodePacked(r, s, v); | ||
| return abi.encode(user, expires, sig); | ||
| } | ||
|
|
||
| function test_constructor() public { | ||
| vm.expectRevert(SignatureDiscountValidator.NoZeroAddress.selector); | ||
| new SignatureDiscountValidator(address(0), signer); | ||
|
|
||
| vm.expectRevert(SignatureDiscountValidator.NoZeroAddress.selector); | ||
| new SignatureDiscountValidator(owner, address(0)); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.