Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. pragma solidity ^0.4.4;
  2.  
  3. // Simple Solidity intro/demo contract for BlockGeeks Article
  4. // Владелец может менять название гостевой книги
  5. // Любой пользователь может добавлять любое сообщение, в котором необходимо указать имя и текст сообщения
  6. // Пользователи могут ставить рейтинг за сообщение
  7. // Владелец может убить контракт
  8. // Владелец может удалить сообщение
  9. contract GuestBook {
  10.  
  11. address adminAddress; // адрес администратора
  12. string guestbookName; // название гостевой книги
  13.  
  14. mapping ( bytes32 => notarizedImage) notarizedImages; // this allows to look up notarizedImages by their SHA256notaryHash
  15. bytes32[] imagesByNotaryHash; // this is like a whitepages of all images, by SHA256notaryHash
  16.  
  17. mapping ( address => User ) Users; // this allows to look up Users by their ethereum address
  18. address[] usersByAddress; // this is like a whitepages of all users, by ethereum address
  19.  
  20. struct notarizedImage {
  21. string imageURL; // адрес картинки
  22. uint timeStamp; // временная метка
  23. }
  24.  
  25. struct User {
  26. string handle;
  27. bytes32 city;
  28. bytes32 state;
  29. bytes32 country;
  30. bytes32[] myImages;
  31. }
  32.  
  33. function GuestBook() payable { // this is the CONSTRUCTOR (same name as contract) it gets called ONCE only when contract is first deployed
  34. adminAddress = msg.sender; // just set the admin, so they can remove bad users or images if needed, but nobody else can
  35. }
  36.  
  37. modifier onlyAdmin() {
  38. if (msg.sender != adminAddress)
  39. throw;
  40. // Do not forget the "_;"! It will be replaced by the actual function body when the modifier is used.
  41. _;
  42. }
  43.  
  44. function removeUser(address badUser) onlyAdmin returns (bool success) {
  45. delete Users[badUser];
  46. return true;
  47. }
  48.  
  49. function removeImage(bytes32 badImage) onlyAdmin returns (bool success) {
  50. delete notarizedImages[badImage];
  51. return true;
  52. }
  53.  
  54. function registerNewUser(string handle, bytes32 city, bytes32 state, bytes32 country) returns (bool success) {
  55. address thisNewAddress = msg.sender;
  56. // don't overwrite existing entries, and make sure handle isn't null
  57. if(bytes(Users[msg.sender].handle).length == 0 && bytes(handle).length != 0){
  58. Users[thisNewAddress].handle = handle;
  59. Users[thisNewAddress].city = city;
  60. Users[thisNewAddress].state = state;
  61. Users[thisNewAddress].country = country;
  62. usersByAddress.push(thisNewAddress); // adds an entry for this user to the user 'whitepages'
  63. return true;
  64. } else {
  65. return false; // either handle was null, or a user with this handle already existed
  66. }
  67. }
  68.  
  69. function addImageToUser(string imageURL, bytes32 SHA256notaryHash) returns (bool success) {
  70. address thisNewAddress = msg.sender;
  71. if(bytes(Users[thisNewAddress].handle).length != 0){ // make sure this user has created an account first
  72. if(bytes(imageURL).length != 0){ // ) { // couldn't get bytes32 null check to work, oh well!
  73. // prevent users from fighting over sha->image listings in the whitepages, but still allow them to add a personal ref to any sha
  74. if(bytes(notarizedImages[SHA256notaryHash].imageURL).length == 0) {
  75. imagesByNotaryHash.push(SHA256notaryHash); // adds entry for this image to our image whitepages
  76. }
  77. notarizedImages[SHA256notaryHash].imageURL = imageURL;
  78. notarizedImages[SHA256notaryHash].timeStamp = block.timestamp; // note that updating an image also updates the timestamp
  79. Users[thisNewAddress].myImages.push(SHA256notaryHash); // add the image hash to this users .myImages array
  80. return true;
  81. } else {
  82. return false; // either imageURL or SHA256notaryHash was null, couldn't store image
  83. }
  84. return true;
  85. } else {
  86. return false; // user didn't have an account yet, couldn't store image
  87. }
  88. }
  89.  
  90. function getUsers() constant returns (address[]) { return usersByAddress; }
  91.  
  92. function getUser(address userAddress) constant returns (string,bytes32,bytes32,bytes32,bytes32[]) {
  93. return (Users[userAddress].handle,Users[userAddress].city,Users[userAddress].state,Users[userAddress].country,Users[userAddress].myImages);
  94. }
  95.  
  96. function getAllImages() constant returns (bytes32[]) { return imagesByNotaryHash; }
  97.  
  98. function getUserImages(address userAddress) constant returns (bytes32[]) { return Users[userAddress].myImages; }
  99.  
  100. function getImage(bytes32 SHA256notaryHash) constant returns (string,uint) {
  101. return (notarizedImages[SHA256notaryHash].imageURL,notarizedImages[SHA256notaryHash].timeStamp);
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement