-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSignatureValidator.sol
More file actions
39 lines (33 loc) · 1.25 KB
/
SignatureValidator.sol
File metadata and controls
39 lines (33 loc) · 1.25 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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
library SignatureValidator {
error IncorrectSignature(address expectedAddress, bytes32 usedHash, bytes signature);
error ZeroAddress();
/**
@dev Verfies signature against address
@param addr The signing address
@param quoteHash The hash of the signed data
@param signature The signature containing v, r and s
@return True if the signature is valid, false otherwise.
*/
function verify(address addr, bytes32 quoteHash, bytes memory signature) public pure returns (bool) {
if (addr == address(0)) {
revert ZeroAddress();
}
if (signature.length != 65) {
revert IncorrectSignature(addr, quoteHash, signature);
}
bytes32 r;
bytes32 s;
uint8 v;
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
// TODO use EIP712 compatible format instead
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHash = keccak256(abi.encodePacked(prefix, quoteHash));
return ecrecover(prefixedHash, v, r, s) == addr;
}
}