|
| 1 | +// SPDX-License-Identifier: GPL-3.0 |
| 2 | +pragma solidity 0.8.20; |
| 3 | + |
| 4 | +import "@account-abstraction/core/BasePaymaster.sol"; |
| 5 | +import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; |
| 6 | +import "./BaseFundedPaymaster.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * A paymaster that uses external service to decide whether to pay for the UserOp. |
| 10 | + * The paymaster trusts an external signer to sign the transaction. |
| 11 | + * The calling user must pass the UserOp to that external signer first, which performs |
| 12 | + * whatever off-chain verification before signing the UserOp.\ |
| 13 | + * Actual funding is provided by a meta-paymaster. |
| 14 | + */ |
| 15 | +contract FundedPaymaster is BaseFundedPaymaster { |
| 16 | + using UserOperationLib for UserOperation; |
| 17 | + |
| 18 | + address public immutable verifyingSigner; |
| 19 | + |
| 20 | + uint256 private constant VALID_TIMESTAMP_OFFSET = 20; |
| 21 | + uint256 private constant SIGNATURE_OFFSET = VALID_TIMESTAMP_OFFSET + 64; |
| 22 | + |
| 23 | + constructor(IEntryPoint _entryPoint, MetaPaymaster _metaPaymaster, address _verifyingSigner) BaseFundedPaymaster(_entryPoint, _metaPaymaster) { |
| 24 | + verifyingSigner = _verifyingSigner; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * return the hash we're going to sign off-chain (and validate on-chain) |
| 29 | + * this method is called by the off-chain service, to sign the request. |
| 30 | + * it is called on-chain from the validatePaymasterUserOp, to validate the signature. |
| 31 | + * note that this signature covers all fields of the UserOperation, except the "paymasterAndData", |
| 32 | + * which will carry the signature itself. |
| 33 | + */ |
| 34 | + function getHash(UserOperation calldata userOp, uint48 validUntil, uint48 validAfter) |
| 35 | + public view returns (bytes32) { |
| 36 | + // can't use userOp.hash(), since it contains also the paymasterAndData itself. |
| 37 | + return keccak256( |
| 38 | + abi.encode( |
| 39 | + userOp.getSender(), |
| 40 | + userOp.nonce, |
| 41 | + calldataKeccak(userOp.initCode), |
| 42 | + calldataKeccak(userOp.callData), |
| 43 | + userOp.callGasLimit, |
| 44 | + userOp.verificationGasLimit, |
| 45 | + userOp.preVerificationGas, |
| 46 | + userOp.maxFeePerGas, |
| 47 | + userOp.maxPriorityFeePerGas, |
| 48 | + block.chainid, |
| 49 | + address(this), |
| 50 | + validUntil, |
| 51 | + validAfter |
| 52 | + ) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * verify our external signer signed this request. |
| 58 | + * the "paymasterAndData" is expected to be the paymaster and a signature over the entire request params |
| 59 | + * paymasterAndData[:20] : address(this) |
| 60 | + * paymasterAndData[20:84] : abi.encode(validUntil, validAfter) |
| 61 | + * paymasterAndData[84:] : signature |
| 62 | + */ |
| 63 | + function __validatePaymasterUserOp(UserOperation calldata userOp, bytes32 /*userOpHash*/, uint256 /*requiredPreFund*/) |
| 64 | + internal override view returns (uint256) { |
| 65 | + (uint48 validUntil, uint48 validAfter, bytes calldata signature) = parsePaymasterAndData(userOp.paymasterAndData); |
| 66 | + // Only support 65-byte signatures, to avoid potential replay attacks. |
| 67 | + require(signature.length == 65, "Paymaster: invalid signature length in paymasterAndData"); |
| 68 | + bytes32 hash = ECDSA.toEthSignedMessageHash(getHash(userOp, validUntil, validAfter)); |
| 69 | + |
| 70 | + // don't revert on signature failure: return SIG_VALIDATION_FAILED |
| 71 | + if (verifyingSigner != ECDSA.recover(hash, signature)) { |
| 72 | + return _packValidationData(true, validUntil, validAfter); |
| 73 | + } |
| 74 | + |
| 75 | + // no need for other on-chain validation: entire UserOp should have been checked |
| 76 | + // by the external service prior to signing it. |
| 77 | + return _packValidationData(false, validUntil, validAfter); |
| 78 | + } |
| 79 | + |
| 80 | + function parsePaymasterAndData(bytes calldata paymasterAndData) |
| 81 | + internal pure returns(uint48 validUntil, uint48 validAfter, bytes calldata signature) { |
| 82 | + (validUntil, validAfter) = abi.decode(paymasterAndData[VALID_TIMESTAMP_OFFSET:SIGNATURE_OFFSET],(uint48, uint48)); |
| 83 | + signature = paymasterAndData[SIGNATURE_OFFSET:]; |
| 84 | + } |
| 85 | + |
| 86 | + receive() external payable { |
| 87 | + // use address(this).balance rather than msg.value in case of force-send |
| 88 | + (bool callSuccess, ) = payable(address(entryPoint)).call{value: address(this).balance}(""); |
| 89 | + require(callSuccess, "Deposit failed"); |
| 90 | + } |
| 91 | +} |
0 commit comments