Skip to content

Commit

Permalink
mitigations: off chain signature validator (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
arr00 authored Nov 29, 2023
1 parent 98e684c commit be04028
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ contract OffChainSignatureValidator is IERC1271 {
error NotMemberOfParty();
error InsufficientVotingPower();
error MessageHashMismatch();
error InvalidSignature();

/// @notice Event emitted when signing threshold updated
event SigningThresholdBpsSet(
Expand Down Expand Up @@ -56,6 +57,11 @@ contract OffChainSignatureValidator is IERC1271 {

Party party = Party(payable(msg.sender));
address signer = ecrecover(hash, v, r, s);

if (signer == address(0)) {
revert InvalidSignature();
}

uint96 signerVotingPowerBps = party.getVotingPowerAt(
signer,
uint40(block.timestamp),
Expand All @@ -73,7 +79,7 @@ contract OffChainSignatureValidator is IERC1271 {
// Either threshold is 0 or signer votes above threshold
if (
thresholdBps == 0 ||
(signerVotingPowerBps > totalVotingPower &&
(signerVotingPowerBps >= totalVotingPower &&
signerVotingPowerBps / totalVotingPower >= thresholdBps)
) {
return IERC1271.isValidSignature.selector;
Expand Down
22 changes: 22 additions & 0 deletions test/signature-validators/OffChainSignatureValidator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ contract OffChainSignatureValidatorTest is SetupPartyHelper {
assertEq(abi.decode(res, (bytes4)), IERC1271.isValidSignature.selector);
}

function testOffChainSignatureValidator_invalidSignature() public {
string memory message = "Hello, this is my message";
bytes memory encondedMessage = abi.encodePacked(message);
bytes memory encodedPacket = abi.encodePacked(
"\x19Ethereum Signed Message:\n",
Strings.toString(encondedMessage.length),
encondedMessage
);
bytes32 messageHash = keccak256(encodedPacket);
(, bytes32 r, bytes32 s) = vm.sign(johnPk, messageHash);
bytes memory signature = abi.encodePacked(r, s, uint8(0), message);

bytes memory staticCallData = abi.encodeWithSelector(
IERC1271.isValidSignature.selector,
messageHash,
signature
);
vm.startPrank(address(0), address(0));
vm.expectRevert(OffChainSignatureValidator.InvalidSignature.selector);
address(party).staticcall(staticCallData);
}

function _signMessage(
uint256 privateKey,
string memory message
Expand Down

0 comments on commit be04028

Please sign in to comment.