SignerECDSA.sol 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. constructor(address signerAddr) {
  27. _setSigner(signerAddr);
  28. }
  29. /**
  30. * @dev Sets the signer with the address of the native signer. This function should be called during construction
  31. * or through an initializer.
  32. */
  33. function _setSigner(address signerAddr) internal {
  34. _signer = signerAddr;
  35. }
  36. /// @dev Return the signer's address.
  37. function signer() public view virtual returns (address) {
  38. return _signer;
  39. }
  40. /// @inheritdoc AbstractSigner
  41. function _rawSignatureValidation(
  42. bytes32 hash,
  43. bytes calldata signature
  44. ) internal view virtual override returns (bool) {
  45. (address recovered, ECDSA.RecoverError err, ) = ECDSA.tryRecover(hash, signature);
  46. return signer() == recovered && err == ECDSA.RecoverError.NoError;
  47. }
  48. }