Caesar21

Untitled

Sep 15th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. pragma solidity ^0.4.22;
  2.  
  3. pragma experimental ABIEncoderV2;
  4.  
  5. contract PHIL{
  6.  
  7. //表示授權多久&授權區間
  8. struct Authorized_times {
  9. uint expiration_time;
  10. uint access_period_start_time;
  11. uint access_period_end_time;
  12. }
  13.  
  14. //ID mapping table
  15. struct Service_address_table {
  16. string service_name;
  17. address blockchain_address;
  18. }
  19.  
  20. //記錄上鏈資料
  21. /*struct Meta_data {
  22. address blockchain_address;
  23. string service_name;
  24. string data_category;
  25. uint access_period_start_time;
  26. uint access_period_end_time;
  27. bytes32 data_hash;
  28. }*/
  29.  
  30.  
  31. //映射關係,從address->apps'id->data category->時間
  32. mapping (address => mapping(string => mapping(string => Authorized_times))) authorizations;
  33.  
  34. //映射關係,從user's_id->Service_address_table[]
  35. mapping (string => Service_address_table[]) id_mapping_table;
  36.  
  37. //記錄授權的事件
  38. event create_auth(
  39. address blockchain_address,
  40. string app_id,
  41. string data_category,
  42. uint expiration_time,
  43. uint access_period_start_time,
  44. uint access_period_end_time
  45. );
  46.  
  47. //創建使用者給APP的授權
  48. function create_authorization(address blockchain_address, string app_id, string data_category, uint end_time_stamp, uint begin_date, uint end_date) public {
  49. require(
  50. msg.sender == blockchain_address,
  51. "You can only authorize your own account. "
  52. );
  53. authorizations[blockchain_address][app_id][data_category].expiration_time = end_time_stamp;
  54. authorizations[blockchain_address][app_id][data_category].access_period_start_time = begin_date;
  55. authorizations[blockchain_address][app_id][data_category].access_period_end_time = end_date;
  56.  
  57. emit create_auth(blockchain_address, app_id, data_category,
  58. authorizations[blockchain_address][app_id][data_category].expiration_time,
  59. authorizations[blockchain_address][app_id][data_category].access_period_start_time,
  60. authorizations[blockchain_address][app_id][data_category].access_period_end_time);
  61. }
  62.  
  63. //記錄取消授權的事件
  64. event cancel_auth(
  65. address blockchain_address,
  66. string app_id,
  67. string data_category,
  68. uint expiration_time,
  69. uint access_period_start_time,
  70. uint access_period_end_time
  71. );
  72.  
  73. //取消授權
  74. function cancel_authorization(address blockchain_address, string app_id, string data_category) public {
  75. require(
  76. msg.sender == blockchain_address,
  77. "You can only cancel your own account's authorization. "
  78. );
  79.  
  80. authorizations[blockchain_address][app_id][data_category].expiration_time = 0;
  81. authorizations[blockchain_address][app_id][data_category].access_period_start_time = 0;
  82. authorizations[blockchain_address][app_id][data_category].access_period_end_time = 0;
  83.  
  84. emit cancel_auth(blockchain_address, app_id, data_category,
  85. authorizations[blockchain_address][app_id][data_category].expiration_time,
  86. authorizations[blockchain_address][app_id][data_category].access_period_start_time,
  87. authorizations[blockchain_address][app_id][data_category].access_period_end_time);
  88. }
  89.  
  90. /*
  91. //使用者修改授權效期的事件
  92. event modify_auth_time_expiration(
  93. address blockchain_address,
  94. string app_id,
  95. string data_category,
  96. uint expiration_time
  97. );
  98.  
  99. //修改授權的效期時間
  100. function modify_authorization_time_expiration(address blockchain_address, string app_id, string data_category, uint time_num) public {
  101. require(
  102. msg.sender == blockchain_address,
  103. "You can only modify authorization on your own account. "
  104. );
  105. authorizations[blockchain_address][app_id][data_category].expiration_time = now + time_num * 1 days;
  106.  
  107. emit modify_auth_time_expiration(blockchain_address, app_id, data_category,
  108. authorizations[blockchain_address][app_id][data_category].expiration_time);
  109. }
  110.  
  111. //使用者修改授權起止時間的事件
  112. event modify_auth_time_period(
  113. address blockchain_address,
  114. string app_id,
  115. string data_category,
  116. uint access_period_start_time,
  117. uint access_period_end_time
  118. );
  119.  
  120. //修改授權的起止時間
  121. function modify_authorization_time_period(address blockchain_address, string app_id, string data_category, uint begin_date, uint end_date) public {
  122. require(
  123. msg.sender == blockchain_address,
  124. "You can only modify authorization on your own account. "
  125. );
  126. authorizations[blockchain_address][app_id][data_category].access_period_start_time = begin_date;
  127. authorizations[blockchain_address][app_id][data_category].access_period_end_time = end_date;
  128.  
  129. emit modify_auth_time_period(blockchain_address, app_id, data_category,
  130. authorizations[blockchain_address][app_id][data_category].access_period_start_time,
  131. authorizations[blockchain_address][app_id][data_category].access_period_end_time);
  132. }
  133. */
  134.  
  135. //查看授權的效期時間
  136. function get_time_expiration(address blockchain_address, string app_id, string data_category) public returns(uint exp_time) {
  137. require(
  138. msg.sender == blockchain_address,
  139. "You can only get your own account's message. "
  140. );
  141. return (authorizations[blockchain_address][app_id][data_category].expiration_time);
  142. }
  143.  
  144. //查看授權的起止時間
  145. function get_time_period(address blockchain_address, string app_id, string data_category) public returns(uint d_begin, uint d_end) {
  146. require(
  147. msg.sender == blockchain_address,
  148. "You can only get your own account's message. "
  149. );
  150. return (authorizations[blockchain_address][app_id][data_category].access_period_start_time, authorizations[blockchain_address][app_id][data_category].access_period_end_time);
  151. }
  152.  
  153.  
  154.  
  155. //判斷對APP是否有授權
  156. function verify_user_authorization(address blockchain_address, string app_id, string data_category, uint now_timestamp, uint date_begin, uint date_end) public returns(bool){
  157. bool result = false;
  158. if(now_timestamp <= authorizations[blockchain_address][app_id][data_category].expiration_time)
  159. {
  160. 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
  161. && date_begin <= date_end)
  162. {
  163. result = true;
  164. }
  165. }
  166. return result;
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. //新增address的事件
  174. event append_address_service (
  175. string user_id,
  176. string service_name,
  177. address blockchain_address
  178. );
  179.  
  180. //在user_id新增一組service_name, blockchain address
  181. function append_ID_address_service_mapping_table(string user_id, string service_name, address blockchain_address)public{
  182. //Service_address_table memory table = Service_address_table(service_name, blockchain_address);
  183. id_mapping_table[user_id].push(Service_address_table(service_name, blockchain_address));
  184.  
  185. emit append_address_service(user_id, service_name, blockchain_address);
  186. }
  187.  
  188. //刪除一組service_name, blockchain address的事件
  189. event delete_address_service (
  190. string user_id,
  191. address blockchain_address
  192. );
  193.  
  194. //在一個user id刪除一組service_name, blockchain address
  195. function delete_ID_address_service(string user_id, address blockchain_address)public{
  196.  
  197. uint index = id_mapping_table[user_id].length;
  198.  
  199. for(uint i = 0; i < index; i++)
  200. {
  201. if(id_mapping_table[user_id][i].blockchain_address == blockchain_address)
  202. {
  203. delete id_mapping_table[user_id][i];
  204. }
  205. }
  206.  
  207. //很奇怪,這裡記錄日誌會和delete衝突,不能同時存在
  208. emit delete_address_service(user_id, blockchain_address);
  209. }
  210.  
  211. //得到 ID and blockchain address mapping table
  212. function get_ID_address_service_mapping_table(string user_id)public returns(Service_address_table[] get_table){
  213. return id_mapping_table[user_id];
  214. }
  215.  
  216.  
  217.  
  218. //上鏈記錄日誌
  219. event push_chain(
  220. address blockchain_address,
  221. string service_name,
  222. string data_category,
  223. uint access_period_start_time,
  224. uint access_period_end_time,
  225. bytes32 data_hash
  226. );
  227.  
  228. //Meta_data[] private meta_datas;
  229.  
  230. //上鏈
  231. function push_on_chain(address bc_address, string sv_name, string dt_category, uint p_start_time, uint p_end_time, bytes32 dt_hash) public {
  232. /*meta_datas.push(Meta_data({
  233. blockchain_address: bc_address,
  234. service_name: sv_name,
  235. data_category: dt_category,
  236. access_period_start_time: p_start_time,
  237. access_period_end_time: p_end_time,
  238. data_hash: dt_hash
  239. }));*/
  240.  
  241. emit push_chain(bc_address, sv_name, dt_category, p_start_time, p_end_time, dt_hash);
  242. }
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment