-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracting UserOperationLib from UserOpertaion.sol and moving to core/
- Loading branch information
Showing
8 changed files
with
98 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity ^0.8.12; | ||
|
||
import "../interfaces/UserOperation.sol"; | ||
import {calldataKeccak} from "./Helpers.sol"; | ||
|
||
/** | ||
* Utility functions helpful when working with UserOperation structs. | ||
*/ | ||
library UserOperationLib { | ||
/** | ||
* Get sender from user operation data. | ||
* @param userOp - The user operation data. | ||
*/ | ||
function getSender( | ||
UserOperation calldata userOp | ||
) internal pure returns (address) { | ||
address data; | ||
//read sender from userOp, which is first userOp member (saves 800 gas...) | ||
assembly { | ||
data := calldataload(userOp) | ||
} | ||
return address(uint160(data)); | ||
} | ||
|
||
/** | ||
* Relayer/block builder might submit the TX with higher priorityFee, | ||
* but the user should not pay above what he signed for. | ||
* @param userOp - The user operation data. | ||
*/ | ||
function gasPrice( | ||
UserOperation calldata userOp | ||
) internal view returns (uint256) { | ||
unchecked { | ||
uint256 maxFeePerGas = userOp.maxFeePerGas; | ||
uint256 maxPriorityFeePerGas = userOp.maxPriorityFeePerGas; | ||
if (maxFeePerGas == maxPriorityFeePerGas) { | ||
//legacy mode (for networks that don't support basefee opcode) | ||
return maxFeePerGas; | ||
} | ||
return min(maxFeePerGas, maxPriorityFeePerGas + block.basefee); | ||
} | ||
} | ||
|
||
/** | ||
* Pack the user operation data into bytes for hashing. | ||
* @param userOp - The user operation data. | ||
*/ | ||
function pack( | ||
UserOperation calldata userOp | ||
) internal pure returns (bytes memory ret) { | ||
address sender = getSender(userOp); | ||
uint256 nonce = userOp.nonce; | ||
bytes32 hashInitCode = calldataKeccak(userOp.initCode); | ||
bytes32 hashCallData = calldataKeccak(userOp.callData); | ||
uint256 callGasLimit = userOp.callGasLimit; | ||
uint256 verificationGasLimit = userOp.verificationGasLimit; | ||
uint256 preVerificationGas = userOp.preVerificationGas; | ||
uint256 maxFeePerGas = userOp.maxFeePerGas; | ||
uint256 maxPriorityFeePerGas = userOp.maxPriorityFeePerGas; | ||
bytes32 hashPaymasterAndData = calldataKeccak(userOp.paymasterAndData); | ||
|
||
return abi.encode( | ||
sender, nonce, | ||
hashInitCode, hashCallData, | ||
callGasLimit, verificationGasLimit, preVerificationGas, | ||
maxFeePerGas, maxPriorityFeePerGas, | ||
hashPaymasterAndData | ||
); | ||
} | ||
|
||
/** | ||
* Hash the user operation data. | ||
* @param userOp - The user operation data. | ||
*/ | ||
function hash( | ||
UserOperation calldata userOp | ||
) internal pure returns (bytes32) { | ||
return keccak256(pack(userOp)); | ||
} | ||
|
||
/** | ||
* The minimum of two numbers. | ||
* @param a - First number. | ||
* @param b - Second number. | ||
*/ | ||
function min(uint256 a, uint256 b) internal pure returns (uint256) { | ||
return a < b ? a : b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters