Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.22;
- pragma experimental ABIEncoderV2;
- contract PHIL{
- //表示授權多久&授權區間
- struct Authorized_times {
- uint expiration_time;
- uint access_period_start_time;
- uint access_period_end_time;
- }
- //ID mapping table
- struct Service_address_table {
- string service_name;
- address blockchain_address;
- }
- //記錄上鏈資料
- /*struct Meta_data {
- address blockchain_address;
- string service_name;
- string data_category;
- uint access_period_start_time;
- uint access_period_end_time;
- bytes32 data_hash;
- }*/
- //映射關係,從address->apps'id->data category->時間
- mapping (address => mapping(string => mapping(string => Authorized_times))) authorizations;
- //映射關係,從user's_id->Service_address_table[]
- mapping (string => Service_address_table[]) id_mapping_table;
- //記錄授權的事件
- event create_auth(
- address blockchain_address,
- string app_id,
- string data_category,
- uint expiration_time,
- uint access_period_start_time,
- uint access_period_end_time
- );
- //創建使用者給APP的授權
- function create_authorization(address blockchain_address, string app_id, string data_category, uint end_time_stamp, uint begin_date, uint end_date) public {
- require(
- msg.sender == blockchain_address,
- "You can only authorize your own account. "
- );
- authorizations[blockchain_address][app_id][data_category].expiration_time = end_time_stamp;
- authorizations[blockchain_address][app_id][data_category].access_period_start_time = begin_date;
- authorizations[blockchain_address][app_id][data_category].access_period_end_time = end_date;
- emit create_auth(blockchain_address, app_id, data_category,
- authorizations[blockchain_address][app_id][data_category].expiration_time,
- authorizations[blockchain_address][app_id][data_category].access_period_start_time,
- authorizations[blockchain_address][app_id][data_category].access_period_end_time);
- }
- //記錄取消授權的事件
- event cancel_auth(
- address blockchain_address,
- string app_id,
- string data_category,
- uint expiration_time,
- uint access_period_start_time,
- uint access_period_end_time
- );
- //取消授權
- function cancel_authorization(address blockchain_address, string app_id, string data_category) public {
- require(
- msg.sender == blockchain_address,
- "You can only cancel your own account's authorization. "
- );
- authorizations[blockchain_address][app_id][data_category].expiration_time = 0;
- authorizations[blockchain_address][app_id][data_category].access_period_start_time = 0;
- authorizations[blockchain_address][app_id][data_category].access_period_end_time = 0;
- emit cancel_auth(blockchain_address, app_id, data_category,
- authorizations[blockchain_address][app_id][data_category].expiration_time,
- authorizations[blockchain_address][app_id][data_category].access_period_start_time,
- authorizations[blockchain_address][app_id][data_category].access_period_end_time);
- }
- /*
- //使用者修改授權效期的事件
- event modify_auth_time_expiration(
- address blockchain_address,
- string app_id,
- string data_category,
- uint expiration_time
- );
- //修改授權的效期時間
- function modify_authorization_time_expiration(address blockchain_address, string app_id, string data_category, uint time_num) public {
- require(
- msg.sender == blockchain_address,
- "You can only modify authorization on your own account. "
- );
- authorizations[blockchain_address][app_id][data_category].expiration_time = now + time_num * 1 days;
- emit modify_auth_time_expiration(blockchain_address, app_id, data_category,
- authorizations[blockchain_address][app_id][data_category].expiration_time);
- }
- //使用者修改授權起止時間的事件
- event modify_auth_time_period(
- address blockchain_address,
- string app_id,
- string data_category,
- uint access_period_start_time,
- uint access_period_end_time
- );
- //修改授權的起止時間
- function modify_authorization_time_period(address blockchain_address, string app_id, string data_category, uint begin_date, uint end_date) public {
- require(
- msg.sender == blockchain_address,
- "You can only modify authorization on your own account. "
- );
- authorizations[blockchain_address][app_id][data_category].access_period_start_time = begin_date;
- authorizations[blockchain_address][app_id][data_category].access_period_end_time = end_date;
- emit modify_auth_time_period(blockchain_address, app_id, data_category,
- authorizations[blockchain_address][app_id][data_category].access_period_start_time,
- authorizations[blockchain_address][app_id][data_category].access_period_end_time);
- }
- */
- //查看授權的效期時間
- function get_time_expiration(address blockchain_address, string app_id, string data_category) public returns(uint exp_time) {
- require(
- msg.sender == blockchain_address,
- "You can only get your own account's message. "
- );
- return (authorizations[blockchain_address][app_id][data_category].expiration_time);
- }
- //查看授權的起止時間
- function get_time_period(address blockchain_address, string app_id, string data_category) public returns(uint d_begin, uint d_end) {
- require(
- msg.sender == blockchain_address,
- "You can only get your own account's message. "
- );
- return (authorizations[blockchain_address][app_id][data_category].access_period_start_time, authorizations[blockchain_address][app_id][data_category].access_period_end_time);
- }
- //判斷有無取消授權的事件
- event verify_auth(
- address blockchain_address,
- string app_id,
- string data_category,
- uint now_time,
- uint date_begin,
- uint date_end,
- bool result
- );
- //判斷對APP是否有授權
- function verify_user_authorization(address blockchain_address, string app_id, string data_category, uint date_begin, uint date_end) public returns(bool){
- bool result = false;
- uint now_stamp = now;
- if(now_stamp <= authorizations[blockchain_address][app_id][data_category].expiration_time)
- {
- 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
- && date_begin <= date_end)
- {
- result = true;
- }
- }
- emit verify_auth(blockchain_address, app_id, data_category, now_stamp, date_begin, date_end, result);
- return result;
- }
- //新增address的事件
- event append_address_service (
- string user_id,
- string service_name,
- address blockchain_address
- );
- //在user_id新增一組service_name, blockchain address
- function append_ID_address_service_mapping_table(string user_id, string service_name, address blockchain_address)public{
- //Service_address_table memory table = Service_address_table(service_name, blockchain_address);
- id_mapping_table[user_id].push(Service_address_table(service_name, blockchain_address));
- emit append_address_service(user_id, service_name, blockchain_address);
- }
- //刪除一組service_name, blockchain address的事件
- event delete_address_service (
- string user_id,
- address blockchain_address
- );
- //在一個user id刪除一組service_name, blockchain address
- function delete_ID_address_service(string user_id, address blockchain_address)public /*returns(Service_address_table[] get_deleted_table)*/{
- uint index = id_mapping_table[user_id].length;
- for(uint i = 0; i < index; i++)
- {
- if(id_mapping_table[user_id][i].blockchain_address == blockchain_address)
- {
- //delete id_mapping_table[user_id][i];
- for (uint j = i; j < index-1; j++)
- {
- id_mapping_table[user_id][j] = id_mapping_table[user_id][j+1];
- }
- delete id_mapping_table[user_id][index-1];
- id_mapping_table[user_id].length = id_mapping_table[user_id].length-1;
- }
- }
- //return id_mapping_table[user_id];
- //記錄日誌
- emit delete_address_service(user_id, blockchain_address);
- }
- //得到 ID and blockchain address mapping table
- function get_ID_address_service_mapping_table(string user_id)public returns(Service_address_table[] get_table){
- return id_mapping_table[user_id];
- }
- //上鏈記錄日誌
- event push_chain(
- address blockchain_address,
- string service_name,
- string data_category,
- uint access_period_start_time,
- uint access_period_end_time,
- bytes32 data_hash
- );
- //Meta_data[] private meta_datas;
- //上鏈
- 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 {
- /*meta_datas.push(Meta_data({
- blockchain_address: bc_address,
- service_name: sv_name,
- data_category: dt_category,
- access_period_start_time: p_start_time,
- access_period_end_time: p_end_time,
- data_hash: dt_hash
- }));*/
- emit push_chain(bc_address, sv_name, dt_category, p_start_time, p_end_time, dt_hash);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement