I am storing user information on IPFS in JSON object format and then storing that file hash on blockchain. I want to update that JSON object array every time I add a new user object. How can I achieve this?
I'm using Etherium Blockchain and ReactJS
I am storing user information on IPFS in JSON object format and then storing that file hash on blockchain. I want to update that JSON object array every time I add a new user object. How can I achieve this?
I'm using Etherium Blockchain and ReactJS
IPFS hashes are based on the content so the IPFS hash will change when the JSON data changes in this case. This means the content hash on-chain will have to be updated.
Below is an example of a smart contract that can be used to maintain a list of links to user objects.
Each user is assigned a unique identifier (for example, a GUID without delimiters just fits in bytes32). Method PutUser is used to add / update a link to the user's object. The GetUser method is used to get a link to the user's object. Method GetUsersList is used to get a list of users.
When you change the some user's object, you put it again in the IPFS and add a new link using the PutUser
pragma solidity >=0.5.8 <0.6.0;
contract UsersList
{
address Owner ;
struct IpfsLink
{
bytes32 used ;
string link ;
}
mapping (bytes32 => IpfsLink) UsersIpfsLinks ;
bytes32[] Users ;
//
constructor() public
{
Owner = tx.origin ;
}
//
function PutUser(bytes32 user_, string memory ipfs_link_) public
{
if(msg.sender!=Owner) return ;
if(UsersIpfsLinks[user_].used!="Y")
{
UsersIpfsLinks[user_]=IpfsLink({ used: "Y", link: ipfs_link_ }) ;
Users.push(user_) ;
}
else
{
UsersIpfsLinks[user_].link=ipfs_link_ ;
}
}
//
function GetUser(bytes32 user_) public view returns (string memory retVal)
{
return(UsersIpfsLinks[user_].link) ;
}
//
function GetUsersList() public view returns (bytes32[] memory retVal)
{
return(Users) ;
}
}