-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathECDSA.t.sol
More file actions
50 lines (39 loc) · 1.77 KB
/
ECDSA.t.sol
File metadata and controls
50 lines (39 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.0;
import {ECDSA} from "src/utils/ECDSA.sol";
import {DSTest} from "ds-test/test.sol";
interface Vm {
function sign(uint256 privateKey, bytes32 digest) external returns (uint8 v, bytes32 r, bytes32 s);
function addr(uint256 privateKey) external returns (address);
}
contract ECDSATest is DSTest {
Vm public constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
function testRecoverValidSignature() public {
bytes32 message = keccak256("hello solmate");
(uint8 v, bytes32 r, bytes32 s) = vm.sign(1, message);
address expected = vm.addr(1);
bytes memory sig = abi.encodePacked(r, s, v);
address recovered = ECDSA.recover(message, sig);
assertEq(recovered, expected);
}
function testInvalidSigLength() public {
address recovered = ECDSA.recover(keccak256("msg"), hex"1234");
assertEq(recovered, address(0));
}
function testWrongSignatureReturnsZero() public {
address recovered = ECDSA.recover(
keccak256("hello"),
hex"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb"
);
emit log_address(recovered); // <-- This will print the address
assertEq(recovered, address(0));
}
function testMalleableSignature() public {
bytes32 message = keccak256("test");
(uint8 v, bytes32 r, bytes32 s) = vm.sign(2, message);
// Modify the signature to make it malleable
bytes memory sig = abi.encodePacked(r, s, v + 1); // Invalid v value
address recovered = ECDSA.recover(message, sig);
assertEq(recovered, address(0)); // Should return zero for malleable signature
}
}