SignerP256.sol 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0-rc.0) (utils/cryptography/signers/SignerP256.sol)
  3. pragma solidity ^0.8.20;
  4. import {AbstractSigner} from "./AbstractSigner.sol";
  5. import {P256} from "../P256.sol";
  6. /**
  7. * @dev Implementation of {AbstractSigner} using xref:api:utils/cryptography#P256[P256] signatures.
  8. *
  9. * For {Account} usage, a {_setSigner} function is provided to set the {signer} public key.
  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 MyAccountP256 is Account, SignerP256, Initializable {
  16. * function initialize(bytes32 qx, bytes32 qy) public initializer {
  17. * _setSigner(qx, qy);
  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 SignerP256 is AbstractSigner {
  26. bytes32 private _qx;
  27. bytes32 private _qy;
  28. error SignerP256InvalidPublicKey(bytes32 qx, bytes32 qy);
  29. /**
  30. * @dev Sets the signer with a P256 public key. This function should be called during construction
  31. * or through an initializer.
  32. */
  33. function _setSigner(bytes32 qx, bytes32 qy) internal {
  34. if (!P256.isValidPublicKey(qx, qy)) revert SignerP256InvalidPublicKey(qx, qy);
  35. _qx = qx;
  36. _qy = qy;
  37. }
  38. /// @dev Return the signer's P256 public key.
  39. function signer() public view virtual returns (bytes32 qx, bytes32 qy) {
  40. return (_qx, _qy);
  41. }
  42. /// @inheritdoc AbstractSigner
  43. function _rawSignatureValidation(
  44. bytes32 hash,
  45. bytes calldata signature
  46. ) internal view virtual override returns (bool) {
  47. if (signature.length < 0x40) return false;
  48. bytes32 r = bytes32(signature[0x00:0x20]);
  49. bytes32 s = bytes32(signature[0x20:0x40]);
  50. (bytes32 qx, bytes32 qy) = signer();
  51. return P256.verify(hash, r, s, qx, qy);
  52. }
  53. }