0

I am creating a dapp using web3-1.0. I have a contract that stores personal data for a particular user(account). I want to implement a functionality so that a user can share his data (stored in the smart contract) with another user for a particular time period. After that, the data should not be visible to the other user. Can we achieve this using solidity and web3-1.0 ? I am very new to this, please help me out !!

1

Basically, no.

All information in the blockchain is public. Forever. So whatever you once input in to your smart contract will be visible for anyone forever - even if you remove it. Anyone can just see the state of the contract during a certain block and see the data which was there in that block.

Therefore it is also not possible to limit the visibility of some data to some time period. The data accessibility may be limited with some code but if the user really wants they can just see the data from an earlier block.

But, if it's enough for your usage that another smart contract can't access the data (they can't access earlier blocks) after a certain time period, then you can implement some logic to give out the data only before certain time period. You could achieve this with something like this (set the variable timeLimit to a proper value somewhere):

pragma solidity ^0.5.5;

contract TimeLimited {

    uint timeLimit;

    modifier isWithinTime() {
        require(now < timeLimit, "not before the given time limit");
        _;
    }

    function giveData() public view isWithinTime returns (uint) {
        return 5;
    }
}
0

Well we can limit the access of information from anyone for always or for certain period of time. You just need to make your information variables private and make getter functions for that and apply some modifiers on that getter functions. Here I wrote an example for you.

pragma solidity ^0.5.5;

contract UserData {

    uint timeLimit;
    struct userInfo{
        bool lock;
        uint endTime;
        string name;
    }
    mapping(address=> userInfo) private user;

    function setName(string memory name) public{
        user[msg.sender].name = name;
    }

    function setLockTime(uint timeToEnd, bool lock) public {
        user[msg.sender].lock = lock;
        user[msg.sender].endTime = timeToEnd;
    }

    modifier isLocked(address userAddress){
        require(user[userAddress].lock != true);
        require(user[userAddress].endTime < now);
        _;
    }

    function getUserInfo(address userAddress) public view isLocked(userAddress) returns (string memory){
        return user[userAddress].name;
    }
}

Here I am recording the lock state and end lock time in user info. You can run this get the idea to achieve the functionality. Currently this contract is limiting everyone to access data of user if locked. But again you can use mapping to track specific user who is locked.

mapping(address => lock) whichUserIsLocked; 

Something like that. But again you get the idea. Hope it will help you.

  • As I said in my answer there is no way to hide the information from users. It can be hidden only from other smart contracts. – Lauri Peltonen Mar 22 at 4:28
  • Okay, and I can also create a modifier that will allow the function to return data only if the msg.sender is in the list of accounts that have authorization for access. By the way, by inspecting the blocks,can we see the actual data stored in the contract or only the hash of the data ? – harshit Mar 22 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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