Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. type document = {
  2. hash: string;
  3. owner: address;
  4. }
  5.  
  6. (*
  7. @signs - Need for finding user storage
  8. @documents -Need for access document by hash
  9. @access -Need for check use access for document
  10. *)
  11. type storage = {
  12. signs : (address, string set) map;
  13. documents : (string, document) map;
  14. access : (address, string set) map;
  15. }
  16.  
  17. let%init storage = {
  18. signs= Map;
  19. documents= Map;
  20. access= Map;
  21. }
  22.  
  23. (*
  24. withdraw access for document can only owner
  25. *)
  26. let%entry removeAccess ((fileHash : string),(user : address)) storage =
  27. let userAddress = Current.sender() in
  28. let document = match Map.find fileHash storage.documents with
  29. | None -> failwith ("Document not found")
  30. | Some document -> document
  31. in
  32.  
  33. if document.owner <> userAddress then failwith("you not owner");
  34.  
  35. let userAccessStorage = match Map.find userAddress storage.access with
  36. | None -> Set
  37. | Some userAccessStorage ->
  38. if not(Set.mem document.hash userAccessStorage) then
  39. failwith ("access not found");
  40. userAccessStorage
  41. in
  42.  
  43. let userAccessStorage = Set.remove fileHash userAccessStorage in
  44. let storage = storage.access <- Map.add user userAccessStorage storage.access in
  45. [],storage
  46.  
  47. (*
  48. add access for document can only owner
  49. *)
  50. let%entry addAccess ((fileHash : string),(user : address)) storage =
  51. let userAddress = Current.sender() in
  52. let document = match Map.find fileHash storage.documents with
  53. | None -> failwith ("Document not found")
  54. | Some document -> document
  55. in
  56.  
  57. if document.owner <> userAddress then failwith("you not owner");
  58.  
  59. let userAccessStorage = match Map.find userAddress storage.access with
  60. | None -> Set
  61. | Some userAccessStorage ->
  62. if Set.mem document.hash userAccessStorage then
  63. failwith ("access already granted");
  64. userAccessStorage
  65. in
  66.  
  67. let userAccessStorage = Set.add fileHash userAccessStorage in
  68. let storage = storage.access <- Map.add user userAccessStorage storage.access in
  69. [],storage
  70.  
  71. let%entry signFile (fileHash : string) storage =
  72. let userAddress = Current.sender() in
  73. let existDocument = Map.mem fileHash storage.documents in
  74. let userSignsStorage = match Map.find userAddress storage.signs with
  75. | None -> Set
  76. | Some userSigns -> userSigns
  77. in
  78.  
  79. if existDocument then
  80.  
  81. let document = match Map.find fileHash storage.documents with
  82. | None -> failwith ("Exist document not found")
  83. | Some document -> document
  84. in
  85.  
  86. if Set.mem document.hash userSignsStorage then failwith "document now signed";
  87.  
  88. if document.owner = userAddress then failwith ("document signed");
  89.  
  90. begin match Map.find userAddress storage.access with
  91. | None -> failwith ("sign not allowed")
  92. | Some access ->
  93. if not(Set.mem document.hash access) then
  94. failwith ("sign not allowed");
  95. end;
  96.  
  97. let userSignsStorage = Set.add document.hash userSignsStorage in
  98. let storage = storage.signs <- Map.add userAddress userSignsStorage storage.signs in
  99. [],storage
  100.  
  101. else
  102.  
  103. let document = { hash = fileHash; owner = userAddress } in
  104. let storage = storage.documents <- Map.add fileHash document storage.documents in
  105. let userSignsStorage = Set.add fileHash userSignsStorage in
  106. let storage = storage.signs <- Map.add userAddress userSignsStorage storage.signs in
  107.  
  108. [],storage
  109.  
  110. let%entry main (fileHash : string) storage =
  111. ([], storage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement