> For the complete documentation index, see [llms.txt](https://rapchan.gitbook.io/hyperledger-food-supply-chain/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rapchan.gitbook.io/hyperledger-food-supply-chain/implementation.md).

# Implementation

## Overview

![](/files/-Mdnuegn_XADP_nhHVAV)

## Prerequisites

The following prerequisites are required to run a Docker-based Fabric test network on your local machine (**001-pre-requisites.sh**).

```bash
# Update your Linux system
$ apt-get update
$ apt-get upgrade

# Install the latest version of git if it is not already installed.
$ sudo apt-get install git

# Install the latest version of cURL if it is not already installed.
$ sudo apt-get install curl

# Optional: Install the latest version of jq if it is not already installed (only required for the tutorials related to channel configuration transactions).
sudo apt-get install jq

# Install Node JS
$ sudo apt-get install nodejs
$ npm
```

### Setting up Docker

```bash
# Install the latest version of Docker if it is not already installed.
$ sudo apt-get -y install docker-compose

# Make sure the Docker daemon is running.
$ sudo systemctl start docker

# Optional: If you want the Docker daemon to start when the system starts, use the following:
$ sudo systemctl enable docker

#Add your user to the Docker group.
$ sudo usermod -a -G docker <username>

$ sudo groupadd docker
$ sudo usermod -aG docker alexandrebarros $USER

$ newgrp docker
$ docker run hello-world
$ docker ps
$ docker ps -a
$ docker images
$ docker logs --tail 20 [processIdNumber]
$ docker restart

# Reboot if still got error
$ reboot
```

### Install Fabric SDK for NodeJS

The Hyperledger Fabric SDK allows applications to interact with a Fabric blockchain network. It provides a simple API to submit transactions to a ledger or query the contents of a ledger with minimal code.

The client API is published to the npm registry in the fabric-network package.

```bash
npm install fabric-network
```

### Install Docker Images, Fabric Tools and Fabric Samples

Clone from Github Hyperledger Fabric Samples.

1. Run Docker on your machine
2. Create a project folder and cd into it

```bash
mkdir new-network
cd new-network
```

1. Run script:

```bash
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.2 1.4.9

# sudo curl -sSL https://goo.gl/6wtTN5 | sudo bash -s 1.1.0
# curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh | bash -s 1.1.0
# sudo chmod 777 -R fabric-samples
```

Now you'll have:

* Fabric-samples downloaded
* Binary tools installed in /bin
* Docker Images downloaded

### Create your first Fabric test network

```bash
cd /fabric-samples/test-network/
./network.sh down
./network.sh up -s couchdb -ca -verbose
```

* Change into the first-network directory and run the generate script that will create the certificates and keys for the entities that are going to exist on our blockchain.
* This will also create the genesis block, the first block on the blockchain, among other things.
* Main script: [byfn.sh](http://byfn.sh/) (well documented and worth reading through)
* Use this to generate cryptographic and network artifacts, bring up the network & run a sample scenario

```bash
# before starting, we need to set a path to the binary tools:
export PATH=../bin:$PATH

cd fabric-samples/first-network

# to create the cryptographic and channel artefacts
sudo ./byfn.sh generate

# to bring up the network and run a scenario using chaincode
sudo ./byfn.sh up

# to stop the network and clean up the system
sudo ./byfn.sh down
```

#### Interact with the network

```bash
cd /fabric-samples/fabcar
~/fabric-samples/fabcar$ ./startFabric.sh javascript
```

### Packages Dependencies for the Backend

```bash
# "fabric-ca-client": "^2.2.7",
npm install --save fabric-ca-client

# "fabric-network": "^2.2.7",
npm install --save fabric-network

# "body-parser": "^1.19.0",
npm install --save body-parser

# "cors": "^2.8.5",
npm install --save cors

# "express": "^4.17.1",
npm install --save express

# "js-yaml": "^4.1.0"
npm install --save js-yaml
```

### Packages Dependencies for the Chaincode

```bash
# fabric-contract-api": "^2.2.0"
npm install --save fabric-contract-api

# "fabric-shim": "^2.2.0"
npm install --save fabric-shim
```

#### Dev Dependencies for TypeScript

```bash
"devDependencies": {
    "@types/chai": "^4.2.11",
    "@types/chai-as-promised": "^7.1.2",
    "@types/mocha": "^7.0.2",
    "@types/node": "^13.9.3",
    "@types/sinon": "^7.5.2",
    "@types/sinon-chai": "^3.2.3",
    "chai": "^4.2.0",
    "chai-as-promised": "^7.1.1",
    "mocha": "^7.1.1",
    "nyc": "^15.0.0",
    "sinon": "^9.0.1",
    "sinon-chai": "^3.5.0",
    "ts-node": "^10.0.0",
    "tslint": "^6.1.3",
    "typescript": "^4.3.4",
    "winston": "^3.2.1"
  },
```
