Let's say, for example, we want to store a URL on-chain for a specific NFT, but we do not want anyone else to be able to see the URL.

We can have a function in the contract that asks for an ID and firstly can only be called by the owner of that particular NFT ID but will secondly be able to decrypt the URL we stored for it.

1 Answer 1


Maybe you are referring to encrypting data rather than encoding data.

There is no secure way to do this from a smart contract, since there is nothing actually private in a smart contract or blockchain, and if you send the data to the smart contract, it's visible to miners and attackers.

Also, you would need something the user's public key to be able to encrypt data that only the user can decrypt. But for decrypting data from the smart contract itself you would need a private key, the user's private key, which is not a good idea to have it in a smart contract, not even for a second, since miners and attackers can see it when you send the transaction.

I recommend that if you really need a functionality like this, to do it off-chain, in a secure server, then you save the encrypted data in the smart contract in a mapping that holds the msg.sender address as the key and the encrypted data as the value and return that data when msg.sender requests it and let the user decrypt it off-chain. You could also provide an off-chain tool to help the user decrypt the data. Anyways, it doesn't seem that the data you are trying to save is too sensitive. Analyze if it's worth the effort.

Your Answer

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

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