Solidity
查詢多個智能合約的公開數據
我有一個智能合約,其中包含一個人的詳細資訊。每個新人都會獲得一份他們“擁有”的新智能合約。
我有一個監管者/管理員想查看系統上存在的此類智能合約的數量並查看擁有它的人。他將無法查看儲存在其中的任何私人數據(數據將被加密)。只有所有者名稱(公共變數)將採用未加密形式。是否可以編寫一個執行此操作的函式?我考慮過創建一個系統資料庫,將
address
vs儲存在owner name
數據庫中,但我正在尋找僅使用智能合約的解決方案。contract People{ bool public isActive = false; uint public objectId; string public ownerName; string somePrivateData; mapping (address => bool) owned; function initPeopleContract(string name){ if (!isActive) { isActive = true; ownerName = name; } } function getOwnerName() returns (string val) { if (!isActive) { val = ownerName; }else { val = "Account Deactivated"; } } function getPrivateData() returns (string data) { if (msg.sender == address){ // Send the data back } else { // Reject due to un-authorized request } } }
最後一個功能不完整,我將添加檢查請求交易的人是否真的是智能合約的所有者的部分。這
ownerName
是管理員應該能夠查詢和查看的公共變數。我正在使用 web3.js ,瀏覽器可靠性來編譯
abi
程式碼和使用geth
控制台命令執行的私有區塊鏈。
這是一種適用於大致映射到您的程式碼的中心輻射模式。Hub 部署人員合約並對其進行跟踪。您可以從 web3 遍歷無序列表,並在需要時從列表中刪除一個項目。您可以在人員級別添加或刪除人員和中心權限。例如,如果需要,可以在 Person 合約中設置一個 selfDestruct 函式,並使其成為 onlyOwner(只有 Hub 可以銷毀它)或 onlyPerson 用於自刪除。不提供保修。很少測試。;-)
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; } }