-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcdsaHelperLib.sol
More file actions
64 lines (60 loc) · 2.43 KB
/
Copy pathEcdsaHelperLib.sol
File metadata and controls
64 lines (60 loc) · 2.43 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
import { ECDSA } from "solady/utils/ECDSA.sol";
/**
* @title EcdsaHelperLib
* @notice A library that provides helper functions for ECDSA operations
* @author Biconomy
*/
library EcdsaHelperLib {
using ECDSA for bytes32;
/**
* @notice Checks if the signature is valid for the given hash and expected signer
* @param expectedSigner The expected signer of the signature
* @param hash The hash of the data to be signed
* @param signature The signature to be validated
* @return bool True if the signature is valid, false otherwise
* @dev Solady ECDSA does not revert on incorrect signatures.
* Instead, it returns address(0) as the recovered address.
* Make sure to never pass address(0) as expectedSigner to this function.
*/
// solhint-disable-next-line gas-named-return-values
function isValidSignature(
address expectedSigner,
bytes32 hash,
bytes memory signature
)
internal
view
returns (bool)
{
if (hash.tryRecover(signature) == expectedSigner) return true;
if (hash.toEthSignedMessageHash().tryRecover(signature) == expectedSigner) return true;
return false;
}
/**
* @notice Returns the keccak256 digest of an EIP-712 typed data (EIP-191 version `0x01`).
* @param domainSeparator The domain separator of the typed data
* @param structHash The struct hash of the typed data
* @return digest The keccak256 digest of the typed data
* @dev Returns the keccak256 digest of an EIP-712 typed data (EIP-191 version `0x01`).
*
* The digest is calculated from a `domainSeparator` and a `structHash`, by prefixing them with
* `\x19\x01` and hashing the result. It corresponds to the hash signed by the
* https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`] JSON-RPC method as part of EIP-712.
*
* See {ECDSA-recover}.
*/
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 digest) {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, hex"1901")
mstore(add(ptr, 0x02), domainSeparator)
mstore(add(ptr, 0x22), structHash)
digest := keccak256(ptr, 0x42)
// restore free memory ptr
mstore(0x40, add(ptr, 0x42))
}
}
}