SignerECDSA.sol 1.7 KB

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