Advertisement
Caesar21

Untitled

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