MessageHashUtils.sol 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.20;
  3. import {Strings} from "../Strings.sol";
  4. /**
  5. * @dev Signature message hash utilities for producing digests to be consumed by {ECDSA} recovery or signing.
  6. *
  7. * The library provides methods for generating a hash of a message that conforms to the
  8. * https://eips.ethereum.org/EIPS/eip-191[EIP 191] and https://eips.ethereum.org/EIPS/eip-712[EIP 712]
  9. * specifications.
  10. */
  11. library MessageHashUtils {
  12. /**
  13. * @dev Returns the keccak256 digest of an EIP-191 signed data with version
  14. * `0x45` (`personal_sign` messages).
  15. *
  16. * The digest is calculated by prefixing a bytes32 `messageHash` with
  17. * `"\x19Ethereum Signed Message:\n32"` and hashing the result. It corresponds with the
  18. * hash signed when using the https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`] JSON-RPC method.
  19. *
  20. * NOTE: The `hash` parameter is intended to be the result of hashing a raw message with
  21. * keccak256, although any bytes32 value can be safely used because the final digest will
  22. * be re-hashed.
  23. *
  24. * See {ECDSA-recover}.
  25. */
  26. function toEthSignedMessageHash(bytes32 messageHash) internal pure returns (bytes32 digest) {
  27. /// @solidity memory-safe-assembly
  28. assembly {
  29. mstore(0x00, "\x19Ethereum Signed Message:\n32") // 32 is the bytes-length of messageHash
  30. mstore(0x1c, messageHash) // 0x1c (28) is the length of the prefix
  31. digest := keccak256(0x00, 0x3c) // 0x3c is the length of the prefix (0x1c) + messageHash (0x20)
  32. }
  33. }
  34. /**
  35. * @dev Returns the keccak256 digest of an EIP-191 signed data with version
  36. * `0x45` (`personal_sign` messages).
  37. *
  38. * The digest is calculated by prefixing an arbitrary `message` with
  39. * `"\x19Ethereum Signed Message:\n" + len(message)` and hashing the result. It corresponds with the
  40. * hash signed when using the https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`] JSON-RPC method.
  41. *
  42. * See {ECDSA-recover}.
  43. */
  44. function toEthSignedMessageHash(bytes memory message) internal pure returns (bytes32 digest) {
  45. return
  46. keccak256(bytes.concat("\x19Ethereum Signed Message:\n", bytes(Strings.toString(message.length)), message));
  47. }
  48. /**
  49. * @dev Returns the keccak256 digest of an EIP-191 signed data with version
  50. * `0x00` (data with intended validator).
  51. *
  52. * The digest is calculated by prefixing an arbitrary `data` with `"\x19\x00"` and the intended
  53. * `validator` address. Then hashing the result.
  54. *
  55. * See {ECDSA-recover}.
  56. */
  57. function toDataWithIntendedValidatorHash(
  58. address validator,
  59. bytes memory data
  60. ) internal pure returns (bytes32 digest) {
  61. return keccak256(abi.encodePacked(hex"19_00", validator, data));
  62. }
  63. /**
  64. * @dev Returns the keccak256 digest of an EIP-712 typed data (EIP-191 version `0x01`).
  65. *
  66. * The digest is calculated from a `domainSeparator` and a `structHash`, by prefixing them with
  67. * `\x19\x01` and hashing the result. It corresponds to the hash signed by the
  68. * https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`] JSON-RPC method as part of EIP-712.
  69. *
  70. * See {ECDSA-recover}.
  71. */
  72. function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 digest) {
  73. /// @solidity memory-safe-assembly
  74. assembly {
  75. let ptr := mload(0x40)
  76. mstore(ptr, hex"19_01")
  77. mstore(add(ptr, 0x02), domainSeparator)
  78. mstore(add(ptr, 0x22), structHash)
  79. digest := keccak256(ptr, 0x42)
  80. }
  81. }
  82. }