Guest User

Untitled

a guest
Jan 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. pragma solidity >=0.4.21 <0.6.0;
  2.  
  3. contract ProductManagement {
  4. struct Part{
  5. address manufacturer;
  6. string serial_number;
  7. string part_type;
  8. string creation_date;
  9. }
  10.  
  11. struct Product{
  12. address manufacturer;
  13. string serial_number;
  14. string product_type;
  15. string creation_date;
  16. bytes32[6] parts;
  17. }
  18.  
  19. mapping(bytes32 => Part) public parts;
  20. mapping(bytes32 => Product) public products;
  21.  
  22. constructor() public {
  23. }
  24.  
  25. function concatenateInfoAndHash(address a1, string memory s1, string memory s2, string memory s3) private returns (bytes32){
  26. //First, get all values as bytes
  27. bytes20 b_a1 = bytes20(a1);
  28. bytes memory b_s1 = bytes(s1);
  29. bytes memory b_s2 = bytes(s2);
  30. bytes memory b_s3 = bytes(s3);
  31.  
  32. //Then calculate and reserve a space for the full string
  33. string memory s_full = new string(b_a1.length + b_s1.length + b_s2.length + b_s3.length);
  34. bytes memory b_full = bytes(s_full);
  35. uint j = 0;
  36. uint i;
  37. for(i = 0; i < b_a1.length; i++){
  38. b_full[j++] = b_a1[i];
  39. }
  40. for(i = 0; i < b_s1.length; i++){
  41. b_full[j++] = b_s1[i];
  42. }
  43. for(i = 0; i < b_s2.length; i++){
  44. b_full[j++] = b_s2[i];
  45. }
  46. for(i = 0; i < b_s3.length; i++){
  47. b_full[j++] = b_s3[i];
  48. }
  49.  
  50. //Hash the result and return
  51. return keccak256(b_full);
  52. }
  53.  
  54. function buildPart(string memory serial_number, string memory part_type, string memory creation_date) public returns (bytes32){
  55. //Create hash for data and check if it exists. If it doesn't, create the part and return the ID to the user
  56. bytes32 part_hash = concatenateInfoAndHash(msg.sender, serial_number, part_type, creation_date);
  57.  
  58. require(parts[part_hash].manufacturer == address(0), "Part ID already used");
  59.  
  60. Part memory new_part = Part(msg.sender, serial_number, part_type, creation_date);
  61. parts[part_hash] = new_part;
  62. return part_hash;
  63. }
  64.  
  65. function buildProduct(string memory serial_number, string memory product_type, string memory creation_date, bytes32[6] memory part_array) public returns (bytes32){
  66. //Check if all the parts exist, hash values and add to product mapping.
  67. uint i;
  68. for(i = 0;i < part_array.length; i++){
  69. require(parts[part_array[i]].manufacturer != address(0), "Inexistent part used on product");
  70. }
  71.  
  72. //Create hash for data and check if exists. If it doesn't, create the part and return the ID to the user
  73. bytes32 product_hash = concatenateInfoAndHash(msg.sender, serial_number, product_type, creation_date);
  74.  
  75. require(products[product_hash].manufacturer == address(0), "Product ID already used");
  76.  
  77. Product memory new_product = Product(msg.sender, serial_number, product_type, creation_date, part_array);
  78. products[product_hash] = new_product;
  79. return product_hash;
  80. }
  81.  
  82. function getParts(bytes32 product_hash) public returns (bytes32[6] memory){
  83. //The automatic getter does not return arrays, so lets create a function for that
  84. require(products[product_hash].manufacturer != address(0), "Product inexistent");
  85. return products[product_hash].parts;
  86. }
  87. }
Add Comment
Please, Sign In to add comment