-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathSignatureValidator.sol
87 lines (76 loc) · 3.15 KB
/
SignatureValidator.sol
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "../authority/OwnerAuth.sol";
import "../base/Validator.sol";
import "../libraries/Errors.sol";
import "../libraries/TypeConversion.sol";
import "../libraries/SignatureDecoder.sol";
/**
* @title SignatureValidator
* @dev This contract provides functionality for validating cryptographic signatures
*/
abstract contract SignatureValidator is OwnerAuth, Validator {
using ECDSA for bytes32;
using TypeConversion for address;
/**
* @dev Encodes the raw hash using a validator to prevent replay attacks
* If the same owner signs the message for different smart contract accounts,
* this function uses EIP-712-like encoding to encode the raw hash
* @param rawHash The raw hash to encode
* @return encodeRawHash The encoded hash
*/
function _encodeRawHash(bytes32 rawHash) internal view returns (bytes32 encodeRawHash) {
return validator().encodeRawHash(rawHash);
}
/**
* @dev Validates an EIP1271 signature
* @param rawHash The raw hash against which the signature is to be checked
* @param rawSignature The signature to validate
* @return validationData The data used for validation
* @return sigValid A boolean indicating if the signature is valid or not
*/
function _isValidate1271Signature(bytes32 rawHash, bytes calldata rawSignature)
internal
view
returns (uint256 validationData, bool sigValid)
{
bytes32 recovered;
bool success;
bytes calldata guardHookInputData;
bytes calldata validatorSignature;
(guardHookInputData, validatorSignature) = SignatureDecoder.decodeSignature(rawSignature);
// To prevent potential attacks, prohibit the use of guardHookInputData with EIP1271 signatures.
require(guardHookInputData.length == 0);
(validationData, recovered, success) = validator().recover1271Signature(rawHash, validatorSignature);
if (!success) {
sigValid = false;
} else {
sigValid = _isOwner(recovered);
}
}
/**
* @dev Validates a user operation signature
* @param userOpHash The hash of the user operation
* @param userOpSignature The signature of the user operation
* @return validationData same as defined in EIP4337
* @return sigValid A boolean indicating if the signature is valid or not
* @return guardHookInputData Input data for the guard hook
*/
function _isValidUserOp(bytes32 userOpHash, bytes calldata userOpSignature)
internal
view
returns (uint256 validationData, bool sigValid, bytes calldata guardHookInputData)
{
bytes32 recovered;
bool success;
bytes calldata validatorSignature;
(guardHookInputData, validatorSignature) = SignatureDecoder.decodeSignature(userOpSignature);
(validationData, recovered, success) = validator().recoverSignature(userOpHash, validatorSignature);
if (!success) {
sigValid = false;
} else {
sigValid = _isOwner(recovered);
}
}
}