Solidity How i can return data from hash value

0 votes

I have a solidity contract with two struct and a  mapping table,the question is how I can get data of the struct Parts from hash value?

 Here's the code that I am using:


    struct Part{

        address manufacturer;

        string serial_number;

        string part_type;

        string creation_date;

    }


    struct Product{

        address manufacturer;

        string serial_number;

        string product_type;

        string creation_date;

        bytes32[] parts;

    }


    mapping(bytes32 => Part) public parts;

    mapping(bytes32 => Product) public products;


    function getParts(bytes32 product_hash) public returns (bytes32[6] memory) {}

}


contract ChangeOwnership {


    enum OperationType {PART, PRODUCT}

    mapping(bytes32 => address) public currentPartOwner;

    mapping(bytes32 => address) public currentProductOwner;


    event TransferPartOwnership(bytes32 indexed p, address indexed account);

    event TransferProductOwnership(bytes32 indexed p, address indexed account);

    ProductManagement private pm;


    constructor(address prod_contract_addr) public {


        //contratto pm ausiliario, lo uso per verificare se la part(bottiglia) o il product(cassa/spedizione) esiste

        pm = ProductManagement(prod_contract_addr);

    }

//funzione per aggiungere la proprietà di una part(bottiglia) o prodotto(cassa/spedizione) una volta che sono state prodotte

//successivamente troviamo la funzione per il cambio di propietà

    function addOwnership(uint op_type, bytes32 p_hash) public returns (bool) {

        if(op_type == uint(OperationType.PART)){

            address manufacturer;

            (manufacturer, , , ) = pm.parts(p_hash);

            require(currentPartOwner[p_hash] == address(0), "Part was already registered");

            require(manufacturer == msg.sender, "Part was not made by requester");

            currentPartOwner[p_hash] = msg.sender;

            //emit si utilizza in solidity per emettere l'evento

            emit TransferPartOwnership(p_hash, msg.sender);

        } else if (op_type == uint(OperationType.PRODUCT)){

            address manufacturer;

            (manufacturer, , , ) = pm.products(p_hash);

            require(currentProductOwner[p_hash] == address(0), "Product was already registered");

            require(manufacturer == msg.sender, "Product was not made by requester");

            currentProductOwner[p_hash] = msg.sender;

            emit TransferProductOwnership(p_hash, msg.sender);

        }

    }


    function changeOwnership(uint op_type, bytes32 p_hash, address to) public returns (bool) {

      //comtrollo se l'elemento esiste ed appartiene all utente che ha chiesto il change ownership

              if(op_type == uint(OperationType.PART)){

            require(currentPartOwner[p_hash] == msg.sender, "Part is not owned by requester");

            currentPartOwner[p_hash] = to;

            emit TransferPartOwnership(p_hash, to);

        } else if (op_type == uint(OperationType.PRODUCT)){

            require(currentProductOwner[p_hash] == msg.sender, "Product is not owned by requester");

            currentProductOwner[p_hash] = to;

            emit TransferProductOwnership(p_hash, to);

            //Change part ownership too

            bytes32[6] memory part_list = pm.getParts(p_hash);

            for(uint i = 0; i < part_list.length; i++){

                currentPartOwner[part_list[i]] = to;

                emit TransferPartOwnership(part_list[i], to);

            }


        }

    }

}


Feb 14, 2020 in Blockchain by simone
• 120 points
1,082 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Blockchain

0 votes
1 answer

How can I get the same return value as solidity `abi.encodePacked` in Golang?

This is what I used:  package main import ( ...READ MORE

answered Sep 26, 2018 in Blockchain by slayer
• 29,350 points
3,796 views
0 votes
1 answer

Solidity: How to return id from hash value?

You can do it by storing the hash ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 69,210 points
2,074 views
0 votes
1 answer

How to get return value from solidity contract?

Once you call a constant function, you ...READ MORE

answered Aug 16, 2018 in Blockchain by slayer
• 29,350 points
4,761 views
0 votes
1 answer

Solidity: How can I return a list of user-defined function?

You can do it with something like ...READ MORE

answered Sep 26, 2018 in Blockchain by digger
• 26,740 points
1,137 views
+1 vote
2 answers

Can I include real world data in a smart contract? If so, how?

You cant access/embed real world data using ...READ MORE

answered Jul 18, 2018 in Blockchain by sapan
893 views
+1 vote
1 answer

How can I modify stored data in blockchain??

One of the important feature of a ...READ MORE

answered Apr 18, 2018 in Blockchain by Christine
• 15,790 points
5,733 views
+1 vote
2 answers

How to return value from a chaincode in Hyperledger Fabric?

Hyperledger Fabric supports only 2 types of ...READ MORE

answered Jun 13, 2018 in Blockchain by Perry
• 17,100 points
2,605 views
0 votes
1 answer
0 votes
1 answer

How can I retrieve to and from address in transaction directly from blockchain?

The spending conditions, i.e., who is able ...READ MORE

answered Jul 11, 2018 in Blockchain by Shashank
• 10,400 points
1,030 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP