I’m working on a smart contract where users need to prove they own an NFT before depositing it. The process should work like this: when someone calls the deposit function, they provide a signature created with their wallet (like MetaMask), and the contract checks if that signature proves they actually own the NFT.
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
using Counters for Counters.Counter;
using ECDSA for bytes32;
Counters.Counter private _depositCounter;
mapping(address => uint256) private deposits;
bytes32 private constant MESSAGE_TYPEHASH =
keccak256("TokenOwnership(address contractAddr,uint256 id)");
constructor() EIP712("MyNFTValidator", "2.0.0") {}
function stakeNFT(TokenData calldata data, bytes calldata sig) external returns (bool isValidOwner) {
require(validateOwnership(data, sig));
uint256 depositId = _depositCounter.current();
deposits[msg.sender] = depositId;
_depositCounter.increment();
return isValidOwner;
}
function validateOwnership(TokenData calldata data, bytes calldata sig) public view returns (bool) {
address recovered = _hashTypedDataV4(
keccak256(abi.encode(MESSAGE_TYPEHASH, data.contractAddr, data.id))
).recover(sig);
return true;
}
Right now my verification function always returns true, but I need it to actually check if the person who signed the message is the real owner of the NFT. How can I properly implement this ownership verification?