## 5 Implementation
### Practical Deployment
In this process, the blockchain performs the following validations: (i) it checks whether the entity is registered as a manufacturer since only the manufacturer can add the product to whom the initial ownership is assigned; (ii) it checks whether the product is a new product and not previously added (i.e., if _product_id_ already exists). Among the various mappings used in the smart contract, one maps the _product_id_ to the respective IPFS reference, and the other maps to the critical information about the product. The smart contract also adds transaction details to trace the previous state of a product; for a new product, the previous state is added as NULL, indicating it is the starting point of the product on the blockchain. Once the transaction is completed in the blockchain, the system retrieves the transaction details (step 1.6) and stores them on the backend server for future reference. Algorithm 1 shows the process of adding a product. Figure 4: Sequence diagram [8]. ```
1:\(ID_{p}\leftarrow\text{UniqueID}(P)\)
2:\(MP_{p}\leftarrow\text{GenerateMaterialPassport}(P)\)
3:\(Ref_{IFPS}\leftarrow\text{SendToIPFS}(MP_{p})\)
4:if\(\text{IsManufacturer}()\land\
eg\text{ProductExists}(ID_{p})\)then
5:\(IPFS\_Map[ID_{p}]\gets Ref_{ipfs}\)
6:\(Info\_Map[ID_{p}]\leftarrow\text{CriticalInformation}(P)\)
7:\(TX\_Details\leftarrow\text{BlockchainTransactionDetails}\)
8: StoreTransactionDetails(\(TX\_Details\))
9:return TRUE
10:else
11:if\(\
eg\text{IsManufacturer}()\)then
12:return ERROR(\"Invalid access. Permission denied\")
13:else
14:return ERROR(\"Duplicate Product\")
15:endif
```  
**Algorithm 1** Adding a product  
The second operation deals with a change in ownership. In a supply chain, as a product moves from one owner to another, such changes must be reflected on the blockchain to enable provenance tracking. This process expects two inputs from the user: the _product_id_ and the identifier of the new owner (step 2.1). As the system receives this information, it is sent through the smart contract to the blockchain (steps 2.2 and 2.3). The smart contract performs two validations in this process: it checks whether the products exist on the chain and whether the user who executes the function is the current owner of the product. If there is no matching product on the chain, the process fails. Similarly, only the current owner can transfer the product to another user, and if any other user attempts to perform this action, it results in transaction failure. Along with the user input of _product_id_ and the new owner, the system fetches the previous transaction details of the product and sends them to the smart contract. Adding the previous transaction details with the current state helps to trace the previous state of the product. If the validations fail, the process is aborted, whereas successful transactions return the transaction details (step 2.4), which are stored for future reference. Algorithm 2 describes this process. ```
1:\(ID_{p}\leftarrow\text{UniqueID}(P)\)
2:\(ID_{new\_owner}\leftarrow\text{NewOwner}(P)\)
3:\(TX_{previous\_state}\leftarrow\text{PreviousStateDetails}(P)\)
4:if\(\text{ProductExists}(ID_{p})\land\text{IsoWner}(ID_{p},ID_{caller})\)then
5:\(TX\_Details\leftarrow\text{TransferOwnership}(ID_{p},ID_{new\_owner},TX_{previous\_state})\)
6: StoreTransactionDetails(\(TX\_Details\))
7:return TRUE
8:else
9:if\(\
eg\text{ProductExists}(ID_{p})\)then
10:return ERROR(\"Product does not exist\")
11:else
12:return ERROR(\"Invalid access. Caller is not current owner\")
13:endif
14:endif
```  
**Algorithm 2** Ownership transfer process The third process focuses on retrieving information from the IPFS. Even reading information from the blockchain requires gas expenses in most blockchain platforms. This process permits users to retrieve an MP from the IPFS without the involvement of the blockchain. By providing the _product_id_ (step 3.1), the framework gathers and sends the reference to the IPFS (step 3.2), which returns the MP (steps 3.3 and 3.4).