Is there any way I can encrypt my MetaMask wallet address and save this encrypted address in a Solidity smart contract? For example, encrypt the address off-chain and store it in the smart contract. Then, when I want to retrieve the address, I will retrieve my encrypted address from the smart contract and decrypt it off-chain.

1 Answer 1


Yes, it is easy to do, you can encrypt your address in the backend or frontend off-chain and save it in the smart contract and access it and decrypt it again whenever you want. You can use the following similar code for the smart contract and connect to it with the Web3 library

pragma solidity ^0.8.0;

contract EncryptedAddressStorage {
    address public owner;
    string public encryptedAddress;

    constructor() {
        owner = msg.sender;

    modifier onlyOwner() {
        require(msg.sender == owner, "Only the owner can modify the address.");

    function setEncryptedAddress(string memory _encryptedAddress) public onlyOwner {
        encryptedAddress = _encryptedAddress;

    function getEncryptedAddress() public view returns (string memory) {
        return encryptedAddress;
  • Thank you so much! I have another question, lets say my wallet address is: 0xdAC17F958D2ee523a2206206994597C13D831ec7 after encryption it becomes: 0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD so instead of string i can save address? Sep 2, 2023 at 13:38
  • you're welcome , To store encrypted data, you must use strings or bytes because addresses are in a special format and do not accept every data.
    – Mehran Ra
    Sep 2, 2023 at 13:53

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.