Skip to content

Commit

Permalink
✨ 2098 tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
  • Loading branch information
pcaversaccio committed Aug 3, 2022
1 parent 4e6ad70 commit 30a7645
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "lib/create-util"]
path = lib/create-util
url = https://github.com/pcaversaccio/create-util.git
[submodule "lib/solidity-bytes-utils"]
path = lib/solidity-bytes-utils
url = https://github.com/GNSPS/solidity-bytes-utils.git
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
lib/create-util
lib/forge-std
lib/solidity-bytes-utils
bin
cache
out
1 change: 1 addition & 0 deletions .solhintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
lib/create-util
lib/forge-std
lib/solidity-bytes-utils
1 change: 1 addition & 0 deletions lib/solidity-bytes-utils
Submodule solidity-bytes-utils added at 6458fb
39 changes: 34 additions & 5 deletions test/utils/ECDSA.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@ pragma solidity ^0.8.15;

import {Test} from "../../lib/forge-std/src/Test.sol";
import {console} from "../../lib/forge-std/src/console.sol";
import {BytesLib} from "../../lib/solidity-bytes-utils/contracts/BytesLib.sol";
import {VyperDeployer} from "../../lib/utils/VyperDeployer.sol";

import {IECDSA} from "../../test/utils/IECDSA.sol";

contract ECDSATest is Test {
using BytesLib for bytes;

VyperDeployer private vyperDeployer = new VyperDeployer();
// solhint-disable-next-line var-name-mixedcase
IECDSA private ECDSA;

function to2098Format(bytes calldata signature)
function to2098Format(bytes memory signature)
public
pure
returns (bytes memory)
{
require(signature.length == 65, "invalid signature length");
require(uint8(signature[32]) >> 7 != 1, "invalid signature 's' value");
bytes memory short = signature[0:32];
bytes memory short = signature.slice(0, 64);
uint8 parityBit = uint8(short[32]) | ((uint8(signature[64]) % 27) << 7);
short[32] = bytes1(parityBit);
return short;
Expand All @@ -37,15 +40,18 @@ contract ECDSATest is Test {
assertEq(alice, ECDSA.recover_sig(hash, signature));

// EIP-2098
// console.logBytes1(signature[32]);
// bytes memory signature2098 = to2098Format(signature);
// assertEq(alice, ECDSA.recover_sig(hash, signature2098));
bytes memory signature2098 = to2098Format(signature);
assertEq(alice, ECDSA.recover_sig(hash, signature2098));
}

function testRecoverWithTooShortSignature() public {
bytes32 hash = keccak256("WAGMI");
bytes memory signature = "0x0123456789";
assertEq(address(0), ECDSA.recover_sig(hash, signature));

// EIP-2098
vm.expectRevert(bytes("invalid signature length"));
to2098Format(signature);
}

function testRecoverWithTooLongSignature() public {
Expand All @@ -55,6 +61,10 @@ contract ECDSATest is Test {
);
vm.expectRevert();
ECDSA.recover_sig(hash, signature);

// EIP-2098
vm.expectRevert(bytes("invalid signature length"));
to2098Format(signature);
}

function testRecoverWithArbitraryMessage() public {
Expand All @@ -63,6 +73,10 @@ contract ECDSATest is Test {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(1, hash);
bytes memory signature = abi.encodePacked(r, s, v);
assertEq(alice, ECDSA.recover_sig(hash, signature));

// EIP-2098
bytes memory signature2098 = to2098Format(signature);
assertEq(alice, ECDSA.recover_sig(hash, signature2098));
}

function testRecoverWithWrongMessage() public {
Expand All @@ -73,6 +87,10 @@ contract ECDSATest is Test {
bytes32 hashWrong = keccak256("WAGMI1");
address recoveredAddress = ECDSA.recover_sig(hashWrong, signature);
assertTrue(alice != recoveredAddress);

// EIP-2098
bytes memory signature2098 = to2098Format(signature);
assertTrue(alice != ECDSA.recover_sig(hashWrong, signature2098));
}

function testRecoverWithInvalidSignature() public {
Expand Down Expand Up @@ -119,6 +137,12 @@ contract ECDSATest is Test {
abi.encodePacked(signatureWithoutVersion, v)
)
);

// EIP-2098
bytes memory signature2098 = to2098Format(
abi.encodePacked(signatureWithoutVersion, v)
);
assertEq(alice, ECDSA.recover_sig(hash, signature2098));
}

function testRecoverWithTooHighSValue() public {
Expand All @@ -129,6 +153,11 @@ contract ECDSATest is Test {
bytes memory signature = abi.encodePacked(r, bytes32(sTooHigh), v);
vm.expectRevert(bytes("ECDSA: invalid signature 's' value"));
ECDSA.recover_sig(hash, signature);

// EIP-2098
vm.expectRevert(bytes("invalid signature 's' value"));
bytes memory signature2098 = to2098Format(signature);
vm.expectRevert(bytes("invalid signature 's' value"));
}

function testEthSignedMessageHash() public {
Expand Down

0 comments on commit 30a7645

Please sign in to comment.