Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type document = {
- hash: string;
- owner: address;
- }
- (*
- @signs - Need for finding user storage
- @documents -Need for access document by hash
- @access -Need for check use access for document
- *)
- type storage = {
- signs : (address, string set) map;
- documents : (string, document) map;
- access : (address, string set) map;
- }
- let%init storage = {
- signs= Map;
- documents= Map;
- access= Map;
- }
- (*
- withdraw access for document can only owner
- *)
- let%entry removeAccess ((fileHash : string),(user : address)) storage =
- let userAddress = Current.sender() in
- let document = match Map.find fileHash storage.documents with
- | None -> failwith ("Document not found")
- | Some document -> document
- in
- if document.owner <> userAddress then failwith("you not owner");
- let userAccessStorage = match Map.find userAddress storage.access with
- | None -> Set
- | Some userAccessStorage ->
- if not(Set.mem document.hash userAccessStorage) then
- failwith ("access not found");
- userAccessStorage
- in
- let userAccessStorage = Set.remove fileHash userAccessStorage in
- let storage = storage.access <- Map.add user userAccessStorage storage.access in
- [],storage
- (*
- add access for document can only owner
- *)
- let%entry addAccess ((fileHash : string),(user : address)) storage =
- let userAddress = Current.sender() in
- let document = match Map.find fileHash storage.documents with
- | None -> failwith ("Document not found")
- | Some document -> document
- in
- if document.owner <> userAddress then failwith("you not owner");
- let userAccessStorage = match Map.find userAddress storage.access with
- | None -> Set
- | Some userAccessStorage ->
- if Set.mem document.hash userAccessStorage then
- failwith ("access already granted");
- userAccessStorage
- in
- let userAccessStorage = Set.add fileHash userAccessStorage in
- let storage = storage.access <- Map.add user userAccessStorage storage.access in
- [],storage
- let%entry signFile (fileHash : string) storage =
- let userAddress = Current.sender() in
- let existDocument = Map.mem fileHash storage.documents in
- let userSignsStorage = match Map.find userAddress storage.signs with
- | None -> Set
- | Some userSigns -> userSigns
- in
- if existDocument then
- let document = match Map.find fileHash storage.documents with
- | None -> failwith ("Exist document not found")
- | Some document -> document
- in
- if Set.mem document.hash userSignsStorage then failwith "document now signed";
- if document.owner = userAddress then failwith ("document signed");
- begin match Map.find userAddress storage.access with
- | None -> failwith ("sign not allowed")
- | Some access ->
- if not(Set.mem document.hash access) then
- failwith ("sign not allowed");
- end;
- let userSignsStorage = Set.add document.hash userSignsStorage in
- let storage = storage.signs <- Map.add userAddress userSignsStorage storage.signs in
- [],storage
- else
- let document = { hash = fileHash; owner = userAddress } in
- let storage = storage.documents <- Map.add fileHash document storage.documents in
- let userSignsStorage = Set.add fileHash userSignsStorage in
- let storage = storage.signs <- Map.add userAddress userSignsStorage storage.signs in
- [],storage
- let%entry main (fileHash : string) storage =
- ([], storage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement