MessageHashUtils.sol 3.7 KB

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