-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/chainlight remediations #81
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e7ce3ea
chainlight remediations
livingrockrises 0d05691
Merge branch 'develop' into fix/chainlight-remediations
livingrockrises e79673a
gas optimisation
livingrockrises 5b4827a
add priceUpdateThreshold per oracle + update deployment script
livingrockrises 50da798
fix test cases
livingrockrises 902146b
fetch token's decimals directly + update the test cases
livingrockrises feb42a8
respond to PR comments
livingrockrises 8c4caf6
fix: math lib respond to PR comments
livingrockrises fd750fe
notes for unaccounted overhead values + update storage to memory
livingrockrises 5d2988a
Merge pull request #82 from bcnmy/zellic-remediation-3.1
livingrockrises 4b59605
Merge branch 'develop' into fix/chainlight-remediations
livingrockrises 8969f36
Merge branch 'develop' into fix/chainlight-remediations
livingrockrises 5d95cd6
respond to PR comments
livingrockrises File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | |
import "@account-abstraction/contracts/core/Helpers.sol" as Helpers; | ||
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; | ||
import "../utils/SafeTransferLib.sol"; | ||
import {MathLib} from "../libs/MathLib.sol"; | ||
import {TokenPaymasterErrors} from "./TokenPaymasterErrors.sol"; | ||
import "@openzeppelin/contracts/utils/Address.sol"; | ||
import {OracleAggregator} from "./oracles/OracleAggregator.sol"; | ||
|
@@ -77,6 +78,10 @@ contract BiconomyTokenPaymaster is | |
} | ||
} | ||
|
||
receive() external payable { | ||
emit Received(msg.sender, msg.value); | ||
} | ||
|
||
/** | ||
* @dev Set a new verifying signer address. | ||
* Can only be called by the owner of the contract. | ||
|
@@ -178,11 +183,12 @@ contract BiconomyTokenPaymaster is | |
onlyOwner | ||
nonReentrant | ||
{ | ||
if (token.length != amount.length) { | ||
uint256 tokLen = token.length; | ||
if (tokLen != amount.length) { | ||
revert TokensAndAmountsLengthMismatch(); | ||
} | ||
unchecked { | ||
for (uint256 i; i < token.length;) { | ||
for (uint256 i; i < tokLen;) { | ||
_withdrawERC20(token[i], target, amount[i]); | ||
++i; | ||
} | ||
|
@@ -196,7 +202,8 @@ contract BiconomyTokenPaymaster is | |
*/ | ||
function withdrawMultipleERC20Full(IERC20[] calldata token, address target) public payable onlyOwner nonReentrant { | ||
unchecked { | ||
for (uint256 i; i < token.length;) { | ||
uint256 tokLen = token.length; | ||
for (uint256 i; i < tokLen;) { | ||
uint256 amount = token[i].balanceOf(address(this)); | ||
_withdrawERC20(token[i], target, amount); | ||
++i; | ||
|
@@ -283,71 +290,6 @@ contract BiconomyTokenPaymaster is | |
signature = paymasterAndData[VALID_PND_OFFSET + 53:]; | ||
} | ||
|
||
function _getRequiredPrefund(UserOperation calldata userOp) internal view returns (uint256 requiredPrefund) { | ||
unchecked { | ||
uint256 requiredGas = | ||
userOp.callGasLimit + userOp.verificationGasLimit + userOp.preVerificationGas + unaccountedEPGasOverhead; | ||
|
||
requiredPrefund = requiredGas * userOp.maxFeePerGas; | ||
} | ||
} | ||
|
||
/** | ||
* @dev Verify that an external signer signed the paymaster data of a user operation. | ||
* The paymaster data is expected to be the paymaster address, request data and a signature over the entire request parameters. | ||
* paymasterAndData: hexConcat([paymasterAddress, priceSource, abi.encode(validUntil, validAfter, feeToken, exchangeRate, priceMarkup), signature]) | ||
* @param userOp The UserOperation struct that represents the current user operation. | ||
* userOpHash The hash of the UserOperation struct. | ||
* @param requiredPreFund The required amount of pre-funding for the paymaster. | ||
* @return context A context string returned by the entry point after successful validation. | ||
* @return validationData An integer returned by the entry point after successful validation. | ||
*/ | ||
function _validatePaymasterUserOp(UserOperation calldata userOp, bytes32 userOpHash, uint256 requiredPreFund) | ||
internal | ||
view | ||
override | ||
returns (bytes memory context, uint256 validationData) | ||
{ | ||
(requiredPreFund); | ||
|
||
( | ||
ExchangeRateSource priceSource, | ||
uint48 validUntil, | ||
uint48 validAfter, | ||
address feeToken, | ||
uint128 exchangeRate, | ||
uint32 priceMarkup, | ||
bytes calldata signature | ||
) = parsePaymasterAndData(userOp.paymasterAndData); | ||
|
||
bytes32 _hash = getHash(userOp, priceSource, validUntil, validAfter, feeToken, exchangeRate, priceMarkup) | ||
.toEthSignedMessageHash(); | ||
|
||
//don't revert on signature failure: return SIG_VALIDATION_FAILED | ||
if (verifyingSigner != _hash.recover(signature)) { | ||
// empty context and sigFailed true | ||
return (context, Helpers._packValidationData(true, validUntil, validAfter)); | ||
} | ||
|
||
address account = userOp.getSender(); | ||
|
||
// This model assumes irrespective of priceSource exchangeRate is always sent from outside | ||
// for below checks you would either need maxCost or some exchangeRate | ||
|
||
uint256 btpmRequiredPrefund = _getRequiredPrefund(userOp); | ||
|
||
uint256 tokenRequiredPreFund = (btpmRequiredPrefund * exchangeRate) / 10 ** 18; | ||
require(priceMarkup <= 2e6, "BTPM: price markup percentage too high"); | ||
require( | ||
IERC20(feeToken).balanceOf(account) >= ((tokenRequiredPreFund * priceMarkup) / PRICE_DENOMINATOR), | ||
"BTPM: account does not have enough token balance" | ||
); | ||
|
||
context = abi.encode(account, feeToken, priceSource, exchangeRate, priceMarkup, userOpHash); | ||
|
||
return (context, Helpers._packValidationData(false, validUntil, validAfter)); | ||
} | ||
|
||
/** | ||
* @dev Executes the paymaster's payment conditions | ||
* @param mode tells whether the op succeeded, reverted, or if the op succeeded but cause the postOp to revert | ||
|
@@ -360,11 +302,13 @@ contract BiconomyTokenPaymaster is | |
ExchangeRateSource priceSource; | ||
uint128 exchangeRate; | ||
uint32 priceMarkup; | ||
uint256 maxFeePerGas; | ||
uint256 maxPriorityFeePerGas; | ||
bytes32 userOpHash; | ||
assembly ("memory-safe") { | ||
let offset := context.offset | ||
|
||
account := calldataload(context.offset) | ||
account := calldataload(offset) | ||
offset := add(offset, 0x20) | ||
|
||
feeToken := calldataload(offset) | ||
|
@@ -379,6 +323,12 @@ contract BiconomyTokenPaymaster is | |
priceMarkup := calldataload(offset) | ||
offset := add(offset, 0x20) | ||
|
||
maxFeePerGas := calldataload(offset) | ||
offset := add(offset, 0x20) | ||
|
||
maxPriorityFeePerGas := calldataload(offset) | ||
offset := add(offset, 0x20) | ||
|
||
userOpHash := calldataload(offset) | ||
} | ||
|
||
|
@@ -389,11 +339,16 @@ contract BiconomyTokenPaymaster is | |
if (result != 0) effectiveExchangeRate = result; | ||
} | ||
|
||
uint256 effectiveGasPrice = getGasPrice( | ||
maxFeePerGas, | ||
maxPriorityFeePerGas | ||
); | ||
|
||
// We could either touch the state for BASEFEE and calculate based on maxPriorityFee passed (to be added in context along with maxFeePerGas) or just use tx.gasprice | ||
uint256 charge; // Final amount to be charged from user account | ||
{ | ||
uint256 actualTokenCost = | ||
((actualGasCost + (unaccountedEPGasOverhead * tx.gasprice)) * effectiveExchangeRate) / 1e18; | ||
((actualGasCost + (unaccountedEPGasOverhead * effectiveGasPrice)) * effectiveExchangeRate) / 1e18; | ||
charge = ((actualTokenCost * priceMarkup) / PRICE_DENOMINATOR); | ||
} | ||
|
||
|
@@ -423,12 +378,90 @@ contract BiconomyTokenPaymaster is | |
} | ||
} | ||
|
||
function _getRequiredPrefund(UserOperation calldata userOp) internal view returns (uint256 requiredPrefund) { | ||
unchecked { | ||
uint256 requiredGas = | ||
userOp.callGasLimit + userOp.verificationGasLimit + userOp.preVerificationGas + unaccountedEPGasOverhead; | ||
|
||
requiredPrefund = requiredGas * userOp.maxFeePerGas; | ||
} | ||
} | ||
|
||
// Note: do not use this in validation phase | ||
function getGasPrice( | ||
uint256 maxFeePerGas, | ||
uint256 maxPriorityFeePerGas | ||
) internal view returns (uint256) { | ||
if (maxFeePerGas == maxPriorityFeePerGas) { | ||
//legacy mode (for networks that don't support basefee opcode) | ||
return maxFeePerGas; | ||
} | ||
return | ||
MathLib.minuint256( | ||
maxFeePerGas, | ||
maxPriorityFeePerGas + block.basefee | ||
); | ||
} | ||
|
||
/** | ||
* @dev Verify that an external signer signed the paymaster data of a user operation. | ||
* The paymaster data is expected to be the paymaster address, request data and a signature over the entire request parameters. | ||
* paymasterAndData: hexConcat([paymasterAddress, priceSource, abi.encode(validUntil, validAfter, feeToken, exchangeRate, priceMarkup), signature]) | ||
* @param userOp The UserOperation struct that represents the current user operation. | ||
* userOpHash The hash of the UserOperation struct. | ||
* @param requiredPreFund The required amount of pre-funding for the paymaster. | ||
* @return context A context string returned by the entry point after successful validation. | ||
* @return validationData An integer returned by the entry point after successful validation. | ||
*/ | ||
function _validatePaymasterUserOp(UserOperation calldata userOp, bytes32 userOpHash, uint256 requiredPreFund) | ||
internal | ||
view | ||
override | ||
returns (bytes memory context, uint256 validationData) | ||
{ | ||
(requiredPreFund); | ||
|
||
( | ||
ExchangeRateSource priceSource, | ||
uint48 validUntil, | ||
uint48 validAfter, | ||
address feeToken, | ||
uint128 exchangeRate, | ||
uint32 priceMarkup, | ||
bytes calldata signature | ||
) = parsePaymasterAndData(userOp.paymasterAndData); | ||
|
||
bytes32 _hash = getHash(userOp, priceSource, validUntil, validAfter, feeToken, exchangeRate, priceMarkup) | ||
.toEthSignedMessageHash(); | ||
|
||
//don't revert on signature failure: return SIG_VALIDATION_FAILED | ||
if (verifyingSigner != _hash.recover(signature)) { | ||
// empty context and sigFailed true | ||
return (context, Helpers._packValidationData(true, validUntil, validAfter)); | ||
} | ||
|
||
address account = userOp.getSender(); | ||
|
||
// This model assumes irrespective of priceSource exchangeRate is always sent from outside | ||
// for below checks you would either need maxCost or some exchangeRate | ||
|
||
uint256 btpmRequiredPrefund = _getRequiredPrefund(userOp); | ||
|
||
uint256 tokenRequiredPreFund = (btpmRequiredPrefund * exchangeRate) / 10 ** 18; | ||
require(priceMarkup <= 2e6, "BTPM: price markup percentage too high"); | ||
require( | ||
IERC20(feeToken).balanceOf(account) >= ((tokenRequiredPreFund * priceMarkup) / PRICE_DENOMINATOR), | ||
"BTPM: account does not have enough token balance" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To fit into 32 bytes we can use the following require msg: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
); | ||
|
||
context = abi.encode(account, feeToken, priceSource, exchangeRate, priceMarkup, userOp.maxFeePerGas, userOp.maxPriorityFeePerGas, userOpHash); | ||
|
||
return (context, Helpers._packValidationData(false, validUntil, validAfter)); | ||
} | ||
|
||
function _withdrawERC20(IERC20 token, address target, uint256 amount) private { | ||
if (target == address(0)) revert CanNotWithdrawToZeroAddress(); | ||
SafeTransferLib.safeTransfer(address(token), target, amount); | ||
} | ||
|
||
receive() external payable { | ||
emit Received(msg.sender, msg.value); | ||
emit TokensWithdrawn(address(token), target, amount, msg.sender); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure
_amount
should beindexed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can index 3 params and I think it's fine. also out of scope of remediations PR