0

I trying to save e-mails of my users in contract but I can simulate the from when I call methods of contract.

Contract.sol

contract Test {
    ...
    mapping (address => string) private _emails;

    function getEmail(address from) public view returns (string memory) {
        require(msg.sender == from, 'No!');
        return _emails[msg.sender];
    }
}

app.js (web3js)

let email = await Contract.methods.getEmail(any_other_address).call({from: any_other_address});
console.log(email);

output

[email protected]

I thought msg.sender couldn't be manipulated by from in web3.js. Is there a safe way to save emails in the contract?

2 Answers 2

2

Everything you write on your blockchain is open and anybody in the world can read it. Plus, it will cost your user something like $1 to $10 to give up his information to the world.

Why do you want to store it securely ? There is no security, everything will be public.

1
  • Yeah, I suspected I couldn't save private information in the contract...but thanks for confirming!
    – GIA
    Commented Jun 28, 2021 at 23:54
0

You can actually save private information in a contract if there is only ever going to be read later by one person outside the contract. You could use private key cryptography running in the browser to encrypt the user's email to your own public key before it is sent to the blockchain. Later, you can use your private key to read the email address.

In general, if you are just one way sending information from the user to only be read outside the blockchain, and never on-chain, then it is much cheaper to use logs for the data, rather than writing it to on-chain storage.

Also in general, if you have large amounts of data, it can be much better to write it to something like IPFS and then store just the hash to the data on-chain. To have this secure and one way, you can encrypt the data to your public key before you store it on IPFS.

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.