SignerECDSA.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0) (utils/cryptography/signers/SignerECDSA.sol)
  3. pragma solidity ^0.8.20;
  4. import {AbstractSigner} from "./AbstractSigner.sol";
  5. import {ECDSA} from "../ECDSA.sol";
  6. /**
  7. * @dev Implementation of {AbstractSigner} using xref:api:utils/cryptography#ECDSA[ECDSA] signatures.
  8. *
  9. * For {Account} usage, a {_setSigner} function is provided to set the {signer} address.
  10. * Doing so is easier for a factory, who is likely to use initializable clones of this contract.
  11. *
  12. * Example of usage:
  13. *
  14. * ```solidity
  15. * contract MyAccountECDSA is Account, SignerECDSA, Initializable {
  16. * function initialize(address signerAddr) public initializer {
  17. * _setSigner(signerAddr);
  18. * }
  19. * }
  20. * ```
  21. *
  22. * IMPORTANT: Failing to call {_setSigner} either during construction (if used standalone)
  23. * or during initialization (if used as a clone) may leave the signer either front-runnable or unusable.
  24. */
  25. abstract contract SignerECDSA is AbstractSigner {
  26. address private _signer;
  27. constructor(address signerAddr) {
  28. _setSigner(signerAddr);
  29. }
  30. /**
  31. * @dev Sets the signer with the address of the native signer. This function should be called during construction
  32. * or through an initializer.
  33. */
  34. function _setSigner(address signerAddr) internal {
  35. _signer = signerAddr;
  36. }
  37. /// @dev Return the signer's address.
  38. function signer() public view virtual returns (address) {
  39. return _signer;
  40. }
  41. /// @inheritdoc AbstractSigner
  42. function _rawSignatureValidation(
  43. bytes32 hash,
  44. bytes calldata signature
  45. ) internal view virtual override returns (bool) {
  46. (address recovered, ECDSA.RecoverError err, ) = ECDSA.tryRecover(hash, signature);
  47. return signer() == recovered && err == ECDSA.RecoverError.NoError;
  48. }
  49. }