Caesar21

Untitled

Sep 15th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.90 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. event verify_auth(
  155. address blockchain_address,
  156. string app_id,
  157. string data_category,
  158. uint now_time,
  159. uint date_begin,
  160. uint date_end,
  161. bool result
  162. );
  163.  
  164. //判斷對APP是否有授權
  165. function verify_user_authorization(address blockchain_address, string app_id, string data_category, uint date_begin, uint date_end) public returns(bool){
  166. bool result = false;
  167. if(now <= authorizations[blockchain_address][app_id][data_category].expiration_time)
  168. {
  169. 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
  170. && date_begin <= date_end)
  171. {
  172. result = true;
  173. }
  174. }
  175.  
  176. emit verify_auth(blockchain_address, app_id, data_category, now, date_begin, date_end, result);
  177.  
  178. return result;
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185. //新增address的事件
  186. event append_address_service (
  187. string user_id,
  188. string service_name,
  189. address blockchain_address
  190. );
  191.  
  192. //在user_id新增一組service_name, blockchain address
  193. function append_ID_address_service_mapping_table(string user_id, string service_name, address blockchain_address)public{
  194. //Service_address_table memory table = Service_address_table(service_name, blockchain_address);
  195. id_mapping_table[user_id].push(Service_address_table(service_name, blockchain_address));
  196.  
  197. emit append_address_service(user_id, service_name, blockchain_address);
  198. }
  199.  
  200. //刪除一組service_name, blockchain address的事件
  201. event delete_address_service (
  202. string user_id,
  203. address blockchain_address
  204. );
  205.  
  206. //在一個user id刪除一組service_name, blockchain address
  207. function delete_ID_address_service(string user_id, address blockchain_address)public{
  208.  
  209. uint index = id_mapping_table[user_id].length;
  210.  
  211. for(uint i = 0; i < index; i++)
  212. {
  213. if(id_mapping_table[user_id][i].blockchain_address == blockchain_address)
  214. {
  215. delete id_mapping_table[user_id][i];
  216. }
  217. }
  218.  
  219. //很奇怪,這裡記錄日誌會和delete衝突,不能同時存在
  220. emit delete_address_service(user_id, blockchain_address);
  221. }
  222.  
  223. //得到 ID and blockchain address mapping table
  224. function get_ID_address_service_mapping_table(string user_id)public returns(Service_address_table[] get_table){
  225. return id_mapping_table[user_id];
  226. }
  227.  
  228.  
  229.  
  230. //上鏈記錄日誌
  231. event push_chain(
  232. address blockchain_address,
  233. string service_name,
  234. string data_category,
  235. uint access_period_start_time,
  236. uint access_period_end_time,
  237. bytes32 data_hash
  238. );
  239.  
  240. //Meta_data[] private meta_datas;
  241.  
  242. //上鏈
  243. 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 {
  244. /*meta_datas.push(Meta_data({
  245. blockchain_address: bc_address,
  246. service_name: sv_name,
  247. data_category: dt_category,
  248. access_period_start_time: p_start_time,
  249. access_period_end_time: p_end_time,
  250. data_hash: dt_hash
  251. }));*/
  252.  
  253. emit push_chain(bc_address, sv_name, dt_category, p_start_time, p_end_time, dt_hash);
  254. }
  255.  
  256. }
Advertisement
Add Comment
Please, Sign In to add comment