Advertisement
Caesar21

Untitled

Sep 16th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.31 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. uint now_stamp = now;
  168. if(now_stamp <= authorizations[blockchain_address][app_id][data_category].expiration_time)
  169. {
  170. 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
  171. && date_begin <= date_end)
  172. {
  173. result = true;
  174. }
  175. }
  176.  
  177. emit verify_auth(blockchain_address, app_id, data_category, now_stamp, date_begin, date_end, result);
  178.  
  179. return result;
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186. //新增address的事件
  187. event append_address_service (
  188. string user_id,
  189. string service_name,
  190. address blockchain_address
  191. );
  192.  
  193. //在user_id新增一組service_name, blockchain address
  194. function append_ID_address_service_mapping_table(string user_id, string service_name, address blockchain_address)public{
  195. //Service_address_table memory table = Service_address_table(service_name, blockchain_address);
  196. id_mapping_table[user_id].push(Service_address_table(service_name, blockchain_address));
  197.  
  198. emit append_address_service(user_id, service_name, blockchain_address);
  199. }
  200.  
  201. //刪除一組service_name, blockchain address的事件
  202. event delete_address_service (
  203. string user_id,
  204. address blockchain_address
  205. );
  206.  
  207. //在一個user id刪除一組service_name, blockchain address
  208. function delete_ID_address_service(string user_id, address blockchain_address)public /*returns(Service_address_table[] get_deleted_table)*/{
  209.  
  210. uint index = id_mapping_table[user_id].length;
  211.  
  212. for(uint i = 0; i < index; i++)
  213. {
  214. if(id_mapping_table[user_id][i].blockchain_address == blockchain_address)
  215. {
  216. //delete id_mapping_table[user_id][i];
  217. for (uint j = i; j < index-1; j++)
  218. {
  219. id_mapping_table[user_id][j] = id_mapping_table[user_id][j+1];
  220. }
  221. delete id_mapping_table[user_id][index-1];
  222. id_mapping_table[user_id].length = id_mapping_table[user_id].length-1;
  223. }
  224. }
  225.  
  226. //return id_mapping_table[user_id];
  227.  
  228. //記錄日誌
  229. emit delete_address_service(user_id, blockchain_address);
  230. }
  231.  
  232. //得到 ID and blockchain address mapping table
  233. function get_ID_address_service_mapping_table(string user_id)public returns(Service_address_table[] get_table){
  234. return id_mapping_table[user_id];
  235. }
  236.  
  237.  
  238.  
  239. //上鏈記錄日誌
  240. event push_chain(
  241. address blockchain_address,
  242. string service_name,
  243. string data_category,
  244. uint access_period_start_time,
  245. uint access_period_end_time,
  246. bytes32 data_hash
  247. );
  248.  
  249. //Meta_data[] private meta_datas;
  250.  
  251. //上鏈
  252. 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 {
  253. /*meta_datas.push(Meta_data({
  254. blockchain_address: bc_address,
  255. service_name: sv_name,
  256. data_category: dt_category,
  257. access_period_start_time: p_start_time,
  258. access_period_end_time: p_end_time,
  259. data_hash: dt_hash
  260. }));*/
  261.  
  262. emit push_chain(bc_address, sv_name, dt_category, p_start_time, p_end_time, dt_hash);
  263. }
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement