Skip to content

Commit

Permalink
mitigation: check onERC1155Received() (#28)
Browse files Browse the repository at this point in the history
* check `onERC1155Received()`

* fix redeclared vars
  • Loading branch information
0xble authored Aug 12, 2024
1 parent ade3e05 commit 9a7c940
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
37 changes: 25 additions & 12 deletions src/MintERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,38 @@ contract MintERC1155 is ERC1155Upgradeable, OwnableUpgradeable, ERC2981Upgradeab
* @notice Check if the given address can receive tokens from this contract
* @param to Address to check if receiving tokens is safe
*/
function safeBatchTransferAcceptanceCheckOnMint(address to) external view returns (bool) {
function safeTransferAcceptanceCheckOnMint(address to) external view returns (bool) {
if (to.code.length == 0) return true;

(bool success, bytes memory res) = to.staticcall{ gas: 400_000 }(
abi.encodeCall(IERC1155Receiver.onERC1155Received, (MINTER, address(0), 1, 1, ""))
);
if (success) {
bytes4 response = abi.decode(res, (bytes4));
if (response != IERC1155Receiver.onERC1155Received.selector) {
return false;
}
} else {
return false;
}

uint256[] memory idOrAmountArray = new uint256[](1);
idOrAmountArray[0] = 1;

bytes memory callData = abi.encodeCall(
IERC1155Receiver.onERC1155BatchReceived, (MINTER, address(0), idOrAmountArray, idOrAmountArray, "")
(success, res) = to.staticcall{ gas: 400_000 }(
abi.encodeCall(
IERC1155Receiver.onERC1155BatchReceived, (MINTER, address(0), idOrAmountArray, idOrAmountArray, "")
)
);

if (to.code.length > 0) {
(bool success, bytes memory res) = to.staticcall{ gas: 400_000 }(callData);
if (success) {
bytes4 response = abi.decode(res, (bytes4));
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
return false;
}
} else {
if (success) {
bytes4 response = abi.decode(res, (bytes4));
if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
return false;
}
} else {
return false;
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NFTMint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ contract NFTMint is Ownable {
}
}

if (!mint.safeBatchTransferAcceptanceCheckOnMint(msg.sender)) {
if (!mint.safeTransferAcceptanceCheckOnMint(msg.sender)) {
revert NFTMint_BuyerNotAcceptingERC1155();
}

Expand Down

0 comments on commit 9a7c940

Please sign in to comment.