Search code examples
blockchainethereumsoliditysmartcontracts

Is it possible to make certain fields of a structure only available to contract owner?


For example, given the following structure, I want some fields to be visible only for the contract owner

struct Participant{
    address participantAddress; // can be seen by anyone
    string team; // can be seen by anyone
    string personalDescription; // can be seen by anyone
    string secret // can be seen only by contract owner    
}
// Mapping each participant to an uint id
mapping(address=>Participant) public participantsMapping;

In other words, someone inspecting the participantsMapping can see all the fields EXCEPT the secret one (which can only be seen by owner)

From what I know, the "private" keyword doesn't hide any data, it's only a code-level specifier.

There are modifiers for function, but do they work for fields in a structure too? If not, how can it be achieved?

modifier onlyOwner() {
    require(msg.sender==ownerAddress,
    "Visible only by owner");
    _;
}

Solution

  • No. Storing private data on-chain is impossible.

    It's possible to encrypt the data, so that only the person with the secret key can access it. See https://docs.metamask.io/guide/rpc-api.html#example-4 and https://docs.metamask.io/guide/rpc-api.html#encrypting for example usage.