Guest User

Untitled

a guest
Nov 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. pragma solidity 0.4.18;
  2.  
  3. import "./contract/Owned.sol";
  4. import "./RocketUser.sol";
  5. import "./RocketStorage.sol";
  6.  
  7.  
  8. /// @title Examples of how to store and read a contract address in your dApps eternal storage for use in the hub / spoke pattern
  9. /// @author David Rugendyke
  10.  
  11. contract RocketStorageExamples is Owned {
  12.  
  13.  
  14. /*** Contracts **************/
  15.  
  16. RocketStorage rocketStorage = RocketStorage(0); // The main RocketStorage contract where primary persistant storage is maintained
  17. RocketUser rocketUser = RocketUser(0); // The contract that contains the user methods in Rocket Pool
  18.  
  19. /*** Constructor ***********/
  20.  
  21. /// @dev RocketStorageExamples constructor
  22. function RocketStorageExamples(address _rocketStorageAddress) public {
  23. // Update the storage contracts address when this contract is deployed so that it knows the location of the storage contract
  24. rocketStorage = RocketStorage(_rocketStorageAddress);
  25. }
  26.  
  27.  
  28. /**** Add a contract to the dApp storage/hub ***********/
  29.  
  30. // An example of a method that could be used to add a contract to the dApps eternal storage/hub
  31. function addContract(string _name, address _newContractAddress) onlyOwner external {
  32. // Add the contract to the storage using a hash of the "contract.name" namespace and the name of the contract that was supplied as the 'key' and use the new contract address as the 'value'
  33. // This means we can get the address of the contract later by looking it up using its name eg 'rocketUser'
  34. rocketStorage.setAddress(keccak256("contract.name", _name), _newContractAddress);
  35. // Add the contract to the storage using a hash of the "contract.address" namespace and the address of the contract that was supplied as the 'key' and use the new contract address as the 'value'
  36. // This means we can verify this contract as belonging to the dApp by using it's address rather than its name.
  37. // Handy when you need to protect certain methods from being accessed by any contracts that are not part of the dApp using msg.sender (see the modifier onlyLatestRocketNetworkContract() in the RocketStorage code)
  38. rocketStorage.setAddress(keccak256("contract.address", _newContractAddress), _newContractAddress);
  39. }
  40.  
  41. // An example of a method that asks the hub for the address of another contract in the dApp before communicating directly with that contract, ensures it gets the most up to date contract address
  42. function getUserExists(string _name) onlyOwner external returns (bool) {
  43. // Get the address of the user contract so that we can check if a user with this name exists, it was registered with the name 'rocketUser'
  44. rocketUser = RocketUser(rocketStorage.getAddress(keccak256("contract.name", "rocketUser")));
  45. // Now we have an instance of that contract using its most up to date address supplied by the hub, check if the user exists
  46. if (rocketUser.exists(_name)) {
  47. // User exists
  48. return true;
  49. }
  50. // Use does not exist
  51. return false;
  52. }
  53.  
  54. }
Add Comment
Please, Sign In to add comment