|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.24; |
| 3 | + |
| 4 | +import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol"; |
| 5 | +import {UserOperation, SIG_VALIDATION_FAILED} from "./UserOperation.sol"; |
| 6 | +import {IEntryPoint} from "./interfaces/IEntryPoint.sol"; |
| 7 | + |
| 8 | +/// @notice Minimal ERC-4337-style account secured by a P-256 pubkey (raw r,s signatures). |
| 9 | +/// @dev Uses Nibiru RIP-7212 precompile at 0x...0100. Signature format: abi.encode(r,s). |
| 10 | +contract PasskeyAccount { |
| 11 | + IEntryPoint public entryPoint; |
| 12 | + bytes32 public qx; |
| 13 | + bytes32 public qy; |
| 14 | + uint256 public nonce; |
| 15 | + bool private initialized; |
| 16 | + |
| 17 | + event Executed(address indexed target, uint256 value, bytes data); |
| 18 | + |
| 19 | + modifier onlyEntryPoint() { |
| 20 | + require(msg.sender == address(entryPoint), "not entrypoint"); |
| 21 | + _; |
| 22 | + } |
| 23 | + |
| 24 | + function initialize(address _entryPoint, bytes32 _qx, bytes32 _qy) external { |
| 25 | + require(!initialized, "initialized"); |
| 26 | + require(_entryPoint != address(0), "entrypoint=0"); |
| 27 | + initialized = true; |
| 28 | + entryPoint = IEntryPoint(_entryPoint); |
| 29 | + qx = _qx; |
| 30 | + qy = _qy; |
| 31 | + } |
| 32 | + |
| 33 | + receive() external payable {} |
| 34 | + |
| 35 | + function validateUserOp( |
| 36 | + UserOperation calldata userOp, |
| 37 | + bytes32 userOpHash, |
| 38 | + uint256 missingAccountFunds |
| 39 | + ) external onlyEntryPoint returns (uint256 validationData) { |
| 40 | + if (userOp.nonce != nonce) return SIG_VALIDATION_FAILED; |
| 41 | + uint256 verified = _verify(userOpHash, userOp.signature); |
| 42 | + if (verified != 1) return SIG_VALIDATION_FAILED; |
| 43 | + nonce++; |
| 44 | + if (missingAccountFunds > 0) { |
| 45 | + entryPoint.depositTo{value: missingAccountFunds}(address(this)); |
| 46 | + } |
| 47 | + return 0; |
| 48 | + } |
| 49 | + |
| 50 | + function execute(address to, uint256 value, bytes calldata data) external onlyEntryPoint { |
| 51 | + (bool ok, ) = to.call{value: value}(data); |
| 52 | + require(ok, "exec failed"); |
| 53 | + emit Executed(to, value, data); |
| 54 | + } |
| 55 | + |
| 56 | + function _verify(bytes32 userOpHash, bytes calldata signature) internal view returns (uint256) { |
| 57 | + bytes32 digest; |
| 58 | + bytes32 r; |
| 59 | + bytes32 s; |
| 60 | + |
| 61 | + // For Node/EVM tests we accept compact r,s signatures over userOpHash. |
| 62 | + if (signature.length == 64) { |
| 63 | + (r, s) = abi.decode(signature, (bytes32, bytes32)); |
| 64 | + digest = sha256(abi.encodePacked(userOpHash)); |
| 65 | + } else { |
| 66 | + // WebAuthn signatures are over sha256(authenticatorData || sha256(clientDataJSON)). |
| 67 | + // Signature payload layout: abi.encode(authenticatorData, clientDataJSON, r, s) |
| 68 | + (bytes memory authData, bytes memory clientDataJSON, bytes32 rFull, bytes32 sFull) = |
| 69 | + abi.decode(signature, (bytes, bytes, bytes32, bytes32)); |
| 70 | + r = rFull; |
| 71 | + s = sFull; |
| 72 | + digest = sha256(abi.encodePacked(authData, sha256(clientDataJSON))); |
| 73 | + } |
| 74 | + |
| 75 | + bytes memory input = abi.encodePacked(digest, r, s, qx, qy); |
| 76 | + (bool ok, bytes memory out) = address(0x100).staticcall(input); |
| 77 | + if (!ok || out.length != 32) { |
| 78 | + return 0; |
| 79 | + } |
| 80 | + return uint256(bytes32(out)); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// @notice Factory deploying cheap PasskeyAccount minimal-proxy clones. |
| 85 | +contract PasskeyAccountFactory { |
| 86 | + address public immutable IMPLEMENTATION; |
| 87 | + address public immutable ENTRY_POINT; |
| 88 | + |
| 89 | + event AccountCreated(address indexed account, bytes32 qx, bytes32 qy, bytes32 salt); |
| 90 | + |
| 91 | + constructor(address _entryPoint) { |
| 92 | + ENTRY_POINT = _entryPoint; |
| 93 | + IMPLEMENTATION = address(new PasskeyAccount()); |
| 94 | + } |
| 95 | + |
| 96 | + function createAccount(bytes32 _qx, bytes32 _qy) external returns (address account) { |
| 97 | + bytes32 salt = _salt(_qx, _qy); |
| 98 | + account = Clones.cloneDeterministic(IMPLEMENTATION, salt); |
| 99 | + PasskeyAccount(payable(account)).initialize(ENTRY_POINT, _qx, _qy); |
| 100 | + emit AccountCreated(account, _qx, _qy, salt); |
| 101 | + } |
| 102 | + |
| 103 | + function accountAddress(bytes32 _qx, bytes32 _qy) external view returns (address predicted) { |
| 104 | + bytes32 salt = _salt(_qx, _qy); |
| 105 | + predicted = Clones.predictDeterministicAddress(IMPLEMENTATION, salt, address(this)); |
| 106 | + } |
| 107 | + |
| 108 | + function _salt(bytes32 _qx, bytes32 _qy) internal pure returns (bytes32) { |
| 109 | + return keccak256(abi.encodePacked(_qx, _qy)); |
| 110 | + } |
| 111 | +} |
0 commit comments