Testing Signature

This commit is contained in:
David E. Perez Negron R. 2023-10-06 19:11:14 -06:00
parent 446edaca3e
commit cfb46a6520
1 changed files with 32 additions and 0 deletions

32
test/Sign.t.sol Normal file
View File

@ -0,0 +1,32 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
import "forge-std/Test.sol";
contract signTest is Test {
// private key = 123
// public key = vm.addr(private key)
// message = "secret message"
// message hash = keccak256(message)
// vm.sign(private key, message hash)
function testSignature() public {
uint privateKey = 123;
address pubKey = vm.addr(privateKey);
bytes32 messageHash = keccak256("secret message");
(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, messageHash);
address signer = ecrecover(messageHash, v, r, s);
assertEq(pubKey, signer);
bytes32 invalidMessageHash = keccak256("invalid Message");
signer = ecrecover(invalidMessageHash, v, r, s);
assertTrue(signer != pubKey);
}
}