This is a hub & spoke pattern adapted to roughly map to your code. The Hub deploys Person contracts and keeps track of them. You can iterate over the unordered list from web3 and delete an item from the list if needed. You can add or remove person and hub privileges at the person level. For example, if you want, you can have a selfDestruct function in the Person contract and make it onlyOwner (only Hub can destroy it) or onlyPerson for self-delete. Presented without warranty. Very little testing. ;-)
pragma solidity ^0.4.6;
contract Hub {
// two-way interable index with delete
mapping(address => uint) personMap;
address[] public personIndex;
address public owner;
function Hub() {
owner = msg.sender;
}
modifier onlyOwner() {
if(msg.sender != owner) throw;
_;
}
function createPerson()
public
returns(address newPerson)
{
Person p = new Person(msg.sender); // whoever called this will "own" the Person contract created
personMap[p] = personIndex.length; // remember where it lives in the unordered list
personIndex.push(p); // append to the end of the list
return p;
}
function deletePerson(address person)
onlyOwner
returns(bool success)
{
// step by step for clarity
uint location = personMap[person]; // location on the list
address personAddress = personIndex[location]; // should match the person
// as one line
if(personIndex[personMap[person]] != person) throw; // non-existent person
// move the last item in the index to the location where the unperson was
personIndex[personMap[person]] = personIndex[personIndex.length-1];
// also have to update the personMap because the last item changed position in the list
// whoever was in the last row is now in the row where we are removing a record
personMap[personIndex[personIndex.length-1]] = personMap[person];
// now the list is shorter
personIndex.length--;
// person is removed from the list
return true;
}
// the next two functions make the unordered list of contracts iterable
function getPersonCount()
public
constant
returns(uint count)
{
return personIndex.length;
}
function getPersonAtIndex(uint index)
public
constant
returns(address person)
{
return personIndex[index];
}
}
contract Person {
// address public owner;
address public personOwner;
struct PersonStruct {
bytes32 encrypted1;
bytes32 encrypted2;
}
PersonStruct p;
modifier onlyPerson { // add this to functions only the "person" passed in should be able to do
if(msg.sender != personOwner) throw;
_;
}
function Person(address person) {
personOwner = person; // passed in by the creating Hub
// owner = msg.sender // this would enable the Hub to have certain privileges if needed
}
function getPerson()
onlyPerson
constant
returns(bytes32, bytes32)
{
return(p.encrypted1, p.encrypted2);
}
function setPerson(bytes32 part1, bytes32 part2)
onlyPerson
returns(bool success)
{
p.encrypted1 = part1;
p.encrypted2 = part2;
return true;
}
}
I think you're getting something wrong in understanding transactions and smart contracts. Below is my explanation.
When you deploy the above smart contract on ethereum, this will create a transaction(say TXN 1) and this "TXN 1" gets added to a block(say BLK 1). You'll also get the address for this smart contract deployed(say 0xabcd
)
- Next, now you enter your student data i.e., your ID and HASH -> then submit it. This will create a new transaction(say TXN 2), which gets added to a new block(BLK 2)
- Next, you enter more student data i.e., your ID and HASH -> then submit it. Every new data creates a new transaction(say TXN 3, TXN 4 ...) and which gets added to new blocks(BLK 3, BLK 4 ...)
- So by doing above, you're already creating new transactions. your old transactions
TXN 1
and so on will stay on blockchain forever with their data.
So, now when you intend to verify the student by submitting your ID and hashToVerify
, you'll have to do this in scope of your smart contract i.e. 0xabcd
. Because each smart contract you deploy is kind of new database in itself. It will assign memory for your student data and store there.
But now say, you deploy a new smart contract with the same code. This will create a new TXN 100, which gets added to BLK 100, and you get smart contract address of 0xpqrs
. This is kind of new fresh database. You'll not be able to verify the student data that you entered in the scope of smart contract 0xabcd
.
Try it:
I deployed your smart contract on testnet ethereum and my smart contract address is 0x1AF3cDb351E65470Cd4FFBF35E5fb8F614d01a75
. Goto https://remix.ethereum.org -> In the "deploy and run transations" tab, choose the environment Injected Web 3
and select your account from Metamask -> and load my smart contract in At Address
section i.e. paste 0x1AF3cDb351E65470Cd4FFBF35E5fb8F614d01a75
in that field, and press enter.
I created a student data of ID 1
and hash abcd
Then in verifyDocument
, type 1, "abcd"
, you'll be able to see true. Here you'll not be able to verify students whatever you've added in your smart contract address.
Best Answer
As you said, all the data is public. Anyone can read anything. From https://programtheblockchain.com/posts/2018/01/02/making-smart-contracts-with-public-variables/: