SignerWebAuthn.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.24;
  3. import {SignerP256} from "./SignerP256.sol";
  4. import {WebAuthn} from "../WebAuthn.sol";
  5. /**
  6. * @dev Implementation of {SignerP256} that supports WebAuthn authentication assertions.
  7. *
  8. * This contract enables signature validation using WebAuthn authentication assertions,
  9. * leveraging the P256 public key stored in the contract. It allows for both WebAuthn
  10. * and raw P256 signature validation, providing compatibility with both signature types.
  11. *
  12. * The signature is expected to be an abi-encoded {WebAuthn-WebAuthnAuth} struct.
  13. *
  14. * Example usage:
  15. *
  16. * ```solidity
  17. * contract MyAccountWebAuthn is Account, SignerWebAuthn, Initializable {
  18. * function initialize(bytes32 qx, bytes32 qy) public initializer {
  19. * _setSigner(qx, qy);
  20. * }
  21. * }
  22. * ```
  23. *
  24. * IMPORTANT: Failing to call {_setSigner} either during construction (if used standalone)
  25. * or during initialization (if used as a clone) may leave the signer either front-runnable or unusable.
  26. */
  27. abstract contract SignerWebAuthn is SignerP256 {
  28. /**
  29. * @dev Validates a raw signature using the WebAuthn authentication assertion.
  30. *
  31. * In case the signature can't be validated, it falls back to the
  32. * {SignerP256-_rawSignatureValidation} method for raw P256 signature validation by passing
  33. * the raw `r` and `s` values from the signature.
  34. */
  35. function _rawSignatureValidation(
  36. bytes32 hash,
  37. bytes calldata signature
  38. ) internal view virtual override returns (bool) {
  39. (bytes32 qx, bytes32 qy) = signer();
  40. (bool decodeSuccess, WebAuthn.WebAuthnAuth calldata auth) = WebAuthn.tryDecodeAuth(signature);
  41. return
  42. decodeSuccess
  43. ? WebAuthn.verify(abi.encodePacked(hash), auth, qx, qy)
  44. : super._rawSignatureValidation(hash, signature);
  45. }
  46. }