onERC721Received must be called by the NFT only

This commit is contained in:
Michele Balistreri 2020-04-13 11:46:09 +03:00
parent f3a07496fa
commit 8e7ab9ebc9
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
2 changed files with 11 additions and 0 deletions

View File

@ -148,6 +148,7 @@ contract NFTBucket is IERC165, IERC721Receiver {
} }
function onERC721Received(address _operator, address _from, uint256 _tokenID, bytes calldata _data) external override(IERC721Receiver) returns(bytes4) { function onERC721Received(address _operator, address _from, uint256 _tokenID, bytes calldata _data) external override(IERC721Receiver) returns(bytes4) {
require(msg.sender == address(tokenContract), "only the NFT contract can call this");
require((_operator == owner) || (_from == owner), "only the owner can create gifts"); require((_operator == owner) || (_from == owner), "only the owner can create gifts");
require(_data.length == 52, "invalid data field"); require(_data.length == 52, "invalid data field");

View File

@ -197,6 +197,16 @@ contract("NFTBucket", function () {
}); });
it("cannot create two gifts for the same token", async function() {
try {
await NFTBucket.methods.onERC721Received(shop, shop, 0xcafe, createGiftData(keycard_3)).send({from: shop});
assert.fail("transfer should have failed");
} catch(e) {
assert.match(e.message, /only the NFT/);
}
});
async function testRedeem(receiver, recipient, signer, relayer, redeemCode, blockNumber, blockHash) { async function testRedeem(receiver, recipient, signer, relayer, redeemCode, blockNumber, blockHash) {
let gift = await NFTBucket.methods.gifts(recipient).call(); let gift = await NFTBucket.methods.gifts(recipient).call();
const tokenID = gift.tokenID; const tokenID = gift.tokenID;