Skip to content

Commit

Permalink
modified paymaster
Browse files Browse the repository at this point in the history
  • Loading branch information
ABD-AZE committed Oct 19, 2024
1 parent 3ff142c commit c961386
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
1. Frontend Social media app for demo
2. Our Main frontend (iske through companies hume contact krengi to show their ads and pay us, this money would go into the main account which funds the paymaster) (what percentage of the money should remain in our paymaster account and what percentage should go in EntryPoint.sol?)
3. Initcode contains the factory contract address
4.
4. One Ad will be shown
2 changes: 1 addition & 1 deletion cache/solidity-files-cache.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion out/ZapAccount.sol/ZapAccount.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion out/ZapAccountFactory.sol/ZapAccountFactory.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion out/build-info/3709585d08f2f9f5968b96af0abd61e7.json

This file was deleted.

17 changes: 17 additions & 0 deletions script/ZapAccountFactory.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import "lib/forge-std/src/Script.sol";
import "lib/account-abstraction/contracts/core/EntryPoint.sol";
import "../src/ZapAccountFactory.sol";
contract MyZapAccountFactory is Script{
EntryPoint private immutable i_entrypoint;
constructor(EntryPoint entrypoint) {
i_entrypoint=entrypoint;
}
function run() external {
vm.startBroadcast();
ZapAccountFactory factory = new ZapAccountFactory(i_entrypoint);
vm.stopBroadcast();
}
}
99 changes: 66 additions & 33 deletions src/ZapPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import "lib/account-abstraction/contracts/interfaces/PackedUserOperation.sol";
import "node_modules/@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "node_modules/@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import "lib/account-abstraction/contracts/core/Helpers.sol";

contract ZapPayMaster {
contract ZapPayMaster {
uint256 public constant PAYMASTER_DATA_OFFSET = 52;
uint256 private constant VALID_TIMESTAMP_OFFSET = PAYMASTER_DATA_OFFSET;
uint256 private constant SIGNATURE_OFFSET = VALID_TIMESTAMP_OFFSET + 64;
Expand All @@ -26,52 +25,86 @@ contract ZapPayMaster {
_;
}

function validatePaymasterUserOp(PackedUserOperation calldata userOp, bytes32 userOpHash, uint256 maxCost)
function validatePaymasterUserOp(
PackedUserOperation calldata userOp,
bytes32 userOpHash,
uint256 maxCost
)
external
view
onlyEntryPoint
returns (bytes memory context, uint256 validationData)
{
(context, validationData) = _validatePaymasterUserOp(userOp, userOpHash);
(context, validationData) = _validatePaymasterUserOp(
userOp,
userOpHash
);
return (context, validationData);
}

function _validatePaymasterUserOp(PackedUserOperation calldata userOp, bytes32 userOpHash)
internal
view
returns (bytes memory context, uint256 validationData)
{
(uint48 validUntil, uint48 validAfter, bytes calldata signature) =
parsePaymasterAndData(userOp.paymasterAndData);
require(signature.length == 64 || signature.length == 65, "invalid signature length");
bytes32 hash = MessageHashUtils.toEthSignedMessageHash(getHash(userOp, validUntil, validAfter));
if (verifyingSigner != ECDSA.recover(hash, signature)) {
function _validatePaymasterUserOp(
PackedUserOperation calldata userOp,
bytes32 userOpHash
) internal view returns (bytes memory context, uint256 validationData) {
(
uint48 validUntil,
uint48 validAfter,
bytes calldata signature
) = parsePaymasterAndData(userOp.paymasterAndData);
require(
signature.length == 64 || signature.length == 65,
"invalid signature length"
);
bytes32 hash = MessageHashUtils.toEthSignedMessageHash(
getHash(userOp, validUntil, validAfter)
);
if (i_owner != ECDSA.recover(hash, signature)) {
return ("", _packValidationData(true, validUntil, validAfter));
}
return ("", _packValidationData(false, validUntil, validAfter));
}

function getHash(PackedUserOperation calldata userOp, uint48 validUntil, uint48 validAfter)
function getHash(
PackedUserOperation calldata userOp,
uint48 validUntil,
uint48 validAfter
) public view returns (bytes32) {
address sender = userOp.sender;
return
keccak256(
abi.encode(
sender,
userOp.nonce,
keccak256(userOp.initCode),
keccak256(userOp.callData),
userOp.accountGasLimits,
uint256(
bytes32(
userOp
.paymasterAndData[PAYMASTER_VALIDATION_GAS_OFFSET:PAYMASTER_DATA_OFFSET]
)
),
userOp.preVerificationGas,
userOp.gasFees,
block.chainid,
address(this),
validUntil,
validAfter
)
);
}
function parsePaymasterAndData(
bytes calldata paymasterAndData
)
public
view
returns (bytes32)
pure
returns (uint48 validUntil, uint48 validAfter, bytes calldata signature)
{
address sender = userOp.sender;
return keccak256(
abi.encode(
sender,
userOp.nonce,
keccak256(userOp.initCode),
keccak256(userOp.callData),
userOp.accountGasLimits,
uint256(bytes32(userOp.paymasterAndData[PAYMASTER_VALIDATION_GAS_OFFSET:PAYMASTER_DATA_OFFSET])),
userOp.preVerificationGas,
userOp.gasFees,
block.chainid,
address(this),
validUntil,
validAfter
)
(validUntil, validAfter) = abi.decode(
paymasterAndData[VALID_TIMESTAMP_OFFSET:],
(uint48, uint48)
);
signature = paymasterAndData[SIGNATURE_OFFSET:];
}

}

0 comments on commit c961386

Please sign in to comment.