Caesar21

Untitled

Sep 13th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. pragma solidity ^0.4.21;
  2.  
  3. contract asus{
  4.  
  5. //表示授權多久&授權區間
  6. struct Authorized_times {
  7. uint expiration_time;
  8. uint access_period_start_time;
  9. uint access_period_end_time;
  10. }
  11.  
  12.  
  13.  
  14. //映射關係,從address->apps'id->data category->時間
  15. mapping (address => mapping(string => mapping(string => Authorized_times))) authorizations;
  16.  
  17. //映射關係,從user's_id->service_name->blockchain_address table
  18. mapping (string => mapping(string => address)) id_mapping_table;
  19.  
  20. //創建使用者給APP授權的事件
  21. event create_auth(
  22. address blockchain_address,
  23. string app_id,
  24. string data_category,
  25. uint expiration_time,
  26. uint access_period_start_time,
  27. uint access_period_end_time
  28. );
  29.  
  30. //創建使用者給APP的授權
  31. function create_authorization(address blockchain_address, string app_id, string data_category, uint time_num, uint begin_date, uint end_date) public {
  32. require(
  33. msg.sender == blockchain_address,
  34. "You can only authorize your own account. "
  35. );
  36. authorizations[blockchain_address][app_id][data_category].expiration_time = now + time_num * 1 days;
  37. authorizations[blockchain_address][app_id][data_category].access_period_start_time = begin_date;
  38. authorizations[blockchain_address][app_id][data_category].access_period_end_time = end_date;
  39.  
  40. emit create_auth(blockchain_address, app_id, data_category,
  41. authorizations[blockchain_address][app_id][data_category].expiration_time,
  42. authorizations[blockchain_address][app_id][data_category].access_period_start_time,
  43. authorizations[blockchain_address][app_id][data_category].access_period_end_time);
  44. }
  45.  
  46. //使用者修改授權效期的事件
  47. event modify_auth_time_expiration(
  48. address blockchain_address,
  49. string app_id,
  50. string data_category,
  51. uint expiration_time
  52. );
  53.  
  54. //修改授權的效期時間
  55. function modify_authorization_time_expiration(address blockchain_address, string app_id, string data_category, uint time_num) public {
  56. require(
  57. msg.sender == blockchain_address,
  58. "You can only modify authorization on your own account. "
  59. );
  60. authorizations[blockchain_address][app_id][data_category].expiration_time = now + time_num * 1 days;
  61.  
  62. emit modify_auth_time_expiration(blockchain_address, app_id, data_category,
  63. authorizations[blockchain_address][app_id][data_category].expiration_time);
  64. }
  65.  
  66. //使用者修改授權起止時間的事件
  67. event modify_auth_time_period(
  68. address blockchain_address,
  69. string app_id,
  70. string data_category,
  71. uint access_period_start_time,
  72. uint access_period_end_time
  73. );
  74.  
  75. //修改授權的起止時間
  76. function modify_authorization_time_period(address blockchain_address, string app_id, string data_category, uint begin_date, uint end_date) public {
  77. require(
  78. msg.sender == blockchain_address,
  79. "You can only modify authorization on your own account. "
  80. );
  81. authorizations[blockchain_address][app_id][data_category].access_period_start_time = begin_date;
  82. authorizations[blockchain_address][app_id][data_category].access_period_end_time = end_date;
  83.  
  84. emit modify_auth_time_period(blockchain_address, app_id, data_category,
  85. authorizations[blockchain_address][app_id][data_category].access_period_start_time,
  86. authorizations[blockchain_address][app_id][data_category].access_period_end_time);
  87. }
  88.  
  89. //查看授權的效期時間
  90. function get_time_expiration(address blockchain_address, string app_id, string data_category) public returns(uint exp_time) {
  91. require(
  92. msg.sender == blockchain_address,
  93. "You can only get your own account's message. "
  94. );
  95. return (authorizations[blockchain_address][app_id][data_category].expiration_time);
  96. }
  97.  
  98. //查看授權的起止時間
  99. function get_time_period(address blockchain_address, string app_id, string data_category) public returns(uint d_begin, uint d_end) {
  100. require(
  101. msg.sender == blockchain_address,
  102. "You can only get your own account's message. "
  103. );
  104. return (authorizations[blockchain_address][app_id][data_category].access_period_start_time, authorizations[blockchain_address][app_id][data_category].access_period_end_time);
  105. }
  106.  
  107.  
  108.  
  109. //判斷對APP是否有授權
  110. function verify_user_authorization(address blockchain_address, string app_id, string data_category, uint date_begin, uint date_end) public returns(bool){
  111. bool result = false;
  112. if(now <= authorizations[blockchain_address][app_id][data_category].expiration_time)
  113. {
  114. if(date_begin >= authorizations[blockchain_address][app_id][data_category].access_period_start_time && date_end <= authorizations[blockchain_address][app_id][data_category].access_period_end_time
  115. && date_begin <= date_end)
  116. {
  117. result = true;
  118. }
  119. }
  120. return result;
  121. }
  122.  
  123.  
  124.  
  125. //新增address的事件
  126. event append_address_service (
  127. string user_id,
  128. string service_name,
  129. address blockchain_address
  130. );
  131.  
  132. //在user_id新增一組service_name, blockchain address
  133. function append_ID_address_service_mapping_table(string user_id, string service_name, address blockchain_address)public{
  134. id_mapping_table[user_id][service_name] = blockchain_address;
  135.  
  136. emit append_address_service(user_id, service_name, id_mapping_table[user_id][service_name]);
  137. }
  138.  
  139. //刪除一組service_name, blockchain address的事件
  140. event delete_address_service (
  141. string user_id,
  142. string service_name,
  143. address before_delete_blockchain_address,
  144. address after_delete_blockchain_address
  145. );
  146.  
  147. //在一個user id刪除一組service_name, blockchain address
  148. function delete_ID_address_service(string user_id, string service_name)public{
  149. address temp_blockchain_address;
  150. temp_blockchain_address = id_mapping_table[user_id][service_name];
  151. delete id_mapping_table[user_id][service_name];
  152.  
  153. //很奇怪,這裡記錄日誌會和delete衝突,不能同時存在
  154. //emit delete_address_service(user_id, service_name, temp_blockchain_address, id_mapping_table[user_id][service_name]);
  155. }
  156.  
  157. //得到 ID and blockchain address mapping table
  158. function get_ID_address_service_mapping_table(string user_id, string service_name)public returns(address){
  159. return id_mapping_table[user_id][service_name];
  160. }
  161.  
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment