<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: capricious_rebel</title>
    <description>The latest articles on DEV Community by capricious_rebel (@capriciousrebel).</description>
    <link>https://dev.to/capriciousrebel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F813167%2F7c28596f-2d72-4846-9b8c-96bd556404cd.jpeg</url>
      <title>DEV Community: capricious_rebel</title>
      <link>https://dev.to/capriciousrebel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/capriciousrebel"/>
    <language>en</language>
    <item>
      <title>
Getting started with Hyperledger Fabric on macOS</title>
      <dc:creator>capricious_rebel</dc:creator>
      <pubDate>Fri, 11 Feb 2022 14:47:52 +0000</pubDate>
      <link>https://dev.to/capriciousrebel/getting-started-with-hyperledger-fabric-on-macos-2937</link>
      <guid>https://dev.to/capriciousrebel/getting-started-with-hyperledger-fabric-on-macos-2937</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Hyperledger&lt;/strong&gt;&lt;br&gt;
"Hyperledger Fabric is a modular blockchain framework that acts as a foundation for developing blockchain-based products using plug-and-play components that are aimed for use within private enterprises" - &lt;a href="https://www.investopedia.com/terms/h/hyperledger-fabric.asp#:~:text=Hyperledger%20Fabric%20is%20a%20modular,for%20use%20within%20private%20enterprises."&gt;Investopedia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Hyperledger&lt;/strong&gt;&lt;br&gt;
While you may have heard of ethereum, polygon and solana being commonly used for blockchain development, these are public blockchains. As public blockchains, all the code, data and activities are available for anyone to access. For large companies like Walmart, a lot of whose code is IP and not publicly available, this maybe a problem.&lt;/p&gt;

&lt;p&gt;Hyperledger allows large enterprises to set up their own copy of the blockchain whether its for just collaboration within a team, within a company or across a fixed set of companies. This gives them more control on their processes within the blockchain.&lt;/p&gt;

&lt;p&gt;Several other benefits of Hyperledger are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Privacy: Hyperledger fabric is a permissioned, private blockchain where organizations can cooperate with other authorized organizations while sharing resources, away from the public view.&lt;/li&gt;
&lt;li&gt;Modularity and Reusability: Everything a developer builds on Fabric is reusable.&lt;/li&gt;
&lt;li&gt;Scalability: Thanks to the plug-and-play approach, scaling an application becomes a delight.&lt;/li&gt;
&lt;li&gt;Support and Credibility: Hyperledger is built under the Linux Foundation, One of the most well-respected communities of developers, and comes with extensive, well-written documentation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;An example of a practical use case: "A B2B Contract"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose business A wants a service from business B, then there is a requirement for a contract to be agreed upon by both the parties. The businesses can join a channel on the fabric network, deploy a chain code that both agree upon, and automate the contract.&lt;/p&gt;

&lt;p&gt;Since the blockchain is permissioned, only the two businesses involved can see the contract and interact with it. At the same time, no third party has access to any sensitive/private business information shared over the blockchain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation Steps&lt;/strong&gt;&lt;br&gt;
In this blog, we will install Hyperledger Fabric on macOS along with &lt;a href="https://github.com/hyperledger/fabric-samples"&gt;Fabric Samples&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://hyperledger-fabric.readthedocs.io/en/release-2.2/channels.html"&gt;Channel&lt;/a&gt; is a private 'subnet' of communication between two or more specific network members. Think of it as a private group on the network.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://hyperledger-fabric.readthedocs.io/en/release-1.3/chaincode.html"&gt;Chaincode&lt;/a&gt; is a program, written in Go, node.js, or Java that runs in a secured Docker container on the network. It manages the ledger and implements business logic.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We will be using fabric-samples/test-network scripts and doing the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a test network.&lt;/li&gt;
&lt;li&gt;Create two example orgs on this test-network.&lt;/li&gt;
&lt;li&gt;Create a channel and have the two orgs join it.&lt;/li&gt;
&lt;li&gt;Deploy a chaincode onto the channel after both the orgs approve it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Install pre-requisites.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Homebrew&lt;/strong&gt;&lt;br&gt;
Install the latest version of &lt;a href="https://brew.sh/"&gt;Homebrew&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Along with Homebrew installation, Xcode cli tools are installed (if not already installed).&lt;/p&gt;

&lt;p&gt;Ensure Homebrew is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew --version
Homebrew 3.3.12
Homebrew/homebrew-core (git revision b81bc29e8a1; last commit 2022-01-24)
Homebrew/homebrew-cask (git revision 7792a6e065; last commit 2022-01-24)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Git&lt;/strong&gt;&lt;br&gt;
Install the latest version of &lt;a href="https://git-scm.com/download/mac"&gt;Git&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew install git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure Git is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$  git --version
git version 2.35.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;cURL&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install the latest version of &lt;a href="https://curl.se/download.html"&gt;cURL&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew install curl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure cURL is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl --version
curl 7.64.1 (x86_64-apple-darwin20.0) libcurl/7.64.1 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.41.0
Release-Date: 2019-03-27
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS GSS-API HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL UnixSockets
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Docker&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Download docker desktop from &lt;a href="https://docs.docker.com/desktop/mac/install/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ensure docker and docker-compose is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker --version
Docker version 20.10.12, build e91ed57
$ docker-compose --version
Docker Compose version v2.2.3 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Go&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;(for Go chaincode or SDK applications)&lt;/em&gt;&lt;br&gt;
Install the latest &lt;em&gt;supported&lt;/em&gt; version of go:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew install go@1.17.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure go is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go version
go1.17.5 darwin/amd64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;JQ&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;(for channel configuration transactions)&lt;/em&gt;&lt;br&gt;
Install the latest version of jq:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew install jq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure jq is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ jq --version
jq-1.6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Install Fabric and Fabric Samples&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Download fabric samples, docker images, and binaries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Make a directory to clone the fabric samples to and cd into it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir -p $HOME/go/src/hyperledger &amp;amp;&amp;amp; cd $HOME/go/src/hyperledger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Download the latest version of fabric samples, docker images and binaries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ curl -sSL https://bit.ly/2ysbOFE | bash -s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Run a test network&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bring up the network along with two example orgs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;cd into the &lt;code&gt;test-network&lt;/code&gt; directory containing the main &lt;code&gt;network.sh&lt;/code&gt; script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd fabric-samples/test-network
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run the following command to bring up a &lt;em&gt;fresh&lt;/em&gt; test-network:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./network.sh down &amp;amp;&amp;amp; ./network.sh up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run the following command to see the running docker-containers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker ps -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see 4 containers with the following names:&lt;br&gt;
&lt;code&gt;hyperledger/fabric-tools:latest&lt;/code&gt;: 1 container&lt;br&gt;
&lt;code&gt;hyperledger/fabric-orderer:latest&lt;/code&gt;: 1 container&lt;br&gt;
&lt;code&gt;hyperledger/fabric-peer:latest&lt;/code&gt;: 2 containers&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create a channel and have the two example orgs join it&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./network.sh createChannel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the following message at the end of the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Channel 'mychannel' joined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Deploy a smart contract on the &lt;br&gt;
network&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Package the smart contract&lt;/strong&gt;&lt;br&gt;
Firstly we want to package our go lang smart contract code into a binary file to deploy it onto the network.&lt;/p&gt;

&lt;p&gt;Go to the folder containing the golang Chaincode and install golang dependencies there, then return to the current directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd ../chaincode/fabcar/go &amp;amp;&amp;amp; GO111MODULE=on go mod vendor &amp;amp;&amp;amp; cd ../../../test-network
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add the following variables to your cli:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export PATH=${PWD}/../bin:${PWD}:$PATH
$ export FABRIC_CFG_PATH=$PWD/../config/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, create the package for the smart contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the following command to ensure that the package has been created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ls | grep fabcar.tar.gz

fabcar.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Install the chaincode package on both the orgs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, let's take up the role of org 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export CORE_PEER_TLS_ENABLED=true
$ export CORE_PEER_LOCALMSPID="Org1MSP"
$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
$ export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
$ export CORE_PEER_ADDRESS=localhost:7051
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the chaincode onto the peer (org1):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode install fabcar.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's take up the role of org 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export CORE_PEER_LOCALMSPID="Org2MSP"
$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
$ export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
$ export CORE_PEER_ADDRESS=localhost:9051
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the chaincode onto the peer (org2):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode install fabcar.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Approve the chaincode on both the orgs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, let's take up the role of org 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export CORE_PEER_TLS_ENABLED=true
$ export CORE_PEER_LOCALMSPID="Org1MSP"
$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
$ export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
$ export CORE_PEER_ADDRESS=localhost:7051
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure that the chaincode is installed on org1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode queryinstalled
Installed chaincodes on peer:
Package ID: fabcar_1:1146b4b491871bf18b23dd67dd8cc058655b36cc0e2274f165ed06b796a8f276, Label: fabcar_1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the Package ID from the result in the previous command, and set the following variable equal to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ CC_PACKAGE_ID=fabcar_1:1146b4b491871bf18b23dd67dd8cc058655b36cc0e2274f165ed06b796a8f276
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Approve the chaincode for org1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's take up the role of org 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export CORE_PEER_LOCALMSPID="Org2MSP"
$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
$ export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
$ export CORE_PEER_ADDRESS=localhost:9051
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure that the chaincode is installed onto org 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode queryinstalled
Installed chaincodes on peer:
Package ID: fabcar_1:1146b4b491871bf18b23dd67dd8cc058655b36cc0e2274f165ed06b796a8f276, Label: fabcar_1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the Package ID from the result in the previous command, and set the following variable equal to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ CC_PACKAGE_ID=fabcar_1:1146b4b491871bf18b23dd67dd8cc058655b36cc0e2274f165ed06b796a8f276
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now approve the chaincode for org2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Commit the chaincode to the channel&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check if the chaincode is ready to be committed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json

{
    "approvals": {
        "Org1MSP": true,
        "Org2MSP": true
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, both the orgs have approved the chaincode. Hence, it is ready to be committed to the channel.&lt;/p&gt;

&lt;p&gt;Commit the chaincode to the channel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check if the chaincode is committed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Committed chaincode definition for chaincode 'fabcar' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Invoke the chaincode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have committed the chaincode onto the channel, finally it's time to have some fun! &lt;br&gt;
Let's invoke various functions of the committed chaincode:&lt;/p&gt;

&lt;p&gt;Call the &lt;code&gt;queryAllCars&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

[]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see by the empty array, there are no cars onto the ledger, so let's try adding some.&lt;/p&gt;

&lt;p&gt;Call the &lt;code&gt;initLedger&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls true --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n fabcar --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"initLedger","Args":[]}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should add some cars to the ledger, let's check it by querying all cars again.&lt;/p&gt;

&lt;p&gt;Call the &lt;code&gt;queryAllCars&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},{"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}},{"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}},{"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}},{"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}},{"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}},{"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}},{"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}},{"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, the cars were added to the ledger which confirms that our smart contract (Chaincode) is working!&lt;/p&gt;

&lt;p&gt;That's it for this tutorial! You can check out more functions in the Chaincode by heading over to &lt;code&gt;../chaincode/fabcar/go/fabar.go&lt;/code&gt; and try invoking/querying them.&lt;/p&gt;

&lt;p&gt;Make sure to run &lt;code&gt;$ ./network.sh down&lt;/code&gt; to kill any containers related to fabric samples once you are done.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;-This article was written by &lt;a href="https://www.linkedin.com/in/capriciousrebel/"&gt;Smith Pereira&lt;/a&gt;, Software Developer at DisruptX&lt;/p&gt;

</description>
      <category>web3</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
