-
Notifications
You must be signed in to change notification settings - Fork 2
Example ECDSA verifier #72
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 2 commits
Commits
Show all changes
4 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 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,48 @@ | ||
| // SPDX-License-Identifier: GPL-3.0 | ||
| pragma solidity 0.8.29; | ||
|
|
||
| import { | ||
| OwnableBasedApp | ||
| } from "@ssv/src/middleware/modules/core+roles/OwnableBasedApp.sol"; | ||
|
|
||
| import { | ||
| SignatureChecker | ||
| } from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol"; | ||
| import "forge-std/Test.sol"; | ||
| import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; | ||
|
|
||
| contract ECDSAVerifier is OwnableBasedApp, Test { | ||
| mapping(address => bool) public hasOptedIn; | ||
| constructor( | ||
| address _basedAppManager, | ||
| address _initOwner | ||
| ) OwnableBasedApp(_basedAppManager, _initOwner) {} | ||
|
|
||
| function optInToBApp( | ||
| uint32, | ||
| address[] calldata, | ||
| uint32[] calldata, | ||
| bytes calldata data | ||
| ) external override onlySSVBasedAppManager returns (bool success) { | ||
| (address signer, bytes32 messageHash, bytes memory signature) = abi | ||
| .decode(data, (address, bytes32, bytes)); | ||
| success = SignatureChecker.isValidSignatureNow( | ||
| signer, | ||
| messageHash, | ||
| signature | ||
| ); | ||
|
|
||
| require(!hasOptedIn[signer], "Replay signature is not allowed"); | ||
riccardo-ssvlabs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Validate signature | ||
| success = SignatureChecker.isValidSignatureNow( | ||
riccardo-ssvlabs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| signer, | ||
| messageHash, | ||
| signature | ||
| ); | ||
| require(success, "Invalid signature"); | ||
riccardo-ssvlabs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Prevent replay | ||
| hasOptedIn[signer] = true; // mark as completed | ||
| } | ||
| } | ||
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
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,118 @@ | ||
| // SPDX-License-Identifier: GPL-3.0 | ||
| pragma solidity 0.8.29; | ||
|
|
||
| import { | ||
| IBasedAppWhitelisted | ||
| } from "@ssv/src/middleware/interfaces/IBasedAppWhitelisted.sol"; | ||
| import { IBasedApp } from "@ssv/test/helpers/Setup.t.sol"; | ||
| import { UtilsTest } from "@ssv/test/helpers/Utils.t.sol"; | ||
| import { ICore } from "@ssv/src/core/interfaces/ICore.sol"; | ||
| import { console, Script } from "forge-std/Script.sol"; | ||
|
|
||
| contract WhitelistExampleTest is UtilsTest, Script { | ||
| function testCreateStrategies() public { | ||
| vm.startPrank(USER1); | ||
| erc20mock.approve(address(proxiedManager), INITIAL_USER1_BALANCE_ERC20); | ||
| erc20mock2.approve( | ||
| address(proxiedManager), | ||
| INITIAL_USER1_BALANCE_ERC20 | ||
| ); | ||
| uint32 strategyId1 = proxiedManager.createStrategy( | ||
| STRATEGY1_INITIAL_FEE, | ||
| "" | ||
| ); | ||
| assertEq(strategyId1, STRATEGY1, "Should set the correct strategy ID"); | ||
| (address owner, uint32 delegationFeeOnRewards) = proxiedManager | ||
| .strategies(strategyId1); | ||
| assertEq(owner, USER1, "Should set the correct strategy owner"); | ||
| assertEq( | ||
| delegationFeeOnRewards, | ||
| STRATEGY1_INITIAL_FEE, | ||
| "Should set the correct strategy fee" | ||
| ); | ||
| vm.stopPrank(); | ||
| vm.prank(USER2); | ||
| uint32 strategyId2 = proxiedManager.createStrategy( | ||
| STRATEGY1_INITIAL_FEE, | ||
| "" | ||
| ); | ||
| } | ||
|
|
||
| function testRegisterECDSAVerifierExampleBApp() public { | ||
| vm.startPrank(USER1); | ||
| ICore.TokenConfig[] memory tokenConfigsInput = createSingleTokenConfig( | ||
| address(erc20mock), | ||
| 102 | ||
| ); | ||
| ecdsaVerifierExample.registerBApp(tokenConfigsInput, ""); | ||
| checkBAppInfo( | ||
| tokenConfigsInput, | ||
| address(ecdsaVerifierExample), | ||
| proxiedManager | ||
| ); | ||
| vm.stopPrank(); | ||
| } | ||
|
|
||
| function testRevertOptInToBAppWithUnauthorizedCaller() public { | ||
| vm.prank(USER1); | ||
| ( | ||
| address[] memory tokensInput, | ||
| uint32[] memory riskLevelInput | ||
| ) = createSingleTokenAndSingleRiskLevel(address(erc20mock), 10_000); | ||
| vm.expectRevert( | ||
| abi.encodeWithSelector(IBasedApp.UnauthorizedCaller.selector) | ||
| ); | ||
| ecdsaVerifierExample.optInToBApp( | ||
| STRATEGY1, | ||
| tokensInput, | ||
| riskLevelInput, | ||
| "" | ||
| ); | ||
| } | ||
|
|
||
| function testOptInToBApp() public { | ||
| testCreateStrategies(); | ||
| testRegisterECDSAVerifierExampleBApp(); | ||
| ( | ||
| address[] memory tokensInput, | ||
| uint32[] memory riskLevelInput | ||
| ) = createSingleTokenAndSingleRiskLevel(address(erc20mock), 10_000); | ||
| address signer = 0x763569566a3CE4f8D73f96c4996aBdf297f74ADE; | ||
| bytes32 messageHash = 0x5b001f2ad81fe86899545b51f8ecd1ca08674437d5c4748e1b70ba5dcf85ed86; | ||
| bytes | ||
| memory signature = hex"ddc30e871857b9a4d2dce47f49aec426404e69c98e05abc345df2f096e47fcb33d3e061da2435584d92df8731522d35ae485447311eb4a3c0bfdb09150cbad081b"; | ||
|
|
||
| bytes memory data = abi.encode(signer, messageHash, signature); | ||
| vm.prank(USER1); | ||
| proxiedManager.optInToBApp( | ||
| STRATEGY1, | ||
| address(ecdsaVerifierExample), | ||
| tokensInput, | ||
| riskLevelInput, | ||
| data | ||
| ); | ||
| } | ||
|
|
||
| function testReplayAttack() public { | ||
| testOptInToBApp(); | ||
| ( | ||
| address[] memory tokensInput, | ||
| uint32[] memory riskLevelInput | ||
| ) = createSingleTokenAndSingleRiskLevel(address(erc20mock), 10_000); | ||
| address signer = 0x763569566a3CE4f8D73f96c4996aBdf297f74ADE; | ||
| bytes32 messageHash = 0x5b001f2ad81fe86899545b51f8ecd1ca08674437d5c4748e1b70ba5dcf85ed86; | ||
| bytes | ||
| memory signature = hex"ddc30e871857b9a4d2dce47f49aec426404e69c98e05abc345df2f096e47fcb33d3e061da2435584d92df8731522d35ae485447311eb4a3c0bfdb09150cbad081b"; | ||
|
|
||
| bytes memory data = abi.encode(signer, messageHash, signature); | ||
| vm.prank(USER2); | ||
| vm.expectRevert(); | ||
| proxiedManager.optInToBApp( | ||
| STRATEGY2, | ||
| address(ecdsaVerifierExample), | ||
| tokensInput, | ||
| riskLevelInput, | ||
| data | ||
| ); | ||
| } | ||
| } |
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,7 @@ | ||
| ✅ Generated test input: | ||
| Signer Address : 0x86266729219486B9Fb9181ade660d2B4B286Daa8 | ||
| Message : Hello, Ethereum! | ||
| Message Hash : 0x5b001f2ad81fe86899545b51f8ecd1ca08674437d5c4748e1b70ba5dcf85ed86 | ||
| Signature : 0xad99db4ff3eea3e20e0d23a3c63eda2fbe6d05f60aee19e1f8855dd8cd6e06bc0ca32d083d9f9b8f49910a48bb006edb5f44355006a33ecae1a6d1b5035706a31c | ||
| ABI Encoded Payload: 0x00000000000000000000000086266729219486b9fb9181ade660d2b4b286daa85b001f2ad81fe86899545b51f8ecd1ca08674437d5c4748e1b70ba5dcf85ed8600000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000041ad99db4ff3eea3e20e0d23a3c63eda2fbe6d05f60aee19e1f8855dd8cd6e06bc0ca32d083d9f9b8f49910a48bb006edb5f44355006a33ecae1a6d1b5035706a31c00000000000000000000000000000000000000000000000000000000000000 | ||
| res: 0x0A823667Ea010859244c74e7915a32C2BFb59c08 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
| { | ||
| "name": "client", | ||
| "version": "1.0.0", | ||
| "main": "sig.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "description": "", | ||
| "dependencies": { | ||
| "ethers": "^6.14.3" | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.