Guest User

Untitled

a guest
Jan 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. pragma solidity >=0.4.21 <0.6.0;
  2.  
  3. contract ChangeOwnership {
  4.  
  5. enum OperationType {PART, PRODUCT}
  6. mapping(bytes32 => address) public currentPartOwner;
  7. mapping(bytes32 => address) public currentProductOwner;
  8.  
  9. event TransferPartOwnership(bytes32 indexed p, address indexed account);
  10. event TransferProductOwnership(bytes32 indexed p, address indexed account);
  11. ProductManagement private pm;
  12.  
  13. constructor(address prod_contract_addr) public {
  14. //Just create a new auxiliary contract. We will use it to check if the part or product really exist
  15. pm = ProductManagement(prod_contract_addr);
  16. }
  17.  
  18. function addOwnership(uint op_type, bytes32 p_hash) public returns (bool) {
  19. if(op_type == uint(OperationType.PART)){
  20. address manufacturer;
  21. (manufacturer, , , ) = pm.parts(p_hash);
  22. require(currentPartOwner[p_hash] == address(0), "Part was already registered");
  23. require(manufacturer == msg.sender, "Part was not made by requester");
  24. currentPartOwner[p_hash] = msg.sender;
  25. emit TransferPartOwnership(p_hash, msg.sender);
  26. } else if (op_type == uint(OperationType.PRODUCT)){
  27. address manufacturer;
  28. (manufacturer, , , ) = pm.products(p_hash);
  29. require(currentProductOwner[p_hash] == address(0), "Product was already registered");
  30. require(manufacturer == msg.sender, "Product was not made by requester");
  31. currentProductOwner[p_hash] = msg.sender;
  32. emit TransferProductOwnership(p_hash, msg.sender);
  33. }
  34. }
  35.  
  36. function changeOwnership(uint op_type, bytes32 p_hash, address to) public returns (bool) {
  37. //Check if the element exists and belongs to the user requesting ownership change
  38. if(op_type == uint(OperationType.PART)){
  39. require(currentPartOwner[p_hash] == msg.sender, "Part is not owned by requester");
  40. currentPartOwner[p_hash] = to;
  41. emit TransferPartOwnership(p_hash, to);
  42. } else if (op_type == uint(OperationType.PRODUCT)){
  43. require(currentProductOwner[p_hash] == msg.sender, "Product is not owned by requester");
  44. currentProductOwner[p_hash] = to;
  45. emit TransferProductOwnership(p_hash, to);
  46. //Change part ownership too
  47. bytes32[6] memory part_list = pm.getParts(p_hash);
  48. for(uint i = 0; i < part_list.length; i++){
  49. currentPartOwner[part_list[i]] = to;
  50. emit TransferPartOwnership(part_list[i], to);
  51. }
  52.  
  53. }
  54. }
  55. }
Add Comment
Please, Sign In to add comment