0
function generateSeed(uint256 tokenId) public view returns (uint256) {
    uint256 r = random(tokenId);
    uint256 headSeed = 100 * (r % 7 + 10) + ((r >> 48) % 20 + 10);
    uint256 faceSeed = 100 * ((r >> 96) % 6 + 10) + ((r >> 96) % 20 + 10);
    uint256 bodySeed = 100 * ((r >> 144) % 7 + 10) + ((r >> 144) % 20 + 10);
    uint256 legsSeed = 100 * ((r >> 192) % 2 + 10) + ((r >> 192) % 20 + 10);
    return headSeed;

}

the code is from owls nft: https://etherscan.io/address/0xe2e27b49e405f6c25796167b2500c195f972ebac#code

1 Answer 1

0

In your code, the calculation of headSeed is deterministic from the value of r. The source of randomness comes from the function random().

    function random(uint256 tokenId) private view returns (uint256 pseudoRandomness) {
        pseudoRandomness = uint256(
            keccak256(abi.encodePacked(blockhash(block.number - 1), tokenId))
        );

        return pseudoRandomness;
    }

Randomness here is gotten from blockhash of the last block. It is recommended not to use block values as a source of randomness as it can be controlled by other actors https://swcregistry.io/docs/SWC-120 . One of the recommended ways is to use Chainlink VRF Verifiable Random Function

Nonetheless, if the NFT value is not really high (and the benefit of manipulating the randomness is not high compared to the resource to obtain the block mining (proposing) right), the usage of blockhash is justifiable because of its simplicity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.