-
Notifications
You must be signed in to change notification settings - Fork 9
Add ERC1155Holder to prevent Packs.reveal abuse #40
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
98e9080
Update initialize checks and constructors
ScreamingHawk 4c335de
Optimise runtime bytecode
ScreamingHawk 2946cb2
Add ERC1155Holder to prevent pack reveal abuse
ScreamingHawk 830f5e9
Fix test validation
ScreamingHawk 909eab7
Add gas bomb handling to holder
ScreamingHawk 2cd4f8f
ERC1155Holder always holds
ScreamingHawk 6b6498d
Update gas snapshot
ScreamingHawk 9a9320b
NC-03 Flip _commitment keys
ScreamingHawk 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,114 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.19; | ||
|
||
import { IERC1155 } from "openzeppelin-contracts/contracts/token/ERC1155/IERC1155.sol"; | ||
import { IERC1155Receiver, IERC165 } from "openzeppelin-contracts/contracts/token/ERC1155/IERC1155Receiver.sol"; | ||
|
||
/** | ||
* An ERC-1155 contract that allows permissive minting. | ||
*/ | ||
contract ERC1155Holder is IERC1155Receiver { | ||
|
||
/// @dev Emitted when a claim is added. | ||
event ClaimAdded(address claimant, address tokenAddress, uint256 tokenId, uint256 amount); | ||
/// @dev Emitted when a batch of claims is added. | ||
event ClaimAddedBatch(address claimant, address tokenAddress, uint256[] tokenIds, uint256[] amounts); | ||
|
||
/// @dev Emitted when a claim is claimed. | ||
event Claimed(address claimant, address tokenAddress, uint256 tokenId, uint256 amount); | ||
/// @dev Emitted when a batch of claims is claimed. | ||
event ClaimedBatch(address claimant, address tokenAddress, uint256[] tokenIds, uint256[] amounts); | ||
|
||
/// @dev Error thrown when the claimant is invalid. | ||
error InvalidClaimant(); | ||
|
||
/// @dev claimant -> tokenAddress -> tokenId -> amount | ||
mapping(address => mapping(address => mapping(uint256 => uint256))) public claims; | ||
|
||
/// @dev Claims a token. | ||
/// @param claimant The claimant. | ||
/// @param tokenAddress The token address. | ||
/// @param tokenId The token id. | ||
function claim(address claimant, address tokenAddress, uint256 tokenId) public { | ||
uint256 amount = claims[claimant][tokenAddress][tokenId]; | ||
delete claims[claimant][tokenAddress][tokenId]; | ||
emit Claimed(claimant, tokenAddress, tokenId, amount); | ||
IERC1155(tokenAddress).safeTransferFrom(address(this), claimant, tokenId, amount, ""); | ||
} | ||
|
||
/// @dev Claims a batch of tokens. | ||
/// @param claimant The claimant. | ||
/// @param tokenAddress The token address. | ||
/// @param tokenIds The token ids. | ||
function claimBatch(address claimant, address tokenAddress, uint256[] memory tokenIds) public { | ||
uint256[] memory amounts = new uint256[](tokenIds.length); | ||
for (uint256 i = 0; i < tokenIds.length; i++) { | ||
amounts[i] = claims[claimant][tokenAddress][tokenIds[i]]; | ||
delete claims[claimant][tokenAddress][tokenIds[i]]; | ||
} | ||
emit ClaimedBatch(claimant, tokenAddress, tokenIds, amounts); | ||
IERC1155(tokenAddress).safeBatchTransferFrom(address(this), claimant, tokenIds, amounts, ""); | ||
} | ||
|
||
/// @inheritdoc IERC1155Receiver | ||
/// @param claimData The encoded claimant. | ||
function onERC1155Received( | ||
address, | ||
address, | ||
uint256 tokenId, | ||
uint256 amount, | ||
bytes calldata claimData | ||
) public virtual override returns (bytes4) { | ||
address claimant = _decodeClaimant(claimData); | ||
address tokenAddress = msg.sender; | ||
claims[claimant][tokenAddress][tokenId] += amount; | ||
emit ClaimAdded(claimant, tokenAddress, tokenId, amount); | ||
return this.onERC1155Received.selector; | ||
} | ||
|
||
/// @inheritdoc IERC1155Receiver | ||
/// @param claimData The encoded claimant. | ||
function onERC1155BatchReceived( | ||
address, | ||
address, | ||
uint256[] calldata tokenIds, | ||
uint256[] calldata amounts, | ||
bytes calldata claimData | ||
) public virtual override returns (bytes4) { | ||
address claimant = _decodeClaimant(claimData); | ||
address tokenAddress = msg.sender; | ||
for (uint256 i = 0; i < tokenIds.length; i++) { | ||
claims[claimant][tokenAddress][tokenIds[i]] += amounts[i]; | ||
} | ||
emit ClaimAddedBatch(claimant, tokenAddress, tokenIds, amounts); | ||
return this.onERC1155BatchReceived.selector; | ||
} | ||
|
||
/// @dev Decodes the claimant from the claim data. | ||
function _decodeClaimant( | ||
bytes calldata claimData | ||
) internal pure returns (address claimant) { | ||
if (claimData.length == 20) { | ||
// Packed address format | ||
assembly { | ||
calldatacopy(0, claimData.offset, 20) | ||
claimant := shr(96, mload(0)) | ||
} | ||
} else if (claimData.length == 32) { | ||
// ABI encoded address format | ||
(claimant) = abi.decode(claimData, (address)); | ||
} | ||
if (claimant == address(0)) { | ||
revert InvalidClaimant(); | ||
} | ||
return claimant; | ||
} | ||
|
||
/// @inheritdoc IERC165 | ||
function supportsInterface( | ||
bytes4 interfaceId | ||
) public view virtual override(IERC165) returns (bool) { | ||
return interfaceId == type(IERC1155Receiver).interfaceId || interfaceId == type(IERC165).interfaceId; | ||
} | ||
|
||
} |
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.
I'm surprised that Solidity doesn't recognize that the previous
i++
version was already bound byi < packContent.tokenAddresses.length
and cannot overflow :)Feels like Solidity could do this optimization by itself?